-
- //+------------------------------------------------------------------+
- //| My_First_EA_edits.mq4 |
- //| Agent86's Version of Dirty Rat |
- //| http://www.iclbiz.com/joomla |
- //+------------------------------------------------------------------+
- #property copyright "Agent86"
- #property link "www.iclbiz.com/joomla"
-
- //---- input parameters
- extern double TakeProfit=1000.0;
- extern double Lots=0.1;
- extern double StopLoss=500.0;
- //+------------------------------------------------------------------+
- //| expert initialization function |
- //+------------------------------------------------------------------+
- int init()
- {
- //----
-
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| expert deinitialization function |
- //+------------------------------------------------------------------+
- int deinit()
- {
- //----
-
- //----
- return(0);
- }
-
- int Crossed_1 (double line1 , double line2)
- {
- static int last_direction_1 = 0;
- static int current_direction_1 = 0;
-
- if(line1>line2)current_direction_1 = 1; //up
- if(line1<line2)current_direction_1 = 2; //down
-
-
-
- if(current_direction_1 != last_direction_1) //changed
- {
- last_direction_1 = current_direction_1;
- return (last_direction_1);
- }
- else
- {
- return (0);
- }
- }
-
- //my attempt to make/duplicate a function to make 5min and 4hr MACD agreement
- int Crossed_2 (double line1 , double line2)
- {
- static int last_direction_2 = 0;
- static int current_direction_2 = 0;
-
- if(line1>line2)current_direction_2 = 1; //up
- if(line1<line2)current_direction_2 = 2; //down
-
-
-
- if(current_direction_2 != last_direction_2) //changed
- {
- last_direction_2 = current_direction_2;
- return (last_direction_2);
- }
- else
- {
- return (0);
- }
- }
- //+------------------------------------------------------------------+
- //| expert start function |
- //+------------------------------------------------------------------+
- int start()
- {
- //----
-
-
- int cnt, ticket, total;
- double faster_1, slower_1;
- double faster_2, slower_2;
-
- if(Bars<100)
- {
- Print("bars less than 100");
- return(0);
- }
- if(TakeProfit<10)
- {
- Print("TakeProfit less than 10");
- return(0); // check TakeProfit
- }
- // added PERIOD_H4 to try to trade both 5min and 4hr MACD agreement ?
- faster_1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
- slower_1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
- faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
- slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
-
- int isCrossed_1 = Crossed_1 (faster_1,slower_1);
- int isCrossed_2 = Crossed_2 (faster_2,slower_2);
-
- total = OrdersTotal();
- if(total < 1)
- {
-
- if(isCrossed_1 == 1 && isCrossed_2 == 1) //thought this would make them agree ?
- {
- ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Agent86",12345,0,Green);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
- }
- else Print("Error opening BUY order : ",GetLastError());
- return(0);
- }
-
- if(isCrossed_1 == 2 && isCrossed_2 == 2) //and this should agree ?
- {
- ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Agent86",12345,0,Red);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
- }
- else Print("Error opening SELL order : ",GetLastError());
- return(0);
- }
- return(0);
- }
- for(cnt=0;cnt<total;cnt++)
- {
- OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
- if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
- {
- if(OrderType()==OP_BUY) // long position is opened
- {
- // should it be closed?
- if(isCrossed_1 == 2)
- {
- OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
- return(0); // exit
- }
-
- // should it be closed?
- if(isCrossed_1 == 1)
- {
- OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
- return(0); // exit
- }
- }
- }
- }
- return(0);
- }
- //+------------------------------------------------------------------+
-