11#property copyright " Copyright 2017-2021, Artur Zas"
22// GNU General Public License v3.0 -> https://github.com/9nix6/Median-and-Turbo-Renko-indicator-bundle/blob/master/LICENSE
33#property link " https://www.az-invest.eu"
4- #property version " 1.17"
4+ #define VERSION " 1.20"
5+ #property version VERSION
56#property description " Example EA: Trading based on 2 moving average crossover."
6- #property description " MA1 & MA2 need to be enabled on the inicator creating the chart."
7+ #property description " MA1 && MA2 need to be enabled on the inicator creating the chart."
78#property description " MA1 - Fast moving average"
89#property description " MA2 - Slow moving average"
10+ #property description " "
11+ #property description " GNU General Public License v3.0"
912
13+ //#define RANGEBAR_LICENSE // uncomment when used on a Tick & Volume bar chart from https://www.az-invest.eu/rangebars-for-metatrader-5
1014//#define ULTIMATE_RENKO_LICENSE // uncomment when used on Ultimate Renko chart from https://www.az-invest.eu/ultimate-renko-indicator-generator-for-metatrader-5
1115//#define VOLUMECHART_LICENSE // uncomment when used on a Tick & Volume bar chart from https://www.az-invest.eu/Tick-chart-and-volume-chart-for-mt5
12- //#define RANGEBAR_LICENSE // uncomment when used on a Tick & Volume bar chart from https://www.az-invest.eu/rangebars-for-metatrader-5
1316//#define SECONDSCHART_LICENSE // uncomment when used on a Seconds TF bar chart from https://www.az-invest.eu/seconds-timeframe-chart-for-metatrader-5
14- //#define LINEBREAKCHART_LICENSE // uncomment when used on a Line Break chart from https://www.az-invest.eu
17+ //#define LINEBREAKCHART_LICENSE // uncomment when used on a Line Break chart from https://www.az-invest.eu/linebreak-chart-for-metatrader-5
1518//
1619// Uncomment only ONE of the 5 directives listed below and recompile
1720// -----------------------------------------------------------------
2124//#define EA_ON_XTICK_CHART // Use EA on XTick Chart (obsolete)
2225//#define EA_ON_TICK_VOLUME_CHART // Use EA on Tick & Volume Bar Chart
2326//#define EA_ON_SECONDS_CHART // Use EA on Seconds Interval chart
24- //#define EA_ON_LINEBREAK_CHART // Use EA on LineBreak charts
27+ //#define EA_ON_LINEBREAK_CHART // Use EA on LineBreak charts
2528
2629//#define DEVELOPER_VERSION // used when I develop ;) should always be commented out
2730
2831// Uncomment the directive below and recompile if EA is used with P-Renko BR Ultimate
2932// ----------------------------------------------------------------------------------
3033//
31- // #define P_RENKO_BR_PRO // Use in P-Renko BR Ultimate version
34+ //#define P_RENKO_BR_PRO // Use in P-Renko BR Ultimate version
3235
3336//
3437// Uncomment the directive below and recompile for use in a backtest only
6568
6669#include < AZ-INVEST/SDK/TimeControl.mqh>
6770#include < AZ-INVEST/SDK/TradeFunctions.mqh>
71+ #include < AZ-INVEST/SDK/TradeManager.mqh>
6872
6973enum ENUM_TRADE_DIRECTION
7074{
@@ -74,22 +78,30 @@ enum ENUM_TRADE_DIRECTION
7478};
7579
7680#ifdef SHOW_INDICATOR_INPUTS
77- input group " EA parameters"
81+ input group " ### EA parameters"
7882#endif
7983input double Lots = 0.1 ; // Traded lots
8084input uint StopLoss = 100 ; // Stop Loss (in points)
81- input uint TakeProfit = 250 ; // Take profit (in points)
85+ input uint TakeProfit = 300 ; // Take profit (in points)
8286input ENUM_TRADE_DIRECTION ValidTradeDirection = TRADE_DIRECTION_ALL ; // Valid trading type
8387input bool ForceSR = false ; // Force Stop & Reverse
8488input bool ReverseOnMACrossInsideGap = true ; // Reverse trade if MA cross inside a gap
85- input bool CloseTradeAfterTradingHours = true ; // Close trade after trading hours
86- input ulong DeviationPoints = 0 ; // Maximum defiation (in points)
87- input double ManualTickSize = 0.000 ; // Tick Size (0 = auto detect)
89+ input group " ### Trading schedule (Non stop if start = 0 & end = 0)"
8890input string Start =" 9:00" ; // Start trading at
8991input string End =" 17:55" ; // End trading at
92+ input bool CloseTradeAfterTradingHours = false ; // Close trade after trading hours
93+ input group " ### Trade management" ;
94+ input int InpBEPoints = 0 ; // BreakEven (Points) [ 0 = OFF ]
95+ input int InpTrailByPoints = 0 ; // Trail by (Points) [ 0 = OFF ]
96+ input int InpTrailStartPoints = 150 ; // Start trailing after (Points)
97+ input int InpPartialCloseAtProfitPoints = 0 ; // Partial close at (Points) [ 0 = OFF ]
98+ input int InpPartialClosePercentage = 50 ; // Partial close %
99+ input group " ### Misc" ;
90100input ulong MagicNumber =5150 ; // Assign trade ID
101+ input ulong DeviationPoints = 0 ; // Maximum deviation (in points)
102+ input double ManualTickSize = 0.000 ; // Tick Size (0 = auto detect)
91103input int NumberOfRetries = 50 ; // Maximum number of retries
92- input int BusyTimeout_ms = 1000 ; // Wait [ms] before retry on bussy errors
104+ input int BusyTimeout_ms = 1000 ; // Wait [ms] before retry on busy errors
93105input int RequoteTimeout_ms = 250 ; // Wait [ms] before retry on requotes
94106
95107// Global data buffers
@@ -106,8 +118,10 @@ int numberOfBars = 3;
106118
107119CMarketOrder *marketOrder = NULL ;
108120CTimeControl *timeControl = NULL ;
121+ CTradeManager *tradeManager = NULL ;
109122
110123ulong currentTicket ;
124+ CTradeManagerState tradeManagerState ;
111125ENUM_POSITION_TYPE currentPositionType ;
112126ENUM_POSITION_TYPE signal ;
113127ENUM_POSITION_TYPE validation ;
@@ -191,7 +205,25 @@ int OnInit()
191205
192206 timeControl .SetValidTraingHours (Start ,End );
193207
194- return (INIT_SUCCEEDED );
208+ //
209+ // Init TradeManager
210+ //
211+
212+ CTradeManagerParameters params2 ;
213+ {
214+ params2 .BEPoints = InpBEPoints ;
215+ params2 .TrailByPoints = InpTrailByPoints ;
216+ params2 .TrailStartPoints = InpTrailStartPoints ;
217+ params2 .PartialCloseAtProfitPoints = InpPartialCloseAtProfitPoints ;
218+ params2 .PartialClosePercentage = InpPartialClosePercentage ;
219+ }
220+
221+ if (tradeManager == NULL )
222+ {
223+ tradeManager = new CTradeManager (params2 , marketOrder );
224+ }
225+
226+ return INIT_SUCCEEDED ;
195227}
196228//+------------------------------------------------------------------+
197229//| Expert deinitialization function |
@@ -223,28 +255,29 @@ void OnDeinit(const int reason)
223255 delete customBars ;
224256 customBars = NULL ;
225257 }
226-
258+
259+ if (tradeManager != NULL )
260+ {
261+ delete tradeManager ;
262+ tradeManager = NULL ;
263+ }
264+
227265 Comment (" " );
228266}
229267//+------------------------------------------------------------------+
230268//| Expert tick function |
231269//+------------------------------------------------------------------+
232270void OnTick ()
233271{
234- if (marketOrder == NULL || customBars == NULL || timeControl == NULL )
272+ if (marketOrder == NULL || customBars == NULL || timeControl == NULL || tradeManager == NULL )
235273 return ;
236274
237- if (customBars .IsNewBar ())
275+ // trade management
276+
277+ if (marketOrder .IsOpen (currentTicket , _Symbol , MagicNumber ))
238278 {
239- if (timeControl .IsScheduleEnabled ())
240- {
241- Comment (" EA trading schedule ON (" +Start +" to " +End +" ) | trading enabled = " +(string )timeControl .IsTradingTimeValid ());
242- }
243- else
244- {
245- Comment (" EA trading schedule OFF" );
246- }
247-
279+ // checks done on every tick
280+
248281 if (!timeControl .IsTradingTimeValid ())
249282 {
250283 if (marketOrder .IsOpen (currentTicket ,_Symbol ,MagicNumber ))
@@ -258,6 +291,22 @@ void OnTick()
258291
259292 return ;
260293 }
294+
295+ tradeManager .Manage (currentTicket , tradeManagerState );
296+ }
297+
298+ // Signal handler
299+
300+ if (customBars .IsNewBar ())
301+ {
302+ if (timeControl .IsScheduleEnabled ())
303+ {
304+ Comment (" EA trading schedule ON (" +Start +" to " +End +" ) | trading enabled = " +(string )timeControl .IsTradingTimeValid ());
305+ }
306+ else
307+ {
308+ Comment (" EA trading schedule OFF" );
309+ }
261310
262311 //
263312 // Get moving average values for current, last completed bar and the bar before that...
@@ -280,19 +329,21 @@ void OnTick()
280329 if (timeControl .IsScheduleEnabled ())
281330 {
282331 Comment (" EA trading schedule (" +Start +" to " +End +" ) | trading enabled = " +(string )timeControl .IsTradingTimeValid ()+
283- " \n MA1 [2]: " +DoubleToString (MA1 [2 ],_Digits )+" [1]: " +DoubleToString (MA1 [1 ],_Digits )+
284- " \n MA2 [2]: " +DoubleToString (MA2 [2 ],_Digits )+" [1]: " +DoubleToString (MA2 [1 ],_Digits )+
332+ // "\n MA1 [2]: "+DoubleToString(MA1[2],_Digits)+" [1]: "+DoubleToString(MA1[1],_Digits)+
333+ // "\n MA2 [2]: "+DoubleToString(MA2[2],_Digits)+" [1]: "+DoubleToString(MA2[1],_Digits)+
285334 " \n MA cross signal = " +marketOrder .PositionTypeToString (signal )+
286335 " \n MA validation = " +marketOrder .PositionTypeToString (validation )+
336+ " \n Trade manager: " +tradeManager .ToString ()+
287337 " \n " );
288338 }
289339 else
290340 {
291341 Comment (" EA trading schedule not used. Trading is enabled." +
292- " \n MA1 [2]: " +DoubleToString (MA1 [2 ],_Digits )+" [1]: " +DoubleToString (MA1 [1 ],_Digits )+
293- " \n MA2 [2]: " +DoubleToString (MA2 [2 ],_Digits )+" [1]: " +DoubleToString (MA2 [1 ],_Digits )+
342+ // "\n MA1 [2]: "+DoubleToString(MA1[2],_Digits)+" [1]: "+DoubleToString(MA1[1],_Digits)+
343+ // "\n MA2 [2]: "+DoubleToString(MA2[2],_Digits)+" [1]: "+DoubleToString(MA2[1],_Digits)+
294344 " \n MA cross signal = " +marketOrder .PositionTypeToString (signal )+
295345 " \n MA validation = " +marketOrder .PositionTypeToString (validation )+
346+ " \n Trade manager: " +tradeManager .ToString ()+
296347 " \n " );
297348 }
298349
@@ -306,15 +357,18 @@ void OnTick()
306357 {
307358 PrintFormat (" Reversing %s position on Stop&Reverse condition (ticket:%d)" , _Symbol , currentTicket );
308359 marketOrder .Reverse (currentTicket ,Lots ,StopLoss ,TakeProfit );
360+ tradeManagerState .Clear ();
309361 }
310362 }
311363 return ;
312364 }
313365 else if (!marketOrder .IsOpen (_Symbol ,POSITION_TYPE_BUY ,MagicNumber ))
314366 {
315367 if (IsTradeDirectionValid (POSITION_TYPE_BUY ))
368+ {
316369 marketOrder .Long (_Symbol ,Lots ,StopLoss ,TakeProfit );
317-
370+ tradeManagerState .Clear ();
371+ }
318372 return ;
319373 }
320374 }
@@ -328,15 +382,18 @@ void OnTick()
328382 {
329383 PrintFormat (" Reversing %s position on Stop&Reverse condition (ticket:%d)" , _Symbol , currentTicket );
330384 marketOrder .Reverse (currentTicket ,Lots ,StopLoss ,TakeProfit );
385+ tradeManagerState .Clear ();
331386 }
332387 }
333388 return ;
334389 }
335390 else if (!marketOrder .IsOpen (_Symbol ,POSITION_TYPE_SELL ,MagicNumber ))
336391 {
337392 if (IsTradeDirectionValid (POSITION_TYPE_SELL ))
393+ {
394+ tradeManagerState .Clear ();
338395 marketOrder .Short (_Symbol ,Lots ,StopLoss ,TakeProfit );
339-
396+ }
340397 return ;
341398 }
342399 }
@@ -357,18 +414,20 @@ void OnTick()
357414 // reverse position on signal change inside gap.
358415 PrintFormat (" Reversing %s position on signal change inside gap (ticket:%d)" , _Symbol , currentTicket );
359416 marketOrder .Reverse (currentTicket ,Lots ,StopLoss ,TakeProfit );
417+ tradeManagerState .Clear ();
360418 }
361419 else
362420 {
363421 // close position on signal change inside gap.
364422 PrintFormat (" Closing %s position on signal change inside gap (ticket:%d)" , _Symbol , currentTicket );
365423 marketOrder .Close (currentTicket );
424+ tradeManagerState .Clear ();
366425 }
367426 }
368427 }
369428 }
370429 }
371- }
430+ }
372431}
373432
374433//
0 commit comments