AddOrder only executes on open of next bar = fatal flaw of thinkScript?

somefool

New member
I'm assuming one or more of my points below is not 100% accurate so I welcome anyone to correct me. I apologize in advance for writing in frustration. Please take that into consideration.

New to this forum and thinkScript but not new to TOS or trading. Have had some success with discretionary trading but am ready to take the next step and translate my methodologies into thinkScript Strategies so I can back test and forward test my ideas and use these signals to enter real trades in live trading. Eventually I would like to move on to fully automated trading. I've taken some programming classes (mainly C++ and C#). I'm not programmer but can read and cobble together some code and do a few things.

After weeks of studying thinkScript I'm about to give up and try a different platform like TradeStation or NinjaTrader. I am frustrated that thinkScript's AddOrder function only executes trades on Open of the next bar instead of now if my condition is met (while the current bar is still being drawn like in real trading). After years of discretionary trading I'm painfully aware that bad things can happen between now and open of the next bar 😰. Entering a trade on open of the next bar is not illustrative of real trading in my experience. You can do that if you want, but many traders have no desire to wait that long. Because of this apparent AddOrder limitation, the backtesting reports thinkScript generates are inaccurate imo, rendering them essentially useless. I cannot trust the reports, so I cannot truly test my Strategies.

While it is possible to code a Study (or Strategy) indicator and follow its alerts (which do appear to be immediate) and use those signals to manually place real trades during live trading, it is not possible (to my knowledge) to accurately back test or forward test a Strategy which kinda deletes the whole point of thinkScript Strategies. I actually saw another person ask the same question this week somewhere else on the net and the answer was to tweak the reports in thinkScript so they model results based on orders being executed now, instead of based on orders being executed on open of the next bar. Hmm. I guess that might work but haven't tested it yet. Curious if anyone has had any luck doing a tweak like that or if I am not correct about this limitation of AddOrder?

Not to run my point into the ground, but just for clarity TradeStation, for example, has two modes;

On Bar Close Order Generation (Default)
Orders are generated at the close of this bar to be filled on the next bar

Intra-Bar Order Generation
Orders are generated multiple times on this bar to be filled this bar

Thank you
 
Last edited:
Solution
The reason that ToS will not be able to apply AddOrder() calls intra-bar is that, for back testing purposes (as well as all charting purposes, really) ToS only stores the OHLCV values for the bar. It does not store the continuous price change that happens intra-bar.

When ToS calculates charts, studies, and strategies, it only has access to (and can only use) the OHLCV values, and that is the reason it will not be able to do intra-bar buy or sell signals.

Remember that all back-testing is really for reference and is never (no matter what the sales pitch may say) exactly correct for actual trading. There are too many variables. This is not true for a live trading simulator (think python with a high speed data pipeline) which could...
I can only share your frustration. I have seen some posts that say that "BUY_AUTO" overcomes the problem, but I have not found that to be true. I hope someone is able to provide a way to open positions intra-bar in a strategy!!! As you say, the study works fine. But the buys and sells don't line up with the strategy derived from the study. always one bar out.
 
Hey, Somefool, the problem may not be as bad as we think. We are talking "strategies" here, not "studies". It seems that the chart arrows will ALWAYS be one bar late. So, if the action meets our criteria for entering a trade anywhere in the current bar, the arrow for opening the trade will show on the next bar. But, the price used when calculating profit/loss will be the price we pass in our AddOrder statement. In my case, I want the strategy to enter a long position at the open of the bar plus some calculated amount. if i pass "open + calculated amount" in the AddOrder statement as the entry price, that's what will be used when calculating the P/L. Less than ideal, but it works.
 
The reason that ToS will not be able to apply AddOrder() calls intra-bar is that, for back testing purposes (as well as all charting purposes, really) ToS only stores the OHLCV values for the bar. It does not store the continuous price change that happens intra-bar.

When ToS calculates charts, studies, and strategies, it only has access to (and can only use) the OHLCV values, and that is the reason it will not be able to do intra-bar buy or sell signals.

Remember that all back-testing is really for reference and is never (no matter what the sales pitch may say) exactly correct for actual trading. There are too many variables. This is not true for a live trading simulator (think python with a high speed data pipeline) which could execute 'trades' as the order flow comes in. I have written such things, but they are far, far beyond the scope and ability of ToS.

-mashume
 
Solution
ToS only stores the OHLCV values for the bar. It does not store the continuous price change that happens intra-bar.
I can open OnDemand, switch to the 100 tick timeframe and watch the ticks move up an down 100 times for each bar. In a highly liquid market a 100 tick bar is often drawn in less than 1 minute. It seems there is a lot more data in TOS than AddOrder can handle.
 
I can open OnDemand, switch to the 100 tick timeframe and watch the ticks move up an down 100 times for each bar. In a highly liquid market a 100 tick bar is often drawn in less than 1 minute. It seems there is a lot more data in TOS than AddOrder can handle.
On Demand will indeed be able to retrieve a live stream playback... and apply the strategy of your choice at whatever interval you choose (1m. 50tick, etc...). That strategy is still limited to the OHLCV values that the ToS servers send you when you select your time/trade aggregation. It is not that AddOrder() can not handle the data, but what AddOrder() was designed to do (which is use OHLCV values to do backtesting. AddOrder is not designed to operate on anything but the condition of the strategy at the close of a bar. Think of it as doing a calculation once per bar, at the end of the bar, and it makes more sense. This is just the way it is designed. It is a limitation, but not a design flaw per sey.

-mashume
 
AddOrder is not designed to operate on anything but the condition of the strategy at the close of a bar.
I think you put your finger on the problem. I wonder if TOS is even being developed anymore. For example 2 1/2 years ago I asked client support why OnDemand could not recognize hotkeys nor display trailing stops. They had no answer. Almost 3 years later the issues have still not been resolved. And now I find that AddOrder is of limited or no value too.
 
I want the strategy to enter a long position at the open of the bar plus some calculated amount. if i pass "open + calculated amount" in the AddOrder statement as the entry price, that's what will be used when calculating the P/L. Less than ideal, but it works.
Thanks. I will take a look at this.
 
This may not be pretty but I managed to get thinkScript to enter a position at the moment my condition is true, instead of on the next bar. This example is a short trade taken the moment price crosses below a plot line. Still working on other entry scenarios, and exits. The chart shows the 'Sell to Open' text and its arrow on the next candle, but the actual order execution price is now, on the current candle. This isn't really working quite right yet, but I think you get the idea.

Code:
input priceLow = low;
input priceClose = close;
input length = 9;
input averageType = AverageType.SIMPLE;

plot avg = MovingAverage(averageType, priceClose, length);

def goShortCondition = if priceLow crosses below avg then 1 else 0;

def shortEntryPrice = avg;

AddOrder(OrderType.SELL_TO_OPEN, goShortCondition, shortEntryPrice, 100, Color.RED, Color.RED, "Sell to Open @ " + shortEntryPrice);
 
Last edited:

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
254 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