我在我的代码中对orderend和orderclose中的mql4有一些疑问

时间:2018-10-24 21:27:16

标签: mql4 metatrader4 forex

我有一个外汇交易机器人的mql4专家 但是我在运行此代码以在MetaTrader 4中进行回测时在获取代码时遇到了一些问题 我的代码详细信息是: 我有2个ema,何时交叉获得买入,何时向下交叉获得卖出 但在回测中损坏2 ema后获得位置的问题。 我的止损定为10 pip,但tp为0,我们有开放交易直到2 ema的下一个交叉,然后关闭pervios头寸并获得新头寸。 我添加了测试立体声,并显示了我在定位上的问题

#property copyright "Copyright 2018"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict

input int Ema_Fast_Period = 62;
input int Ema_Slow_Period = 30;

input int MagicNumber = 1982;
input double Lots = 0.01;
input double StopLoss = 100;
input double TakeProfit = 0;

double FastMACurrent ,SlowMACurrent ,FastMAPrevious ,SlowMAPrevious;

bool BuyCondition = False, SellCondition = False, CrossPriseWithFastMAUpShado = False, CrossPriseWithFastMADownShado = False;
//---
int Slippage=5;

double OpenPosition = 0;

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//|   expert OnTick function                                         |
//+------------------------------------------------------------------+
void OnTick()
  {
      if(Volume[0]<=1)
      {
         FastMACurrent = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Fast_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,1 );
         SlowMACurrent = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Slow_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,1 );
         FastMAPrevious = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Fast_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,2 );
         SlowMAPrevious = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Slow_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,2 ); 
      //----------------------- BUY CONDITION   
         BuyCondition = (FastMAPrevious<SlowMAPrevious && FastMACurrent>SlowMACurrent);      
      //----------------------- SELL CONDITION   
         SellCondition = (FastMAPrevious>SlowMAPrevious && FastMACurrent<SlowMACurrent);

         CrossPriseWithFastMADownShado = ( Low[1]<FastMACurrent && FastMACurrent<Open[1] );
         if( BuyCondition )
         {
            //If we have open trade before get another trade close perivios trade and save money
            if( OrderSelect(0, SELECT_BY_POS,MODE_TRADES) )
            {
               int a = OrderClose( OrderTicket(),OrderLots(),OrderClosePrice(), Slippage, clrWhite );
            }
            BuyCondition = False;
            GetBuy();
         }
         if( SellCondition )
         {
            //If we have open trade before get another trade close perivios trade and save money
            if( OrderSelect(0, SELECT_BY_POS,MODE_TRADES) )
            {
               int a = OrderClose( OrderTicket(),OrderLots(),OrderClosePrice(), Slippage, clrWhite );
            }
            SellCondition = False;
            GetSell();
         }
      }
 }
//+------------------------------------------------------------------+
//|   expert Buy Or Sell function                                    |
//+------------------------------------------------------------------+
int GetBuy(){
   int getposition = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-(StopLoss*Point),0,"Buy",MagicNumber,0,Blue);
   return True;
}
int GetSell(){
   int getposition = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+(StopLoss*Point),0,"Sell",MagicNumber,0,Red);
   return True;
}

enter image description here

1 个答案:

答案 0 :(得分:1)

我编辑了您的代码。您代码中的主要问题是止盈! 在GetBuy()和GetSell()函数中,您编写了:

Ask+(TakeProfit*Point)

它返回询问!因为您的TakeProfit已设置为零。如果您不想设置止盈,请输入:

int ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-(StopLoss*Point),0,"Buy",MagicNumber,0,Blue);

这是新代码:

#property copyright "Copyright 2018"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict

input int Ema_Fast_Period = 62;
input int Ema_Slow_Period = 30;

input int MagicNumber = 1982;
input double Lots = 0.01;
input int StopLoss = 100;
input int TakeProfit = 1000;

double FastMACurrent ,SlowMACurrent ,FastMAPrevious ,SlowMAPrevious;

bool BuyCondition = False, SellCondition = False, CrossPriseWithFastMAUpShado =     False, CrossPriseWithFastMADownShado = False;
//---
int Slippage=5;

double OpenPosition = 0;

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//|   expert OnTick function                                         |
//+------------------------------------------------------------------+
void OnTick()
  {
  if(Volume[0]<=1)
  {
     FastMACurrent = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Fast_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,1 );
     SlowMACurrent = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Slow_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,1 );
     FastMAPrevious = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Fast_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,2 );
     SlowMAPrevious = iMA(Symbol() ,PERIOD_CURRENT ,Ema_Slow_Period ,0 ,MODE_EMA ,PRICE_CLOSE ,2 ); 
  //----------------------- BUY CONDITION   
     BuyCondition = (FastMAPrevious<SlowMAPrevious && FastMACurrent>SlowMACurrent);      
  //----------------------- SELL CONDITION   
     SellCondition = (FastMAPrevious>SlowMAPrevious && FastMACurrent<SlowMACurrent);

     CrossPriseWithFastMADownShado = ( Low[1]<FastMACurrent && FastMACurrent<Open[1]         );

     if( BuyCondition )
     {
        //If we have open trade before get another trade close perivios trade and save money
        if( OrderSelect(0, SELECT_BY_POS,MODE_TRADES) )
        {
           int a = OrderClose( OrderTicket(),OrderLots(),OrderType()==OP_SELL ? Ask : Bid, Slippage, clrWhite );
        }
        if(GetBuy()) BuyCondition = False;

     }
     if( SellCondition )
     {
        //If we have open trade before get another trade close perivios trade and     save money
        if( OrderSelect(0, SELECT_BY_POS,MODE_TRADES) )
        {
           int a = OrderClose( OrderTicket(),OrderLots(),OrderType()==OP_BUY ? Bid : Ask, Slippage, clrWhite );
        }
        if(GetSell()) SellCondition = False;
     }
  }
 }
//+------------------------------------------------------------------+
//|   expert Buy Or Sell function                                    |
//+------------------------------------------------------------------+
    bool GetBuy(){
   int ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-(StopLoss*Point),Ask+    (TakeProfit*Point),"Buy",MagicNumber,0,Blue);
   if(ticket > 0) return true;
   return false;
}
bool GetSell(){
   int ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+(StopLoss*Point),Bid-        (TakeProfit*Point),"Sell",MagicNumber,0,Red);
   if(ticket > 0) return true;
   return false;
}