У меня есть некоторые вопросы о mql4 в порядке отправки и закрытия в моем коде - PullRequest
0 голосов
/ 25 октября 2018

У меня есть небольшой эксперт с 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;
}

введите описание изображения здесь

1 Ответ

0 голосов
/ 25 октября 2018

Я отредактировал твой код.Основная проблема в вашем коде - это прибыль!В функциях GetBuy () и GetSell () вы написали:

Ask+(TakeProfit*Point)

Возвращает Ask!потому что ваш TakeProfit был установлен на ноль.Если вы не хотите устанавливать 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;
}
...