Transmitting new order on close of bar?

Questions about MultiCharts and user contributed studies.
Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Transmitting new order on close of bar?

Postby Xyzzy » 06 Nov 2014

I have an automated strategy that's running on daily bars using IOG through Interactive Brokers. My strategy enters through long limit orders, and it calculates the long entry price for the next bar during the closing tick of the current bar. In code, it looks something like this:

Code: Select all

If BarStatus(1) = 2 then begin
BuyPrice = (some calculation) // This only happens on the closing tick of the bar
end;

Buy next bar at BuyPrice limit; // This happens on every tick of the bar
The problem is that the new order doesn't seem to actually get transmitted to my broker until the opening ticks of the next bar. For example, let's say that my BuyPrice value for today's bar is 1200, so my strategy places a "buy limit 1200" order for today at my broker. It remains active at the broker unless filled.

Then, on the closing tick, my strategy calculates a new BuyPrice value of 1190. It should then transmit a "Buy limit 1190" order to my broker for the next bar.

Unfortunately, MultiCharts does not seem to transmit the new order to my broker until the opening ticks of the next bar. If there's an older order still pending (like the Buy Limit 1200 order), then that order will remain active until the opening ticks of the next bar. That means that my broker might inadvertently fill my order at the old price of 1200, rather than the new price of 1190.

Is there any way around this? I.e., for my new order to be transmitted on the close of the bar, rather than on the open of the next bar?

Thanks.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Transmitting new order on close of bar?

Postby Andrew MultiCharts » 07 Nov 2014

Hello Xyzzy,

Unfortunately there is no way to send your order exactly on closing tick of a bar in this case. Thу only workaround is to use RecalcLastBarAfter(1) to send the order (IOG must be ON for this) in 1 second after the calculation on close.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Transmitting new order on close of bar?

Postby orion » 07 Nov 2014

Another solution: since you are able to use IOG, if you set your PC clock accurately enough, you can check if computerDateTime is within X seconds of session close and place the order on that tick.

TA100
Posts: 54
Joined: 10 Feb 2014
Has thanked: 39 times
Been thanked: 9 times

Re: Transmitting new order on close of bar?

Postby TA100 » 07 Nov 2014

Hello Xyzzy,

Unfortunately there is no way to send your order exactly on closing tick of a bar in this case. Thу only workaround is to use RecalcLastBarAfter(1) to send the order (IOG must be ON for this) in 1 second after the calculation on close.
Andrew, would you be so kind to demonstrate the application of RecalcLastBar to a Trading Strategy rather than the example given, which is a print statement.

Thank you.

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

Re: Transmitting new order on close of bar?

Postby TJ » 07 Nov 2014

Hello Xyzzy,
Unfortunately there is no way to send your order exactly on closing tick of a bar in this case. Thу only workaround is to use RecalcLastBarAfter(1) to send the order (IOG must be ON for this) in 1 second after the calculation on close.
Andrew, would you be so kind to demonstrate the application of RecalcLastBar to a Trading Strategy rather than the example given, which is a print statement.
Thank you.
Let' start with the basics:

MultiCharts is an "Event Driven" software.
(most of the trading software on the market work this way).

MultiCharts is on "Standby" mode... until a tick comes in. (that's the event!)
When a tick comes in, MultiCharts will come alive and spring into action... it will start from the top of the indicator/signal and re-calculate every formula in the code. Then re-paints the charts.

If the code triggers an order, it will be sent at the next tick.

So far so good.

Until the market ran into a quiet period... no new ticks = no event = no re-calculation = no action

Ie. you are stuck if you want to submit an order.

... to circumvent the shortcoming, a new keyword was invented -- RecalcLastBarAfter

With this new keyword, MultiCharts will force a re-calculation of your code even if there is no new ticks coming in.

How to apply this keyword to your code?
There is really no magic to it...
Simply code your strategy as usual, add this line to your code, and that's it.

Code: Select all


RecalcLastBarAfter( x );

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Transmitting new order on close of bar?

Postby Xyzzy » 07 Nov 2014

Thanks everyone. Andrew, I tried the RecalcLastBarAfter technique today. However, the same situation happened again -- my old order remained active at the broker after the close of the bar, and the new order apparently won't be transmitted until the next bar opens. Are you sure that this should work?

An alternative is to tie this to time, as Orion suggests. Then, I could time the new order to be submitted one minute before the close of the bar. (I use the Dimension4 utility to keep my PC's time closely synced.)

However, I'd prefer to use the time at the broker for the last tick, rather than my local system time. That way, I could compare the current time at the broker to Sess1EndTime.

Is there any way to get the current broker time for each tick when using IOG? If I use the "Time" keyword, MultiCharts only returns the time for the end of the bar, rather than the current time at the broker.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Transmitting new order on close of bar?

Postby orion » 07 Nov 2014

However, I'd prefer to use the time at the broker for the last tick, rather than my local system time. That way, I could compare the current time at the broker to Sess1EndTime.

Is there any way to get the current broker time for each tick when using IOG? If I use the "Time" keyword, MultiCharts only returns the time for the end of the bar, rather than the current time at the broker.
There is no way to get current time at the broker.

All the recalcLastBarAfter does is that is recalcs after a timeout as TJ mentioned. If it did not work without recalc, it won't work with recalc. EL semantics is to transmit orders on next trading day if it is a daily chart (see "buy" keyword in EL language reference). PL description of buy on wiki does not describe this behavior but one can expect it is does the same to ensure compatibility.

You may also want to try another workaround. They apparently have a new feature in PL where they allow orders to be sent when barStatus = -1 if you use [AllowSendOrderAlways = true].

http://www.multicharts.com/discussion/v ... =1&t=22239

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Transmitting new order on close of bar?

Postby Xyzzy » 08 Nov 2014

Thanks very much for the detailed response, Orion! I'll play around with the "AllowSendOrdersAlways" attribute.


Return to “MultiCharts”