To execute a trade on same bar if possible  [SOLVED]

Questions about MultiCharts and user contributed studies.
stefanols
Posts: 51
Joined: 01 Jan 2014
Has thanked: 14 times
Been thanked: 2 times

To execute a trade on same bar if possible

Postby stefanols » 03 Jul 2015

Hi,
I am trying to learn how to if possible execute all trades on the same bar instead of waiting for next one. See image. where last position should have hit target and execute on prior bar. Also in this case it stops out directly. Do not know why. Seem to be the wrong sequence trade.

Many thanks in advance. Hope the code tags get right this time.


Code: Select all


[IntrabarOrderGeneration = true]


inputs:
smalength ( 200 ),
emalength ( 100 ),
hmalength ( 34 ),
target1 ( 12 ),
target2 ( 12 ),
target3 ( 20 ),
stopsize ( 12 ),
BE2 ( 0 ), // 0=false, 1=true
BE3 ( 0 ), // 0=false, 1=true

rsilength ( 14 ),
Price ( Close) ,
Oversold ( 30 ),
Overbought ( 70 );


vars:
TickSize ( MinMove / PriceScale ),
smav ( 0 ),
emav ( 0 ),
hmav ( 0 ),
t1 ( target1 * TickSize ),
t2 ( (target1 + target2) * TickSize ),
t3 ( (target1 + target2 + target3) * TickSize ),
st1 ( 0 ),
st2 ( 0 ),
st3 ( 0 ),

var0 ( 0 );




smav = Average(Close, smalength);
emav = XAverage(Close, emalength);
hmav = jtHMA(Close, hmalength);

var0 = RSI(Price,rsilength);


// open new positions


if (BarStatus(1) = 2) then begin

condition8 = var0 crosses above oversold;

end;



if (BarStatus(1) = 2) then begin


condition18 = var0 crosses belowx overbought;

end;





if MarketPosition = 0 then begin

if condition8 then begin

Buy ("Enter long") 3 Contracts Next Bar At Market;

end;

if condition18 then begin

SellShort ("Enter short") 3 Contracts Next Bar At Market;

end;
end;

// manage open orders

if MarketPosition = 1 then begin

st1 = EntryPrice - (stopsize * TickSize);
st2 = iff(BE2 = 1, EntryPrice, EntryPrice - (stopsize * TickSize));
st3 = iff(BE3 = 1, EntryPrice, EntryPrice - (stopsize * TickSize));

if CurrentContracts = 1 then begin
Sell ("Exit l3-c1 Target") 1 Contracts Next Bar At (EntryPrice + t3) Limit;
Sell ("Exit l3-c1 Stop") 1 Contracts Next Bar At st3 Stop;
end;

if CurrentContracts = 2 then begin
Sell ("Exit l2-c2 Target") 1 Contracts Next Bar At (EntryPrice + t2) Limit;
Sell ("Exit l2-c2 Stop") 1 Contracts Next Bar At st2 Stop;
Sell ("Exit l3-c2 Target") 1 Contracts Next Bar At (EntryPrice + t3) Limit;
Sell ("Exit l3-c2 Stop") 1 Contracts Next Bar At st3 Stop;
end;

if CurrentContracts = 3 then begin
Sell ("Exit l1-c3 Target") 1 Contracts Next Bar At (EntryPrice + t1) Limit;
Sell ("Exit l1-c3 Stop") 1 Contracts Next Bar At st1 Stop;
Sell ("Exit l2-c3 Target") 1 Contracts Next Bar At (EntryPrice + t2) Limit;
Sell ("Exit l2-c3 Stop") 1 Contracts Next Bar At st2 Stop;
Sell ("Exit l3-c3 Target") 1 Contracts Next Bar At (EntryPrice + t3) Limit;
Sell ("Exit l3-c3 Stop") 1 Contracts Next Bar At st3 Stop;
end;
end;

if MarketPosition = -1 then begin

st1 = EntryPrice + (stopsize * TickSize);
st2 = iff(BE2 = 1, EntryPrice, EntryPrice + (stopsize * TickSize));
st3 = iff(BE3 = 1, EntryPrice, EntryPrice + (stopsize * TickSize));

if CurrentContracts = 1 then begin
BuyToCover ("Exit s3-c1 Target") 1 Contracts Next Bar At (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c1 Stop") 1 Contracts Next Bar At st3 Stop;
end;

if CurrentContracts = 2 then begin
BuyToCover ("Exit s2-c2 Target") 1 Contracts Next Bar At (EntryPrice - t2) Limit;
BuyToCover ("Exit s2-c2 Stop") 1 Contracts Next Bar At st2 Stop;
BuyToCover ("Exit s3-c2 Target") 1 Contracts Next Bar At (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c2 Stop") 1 Contracts Next Bar At st3 Stop;
end;

if CurrentContracts = 3 then begin
BuyToCover ("Exit s1-c3 Target") 1 Contracts Next Bar At (EntryPrice - t1) Limit;
BuyToCover ("Exit s1-c3 Stop") 1 Contracts Next Bar At st1 Stop;
BuyToCover ("Exit s2-c3 Target") 1 Contracts Next Bar At (EntryPrice - t2) Limit;
BuyToCover ("Exit s2-c3 Stop") 1 Contracts Next Bar At st2 Stop;
BuyToCover ("Exit s3-c3 Target") 1 Contracts Next Bar At (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c3 Stop") 1 Contracts Next Bar At st3 Stop;
end;

end;
Attachments
BMT MM 4 min.PNG
(6.2 KiB) Downloaded 326 times

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

Re: To execute a trade on same bar if possible  [SOLVED]

Postby JoshM » 03 Jul 2015

Hi,
I am trying to learn how to if possible execute all trades on the same bar instead of waiting for next one. See image. where last position should have hit target and execute on prior bar. Also in this case it stops out directly. Do not know why. Seem to be the wrong sequence trade.
Do you have the correct settings in your strategy's settings to allow for multiple entries/exits per bar? (Just checking)

Image

Also, do you have the Bar Magnifier enabled? That way that the strategy is calculated multiple times per bar and can trigger the multiple take profit orders in one bar.

Image

stefanols
Posts: 51
Joined: 01 Jan 2014
Has thanked: 14 times
Been thanked: 2 times

Re: To execute a trade on same bar if possible

Postby stefanols » 03 Jul 2015

Brilliant Josh,

I have been looking and looking and thought it was something in the code.

Works just as I wanted.

Thanks and best regards Stefan


Return to “MultiCharts”