EA opening new position with every tick

 

I'm working on coding my manual strategy into my first EA, and I've run into a problem while back-testing. Originally, my code was opening a new position with every tick (when criteria was met). This resulted in multiple positions, when I only wanted one position (eventually eating up all margin in a matter of minutes). I re-coded and part of the code is below. This code is opening one position, which is what I wanted, but when this position closes, it won't open another position if the criteria is met. I know it is doing this because the requirement for Globalx to not equal zero will never be met after the initial tick is received (because it can only be reset within the loop). If I move the second "if" outside the first "if", then no positions will open at all. I've searched the forum and I can't find an answer. I think I missing something simple. I hope this makes sense to someone. Any help someone might provide is greatly appreciated. Thanks!

int Globalx = 1;


int init()
{
return(0);
}

int start()
{
double Daily_High = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,24,Hour()+1)],4); //Calculates high from market open to close for previous day
double Daily_Low = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,24,Hour()+1)],4); //Calculates low from market open to close for previous day
double Daily_Range = Daily_High - Daily_Low; //Calculates range of high and low from open to close for previous day
double Stop = Ask - Daily_Range / 2; //Calculates where to put stop loss
double Lots = NormalizeDouble(((AccountBalance()*(0.025))/(Daily_Range/2*100000)),2); //Lots to trade which would result in loss of 2.5% of account at stop loss

if((Ask <= Daily_Low) && (Globalx != 0))
{
int Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stop,Daily_High); //Opens 1 long position with limit of daily high
OrderSelect(Ticket,SELECT_BY_TICKET);
if(OrderCloseTime() != 0)
{
Globalx = 1;
}
else Globalx = 0;
}
return(0);
}

int deinit()
{
return(0);
}

 
PIPPilot wrote >>

I'm working on coding my manual strategy into my first EA, and I've run into a problem while back-testing. Originally, my code was opening a new position with every tick (when criteria was met). This resulted in multiple positions, when I only wanted one position (eventually eating up all margin in a matter of minutes). I re-coded and part of the code is below. This code is opening one position, which is what I wanted, but when this position closes, it won't open another position if the criteria is met. I know it is doing this because the requirement for Globalx to not equal zero will never be met after the initial tick is received (because it can only be reset within the loop). If I move the second "if" outside the first "if", then no positions will open at all. I've searched the forum and I can't find an answer. I think I missing something simple. I hope this makes sense to someone. Any help someone might provide is greatly appreciated. Thanks!

int Globalx = 1;


int init()
{
return(0);
}

int start()
{
double Daily_High = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,24,Hour()+1)],4); //Calculates high from market open to close for previous day
double Daily_Low = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,24,Hour()+1)],4); //Calculates low from market open to close for previous day
double Daily_Range = Daily_High - Daily_Low; //Calculates range of high and low from open to close for previous day
double Stop = Ask - Daily_Range / 2; //Calculates where to put stop loss
double Lots = NormalizeDouble(((AccountBalance()*(0.025))/(Daily_Range/2*100000)),2); //Lots to trade which would result in loss of 2.5% of account at stop loss

if((Ask <= Daily_Low) && (Globalx != 0))
{
int Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stop,Daily_High); //Opens 1 long position with limit of daily high
OrderSelect(Ticket,SELECT_BY_TICKET);
if(OrderCloseTime() != 0)
{
Globalx = 1;
}
else Globalx = 0;
}
return(0);
}

int deinit()
{
return(0);
}

Include a magicnumber in your ordersend function. Then add a code to check if an order already exists for this magicnumber.

include the order check function like this...if((Ask <= Daily_Low) && (OrderCheck == 0))

If an order exist, make the order check function return 1. If no order exists, make it return zero.

 
Tom23824 wrote >>

Include a magicnumber in your ordersend function. Then add a code to check if an order already exists for this magicnumber.

include the order check function like this...if((Ask <= Daily_Low) && (OrderCheck == 0))

If an order exist, make the order check function return 1. If no order exists, make it return zero.

Thanks Tom!

Reason: