Pause calculation When Position is Open-Backtesting - futures io
futures io



Pause calculation When Position is Open-Backtesting


Discussion in ThinkOrSwim

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Pause calculation When Position is Open-Backtesting

(login for full post details)
  #1 (permalink)
 apmodeler 
Boise, Idaho
 
Experience: Intermediate
Platform: TOS
Trading: Stocks
 
Posts: 33 since Aug 2013
Thanks: 52 given, 9 received

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 futures io?
how to avoid these type of trades in algo
NinjaTrader
NT 8 Strateg Builder issue I am baffled! Anyone?
NinjaTrader
Google Keyword Search Alerts
Traders Hideout
Store value in variable when emas crossover easy language
EasyLanguage Programming
Brad Cowan
Trading Reviews and Vendors
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Why dont people trade 150 ES contracts?
40 thanks
Spoo-nalysis ES e-mini futures S&P 500
36 thanks
Any long term success stories from funded traders in the …
26 thanks
Am I finally on the right path?
18 thanks
Daytrading.Coach Review
17 thanks
 
(login for full post details)
  #2 (permalink)
tradecombine
SAN ANTONIO
 
 
Posts: 9 since Feb 2021
Thanks: 11 given, 5 received

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
The following user says Thank You to tradecombine for this post:
 
(login for full post details)
  #3 (permalink)
 apmodeler 
Boise, Idaho
 
Experience: Intermediate
Platform: TOS
Trading: Stocks
 
Posts: 33 since Aug 2013
Thanks: 52 given, 9 received


Thank you so much! It is working!

Started this thread Reply With Quote


futures io Trading Community Platforms and Indicators ThinkOrSwim > Pause calculation When Position is Open-Backtesting


Last Updated on October 29, 2021


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
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.
no new posts