fork download
  1. //+------------------------------------------------------------------+
  2. //| XAUUSD SMA Crossover EA with TP, SL, Trailing Stop, Volume |
  3. //+------------------------------------------------------------------+
  4. #property strict
  5. #include <Trade\Trade.mqh>
  6. CTrade trade;
  7.  
  8. input int fastPeriod = 2;
  9. input int slowPeriod = 5;
  10. input double lotSize = 0.01;
  11. input double slippage = 0.03;
  12. input double maxLossUSD = 10.0;
  13. input double takeProfitUSD = 10.0;
  14. input bool debugLogs = true;
  15.  
  16. int fastHandle, slowHandle;
  17. double fastBuffer[], slowBuffer[];
  18. long volume[];
  19.  
  20. double PointSize, TickValue;
  21. int DigitsCount;
  22. double SL_Points, TP_Points;
  23.  
  24. //+------------------------------------------------------------------+
  25. int OnInit()
  26. {
  27. fastHandle = iMA(_Symbol, _Period, fastPeriod, 0, MODE_SMA, PRICE_CLOSE);
  28. slowHandle = iMA(_Symbol, _Period, slowPeriod, 0, MODE_SMA, PRICE_OPEN);
  29.  
  30. if(fastHandle == INVALID_HANDLE || slowHandle == INVALID_HANDLE)
  31. {
  32. Print("Indicator creation failed.");
  33. return INIT_FAILED;
  34. }
  35.  
  36. ArraySetAsSeries(fastBuffer, true);
  37. ArraySetAsSeries(slowBuffer, true);
  38. ArraySetAsSeries(volume, true);
  39.  
  40. SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE, TickValue);
  41. SymbolInfoDouble(_Symbol, SYMBOL_POINT, PointSize);
  42. DigitsCount = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
  43.  
  44. SL_Points = NormalizeDouble((maxLossUSD / (TickValue * lotSize)) / PointSize, 0);
  45. TP_Points = NormalizeDouble((takeProfitUSD / (TickValue * lotSize)) / PointSize, 0);
  46.  
  47. return INIT_SUCCEEDED;
  48. }
  49.  
  50. //+------------------------------------------------------------------+
  51. void OnTick()
  52. {
  53. if(Bars(_Symbol, _Period) < 25) return;
  54.  
  55. // Apply trailing stop if there's a position
  56. if(PositionSelect(_Symbol))
  57. ManageTrailingStop();
  58.  
  59. if(CopyBuffer(fastHandle, 0, 0, 3, fastBuffer) < 3 ||
  60. CopyBuffer(slowHandle, 0, 0, 3, slowBuffer) < 3 ||
  61. CopyTickVolume(_Symbol, 0, 0, 25, volume) < 25)
  62. {
  63. Print("Failed to copy data.");
  64. return;
  65. }
  66.  
  67. // Volume filter check
  68. double avgVol = 0;
  69. for(int i = 3; i < 23; i++) avgVol += volume[i];
  70. avgVol /= 20;
  71.  
  72. if(volume[1] <= avgVol || volume[2] <= avgVol)
  73. {
  74. if(debugLogs)
  75. Print("Low volume — no trade. V1:", volume[1], " V2:", volume[2], " AVG:", avgVol);
  76. return;
  77. }
  78.  
  79. // Crossover
  80. bool bullish = fastBuffer[1] < slowBuffer[1] && fastBuffer[0] > slowBuffer[0];
  81. bool bearish = fastBuffer[1] > slowBuffer[1] && fastBuffer[0] < slowBuffer[0];
  82.  
  83. bool hasPosition = PositionSelect(_Symbol);
  84. long posType = hasPosition ? PositionGetInteger(POSITION_TYPE) : -1;
  85.  
  86. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  87. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  88.  
  89. // Exit on reverse crossover
  90. if(hasPosition)
  91. {
  92. if((posType == POSITION_TYPE_BUY && bearish) ||
  93. (posType == POSITION_TYPE_SELL && bullish))
  94. {
  95. trade.PositionClose(_Symbol);
  96. Print("Closed due to reverse crossover.");
  97. return;
  98. }
  99. }
  100.  
  101. // Entry
  102. if(!hasPosition)
  103. {
  104. if(bullish)
  105. {
  106. double sl = NormalizeDouble(bid - SL_Points * PointSize, DigitsCount);
  107. double tp = NormalizeDouble(bid + TP_Points * PointSize, DigitsCount);
  108. if(trade.Buy(lotSize, _Symbol, ask, slippage, sl, tp))
  109. Print("BUY @ ", ask, " SL: ", sl, " TP: ", tp);
  110. else
  111. Print("BUY failed: ", trade.ResultRetcodeDescription());
  112. }
  113. else if(bearish)
  114. {
  115. double sl = NormalizeDouble(ask + SL_Points * PointSize, DigitsCount);
  116. double tp = NormalizeDouble(ask - TP_Points * PointSize, DigitsCount);
  117. if(trade.Sell(lotSize, _Symbol, bid, slippage, sl, tp))
  118. Print("SELL @ ", bid, " SL: ", sl, " TP: ", tp);
  119. else
  120. Print("SELL failed: ", trade.ResultRetcodeDescription());
  121. }
  122. }
  123. }
  124.  
  125. //+------------------------------------------------------------------+
  126. //| Trailing Stop Management |
  127. //+------------------------------------------------------------------+
  128. void ManageTrailingStop()
  129. {
  130. double currentPrice = 0.0;
  131. long type = PositionGetInteger(POSITION_TYPE);
  132. double entry = PositionGetDouble(POSITION_PRICE_OPEN);
  133. double sl = PositionGetDouble(POSITION_SL);
  134. double tp = PositionGetDouble(POSITION_TP);
  135.  
  136. if(type == POSITION_TYPE_BUY)
  137. {
  138. currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  139. double newSL = NormalizeDouble(currentPrice - SL_Points * PointSize, DigitsCount);
  140. if(newSL > sl)
  141. trade.PositionModify(_Symbol, newSL, tp);
  142. }
  143. else if(type == POSITION_TYPE_SELL)
  144. {
  145. currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  146. double newSL = NormalizeDouble(currentPrice + SL_Points * PointSize, DigitsCount);
  147. if(newSL < sl || sl == 0.0)
  148. trade.PositionModify(_Symbol, newSL, tp);
  149. }
  150. }
  151.  
  152. //+------------------------------------------------------------------+
  153. void OnDeinit(const int reason)
  154. {
  155. if(fastHandle != INVALID_HANDLE) IndicatorRelease(fastHandle);
  156. if(slowHandle != INVALID_HANDLE) IndicatorRelease(slowHandle);
  157. }
  158. //+------------------------------------------------------------------+
Success #stdin #stdout #stderr 0.01s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "/": syntax error
Error: near line 8: near "input": syntax error
Error: near line 9: near "input": syntax error
Error: near line 10: near "input": syntax error
Error: near line 11: near "input": syntax error
Error: near line 12: near "input": syntax error
Error: near line 13: near "input": syntax error
Error: near line 14: near "input": syntax error
Error: near line 16: near "int": syntax error
Error: near line 17: near "double": syntax error
Error: near line 18: near "long": syntax error
Error: near line 20: near "double": syntax error
Error: near line 21: near "int": syntax error
Error: near line 22: near "double": syntax error
Error: near line 24: near "/": syntax error
Error: near line 28: near "slowHandle": syntax error
Error: near line 30: near "if": syntax error
Error: near line 33: near "return": syntax error
Error: near line 34: unrecognized token: "}"
Error: near line 37: near "ArraySetAsSeries": syntax error
Error: near line 38: near "ArraySetAsSeries": syntax error
Error: near line 40: near "SymbolInfoDouble": syntax error
Error: near line 41: near "SymbolInfoDouble": syntax error
Error: near line 42: near "DigitsCount": syntax error
Error: near line 44: near "SL_Points": syntax error
Error: near line 45: near "TP_Points": syntax error
Error: near line 47: near "return": syntax error
Error: near line 48: unrecognized token: "}"
Error: near line 55: near "/": syntax error