首页 > 解决方案 > 我对我的代码中的 ordersend 和 orderclose 中的 mql4 有一些疑问

问题描述

我有一个用于外汇机器人的 mql4 小专家,但是在 metatrader 4 中运行此代码进行回测时,我在获取代码时遇到了一些问题,我的代码详细信息是:我有 2 个 ema,当交叉时买入,当交叉时卖出但在回测中超过 2 EMA 后获得位置的问题。我的止损固定为 10 点,但 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;
}

在此处输入图像描述

标签: mql4metatrader4forex

解决方案


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

Ask+(TakeProfit*Point)

它返回询问!因为您的止盈已设置为零。如果你不想设置止盈你应该写:

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;
}

推荐阅读