Position Controller using Global Variables and IOG and allowing multiple entries and exits per bar

Questions about MultiCharts and user contributed studies.
DanielFXS
Posts: 29
Joined: 08 Oct 2019
Has thanked: 10 times
Been thanked: 4 times

Position Controller using Global Variables and IOG and allowing multiple entries and exits per bar

Postby DanielFXS » 07 Nov 2019

Hi Everyone,
I've coded a position controller than adds and subtracts the total contracts needed, taking market positions from different charts. However I can only get it to work when applied to a 1 tick chart and entries and exits limited to once per bar.
If I allow multiple entries per bar it will quickly start adding and subtracting market positions failing to find equilibrium. At first I thought maybe a delay from global variables was causing my issue however I think its due to when multiple entries is checked multiple orders are sent to broker in between each tick. Since the script recalculates per tick it's always chasing its tail.
Ideally I'd like to have the script applied to a 1 hour chart and allow it to enter and exit multiple times within each bar. I was trying to put some sort of only send one order per tick speed bump in place but failed.
If anyone knows a way to fix this or even has a better solution please let me know. Thanks.

Code: Select all

[IntrabarOrderGeneration = True];
[AllowSendOrdersAlways = True];
[SameExitFromOneEntryOnce = False] ;
////Strategy Inputs////
Vars: Strategy1( 0 ) ,
Strategy2( 0 ),
MP( 0 ) ,
Entry_Exit_Time( 0 );

Strategy1 = GVGetNamedInt(getsymbolname + NumToStr(1, 1), 1);
Strategy2 = GVGetNamedInt(getsymbolname + NumToStr(2, 1), 2);

//***********************************************
//COMBINE RESULTS, FIGURE OUT NET POSITION

//we have to figure out how many long or short we should be
var:totalcons(0),currentcons(0),constobuy(0),constosellshort(0);
totalcons= Strategy1 + Strategy2;
//so now totalcons should be the final position, after all calculations
MP = MarketPosition*currentcontracts;
currentcons = MP ;
//make variables cons to buy, cons to sell
constobuy=0;
constosellshort=0;
if totalcons <> currentcons then begin
//BUY SIDE
If totalcons>0 then begin
If currentcons<=0 then constobuy= totalcons;
If currentcons>0 then begin
if totalcons<currentcons then begin
Sell currentcons-totalcons contracts total next bar at market;
end;
if totalcons>currentcons then constobuy= totalcons-currentcons;
end;
buy constobuy contracts next bar at market;
end;
//SELL SIDE
If totalcons<0 then begin
If currentcons>=0 then constosellshort= -(totalcons);
If currentcons<0 then begin
If totalcons>currentcons then begin
Buytocover -(currentcons-totalcons) contracts total next bar at market;
//print("Exit Short", currentcons+totalcons);
end;
If totalcons<currentcons then constosellshort= -(totalcons-currentcons) ;
end;
Sellshort constosellshort contracts next bar at market;
end;

end;
If totalcons=0 then Sell All contracts next bar at market;
If totalcons=0 then BuyToCover All contracts next bar at market;

Capture.PNG
(21.35 KiB) Not downloaded yet

Return to “MultiCharts”