Download MetaTrader 5

Order openings at wrong points

To add comments, please log in or register
Traderole
32
Traderole  

Hey guys,

I coded an indicator that creates Support/Resistance lines. Now I've tried to create an EA which opens orders directly when market price touches a Support/Resistance lines. For some reason the orders are not being placed directly at the lines. Can somebody help me, please?


Here's my code

extern int magicnumber = 111;
extern double lotsize = 0.1;
extern int ATR_multiplier = 10;
extern int starttime = 8;
extern int endtime = 22;
extern int normalize_digits = 4; 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //---Custom_Indicator_Call-----
   //---Stoploss-----
   int ATR_timeframe = 0;
   int ATR_periode = 10;
   double ATR_value = iATR(NULL,ATR_timeframe,ATR_periode,1);
   
   double stopplossshort = Close[1] + ATR_value * ATR_multiplier;
   double stopplosslong = Close[1] - ATR_value * ATR_multiplier;
   //---TakeProfit-----
   
   double takeprofitshort = Close[1] - ATR_value * ATR_multiplier;
   double takeprofitlong = Close[1] + ATR_value * ATR_multiplier;
   //---Order_Condition-----
   double object_price, market_price;
   string object_name;
   
   if(ObjectsTotal(0,0,OBJ_TREND) == 0)
      {Alert("No objects available");}
   
   for(int i = 0; i <= ObjectsTotal(0,0,OBJ_TREND) - 1; i++)
      {
      object_name = ObjectName(i);
      object_price = NormalizeDouble(ObjectGetDouble(0,object_name,OBJPROP_PRICE1),normalize_digits);
      market_price = (NormalizeDouble(Ask,normalize_digits) + NormalizeDouble(Bid,normalize_digits)) / 2;
      
      //---Sell_Condition-----
      if(NormalizeDouble(Close[1],normalize_digits) < object_price && market_price == object_price && trading_hour() == true)
         {
            if(!OrderSend(Symbol(),OP_SELL,lotsize,Bid,5,stopplossshort,takeprofitshort,"",magicnumber,0,Red))
               {
               Alert("failed to send Sell order = ",GetLastError());
               }
               else Sleep(60000);
            }
         
      //---Buy_Condition-----
      if(NormalizeDouble(Close[1],normalize_digits) > object_price && market_price == object_price && trading_hour() == true)
         {
            if(!OrderSend(Symbol(),OP_BUY,lotsize,Ask,5,stopplosslong,takeprofitlong,"",magicnumber,0,Blue))
               {
               Alert("failed to send Sell order = ",GetLastError());
               }
               else Sleep(60000);
            }
         }
   
   //---Comment---
   Comment( "MN = ",magicnumber,
            "\nTradinghours = ",trading_hour(),
            "\nObjectstotal = ",ObjectsTotal(0,0,OBJ_TREND),
            "\nS/L = ",MarketInfo(Symbol(),MODE_STOPLEVEL),
            "\nSpread = ",MarketInfo(Symbol(),MODE_SPREAD),
            "\nATR_multiplier = ",ATR_multiplier,
            "\nmarket_price = ",NormalizeDouble(Bid,normalize_digits));
      
  }
//+------------------------------------------------------------------+


//---Check_Tradinghours--------------------------------------------------------------------- 
bool trading_hour()
   {
   if(starttime < TimeHour(TimeCurrent()) && TimeHour(TimeCurrent()) < endtime)
      {
      return (true);
      }
      else return(false);
   }
To add comments, please log in or register