NexusFi: Find Your Edge


Home Menu

 





Everything else works; TOS ThinkScript AddOrder does not.


Discussion in ThinkOrSwim

Updated
    1. trending_up 3,495 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 2 attachments




 
Search this Thread

Everything else works; TOS ThinkScript AddOrder does not.

  #1 (permalink)
gmftrs
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");

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Increase in trading performance by 75%
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
Vinny E-Mini & Algobox Review TRADE ROOM
13 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #2 (permalink)
 Zogger99 
Kannapolis, NC
 
Experience: Beginner
Platform: TS, TOS, Quantower
Broker: TOS, AMP, TastyWorks
Trading: Emini ES, Currency Futures, Options
Posts: 5 since Apr 2019
Thanks Given: 884
Thanks Received: 1

Not sure if you figured this out,

I uncommented the addOrder lines and it looks like it worked fine.

I wonder if you have the Strategy set to plot the trades?
When you click the gear for the strategy, in the upper right there is a check box for Show Study - make sure it's checked off.

Attached Thumbnails
Click image for larger version

Name:	image_822.png
Views:	350
Size:	88.0 KB
ID:	303160   Click image for larger version

Name:	image_443.png
Views:	288
Size:	114.9 KB
ID:	303161  
Reply With Quote




Last Updated on July 20, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts