Need a help on debugging my EA based on my strategy

 

Hi friends. I hope that you are fine. I built an strategy and I want to make an EA of it. I wrote the basics code which I'll be sharing with you but it doesn't do what I want from it. I'll attach a picture to this post. In this picture you will see four vertical lines. I'll tell you what those lines mean. The the blue line is on the candle that shows an arrow up after its last arrow which is down with a red vertical line on the candle. The candle after the candle which has a blue vertical line is my  long entry. My exist will be the candle which has an arrow down and a brown vertical line on it. Also notice that the last arrow is an up one which is on the candle with a green vertical line. At this exit, I want to open a short position too. and exit when a arrow up appears (the candle after it at open price) and not to mention that there's an arrow down before it. and here again is a long position entry and this cycle goes on and on with an stoploss of 100 pips. I hope you can help me with the code.

here's my basic code (I've already defined the sell conditions and buy conditions but for obvious reasons I can't share them here): 

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=100;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  string signal="";
  double last_signal;
  bool buy_condition_1 = 
  bool buy_condition_2 = 
  bool buy_condition_3 = 
  bool buy_condition_4 = 
  bool buy_condition_5 = 
  bool sell_condition_1 = 
  bool sell_condition_2 = 
  bool sell_condition_3 = 
  bool sell_condition_4 = 
  bool sell_condition_5 = 
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  
     int result=0;
     if(buy_condition_1 && buy_condition_2 && buy_condition_3 && buy_condition_4 && buy_condition_5)
     {
     signal="buy";
     }
     if(sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5)
     {
     signal="sell";
     }
     if( last_signal==signal=="sell" && signal=="buy" && TotalOrdersCount()==0 )
     { 
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if( last_signal==signal=="buy" && signal=="sell" && TotalOrdersCount()==0 )
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if( signal=="sell" )
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if( signal=="buy")
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
     return(0);
  }

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
Files:
Screenshot.png  129 kb
 
You never set last_signal. And there's no need to use a double for this. Also you never check for signal=="sell". When looping through open orders always go from back to front: for(int i=OrdersTotal()-1;i>=0;i--)
 
lippmaje:
You never set last_signal. And there's no need to use a double for this. Also you never check for signal=="sell". When looping through open orders always go from back to front: for(int i=OrdersTotal()-1;i>=0;i--)

Thanks for your comment.

About the second part yes, it was just a mistake :)

About the first part, I want to count the candles backward and when I find the first arrow up or arrow down, or in other words, signal="buy" or signal="sell", make a position according to conditions (which I said above in the first post). So about the Third part I think it was a mistake of mine to look for a OP_BUY or OP_SELL.

And what I forgot to say is that I want to make the position a candle after the signal candle, and also do this for closing the position a candle after the signal candle, to make sure that it was a correct signal, obviously because my strategy repaints.

So That's all I'm looking for and I don't know what to do about it.

 
Alirzen:

And what I forgot to say is that I want to make the position a candle after the signal candle, and also do this for closing the position a candle after the signal candle, to make sure that it was a correct signal, obviously because my strategy repaints.

In this case it's better to add a new bar check and when a new candle opens look back at the previous one if it got a signal (or even the 2nd last one.)

Reason: