未结订单总量的问题 - 页 4

 

大家好

对不起,我说得太早了。 虽然添加RefreshRates()似乎有所改变,但我仍然有同样的问题。 它有时会打开两个挂单,有时只打开其中一个,有时两个都不打开。 当它不打开一个订单或两个都不打开时,我仍然得到错误130,但当两个都实际打开时没有错误。我还注意到,在那些我的输入低于MODE_STOPLEVEL的货币对上,它从来没有打开一个订单,而且我总是出现错误130,尽管程序按照要求调整了我的输入。 我正在打印这些值,它们被调整为预期值。 所以我试图弄清楚为什么我的OrderSend没有真正发挥作用。

在欧元兑美元这样的货币对上,如果止损位是5,它通常会发送两个订单,但并不总是如此。 然而,在欧元兑美元这样的货币对上,如果止损位是10,它从未发送过订单。

extern int TrailingStart=20;
extern int TrailingStop=5;

extern int Hedge=10;
extern double Multiplier=3;

extern int StopLossOriginal=11;

extern int StopLossHedge=9;

extern double Percentage=1;
extern double Lotsize=0.01;
extern double MyMaxlots=30;

extern datetime StartTime1 = D'2016.03.25 16:50';

extern int Pipmove=5;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
//---
  {
   int TS=TrailingStart-TrailingStop;
   double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
   if(StopLossOriginal<stoplevel || StopLossHedge<stoplevel || TS<stoplevel)
     {
      MessageBox("Please note: Your inputs for StopLossOriginal, StopLossHedge and/or"+
                 "\nTrailingStop are below the minimum levels required by your broker,"+
                 "\nand have been increased automatically to "+StringConcatenate(stoplevel)+" Pips");
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int TS=TrailingStart-TrailingStop;
   Print("TS = ",TS);
   int sloss=StopLossOriginal-Pipmove;
   int stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
   Print("stoplevel = ",stoplevel);
     
   double Lots = NormalizeDouble(AccountEquity()*Percentage*Lotsize/100, 2),
          point=Point*10,
          Price=Pipmove*point,
          SL=(StopLossOriginal-Pipmove)*point,
          MinLots = MarketInfo(Symbol(),MODE_MINLOT),
          MaxLots = MarketInfo(Symbol(),MODE_MAXLOT),
          HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2);
          
   if(sloss<=stoplevel) SL=stoplevel*point;
   Print("SL = ",SL);
   if(StopLossHedge<=stoplevel) StopLossHedge=stoplevel;
   Print("StopLossHedge = ",StopLossHedge);
   if(TS<=stoplevel) TrailingStart=(stoplevel+TrailingStop); 
   Print("TrailingStart = ",TrailingStart);     
          
   datetime time1=StartTime1-3300;      

   if(Lots>MaxLots) Lots=MaxLots;
   if(Lots<MinLots) Lots=MinLots;
   if(HedgeLots>MaxLots) HedgeLots=MaxLots;
   if(Lots>MaxLots || Lots<MinLots || HedgeLots>MaxLots)
     {
      MessageBox("Lotsize have been adjusted automatically");
     }

   int buy_ticket=0, sell_ticket=0, buystop_ticket=0, sellstop_ticket=0, total=0;
   for(int i= OrdersTotal()-1; i>= 0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
        {
         total++;
         if(OrderType()==OP_BUYSTOP) buystop_ticket=OrderTicket();
         if(OrderType()==OP_SELLSTOP) sellstop_ticket=OrderTicket();
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }

   if(total==0 && Time[0]==time1)
     {
      buy_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,0,0,"Pending",magic,0,Lime);
      OrderModify(OrderTicket(),OrderOpenPrice(),Ask-SL,OrderTakeProfit(),Yellow);
      Print("Buystop = ",GetLastError());
      Sleep(1000);
      RefreshRates();
      Sleep(1000);
      sell_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,0,0,"Pending",magic,0,Red);
      OrderModify(OrderTicket(),OrderOpenPrice(),Bid+SL,OrderTakeProfit(),Yellow);
      Print("Sellstop = ",GetLastError());
     }

我也试过这样做,但没有区别。

buy_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      RefreshRates();
sell_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);

即使我这样做,也没有什么区别。

if(total==0) // && (Time[0]==time1)
 

谢谢大家的帮助,我终于成功了。 我唯一能让它持续工作的方法是把它改成这样。

 if(total==0)
     {
      buystop_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      Print("buystop = ",GetLastError());
      RefreshRates();
     }
   if(total==1)
     {
      sellstop_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);
      Print("sellstop = ",GetLastError());
     }

我还发现,在挂单 被激活之前,Pipmove级别也必须高于stoplevel。 所以现在一切似乎都在工作... 谢谢

 
Trader3000:

谢谢大家的帮助,我终于成功了。 我唯一能让它持续工作的方法是把它改成这样。

我还发现,在挂单被激活之前,Pipmove级别也必须高于stoplevel。 所以现在一切似乎都在工作... 谢谢

不,这也不行,因为现在只要有一笔交易,它就会继续打开卖出止损。
 
简单的答案是不要试图在离当前价格 这么近的地方开挂单。5点通常是半个点
 
GumRai:
简单的答案是不要试图在离当前价格这么近的地方开挂单。5点通常是半个点

谢谢你的回答,我的计算实际上是以点为单位的,所以挂单至少是50点(离当前价格 5点),然而,如果我把它移到离止损点至少1点的地方,也就是欧元兑美元的50点,似乎现在除了我把它拖到图表上的第一笔交易,其他的交易都打开了。 但我现在对这个没有意见。 我的代码现在看起来是这样的。

extern int TrailingStart=20;
extern int TrailingStop=5;
extern int Hedge=10;
extern double Multiplier=3;
extern int StopLossOriginal=11;
extern int StopLossHedge=9;
extern double Percentage=1;
extern double Lotsize=0.01;
extern double MyMaxlots=30;
extern datetime StartTime1 = D'2016.03.25 14:50';
extern int Pipmove=5;
int i,TS=TrailingStart-TrailingStop,stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10; //stoplevel has been converted from points to pips (/10)

int start()
  {
   double Lots = NormalizeDouble(AccountEquity()*Percentage*Lotsize/100, 2),
          point=Point*10,
          Price=Pipmove*point,
          SL=StopLossOriginal*point,
          MinLots = MarketInfo(Symbol(),MODE_MINLOT),
          MaxLots = MarketInfo(Symbol(),MODE_MAXLOT),
          HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2);

   if(StopLossHedge<stoplevel) StopLossHedge=stoplevel;
   if(TS<stoplevel) TrailingStart=(stoplevel+TrailingStop);
   if(Pipmove<stoplevel+1) Pipmove=stoplevel+1;
   if(StopLossOriginal<=StopLossHedge) StopLossOriginal=StopLossHedge+1;

   datetime time1=StartTime1-300;

   int buy_ticket=0,sell_ticket=0,buystop_ticket=0,sellstop_ticket=0,total=0;
   for(i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
        {
         total++;
         if(OrderType()==OP_BUYSTOP) buystop_ticket=OrderTicket();
         if(OrderType()==OP_SELLSTOP) sellstop_ticket=OrderTicket();
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }

   if(total<1 && Time[0]==time1)
     {
      buystop_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      RefreshRates();
      sellstop_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);
     }

我遇到了一个单独但类似的问题。 一旦其中一个挂单被触发,就会发生两种情况。 它要么触发追踪止损,这时另一个挂单就会被删除,要么如果这个交易对我不利,它就应该打开一个相反方向的对冲。 根据我写代码的方式,它要么打开多个对冲,要么根本没有对冲。 我已经尝试了各种方法,包括以下两种。

if(OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType()==OP_BUY)
     {
      if(Bid-OrderOpenPrice()>TrailingStart*point)
        {
         if(OrderStopLoss()<Bid-TrailingStop*point)
           {
            if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*point,OrderTakeProfit(),Blue))
              {
               if(OrderSelect(sellstop_ticket,SELECT_BY_TICKET) && OrderType()==OP_SELLSTOP)
                 {
                  if(OrderDelete(sellstop_ticket,Orange))
                     return(0);
                 }
              }
           }
        }
      if(OrderOpenPrice()>Bid+Hedge*point && buy_ticket==1 && sell_ticket<=1)
            {
              sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Red);
            }  
        }
//Same for Sell

或者。


//Same up to here:
else if(total<=2 && OrderOpenPrice()>Bid+Hedge*point)
        {
         sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Red);
        }

我是否应该为此使用一个单独的for循环? 谢谢你

 

大家好

我已经尝试了几个星期,但仍然没有任何进展。 现在一切都正常,只是在某些情况下,EA在原始交易上开了不止一个对冲交易。 原始交易的SL是11点,对冲交易的SL是9点。有时对冲交易在9点时被止损,而原始交易仍然开放。 然后它将打开第二个对冲交易,甚至第三个和第四个,而原始交易仍然开放。 我只是想将对冲交易的数量限制在一个,如果它被止损,只是等待,看看原始交易会发生什么。

这就是我得到的结果类型。

576 2015.01.15 11:39 买入停止 29 0.48 1.16786 1.16616 0.00000 0.00 4834.24

577 2015.01.15 11:39 卖出止损 30 0.48 1.16642 1.16812 0.00000 0.00 4834.24

578 2015.01.15 11:39 卖出 30 0.48 1.16642 1.16812 0.00000 0.00 4834.24

579 2015.01.15 11:39 删除 29 0.48 1.16786 1.16616 0.00000 0.00 4834.24

580 2015.01.15 11:42 购买 31 1.44 1.16743 1.16653 0.00000 0.00 4834.24

581 2015.01.15 11:42 s/l 31 1.44 1.16653 1.16653 0.00000 -129.60 4704.64

582 2015.01.15 11:44 购买 32 1.44 1.16742 1.16652 0.00000 0.00 4704.64

583 2015.01.15 11:44 s/l 30 0.48 1.16812 1.16812 0.00000 -81.60 4623.04

584 2015.01.15 11:48 修改 32 1.44 1.16742 1.16893 0.00000 0.00 4623.04

买入止损卖出止损单(29和30)如期开仓。 价格随后下跌,卖出单(30)被填补,而买入止损单(29)被删除。 价格随后再次上涨,对冲(martingale)单(31)被触发(3*lotsize)。然后价格再次下跌,对冲(31)被止损,但由于30仍然开放,它触发了另一个对冲(32),等等。 如何防止订单32被触发? 谢谢你


 

大家好,我试图解决这个问题已经有一个多月了,我开始认为在程序上是不可能编码的。 所以,如果有人能确认这一点,我就可以把它搁置起来,继续前进。 是否不可能像上面的帖子中解释的那样,为对冲(martingale)订单的数量设置一个深度水平? 谢谢你。

到目前为止,我最好的办法是。

int start()
{
   int buy_ticket=0;
   int sell_ticket=0;
   int total=0;
   for(int i= OrdersTotal()-1; i>= 0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol())
     {
         total++;
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }
   /*if(total==1 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType()==OP_BUY) <------- this blocked out code is irrelevant, but I want to put it here for completeness
     {
      if(Bid-OrderOpenPrice()>TrailingStart*point)
        {
         if(OrderStopLoss()<Bid-TrailingStop*point)
           {
            if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop *point,OrderTakeProfit(),Blue))
               return(0);
           }
        }*/
      else if(OrderOpenPrice()>Bid+Hedge*point)
        {
         sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Blue);
         return(0);
        }
     }
  }
 

有不同的方法可以实现这一点。

当套期保值被打开时,创建一个客户终端的全局变量。

给它一个名称,包括主交易的票号。

给它一个值,作为该票号的对冲交易已被打开的标志,或者必要时的对冲计数。

在开启对冲交易之前,请检查 GV。


对冲和主交易的手数不同。

在打开对冲之前,检查未结订单和历史记录,看是否有相反的订单以相关的手数打开,且订单打开时间()晚于主交易的打开时间。

 
GumRai:

有不同的方法可以实现这一点。

当打开对冲时,创建一个客户终端的全局变量。

给它一个名字,包括主交易的票号。

给它一个值,作为该票号的对冲交易已经开启的标志,或者必要时给它一个对冲计数。

在开启对冲交易之前,请检查GV。


套期保值和主交易有不同的手数。

在打开对冲之前,检查未结订单和历史记录,看是否有相反的订单以相关的手数打开,且订单打开时间()晚于主交易的打开时间。

非常感谢,我将研究这些选项并告诉你。
 

所以我试图通过全局变量来实现这一目标,但自从加入这段代码后,它根本就没有开启对冲交易。 我认为问题在于EA正在进行全局变量检查,但由于还没有创建任何变量,所以它不会继续。 然而,它确实选择并打印了正确的票号。 也许我做错了。 以下是相关代码。

//+------------------------------------------------------------------+
//|  Hedge                                                           |
//+------------------------------------------------------------------+
void Hedgetrade(){
int buy_hedge=0,sell_hedge=0,total=0;
double Pip=Point*10,HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2),SLHedge=StopLossHedge*Pip;
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol()==Symbol()){
      total++;
      int ticket=OrderTicket();
      Print("ticket = ",ticket);
     }          
         if(OrderType()==OP_BUY){
            if(buy_hedge==0 && sell_hedge==0 && OrderOpenPrice()>Bid+Hedge*Pip)
               GlobalVariableCheck(ticket);
               sell_hedge=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+SLHedge,0,"Hedge",0,0,Blue);
                  GlobalVariableSet(ticket,1);
                 }
         if(OrderType()==OP_SELL){
            if(sell_hedge==0 && buy_hedge==0 && OrderOpenPrice()<Ask-Hedge*Pip)
               GlobalVariableCheck(ticket);
               buy_hedge=OrderSend(Symbol(),OP_BUY,HedgeLots,Ask,3,Ask-SLHedge,0,"Hedge",0,0,Red);
                  GlobalVariableSet(ticket,1);
              }
            }
          }