need help with code

 

Hello,

I have never programmed before and am new to MQL4 and looking for some help when I come across something I can not figure out.

I am working on programing an EA that places trades at the break of a fractal line.

My first problem is my rule for not placing a trade if there is an open order is not being followed.

The problem is with the following lines of code:

[ if (allowbuy==1 && Close[0]<frac_up) ] <-----I want only one buystop order to be open at one time

[ if (allowsell==1 && Close[0]>frac_dn) ] <-----I want only one sellstop order to be open at one time

Below is the complete code:

extern double RiskPercent = 2;
extern double space = 6;
extern int MaxOrders = 6;
extern string AccountType = "Cent=0.001, Micro=0.01, Mini=0.1, Standard=1.0";
extern double AccType = 0.01;
extern int Fractal_TF = 60;
double TickValue,PipsAtRisk,AccDollarToRisk,SLDollarAmount,Lots,LotValue,frac_up,frac_dn,takeprofit,stoploss,entry,takeprofit1,stoploss1,entry1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}


//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

int cnt, ticket, total,prevbuystop,TrailingStop,isCrossed;
bool allowsell=true;
bool allowbuy=true;
total = OrdersTotal();
double frac_up=iCustom(NULL,Fractal_TF,"FractalsLine",0,0); //returns value of blue fractal line
double frac_dn=iCustom(NULL,Fractal_TF,"FractalsLine",1,0); //returns value of red fractal line
int spread=MarketInfo(Symbol(), MODE_SPREAD);
double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE)*AccType;
double PipsAtRisk = (frac_up - frac_dn)/Point;
double AccDollarToRisk = (AccountEquity()*RiskPercent) /100;
double SLDollarAmount= PipsAtRisk * TickValue;
double Lots = AccDollarToRisk / SLDollarAmount;
double LotValue = Lots * TickValue;
double takeprofit1 = frac_up+PipsAtRisk*Point;
double stoploss1 = frac_dn-space*Point;
double entry1 = frac_up+space*Point;
double takeprofit = frac_dn-PipsAtRisk*Point;
double stoploss = frac_up+space*Point;
double entry = frac_dn-space*Point;

//---- Counting/Modifying orders ----

int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
}
if (OrderSymbol()==Symbol()&& OrderType()== OP_SELLSTOP)
{
if (OrderOpenPrice() != entry)
{
OrderModify(OrderTicket(),entry,stoploss,takeprofit,0,0);
}
else
{
allowsell=false;
}
}
if (OrderSymbol()==Symbol()&& OrderType()== OP_BUYSTOP)
{
if (OrderOpenPrice() != entry1)
{
OrderModify(OrderTicket(),entry1,stoploss1,takeprofit1,0,0);
}
else
{
allowbuy=false;
}
}


//---- Order Entry Section ----

if (total < MaxOrders)
{
if (allowbuy==1 && Close[0]<frac_up) //only opens buystop order if buystops are equal to 0
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,LotValue,entry1,3,stoploss1,takeprofit1,NULL,0,0,NULL);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUYSTOP opened : ",OrderOpenPrice());
}
else Print("Error opening BUYSTOP order : ",GetLastError());
}

if (allowsell==1 && Close[0]>frac_dn)
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,LotValue,entry,3,stoploss,takeprofit,NULL,0,0,NULL);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELLSTOP order opened : ",OrderOpenPrice());
}
else Print("Error opening SELLSTOP order : ",GetLastError());
}
}
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

Reason: