possible to replace/refresh the limit order after submit?  [SOLVED]

Questions about MultiCharts and user contributed studies.
kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

possible to replace/refresh the limit order after submit?  [SOLVED]

Postby kiyi0317 » 21 May 2016

dear all,

Due to my lack of coding knowledge, I would like to ask for your help for above coding possibility and any key words could be use.

I just want to submit limit order in my signal, and the situation is once the signal has submitted a limit order to the broker IB, but the order has not fulfill within 60 seconds. I would like to resubmit another limit order to the broker to replace the previous one. is it possible to do it in coding? any key words recommend to use?

thanks
kiyi

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

Re: possible to replace/refresh the limit order after submit

Postby TJ » 21 May 2016

dear all,

Due to my lack of coding knowledge, I would like to ask for your help for above coding possibility and any key words could be use.

I just want to submit limit order in my signal, and the situation is once the signal has submitted a limit order to the broker IB, but the order has not fulfill within 60 seconds. I would like to resubmit another limit order to the broker to replace the previous one. is it possible to do it in coding? any key words recommend to use?

thanks
kiyi
What is your instrument?
What is your chart resolution?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: possible to replace/refresh the limit order after submit

Postby JoshM » 21 May 2016

Due to my lack of coding knowledge, I would like to ask for your help for above coding possibility and any key words could be use.

I just want to submit limit order in my signal
Limit orders are submitted with the Buy (for enter long), Sell (to close a long position), SellShort (to open a short position), and BuyToCover (to close a short position) keywords.
, and the situation is once the signal has submitted a limit order to the broker IB, but the order has not fulfill within 60 seconds. I would like to resubmit another limit order to the broker to replace the previous one.
For this we need to do a couple of things:

(1) We need to know the time that the limit order was submitted to the broker. We can approximate that time by storing the current computer time in a variable at the moment that we submit the limit order for the first time. The current computer time is returned by the ComputerDateTime keyword.

(2) Then we need to check whether the 'submitting time' was 60 seconds or more ago. For that we can compare the current `ComputerDateTime` value with the value that we've just stored in a variable. When the time difference between these two values is 60 seconds or greater, we should stop submitting the 'old' limit order.

(3) Cancelling that 'old' limit order (so that the new order can take its place) is done by not resubmitting it again. This means that we shouldn't use the `Buy`, `Sell`, `SellShort`, or `BuyToCover` keyword anymore to send that 'old' limit order.

(4) The last step is then submitting the new limit order. We do that in the same way with the `Buy`, `Sell`, `SellShort`, or `BuyToCover` keyword when we send the first order.

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: possible to replace/refresh the limit order after submit

Postby kiyi0317 » 21 May 2016

What is your instrument?
What is your chart resolution?
dear TJ,
I do it on Hong Kong HSI.
I using range bar.

normally, each bar time frame would be more than 60s.

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: possible to replace/refresh the limit order after submit

Postby kiyi0317 » 21 May 2016

For this we need to do a couple of things:

(1) We need to know the time that the limit order was submitted to the broker. We can approximate that time by storing the current computer time in a variable at the moment that we submit the limit order for the first time. The current computer time is returned by the ComputerDateTime keyword.

(2) Then we need to check whether the 'submitting time' was 60 seconds or more ago. For that we can compare the current `ComputerDateTime` value with the value that we've just stored in a variable. When the time difference between these two values is 60 seconds or greater, we should stop submitting the 'old' limit order.

(3) Cancelling that 'old' limit order (so that the new order can take its place) is done by not resubmitting it again. This means that we shouldn't use the `Buy`, `Sell`, `SellShort`, or `BuyToCover` keyword anymore to send that 'old' limit order.

(4) The last step is then submitting the new limit order. We do that in the same way with the `Buy`, `Sell`, `SellShort`, or `BuyToCover` keyword when we send the first order.
I also have the same thought on above at point 1 and 2. but I do not understand how to cancel the old limit order, because range bar has being used, each bar will take time to complete, generally over few minutes. so the old order will not be cancel, until the current bar close. but I need to create a new order to replace it after 60 seconds whatever the current bar has finish or not. Can I?

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

Re: possible to replace/refresh the limit order after submit

Postby TJ » 21 May 2016

I also have the same thought on above at point 1 and 2. but I do not understand how to cancel the old limit order, because range bar has being used, each bar will take time to complete, generally over few minutes. so the old order will not be cancel, until the current bar close. but I need to create a new order to replace it after 60 seconds whatever the current bar has finish or not. Can I?
Although you are using a range chart,
but in reality, you want to trade on a 1 min chart.

You have to rethink your logic and approach,
and reconfigure your strategy accordingly.


One way to do it is to use a multi-data approach:
use a 1 min chart as data1, and your range chart as data2.
apply your logic to data2, but execute your trades on data1.


see post #18
Multi-data strategy
viewtopic.php?f=16&t=10811

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: possible to replace/refresh the limit order after submit

Postby kiyi0317 » 21 May 2016


Although you are using a range chart,
but in reality, you want to trade on a 1 min chart.

You have to rethink your logic and approach,
and reconfigure your strategy accordingly.


One way to do it is to use a multi-data approach:
use a 1 min chart as data1, and your range chart as data2.
apply your logic to data2, but execute your trades on data1.


see post #18
Multi-data strategy
viewtopic.php?f=16&t=10811
Get it, understand. let me check that is cover all my requirments.

TJ, thank you so much

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: possible to replace/refresh the limit order after submit

Postby kiyi0317 » 22 May 2016

I added a new data 1 and removed the previous data 1 to data 2. but seems the outcomes is not prefect. the orders are not completely follow the script as before.

can we just write coding to refresh the limit order according to time limit setting?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: possible to replace/refresh the limit order after submit

Postby JoshM » 25 May 2016

One way to do it is to use a multi-data approach:
use a 1 min chart as data1, and your range chart as data2.
apply your logic to data2, but execute your trades on data1.
This is indeed one way to do it, but it might be a little overkill here since checking if there's a 60 second time difference only takes a few lines of extra code. (And adding a second data series can make the first data series 'ugly' with gaps between the bars, but that's also a personal preference).
I do not understand how to cancel the old limit order, because range bar has being used, each bar will take time to complete, generally over few minutes. so the old order will not be cancel, until the current bar close. but I need to create a new order to replace it after 60 seconds whatever the current bar has finish or not. Can I?
It doesn't matter which time frame or bar type you use: as long as the script stops re-submitting the unfilled limit order, MultiCharts will cancel that order. This works the same on minute, second, range bars or whichever bar resolution you use.

You're right that the bar resolution does affect how long it takes when the current bar closes. And so the bar resolution does affect how often the script calculates -- but that's when you use 'on bar close' calculation.

However, since you want to use exactly 60 seconds, I wouldn't use a trading strategy that uses on bar close only. Even on 1 minute bars, it can take more than a minute before the price bar closes. That's because the bar is closed when the closing tick is received, and that price update can take a couple more seconds (or a lot more) when there is low liquidity in the market.

So I would use IntrabarOrderGeneration coupled with RecalcLastBarAfter() so that the script is calculated intra-bar and can send (or stop sending) the limit order as soon as the 60 second time limit is reached.

(Of course there's still lag before the broker has acknowledged the cancellation of the limit order, but the whole process will be closer to 60 seconds than waiting on the bar to close).

Also, if you use `IntrabarOrderGeneration` you can also submit the second limit order (that's intended to replace the original limit order) much sooner.

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: possible to replace/refresh the limit order after submit

Postby kiyi0317 » 30 May 2016

just happy to share my code, I just make limit order in the past, here I just added 'RecalcLastBarAfter(60)' as below. then a new market order will be create and replace the previous limit order after 60s, if the limit order has not filled yet.

-coding-
if (MarketPosition < 0) and ...................................................................... then begin;
if ........ then buytocover ("exitsell1") next bar at ........... limit;
if ........ then buytocover ("exitsell2") next bar at ........... limit;
RecalcLastBarAfter(60);
end;
-coding-


Return to “MultiCharts”