Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1631

 

Good afternoon!!!

Help me out here. I am adding a trailing stop to a grid EA, so far only to the first order. Trail has not triggered yet. What is the problem?

Here is the code fragment where single orders are opened

double JAW = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
     double TEETH = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
     double LIPS = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
     double DI_PLUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_PLUSDI,0);
     double DI_MINUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_MINUSDI,0);
     double MacdCurrent=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,0);
     double MacdPrevious=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,2 ); 
     double ATR = iATR(Symbol(), TimeframesVolatility, BarCount, 0);
     if (CountTrade() == 0)
     { 
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS>TEETH&& TEETH>JAW&&DI_PLUSCurrent>18&&DI_PLUSCurrent>DI_MINUSCurrent&&MacdCurrent>MacdPrevious)                              
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Ask + TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_BUY, FirstLots, Ask, slip, 0, tp, "1-ый ордер", Magic, 0, Blue); 
            {
             if(Bid - OrderOpenPrice() > TrailingStop*Point) 
              Trailing();
            }
         }
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS<TEETH&& TEETH<JAW&&DI_MINUSCurrent>18&&DI_MINUSCurrent>DI_PLUSCurrent&&MacdCurrent<MacdPrevious)                            
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Bid - TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_SELL, FirstLots, Bid, slip, 0, tp, "1-ый ордер", Magic, 0, Red); 
           {
           if(OrderOpenPrice() - Ask > TrailingStop*Point)
              Trailing();
           }
         }
       }

Here is the modification function for single orders

//+----------------------------------------------------------------------------+
//| Трейлинг стоп одиночных ордеров                                            |
//+----------------------------------------------------------------------------+
void Trailing()
{
   for(int i = OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         {
           if(OrderType() == OP_BUY)
           {
             if(Bid - OrderOpenPrice() > TrailingStop*Point || OrderStopLoss() == 0)
             {
                if(OrderStopLoss() < Bid - (TrailingStep + TrailingStop)*Point || OrderStopLoss() == 0)
                {
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - TrailingStop*Point, Digits), tp, 0))
                    Print("Ошибка модификации ордера на покупку!");
                }
             }
           }
           if(OrderType() == OP_SELL)
           {
             if(OrderOpenPrice() - Ask > TrailingStop*Point || OrderStopLoss() == 0)
             {
                if(OrderStopLoss() > Ask + (TrailingStep + TrailingStop)*Point || OrderStopLoss() == 0)
               {
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask + TrailingStop*Point, Digits), tp, 0))
                    Print("Ошибка модификации ордера на продажу!");
               }
             }
           }
         }
      }
   }
}

Thank you!!!!

 
EVGENII SHELIPOV #:

Good afternoon!!!

Help me out here. I am adding a trailing stop to a grid EA, so far only to the first order. Trail has not triggered yet. What is the problem?

Here is the code fragment where single orders are opened

Here is the modification function for single orders

Thank you!!!!

You only have recourse to a trawl when there are no orders...
 
MakarFX #:
You're right, it works...there's an error in the docks.
Multi-currency mode in1345 build works). I checked it on 10 majors, it gets data from all 10 pairs and draws whatever you want)
And pushing buttons in the tester)
 
MakarFX #:
You only have a call to the trawl when there are no orders...

Changed nothing has changed

void OnTick()
{
     double JAW = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
     double TEETH = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
     double LIPS = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
     double DI_PLUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_PLUSDI,0);
     double DI_MINUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_MINUSDI,0);
     double MacdCurrent=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,0);
     double MacdPrevious=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,2 ); 
     double ATR = iATR(Symbol(), TimeframesVolatility, BarCount, 0);
     if (CountTrade() == 0)
     { 
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS>TEETH&& TEETH>JAW&&DI_PLUSCurrent>18&&DI_PLUSCurrent>DI_MINUSCurrent&&MacdCurrent>MacdPrevious)                              
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Ask + TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_BUY, FirstLots, Ask, slip, 0, 0, "1-ый ордер", Magic, 0, Blue); 
         }
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS<TEETH&& TEETH<JAW&&DI_MINUSCurrent>18&&DI_MINUSCurrent>DI_PLUSCurrent&&MacdCurrent<MacdPrevious)                            
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Bid - TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_SELL, FirstLots, Bid, slip, 0, 0, "1-ый ордер", Magic, 0, Red); 
         }
         if (CountTrade() == 1) Trailing();

       }
 
EVGENII SHELIPOV #:

Changed nothing has changed

You haven't changed anything.

your conversion

 if (CountTrade() == 1) Trailing();

is inside.

if (CountTrade() == 0)
 
Hi, could you tell me if it is possible to add a one way (buy or sell) trade in moving averages-expert for mt5 ?
 
amsgif80 moving averages-expert for mt5 ?

Write it in the MQL5 code of the Expert Advisor and you will have this option.

 
Vladimir Karputov #:

Write it in the MQL5 code of the Expert Advisor and you will have this option.

I tried, but it didn't work. I am a total zeros in this business :(
 
amsgif80 #:
I tried, it didn't work. I'm a complete novice at it :(
Show me your attempts...
 
amsgif80 #:
I tried, but it didn't work. I am a complete novice in this business :(

This is the code I tried to insert:

At the top of the code throw this:
enum ENUM_DIRECTION{
DIRECTION_BUY = 0, // buy only
DIRECTION_SELL = 1, // sell only
DIRECTION_ANY = 2, // buy & sell
};

input ENUM_DIRECTION inp_direction = DIRECTION_ANY; // allowed trade direction


further find where in code opens buy and where sell. Look inside the start() or OnTick() function
where buy is in the conditions add:
if(inp_direction!=DIRECTION_SELL)
where sell is in the conditions add:
if(inp_direction!=DIRECTION_BUY )