Bläddra i källkod

Ticket: 3995 Complete

Huzaifa-MQLDev 1 år sedan
förälder
incheckning
edc761e396
2 ändrade filer med 333 tillägg och 0 borttagningar
  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 @@
1
+//+------------------------------------------------------------------+
2
+//|                                                line_level_ea.mq5 |
3
+//|                                  Copyright 2025, MQL Development |
4
+//|                                  https://www.mqldevelopment.com/ |
5
+//+------------------------------------------------------------------+
6
+#property copyright "Copyright 2025, MQL Development"
7
+#property link      "https://www.mqldevelopment.com/"
8
+#property version   "1.00"
9
+#include <Trade\Trade.mqh>
10
+CTrade  trade;
11
+
12
+enum tradeType
13
+  {
14
+   buyTrades,   // Buy
15
+   sellTrades,  // Sell
16
+  };
17
+
18
+input        string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
19
+input        int                     magic_no                   = 333;             // Magic no
20
+input        tradeType               selectSide                 = buyTrades;       // Select Side
21
+input        double                  lot_size                   = 0.1;             // Lot Size
22
+input        double                  lot_multiplier             = 2;               // Lot Multiplier on Loss Trade
23
+input        double                  levels_distence            = 10;              // Levels Distance in Dollars
24
+input        double                  stoploss                   = 10;              // Fixed Stop Loss in Pips
25
+input        double                  takeprofit                 = 10;              // Fixed Take Profit in Pips
26
+input        bool                    countinueCycleAfterProfit  = true;            // Continue Trading After Profit
27
+
28
+input        string                  time_setting               = "<><><><><> Time Filter Settings <><><><><>";             //_
29
+input        bool                    EnableTimeFilter           = false;           // Enable Time Filter
30
+input        string                  startTime                  = "03:00";         // Start Time Session
31
+input        string                  endTime                    = "09:00";         // End Time Session
32
+
33
+// Global Variables
34
+double above_level = 0, below_level = 0;
35
+static double tickCurrentBid = 0, tickCurrentAsk = 0;
36
+double tickPreviousBid = 0, tickPreviousAsk = 0;
37
+datetime  startTradingTime = 0, endTradingTime = 0;
38
+string sep  =  ":";                // A separator as a character
39
+ushort u_sep;                      // The code of the separator character
40
+string result1[];
41
+datetime ea_start_time = 0;
42
+bool tradeNow = true;
43
+//+------------------------------------------------------------------+
44
+//| Expert initialization function                                   |
45
+//+------------------------------------------------------------------+
46
+int OnInit()
47
+  {
48
+//---
49
+// Fill Values above and below levels
50
+   trade.SetExpertMagicNumber(magic_no);
51
+   trade.SetDeviationInPoints(10);
52
+   trade.SetTypeFilling(ORDER_FILLING_IOC);
53
+   trade.LogLevel(LOG_LEVEL_ALL);
54
+   trade.SetAsyncMode(false);
55
+   ea_start_time = TimeCurrent();
56
+   double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
57
+   getNearestLevels(currentPrice, levels_distence, below_level, above_level);
58
+
59
+//---
60
+   return(INIT_SUCCEEDED);
61
+  }
62
+//+------------------------------------------------------------------+
63
+//| Expert deinitialization function                                 |
64
+//+------------------------------------------------------------------+
65
+void OnDeinit(const int reason)
66
+  {
67
+//---
68
+
69
+  }
70
+//+------------------------------------------------------------------+
71
+//| Expert tick function                                             |
72
+//+------------------------------------------------------------------+
73
+void OnTick()
74
+  {
75
+//---
76
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
77
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
78
+   tickPreviousBid = tickCurrentBid;
79
+   tickCurrentBid = Bid;
80
+   tickPreviousAsk = tickCurrentAsk;
81
+   tickCurrentAsk = Ask;
82
+   timeConversion();
83
+// Comment(" Below Value is: ", below_level, " Above Value: ",  above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
84
+   double lastLot = 0;
85
+   if(!countinueCycleAfterProfit)
86
+      if(selectLatestTicket(lastLot) > 0)
87
+        {
88
+         tradeNow = false;
89
+        }
90
+   if(tradeNow)
91
+      if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) ||  !EnableTimeFilter)
92
+        {
93
+         if(selectSide == buyTrades)  // Buy trade case
94
+           {
95
+            if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
96
+               ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
97
+              {
98
+               placeBuyTrade();
99
+               Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
100
+
101
+               double currentPrice = 0;
102
+               if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
103
+                 {
104
+                  currentPrice = above_level;
105
+                 }
106
+               else
107
+                  if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
108
+                    {
109
+                     currentPrice = below_level;
110
+                    }
111
+               getNearestLevels(currentPrice, levels_distence, below_level, above_level);
112
+
113
+               Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
114
+              }
115
+           }
116
+         if(selectSide == sellTrades)
117
+           {
118
+            if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
119
+               ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
120
+              {
121
+               placeSellTrade();
122
+               Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
123
+
124
+               double currentPrice = 0;
125
+               if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
126
+                 {
127
+                  currentPrice = above_level;
128
+                 }
129
+               else
130
+                  if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
131
+                    {
132
+                     currentPrice = below_level;
133
+                    }
134
+               getNearestLevels(currentPrice, levels_distence, below_level, above_level);
135
+
136
+               Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
137
+              }
138
+           }
139
+        }
140
+  }
141
+//+------------------------------------------------------------------+
142
+//|                                                                  |
143
+//+------------------------------------------------------------------+
144
+double calculateBaseLevel(double price, double step)
145
+  {
146
+   return step * MathFloor(price / step);
147
+  }
148
+//+------------------------------------------------------------------+
149
+//|                                                                  |
150
+//+------------------------------------------------------------------+
151
+void getNearestLevels(double currentPrice, double step,
152
+                      double &LowerLevel, double &UpperLevel)
153
+  {
154
+   double epsilon = 0.000001;
155
+// Compute the anchor as the nearest multiple of step.
156
+   double anchor = MathRound(currentPrice / step) * step;
157
+   double diff = fabs(currentPrice - anchor);
158
+
159
+// If currentPrice is exactly on a multiple, use symmetric levels.
160
+   if(diff < epsilon)
161
+     {
162
+      LowerLevel = anchor - step;
163
+      UpperLevel = anchor + step;
164
+      return;
165
+     }
166
+
167
+   double ratio = diff / step;
168
+   double threshold = 0.45;  // Adjusted threshold
169
+
170
+   if(currentPrice > anchor)
171
+     {
172
+      if(ratio > threshold)
173
+        {
174
+         LowerLevel = anchor - step;
175
+         UpperLevel = anchor + step;
176
+        }
177
+      else
178
+        {
179
+         LowerLevel = anchor;
180
+         UpperLevel = anchor + step;
181
+        }
182
+     }
183
+   else // currentPrice < anchor
184
+     {
185
+      if(ratio > threshold)
186
+        {
187
+         LowerLevel = anchor - step;
188
+         UpperLevel = anchor + step;
189
+        }
190
+      else
191
+        {
192
+         LowerLevel = anchor - step;
193
+         UpperLevel = anchor;
194
+        }
195
+     }
196
+  }
197
+//+------------------------------------------------------------------+
198
+//|                                                                  |
199
+//+------------------------------------------------------------------+
200
+void placeBuyTrade()
201
+  {
202
+
203
+   double buySL = 0, buyTp=0;
204
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
205
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
206
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
207
+
208
+   if(stoploss != 0)
209
+     {
210
+      buySL = Ask - (stoploss * 10 * Point());
211
+     }
212
+   if(takeprofit != 0)
213
+     {
214
+      buyTp = Ask + (takeprofit * 10 * Point());
215
+     }
216
+   double lot = 0;
217
+   double lastLot = 0;
218
+   if(selectLatestTicket(lastLot) < 0)
219
+     {
220
+      lot = lastLot * lot_multiplier;
221
+     }
222
+   else
223
+     {
224
+      lot = lot_size;
225
+     }
226
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
227
+     {
228
+      Print("Buy Trade Placed: ",trade.ResultOrder());
229
+     }
230
+   else
231
+     {
232
+      Print("Error in placing Buy: "+Symbol()+"  ",GetLastError());
233
+     }
234
+  }
235
+//+------------------------------------------------------------------+
236
+//|                                                                  |
237
+//+------------------------------------------------------------------+
238
+void placeSellTrade()
239
+  {
240
+
241
+   double sellSL = 0, sellTp = 0;
242
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
243
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
244
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
245
+
246
+   if(stoploss != 0)
247
+     {
248
+      sellSL = Bid + (stoploss * 10 * Point());
249
+     }
250
+   if(takeprofit != 0)
251
+     {
252
+      sellTp = Bid - (takeprofit * 10 * Point());
253
+     }
254
+   double lot = 0;
255
+   double lastLot = 0;
256
+   if(selectLatestTicket(lastLot) < 0)
257
+     {
258
+      lot = lastLot * lot_multiplier;
259
+     }
260
+   else
261
+     {
262
+      lot = lot_size;
263
+     }
264
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
265
+     {
266
+      Print("Sell Trade PLaced: ",trade.ResultOrder());
267
+     }
268
+   else
269
+     {
270
+      Print("Error in placing Sell: "+Symbol()+"  ",GetLastError());
271
+     }
272
+
273
+  }
274
+//+------------------------------------------------------------------+
275
+//|                                                                  |
276
+//+------------------------------------------------------------------+
277
+void timeConversion()
278
+  {
279
+   MqlDateTime date, date1;
280
+
281
+   TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
282
+   u_sep=StringGetCharacter(sep,0);
283
+   StringSplit(startTime,u_sep,result1);
284
+   date.hour = (int)StringToInteger(result1[0]);
285
+   date.min = (int)StringToInteger(result1[1]);
286
+   startTradingTime = StructToTime(date);
287
+
288
+   TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
289
+   StringSplit(endTime,u_sep,result1);
290
+   date.hour = (int)StringToInteger(result1[0]);
291
+   date.min = (int)StringToInteger(result1[1]);
292
+   endTradingTime = StructToTime(date);
293
+  }
294
+//+------------------------------------------------------------------+
295
+//|                                                                  |
296
+//+------------------------------------------------------------------+
297
+double selectLatestTicket(double &lastOrderLot)
298
+  {
299
+
300
+   int count = 0;
301
+   ulong ticket_deal_Out=0, ticket_deal_In = 0;
302
+   datetime latestCloseTime = 0;
303
+   double orderProfit = 0;
304
+   ulong latestTicket = 0;
305
+   if(HistorySelect(ea_start_time, TimeCurrent()))
306
+     {
307
+      int total = HistoryDealsTotal();
308
+      for(int i = total-1; i >= 0 ; i--)
309
+        {
310
+         ticket_deal_Out = HistoryDealGetTicket(i);
311
+         if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
312
+            && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
313
+           {
314
+            datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
315
+            if(orderCloseTime > latestCloseTime)
316
+              {
317
+               latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
318
+               orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
319
+               latestTicket = ticket_deal_Out;
320
+               lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
321
+               // Print(" Last order Lot: ", lastOrderLot);
322
+              }
323
+           }
324
+        }
325
+     }
326
+// Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
327
+
328
+   return orderProfit;
329
+  }
330
+//+------------------------------------------------------------------+
331
+//|                                                                  |
332
+//+------------------------------------------------------------------+
333
+//+------------------------------------------------------------------+