Download MetaTrader 5

Code issues - My entries are incorrect versus my code

To add comments, please log in or register
David Gladwin
110
David Gladwin  

Hello,

Building a strategy for practice based on bollinger bands and stochastics.
What should happen is that if the close of a 5 minute bar is above the upper band and the stochastic has gone above 80, a sell order should enter, and conversely lower band 20.

However when I run it to test in mt4, I notice that the trades are wildly off the entry mark. Rather than opening at the close of a bar, they are several pips too high or too low. What am I doing wrong? 

This is my code:

extern int MagicNumber=1;
extern double Lots =1;
extern double StopLoss=5;
extern double TakeProfit=15;
extern int TrailingStop=0;
int ThisBarTrade = 0;
int start() {
   double MyPoint=Point;
   if(Digits==3 || Digits==5) MyPoint=Point*10;
      double TheStopLoss=0;
      double TheTakeProfit=0;
      if(TotalOrdersCount() == 0) {
         if (Bars != ThisBarTrade) {
            ThisBarTrade = Bars;
            int result=0;
            if((Open[0]<iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0))&&(iStochastic(NULL,0,12,1,3,MODE_SMA,1,MODE_MAIN,0)<20)&& (OrdersTotal() == 0)) // Here is your open buy rule
            {
               result=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",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((Open[0]>iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0))&&(iStochastic(NULL,0,12,1,3,MODE_SMA,1,MODE_MAIN,0)>80)&& (OrdersTotal() == 0)) // Here is your open Sell rule
            {
               result=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"",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(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(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);
}

To add comments, please log in or register