mt4+ea外汇交易测试脚本

//+------------------------------------------------------------------+
//|                                                          buy.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
  

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int n = MathRand() + Seconds();
   int i = MathMod(n, 2);
   Print(i,"=",n," mod 2");
   //Alert(i,"=",n," mod 2");
   
   //--- get minimum stop level
   //double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   //Print("Minimum Stop Level=",minstoplevel," points");
//--- calculated SL and TP prices must be normalized
   //double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits);
   //double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits);
   
   int TrailingStop = 200;
   int ticket;
   if( i == 1)
   {
      
      double price=Ask;
      double closeprice=Bid;
      double stoploss=price-TrailingStop * Point;
      double takeprofit=price+TrailingStop * Point;
      int director=OP_BUY;
      Print("director=",director," price=",price," stoploss= ",stoploss," takeprofit=",takeprofit);
      
      ticket=OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(price,Digits),3,0,0,"bet20161115");
      if(ticket==-1)
      {
         Print("Error in OrderSend. Error code=", GetLastError());
      }
      else
      {
         OrderSelect(ticket,SELECT_BY_TICKET);
         bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(price-Point*TrailingStop,Digits),NormalizeDouble(price+Point*TrailingStop,Digits),0,Blue);
         if(!res)
            Print("Error in OrderModify. Error code=",GetLastError());
      }
   }
   else
   {
      double price=Bid;
      double closeprice=Ask;
      double stoploss=price+TrailingStop * Point;
      double takeprofit=price-TrailingStop * Point;
      int director=OP_SELL;
      Print("director=",director," price=",price," stoploss= ",stoploss," takeprofit=",takeprofit);
      ticket=OrderSend(Symbol(),OP_SELL,0.1,NormalizeDouble(price,Digits),3,0,0,"bet20161115");
      if(ticket==-1)
      {
         Print("Error in OrderSend. Error code=", GetLastError());
      }
      else
      {
         OrderSelect(ticket,SELECT_BY_TICKET);
         bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(price+Point*TrailingStop,Digits),NormalizeDouble(price-Point*TrailingStop,Digits),0,Blue);
         if(!res)
            Print("Error in OrderModify. Error code=",GetLastError());
      }
   }
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int total;
   total=OrdersTotal();
   //Print("total=", total);
   if(total<1)
     {
         CheckForOpen();    
     }
   
  }
//+------------------------------------------------------------------+