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;