• Get $40 off VIP by signing up for a free account! Sign Up

AddOrders executing 2 bars after event instead of 1?

Hey guys and gals,

I've been backtesting a new strategy and for some reason my orders are not executing on the 1st bar after the conditions are satisfied, but instead skipping a bar and executing 2 bars after the event. What makes it stranger is that sometimes it executes like it normally does and buys/sells on the bar after the conditions are satisfied, but this is a small percentage of the time.

I've narrowed the script down to be extremely simple (pardon my hiding the key variables for confidentiality) for testing:

def x= if high("XYZ") >= # then 1 else 0;
def y= if low("XYZ") <= # then 1 else 0;

AddOrder(OrderType.BUY_AUTO, x== 1, close, 300, tickcolor = GetColor(1), arrowcolor = GetColor(1));

AddOrder(OrderType.SELL_AUTO, y== 1, close, 300, tickcolor = GetColor(2), arrowcolor = GetColor(2));

This is literally it... no offsets, nothing. Buy when x==1 it doesn't buy on the next bar, but the following. Anyone else experienced this or know what might be causing it?
@TraderKevin instead of BUY on close, try open[-1]

This had no effect on which candle the buy/sell order executed, only on the price that it executed at. This is to be expected... that parameter of the addorder function only controls the price when backtesting I believe. If you put 100 in there, the buy/sell price will always be 100, no matter what's happening with the candle or the rest of the strategy, and won't change the candle the addorder function executes on.
Upon further testing, I've noticed that when backtesting the strat over the last 30 days, before June 26th it adds an extra candle before executing the buy/sell, but after June 26th it acts normally (buys/sells on the bar following the condition being met). So weird!

Any ideas?

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
634 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.