esolis2014
New member
Hello,
I’ve been working on thinkscript for a while and although I’m no expert, I have managed to patch together a group of strategies to be used primary on /NQ, using Daily Candles (AggregationPeriod.DAY)
My strategy has a sub-routine where it gets the PnL of itself and then calculates how many contracts the strategy can use, based on the margin requirements specified.
My question is: When I use the backtesting (and FloatingPL) for /NQ and 10 year time interval, the reinvest doesn’t seem to work correctly, the FloatingPL results changes each time y press apply.
Is there something that can be done to fix this?
Also feel free to check, use, test and comment the rest of the strategy if there is anything that can be added or modded to make it a better strategy.
Should work fine with /NQ and /ES.
The setup looks like this: https://tos.mx/A7JRCjQ
I’ve been working on thinkscript for a while and although I’m no expert, I have managed to patch together a group of strategies to be used primary on /NQ, using Daily Candles (AggregationPeriod.DAY)
My strategy has a sub-routine where it gets the PnL of itself and then calculates how many contracts the strategy can use, based on the margin requirements specified.
My question is: When I use the backtesting (and FloatingPL) for /NQ and 10 year time interval, the reinvest doesn’t seem to work correctly, the FloatingPL results changes each time y press apply.
Is there something that can be done to fix this?
Also feel free to check, use, test and comment the rest of the strategy if there is anything that can be added or modded to make it a better strategy.
Should work fine with /NQ and /ES.
The setup looks like this: https://tos.mx/A7JRCjQ
Code:
#Designed by esolis 2021
#Credits to:
#Heiken Ashi Strategy:
# Software modified by Pete Hahn for #L.J.Jedrychowski
# Leslie Jedrychowski V 7.0 2grn HA bars after a HA red bar.
#SpectrumBarsLE
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#As soon as I find info on the authors I based this script on, I will update the credits here.
#---------- Strategy Inputs -----------
# Reinvesting parameters
def contracts = 2;
def FPL = FPL();
def MarginReq = 20000;
#Defining reinvesting param.
def reinvestcontracts = rounddown(FPL[1]/MarginReq, 0);
def incrementalFPL = if IsNaN(reinvestcontracts) then incrementalFPL[+1] else reinvestcontracts; #MAX(FPL, minFPL)
def YesOrNoReinvest;
input ReinvestPnL = {default NoReinvestPnL, ReinvestPnL};
#hint ReinvestPnL: Defines if the cumulative profit (PnL) can be reinvested.
switch (ReinvestPnL) {
case NoReinvestPnL:
YesOrNoReinvest = contracts;
case ReinvestPnL:
YesOrNoReinvest = Max (incrementalFPL, contracts);
}
def tradeSize = YesOrNoReinvest;
#--------------------------------------
#-----esolis 50 or 200 day SMA limit---
def averageType = AverageType.SIMPLE;
def SMAPrice = FundamentalType.CLOSE;
input lengthSMAday = 50; #It can be 200 too
def aggregationPeriod = AggregationPeriod.DAY;
#def SMA200day = MovingAverage(averageType, SMAPrice, length200day);
def DailySMA = Average(fundamental(SMAPrice, period = aggregationPeriod), lengthSMAday);
#--------------------------------------
#--------- Strategy Open Selection-----
#SpectrumBarsLE Buy Strategy
def lengthSpectrumBarsLE = 5;
def buySpectrumBarsLE = close > close[lengthSpectrumBarsLE];
#Heiken_Ashi Buy Strategy
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def greenCandle = haClose > haOpen;
#def redCandle = haClose < haOpen;
#Heiken_Ashi Buy Strategy Original
def trendUp = greenCandle and greenCandle[1] and !greenCandle[2];
#def trendDown = redCandle and redCandle[1] and !redCandle[2]; #not used here
#Heiken_Ashi Buy Strategy Modified by esolis
def trendUp_mod = greenCandle[0]; #Added by esolis
#def trendDown_mod = redCandle[0]; #Added by esolis #not used here
#ReversalCandleOpen
def redbar = open > close;
def buyReversalCandle =
# (open[2] > close[2] and close[1] > high[2])
# or (open[3] > close[3] and open[2] < close[2] and close[2] < open[3] and close[1] > (high[3] or high[2]));
(open[1] > close[1] and close > high[1]) or
(open[2] > close[2] and close > high[2]) or
(open[3] > close[3] and close > high[3]) or
(open[4] > close[4] and close > high[4]) or
(open[5] > close[5] and close > high[5]);
#def price = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Max(high, price[1]) else entryPrice;
# if (open[1] > close[1] and high[0] > high[1], yes,
# if (open[2] > close[2] and high[0] > high[2], yes,
# if (open[3] > close[3] and high[0] > high[3], yes,
# if (open[4] > close[4] and high[0] > high[4], yes,
# if (open[5] > close[5] and high[0] > high[5], yes, no)))));
#def reversalCandleBuyPrice =
# if (open[1] > close[1] and high > high[1], high[1],
# if (open[2] > close[2] and high > high[2], high[2],
# if (open[3] > close[3] and high > high[3], high[3],
# if (open[4] > close[4] and high > high[4], high[4],
# if (open[5] > close[5] and high > high[5], high[5], open[-1])))));
input OpenStrategy = {default SpectrumBarsLE, GreenHeikenAshi_Original, GreenHeikenAshi_Modified, ReversalCandleOpen};
def buyStratSignal;
def stratBuyPrice;
def buyPrice = stratBuyPrice;
switch (OpenStrategy) {
case SpectrumBarsLE:
buyStratSignal = buySpectrumBarsLE;
stratBuyPrice = open[-1];
case GreenHeikenAshi_Original:
buyStratSignal = trendUp; #Heiken_Ashi trendUp_Original
stratBuyPrice = open[-1];
case GreenHeikenAshi_Modified:
buyStratSignal = trendUp_mod; #Heiken_Ashi trendUp_Modified by esolis
stratBuyPrice = open[-1];
case ReversalCandleOpen:
buyStratSignal = buyReversalCandle; #buySigReversal
stratBuyPrice = open[-1];
}
input SMAbuyBlock = {default No_SMA, With_SMA};
def buySignal;
switch (SMAbuyBlock) {
case No_SMA:
buySignal = buyStratSignal;
case With_SMA:
buySignal = buyStratSignal and (close[1] > DailySMA); #for v1
}
AddOrder(OrderType.BUY_TO_OPEN, buySignal[0], price = buyPrice, tradeSize = tradeSize, tickcolor = GetColor(1), name = "buy", arrowcolor = GetColor(1));
AddLabel(1, "Buy Strategy = " + OpenStrategy + " - " + SMAbuyBlock, Color.LIGHT_GREEN);
#--------------------------------------
#--------- Strategy Close Selection----
#Heiken_Ashi Close Strategy
def haRedClose = ohlc4;
def haRedOpen = if haRedOpen[1] == 0 then haRedClose[1] else (haRedOpen[1] + haRedClose[1]) / 2;
def redCandleClose = haRedClose < haRedOpen;
input CloseStrategy = {default ReversalCandle, RedHeikenAshi, InsideBarExit};
def closeSignal;
def stratClosePrice;
def sellPrice = stratClosePrice;
switch (CloseStrategy) {
case ReversalCandle: #I know this might repaint but it works somehow.
closeSignal = close[0] < low[1]; # Low[-1] #close[0] < low[1]; #closeSigReversalCandle
stratClosePrice = open[-1]; #low[0]
case RedHeikenAshi:
closeSignal = redCandleClose and redCandleClose[1] and !redCandleClose[2]; #Heiken_Ashi trendDown
stratClosePrice = open[-1];
case InsideBarExit:
closeSignal = high < high[1] and low > low[1] and close < open ; #sellInsideBarExit
stratClosePrice = open[-1];
}
AddOrder(OrderType.SELL_TO_CLOSE, closeSignal[0], price = sellPrice, name = "close", CreateColor(249, 233, 167));
AddLabel(2, "Close Strategy = " + CloseStrategy, Color.LIGHT_ORANGE );
#--------------------------------------
#----------- Strategy Win/Loss Ratio---
#This plots a label to identify the win/loss ratio
def longPrice = CompoundValue(1,
if !longPrice[1] && buySignal[-1] then buyPrice
else if longPrice[1] && closeSignal[-1] then 0
else longPrice[1]
, 0
);
def shortPrice = CompoundValue(1,
if !shortPrice[1] && closeSignal[-1] then sellPrice
else if shortPrice[1] && buySignal[-1] then 0
else shortPrice[1]
, 0
);
def wincntr = CompoundValue(1,
if longPrice[1] && closeSignal[-1] && sellPrice > longPrice[1] then wincntr[1] + 1
else if shortPrice[1] && buySignal[-1] && shortPrice[1] > buyPrice then wincntr[1] + 1
else wincntr[1]
, 0
);
def winNum = if IsNaN(wincntr) then winNum[1] else wincntr ;
def losscntr = CompoundValue(1,
if longPrice[1] && closeSignal[-1] && sellPrice <= longPrice[1] then losscntr[1] + 1
else if shortPrice[1] && buySignal[-1] && shortPrice[1] <= buyPrice then losscntr[1] + 1
else losscntr[1]
, 0
);
def lossNum = if IsNaN(losscntr) then lossNum[1] else losscntr ;
AddLabel(1,
Concat( "Win ratio: ",
Concat( Round( 100 * winNum / (winNum + lossNum), 1),
Concat( "% of ", winNum + lossNum))),
Color.CYAN);
AddLabel(2,
Concat( "Contracts: ",
Concat( reinvestcontracts,
Concat( " est.", " Qty."))),
Color.CYAN);
#--------------------------------------
#--------------------------------------
Last edited: