Why isn't order canceled at end of bar for this signal?

Questions about MultiCharts and user contributed studies.
tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Why isn't order canceled at end of bar for this signal?

Postby tozwp » 06 Jun 2014

I have a signal that runs intrabar order generation on. It looks for a candlestick or a candlestick formation at the end of a 60 minute bar and when true, sets a trigger to buy just above the high of that 60 min candle stick. In backtesting, this works fine. In live testing many times the order to buy is not canceled at the end of the bar when the condition is not met. Not sure if there is something wrong with my logic or the program. Data 1 is 5 min forex data and data2 is the same forex pair, 60 min data. I have more logic on the 5 min but have reduced it to this for testing. I have a few instances where the order to buy stays live for a few hours after the initial buy is sent. If I understand this correctly, as soon as the 'Flag' is false, the order should be canceled. This is simplified to just look for an inside stick but the code looks for others as well. This is running on a practice IB account. Here is the code:

Code: Select all

//5 min data1, 60 min data2

Inputs: pip(0.00002), MinStop(.001);
Vars: MP(0), Flag(false), Trig(0), R(0), size(0), StartEquity(25000);

[intrabarordergeneration = True];

MP = MarketPosition;

//Check for setup at end of bar
If BarStatus(2) = 2 and MP <= 0 then begin

//Check for inside bar and calculate trigger, Risk and trade size

If (H data2 < H[1] data2 and L data2 > L[1] data2) then Flag = True Else Flag = False; //else is redundant?
If Flag then begin
Trig = High data2 + pip;
R = Iff(((High data2 - Low data2) + (pip * 2)) > MinStop, ((High data2 - Low data2) + (pip * 2)), MinStop);
end;

{Size = calculate position size based on account size and R}

size = (((StartEquity + NetProfit)*.001)/R);
end;

If MarketPosition_at_Broker = 0 and Flag then begin //Flag should be false at end of bar when condition isn't met and order canceled
Buy("Long") size shares next bar at Trig Stop;
end;

If MP = 1 then Flag = false;

If MP = 1 then begin
If BarsSinceEntry >= 3 then sell next bar at market;
end;

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Why isn't order canceled at end of bar for this signal?

Postby tozwp » 06 Jun 2014

One other strange thing that the signal is doing that I also don't understand is that it doesn't cancel the order when the broker position is changes from 0 to long. For the above problem with the unclosed order at the end of the bar, it remains unclosed even when the broker position has changed. I thought the command MarketPosition_at_Broker would cause the order to be canceled. I'm running this signal on multiple charts with different time frames for testing. When I have an open order (correct or not) on a higher timeframe and another chart generates an order and is filled, the order on the higher time frame remains active. Is this code being evaluated at every tick? If so, why doesn't the broker position changing to false cancel the order?

Code: Select all

If MarketPosition_at_Broker = 0 and Flag then begin
Buy("Long") size next bar at Trig Stop;
end;
Either I don't understand something and my logic is faulty or something is not running correctly in MC. I can send screenshots or join chat at some point to figure this out. I really hope that I'm doing something stupid because I'm out of ideas.

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Why isn't order canceled at end of bar for this signal?

Postby tozwp » 06 Jun 2014

After more testing, this is what I am seeing. Instead of trading the same pair on three charts with different time frames (I was using: 1 min & 15 min, 5 min & 60 min, 20 min & 240 min) I went back to just running the signal on one chart only. The signal is now running on just the 1 min $15 minute times on one chart. So far it is working perfectly. Seems that the program gets confused about which chart has which position and it starts doing some really weird things.

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

Re: Why isn't order canceled at end of bar for this signal?

Postby JoshM » 07 Jun 2014

Seems that the program gets confused about which chart has which position and it starts doing some really weird things.
What you could try is to create three versions of the same strategy (just copy/paste the code, but saved under different names), and then use MarketPosition at Broker for The Strategy so that each signal (on each chart) has its own `MarketPosition_at_Broker` (if it is caused by "chart confusion", so to speak).

Alternatively, have you tried using RecalcLastBarAfter? Perhaps that can help with a quicker update of the `MarketPosition_at_Broker` value (if that is the issue here).

tozwp
Posts: 145
Joined: 15 Apr 2011
Has thanked: 16 times
Been thanked: 8 times

Re: Why isn't order canceled at end of bar for this signal?

Postby tozwp » 07 Jun 2014

Thanks Josh, I'll give those ideas a try next week. I wasn't aware of the Recalclastbarafter command. The marketpositionatbroker might be just what it needs also.


Return to “MultiCharts”