OrderModify() Problem

 

Hi everyone,


I need some help please to finish this EA.

I am struggling to basically modify a pending order (SL and TP) only once at the next open bar. 

//+------------------------------------------------------------------+
//|                                              Simple strategy.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double Lot=0.02;
int MagicBuy=15;
int LastBars=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  
  {
  
  
  
  // double ema
  double EMA8=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA13=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA21=iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
  
  double EMA8H1=iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA21H1=iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,0);
  
  

  RefreshRates();
  
  int HighOf5=iHighest(Symbol(),0,MODE_HIGH,6,0); // getting index of the highest last 5 bar
  double BuyLevel=iHigh(Symbol(),0,HighOf5)+30*Point; // getting value of buy level
  
  
  
  double StopLoss=NormalizeDouble(iClose(Symbol(),0,1)-30*Point,Digits); // getting stoploss value
  double Risk=NormalizeDouble(BuyLevel-StopLoss,Digits); // getting risk value
  double TakeProfit=NormalizeDouble(BuyLevel+(Risk*2),Digits);// getting take profit
  
  
  
  
  for (int i = 0; i < OrdersTotal() ; i++)
  
    {
   
        int TriggerBarShift=iBarShift(Symbol(),0,OrderOpenTime());
        int CurrentBar=iBarShift(Symbol(),0,TimeCurrent());
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
            if (OrderSymbol() ==Symbol()) 
                if (OrderMagicNumber() == MagicBuy)
                  
                  if (TriggerBarShift==CurrentBar+1) 

                   bool  ordermodified=OrderModify(OrderTicket(),BuyLevel,StopLoss,TakeProfit,0,clrAliceBlue);
                   
                    
                    
    }
  
  
 
  
 
   if(LastBars!=Bars) // if last bar is different than actual bar
   LastBars=Bars; // last bar = Bar 
  
  // Buy conditions
  
  if (EMA8>EMA13 && EMA13>EMA21) // ema M5 are up trend
  if (EMA8H1>EMA21H1) // ema H1 are up trend
  if (Close[1]>EMA8 && Open[1]>EMA8) // checking if candle before trigger bar is over ema 8 
  if (Bid<EMA8) // checking if price is under ema 8 
  if (OrdersTotal()<1) // checking if number of trades is < 1
  
  {
  int Openbuy=OrderSend(Symbol(),OP_BUYSTOP,Lot,BuyLevel,5,0,0,NULL,MagicBuy,0,clrBlueViolet); // buy order 
  
  } 
  
  return(0);}
//+------------------------------------------------------------------+

I tried to get the bar shift of the order opening bar and to compare it to the current bar but it doesn't seems to work, I did tried with OrderOpenTime as well but without success.

I would like to modify the order only once as well which I stupidly struggle to do... I don't want to modify it at every bars but only at the bar after the opening order bar. 


I hope it does makes sense and that someone can help me. 

I attach my code.


Thank you

 
//+------------------------------------------------------------------+
//|                                              Simple strategy.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double Lot=0.02;
int MagicBuy=15;
int LastBars=0;


bool flag = false; // new variable for 1 modify
datetime new_time = 0; // new variable for 1 time
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start() // NO start(), use void OnTick()
  
  {
  
  
  
  // double ema
  double EMA8=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA13=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA21=iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
  
  double EMA8H1=iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,0);
  double EMA21H1=iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,0);
  
  

  RefreshRates();
  
  int HighOf5=iHighest(Symbol(),0,MODE_HIGH,6,0); // getting index of the highest last 5 bar
  double BuyLevel=iHigh(Symbol(),0,HighOf5)+30*Point; // getting value of buy level
  
  
  
  double StopLoss=NormalizeDouble(iClose(Symbol(),0,1)-30*Point,Digits); // getting stoploss value
  double Risk=NormalizeDouble(BuyLevel-StopLoss,Digits); // getting risk value
  double TakeProfit=NormalizeDouble(BuyLevel+(Risk*2),Digits);// getting take profit
  
  
  
  if(new_time != Time[0] && flag == false) // check new bar and no modification last
    {
  for (int i = 0; i < OrdersTotal(); i++)
  
    {
   
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
          {
            if (OrderSymbol() ==Symbol()) 
              {
                if (OrderMagicNumber() == MagicBuy)
                  {
                   if( OrderModify(OrderTicket(),BuyLevel,StopLoss,TakeProfit,0,clrAliceBlue) == true)
                      {
                       flag = true; // modification note
                      }
                  }
               }
          }
                   
                    
                    
    }
  }
  
  
 
  
 
   if(LastBars!=Bars) // if last bar is different than actual bar
   LastBars=Bars; // last bar = Bar 
  
  // Buy conditions
  
  if (EMA8>EMA13 && EMA13>EMA21) // ema M5 are up trend
  if (EMA8H1>EMA21H1) // ema H1 are up trend
  if (Close[1]>EMA8 && Open[1]>EMA8) // checking if candle before trigger bar is over ema 8 
  if (Bid<EMA8) // checking if price is under ema 8 
  if (OrdersTotal()<1) // checking if number of trades is < 1
  
  {
  if( OrderSend(Symbol(),OP_BUYSTOP,Lot,BuyLevel,5,0,0,NULL,MagicBuy,0,clrBlueViolet) != -1) // buy order 
    {
     flag = false; // unmarking a modification
     new_time = Time[0]; // fixing the time of the current bar
    }
  
  } 
  
  return(0);} // Delete it, if you use void OnTick()
//+------------------------------------------------------------------+
 
But you are writing the code altogether wrong. Your approach is outdated, it can give a lot of inaccuracies and misfires.
 
Nikita Chernyshov:
But you are writing the code altogether wrong. Your approach is outdated, it can give a lot of inaccuracies and misfires.
Thank you for your help, I'll definitely try that. Sorry about my poor coding skills but I'll try hard to learn. 
 
Nikita Chernyshov:
But you are writing the code altogether wrong. Your approach is outdated, it can give a lot of inaccuracies and misfires.

I am sorry to be annoying but there is something I don't get, when I run my EA on the backtest it opens an order even if the buy stop level hasn't been reached do you have any idea where this error could come from ? 

Thanks for your help, my sl, tp and buy level are now set properly ! finally !

Reason: