簡體   English   中英

我在我的代碼中對orderend和orderclose中的mql4有一些疑問

[英]i have some question about mql4 in ordersend and orderclose in my code

我在外匯交易機器人上使用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;
}

在此處輸入圖片說明

我編輯了您的代碼。 您代碼中的主要問題是止盈! 在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM