ColoradoSprings Colorado/USA
Posts: 2 since Sep 2019
Thanks Given: 0
Thanks Received: 1
|
I need some help here. I want to backtest a strategy. Below is my TOS ThinkScript code to mark a trade entry and then exit the trade either by hitting the stop loss or the profit target. I cannot get the trade to mark an open price bar, let alone a close.
When the AddOrder code is commented out, the script accurately marks the price chart with an UP arrow where the trade should open. Also, the AddLabel command shows at the top of the chart. However, when the AddOrder code is active, then the AddLabel and the UP arrow does not show - and neither does the script mark the open.
You’ll notice numerous commented out lines. I use these for debugging purposes and, when individually active, they all display on the price chart. So I know everything is working up to the AddOrder code.
It’s my understanding that, if the script is working properly, I should be able to see where the trade begins and where it ends on the price chart when the code is applied to the Price area within the Edit Studies window. Can you spot where I am in error? Thanks for any feedback anyone can provide. Here’s my script in its entirety…
input atrMult = .25;
input atrLength = 20; #Average this many prev candles to get the avg length
input PctCandleBodySize = .25; #Candle body must be at least this % of it's range.
input TrendLength = 5; #Look back this far for downtrend confirmation
#Do we have a downtrend coming into the pattern? Check the slope using the Typical Price.
def CalcSlope = (hlc3 - hlc3[TrendLength]) / TrendLength;
def DownTrendOK = if CalcSlope < 0 then yes else no;
#For debug purposes...
#plot DowntrendValue = downTrendOK;
#DowntrendValue.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Qualify the 1st candle.
def Candle1Down = if open[2] > close[2] then yes else no;
def Candle1BodySizeOK = AbsValue(open[2] - close[2]) / AbsValue(high[2] - low[2]) >= PctCandleBodySize;
def Candle1OK = if Candle1Down then yes else no;
#For debug purposes...
#plot Candle1Value = Candle1OK;
#Candle1Value.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Qualify the 2nd candle...
def Candle2Up = if open[1] < close[1] then yes else no;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax[1] > BodyMax[2] and BodyMin[1] < BodyMin[2];
#def Candle2Engulf = if open[1] < close[2] and close[1] > open[2] then YES else NO;
def Candle2BodySizeOK = AbsValue(open[1] - close[1]) / AbsValue(high[1] - low[1]) >= PctCandleBodySize;
def Candle2OK = if Candle2Up and IsEngulfing then yes else no;
#For debug purposes...
#plot Candle2Value = Candle2OK;
#Candle2Value.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Qualify the third candle...
def Candle3SizeOK = TrueRange(high = high, close = close, low = low) > ATR(length = atrLength, "average type" = "SIMPLE") * atrMult;
def Candle3UpDayOK = close > open ; #Needs to be an UP candle
def Candle3BodySizeOK = AbsValue(open - close) / AbsValue(high - low) >= PctCandleBodySize;
def Candle3GapOK = if open > close[1] then yes else no;
def Candle3OK = if Candle3SizeOK and Candle3UpDayOK and Candle3GapOK then yes else no;
#For debug purposes...
#plot Candle3Value = Candle3OK;
#Candle3Value.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Arrow marks the spot...
def MeetsCriteria = if Candle1OK + Candle2OK + Candle3OK == 3 then yes else no; # + DowntrendOK; MeetsCriteria set to value 1;
#No need to plot MeetsCriteria arrow
#def MarkThisBar = if MeetsCriteria ==1 then 1 else 0;
plot MarkThisBar = if MeetsCriteria ==1 then 1 else 0;
MarkThisBar.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
AddLabel(yes, "Arrows point to a Downside Reversal marked by an engulfing UP candle followed by an upward gap. BUY at the gap w/pos Mkt Tone while coming off support.", Color.GREEN);
#Profit Tracking code
#Define the trade plan - numeric
def StopLoss = if MeetsCriteria then low[1] else StopLoss[1]; #The low of the engulfing candle
def BuyPrice = if MeetsCriteria then open else BuyPrice[1]; #The open of the gapping candle
def ProfitTarget = if MeetsCriteria then (BuyPrice * 1.025) else ProfitTarget[1]; #Target will be 2.50% of the BuyPrice
#For debug purposes...
#plot PriceIs = if MeetsCriteria then ProfitTarget else double.Nan;
#PriceIs.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
#Open BUY and SELL signals
def buy = if MeetsCriteria then yes else no;
def StopHit = close <= StopLoss;
def ProfitHit = close >= ProfitTarget;
def SellLoss = if StopHit then close else SellLoss[1];
def SellProfit = if ProfitHit then close else SellProfit[1];
#This is where the profit/loss values would be calculated
#AddOrder(OrderType.BUY_TO_OPEN, buy, open, tradeSize = 1, tickcolor = Color.GRAY, arrowcolor = Color.GRAY);
#AddOrder(OrderType.SELL_TO_CLOSE, ProfitHit, SellProfit, tradeSize = 1, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Profit");
#AddOrder(type = OrderType.SELL_TO_CLOSE, StopHit, SellLoss, tradeSize = 1, tickcolor = Color.RED, arrowcolor = Color.RED, name = "Loss");
|