Ok, I think I have this fairly accurate regardless of timeframe and whether it's looking at stocks or futures. If you have a study or a strategy you want to test without actually coding a strategy with buy sell orders, you can add this at the bottom of your script and simply define your buy and sell signals (and stops if you wish). It will produce Labels that show total profit for the time frame, along with winners and losers, etc. Pic below
Copy this script BELOW your script and just update the BuySignal and SellSignal variables with your conditions for each. That should be all that is needed. Feel free to find and make corrections. This does not include any fees that might be associated with the trade, so those would need to be subtracted out.
Copy this script BELOW your script and just update the BuySignal and SellSignal variables with your conditions for each. That should be all that is needed. Feel free to find and make corrections. This does not include any fees that might be associated with the trade, so those would need to be subtracted out.
Ruby:
###------------------------------------------------------------------------------------------
input showSignals = yes;
input showLabels = yes;
input showBubbles = yes;
input useStops = no;
############################################
## Create Signals - FILL IN THIS SECTION
##
## IF BuySignal, SellSignal, BuyStop, SellStop are defined above
## comment out the definitions below
##
############################################
def BuySignal = 0<0; # insert condition to create long position
def SellSignal = 0<0; # insert condition to create short position
def BuyStop = if !useStops then 0 else if 0<0 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if 0>0 then 1 else 0 ; # insert condition to stop in place of the 0>0
###------------------------------------------------------------------------------------------
#######################################
## Maintain the position of trades
#######################################
def CurrentPosition; # holds whether flat = 0 long = 1 short = -1
if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
CurrentPosition = 0;
}else{
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal){
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal){
CurrentPosition = -1;
} else if (BuyStop){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal){
CurrentPosition = 1;
} else if (SellStop){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}
def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;
Plot BuySig = if (!isLong[1] and BuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.yellow);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(3);
Alert(BuySig, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig, "Buy Signal",Alert.bar,sound.Ding);
Plot SellSig = if (!isShort[1] and SellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.white);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(3);
Alert(SellSig, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig, "Sell Signal",Alert.bar,sound.Ding);
Plot BuyStpSig = if (BuyStop and isLong[1] and showSignals) then 1 else 0;
BuyStpSig.AssignValueColor(color.gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);
Plot SellStpSig = if (SellStop and isShort[1] and showSignals) then 1 else 0;
SellStpSig.AssignValueColor(color.gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);
#######################################
## Orders
#######################################
def isOrder = if CurrentPosition == CurrentPosition[1] then 0 else 1; # Status changed so it's a new order
def orderPrice = if (isOrder and (BuySignal or SellSignal)) then close[-1] else orderPrice[1];
#######################################
## Price and Profit
#######################################
def profitLoss;
if (!isOrder or orderPRice[1]==0){
profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
profitLoss = orderPrice[1] - close;
} else {
profitLoss = 0;
}
# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);
# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);
# Total Orders
def orderCount = (profitWinners+profitLosers+profitPush);
# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;
# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());
# Might be redundant = dollarProfitLoss
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());
# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(isOrder) then 0 else if isOrder and isLong then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(isOrder) then 0 else if isOrder and isShort then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and isLong then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and isShort then countShort[1]+1 else countShort[1],0);
# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);
# What percent were winners
def PCTWin = round((profitWinners/orderCount)*100,2);
#######################################
## Create Labels
#######################################
AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels, "Orders: " + orderCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);
AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels then 1 else 0, "Current: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);
#######################################
## Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AssignPriceColor(if CurrentPosition == 1 then color.green else if CurrentPosition == -1 then color.red else color.gray);