新人对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论 - 页 193

 

我需要帮助--我有跟踪止损--3个订单,但视频教程中的跟踪例子:)只适用于未连接的订单,在我的案例中,它们是通过算法连接的,即止盈是通过三个订单的总和计算的,如果我进入了一个趋势,则是一个。好吧,我已经想到了一些东西,但我的专家顾问并没有开始赚取更多的钱--它已经开始赚取相同数量的订单,但与我以前的没有跟踪的EA相比,我得到的交易量要少2-3倍。因此,我的追踪不是追踪,而是一些胡言乱语。我应该如何把它放在链接的订单上? 谁能给我一个主意?我恐怕想不出别的办法了。 这是我 "跟踪 "的结果--它以蓝色显示。

附上1个测试的结果。那里到底有没有尾随止损?我不明白。为什么同一时期的利润没有增加?只有交易的数量 减少了2-3倍?

//+------------------------------------------------------------------+
//|                                     BLACKJACK&HOOKERS TrailX.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double  Lots           = 0.01;
extern int     TakeProfit     = 5;
extern int     Step           = 2;

extern int     TrailingStep   = 3;
extern int     TrailingStop   = 1;

extern int     MaPeriod       = 200;
extern int     MaShift        = 1;

extern int     Magic          = 123;
extern int     Slippage       = 5;

int timeprev=0;

extern double price,op,cn,tp;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits == 3 || Digits == 5)
      TakeProfit     *= 10;
      Step           *= 10;
      TrailingStep   *= 10;
      TrailingStop   *= 10;
      Slippage       *= 10;

      
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{  if (timeprev == Time[0]) return;
   timeprev = Time[0];
   double maprice=iMA(Symbol(),0,MaPeriod,MaShift,MODE_SMA,PRICE_CLOSE,1);
   op=CalculateProfit();
   cn=CountTrades();
   tp=TakeProfit;
   if(tp>TakeProfit)
   {
      TakeProfit+=tp;
   }

   
 

 
   if (cn==0 && CountBuy() + CountSell() == 0 && Ask > maprice)
   {
      if (OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", Magic, 0, Blue)<0)
            Print("Не удалось открыть ордер на покупку");
            
   }
   
   if (cn==0 && CountBuy() + CountSell() == 0 && Bid < maprice)
   {
      if (OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", Magic, 0, Red)<0)
            Print("Не удалось открыть ордер на продажу");
            
   }
   

   
   if(cn==1 && CountBuy()==1)//CountBuy()==1 && 
   {
      price=FindLastBuyPrice();
      if((price-Ask)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
            Print("Не удалось открыть ордер на продажу");
      }
   }
   else if(cn==1 && CountSell()==1)//CountSell()==1 && 
   {
      price=FindLastSellPrice();
      if((Bid-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
            Print("Не удалось открыть ордер на покупку");
      }
   }
   
   
   
   if(cn==2 && CountBuy()==1 && CountSell()==1)//CountBuy() + CountSell()==2 && 
   {
      price=FindLastSellPrice();
      if((price-Bid)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
               Print("Не удалось открыть ордер на продажу");
               
      }
      else 
      price=FindLastSellPrice();
      if((Bid-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
            Print("Не удалось открыть ордер на покупку");
           
      }
   }
   else if(cn==2 && CountSell()==1 && CountBuy()==1)//CountSell() + CountBuy()==2 && 
   {
      price=FindLastBuyPrice();
      if((Ask-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
               Print("Не удалось открыть ордер на продажу");
               
      }
      else 
      price=FindLastBuyPrice();
      if((price-Ask)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
            Print("Не удалось открыть ордер на покупку");
           
      }
   }
   
   Trailing();

   

   if (op>=tp)
   {
         CloseAll();    
   }
   
   

 
   
   
   
   
   
   
   






}    
//---------------------------------------------------------------------------------------
//   double Equity() //или OrderProfit() - эти идеи в работе и задуманы как автоподбор лота (без мартингейла) для снижения рисков от контрдвижения (интервенций) рынка 
//   {
//      double eqv=0;
//      eqv+=AccountEquity();
//      return(eqv);     
//   }
//---------------------------------------------------------------------------------------
void Trailing()

{
   for (int i=OrdersTotal() -1; i>=0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         {
            if (OrderType() == OP_BUY)
            
            {
               price=FindLastBuyPrice();   
               if (Bid - price > (TakeProfit+TrailingStep)*Point)
               {
                  {
                     tp=NormalizeDouble((TakeProfit+TrailingStep)*Point, Digits);
                  }
               }
               if (price - (TakeProfit-TrailingStop)*Point < Bid - (TakeProfit+TrailingStep)*Point)
               {
                  {
                     tp=NormalizeDouble((TakeProfit-TrailingStop)*Point, Digits);
                  }
               }
            }
            
            if (OrderType() == OP_SELL)
            {
               price=FindLastSellPrice();
               if (price - Ask > (TakeProfit+TrailingStep)*Point)
               {  
                  {
                     tp=NormalizeDouble((TakeProfit+TrailingStep)*Point, Digits);
                  }
               }
               if (price - (TakeProfit-TrailingStop)*Point > Ask + (TakeProfit+TrailingStep)*Point)
               {  
                  {
                     tp=NormalizeDouble((TakeProfit-TrailingStop)*Point, Digits);
                  }
               }
            }
         }
      }
   }
}
//---------------------------------------------------------------------------------------
double CalculateProfit()
  {
   double oprofit=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               oprofit+=OrderProfit();
              }
           }
        }
     }
   return(oprofit);
  }
//--------------------------------------------------------------------------------------
void CloseAll()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage))
                  Print("Не удалось закрыть ордер на покупку");
              }
            if(OrderType()==OP_SELL)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage))
                  Print("Не удалось закрыть ордер на продажу");
              }
           }
        }
     }
  }
//---------------------------------------------------------------------------------------------------
double FindLastBuyPrice()
  {
   int oldticket,ticket= 0;
   double oldopenprice = 0;
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
           {
            oldticket=OrderTicket();
            if(oldticket>ticket)
              {
               ticket=oldticket;
               oldopenprice=OrderOpenPrice();
              }
           }
        }
     }
   return(oldopenprice);
  }
//---------------------------------------------------------------------------------------------------
double FindLastSellPrice()
  {
   int oldticket,ticket= 0;
   double oldopenprice = 0;
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
           {
            oldticket=OrderTicket();
            if(oldticket>ticket)
              {
               ticket=oldticket;
               oldopenprice=OrderOpenPrice();
              }
           }
        }
     }
   return(oldopenprice);
  }
//----------------------------------------------------------------------------------------------
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//----------------------------------------------------------------------------------------------
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+---------------------------------------------------------------------------------+
int CountTrades()
{
    int count=0;
    for (int i=OrdersTotal()-1; i>=0; i--)
      {  
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                  count++; 
         }
      }
      return(count);
}
//----------------------------------------------------------------------------------+
int FindLastOrderType()
{
      for(int i = OrdersTotal()-1; i>=0; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            return(OrderType());
         } 
      }
      return(-1);
}
//----------------------------------------------------------------------------------+



Автоматический трейдинг и тестирование торговых стратегий
Автоматический трейдинг и тестирование торговых стратегий
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
附加的文件:
 
geratdc:

我需要帮助--我有止损翻转--3个订单,但跟踪的例子只针对不相关的订单,在我的案例中,它们是由一个算法联系起来的,即止盈是基于三个订单的总和,如果你处于趋势中,则是一个订单。好吧,我已经想到了一些东西,但我的专家顾问并没有开始赚取更多的钱--它已经开始赚取相同数量的订单,但与我以前的没有跟踪的EA相比,我得到的交易量要少2-3倍。因此,我的追踪不是追踪,而是一些胡言乱语。我应该如何把它放在链接的订单上? 谁能给我一个主意?恐怕我想不出别的办法,这是我 "拖后腿 "的结果--用红色标出。


你认为有人会把你的代码当作潦草的东西来理解吗?
 
Vitaly Muzichenko:
你认为有人会理解你潦草的代码吗?

应该如何展示?好吧,我不能抛出一段代码,否则就不清楚什么是什么了。
 
geratdc:

你如何展示它?好吧,我不能抛出一段代码,否则就不清楚什么是什么了。

至少像这样。


 
Vitaly Muzichenko:

至少情况是这样的。



有人重做了它。这当然更好,谢谢你。
 


朋友们,我正在学习MQL4,并决定为Lazy Trader系统写一个EA
EA已经写好了,但它不能正常工作。我看到的错误是,订单没有按时间关闭。可能是有一些其他的错误。


请帮助我找到这些错误,并对你看到的代码进行反馈。

以下是我编写的专家顾问系统的规则:

1/ 在本周初,等待第一根四小时蜡烛收盘。
2/ 我们将买入止损和卖出止损订单放在该蜡烛图的高点以上20点和低点以下20点。
3/ 止损应设置在相反订单的水平。
4/当订单达到利润,等于止损时,我们将止损转移到Breakeven(我们将头寸的止损转移到其开盘价)。
5/ 激活第一个订单后的第二个订单,请勿删除。
6/ 在周末,我们删除/关闭所有订单,无论其盈利/亏损。

谢谢你的帮助:)

我附上了一个带有EA代码的文件

附加的文件:
 
ivan-baaton:


朋友们,我正在学习MQL4,并决定为Lazy Trader系统写一个EA
EA已经写好了,但它不能正常工作。我看到的错误是,订单没有按时间关闭。可能是有一些其他的错误。


请帮助我找到这些错误,并对你看到的代码进行反馈。

以下是我编写的专家顾问系统的规则:

1/ 在本周初,等待第一根四小时蜡烛收盘。
2/ 我们将买入止损和卖出止损订单放在该蜡烛图的高点以上20点和低点以下20点。
3/ 止损应设置在相反订单的水平。
4/当订单达到利润,等于止损时,我们将止损转移到Breakeven(我们将头寸的止损转移到其开盘价)。
5/ 激活第一个订单后的第二个订单,请勿删除。
6/ 在周末,我们删除/关闭所有订单,无论其盈利/亏损。

谢谢你的帮助:)

我把顾问的代码附在文件上。

TimeHour(TimeCurrent()) == DayTimeClose

这样的绝对平等可能是非常罕见的,如果你在23点结束本周的交易,最好提前一点结束订单,至少15分钟。我没有寻找其他错误。乍一看,似乎写得很准确。

 
Alexey Viktorov:

这种绝对的平等可能是非常罕见的,如果你在本周以23点收盘,你最好提前一点收盘,至少要提前15分钟。我没有寻找其他错误。乍一看,它似乎写得很整齐。

本周的收盘时间是00:00,也就是说,我提前1小时关闭订单。


这段代码有什么问题:TimeHour(TimeCurrent())== DayTimeClose ?


我认为在这段代码的某个地方有一个错误。


for (int i = OrdersTotal(); i>=0; i--)
{
如果(OrderSelect(k, SELECT_BY_POS, MODE_TRADES) == true && TimeHour(TimeCurrent())== DayTimeClose && DayOfWeek() == 5 && (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) && OrderMagicNumber() == Magic)
{
ticket_sell = OrderClose(ticket_sell, Lots, Bid, 3, Red)。
如果(ticket_sell == false)
Print("Sell order not closed");
}

}


谢谢你

 
ivan-baaton:

本周的收盘时间是00:00,也就是说,我提前1小时关闭订单。


这段代码有什么问题:TimeHour(TimeCurrent())== DayTimeClose ?


我认为在这段代码的某个地方有一个错误。


for(int i= OrdersTotal(); i>=0; i--)
{
如果(OrderSelect(k, SELECT_BY_POS, MODE_TRADES) == true &&TimeHour(TimeCurrent())== DayTimeClose&& DayOfWeek() == 5 && (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) && OrderMagicNumber() == Magic)
{
ticket_sell = OrderClose(ticket_sell, Lots, Bid, 3, Red)。
如果(ticket_sell == false)
Print("Sell order not closed");
}

}


谢谢

这正是我所指出的。虽然我现在发现我当时没有注意到。我只看到TimeHour(TimeCurrent()) 被强调了。==DayTimeClose。

但事实上,循环计数器是 i,而顺序k被选中,我没有马上看到。

 
Alexey Viktorov:

这正是我所指出的。虽然我现在发现我当时没有注意到。我只看到突出显示了TimeHour(TimeCurrent())==DayTimeClose。

但事实上,循环计数器是 i,而顺序k被选中,我没有马上看到。


是的,这个K也写在上头的外部变量 中......也许是某个函数的返回值......


阿列克谢,你对我的问题能说些什么?我尝试在反向订单上附加追踪止损。你是否遇到过这样的问题?