소스 검색

Ticket: 3995 Complete

Huzaifa-MQLDev 1 년 전
부모
커밋
edc761e396
2개의 변경된 파일333개의 추가작업 그리고 0개의 파일을 삭제
  1. BIN
      line_level_ea.ex5
  2. 333 0
      line_level_ea.mq5

BIN
line_level_ea.ex5


+ 333 - 0
line_level_ea.mq5

@@ -0,0 +1,333 @@
+//+------------------------------------------------------------------+
+//|                                                line_level_ea.mq5 |
+//|                                  Copyright 2025, MQL Development |
+//|                                  https://www.mqldevelopment.com/ |
+//+------------------------------------------------------------------+
+#property copyright "Copyright 2025, MQL Development"
+#property link      "https://www.mqldevelopment.com/"
+#property version   "1.00"
+#include <Trade\Trade.mqh>
+CTrade  trade;
+
+enum tradeType
+  {
+   buyTrades,   // Buy
+   sellTrades,  // Sell
+  };
+
+input        string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
+input        int                     magic_no                   = 333;             // Magic no
+input        tradeType               selectSide                 = buyTrades;       // Select Side
+input        double                  lot_size                   = 0.1;             // Lot Size
+input        double                  lot_multiplier             = 2;               // Lot Multiplier on Loss Trade
+input        double                  levels_distence            = 10;              // Levels Distance in Dollars
+input        double                  stoploss                   = 10;              // Fixed Stop Loss in Pips
+input        double                  takeprofit                 = 10;              // Fixed Take Profit in Pips
+input        bool                    countinueCycleAfterProfit  = true;            // Continue Trading After Profit
+
+input        string                  time_setting               = "<><><><><> Time Filter Settings <><><><><>";             //_
+input        bool                    EnableTimeFilter           = false;           // Enable Time Filter
+input        string                  startTime                  = "03:00";         // Start Time Session
+input        string                  endTime                    = "09:00";         // End Time Session
+
+// Global Variables
+double above_level = 0, below_level = 0;
+static double tickCurrentBid = 0, tickCurrentAsk = 0;
+double tickPreviousBid = 0, tickPreviousAsk = 0;
+datetime  startTradingTime = 0, endTradingTime = 0;
+string sep  =  ":";                // A separator as a character
+ushort u_sep;                      // The code of the separator character
+string result1[];
+datetime ea_start_time = 0;
+bool tradeNow = true;
+//+------------------------------------------------------------------+
+//| Expert initialization function                                   |
+//+------------------------------------------------------------------+
+int OnInit()
+  {
+//---
+// Fill Values above and below levels
+   trade.SetExpertMagicNumber(magic_no);
+   trade.SetDeviationInPoints(10);
+   trade.SetTypeFilling(ORDER_FILLING_IOC);
+   trade.LogLevel(LOG_LEVEL_ALL);
+   trade.SetAsyncMode(false);
+   ea_start_time = TimeCurrent();
+   double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
+   getNearestLevels(currentPrice, levels_distence, below_level, above_level);
+
+//---
+   return(INIT_SUCCEEDED);
+  }
+//+------------------------------------------------------------------+
+//| Expert deinitialization function                                 |
+//+------------------------------------------------------------------+
+void OnDeinit(const int reason)
+  {
+//---
+
+  }
+//+------------------------------------------------------------------+
+//| Expert tick function                                             |
+//+------------------------------------------------------------------+
+void OnTick()
+  {
+//---
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+   tickPreviousBid = tickCurrentBid;
+   tickCurrentBid = Bid;
+   tickPreviousAsk = tickCurrentAsk;
+   tickCurrentAsk = Ask;
+   timeConversion();
+// Comment(" Below Value is: ", below_level, " Above Value: ",  above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
+   double lastLot = 0;
+   if(!countinueCycleAfterProfit)
+      if(selectLatestTicket(lastLot) > 0)
+        {
+         tradeNow = false;
+        }
+   if(tradeNow)
+      if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) ||  !EnableTimeFilter)
+        {
+         if(selectSide == buyTrades)  // Buy trade case
+           {
+            if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
+               ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
+              {
+               placeBuyTrade();
+               Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
+
+               double currentPrice = 0;
+               if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
+                 {
+                  currentPrice = above_level;
+                 }
+               else
+                  if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
+                    {
+                     currentPrice = below_level;
+                    }
+               getNearestLevels(currentPrice, levels_distence, below_level, above_level);
+
+               Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
+              }
+           }
+         if(selectSide == sellTrades)
+           {
+            if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
+               ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
+              {
+               placeSellTrade();
+               Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
+
+               double currentPrice = 0;
+               if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
+                 {
+                  currentPrice = above_level;
+                 }
+               else
+                  if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
+                    {
+                     currentPrice = below_level;
+                    }
+               getNearestLevels(currentPrice, levels_distence, below_level, above_level);
+
+               Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
+              }
+           }
+        }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+double calculateBaseLevel(double price, double step)
+  {
+   return step * MathFloor(price / step);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void getNearestLevels(double currentPrice, double step,
+                      double &LowerLevel, double &UpperLevel)
+  {
+   double epsilon = 0.000001;
+// Compute the anchor as the nearest multiple of step.
+   double anchor = MathRound(currentPrice / step) * step;
+   double diff = fabs(currentPrice - anchor);
+
+// If currentPrice is exactly on a multiple, use symmetric levels.
+   if(diff < epsilon)
+     {
+      LowerLevel = anchor - step;
+      UpperLevel = anchor + step;
+      return;
+     }
+
+   double ratio = diff / step;
+   double threshold = 0.45;  // Adjusted threshold
+
+   if(currentPrice > anchor)
+     {
+      if(ratio > threshold)
+        {
+         LowerLevel = anchor - step;
+         UpperLevel = anchor + step;
+        }
+      else
+        {
+         LowerLevel = anchor;
+         UpperLevel = anchor + step;
+        }
+     }
+   else // currentPrice < anchor
+     {
+      if(ratio > threshold)
+        {
+         LowerLevel = anchor - step;
+         UpperLevel = anchor + step;
+        }
+      else
+        {
+         LowerLevel = anchor - step;
+         UpperLevel = anchor;
+        }
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeBuyTrade()
+  {
+
+   double buySL = 0, buyTp=0;
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+
+   if(stoploss != 0)
+     {
+      buySL = Ask - (stoploss * 10 * Point());
+     }
+   if(takeprofit != 0)
+     {
+      buyTp = Ask + (takeprofit * 10 * Point());
+     }
+   double lot = 0;
+   double lastLot = 0;
+   if(selectLatestTicket(lastLot) < 0)
+     {
+      lot = lastLot * lot_multiplier;
+     }
+   else
+     {
+      lot = lot_size;
+     }
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
+     {
+      Print("Buy Trade Placed: ",trade.ResultOrder());
+     }
+   else
+     {
+      Print("Error in placing Buy: "+Symbol()+"  ",GetLastError());
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeSellTrade()
+  {
+
+   double sellSL = 0, sellTp = 0;
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+
+   if(stoploss != 0)
+     {
+      sellSL = Bid + (stoploss * 10 * Point());
+     }
+   if(takeprofit != 0)
+     {
+      sellTp = Bid - (takeprofit * 10 * Point());
+     }
+   double lot = 0;
+   double lastLot = 0;
+   if(selectLatestTicket(lastLot) < 0)
+     {
+      lot = lastLot * lot_multiplier;
+     }
+   else
+     {
+      lot = lot_size;
+     }
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
+     {
+      Print("Sell Trade PLaced: ",trade.ResultOrder());
+     }
+   else
+     {
+      Print("Error in placing Sell: "+Symbol()+"  ",GetLastError());
+     }
+
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void timeConversion()
+  {
+   MqlDateTime date, date1;
+
+   TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
+   u_sep=StringGetCharacter(sep,0);
+   StringSplit(startTime,u_sep,result1);
+   date.hour = (int)StringToInteger(result1[0]);
+   date.min = (int)StringToInteger(result1[1]);
+   startTradingTime = StructToTime(date);
+
+   TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
+   StringSplit(endTime,u_sep,result1);
+   date.hour = (int)StringToInteger(result1[0]);
+   date.min = (int)StringToInteger(result1[1]);
+   endTradingTime = StructToTime(date);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+double selectLatestTicket(double &lastOrderLot)
+  {
+
+   int count = 0;
+   ulong ticket_deal_Out=0, ticket_deal_In = 0;
+   datetime latestCloseTime = 0;
+   double orderProfit = 0;
+   ulong latestTicket = 0;
+   if(HistorySelect(ea_start_time, TimeCurrent()))
+     {
+      int total = HistoryDealsTotal();
+      for(int i = total-1; i >= 0 ; i--)
+        {
+         ticket_deal_Out = HistoryDealGetTicket(i);
+         if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
+            && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
+           {
+            datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
+            if(orderCloseTime > latestCloseTime)
+              {
+               latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
+               orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
+               latestTicket = ticket_deal_Out;
+               lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
+               // Print(" Last order Lot: ", lastOrderLot);
+              }
+           }
+        }
+     }
+// Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
+
+   return orderProfit;
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+//+------------------------------------------------------------------+