Help porting this little piece of code to mql5

To add comments, please log in or register
AWer1001
56
AWer1001  

Hello everyone and thanks in advance.

My problem is the following, in MQL4 I can use the following code:

double priceopen = OrderOpenPrice();
MqlDateTime time;
TimeToStruct(OrderOpenTime(),time);
int candle = time.hour;
double atr = iATR(_Symbol,_Period,candle,0);

Once I have the atr value i then (if buying substract it to the open price to determine my stop or if selling sum it) and everything works well.

But in MQL5 I can't seem to get the same result. I have tried using the same approach shown in https://www.mql5.com/en/articles/4318 and the 'original' indicator handling mode of the language but everytime i get that atr is 0 and therefore get invalid stoploss error. The only thing that I managed to do was to create the 24 atrs handlers in the OnInit() function but I know this is not only memory consuming but when I backtest my idea I have 24 indicators + other one I use to determine entries drown in my chart.

I would really be thankful if anyone could point me in the right direction as I have the intention and the conviction that I can learn how to solve this problem.

Here is the attempt I did with the article style:

void EditMarketOrder()
{
  MqlTradeRequest request={0};
  MqlTradeResult result={0};
  datetime day = 0;
  datetime hour = 0;
  double priceopen = PositionGetDouble(POSITION_PRICE_OPEN);
  if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
    if(PositionGetDouble(POSITION_SL) == 0)
      CopyTime(_Symbol,PERIOD_D1,0,1,today); //copy today D1 time.
      day = today[0];
      CopyTime(_Symbol,_Period,0,1,now); //copy current timeframe time.
      hour = now[0];
      long difference = hour-day;
      long realcandle = 0;
      if(difference == 0) realcandle = 8; //if the time of now is 00:00 then atr is 8. else realcandle is diff
      if(difference != 0) realcandle = difference/3600;
      double atr = iATRMQL4(_Symbol,_Period,(int)realcandle,0);
      request.action = TRADE_ACTION_SLTP;
      request.symbol = _Symbol;
      request.position = PositionGetInteger(POSITION_IDENTIFIER);
      request.sl = priceopen-(atr*2);
      request.tp = priceopen+(atr*2);
      if(!OrderSend(request,result)) ErrorManager();
      ZeroMemory(request); ZeroMemory(result);
      ZeroMemory(today); ZeroMemory(now);  
  //SELL    
}

double iATRMQL4(string symbol,ENUM_TIMEFRAMES period,int ma_period,int shift)
{
  double result = 0;
  int handle = iATR(symbol,period,period);
  double val[1];
  int copied = CopyBuffer(handle,MAIN_LINE,shift,1,val);
  if(copied>0)result=val[0];
  else(ErrorManager());
  return(result);  
}
LifeHack for traders: Fast food made of indicators
LifeHack for traders: Fast food made of indicators
  • 2018.03.09
  • Vladimir Karputov
  • www.mql5.com
If you want something forbidden really bad, then it is allowed.  Russian proverb Simplicity vs Reliability Back in 2005, in the newly released MetaTrader 4, the simple MQL-II script language was replaced by MQL4. As funny as it may seem today, many traders met the new C-like language with hostility. There were many furious debates and...
whroeder1
17942
whroeder1  
int candle = time.hour;
double atr = iATR(_Symbol,_Period,candle,0);
Why would you want variable length ATR, and length being a function of the hour it opened. A zero length ATR (bogus) if the order was opened at midnight, or a 12 bar ATR if it was opened at noon.
AWer1001
56
AWer1001  
whroeder1:
Why would you want variable length ATR, and length being a function of the hour it opened. A zero length ATR (bogus) if the order was opened at midnight, or a 12 bar ATR if it was opened at noon.
If the order was opened at midnight (00:00) then
if(difference == 0) realcandle = 8; //if the time of now is 00:00 then atr is 8. else realcandle is diff

the reason I want a dynamic atr is that I had already been asked to code this for mql4 and I am trying to slowly port many of my codes to mql5

still even when realcandle is 8 the atr will return 0
whroeder1
17942
whroeder1  
That isn't the code you posted.
AWer1001
56
AWer1001  
whroeder1:
That isn't the code you posted.
I think i am lost in this . I did post two pieces of code first one from mql4 that work perfectly and the other one (larger) from mql5 which works until the point were the returned value is always 0. Therefore priceopen + 0 = Invalid stop
To add comments, please log in or register