Very simple EA but needs code to be looked at

 

Gidday

I am not a coder by any stretch of the imagination but you have to start somewhere.

Could someone please look over the code and point out where I have gone wrong.

extern double Lots = 0.1;
extern double MaximumRisk = 0.05;
extern double DecreaseFactor = 0;
extern string i3="DeltaStop";
extern int Mode=0; //0 - auto, 1 - manual
extern double DeltaPrice=50;
extern int ATRperiod=14;
extern double ATRratio=2.824;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string DeltaStop)
{
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()==MAGIC)
{
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=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/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) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor, 1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double DeltaStop;
int res;


DeltaStop=iCustom(NULL,1,"DeltaStop",Mode,DeltaPrice,ATRperiod, ATRratio, PRICE_OPEN,0);
//---- sell conditions
if(Close[1]<DeltaStop && Open[0]<DeltaStop)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid, 3, 0,0,"", MAGIC, 0,Red);
return;
}
//---- buy conditions
if(Close[1]>DeltaStop && Open[0]>DeltaStop)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask, 3,0, 0,"", MAGIC,0, Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double DeltaStop;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get DeltaStop
DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod, ATRratio, PRICE_OPEN,0);
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(Open[0]>DeltaStop && Close[1]>DeltaStop) OrderClose(OrderTicket(), OrderLots(),Bid,0,White);
break;
}
if(OrderType()==OP_SELL)
{
if(Open[0]<DeltaStop && Close[1]<DeltaStop) OrderClose(OrderTicket(), OrderLots(),Ask,0,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();

Cheers

 
extern double  Lots               = 0.1;
extern double  MaximumRisk        = 0.05;
extern double  DecreaseFactor     = 0;
extern string  i3="DeltaStop";
extern int     Mode=0; //0 - auto, 1 - manual
extern double  DeltaPrice=50;
extern int     ATRperiod=14;
extern double  ATRratio=2.824;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string DeltaStop)
  {
   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()==MAGIC)
        {
         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=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/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) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double DeltaStop;
   int    res;
 
 
   DeltaStop=iCustom(NULL,1,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_OPEN,0);
//---- sell conditions
   if(Close[1]<DeltaStop && Open[0]<DeltaStop)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGIC,0,Red);
      return;
     }
//---- buy conditions
   if(Close[1]>DeltaStop && Open[0]>DeltaStop) 
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGIC,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double DeltaStop;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get DeltaStop
   DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_OPEN,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
      //---- check order type
      if(OrderType()==OP_BUY)
        {
         if(Open[0]>DeltaStop && Close[1]>DeltaStop) OrderClose(OrderTicket(),OrderLots(),Bid,0,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[0]<DeltaStop && Close[1]<DeltaStop) OrderClose(OrderTicket(),OrderLots(),Ask,0,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
Thera are absent some braсkets in you code. Use button "MQL" for code inserting.
 
 
The above is what I am trying to achieve but I am getting this result below
Files:
dea.mq4  5 kb
 
Here is the indi
Files:
deltastop.mq4  3 kb
 
The opening condition uses "DeltaStop" with time frame 1 minute, while the closing condition uses "DeltaStop" with the chart time frame, which is 1 hour in your image. This might be your intended logic, or it might be the cause of your problem, or it might be irrelevant :-)
 

void CheckForOpen()
{
double DeltaStop;
int res;


DeltaStop=iCustom(NULL,1,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio, PRICE_OPEN,0);

iCustom(NULL,1,"DeltaStop....

check the meaning of 1 !!!!!!!!!!!!!

 
richplank:
The opening condition uses "DeltaStop" with time frame 1 minute, while the closing condition uses "DeltaStop" with the chart time frame, which is 1 hour in your image. This might be your intended logic, or it might be the cause of your problem, or it might be irrelevant :-)

thanks for the responce I am only looking for what is happening in pic 1 ie 1 entry and 1 exit not multiple. I am sorry but I don't know what you mean by  opening condition uses "DeltaStop" with time frame 1 minute, while the closing condition uses "DeltaStop" with the chart time frame, which is 1 hour in your image
 
I meant to point out that the second argument to iCustom(...) tells the time frame that the indicator operates on. This is expressed in minutes (often by using one of the pre-defined PERIOD_* constants), or by a 0, which then means "current chart time frame", i.e. the time frame of the chart that the EA was attached to.

In your code, the two ICustom calls have different second argument: the entering condition code uses 1, and the exiting condition code uses 0. Therefore, the EA decides to enter trades on the basis of the DeltaStep value for the current bar of the 1 minute time frame, and it decides to exit trades on the basis of the DeltaStep value for the current bar of the 1 hour time frame.

Perhaps you meant to have 0 as second argument in both cases, to make the EA logic operate relative to the chart time frame. Or perhaps you meant to have 1 as second argument in both cases, to make the EA logic always operate relative to the 1 minute time frame, regardless of which time frame you would drop it on.

As it is, if the 1 minute DeltaStep says enter, and the 1 hour DeltaStep says exit, the EA will, on every tick during that minute, alternate between opening a trade and closing it.
 
richplank:
I meant to point out that the second argument to iCustom(...) tells the time frame that the indicator operates on. This is expressed in minutes (often by using one of the pre-defined PERIOD_* constants), or by a 0, which then means "current chart time frame", i.e. the time frame of the chart that the EA was attached to.

In your code, the two ICustom calls have different second argument: the entering condition code uses 1, and the exiting condition code uses 0. Therefore, the EA decides to enter trades on the basis of the DeltaStep value for the current bar of the 1 minute time frame, and it decides to exit trades on the basis of the DeltaStep value for the current bar of the 1 hour time frame.

Perhaps you meant to have 0 as second argument in both cases, to make the EA logic operate relative to the chart time frame. Or perhaps you meant to have 1 as second argument in both cases, to make the EA logic always operate relative to the 1 minute time frame, regardless of which time frame you would drop it on.

As it is, if the 1 minute DeltaStep says enter, and the 1 hour DeltaStep says exit, the EA will, on every tick during that minute, alternate between opening a trade and closing it.

Thanks for the explanation I have changed them both to 0 but still seem to be getting the same results, I have been demo trading this manually and its a good little money spinner it's not going to make you filthy rich, but it should be steady as an EA, if I can get the thing to work.
 
I suppose you'd also like something to make the EA only act on the first tick on a bar.
A snippet like the following at the top of the CheckForOpen function might do the trick:
static datetime lasttime = 0;
if ( Time[0] == lasttime ) return;
lasttime = Time[0];