spacepaste

  1.  
  2. //+------------------------------------------------------------------+
  3. //| My_First_EA_edits.mq4 |
  4. //| Agent86's Version of Dirty Rat |
  5. //| http://www.iclbiz.com/joomla |
  6. //+------------------------------------------------------------------+
  7. #property copyright "Agent86"
  8. #property link "www.iclbiz.com/joomla"
  9. //---- input parameters
  10. extern double TakeProfit=200.0;
  11. extern double Lots=0.1;
  12. extern double StopLoss=120.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_1 (double line1 , double line2)
  32. {
  33. static int last_direction = 0;
  34. static int current_direction = 0;
  35. if(line1>line2)current_direction = 1; //up
  36. if(line1<line2)current_direction = 2; //down
  37. if(current_direction != last_direction) //changed
  38. {
  39. last_direction = current_direction;
  40. return (last_direction);
  41. }
  42. else
  43. {
  44. return (0);
  45. }
  46. }
  47. //my attempt to make/duplicate a function to make 5min and 4hr MACD agreement
  48. int Crossed_2 (double line1 , double line2)
  49. {
  50. static int last_direction = 0;
  51. static int current_direction = 0;
  52. if(line1>line2)current_direction = 1; //up
  53. if(line1<line2)current_direction = 2; //down
  54. if(current_direction != last_direction) //changed
  55. {
  56. last_direction = current_direction;
  57. return (last_direction);
  58. }
  59. else
  60. {
  61. return (0);
  62. }
  63. }
  64. //+------------------------------------------------------------------+
  65. //| expert start function |
  66. //+------------------------------------------------------------------+
  67. int start()
  68. {
  69. //----
  70. int cnt, ticket, total;
  71. double faster_1, slower_1;
  72. double faster_2, slower_2;
  73. if(Bars<100)
  74. {
  75. Print("bars less than 100");
  76. return(0);
  77. }
  78. if(TakeProfit<10)
  79. {
  80. Print("TakeProfit less than 10");
  81. return(0); // check TakeProfit
  82. }
  83. // added PERIOD_H4 to try to trade both 5min and 4hr MACD agreement ?
  84. faster_1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
  85. slower_1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
  86. faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
  87. slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
  88. int isCrossed_1 = Crossed_1 (faster_1,slower_1);
  89. int isCrossed_2 = Crossed_2 (faster_2,slower_2);
  90. total = OrdersTotal();
  91. if(total < 1)
  92. {
  93. if(isCrossed_1 && isCrossed_2 == 1) //thought this would make them agree ?
  94. {
  95. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Agent86",12345,0,Green);
  96. if(ticket>0)
  97. {
  98. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
  99. }
  100. else Print("Error opening BUY order : ",GetLastError());
  101. return(0);
  102. }
  103. if(isCrossed_1 && isCrossed_2 == 2) //and this should agree ?
  104. {
  105. ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Agent86",12345,0,Red);
  106. if(ticket>0)
  107. {
  108. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
  109. }
  110. else Print("Error opening SELL order : ",GetLastError());
  111. return(0);
  112. }
  113. return(0);
  114. }
  115. for(cnt=0;cnt<total;cnt++)
  116. {
  117. OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
  118. if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
  119. {
  120. if(OrderType()==OP_BUY) // long position is opened
  121. {
  122. // should it be closed?
  123. if(isCrossed_1 == 2)
  124. {
  125. OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
  126. return(0); // exit
  127. }
  128. // should it be closed?
  129. if(isCrossed_1 == 1)
  130. {
  131. OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
  132. return(0); // exit
  133. }
  134. }
  135. }
  136. }
  137. return(0);
  138. }
  139. //+------------------------------------------------------------------+
  140.