MT5

mail@pastecode.io avatar
unknown
c_cpp
a year ago
6.5 kB
3
Indexable
Never
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.kalungat.net |
//+------------------------------------------------------------------+
input double Balance = 25000;
input double Leverage = 2;
input double SizePercentage = 5.7;
input double SizeUSD = 1425;
input double QuantityPerGrid = 0.11;
input int Spread = 12;
input double ConstantPrice = 12160;

input double Pivot1 = 2320;
input int NumberOfGrids1 = 5;
input int PipsPerInterval1 = 100;
input int TakeProfit1 = 112;
input int StopLoss1 = 650;
input double StopAllProfitPercentage1 = 0.1;

input double Pivot2 = 2820;
input int NumberOfGrids2 = 5;
input int PipsPerInterval2 = 100;
input int TakeProfit2 = 112;
input int StopLoss2 = 650;
input double StopAllProfitPercentage2 = 0.1;


double pipSize;
int magicNumber = 66622900; // Unique identifier for this EA's trades

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   pipSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   Print(AccountInfoDouble(ACCOUNT_BALANCE));
// Check if the EA has any open positions
   if(PositionsTotal() == 0)
     {
      // If there are no open positions, execute the opening logic
      OpenPositions();
     }
   else
     {
      // If there are open positions, manage the existing positions
      ManagePositions();
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OpenPositions()
  {
// Calculate the lot size based on the given parameters
   double lotSize = (Balance * SizePercentage * 0.01) / (SizeUSD / QuantityPerGrid);

// Loop through both pivot levels
   for(int i = 1; i <= 2; i++)
     {
      double pivot = (i == 1) ? Pivot1 : Pivot2;
      int numberOfGrids = (i == 1) ? NumberOfGrids1 : NumberOfGrids2;
      int pipsPerInterval = (i == 1) ? PipsPerInterval1 : PipsPerInterval2;

      // Open positions for each grid
      for(int j = 1; j <= numberOfGrids; j++)
        {
         double entryPrice = pivot + (j * pipsPerInterval * pipSize);
         int stopLossPips = (i == 1) ? StopLoss1 : StopLoss2;
         int takeProfitPips = (i == 1) ? TakeProfit1 : TakeProfit2;

         // Adjust the stop loss and take profit levels based on the entry price
         double stopLossLevel = entryPrice - (stopLossPips * pipSize);
         double takeProfitLevel = entryPrice + (takeProfitPips * pipSize);

         // Send a buy stop order for the calculated entry price, stop loss, and take profit levels
         MqlTradeRequest request;
         ZeroMemory(request);
         request.action = TRADE_ACTION_PENDING;
         request.type = ORDER_TYPE_BUY_STOP;
         request.symbol = _Symbol;
         request.volume = lotSize;
         request.price = NormalizeDouble(entryPrice, _Digits);
         request.stoplimit = 0;
         request.sl = NormalizeDouble(stopLossLevel, _Digits);
         request.tp = NormalizeDouble(takeProfitLevel, _Digits);
         request.deviation = Spread;
         request.magic = magicNumber;
         request.expiration = 0;
         request.order = 0;
         request.position = 0;

         MqlTradeResult result;
         ZeroMemory(result);

         // Send the order
         if(!OrderSend(request, result))
           {
            Print("OrderSend() failed. Error: ", GetLastError());
           }
        }
     }
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ManagePositions()
  {
   double totalProfit = 0;

// Loop through all open positions
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ulong positionTicket = PositionGetTicket(i);

      if(PositionGetInteger(POSITION_MAGIC) == magicNumber)  // Check if position belongs to this EA
        {
         double positionProfit = PositionGetDouble(POSITION_PROFIT);
         totalProfit += positionProfit;
        }
     }

// Calculate the profit percentage
   double profitPercentage = (totalProfit / Balance) * 100;

// Check if the profit percentage exceeds the stop percentage for any pivot
   if(profitPercentage >= StopAllProfitPercentage1 || profitPercentage >= StopAllProfitPercentage2)
     {
      // Close all positions belonging to this EA
      for(int i = PositionsTotal() - 1; i >= 0; i--)
        {
         ulong positionTicket = PositionGetTicket(i);

         if(PositionGetInteger(POSITION_MAGIC) == magicNumber)  // Check if position belongs to this EA
           {
            // Close the position
            MqlTradeRequest request;
            ZeroMemory(request);
            request.action = TRADE_ACTION_DEAL;
            request.type = (ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
            request.symbol = _Symbol;
            request.volume = PositionGetDouble(POSITION_VOLUME);
            request.price = (request.type == ORDER_TYPE_SELL) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            request.sl = 0;
            request.tp = 0;
            request.deviation = Spread;
            request.magic = magicNumber;
            request.position = positionTicket;

            MqlTradeResult result;
            ZeroMemory(result);

            // Send the order
            if(!OrderSend(request, result))
              {
               Print("OrderSend() failed. Error: ", GetLastError());
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+