spacepaste

  1.  
  2. //+------------------------------------------------------------------+
  3. //| My_First_EA.mq4 |
  4. //| Coders Guru |
  5. //| http://www.forex-tsd.com |
  6. //+------------------------------------------------------------------+
  7. #property copyright "Coders Guru"
  8. #property link "http://www.forex-tsd.com"
  9. //---- input parameters
  10. extern double TakeProfit=250.0;
  11. extern double Lots=0.1;
  12. extern double TrailingStop=35.0;
  13. //+------------------------------------------------------------------+
  14. //| expert initialization function |
  15. //+------------------------------------------------------------------+
  16. int init()
  17. {
  18. //----
  19. //----
  20. return(0);
  21. }
  22. //+------------------------------------------------------------------+
  23. //| expert deinitialization function |
  24. //+------------------------------------------------------------------+
  25. int deinit()
  26. {
  27. //----
  28. //----
  29. return(0);
  30. }
  31. int Crossed (double line1 , double line2)
  32. // I'm confused about initialization here ?
  33. // So only declared is required and then you can call it like seen below in isCrossed
  34. // Where isCrossed = Crossed(shortEMA,longEMA)?
  35. {
  36. static int last_direction = 0;
  37. static int current_dirction = 0;
  38. if(line1>line2)current_dirction = 1; //up
  39. if(line1<line2)current_dirction = 2; //down
  40. if(current_dirction != last_direction) //changed
  41. {
  42. last_direction = current_dirction;
  43. return (last_direction);
  44. }
  45. else
  46. {
  47. return (0);
  48. }
  49. }
  50. //+------------------------------------------------------------------+
  51. //| expert start function |
  52. //+------------------------------------------------------------------+
  53. int start()
  54. {
  55. //----
  56. int cnt, ticket, total;
  57. double shortEma, longEma;
  58. if(Bars<100)
  59. {
  60. Print("bars less than 100");
  61. return(0);
  62. }
  63. if(TakeProfit<10)
  64. {
  65. Print("TakeProfit less than 10");
  66. return(0); // check TakeProfit
  67. }
  68. shortEma = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
  69. longEma = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
  70. int isCrossed = Crossed (shortEma,longEma);
  71. total = OrdersTotal();
  72. if(total < 1)
  73. {
  74. if(isCrossed == 1)
  75. {
  76. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);
  77. if(ticket>0)
  78. {
  79. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
  80. }
  81. else Print("Error opening BUY order : ",GetLastError());
  82. return(0);
  83. }
  84. if(isCrossed == 2)
  85. {
  86. ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);
  87. if(ticket>0)
  88. {
  89. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
  90. }
  91. else Print("Error opening SELL order : ",GetLastError());
  92. return(0);
  93. }
  94. return(0);
  95. }
  96. for(cnt=0;cnt<total;cnt++)
  97. {
  98. OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
  99. if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
  100. {
  101. if(OrderType()==OP_BUY) // long position is opened
  102. {
  103. // should it be closed?
  104. if(isCrossed == 2)
  105. {
  106. OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
  107. return(0); // exit
  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. OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
  117. return(0);
  118. }
  119. }
  120. }
  121. }
  122. else // go to short position
  123. {
  124. // should it be closed?
  125. if(isCrossed == 1)
  126. {
  127. OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
  128. return(0); // exit
  129. }
  130. // check for trailing stop
  131. if(TrailingStop>0)
  132. {
  133. if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
  134. {
  135. if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
  136. {
  137. OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
  138. return(0);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return(0);
  146. }
  147. //+------------------------------------------------------------------+
  148.