MACD & CCI .mq4


SUBMITTED BY: wendra

DATE: Nov. 22, 2015, 10:59 a.m.

FORMAT: Text only

SIZE: 6.3 kB

HITS: 568

  1. //+------------------------------------------------------------------+
  2. //| MACD & CCI .mq4 |
  3. //| Copyright © 2011-2012, baguswidyantoro |
  4. //| http://baguswidyantoro.t35.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright © 2011, isikan nama anda disni"
  7. #property link "http://isikan website anda disini"
  8. #include <stdlib.mqh>
  9. #include <stderror.mqh>
  10. #define MAGICMA 20050610
  11. //---- input parameters
  12. extern int FMa=4; // Fast MA
  13. extern int SMa=8; // Slow MA
  14. extern int PCCi=4; // CCI Period
  15. extern int pATR=4; // ATR Period for S/L
  16. extern double Lots=0.1; // Lot
  17. extern bool SndMl=true; // E-mail Sending Parameter
  18. extern double DcF = 3; // Optimization Factor
  19. extern double MaxR = 0.02; // Maximum Risk
  20. //+------------------------------------------------------------------+
  21. //| expert initialization function |
  22. //+------------------------------------------------------------------+
  23. int CalculateCurrentOrders(string symbol)
  24. {
  25. int buys=0,sells=0;
  26. //----
  27. for(int i=0;i<OrdersTotal();i++)
  28. {
  29. if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
  30. break;
  31. if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
  32. {
  33. if(OrderType()==OP_BUY)
  34. buys++;
  35. if(OrderType()==OP_SELL)
  36. sells++;
  37. }
  38. }
  39. //---- return orders volume
  40. if(buys>0)
  41. return(buys);
  42. else
  43. return(-sells);
  44. }
  45. void CheckForOpen()
  46. {
  47. double mas;
  48. double maf;
  49. double mas_p;
  50. double maf_p;
  51. double Atr;
  52. double icc;
  53. double icc_p;
  54. int res;
  55. string sHeaderLetter;
  56. string sBodyLetter;
  57. //---- trading will be started with first tick of new bar only
  58. if(Volume[0]>1) return;
  59. //---- define Moving Average
  60. mas=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,1); // Slow MA shifted on 1 Period
  61. maf=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,1); // Fast MA shifted on 1 Period
  62. mas_p=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,2); // Slow MA shifted on 2 Period
  63. maf_p=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,2); // Fast MA shifted on 2 Period
  64. Atr = iATR(NULL,0,pATR,0);
  65. icc = iCCI(NULL,0,PCCi,PRICE_CLOSE,1); // CCI shifted on 1 Period
  66. icc_p = iCCI(NULL,0,PCCi,PRICE_CLOSE,2); // CCI shifted on 2 Period
  67. //---- check for open sell order
  68. if ( (maf<mas && maf_p>=mas_p)&&(icc<0 && icc_p >=0 ))
  69. {
  70. res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Ask+Atr,0,"",MAGICMA,0,Red);
  71. if (SndMl == True && res != -1)
  72. {
  73. sHeaderLetter = "Operation SELL by " + Symbol()+"";
  74. sBodyLetter = "Order Sell by "+ Symbol() + " at " + DoubleToStr(Bid,4)+ ", and set stop/loss at " + DoubleToStr(Ask+Atr,4)+"";
  75. sndMessage(sHeaderLetter, sBodyLetter);
  76. }
  77. return;
  78. }
  79. //---- check for open buy order
  80. if ((maf>mas && maf_p<=mas_p)&& (icc > 0 && icc_p <=0 ))
  81. {
  82. res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Bid-Atr,0,"",MAGICMA,0,Blue);
  83. if ( SndMl == True && res != -1)
  84. {
  85. sHeaderLetter = "Operation BUY at " + Symbol()+"";
  86. sBodyLetter = "Order Buy at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and set stop/loss at " + DoubleToStr(Bid-Atr,4)+"";
  87. sndMessage(sHeaderLetter, sBodyLetter);
  88. }
  89. return;
  90. }
  91. }
  92. void CheckForClose()
  93. {
  94. double mas;
  95. double maf;
  96. double mas_p;
  97. double maf_p;
  98. string sHeaderLetter;
  99. string sBodyLetter;
  100. bool CloseOrd;
  101. //----
  102. if(Volume[0]>1) return;
  103. //----
  104. mas=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,1); // Slow MA shifted on 1 Period
  105. maf=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,1); // Fast MA shifted on 1 Period
  106. mas_p=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,2); // Slow MA shifted on 2 Period
  107. maf_p=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,2); // Fast MA shifted on 2 Period
  108. //----
  109. for(int i=0;i<OrdersTotal();i++)
  110. {
  111. if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
  112. if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  113. //----
  114. if(OrderType()==OP_BUY)
  115. {
  116. if(maf<mas && maf_p>=mas_p) CloseOrd=OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
  117. if ( SndMl == True && CloseOrd == True)
  118. {
  119. sHeaderLetter = "Operation CLOSE BUY at" + Symbol()+"";
  120. sBodyLetter = "Close order Buy at "+ Symbol() + " for " + DoubleToStr(Bid,4)+ ", and finish this Trade";
  121. sndMessage(sHeaderLetter, sBodyLetter);
  122. }
  123. break;
  124. }
  125. if(OrderType()==OP_SELL)
  126. {
  127. if(maf>mas && maf_p<=mas_p) OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
  128. if ( SndMl == True && CloseOrd == True)
  129. {
  130. sHeaderLetter = "Operation CLOSE SELL at" + Symbol()+"";
  131. sBodyLetter = "Close order Sell at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and finish this Trade";
  132. sndMessage(sHeaderLetter, sBodyLetter);
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. //+------------------------------------------------------------------+
  139. //| Optimized Lot Value Calculation |
  140. //+------------------------------------------------------------------+
  141. double LotsOptimized()
  142. {
  143. double lot=Lots;
  144. int orders=HistoryTotal(); // history orders total
  145. int losses=0; // number of losses orders without a break
  146. //---- select lot size
  147. lot=NormalizeDouble(AccountFreeMargin()*MaxR/1000.0,1);
  148. //---- calcuulate number of losses orders without a break
  149. if(DcF>0)
  150. {
  151. for(int i=orders-1;i>=0;i--)
  152. {
  153. if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Îøèáêà â èñòîðèè!"); break; }
  154. if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
  155. //----
  156. if(OrderProfit()>0) break;
  157. if(OrderProfit()<0) losses++;
  158. }
  159. if(losses>1) lot=NormalizeDouble(lot-lot*losses/DcF,1);
  160. }
  161. //---- return lot size
  162. if(lot<0.1) lot=0.1;
  163. return(lot);
  164. }
  165. //-------------------------------------------------------------------+
  166. // Send e-mail message function |
  167. //-------------------------------------------------------------------+
  168. void sndMessage(string HeaderLetter, string BodyLetter)
  169. {
  170. int RetVal;
  171. SendMail( HeaderLetter, BodyLetter );
  172. RetVal = GetLastError();
  173. if (RetVal!= ERR_NO_MQLERROR) Print ("Îøèáêà, ñîîáùåíèå íå îòïðàâëåíî: ", ErrorDescription(RetVal));
  174. }
  175. //+------------------------------------------------------------------+
  176. //| Expert start function |
  177. //+------------------------------------------------------------------+
  178. int start()
  179. {
  180. //----
  181. if(Bars<25 || IsTradeAllowed()==false) return;
  182. //---- calculate open orders by current symbol
  183. if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
  184. else CheckForClose();
  185. //----
  186. return(0);
  187. }
  188. //+------------------------------------------------------------------+

comments powered by Disqus