limit orders with IB

Questions about MultiCharts and user contributed studies.
janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

limit orders with IB

Postby janus » 03 Aug 2010

I'm using code like that below to set and keep active until filled a limit order to IB on a 2 minute chart. The study needs to update on each tick for other purposes. It works most of the time but occasionally I get two buy orders fulfilled instead of one. Is there a better way to submit a live limit order to IB? Perhaps use marketposition_checked?

Code: Select all

[IntrabarOrderGeneration = True]

variables:
intrabarpersist level1(0);

if LastBarOnChart_s then begin
if level1 = 0 then level1 = close-2;
if marketposition = 0 then buy 1 contract next bar at level1 limit;
end;

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit orders with IB

Postby janus » 03 Aug 2010

When I execute this sample code offline (ie, automatic order execution turned off) only one buy signal is generated and displayed on the chart. Yet when I turn automatic order execution on I invariably get two buy orders. Someone must know what is going on. Surely I'm not the only one doing this.

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Re: limit orders with IB

Postby Spaceant » 04 Aug 2010

Hi Janus,

1) I am wondering what price are you attempting to buy next bar, level1.....

2) I have been doing a similar exercise to test an IOG enabled strategy, using PlaceMarketOrder, to rectify the number of contracts if there is any when compared to the market position plotted on the chart. But the result is not satified as it may execute more than once, sometimes a few times more (similar to what you have said). The problem with my code is still unresolved yet.

I think, it is only my view and I am not sure if it 100% correct, the problem is that your IOG enabled strategy send an order to IB, say if it is executed at Tick '#1'. Before MC has got the confirmation of the execution to change marketposition to 1, the next Tick '#2' gets in and is so quick that MC's marketposition value has not changed. Therefore, another limit order is sent to IB again resulting double execution.

If this is right, I can't think of any way to resolve it.

Sa

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit orders with IB

Postby janus » 04 Aug 2010

After some testing I think your explanation is correct. Sometimes I get more than two orders. The way I can prevent this form happening is to use the IOG setting "Limit this signal to one entry and one exit per bar" instead of "Allow unlimited entries and exits per bar". I didn't want to do this in the first place because occasionally I do want to execute multiple entries and/or exits within a two minute bar. However, this is rare so for now I will use the other option since it appears to fix the problem.

It would be much nicer if a limit/stop or any other order is kept alive once submitted so we don't have to keep repeating the command on every tick. Then we could use some kind of cancel command to cancel such orders at any time. This is far more logical as it's the way a manual trader would place orders via the TWS window. It would not only fix the problem I'm having but make coding much simpler and less confusing.

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Re: limit orders with IB

Postby Spaceant » 04 Aug 2010

Hi Janus,

Do you mean checking the following?
"Allow up to 1 entry orders in the same direction as the currently held position:
regardless of the entry that generated ther order"

Another way is to try to code an EOB strategy instead of an IOG enabled strategy if there is a way to do it.....

Sa

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit orders with IB

Postby janus » 04 Aug 2010

No, that's in the Strategy Properties window. I'm referring to the Format Signal window.

As for changing it to EOB, it might work. In other words, if I use barstatus = 2 to execute some of my code it should avoid the duplicate orders while still having IOG turned on, which i need to for other actions. Good idea.

Aaaah!! Just realised the barstatus = 2 idea won't work. The limit order won't be active for all the other ticks updates. My original idea will work for me for now. Goes to show my suggestion to avoid repeating the same command on every tick update is a far better approach. It keeps things simple and effective.

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Re: limit orders with IB

Postby Spaceant » 04 Aug 2010

No, that's in the Strategy Properties window. I'm referring to the Format Signal window.
Which version are you using? I can't find it.
Aaaah!! Just realised the barstatus = 2 idea won't work. The limit order won't be active for all the other ticks updates. My original idea will work for me for now. Goes to show my suggestion to avoid repeating the same command on every tick update is a far better approach. It keeps things simple and effective.
If you are using non-IOG strategy, say with the following:

If condition1 then buy next bar at close limit;

The limt order will be active for every tick on next bar (say for a 2-min bar).... is tit fit for your need?

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: limit orders with IB

Postby janus » 04 Aug 2010

> Which version are you using? I can't find it.

6.0. Format Signal -> "Format" button -> "Intra-Bar Order Generation" tab

> If you are using non-IOG strategy, say with the following:
> If condition1 then buy next bar at close limit;

As I said I have to turn on IOG for other reasons so it's not that simple.


Return to “MultiCharts”