Buy this bar at close, buys next bar at open?  [SOLVED]

Questions about MultiCharts and user contributed studies.
FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 10 Aug 2016

Hi,

I've developed a strategy which triggers a long entry when a certain set of conditions is met. In back-testing and forward-testing the code executes as expected.

However, when I run the following code live it appears to be executing orders at the price of the open of the next bar, instead of the close of the current bar.

Code: Select all

Buy("Go Long") This Bar positionSize contracts at close;
The attached screenshot illustrates the issue.

I've tried toggling

Code: Select all

[IntraBarOrderGeneration=True]
on/off but it doesn't make a difference. I have noticed a similar issue occurs for Trailing Stops (exits are being executed 1 tick below) where they should.

Am I missing something obvious or is it even possible to get orders executed at the close of a bar in MultiCharts? Any advice is appreciated.
Attachments
MarketOrderTest.PNG
(185.29 KiB) Downloaded 2799 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Buy this bar at close, buys next bar at open?

Postby TJ » 10 Aug 2016

Hi,

I've developed a strategy which triggers a long entry when a certain set of conditions is met. In back-testing and forward-testing the code executes as expected.

However, when I run the following code live it appears to be executing orders at the price of the open of the next bar, instead of the close of the current bar.

Code: Select all

Buy("Go Long") This Bar positionSize contracts at close;
The attached screenshot illustrates the issue.

I've tried toggling

Code: Select all

[IntraBarOrderGeneration=True]
on/off but it doesn't make a difference. I have noticed a similar issue occurs for Trailing Stops (exits are being executed 1 tick below) where they should.

Am I missing something obvious or is it even possible to get orders executed at the close of a bar in MultiCharts? Any advice is appreciated.

see post #7
[FAQ] This Bar on Close
viewtopic.php?f=16&t=10811

FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Re: Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 10 Aug 2016

Thanks for the prompt reply, I read page 80 of the EasyLanguage Essentials Programmers Guide.
this bar on Close: Market order on the close of this bar, generally used for historical backtesting
purposes only. This will not generate a market on close order.
It seems like quite a limitation to have to delay order execution until the next bar shows up, and I can't think of any scenario where back-testing a strategy that you can't replicate live would be of any use to a trader.

Is there any work around for the above or is it simply not possible to achieve a 'buy the close of the bar' type scenario in EasyLanguage?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Buy this bar at close, buys next bar at open?

Postby TJ » 10 Aug 2016

What is your chart resolution?

FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Re: Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 12 Aug 2016

Working off tick charts of various resolutions (500,1000 etc.), I wonder if something like setting up a 2nd data feed to count remaining ticks left to bar completion and executing entry on the penultimate tick before closing would work.

I'd love to have something like this for range bars, but given their nature I don't think it's feasible.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Buy this bar at close, buys next bar at open?

Postby tony » 12 Aug 2016

I assume you are using market orders in live trading based on how your code is written. That would explain being off one tick in some instances. If you have IOG=False, then if you generate orders at bar close, then the only opportunity for your generated order to fill would be on the next tick which is the open of the next bar.

I use IOG=True and my orders are written "buy next bar at market;" which results often in a delay of a few seconds until the next tick occurs even though I am buying via market I have to wait for the next trade to happen. I trade ZB which moves insanely slow compared to ES for example. But it's important to remember the tick that your system is evaluating is only used to generate orders. Once that order is generated the earliest it can fill is the next tick. Which can be bar open.

Apologies if I totally misunderstood what you are asking.

FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Re: Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 12 Aug 2016

I assume you are using market orders in live trading based on how your code is written. That would explain being off one tick in some instances. If you have IOG=False, then if you generate orders at bar close, then the only opportunity for your generated order to fill would be on the next tick which is the open of the next bar.

I use IOG=True and my orders are written "buy next bar at market;" which results often in a delay of a few seconds until the next tick occurs even though I am buying via market I have to wait for the next trade to happen. I trade ZB which moves insanely slow compared to ES for example. But it's important to remember the tick that your system is evaluating is only used to generate orders. Once that order is generated the earliest it can fill is the next tick. Which can be bar open.

Apologies if I totally misunderstood what you are asking.
You are right about the bid/ask spread difference Tony, I pretty much want to preempt the close of the bar and enter at the price level the strategy intends, which is the ask of the close of the current bar before the next bar shows up 0.25 points away.

The current issue is trying to replicate strategy real-time performance as close to back-testing and walk-forward testing as is possible.

I have tried limit orders on a co-located server via MultiCharts but it still doesn't guarantee execution. ES uses a FIFO matching algorithm, so as a retail trader someone will always be faster using FPGA's or similar. I was hoping to use MultiCharts as a means to rapidly prototype strategies, that is get them working at a basic level of profitability and then perhaps port them to a low-level language and API to reduce latency and increase performance. However, I'm finding that replicating theoretical results in real-life is proving to be quite a challenge on the platform. Maybe I'm approaching things from the wrong perspective, but I'm starting to think I might be better off coding something custom in C/C++ to meet my exact criteria.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Buy this bar at close, buys next bar at open?

Postby tony » 12 Aug 2016

I assume you are using market orders in live trading based on how your code is written. That would explain being off one tick in some instances. If you have IOG=False, then if you generate orders at bar close, then the only opportunity for your generated order to fill would be on the next tick which is the open of the next bar.

I use IOG=True and my orders are written "buy next bar at market;" which results often in a delay of a few seconds until the next tick occurs even though I am buying via market I have to wait for the next trade to happen. I trade ZB which moves insanely slow compared to ES for example. But it's important to remember the tick that your system is evaluating is only used to generate orders. Once that order is generated the earliest it can fill is the next tick. Which can be bar open.

Apologies if I totally misunderstood what you are asking.
You are right about the bid/ask spread difference Tony, I pretty much want to preempt the close of the bar and enter at the price level the strategy intends, which is the ask of the close of the current bar before the next bar shows up 0.25 points away.

The current issue is trying to replicate strategy real-time performance as close to back-testing and walk-forward testing as is possible.

I have tried limit orders on a co-located server via MultiCharts but it still doesn't guarantee execution. ES uses a FIFO matching algorithm, so as a retail trader someone will always be faster using FPGA's or similar. I was hoping to use MultiCharts as a means to rapidly prototype strategies, that is get them working at a basic level of profitability and then perhaps port them to a low-level language and API to reduce latency and increase performance. However, I'm finding that replicating theoretical results in real-life is proving to be quite a challenge on the platform. Maybe I'm approaching things from the wrong perspective, but I'm starting to think I might be better off coding something custom in C/C++ to meet my exact criteria.
Couple of thoughts. One, is knowing when the last tick equals bar close is likely impossible. Replicating backtests and live tests is nearly impossible as well. What I do is use market on entry and limit to exit where limit buy to cover or limit sell in live testing. I lose a tick on entry because I use market orders and 99% of the time need price to flow through my limits to fill. In live trading I target 6 ticks in ZB and risk 7. But in backtesting I target 8 ticks and risk 6. The result is my backtests and live tests are identical other than those few occasions I have good queue or the bid ask is flipped.

An example in backtests I buy (whether market or limit MC treats it the same) at 172.13 but in the real world my market order fills at 172.14. On exit, again MC fills at the limit so I force it to trade through by a tick the same as in the real world. So in live I'd fill at 172.20, and in backtests my limit fills at 172.20 only when price trades at 172.21 or higher.

I do the same with an ES scalping system but as you know ES moves so fast, my ability to replicate is closer to 90%.

By the way I trade in Denver, 2000 miles roundtrip to Chicago and fill in about 200 ms from the order is generated. Sufficient for my style.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Buy this bar at close, buys next bar at open?

Postby TJ » 12 Aug 2016

You are over complicating things.

All you need is this:

Code: Select all


[ IOG=True ]

If volume = 499 then BUY next bar at market;

FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Re: Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 12 Aug 2016

I assume you are using market orders in live trading based on how your code is written. That would explain being off one tick in some instances. If you have IOG=False, then if you generate orders at bar close, then the only opportunity for your generated order to fill would be on the next tick which is the open of the next bar.

I use IOG=True and my orders are written "buy next bar at market;" which results often in a delay of a few seconds until the next tick occurs even though I am buying via market I have to wait for the next trade to happen. I trade ZB which moves insanely slow compared to ES for example. But it's important to remember the tick that your system is evaluating is only used to generate orders. Once that order is generated the earliest it can fill is the next tick. Which can be bar open.

Apologies if I totally misunderstood what you are asking.
You are right about the bid/ask spread difference Tony, I pretty much want to preempt the close of the bar and enter at the price level the strategy intends, which is the ask of the close of the current bar before the next bar shows up 0.25 points away.

The current issue is trying to replicate strategy real-time performance as close to back-testing and walk-forward testing as is possible.

I have tried limit orders on a co-located server via MultiCharts but it still doesn't guarantee execution. ES uses a FIFO matching algorithm, so as a retail trader someone will always be faster using FPGA's or similar. I was hoping to use MultiCharts as a means to rapidly prototype strategies, that is get them working at a basic level of profitability and then perhaps port them to a low-level language and API to reduce latency and increase performance. However, I'm finding that replicating theoretical results in real-life is proving to be quite a challenge on the platform. Maybe I'm approaching things from the wrong perspective, but I'm starting to think I might be better off coding something custom in C/C++ to meet my exact criteria.
Couple of thoughts. One, is knowing when the last tick equals bar close is likely impossible. Replicating backtests and live tests is nearly impossible as well. What I do is use market on entry and limit to exit where limit buy to cover or limit sell in live testing. I lose a tick on entry because I use market orders and 99% of the time need price to flow through my limits to fill. In live trading I target 6 ticks in ZB and risk 7. But in backtesting I target 8 ticks and risk 6. The result is my backtests and live tests are identical other than those few occasions I have good queue or the bid ask is flipped.

An example in backtests I buy (whether market or limit MC treats it the same) at 172.13 but in the real world my market order fills at 172.14. On exit, again MC fills at the limit so I force it to trade through by a tick the same as in the real world. So in live I'd fill at 172.20, and in backtests my limit fills at 172.20 only when price trades at 172.21 or higher.

I do the same with an ES scalping system but as you know ES moves so fast, my ability to replicate is closer to 90%.

By the way I trade in Denver, 2000 miles roundtrip to Chicago and fill in about 200 ms from the order is generated. Sufficient for my style.
Thanks for sharing your approach, it's good to hear from someone that has got something reliable up and running on the platform. If I can get anywhere near 90% replication on ES I'd be in very good shape. I'm based in Europe which is one of the main reasons for co-location. I'm going to see if I can modify the strategy for worst case scenarios and still keep it profitable.

FuturesTrader
Posts: 7
Joined: 12 Jun 2016
Has thanked: 2 times

Re: Buy this bar at close, buys next bar at open?

Postby FuturesTrader » 12 Aug 2016

You are over complicating things.

All you need is this:

Code: Select all


[ IOG=True ]

If volume = 499 then BUY next bar at market;
Thanks TJ, seems like buy the penultimate bar tick without the need for a 2nd data feed. I'll give that a go.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Buy this bar at close, buys next bar at open?

Postby tony » 12 Aug 2016

You are over complicating things.

All you need is this:

Code: Select all


[ IOG=True ]

If volume = 499 then BUY next bar at market;
Thanks TJ, seems like buy the penultimate bar tick without the need for a 2nd data feed. I'll give that a go.
Word of caution using multiple data series. Data2, etc are only calculated in backtests on OHLC. So if you use IOG=True and bar magnifier your backtests will not be accurate. Unless you reference the prior Data2 which has already closed.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Buy this bar at close, buys next bar at open?  [SOLVED]

Postby TJ » 15 Aug 2016

You are over complicating things.

All you need is this:

Code: Select all


[ IOG=True ]

If volume = 499 then BUY next bar at market;
Thanks TJ, seems like buy the penultimate bar tick without the need for a 2nd data feed. I'll give that a go.

No, the penultimate bar tick is the trigger. You are buying at the next tick, which is the CLOSE of the current bar.

samtd1717
Posts: 1
Joined: 20 Dec 2019

Re: Buy this bar at close, buys next bar at open?

Postby samtd1717 » 25 Apr 2020

I am new to this. I am not an expert. Here is what I know. one of the problems is that Easy language would not be able to recognize MarketPosition until the close of the bar. Most of the codes/strategies require this function. (If Marketposition=-1/0/+1 then ...buy/sell...). Well, your code (buy this bar) will never work in real life. I am Saving myself trouble and headache and using 'buy next bar at market' . I tried buy next bar at open-bigpointvalue and that did not work either. Not sure if C sharp has a solution for this problem. I think 'buy next bar at market" is the only reliable way to execute in easylanguage. Thoughts?

Kloppete
Posts: 2
Joined: 21 Feb 2019

Re: Buy this bar at close, buys next bar at open?

Postby Kloppete » 02 Jan 2021

Hi.

I have a very quick curiosity that the user manual does not fully clarify.
Will "this bar at close" generate a market order during live trading? If a shorter custom session is used (eg on ES 17,00-15,55 instead of 17,00-16,00) and using daily or 1440 min chart will "this bar at close" send a market order to the broker before market closing time?

Thank you in advance.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Buy this bar at close, buys next bar at open?

Postby TJ » 02 Jan 2021

Hi.

I have a very quick curiosity that the user manual does not fully clarify.
Will "this bar at close" generate a market order during live trading? If a shorter custom session is used (eg on ES 17,00-15,55 instead of 17,00-16,00) and using daily or 1440 min chart will "this bar at close" send a market order to the broker before market closing time?

Thank you in advance.
Yes this code will send an order at EOB (End of Bar).

As always, you should test your assumptions on a papertrading account.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: Buy this bar at close, buys next bar at open?

Postby lingwuchung » 03 Jan 2021

Hi,

I've developed a strategy which triggers a long entry when a certain set of conditions is met. In back-testing and forward-testing the code executes as expected.

However, when I run the following code live it appears to be executing orders at the price of the open of the next bar, instead of the close of the current bar.

Code: Select all

Buy("Go Long") This Bar positionSize contracts at close;
The attached screenshot illustrates the issue.

I've tried toggling

Code: Select all

[IntraBarOrderGeneration=True]
on/off but it doesn't make a difference. I have noticed a similar issue occurs for Trailing Stops (exits are being executed 1 tick below) where they should.

Am I missing something obvious or is it even possible to get orders executed at the close of a bar in MultiCharts? Any advice is appreciated.
I reazlied the same issue last month when I started to paper trade. Since then I change my code to "next bar at open", so that my backtest and paper trade will behave the same.

Any reason you must trade "close of this bar"? Suppose the price at "this bar on close" and "open of next bar" should be pretty much the same.

Kloppete
Posts: 2
Joined: 21 Feb 2019

Re: Buy this bar at close, buys next bar at open?

Postby Kloppete » 03 Jan 2021

If your intention is to take position at the end of day but also using info from that same close you could proxy the daily close with a shorter custom session and use "this bar on close" to enter/exit some minutes before end of session in live trading. The alternative would be to use a shorter time frame as primary data series and recalculate all your strategy on the basis of a specific hour near the end of session.

Using "this bar on close" with a shorter session would then make you spare lines of code and secondary data series.
Keep in mind that you can only proxy the session close since if you sent orders right at the close you would enter market next session open.

In intraday trading bar close is next bar open so in this case it is not as important unless the order is sent on the last bar of session.


Return to “MultiCharts”