why it's so hard to code an EA
it doesn't trade sell order, if I delete buy order codes, it triggers sell order codes .. what is wrong with it??? beside when day starts, it begins trading at the beginning of day, it seems it doesn't see conditions. thanks
Inserted Code
extern double TP = 35; // Profit Lock in pips
extern double SL = 25; // Profit Lock in pips
extern int MagicNum = 77;
input double TradePercentage =0.05;
input double DecreaseFactor =1;
input int TimeFrame = PERIOD_D1; //PERIOD_M1 PERIOD_M5 PERIOD_M15 PERIOD_M30 PERIOD_H1 PERIOD_H4 PERIOD_D1
double LMB1;
double LMS1;
double LMB2;
double LMS2;
double LMB3;
double LMS3;
//+------------------------------------------------------------------+
int init()
{
return(0);
}
int deinit()
{
return(0);
}
int start()
{
int i;
for (i=0; i<OrdersTotal(); i++)
{
OrderSelect(SELECT_BY_POS, MODE_TRADES);
if (Hour() == 23 && Minute()>35)
CloseTicket(OrderTicket());
}
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0)
CheckForOpen();
return(0);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
double Poin = Point;
if ((Point == 0.00001) || (Point == 0.001)) Poin *= 10;
if(Volume[0]>1) return;
LMB1 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",1,0),0);
LMS1 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",2,0),0);
LMB2 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",3,0),0);
LMS2 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",4,0),0);
LMB3 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",5,0),0);
LMS3 = NormalizeDouble(iCustom(Symbol(),TimeFrame,"Percentage DMove with Buffers.mq4",6,0),0);
//--- buy conditions
if ((LMB1!=EMPTY_VALUE || LMB2!=EMPTY_VALUE || LMB3!=EMPTY_VALUE))
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,NormalizeDouble(Ask-SL*Poin,Digits),NormalizeDouble(Ask+TP*Poin,Digits),"",MagicNum,0,clrRoyalBlue);
if (res == -1)
Print("Error buying - ", GetLastError());
return;
}
//--- sell conditions
if ((LMS1!=EMPTY_VALUE || LMS2!=EMPTY_VALUE || LMS3!=EMPTY_VALUE))
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,NormalizeDouble(Bid+SL*Poin,Digits),NormalizeDouble(Bid-TP*Poin,Digits),"",MagicNum,0,clrOrangeRed);
if (res == -1)
Print("Error buying - ", GetLastError());
return;
}
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
int CloseTicket(int ticket){
double price = 0;
RefreshRates();
OrderSelect(ticket, SELECT_BY_TICKET);
if (OrderType() == OP_BUY){
price = Bid;
}
if (OrderType() == OP_SELL){
price = Ask;
}
if (price > 0){
return(OrderClose(ticket, OrderLots(), price, 100, Yellow));
}else{
return(OrderDelete(ticket));
}
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNum)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=0.0;
int orders=HistoryTotal(); // history orders total
//--- select lot size
lot = NormalizeDouble(AccountFreeMargin()*TradePercentage/(DecreaseFactor*1000.0),1);
//--- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) lot=NormalizeDouble(AccountFreeMargin()*TradePercentage/(DecreaseFactor*1000.0),1);
if(OrderProfit()<0) lot=NormalizeDouble(AccountFreeMargin()*TradePercentage/(DecreaseFactor*1000.0),1);
}
}
//--- return lot size
if(lot<0.01) lot=0.01;
return(lot);
}