fork download
  1. //+------------------------------------------------------------------+
  2. //| MACD Sample.mq4 |
  3. //| Copyright 2005-2014, MetaQuotes Software Corp. |
  4. //| http://w...content-available-to-author-only...4.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "2005-2014, MetaQuotes Software Corp."
  7. #property link "http://w...content-available-to-author-only...4.com"
  8.  
  9. input double TakeProfit =50;
  10. input double Lots =0.1;
  11. input double TrailingStop =30;
  12. input double MACDOpenLevel =3;
  13. input double MACDCloseLevel=2;
  14. input int MATrendPeriod =26;
  15. //+------------------------------------------------------------------+
  16. //| |
  17. //+------------------------------------------------------------------+
  18. void OnTick(void)
  19. {
  20. double MacdCurrent,MacdPrevious;
  21. double SignalCurrent,SignalPrevious;
  22. double MaCurrent,MaPrevious;
  23. int cnt,ticket,total;
  24. //---
  25. // initial data checks
  26. // it is important to make sure that the expert works with a normal
  27. // chart and the user did not make any mistakes setting external
  28. // variables (Lots, StopLoss, TakeProfit,
  29. // TrailingStop) in our case, we check TakeProfit
  30. // on a chart of less than 100 bars
  31. //---
  32. if(Bars<100)
  33. {
  34. Print("bars less than 100");
  35. return;
  36. }
  37. if(TakeProfit<10)
  38. {
  39. Print("TakeProfit less than 10");
  40. return;
  41. }
  42. //--- to simplify the coding and speed up access data are put into internal variables
  43. MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
  44. MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
  45. SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
  46. SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
  47. MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
  48. MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
  49.  
  50. total=OrdersTotal();
  51. if(total<1)
  52. {
  53. //--- no opened orders identified
  54. if(AccountFreeMargin()<(1000*Lots))
  55. {
  56. Print("We have no money. Free Margin = ",AccountFreeMargin());
  57. return;
  58. }
  59. //--- check for long position (BUY) possibility
  60. if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
  61. MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
  62. {
  63. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
  64. if(ticket>0)
  65. {
  66. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
  67. Print("BUY order opened : ",OrderOpenPrice());
  68. }
  69. else
  70. Print("Error opening BUY order : ",GetLastError());
  71. return;
  72. }
  73. //--- check for short position (SELL) possibility
  74. if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
  75. MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
  76. {
  77. ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
  78. if(ticket>0)
  79. {
  80. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
  81. Print("SELL order opened : ",OrderOpenPrice());
  82. }
  83. else
  84. Print("Error opening SELL order : ",GetLastError());
  85. }
  86. //--- exit from the "no opened orders" block
  87. return;
  88. }
  89. //--- it is important to enter the market correctly, but it is more important to exit it correctly...
  90. for(cnt=0;cnt<total;cnt++)
  91. {
  92. if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
  93. continue;
  94. if(OrderType()<=OP_SELL && // check for opened position
  95. OrderSymbol()==Symbol()) // check for symbol
  96. {
  97. //--- long position is opened
  98. if(OrderType()==OP_BUY)
  99. {
  100. //--- should it be closed?
  101. if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
  102. MacdCurrent>(MACDCloseLevel*Point))
  103. {
  104. //--- close order and exit
  105. if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
  106. Print("OrderClose error ",GetLastError());
  107. return;
  108. }
  109. //--- check for trailing stop
  110. if(TrailingStop>0)
  111. {
  112. if(Bid-OrderOpenPrice()>Point*TrailingStop)
  113. {
  114. if(OrderStopLoss()<Bid-Point*TrailingStop)
  115. {
  116. //--- modify order and exit
  117. if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
  118. Print("OrderModify error ",GetLastError());
  119. return;
  120. }
  121. }
  122. }
  123. }
  124. else // go to short position
  125. {
  126. //--- should it be closed?
  127. if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
  128. MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
  129. {
  130. //--- close order and exit
  131. if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
  132. Print("OrderClose error ",GetLastError());
  133. return;
  134. }
  135. //--- check for trailing stop
  136. if(TrailingStop>0)
  137. {
  138. if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
  139. {
  140. if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
  141. {
  142. //--- modify order and exit
  143. if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
  144. Print("OrderModify error ",GetLastError());
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. //---
  153. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:6:2: error: invalid preprocessing directive #property
 #property copyright   "2005-2014, MetaQuotes Software Corp."
  ^~~~~~~~
prog.c:7:2: error: invalid preprocessing directive #property
 #property link        "http://www.mql4.com"
  ^~~~~~~~
prog.c:9:6: error: expected ‘;’ before ‘double’
 input double TakeProfit    =50;
      ^~~~~~~
      ;
prog.c:10:6: error: expected ‘;’ before ‘double’
 input double Lots          =0.1;
      ^~~~~~~
      ;
prog.c:11:6: error: expected ‘;’ before ‘double’
 input double TrailingStop  =30;
      ^~~~~~~
      ;
prog.c:12:6: error: expected ‘;’ before ‘double’
 input double MACDOpenLevel =3;
      ^~~~~~~
      ;
prog.c:13:6: error: expected ‘;’ before ‘double’
 input double MACDCloseLevel=2;
      ^~~~~~~
      ;
prog.c:14:6: error: expected ‘;’ before ‘int’
 input int    MATrendPeriod =26;
      ^~~~
      ;
prog.c: In function ‘OnTick’:
prog.c:32:7: error: ‘Bars’ undeclared (first use in this function)
    if(Bars<100)
       ^~~~
prog.c:32:7: note: each undeclared identifier is reported only once for each function it appears in
prog.c:34:7: warning: implicit declaration of function ‘Print’ [-Wimplicit-function-declaration]
       Print("bars less than 100");
       ^~~~~
prog.c:43:16: warning: implicit declaration of function ‘iMACD’ [-Wimplicit-function-declaration]
    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                ^~~~~
prog.c:43:22: error: ‘NULL’ undeclared (first use in this function)
    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                      ^~~~
prog.c:43:22: note: ‘NULL’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
prog.c:1:1:
+#include <stddef.h>
 //+------------------------------------------------------------------+
prog.c:43:22:
    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                      ^~~~
prog.c:43:37: error: ‘PRICE_CLOSE’ undeclared (first use in this function)
    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                                     ^~~~~~~~~~~
prog.c:43:49: error: ‘MODE_MAIN’ undeclared (first use in this function)
    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                                                 ^~~~~~~~~
prog.c:45:51: error: ‘MODE_SIGNAL’ undeclared (first use in this function)
    SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
                                                   ^~~~~~~~~~~
prog.c:47:14: warning: implicit declaration of function ‘iMA’ [-Wimplicit-function-declaration]
    MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
              ^~~
prog.c:47:41: error: ‘MODE_EMA’ undeclared (first use in this function)
    MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
                                         ^~~~~~~~
prog.c:50:10: warning: implicit declaration of function ‘OrdersTotal’ [-Wimplicit-function-declaration]
    total=OrdersTotal();
          ^~~~~~~~~~~
prog.c:54:10: warning: implicit declaration of function ‘AccountFreeMargin’ [-Wimplicit-function-declaration]
       if(AccountFreeMargin()<(1000*Lots))
          ^~~~~~~~~~~~~~~~~
prog.c:61:10: warning: implicit declaration of function ‘MathAbs’ [-Wimplicit-function-declaration]
          MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
          ^~~~~~~
prog.c:61:46: error: ‘Point’ undeclared (first use in this function); did you mean ‘int’?
          MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
                                              ^~~~~
                                              int
prog.c:63:17: warning: implicit declaration of function ‘OrderSend’ [-Wimplicit-function-declaration]
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
                 ^~~~~~~~~
prog.c:63:27: warning: implicit declaration of function ‘Symbol’ [-Wimplicit-function-declaration]
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
                           ^~~~~~
prog.c:63:36: error: ‘OP_BUY’ undeclared (first use in this function)
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
                                    ^~~~~~
prog.c:63:48: error: ‘Ask’ undeclared (first use in this function)
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
                                                ^~~
prog.c:63:99: error: ‘Green’ undeclared (first use in this function)
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
                                                                                                   ^~~~~
prog.c:66:16: warning: implicit declaration of function ‘OrderSelect’ [-Wimplicit-function-declaration]
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                ^~~~~~~~~~~
prog.c:66:35: error: ‘SELECT_BY_TICKET’ undeclared (first use in this function)
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                   ^~~~~~~~~~~~~~~~
prog.c:66:52: error: ‘MODE_TRADES’ undeclared (first use in this function)
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                                    ^~~~~~~~~~~
prog.c:67:44: warning: implicit declaration of function ‘OrderOpenPrice’ [-Wimplicit-function-declaration]
                Print("BUY order opened : ",OrderOpenPrice());
                                            ^~~~~~~~~~~~~~
prog.c:70:48: warning: implicit declaration of function ‘GetLastError’ [-Wimplicit-function-declaration]
             Print("Error opening BUY order : ",GetLastError());
                                                ^~~~~~~~~~~~
prog.c:77:36: error: ‘OP_SELL’ undeclared (first use in this function)
          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
                                    ^~~~~~~
prog.c:77:49: error: ‘Bid’ undeclared (first use in this function); did you mean ‘void’?
          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
                                                 ^~~
                                                 void
prog.c:77:100: error: ‘Red’ undeclared (first use in this function)
          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
                                                                                                    ^~~
prog.c:92:27: error: ‘SELECT_BY_POS’ undeclared (first use in this function)
       if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
                           ^~~~~~~~~~~~~
prog.c:94:10: warning: implicit declaration of function ‘OrderType’ [-Wimplicit-function-declaration]
       if(OrderType()<=OP_SELL &&   // check for opened position
          ^~~~~~~~~
prog.c:95:10: warning: implicit declaration of function ‘OrderSymbol’ [-Wimplicit-function-declaration]
          OrderSymbol()==Symbol())  // check for symbol
          ^~~~~~~~~~~
prog.c:105:20: warning: implicit declaration of function ‘OrderClose’ [-Wimplicit-function-declaration]
                if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                    ^~~~~~~~~~
prog.c:105:31: warning: implicit declaration of function ‘OrderTicket’ [-Wimplicit-function-declaration]
                if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                               ^~~~~~~~~~~
prog.c:105:45: warning: implicit declaration of function ‘OrderLots’ [-Wimplicit-function-declaration]
                if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                                             ^~~~~~~~~
prog.c:105:63: error: ‘Violet’ undeclared (first use in this function); did you mean ‘ticket’?
                if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                                                               ^~~~~~
                                                               ticket
prog.c:114:22: warning: implicit declaration of function ‘OrderStopLoss’ [-Wimplicit-function-declaration]
                   if(OrderStopLoss()<Bid-Point*TrailingStop)
                      ^~~~~~~~~~~~~
prog.c:117:26: warning: implicit declaration of function ‘OrderModify’ [-Wimplicit-function-declaration]
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                          ^~~~~~~~~~~
prog.c:117:92: warning: implicit declaration of function ‘OrderTakeProfit’ [-Wimplicit-function-declaration]
                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                                                                                            ^~~~~~~~~~~~~~~
stdout
Standard output is empty