Auto_EA_381

0 Views
0 Downloads
0 Favorites
Auto_EA_381
//+------------------------------------------------------------------+

//|                                                      MACD_EA.mq4 |

//|                                                  Masato Miyaichi |

//|                                                                  |

//+------------------------------------------------------------------+

#property copyright "Masato Miyaichi"

#property link      ""

#property version   "1.00"

#property strict



int handle=NULL;

input string LogFileHeader ="MACD_EA_";

input double TakeProfit    =5000;

input double StopLoss      =1500;

input double Lots          =0.05;

input double TrailingStop  =50;

input double MACDOpenLevel =3;

input double MACDCloseLevel=2;

input int    MATrendPeriod =32;



string LogFile;

int    today;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- create timer

   LogFile=StringConcatenate(LogFileHeader,IntegerToString(Year()*10000+Month()*100+Day()),".txt");

   handle=FileOpen(LogFile,FILE_TXT|FILE_READ|FILE_WRITE);

   today = Day();

   FileWrite(handle,"started");

   printf("LogFile : "+LogFile);



   FileFlush(handle);



   EventSetTimer(10);



//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//--- destroy timer

   EventKillTimer();



  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//|                                                  MACD Sample.mq4 |

//|                   Copyright 2005-2014, MetaQuotes Software Corp. |

//|                                              http://www.mql4.com |

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OnTick(void)

  {

   double MacdCurrent,MacdPrevious;

   double SignalCurrent,SignalPrevious;

   double MaCurrent,MaPrevious;

   int    cnt,ticket,total;



// FileWrite(handle,"OnTick Time:" + IntegerToString(Hour() * 10000 + Minute()* 100 + Seconds()));   

// FileFlush(handle);

//---

// initial data checks

// it is important to make sure that the expert works with a normal

// chart and the user did not make any mistakes setting external 

// variables (Lots, StopLoss, TakeProfit, 

// TrailingStop) in our case, we check TakeProfit

// on a chart of less than 100 bars

//---

   if(Bars<100)

     {

      Print("bars less than 100");

      return;

     }

   if(TakeProfit<10)

     {

      Print("TakeProfit less than 10");

      return;

     }

//--- to simplify the coding and speed up access data are put into internal variables

   MacdCurrent=iMACD(NULL,0,15,60,5,PRICE_CLOSE,MODE_MAIN,0);

   MacdPrevious=iMACD(NULL,0,15,60,5,PRICE_CLOSE,MODE_MAIN,1);

   SignalCurrent=iMACD(NULL,0,15,60,5,PRICE_CLOSE,MODE_SIGNAL,0);

   SignalPrevious=iMACD(NULL,0,15,60,5,PRICE_CLOSE,MODE_SIGNAL,1);

   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);

   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);



   total=OrdersTotal();



   if(total<1)

     {

      //--- no opened orders identified

      if(AccountFreeMargin()<(1000*Lots))

        {

         Print("We have no money. Free Margin = ",AccountFreeMargin());

         return;

        }

      //--- check for long position (BUY) possibility

      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && 

         MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)

        {

         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"macd sample",16384,0,Green);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

              {

               Print("BUY order opened : ",OrderOpenPrice());

               FileWrite(handle,StringConcatenate(DoubleToString(MacdCurrent,5),",",DoubleToString(MacdPrevious,5),"'",DoubleToString(SignalCurrent,5),",",DoubleToString(SignalPrevious,5),",",DoubleToString(MaCurrent,5),",",DoubleToString(MaPrevious,5),",",IntegerToString(ticket),",",IntegerToString(total),",",OrderOpenPrice())); //,",",OrderOpenPrice()))));

              }

           }

         else

            Print("Error opening BUY order : ",GetLastError());

         return;

        }

      //--- check for short position (SELL) possibility

      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 

         MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)

        {

         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"macd sample",16384,0,Red);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

              {

               Print("SELL order opened : ",OrderOpenPrice());

               FileWrite(handle,StringConcatenate(DoubleToString(MacdCurrent,5),",",DoubleToString(MacdPrevious,5),"'",DoubleToString(SignalCurrent,5),",",DoubleToString(SignalPrevious,5),",",DoubleToString(MaCurrent,5),",",DoubleToString(MaPrevious,5),",",IntegerToString(ticket),",",IntegerToString(total),",",OrderOpenPrice())); //,",",OrderOpenPrice()))));

              }

           }

         else

            Print("Error opening SELL order : ",GetLastError());

        }

      //--- exit from the "no opened orders" block

      return;

     }

//--- it is important to enter the market correctly, but it is more important to exit it correctly...   

   for(cnt=0;cnt<total;cnt++)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         continue;

      if(OrderType()<=OP_SELL &&   // check for opened position 

         OrderSymbol()==Symbol())  // check for symbol

        {

         //--- long position is opened

         if(OrderType()==OP_BUY)

           {

            //--- should it be closed?



            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 

               MacdCurrent>(MACDCloseLevel*Point))

              {

               //--- close order and exit

               //  if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))

               //  {

               //     Print("OrderClose error ",GetLastError());

               //  }

               FileWrite(handle,StringConcatenate(DoubleToString(MacdCurrent,5),",",DoubleToString(MacdPrevious,5),"'",DoubleToString(SignalCurrent,5),",",DoubleToString(SignalPrevious,5),",",DoubleToString(MaCurrent,5),",",DoubleToString(MaPrevious,5),",",IntegerToString(ticket),",",IntegerToString(total))); //,",",OrderOpenPri))));

               return;

              }



            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if(Bid-OrderOpenPrice()>Point*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-Point*TrailingStop)

                    {

                     //--- modify order and exit

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))

                        Print("OrderModify error ",GetLastError());

                     return;

                    }

                  FileWrite(handle,StringConcatenate(DoubleToString(MacdCurrent,5),",",DoubleToString(MacdPrevious,5),"'",DoubleToString(SignalCurrent,5),",",DoubleToString(SignalPrevious,5),",",DoubleToString(MaCurrent,5),",",DoubleToString(MaPrevious,5),",",IntegerToString(ticket),",",IntegerToString(total),",","Modify")); //,",",OrderOpenPrice()))));



                 }

              }

           }

         else // go to short position

           {

            //--- should it be closed?

            if(MacdCurrent<0 && MacdCurrent>SignalCurrent && 

               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))

              {

               //--- close order and exit

               //  if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))

               //     Print("OrderClose error ",GetLastError());

               return;

              }

            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     //--- modify order and exit

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))

                        Print("OrderModify error ",GetLastError());

                     return;

                    }

                  FileWrite(handle,StringConcatenate(DoubleToString(MacdCurrent,5),",",DoubleToString(MacdPrevious,5),"'",DoubleToString(SignalCurrent,5),",",DoubleToString(SignalPrevious,5),",",DoubleToString(MaCurrent,5),",",DoubleToString(MaPrevious,5),",",IntegerToString(ticket),",",IntegerToString(total),",","Modify")); //,",",OrderOpenPrice()))));



                 }

              }

           }

        }

     }

//---

  }

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

//| Timer function                                                   |

//+------------------------------------------------------------------+

void OnTimer()

  {

//---

   if(today==Day())

     {

      FileWrite(handle,"Time:"+IntegerToString(Hour()*10000+Minute() *100+Seconds()));

      FileFlush(handle);



     }

   else

     {

      if(handle!=0)

        {

         FileClose(handle);

        }

      LogFile=StringConcatenate(LogFileHeader,IntegerToString(Year()*10000+Month()*100+Day()),".txt");

      handle=FileOpen(LogFile,FILE_TXT|FILE_READ|FILE_WRITE);

      today = Day();

      FileWrite(handle,"started :"+IntegerToString(Day()));

      printf("LogFile : "+LogFile);



      FileFlush(handle);



     }

  }

//+------------------------------------------------------------------+

//| Tester function                                                  |

//+------------------------------------------------------------------+

double OnTester()

  {

//---

   double ret=0.0;

//---



//---

   return(ret);

  }

//+------------------------------------------------------------------+

//| ChartEvent function                                              |

//+------------------------------------------------------------------+

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---



  }

//+------------------------------------------------------------------+

Comments