Like grid sysytem

 

Hello everybody,

I have a code like below. I want to add something to this code but I couldn't. Can you help me.

I want to do the following: This code sends 22 orders at the beginning. The TP and SL points of these orders are certain. When the market starts trading, I want to open a new transaction instead of the TP transaction. If this new transaction is going to be opened instead of the previous BUY transaction, it should be opened 50 pips below the TP price, if it is going to be opened instead of the SELL transaction, it should be opened 50 pips above the TP price. Thanks in advance to those who will help.

#include <Trade\Trade.mqh>

// Giriş parametreleri
input double LotSize = 0.1; // Lot büyüklüğü
input double TakeProfitPercent = 1.0; // Kar al yüzdesi
input double StopLossPercent = 5.0; // Zarar durdur yüzdesi
input double GridSizePips = 200.0; // Grid boyutu (pips)

CTrade trade; // Ticaret objesi
bool firstOrderSent = false; // İlk emir gönderilip gönderilmediğini kontrol eden değişken

// OnInit fonksiyonu
int OnInit() {
   return(INIT_SUCCEEDED);
}

// Piyasanın açık olup olmadığını kontrol eden fonksiyon
bool MarketIsOpen() {
   datetime currentTime = TimeCurrent(); // Şu anki zaman
   MqlDateTime timeStruct;
   TimeToStruct(currentTime, timeStruct); // Zamanı yapılandır

   int dayOfWeek = timeStruct.day_of_week; // Haftanın günü
   if (dayOfWeek == 0 || dayOfWeek == 6) { // Cumartesi ve Pazar
      return false;
   }
   return true; // Hafta içi, piyasa açık
}

// Pips hesaplama fonksiyonu
double PipsToPrice(double pips) {
   double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
   return pips * point;
}

// Emir için Stop Loss ve Take Profit hesaplama
double CalculateStopLoss(bool isBuyOrder, double currentPrice) {
   if (isBuyOrder) {
      return currentPrice * (1 - StopLossPercent / 100); // Buy için SL
   } else {
      return currentPrice * (1 + StopLossPercent / 100); // Sell için SL
   }
}

double CalculateTakeProfit(bool isBuyOrder, double currentPrice) {
   if (isBuyOrder) {
      return currentPrice * (1 + TakeProfitPercent / 100); // Buy için TP
   } else {
      return currentPrice * (1 - TakeProfitPercent / 100); // Sell için TP
   }
}

// OnTick fonksiyonu
void OnTick() {
   if (!firstOrderSent && MarketIsOpen()) {
      double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

      // İlk Buy ve Sell emirlerini gönder
      if (trade.Buy(LotSize, _Symbol, currentPrice, 
                     CalculateStopLoss(true, currentPrice), 
                     CalculateTakeProfit(true, currentPrice))) {
         Print("Buy işlemi gönderildi.");
      } else {
         Print("Buy işlemi gönderilemedi.");
      }

      if (trade.Sell(LotSize, _Symbol, currentPrice, 
                     CalculateStopLoss(false, currentPrice), 
                     CalculateTakeProfit(false, currentPrice))) {
         Print("Sell işlemi gönderildi.");
      } else {
         Print("Sell işlemi gönderilemedi.");
      }

      // 200 pips aralıkla yukarı ve aşağıdaki emirlere yerleştirme
      for (int i = 1; i <= 5; i++) {
         double offset = i * GridSizePips;
         
         // Yukarıdaki emirler (Buy Stop ve Sell Limit)
         double buyStopPrice = currentPrice + PipsToPrice(offset);
         double sellLimitPrice = currentPrice + PipsToPrice(offset);
         
         // Aşağıdaki emirler (Buy Limit ve Sell Stop)
         double buyLimitPrice = currentPrice - PipsToPrice(offset);
         double sellStopPrice = currentPrice - PipsToPrice(offset);

         // Buy Stop emri gönder
         if (trade.BuyStop(LotSize, buyStopPrice, _Symbol, 
                           CalculateStopLoss(true, buyStopPrice), 
                           CalculateTakeProfit(true, buyStopPrice))) {
            Print("Buy Stop işlemi gönderildi: ", buyStopPrice);
         } else {
            Print("Buy Stop işlemi gönderilemedi: ", buyStopPrice);
         }

         // Sell Limit emri gönder
         if (trade.SellLimit(LotSize, sellLimitPrice, _Symbol, 
                             CalculateStopLoss(false, sellLimitPrice), 
                             CalculateTakeProfit(false, sellLimitPrice))) {
            Print("Sell Limit işlemi gönderildi: ", sellLimitPrice);
         } else {
            Print("Sell Limit işlemi gönderilemedi: ", sellLimitPrice);
         }

         // Buy Limit emri gönder
         if (trade.BuyLimit(LotSize, buyLimitPrice, _Symbol, 
                            CalculateStopLoss(true, buyLimitPrice), 
                            CalculateTakeProfit(true, buyLimitPrice))) {
            Print("Buy Limit işlemi gönderildi: ", buyLimitPrice);
         } else {
            Print("Buy Limit işlemi gönderilemedi: ", buyLimitPrice);
         }

         // Sell Stop emri gönder
         if (trade.SellStop(LotSize, sellStopPrice, _Symbol, 
                            CalculateStopLoss(false, sellStopPrice), 
                            CalculateTakeProfit(false, sellStopPrice))) {
            Print("Sell Stop işlemi gönderildi: ", sellStopPrice);
         } else {
            Print("Sell Stop işlemi gönderilemedi: ", sellStopPrice);
         }
      }

      // İlk emirlerin gönderildiğini işaretle
      firstOrderSent = true;
   }
}
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger() , SymbolInfoDouble() and SymbolInfoString() . The first...
 

When you post code please use the CODE button (Alt-S) ! 

NOTE: Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.12.15
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893