MQL5 Trailing Stops base on indicator

 

Hi Guys,


I'm fairly new to MQL5 as a language having mainly been coding with python and associated languages in my day to day work but I'm trying to learn MQL5.


I've been following a lot of the tutorials etc to get a general grasp on the language but I'm strugging with modifying the stop's on a position. It seems much easier in MQL4 which I've used in the past but trying to move to MQL5 instead.


This code below is based overall on one of the tutorials I found on the mql5 site. This is within OnTick

//--- Trailing SL's
// any opened Buy position?
   if(m_Position.SelectByMagic(_Symbol, Buy_Magic))                     //if the position for this symbol already exists
     {
      if(m_Position.PositionType()==POSITION_TYPE_BUY)
        {
         if(m_Position.StopLoss() < iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE))
            m_Trade.PositionModify(m_Position.Ticket(), iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE), 0);
        }
     }

// any opened Sell position?
   if(m_Position.SelectByMagic(_Symbol, Sell_Magic))                     //if the position for this symbol already exists
     {
      if(m_Position.PositionType()==POSITION_TYPE_SELL)
        {
         if(m_Position.StopLoss() > iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE))
               m_Trade.PositionModify(m_Position.Ticket(), iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE), 0);
        }
     }

I'm trying as an example to make the SL move with a moving average.


It compiles fine and the EA runs but it never modifies any stop loss.


Any tips on what I'm doing wrong would be greatly appriciated!


Thanks

 
Draniage:

Hi Guys,


I'm fairly new to MQL5 as a language having mainly been coding with python and associated languages in my day to day work but I'm trying to learn MQL5.


I've been following a lot of the tutorials etc to get a general grasp on the language but I'm strugging with modifying the stop's on a position. It seems much easier in MQL4 which I've used in the past but trying to move to MQL5 instead.


This code below is based overall on one of the tutorials I found on the mql5 site. This is within OnTick

I'm trying as an example to make the SL move with a moving average.


It compiles fine and the EA runs but it never modifies any stop loss.


Any tips on what I'm doing wrong would be greatly appriciated!


Thanks

Read the documentation on iMa and CopyBuffer.
iMa needs to be called only once from OnInit. It returns a handle.
Then use CopyBuffer during OnTick to access the buffer data.
 
La_patates:
Read the documentation on iMa and CopyBuffer.
iMa needs to be called only once from OnInit. It returns a handle.
Then use CopyBuffer during OnTick to access the buffer data.

Thanks, I just put this in as a test. I've been using the copy buffer up until getting so stuck I'd ask for some help.


I've read the documentation but as is fairly normal with metaquotes where its missing sections when trying to learn a language.


I've modified the code to reflect that. The 'fastMA' is a buffer copied from the iMA and working in the entry section of the code but nothing in this modifier.


//--- Trailing Stops
   if(m_Position.SelectByMagic(_Symbol, Buy_Magic))
     {
      if(m_Position.PositionType()==POSITION_TYPE_BUY)
        {
         if(m_Position.PriceOpen() < fastMA[0])
           {
            if(m_Position.StopLoss() < fastMA[0])
               m_Trade.PositionModify(_Symbol, fastMA[0],0);
              }
        }
     }

   if(m_Position.SelectByMagic(_Symbol, Sell_Magic))
     {
      if(m_Position.PositionType()==POSITION_TYPE_SELL)
        {
         if(m_Position.PriceOpen() > fastMA[0])
           {
            if(m_Position.StopLoss() > fastMA[0])
               m_Trade.PositionModify(_Symbol, fastMA[0],0);
              }
        }
     }
 
Draniage:

Thanks, I just put this in as a test. I've been using the copy buffer up until getting so stuck I'd ask for some help.


I've read the documentation but as is fairly normal with metaquotes where its missing sections when trying to learn a language.


I've modified the code to reflect that. The 'fastMA' is a buffer copied from the iMA and working in the entry section of the code but nothing in this modifier.


There are no error in that section of code, it is somewhere else in the logic leading to it

1. You are using 2 magic number but only one instance of CTrade. Are you resetting the Magic number before each trade? if so, don't. almost always better to use only one Magic, to be set in OnInit(). Different direction doesn't require a new magic.

2. if used on Hedging account, do not modify your position based on symbol, but by ticket number instead. Neither would SelectByMagic() do you any good. only to be used on netting account.

3. have you verified that fastMA contains the proper value?

4. (edit) copy your fastMA value in a double variable that you NormalizeDouble before doing any comparison or attempting to modify your stoploss.
 
Draniage :


Simple iMA Trailing :

Utility Expert Advisor: Simple Trailing by iMA (Moving Average, MA) indicator

Simple iMA Trailing

Reason: