NexusFi: Find Your Edge


Home Menu

 





Pause calculation When Position is Open-Backtesting


Discussion in ThinkOrSwim

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




 
Search this Thread

Pause calculation When Position is Open-Backtesting

  #1 (permalink)
 apmodeler 
Boise, Idaho
 
Experience: Intermediate
Platform: TOS
Trading: Stocks
Posts: 37 since Aug 2013
Thanks Given: 56
Thanks Received: 9

Hi All,

I'm trying to create a strategy with a simple buy condition (close crosses above EMA 34) and with an exit based on meeting a target or hitting stop loss. The strategy however recaculated the Target and stop loss each time the buy criteria is met even when an active position is open. Is there a way to pause the Target/stop loss calculation when there is an open position?
The script is posted below.
Any help is appreciated.
Thanks!
AP




def ATR = MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), 14);
def EMA34 = ExpAverage(close, 34);

#BuySignal
def Buy = Close Crosses above EMA34;
# Buy Target
def BuyTarget = if (Buy) then open[-1] + 5*ATR else BuyTarget[1] ;
Plot BT=BuyTarget;
# Buy StopLoss
def BuyStopLoss = if (Buy) then open[-1]-2*ATR else BuyStopLoss[1];
Plot BSL=BuyStopLoss;

#Orders
AddOrder(OrderType.BUY_TO_OPEN, Buy and tradinghours==1, open[-1], tradeSize = 1, tickcolor = GetColor(1), arrowcolor = GetColor(1), name="Buy");
AddOrder(OrderType.SELL_TO_CLOSE, (low crosses below BuyStopLoss) or flatatclose==1, BuyStopLoss, 1, tickcolor = GetColor(1), arrowcolor = GetColor(2));
AddOrder(OrderType.SELL_TO_CLOSE, (high crosses above BuyTarget) or flatatclose==1, open[-1], 1, tickcolor = GetColor(1), arrowcolor = GetColor(3));

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Cheap historycal L1 data for stocks
Stocks and ETFs
Quant vue
Trading Reviews and Vendors
What broker to use for trading palladium futures
Commodities
REcommedations for programming help
Sierra Chart
Better Renko Gaps
The Elite Circle
 
  #2 (permalink)
tradecombine
SAN ANTONIO
 
Posts: 9 since Feb 2021
Thanks Given: 11
Thanks Received: 6

Try this technique:
 
Code
def BuyTarget = if (Buy) AND isNaN(EntryPrice())
                        then open[-1] + 5*ATR 
                        else BuyTarget[1] ;

def BuyStopLoss = if (Buy) AND  isNaN(EntryPrice())
                             then open[-1]-2*ATR 
                             else BuyStopLoss[1];

If condition: (Buy) AND condition (EntryPrice() does not exist)
then, set your variable
else, use the prior value for your variable.

Rationale:
The EntryPrice() function is only populated while you're in a trade, so if it's not set, then you're not in a trade, and free to update the variable. If you're already in a trade, your AND condition will evaluate to false, and you'll use the prior value for the variable.

Reference:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/EntryPrice
https://usethinkscript.com/threads/dont-plot-if-already-in-a-trade.7525/

Reply With Quote
Thanked by:
  #3 (permalink)
 apmodeler 
Boise, Idaho
 
Experience: Intermediate
Platform: TOS
Trading: Stocks
Posts: 37 since Aug 2013
Thanks Given: 56
Thanks Received: 9


Thank you so much! It is working!

Started this thread Reply With Quote




Last Updated on October 29, 2021


© 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