Stop order on same bar as entry (How to)  [SOLVED]

Questions about MultiCharts and user contributed studies.
wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Stop order on same bar as entry (How to)

Postby wilkinsw » 22 Apr 2013

Hi,

the following is my exit code for a strategy that uses stop orders to get in on breakouts:

Code: Select all

//"lowerband" and "upperband" corresponds to trailing stop code

If Marketposition=1 then begin
lstop=Maxlist(lowerband,(entryprice-(stopf)));
llimit=entryprice+(targetf);
sell from entry ("masterl") next bar at lstop stop;
sell from entry ("masterl") next bar at llimit limit;
end;
If Marketposition=-1 then begin
sstop=Minlist(upperband,(entryprice+(stopf)));
slimit=entryprice-(targetf);
Buytocover from entry ("masters") next bar at sstop stop;
Buytocover from entry ("masters") next bar at slimit limit;
end;

My strategy runs off of a 60 min chart. Am I right in assuming that I will not have a stop in place once an entry stop is executed until the next 60min candle?

How would I code a stop that is active immediately?

What settings would I need to backtest an intrabar generated stop?

Many thanks!

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Stop order on same bar as entry (How to)

Postby arjfca » 23 Apr 2013

Use IntraBarOrderGeneration IOG

This will allow order to be generated while the bar is in completion if your condition is true.
Just make sure that you add condition so the same entry order is not repeated on the same bar.

Code: Select all

[IntrabarOrderGeneration=true]

If Marketposition=-1 then begin
sstop=Minlist(upperband,(entryprice+(stopf)));
slimit=entryprice-(targetf);
Buytocover from entry ("masters") next bar at sstop stop;
Buytocover from entry ("masters") next bar at slimit limit;
end;
Martin

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: Stop order on same bar as entry (How to)

Postby wilkinsw » 23 Apr 2013

Great thanks.

Alternatively if used the function under "strategy properties...Auto Trading":

Recalculate on Broker Events ....... Market position change....order filled.

Would this allow me to create a stop immediately after a fill (on the same bar) without the need for IOG code?

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Stop order on same bar as entry (How to)

Postby arjfca » 23 Apr 2013

Great thanks.

Alternatively if used the function under "strategy properties...Auto Trading":

Recalculate on Broker Events ....... Market position change....order filled.

Would this allow me to create a stop immediately after a fill (on the same bar) without the need for IOG code?
I'm really not an expert on that. I will leave to other to answer that. Just don't want to induce you in error.

Martin

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Stop order on same bar as entry (How to)

Postby Henry MultiСharts » 23 Apr 2013

Great thanks.

Alternatively if used the function under "strategy properties...Auto Trading":

Recalculate on Broker Events ....... Market position change....order filled.

Would this allow me to create a stop immediately after a fill (on the same bar) without the need for IOG code?
This option will not help. It recalculates the code but the order can be sent intrabar only if the code has IOG=On.
If you want a stop and a limit order to be placed once a position has been opened- you can use SetProfitTargetand SetStopLoss.
These functions are evaluated intra-bar and not only on close of a bar, and can exit within the same bar as the entry.

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: Stop order on same bar as entry (How to)

Postby wilkinsw » 23 Apr 2013

Can you show me how I can use IOG to execute the following sell exit orders intrabar:
///////////////
If Marketposition=1 then begin
lstop=Maxlist(lowerband,(entryprice-(stopf)));
llimit=entryprice+(targetf);
sell from entry ("masterl") next bar at lstop stop;
sell from entry ("masterl") next bar at llimit limit;
end;
////////////////
I've never used IOG before.

I would need marketposition to change to 1 intrabar upon long fill (or does this change during the "recalculate on broker event" situation). the "lowerband" should not be an intrabar calculation.

How to I avoid pitfalls with IOG in this case?

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: Stop order on same bar as entry (How to)

Postby wilkinsw » 23 Apr 2013

If I use "setstoploss":

If I get stopped out on the bar of entry is it possible for the strategy to then reenter if price then returns to the stop entry price (if all this happens in the same bar)?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Stop order on same bar as entry (How to)  [SOLVED]

Postby Henry MultiСharts » 23 Apr 2013

You do not need to rewrite your code to execute it in IOG mode. First of all you need to understand how signal works in this mode. Refer to How_Signals_are_Calculated article to learn that.
Also keep in mind that Next bar orders are sent to the broker upon next tick after the signal generation tick in IOG mode. That is possible to send Market and Price Next bar orders.

If some code logic should be executed in IOG mode some in non IOG mode then you can
1) split your logic into separate signals, when they are applied to the same chart they will form a complete strategy. How_Scripts_Work
2) use BarStatus to execute parts of the code that should be executed upon bar close only.

I would also recommend you to look through the MultiCharts FAQ on the forum and use forum search. You will find a lot of interesting and useful information, most of the questions have been already asked and replied numerous times.


Return to “MultiCharts”