Scripts: Trailing Stop Management Function

 

Trailing Stop Management Function:

Filter orders based on the set magic number, execute the final stop loss management function, adjust the Stoploss and TrailingStop parameters, and place the Management function according to the structure of ea. It is recommended to place it under void OnTick()

Author: Wei Hu

 
//+------------------------------------------------------------------+
//|                                                      SupportRes.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp.  |
//|                                             https://www.mql5.com  |
//+------------------------------------------------------------------+
#property strict

#include <Trade\Trade.mqh>  // Include the trade library

CTrade trade;  // Create an instance of the CTrade class

input double LotSize = 0.1; // Lot size for trading
#property indicator_label1  "iMA"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- initialization
   Print("EA initialized");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- deinitialization
   Print("EA deinitialized");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Calculate the 50-day Simple Moving Average (SMA)
   double  ma50= iMA(NULL,0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
   // Get the current closing price
   double currentPrice = iClose(Symbol(), PERIOD_D1, 0);

   // Check if the current price is above the 50-day moving average
   if (currentPrice > ma50)
     {
      // Check if there are no open positions
      if (PositionsTotal() == 0)
        {
         // Open a buy position
         if (trade.Buy(LotSize, Symbol()))
           {
            Print("Buy order placed at price: ", currentPrice);
           }
         else
           {
            Print("Error placing buy order: ", GetLastError());
           }
        }
     }
   else
     {
      // Close all open buy positions if the price is below the 50-day moving average
      for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
         // Select position by index
         if (PositionSelectByIndex(i))
           {
            // Check if the position is a buy position
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
              {
               // Close the position
               ulong ticket = PositionGetTicket(i);
               if (trade.PositionClose(ticket))
                 {
                  Print("Position closed for ticket: ", ticket);
                 }
               else
                 {
                  Print("Error closing position for ticket ", ticket, ": ", GetLastError());
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 

nees some assistance please 

Can someone tell me where am going wrong keep getting 3 error messages

'iMA' - wrong parameters count  harmonic finder asap.mq5 37 18

'PositionSelectByIndex' - undeclared identifier  harmonic finder asap.mq5 64 14


'i' - some operator expected  harmonic finder asap.mq5 64 36

need a some help please plus am also trying to create an harmonic scanner that will take trade for me 
 
Whats App #:
//+------------------------------------------------------------------+
//|                                                      SupportRes.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp.  |
//|                                             https://www.mql5.com  |
//+------------------------------------------------------------------+
#property strict

#include <Trade\Trade.mqh>  // Include the trade library

CTrade trade;  // Create an instance of the CTrade class

input double LotSize = 0.1; // Lot size for trading
#property indicator_label1  "iMA"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- initialization
   Print("EA initialized");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- deinitialization
   Print("EA deinitialized");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Calculate the 50-day Simple Moving Average (SMA)
   double  ma50= iMA(NULL,0, 50, 0, MODE_SMA, PRICE_CLOSE, 0); //edit: double  ma50= iMA(NULL,0, 50, 0, MODE_SMA, PRICE_CLOSE);
   // Get the current closing price
   double currentPrice = iClose(Symbol(), PERIOD_D1, 0);

   // Check if the current price is above the 50-day moving average
   if (currentPrice > ma50)
     {
      // Check if there are no open positions
      if (PositionsTotal() == 0)
        {
         // Open a buy position
         if (trade.Buy(LotSize, Symbol()))
           {
            Print("Buy order placed at price: ", currentPrice);
           }
         else
           {
            Print("Error placing buy order: ", GetLastError());
           }
        }
     }
   else
     {
      // Close all open buy positions if the price is below the 50-day moving average
      for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
         // Select position by index
         if (PositionSelectByIndex(i))  // edit: if ( PositionGetSymbol(i)!="") 
           {
            // Check if the position is a buy position
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
              {
               // Close the position
               ulong ticket = PositionGetTicket(i);
               if (trade.PositionClose(ticket))
                 {
                  Print("Position closed for ticket: ", ticket);
                 }
               else
                 {
                  Print("Error closing position for ticket ", ticket, ": ", GetLastError());
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

need edite

 
  1. Whats App #: 'iMA' - wrong parameters count  harmonic finder asap.mq5 37 18
     void OnTick()
      {
       // Calculate the 50-day Simple Moving Average (SMA)
       double  ma50= iMA(NULL,0, 50, 0, MODE_SMA, PRICE_CLOSE, 0); 

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)


  2. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)