关闭的问题,请帮助

 

大家好
希望语法比以前好。上个星期,我一直在阅读Codersguru。

下面的程序,虽然没有任何错误,但编译器只会记录一个订单。但我的代码不会关闭它。
该程序指出 --- ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",Blue),我没有插入止损点和交易量。
我没有插入止损或止盈,你可以看到。原因是,有一个StopLoss条件已经被编码为---
如果(OrderTicket() == OrderOpenPrice() - (ATR*2))
{
OrderClose(OrderTicket(), OrderLots(), 3, Blue)
所以没有必要再插入止损,不是吗?至于止盈
我将在其他问题解决后将其写入代码。
是我编码平仓条件的方式有问题,还是在语法上又有问题?
谢谢大家的帮助,愿我的问题能帮助到其他人。
谢谢

-----------------------------------------------------------------+
//|                                         Opening and Closeing.mq4 |
//|                                      Copyright © 2010, Ben banta |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ben banta"
//+------------------------------------------------------+
//|                   Closing My Way                     |
//+------------------------------------------------------+



double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
  {                                                               
    if (OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
&&  OrderMagicNumber()  == MagicNumber                          // with my MN    
&&  OrderSymbol()       == Symbol())                            // with my symbol
  {                                                                                                                                                    
    if(OrderType() == OP_SELL)
    order = OrderTicket(); 
                       
    if(OrderTicket() == OrderOpenPrice() + (ATR*2))     // ATR*2 above Sell. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),3,Blue);   
      Print("Sell order closed with Hard Stop"); 
     }     
  else
  {
      Print("Error closing Sell Hard Stop", GetLastError()); 
   }   
//---------- Closeing Buy -------------
           
    if(OrderType() == OP_BUY) 
    order = OrderTicket(); 
    
    if(OrderTicket() == OrderOpenPrice() - (ATR*2))     // ATR*2 below Buy. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),3,Red); 
      Print("Buy order closed with Hard Stop");
     }     
  else 
  {
      Print("Error closing Buy Hard Stop", GetLastError());                                      
      return(0); 
    }                                              
  } 
}


//------------ Opening Buy -----------------

if(OrdersTotal() < 1)                                        // Checking for any working orders
   {                                   
     if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
   {                                                                                     
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
     if(ticket>0)
   { 
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                       
      Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
    }
  else
      Print ("Error Opening Buy Order :" , GetLastError()); 
      return(0);                                                  
  }  
                                                      
//--------------- Opening Sell ----------------
                                                                         
  if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                               //with no working orders
    {   
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
   if(ticket>0)
     {     
       if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                
       Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
     }
    else
       Print ("Error Opening Sell Order :" ,GetLastError());
       return(0);
     }  
    return(0); 
  }
}
 
-----------------------------------------------------------------+
//|                                         Opening and Closeing.mq4 |
//|                                      Copyright © 2010, Ben banta |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ben banta"
//+------------------------------------------------------+
//|                   Closing My Way                     |
//+------------------------------------------------------+



double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
       {                                                               
        if (
           OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
           &&  OrderMagicNumber()  == MagicNumber                  // with my MN    
           &&  OrderSymbol()       == Symbol()                     // with my symbol
           )                     
           {                                                                                                                                                    
            if(OrderType() == OP_SELL) order = OrderTicket(); 
                       
            if(OrderTicket() == OrderOpenPrice() + (ATR*2))     // ATR*2 above Sell. Hard Stop
              {
               OrderClose(OrderTicket(),OrderLots(),3,Blue);   
               Print("Sell order closed with Hard Stop"); 
              }     
              else
              {
               Print("Error closing Sell Hard Stop", GetLastError()); 
              } //ENDif (OrderTicket() == OrderOpenPrice() + (ATR*2))
                                 
                                                              //---------- Closeing Buy -------------
                     
              if(OrderType() == OP_BUY) order = OrderTicket(); 
               
              if(OrderTicket() == OrderOpenPrice() - (ATR*2))     // ATR*2 below Buy. Hard Stop
                {
                 OrderClose(OrderTicket(),OrderLots(),3,Red); 
                 Print("Buy order closed with Hard Stop");
                }     
                else 
                {
                 Print("Error closing Buy Hard Stop", GetLastError());                                      
                 return(0); 
                }//ENDif (OrderTicket() == OrderOpenPrice() - (ATR*2))                                              
           }//ENDIf ( OrderSelect(index, SELECT_BY_TICKET) etc
       }// END for loop               
                     
                             
                                        //------------ Opening Buy -----------------
                       
    if(OrdersTotal() < 1)                                        // Checking for any working orders
      {                                   
       if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
        {                                                                                     
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
         if(ticket>0)
           { 
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
           }
           else
           Print ("Error Opening Buy Order :" , GetLastError()); //ENDif (ticket>0)
             
         return(0);                                                  
        }//ENDif (Ask > High[iHighest(NU... etc  
                                                      
                                                             //--------------- Opening Sell ----------------
                                                                         
       if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                                    //with no working orders
          {   
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
           if(ticket>0)
             {     
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
             }//ENDif (ticket>0)
             else
             Print ("Error Opening Sell Order :" ,GetLastError()); 
             //ENDif (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                 
           return(0);
          }//ENDif (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])  
       return(0); 
      }//ENDif  (OrdersTotal() < 1)
}//ENDint start()
应该使你的代码块更容易阅读
 
if(OrderTicket() == OrderOpenPrice() - (ATR*2)) 
订单票 是一个非常大的整数(可能是几十亿,),是订单的关键。
OOP - 2ATR是一个价格。
两者永远不会相等
如果你的意思是OrderClosePrice() == OOP-2ATR实数几乎永远不会比较相等。 使用代替。
 if( OrderClosePrice() - OrderOpenPrice()-ATR*2 <= 0)//...

OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
           &&  OrderMagicNumber()  == MagicNumber                  // with my MN    
           &&  OrderSymbol()       == Symbol() 
你的orderSend并没有设置一个神奇的数字
 
Ickyrus wrote>>
应该使你的代码块更容易阅读


是的,你是对的。每次尝试和询问,我都能学到更多的东西。谢谢你的清理。我也是最近几天才把大括号放在适当的地方。越简单越好。
干杯
 
WHRoeder wrote>>
订单票是一个非常大的整数(可能是几十亿,),是订单的关键。
OOP - 2ATR是一个价格。
两者永远不会相等
如果你的意思是OrderClosePrice() == OOP-2ATR实数几乎永远不会比较相等。 使用代替。

你的orderSend并没有设置一个神奇的数字


你好,WHRoeder
谢谢你的关注和给出的建议。但我必须说我没有马上理解OOP -2ATR。
OOP = OrderOpenPrice。一开始我就不明白。你的解释是完全正确的。我被==符号所迷惑。OrderClosePrice()可能永远不等于OrderOpenPrice()减去(ATR*2)。 谢谢你注意到这一点。
让我看看我是否理解你的建议。我在这里重述一下。 if(OrderClosePrice()-OOP()-ATR*2 <= 0)
这看起来是这样的(我在这里使用任意数字)OCP=1.4361,OOP=1.4321,ATR=20。所以表达式看起来是这样的。
如果(1.4321-1.4361-(20*2)<=0),对不起,我不明白。
也许我可以用同样的数字来重新排列表达式,以显示我的意思。但当我把它们捅到程序中时,这也不起作用。
新的安排看起来像这样。

如果(1.4361>=1.4321+(20*2))。

在OCP>=OOP+ATR*2的地方,控制权传递给。
OrderClose (OrderTicket(), OrderLots(),等等。

你的另一个观察是,OrderSend没有设置一个神奇的数字。
我是否可以把这部分从程序中删除:&& OrderMagicNumber == MagicNumber。
再次感谢你,如果你能看到其他的改进,我将非常感激。
谢谢
 

你好,艾斯
谢谢你的答复。希望你过得很好。
这个建议是今天早上插入的。很抱歉地说,编译器不喜欢我的编码。我不确定程序中的哪条语句是不正确的。我相信这些建议是好的。我将重新发布程序,并标出我认为可能存在问题的地方。我还将说明平仓的策略。

卖出头寸将在订单开盘价以上被Atr*2停止。例如,OOP是1.4321,Atr=20,OCP=1.4361。
所以我将表达式编码为。

if(OrderClosePrice >= OrderOpenPrice(), - (ATR*2))//满足条件,控制权转移到....
OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(),Blue) 。

注意:在OrderSend语句中没有StopLoss()或TakeProfit(),如果这有什么关系的话。

再次感谢大家的帮助
欢呼声
double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
  {                                                               
    if (OrderSelect(index, SELECT_BY_TICKET)                  // existing orders
//&&  OrderMagicNumber()  == MagicNumber                      // with my MN    
&&  OrderSymbol()       == Symbol())                          // with my symbol
  {                                                                                                                                                    
    if(OrderType() == OP_SELL) order = OrderTicket(); 
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                       
    if(OrderClosePrice() >= OrderOpenPrice() + (ATR*2)) // condition to be met,
                                                        // control then passes to...
  {                                                     
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue); // OrderClose
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      Print("Sell order closed with Hard Stop"); 
     }     
  else
  {
      Print("Error closing Sell Hard Stop", GetLastError()); 
   }   
//---------- Closeing Buy -------------
           
    if(OrderType() == OP_BUY)  order = OrderTicket(); 
    
    if(OrderClosePrice() <= OrderOpenPrice() - (ATR*2))        // ATR*2 below Buy. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Red); 
      Print("Buy order closed with Hard Stop");
     }     
  else 
  {
      Print("Error closing Buy Hard Stop", GetLastError());                                      
      return(0); 
    }                                              
  } 
}


//------------ Opening Buy -----------------

if(OrdersTotal() < 1)                                        // Checking for any working orders
   {                                   
     if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
   {                                                                                     
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
     if(ticket>0)
   { 
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                       
      Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
    }
  else
      Print ("Error Opening Buy Order :" , GetLastError()); 
      return(0);                                                  
  }  
                                                      
//--------------- Opening Sell ----------------
                                                                         
  if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                               //with no working orders
    {   
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
   if(ticket>0)
     {     
       if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                
       Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
     }
    else
       Print ("Error Opening Sell Order :" ,GetLastError());
       return(0);
     }  
    return(0); 
  }
}
 

你好,Huckleberry,
,我将稍微重新设计你的程序。
,暂时再见

 

为什么做 "order = OrderTicket(); "而不在orderclose()函数中使用这个值?
虽然所选订单 发生变化的几率很小,但使用正面识别的订单值不是更安全吗?
-
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue)。
OrderTicket()将是当前选择的票据--在我学习的这个阶段
我不知道你在做OrderClose()的时候选择的是哪张票。

//// 编辑
事实上,我不确定 "order= OrderTicket() "是你在前面的If语句中确定的票据。

 
Ais wrote>>

你好,Huckleberry,
,我将稍微重新设计你的程序。
,暂时再见


非常感谢你。我将等待你的答复。
欢呼声
 
Ickyrus wrote>>

为什么做 "order = OrderTicket(); "而不在orderclose()函数中使用这个值?
虽然所选订单发生变化的几率很小,但使用正面识别的订单值不是更安全吗?
-
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue)。
OrderTicket()将是当前选择的票据--在我学习的这个阶段
我不知道你在做OrderClose()的时候选择的是哪张票。

//// 编辑
事实上,我不确定 "order= OrderTicket() "是你在前面的If语句中确定的那个票。


嗨,Ickyrus
谢谢你的观察和建议。你的时间很宝贵。
你的推理很有道理。在选择顺序方面,我不像你那么确定。我会试一试你的建议。
再次感谢
欢呼声