Strategy orders a bar late?

mark.917

Member
VIP
Taking a stab at my first strategy and have a question. The same conditions that draw the "setup" vertical line (dashed green in the picture), are used to open an order. However, the order gets opened a bar later than I would expect.

In this picture, I would expect the order to have been opened on the first red bar after the green vertical line.


1693059067944.png


AddVerticalLine
Code:
AddVerticalLine(show_setups and bullish_2m_atr_cross and !bullish_2m_atr_cross[1] and bullish_5m_atr and (bullish_15m_atr or bullish_tmo),"Buy", if ADX_20_plus then Color.GREEN else if ADX_25_plus then Color.DARK_GREEN else Color.YELLOW, Curve.SHORT_DASH);

AddOrder
Code:
AddOrder(OrderType.BUY_TO_OPEN, trade_setups and ((bullish_2m_atr_cross and !bullish_2m_atr_cross[1] and bullish_5m_atr and (bullish_15m_atr or bullish_tmo)) or (bullish_5m_atr_cross and !bullish_5m_atr_cross[1] and bullish_2m_atr and bullish_tmo)));
 
Solution
Thank you for that, it gives me a place to look.

Not to belabor the point but please help me understand the "repainting" part when the value of the previous bar closing above the ATR TS will not change.
Nothing about your study is repainting.
Your study is excellent, thank you for sharing.

The issue is not you. It is ToS.
You want to use your study in a strategy.
Strategies have many limitations.
The major issue is how it calculates the P&L.

It is NOT possible to capture the moment that you "smash the Buy Mkt button."
Your vertical line occurs BEFORE the bar that creates it closes.
BUT BUT BUT the P&L results are calculated AFTER the bar closes.
Therefore, it is not possible to capture accurate P&L results ...
Taking a stab at my first strategy and have a question. The same conditions that draw the "setup" vertical line (dashed green in the picture), are used to open an order. However, the order gets opened a bar later than I would expect.

In this picture, I would expect the order to have been opened on the first red bar after the green vertical line.


View attachment 19590

AddVerticalLine
Code:
AddVerticalLine(show_setups and bullish_2m_atr_cross and !bullish_2m_atr_cross[1] and bullish_5m_atr and (bullish_15m_atr or bullish_tmo),"Buy", if ADX_20_plus then Color.GREEN else if ADX_25_plus then Color.DARK_GREEN else Color.YELLOW, Curve.SHORT_DASH);

AddOrder
Code:
AddOrder(OrderType.BUY_TO_OPEN, trade_setups and ((bullish_2m_atr_cross and !bullish_2m_atr_cross[1] and bullish_5m_atr and (bullish_15m_atr or bullish_tmo)) or (bullish_5m_atr_cross and !bullish_5m_atr_cross[1] and bullish_2m_atr and bullish_tmo)));
No.
That vertical line is associated with the bar that follows. So that bar needs to close and the next bar open for the AddOrder signal to trigger.

If you want to see what bar vertical lines are associated with, put your vertical line logic into a painted arrow logic statement; it will provide a clearer picture.
The addorder will occur the bar after your painted arrow; the soonest that it is possible to take action of that signal.
 

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

No.
That vertical line is associated with the bar that follows. So that bar needs to close and the next bar open for the AddOrder signal to trigger.

If you want to see what bar vertical lines are associated with, put your vertical line logic into a painted arrow logic statement; it will provide a clearer picture.
The addorder will occur the bar after your painted arrow; the soonest that it is possible to take action of that signal.
In realtime, at the first tick of the first red bar in that picture, the condition that the previous bar closed above the ATR TS is true, that's what prints the vertical line (or the arrow if you want), and that is when I smash the Buy Mkt button.

This, "The addorder will occur the bar after your painted arrow; the soonest that it is possible to take action of that signal." sounds like a limitation of the ToS strategy tester but if this is a consistent condition, is there a workaround, like referencing a previous Open price for the orders entry price?
 
In realtime, at the first tick of the first red bar in that picture, the condition that the previous bar closed above the ATR TS is true, that's what prints the vertical line (or the arrow if you want), and that is when I smash the Buy Mkt button.

This, "The addorder will occur the bar after your painted arrow; the soonest that it is possible to take action of that signal." sounds like a limitation of the ToS strategy tester but if this is a consistent condition, is there a workaround, like referencing a previous Open price for the orders entry price?
Totally understand your POV.
You "smash the Buy Mkt button." before the candle that created that vertical line closes.
Many daytraders do; all scalpers do.

That won't work in backtesting. The backtesting results won't be accurate as it is not possible to accurately measure what results are being achieved on "inter-candle trading".

Can you create a strategy that goes back and repaints an arrow on the candle immediately after the vertical line? Sure, you can.
But any backtested results will not reflect reality.

And so future viewers do not get misled; repainting strategies are not posted on the forum.
But for your own personal use, what you are attempting to achieve; it is possible.

WorkAround:
Read the AddOrder documentation.
Where you see open[-1] in the syntax example (which means calculate the non-repainting P/L results on the open of the next candle).
Change it to use the repainting current candle's open or close, etc...

But don't post it to the forum. The results do not reflect reality.
ToS cannot capture the inter-candle moment of when you "smash the Buy Mkt button. It will repaint every tick of that candle; then calculate the P/L based on when that candle closed. NOT when you hit the button.
 
Totally understand your POV.
You "smash the Buy Mkt button." before the candle that created that vertical line closes.
Many daytraders do; all scalpers do.

That won't work in backtesting. The backtesting results won't be accurate as it is not possible to accurately measure what results are being achieved on "inter-candle trading".

Can you create a strategy that goes back and repaints an arrow on the candle immediately after the vertical line? Sure, you can.
But any backtested results will not reflect reality.

And so future viewers do not get misled; repainting strategies are not posted on the forum.
But for your own personal use, what you are attempting to achieve; it is possible.

WorkAround:
Read the AddOrder documentation.
Where you see open[-1] in the syntax example (which means calculate the non-repainting P/L results on the open of the next candle).
Change it to use the repainting current candle's open or close, etc...

But don't post it to the forum. The results do not reflect reality.
ToS cannot capture the inter-candle moment of when you "smash the Buy Mkt button. It will repaint every tick of that candle; then calculate the P/L based on when that candle closed. NOT when you hit the button.
Thank you for that, it gives me a place to look.

Not to belabor the point but please help me understand the "repainting" part when the value of the previous bar closing above the ATR TS will not change, whatever happens intra-bar on the next (current) bar.
 
Thank you for that, it gives me a place to look.

Not to belabor the point but please help me understand the "repainting" part when the value of the previous bar closing above the ATR TS will not change.
Nothing about your study is repainting.
Your study is excellent, thank you for sharing.

The issue is not you. It is ToS.
You want to use your study in a strategy.
Strategies have many limitations.
The major issue is how it calculates the P&L.

It is NOT possible to capture the moment that you "smash the Buy Mkt button."
Your vertical line occurs BEFORE the bar that creates it closes.
BUT BUT BUT the P&L results are calculated AFTER the bar closes.
Therefore, it is not possible to capture accurate P&L results .

OOPs, I left out the link to the AddOrder documentation. Here it is:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder

Sorry, if I gave the impression that there was anything wrong or repaintish with your script.
Was trying to explain why the AddOrder statement cannot capture the moment that you "smash the Buy Mkt button.

Hope this helps.
 
Solution
Nothing about your study is repainting.
Your study is excellent, thank you for sharing.

The issue is not you. It is ToS.
You want to use your study in a strategy.
Strategies have many limitations.
The major issue is how it calculates the P&L.

It is NOT possible to capture the moment that you "smash the Buy Mkt button."
Your vertical line occurs BEFORE the bar that creates it closes.
BUT BUT BUT the P&L results are calculated AFTER the bar closes.
Therefore, it is not possible to capture accurate P&L results .

OOPs, I left out the link to the AddOrder documentation. Here it is:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder

Sorry, if I gave the impression that there was anything wrong or repaintish with your script.
Was trying to explain why the AddOrder statement cannot capture the moment that you "smash the Buy Mkt button.

Hope this helps.
:LIGHTBULB:

"Your vertical line occurs BEFORE the bar that creates it closes.
BUT BUT BUT the P&L results are calculated AFTER the bar closes."

That did it. I get it now. Sorry if you said that earlier and it didn't resonate. Thank you for your patience.
 
Nothing about your study is repainting.
Your study is excellent, thank you for sharing.

The issue is not you. It is ToS.
You want to use your study in a strategy.
Strategies have many limitations.
The major issue is how it calculates the P&L.

It is NOT possible to capture the moment that you "smash the Buy Mkt button."
Your vertical line occurs BEFORE the bar that creates it closes.
BUT BUT BUT the P&L results are calculated AFTER the bar closes.
Therefore, it is not possible to capture accurate P&L results .

OOPs, I left out the link to the AddOrder documentation. Here it is:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder

Sorry, if I gave the impression that there was anything wrong or repaintish with your script.
Was trying to explain why the AddOrder statement cannot capture the moment that you "smash the Buy Mkt button.

Hope this helps.
The strategy will calculate the pnl based on whatever you tell it in the "price= x" parameter in the addorder function. The strategy will display the entryprice (whatever specified and can also be added to the name parameter to display on the chart and in the strategy report) on the chart at the next candle. Omitting the price parameter will default the entryprice to the next candle open.

mod note:
The ToS P&L can ONLY be calculated after the candle has closed (on the next candle open)
If price is set to anything else in the addorder function, it causes repainting and invalidates any results.
Repainting AddOrder scripts are precluded from being posted to the forum.
 
Last edited by a moderator:
The strategy will calculate the pnl based on whatever you tell it in the "price= x" parameter in the addorder function. The strategy will display the entryprice (whatever specified and can also be added to the name parameter to display on the chart and in the strategy report) on the chart at the next candle. Omitting the price parameter will default the entryprice to the next candle open.

mod note:

Repainting AddOrder scripts are precluded from being posted to the forum.
The price parameter is for the strategy to simulate orders when the market price crosses a predetermined price level regardless of time frame or the new candle open do you consider that repainting ?


Please review the attached image.
The StopLosss strategy is using a predefined price in the addorder function and the strategy report and/or calculation of the pnl is not misleading and accurately computed.

1714371642200.png
 
Last edited by a moderator:
The reason the default open[-1] is used in the addorder is because the
Open is the open of the current bar. So if the signal were to happen closer to the end of the candle where price is say $1.00 better, then your strategy report would be showing a one dollar gain that in reality would have been impossible for you to get. Open[-1] is the open of the bar immediately following the signal and is absolutely probable to get. The strategy will mark the bar following the signal no matter what you put in the AddOrder() function as your trade price. That arrow will be at the price level you told the program to trade. So sometimes it can be out of place to the candle it's showing it with. That's actually irrelevant as long at it marks the place that the trade could have logically been executed at during that previous bar as it was forming. The Strategy Report will show accurately what you instruct the program to do. You can also use a logic point as your entry signal. For example. If you were to test the current close price crossing a moving average as your entry or exit signal. The price of the moving average at that crossing point can be used as the entry or exit price.

I was not clear if your answer was addressing my questioning on the strategy and its calculations vs repainting.
If you were to test the current close price crossing a moving average as your entry or exit signal. The price of the moving average at that crossing point can be used as the entry or exit price.
do you consider that as a repainting issue?

This issue was commented on by mobius in this link
 
A short-coming of the ToS ADDORDER function is that the calculation of P&L results can only be computed after the candle is closed. The ToS app does not provide the ability to capture inter-candle activity.

Which means that the P&L data cannot be calculated until the candle has closed.
Which means the repainted data that you are seeing, can only be determined after the candle has closed.

Yes, Mobius is correct. The arrow always prints afterwards.
This is one way, you can tell your order is setup correctly.
 
Last edited:
A short-coming of the ToS ADDORDER function is that the calculation of P&L results can only be computed after the candle is closed. The ToS app does not provide the ability to capture inter-candle activity.

Which means that the P&L data cannot be calculated until the candle has closed.
Which means the repainted data that you are seeing, can only be determined after the candle has closed.

Yes, Mobius is correct. The arrow always prints afterwards.
This is one way, you can tell your order is setup correctly.
The calculation is using the price you provide which is the crossing price but visually it will display the entryprice on the next candle. I have tested it and it also shows on the strategy report correctly.
The visuals are not corrupting the calculations. Other situations of repainting may have different behavior given how you use the addorder function.
If you were to test a limit order the strategy will simulate and calculate based on what you tell it and it will display visually on the chart placed on the next candle at the excat price given in the addorder function and the calculated pnl will use that entry.
I am not clear on the shortcoming beside the visual displacement.
 
The strategy will calculate the pnl based on whatever you tell it in the "price= x" parameter in the addorder function. The strategy will display the entryprice (whatever specified and can also be added to the name parameter to display on the chart and in the strategy report) on the chart at the next candle. Omitting the price parameter will default the entryprice to the next candle open.

mod note:

Repainting AddOrder scripts are precluded from being posted to the forum.
What do you mean by that statement in reference to this issue?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
396 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.
Back
Top