【问题标题】:i have some question about mql4 in ordersend and orderclose in my code我对我的代码中的 ordersend 和 orderclose 中的 mql4 有一些疑问
【发布时间】:2019-03-29 09:48:01
【问题描述】:

我有一个外汇机器人的 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;
}

enter image description here

【问题讨论】:

    标签: mql4 metatrader4 forex


    【解决方案1】:

    我编辑了您的代码。您代码中的主要问题是获利! 在您编写的 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;
    }
    

    【讨论】:

    • 感谢您的回答,但如果您在元交易者 4 的策略测试器中运行,您可以看到何时打开新仓位会立即止损
    • 欢迎您。是的,它得到了 StopLoss,因为 StopLoss 的值不是零。但是 TakeProfit 将是 Ask 或 Bid 因为 Ask/Bid + Takeprofit(零)*Point = Ask/Bid + 0 = Ask/Bid
    • 是的,我理解这个错误,但是如果你在 strategy tester 中运行,我们在获取位置时会出现另一个错误,知道它
    • 请运行它并在此处写入错误代码。我测试了,没有报错
    • 我已经在我的代码中编辑了这个问题,并在问题下的问题中添加了照片。
    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多