spacepaste

  1.  
  2. //+------------------------------------------------------------------+
  3. //| My_First_EA_edits.mq4 |
  4. //| Agent86's Dirty Rat Trade |
  5. //| http://www.iclbiz.com/joomla |
  6. //+------------------------------------------------------------------+
  7. #property copyright "Agent86 My First EA"
  8. #property link "www.iclbiz.com/joomla"
  9. //---- input parameters
  10. extern double TakeProfit=1000.0;
  11. extern double Lots=0.1;
  12. extern double StopLoss=500.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. {
  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. int Crossed_2 (double line1 , double line2) //i'll use this later for my macd agreement
  48. {
  49. static int last_direction_2 = 0;
  50. static int current_direction_2 = 0;
  51. if(line1>line2)current_direction_2 = 1; //up
  52. if(line1<line2)current_direction_2 = 2; //down
  53. if(current_direction_2 != last_direction_2) //changed
  54. {
  55. last_direction_2 = current_direction_2;
  56. return (last_direction_2);
  57. }
  58. else
  59. {
  60. return (0);
  61. }
  62. }
  63. //+------------------------------------------------------------------+
  64. //| expert start function |
  65. //+------------------------------------------------------------------+
  66. int start()
  67. {
  68. //----
  69. int cnt, ticket, total;
  70. double faster, slower, faster_2, slower_2;
  71. if(Bars<100)
  72. {
  73. Print("bars less than 100");
  74. return(0);
  75. }
  76. if(TakeProfit<10)
  77. {
  78. Print("TakeProfit less than 10");
  79. return(0); // check TakeProfit
  80. }
  81. faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
  82. slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
  83. faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_UPPER,0); //MODE_MAIN
  84. slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_UPPER,0); //MODE_SIGNAL
  85. int isCrossed = Crossed (faster,slower);
  86. int isCrossed_2 = Crossed_2 (faster_2,slower_2); // i'll use this later
  87. total = OrdersTotal();
  88. if(total < 1)
  89. {
  90. if(isCrossed == 1)
  91. {
  92. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Agent86",12345,0,Green);
  93. if(ticket>0)
  94. {
  95. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
  96. }
  97. else Print("Error opening BUY order : ",GetLastError());
  98. return(0);
  99. }
  100. if(isCrossed == 2)
  101. {
  102. ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Agent86",12345,0,Red);
  103. if(ticket>0)
  104. {
  105. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
  106. }
  107. else Print("Error opening SELL order : ",GetLastError());
  108. return(0);
  109. }
  110. return(0);
  111. }
  112. for(cnt=0;cnt<total;cnt++)
  113. {
  114. OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
  115. if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
  116. {
  117. if(OrderType()==OP_BUY) // long position is opened
  118. {
  119. // should it be closed?
  120. if(isCrossed == 2)
  121. {
  122. OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
  123. return(0); // exit
  124. }
  125. // should it be closed?
  126. if(isCrossed == 1)
  127. {
  128. OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
  129. return(0); // exit
  130. }
  131. }
  132. }
  133. }
  134. return(0);
  135. }
  136. //+------------------------------------------------------------------+
  137.