追踪止损和追踪利润 - 页 2

 
Arav007:


对不起,实际上我已经有点糊涂了。

if(iOpenOrders_Buy = 3)

这里,如果总的已开买单等于'3',那么代码将进入下一个部分。

if(iOpenOrders_Buy > 0 )

那么如果总开仓买入订单的数量大于'0',那么将进入下一环节。

if(iOpenOrders_Buy == 3)

那么如果打开的买入订单数等于3,那么它将打开BuyOrder_1,如果等于'2',它将打开 BuyOrder_2,然后 BuyOrder_3是打开的买入订单数等于1。

我说的对吗?

double dTakeProfitPrice_1=10;

double dTakeProfitPrice_2=20;

double dTakeProfitPrice_3=0;
double BuyOrder_1,.......,.......;
if (Buy Condition Met && iOpenOrders_Buy == 0)
{

iOpenOrders_Buy = 3;

{

if(iOpenOrders_Buy  > 0) // we have to open new Buy orders

{

if(iOpenOrders_Buy == 3)
         {
         BuyOrder_1=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_1, "Buy Order",MagicNumber, 0,Blue);
         if(BuyOrder_1 > 0).......
         

         }
if(iOpenOrders_Buy == 2)
         {
         BuyOrder_2=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_2, "Buy Order",MagicNumber, 0,Blue);
....



if(iOpenOrders_Buy == 1)
         {
         BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_3, "Buy Order",MagicNumber, 0,Blue);
....
}

你是对的,再看一下修改后的代码,这样可以吗?
 
deVries:

你是对的,再看一下修改后的代码,这样可以吗?


我不认为如此。

if (Buy Condition Met && iOpenOrders_Buy == 0)

这意味着 "买入条件得到满足",并且没有 "打开的买入 "订单,对吗?

那么代码将继续到下一行,iOpenOrders_Buy = 3

所以它告诉EA,那里已经有3个已开订单,对吗?

但如果我进入代码时知道那里没有未平仓的买单,那么为什么我们要分配iOpenOrders_Buy= 3;?

另外,目标是,三个交易中的一个将继续运行,当最后一个交易被关闭时,只有 "新 "买入订单将被打开。

所以这必须是

if(iOpenOrders_Buy== 0) //没有打开的买入订单,所以我们必须打开新的买入订单


当EA得到买入条件时,它将以不同的止盈 打开三笔交易。

如果其中两笔交易被关闭(通过TP或SL),第三笔将继续运行。

 double dTakeProfitPrice_1=10;

double dTakeProfitPrice_2=20;

double dTakeProfitPrice_3=0;

double BuyOrder_1,BuyOrder_2,BuyOrder_3;  

iMaxOrders=3; 

iOpenOrders_Buy = CntOrd(iOrderType_Buy,MagicNumber,Symbol());  //counting open buy orders. 

if (Buy Condition Met && iOpenOrders_Buy ==0) //Buy condition has met and there is no Open Buy Order

{ 

if(iOpenOrders_Buy < iMaxOrders) //This will Limit the desired number of orders; Though this is not necessary.

{

BuyOrder_1=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_1, "Buy Order",MagicNumber, 0,Blue); 

//First order opened with TP1 

BuyOrder_2=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_2, "Buy Order",MagicNumber, 0,Blue);

//Second order opened with TP2  

BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_3, "Buy Order",MagicNumber, 0,Blue);

////Third order opened with TP3  

} 

} 

else{

print ("There is already a Running Order")

} 

你对这个问题怎么看?它的作用和上面说的一样吗?

请注意

 
Arav007:


我不认为如此。


你对这个问题怎么看?它的作用是否与上面所说的一样?

问讯

如果订单发送不成功会发生什么,如果失败了,你如何检查 你要开的交易?

为什么我们要指定iOpenOrders_Buy = 3; ?

你必须打开3个新的交易,如果订单成功,使iOpenOrders_Buy成为新值

如果 iOpenOrders_Buy变成0,我们就有了3笔交易。

 
deVries:

如果订单不成功会发生什么,你如何检查你必须打开的交易,如果一个失败了,你如何检查?

为什么我们要分配iOpenOrders_Buy = 3; ?

你必须打开3个新的交易,如果订单成功,使iOpenOrders_Buy成为新值

如果 iOpenOrders_Buy变成0,我们就有了3笔交易。

int iLastError;

 for (count=iMaxOrders; count>0; count--)

{

if (count==3) {

 BuyOrder_1=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_1, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_1>0) //Checking if the order was opened or not

{ 

Print( "Buy Order 1 Opened successfully");

}

else {

 Print( "Order Sending Error");

iLastError = GetLastError();

iMaxOrders=3; //Setting iMaxOrders to 3 again thus it goes back and try to open that order again

} 

}

 if (count==2) {

 BuyOrder_2=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_2, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_2>0) {

Print( "Buy Order 2 Opened successfully");

}

else {

 Print( "Order Sending Error");

iLastError = GetLastError();

iMaxOrders=2;

}  

}

if (count==1) {

 BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_3, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_3>0) {

Print( "Buy Order 3 Opened successfully");

}

else {

 Print( "Order Sending Error");

iLastError = GetLastError();

iMaxOrders=1;

}  

}

} 

是的,很有可能'Ordersend()'不能开单。

现在我明白了为什么

if(iOpenOrders_Buy > 0 ) //我们必须打开新的买入订单

被用在你的代码中。因为我们已经设置了iopendOrders=3,所以它会验证它。

但后来我感到困惑的是,iopendOrders的值怎么会不断变化。

可能是我的大脑有限,我没能弄清楚这个机制。所以我试着用上面的方法来实现你的逻辑。

这是你在你的代码中提到的吗?

请注意

 
Arav007:

是的,很有可能 "Ordersend() "无法打开订单。

现在我明白了为什么

if(iOpenOrders_Buy > 0 ) //我们必须打开新的买入订单

被用在你的代码中。因为我们已经设置了iopendOrders=3,所以它会验证它。

但后来我感到困惑的是,iopendOrders的值怎么会不断变化。

可能是我的大脑有限,我没能弄清楚这个机制。所以我试着用上面的方法来实现你的逻辑。

这是你在你的代码中提到的吗?

请注意


if (BuyOrder_1>0) //Checking if the order was opened or not

{ 

Print( "Buy Order 1 Opened successfully");
count = count - 1;
}

 
deVries:



请不要介意,但我又一次感到困惑了!

如果我使用

count=count-1; 之后

Print( "Buy Order 1 Opened successfully") 。

这意味着如果第一个买入订单被打开,那么count的值将减少1。

所以当进入下一个函数时,这里是下一个'if'条件。

if (count==2) {}

计数器的值已经变成'2'。[count=3-1=2]

这样做对吗?

 
Arav007:


请不要介意,但我又一次感到困惑了!

如果我使用

count=count-1; 之后

Print( "Buy Order 1 Opened successfully") 。

这意味着如果第一个买入订单被打开,那么count的值将减少1。

所以当进入下一个函数时,这里是下一个'if'条件。

if (count==2) {}

计数器的值已经变成'2'。[count=3-1=2]

这样做对吗?


试试吧.... 做一些测试
 
deVries:

试试吧.... 做一些测试


累了,这就是结果。

可能是我没能把 "买入前关闭卖出指令 "的代码放在正确的位置。因此,尽管它违反了原始代码,但却得到了卖出和买入。

一共有'4个'买入订单!

我失败了:(

                double OpenPrice=Ask;

                double  dTakeProfitPrice_1,dTakeProfitPrice_2,dTakeProfitPrice_3;
                dStopLossPrice = NormalizeDouble(OpenPrice - StopLoss * dPip,Digits);
                dTakeProfitPrice_1 = NormalizeDouble(OpenPrice + TakeProfit_1 * dPip,Digits);
                dTakeProfitPrice_2 = NormalizeDouble(OpenPrice + TakeProfit_2 * dPip,Digits);
                dTakeProfitPrice_3 = NormalizeDouble(OpenPrice + TakeProfit_3 * dPip,Digits);
                
 for (int count=iMaxOrders; count>0; count--)

{

if (count==3) {

 BuyOrder_1=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_1, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_1>0) //Checking if the order was opened or not

{ 

sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 1 sent successfully. Ticket=" + BuyOrder_1;
                                iLastError = 0;
                                count = count - 1; 

}

else {

 iLastError = GetLastError();
                                
                                sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 1. Error code=" + ErrorDescription(iLastError);

iMaxOrders=3; //Setting iMaxOrders to 3 again thus it goes back and try to open that order again

} 

}

 if (count==2) {

 BuyOrder_2=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_2, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_2>0) { 

sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 2 sent successfully. Ticket=" + BuyOrder_2;
                                iLastError = 0;
                                count = count - 1; 

}

else {

 iLastError = GetLastError();
                                
                                sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 2. Error code=" + ErrorDescription(iLastError);

iMaxOrders=2; //Setting iMaxOrders to 2 again thus it goes back and try to open that order again

} 

}

if (count==1) {

 BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_3, "Buy Order",MagicNumber, 0,Blue);

if (BuyOrder_3>0) {

sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 2 sent successfully. Ticket=" + BuyOrder_3;
                                iLastError = 0;
                                count = count - 1; 
}

else {

  iLastError = GetLastError();
                                
                                sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 2. Error code=" + ErrorDescription(iLastError);

iMaxOrders=1; //Setting iMaxOrders to 1 again thus it goes back and try to open that order again

}  

}
 

opps, 忘了把iOpenOrders_Buy ==0

现在用这个再测试一下。

 
double OpenPrice=Ask;

double  dTakeProfitPrice_1,dTakeProfitPrice_2,dTakeProfitPrice_3;
dStopLossPrice = NormalizeDouble(OpenPrice - StopLoss * dPip,Digits);
dTakeProfitPrice_1 = NormalizeDouble(OpenPrice + TakeProfit_1 * dPip,Digits);
dTakeProfitPrice_2 = NormalizeDouble(OpenPrice + TakeProfit_2 * dPip,Digits);
dTakeProfitPrice_3 = NormalizeDouble(OpenPrice + TakeProfit_3 * dPip,Digits);
                
//some condition
if(Ask>High[1] && OrdersTotal()<1)int count=3; //(int count=iMaxOrders; count>0; count--)
{
iLastError = 0;
   if (count==3) 
    {
    BuyOrder_1=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_1, "Buy Order",MagicNumber, 0,Blue);
    if (BuyOrder_1>0) //Checking if the order was opened or not
      { 
      sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 1 sent successfully. Ticket=" + BuyOrder_1;
      count = count - 1; 
      }
     else {
          iLastError = GetLastError();                               
          sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 1. Error code=" + ErrorDescription(iLastError);
          iMaxOrders=3; //Setting iMaxOrders to 3 again thus it goes back and try to open that order again
          } 
    }

   if (count==2) 
    {
    BuyOrder_2=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_2, "Buy Order",MagicNumber, 0,Blue);
    if (BuyOrder_2>0) 
      { 
      sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 2 sent successfully. Ticket=" + BuyOrder_2;
      count = count - 1; 
      }
     else {
          iLastError = GetLastError();
          sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 2. Error code=" + ErrorDescription(iLastError);
          iMaxOrders=2; //Setting iMaxOrders to 3 again thus it goes back and try to open that order again
          } 
    }

   if (count==1) 
    {
    BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice_3, "Buy Order",MagicNumber, 0,Blue);
    if (BuyOrder_3>0) 
      {
      sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Buy order 2 sent successfully. Ticket=" + BuyOrder_3;
      iLastError = 0;
      count = count - 1; 
      }
     else {
          iLastError = GetLastError();                               
          sLog_CheckBuyConditions = sLog_CheckBuyConditions + sNL + "    Error sending buy order 2. Error code=" + ErrorDescription(iLastError);
          iMaxOrders=1; //Setting iMaxOrders to 3 again thus it goes back and try to open that order again
          }
    }  
}    //all code in red not needed