Hello, I need some code help. This can be applied to the bottom of any strategy and is super helpful.
I was surfing this site the past couple days and came across a posters strategy he was using that contained some really nice P&L info.
https://usethinkscript.com/threads/...acktesting-data-utility-for-thinkorswim.1624/
I'm able to use this in all my strategies to give me some good insight. One thing is was missing, a max loss count and a max draw down in dollars. I'm trying to modify this but after several hours I'm stumped. Can someone take a look and see if you can tell me what I'm doing wrong. I noted the area I'm working in with "My Add". It always returns 1, I thought I was incrementing based on losers in a row, but apparently not. I have not started on the Max Drawdown label yet. Thanks in advance
Regards, Kevin
I was surfing this site the past couple days and came across a posters strategy he was using that contained some really nice P&L info.
https://usethinkscript.com/threads/...acktesting-data-utility-for-thinkorswim.1624/
I'm able to use this in all my strategies to give me some good insight. One thing is was missing, a max loss count and a max draw down in dollars. I'm trying to modify this but after several hours I'm stumped. Can someone take a look and see if you can tell me what I'm doing wrong. I noted the area I'm working in with "My Add". It always returns 1, I thought I was incrementing based on losers in a row, but apparently not. I have not started on the Max Drawdown label yet. Thanks in advance
Code:
### Use this below any script to give Win/Loss info ###
input showStats = yes;
#Globals
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
#Inputs
input fplTargetWinLoss = .50;
#hint fplTargetWinLoss: sets the target winlossRatio (in percent) which determines display colors of the W/L label.
input fplTargetWinRate = 1;
#hint fplTargetWinRate: sets the target winRate (float) which determines display colors of the WinRate label;
input fplHidePrice = no;
#hint fplHidePrice: hide's the underlying price graph. \nDefault is no.
input fplHideFPL = yes;
#hint fplHideFPL: hide's the underlying P&L graph.\nDefault is yes.
#temp input var references
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def hidePrice = fplHidePrice;
def hideFPL = fplHideFPL;
#hide chart candles
HidePricePlot(hidePrice);
#Plot default Floating P&L
plot FPL = FPL();
FPL.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
FPL.DefineColor("Positive and Up", Color.GREEN);
FPL.DefineColor("Positive and Down", Color.DARK_GREEN);
FPL.DefineColor("Negative and Down", Color.RED);
FPL.DefineColor("Negative and Up", Color.DARK_RED);
FPL.AssignValueColor(if FPL >= 0
then if FPL > FPL[1]
then FPL.Color("Positive and Up")
else FPL.Color("Positive and Down")
else if FPL < FPL[1]
then FPL.Color("Negative and Down")
else FPL.Color("Negative and Up"));
FPL.SetHiding(hideFPL);
plot ZeroLine = if IsNaN(close)
then nan
else 0;
ZeroLine.SetDefaultColor(Color.GRAY);
ZeroLine.SetHiding(hideFPL);
#Global Scripts
script incrementValue {
input condition = yes;
input increment = 1;
input startingValue = 0;
def _value = CompoundValue(1,
if condition
then _value[1] + increment
else _value[1], startingValue);
plot incrementValue = _value;
}
;
# Entry Calculations. Note: Only parses on a Strategy Chart
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry)
then entry
else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
#is active trade
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def fplreturn = (FPL - FPL[1]) / FPL[1];
def cumsum = Sum(fplreturn);
def highBarNumber = CompoundValue(1, if FPL == highFPL
then bn
else highBarNumber[1], 0);
def lowBarNumber = CompoundValue(1, if FPL == lowFPL
then bn
else lowBarNumber[1], 0);
#Win/Loss ratios
def entryBarsTemp = if hasEntry
then bn
else nan;
def entryBarNum = if hasEntry and isNewEntry
then bn
else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar
then FPL
else entryBarPL[1];
def exitBarsTemp = if !hasEntry
and bn > entryBarsTemp[1]
then bn
else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1])
then bn
else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar
then FPL
else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
#Counts
def entryCount = incrementValue(entryFPLAll);
def winCount = incrementValue(isWin);
def lossCount = incrementValue(isLoss);
def highestReturn = if entryReturnWin[1] > highestReturn[1]
then entryReturnWin[1]
else highestReturn[1];
def lowestReturn = if entryReturnLoss[1] < lowestReturn[1]
then entryReturnLoss[1]
else lowestReturn[1];
def winRate = winCount / lossCount;
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;
# My Add -----------------------------------------
# This is the area I can't seem to understand what I'm doing wrong
def tmpMaxLossCount = if isLoss then tmpMaxLossCount[1] + 1 else 0;
def maxLossCount = if tmpMaxLossCount > tmpMaxLossCount[1] then tmpMaxLossCount else MaxLossCount[1];
AddLabel(showStats,
text = "Max Loss Count: " + maxLossCount +
" ",
color = color.ORANGE
);
# End My Add -------------------------------------
#Labels
AddLabel(showStats,
text = "Total Trades: " + entryCount + " ",
color = Color.WHITE
);
AddLabel(showStats,
text = "WinCount: " + winCount +
" | LossCount: " + lossCount +
" | WinRate: " + round(winRate,2) + " ",
color = if winRate >= targetWinRate
then Color.CYAN
else Color.MAGENTA
);
AddLabel(showStats,
text = "W/L: " + AsPercent(winLossRatio) + " ",
color = if winLossRatio > targetWinLoss
then Color.CYAN
else Color.MAGENTA
);
AddLabel(showStats,
text = "AvgReturn: " + AsDollars(avgReturn) +
" | AvgLoss: " + AsDollars(avgLoss) +
" | BiggestLoss: " + AsDollars(lowestReturn) +
" | AvgWin: " + AsDollars(avgWin) +
" | HighestWin: " + AsDollars(highestReturn) + " ",
color = if avgReturn >= 0
then Color.CYAN
else Color.MAGENTA
);
AddLabel(showStats,
text = "Total Profit: " + AsDollars(FPL) + " ",
color = if FPL > 0
then Color.CYAN
else Color.MAGENTA
);
Regards, Kevin
Last edited by a moderator: