Seeking help to vet Trail Stop programming codes

 

Dear Sir/Ma'am,

May I seek the help of experts with my coding.. I tried to compile and resulted in 11 errors and 1 warnings (with my LastStopMovingAverage=1000)

Thank you very much for your time.

 

#include<Trade\Trade.mqh>

Ctrade trade;

void OnTick()
  {
      static double LastStopMovingAverageValue;
      
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      
      if (PositionsTotal()<1)
      {
         trade.Sell(0.10,NULL,Bid,0,(Bid-150 * _Point),NULL);
         
         LastStopMovingAverage=1000;
      }
      
      double myMovingAverageArray[];
       
      
      ArraySetAsSeries(myMovingAverageArray,true);
      
       
       int movingAverageDefinition = iMA (_Symbol,_Period,500,0,MODE_EMA,PRICE_CLOSE);
       
       
       CopyBuffer(movingAverageDefinition,0,0,3,myMovingAverageArray);
       

       double StopMovingAverageValue = myMovingAverageArray[1];
       
   
       if (StopMovingAverageValue > Ask)
       
  
       if (StopMovingAverageValue < LastStopMovingAverageValue)
       {
          CheckEMASellTrailingStop (Bid, StopMovingAverageValue); 
          

          LastStopMovingAverageValue= StopMovingAverageValue;
       }
       
  }

void checkEMASellTrailingStop(double Bid, double StopMovingAverageValue)
  {
  
  
      for(int i=PositionsTotal()-1; i>=0; i--)
      {
      
         string symbol=PositionGetSymbol(i); // Get currency pair symbol}
         
         if (_Symbol==symbol) //if the currency pair matches
         {
         
             ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
             
          
             double CurrentStopLoss=PositionGetDouble(POSITION_SL);
             

             if ((CurrentStopLoss>StopMovingAverageValue)||CurrentStopLoss==0)
             {
             trade.PositionModify(PositionTicket,StopMovingAverageValue,0);
             }
         } //End if loop
         
       } //End for loop
 } //End

 
flexjagger :

Code:

***

Please insert your code correctly: when editing a post, press the button Code, then paste the code into the window that appears.

 
Vladimir Karputov:

Please insert your code correctly: when editing a post, press the button , then paste the code into the window that appears.

Thank you for the advice Mr Valdimir. Good Day.
 
flexjagger :
Thank you for the advice Mr Valdimir. Good Day.

Mistake # 1 - Remember, in MQL5, the indicator handle needs to be CREATED ONLY ONCE - and this is done in OnInit !!!

Please read the documentation with iMA examples and correct your mistake. After you fix your MQL5 code, publish it.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5