Trailing Stops and Trailing Profit - page 2

 
Arav007:


Sorry actually I have become confused a bit.

if(iOpenOrders_Buy = 3)

Here if the Total Opened Buy order is equal to '3' then the code will proceed to next part.

if(iOpenOrders_Buy > 0)

Then if the Total Opened Buy order's count is greater than '0' then it'll proceed to next.

if(iOpenOrders_Buy == 3)

Then if the Open Buy order count is equal to 3 then it'll open BuyOrder_1, if equal to '2' then it'll open BuyOrder_2 and then BuyOrder_3 is Opened Buy order count is equal to 1.

Am I right?

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);
....
}

you're right look again to the changed code is this ok ??
 
deVries:

you're right look again to the changed code is this ok ??


I don't think so.

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

This is meaning that 'Buy Condition is Met' and there is no 'Opened Buy' orders, right?

then the code will Proceed to next line where iOpenOrders_Buy = 3;

So it is told to EA that there is already 3 opened orders there, right?

But if I entering into the code knowing that there is no Open Buy orders there then why we are assigning iOpenOrders_Buy = 3; ?

Also the target is, One of the Three trades will keep running and only 'New' buy order will be opened when that last trade will get closed.

So this has to be

if(iOpenOrders_Buy== 0) // There is no Opened Buy order so we have to open new Buy orders


When the EA will get the Buy condition then it'll Open Three trades with different Take Profit.

Then if two of the trades got closed anyway(by either TP or SL), the third one will keep running.

 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")

} 

What do you think about this? Would it do the same as stated above?

Regards

 
Arav007:


I don't think so.


What do you think about this? Would it do the same as stated above?

Regards

no what happens if ordersend not succeed, how do you check which trade you have to open if one fails

why we are assigning iOpenOrders_Buy = 3; ?

you have to open 3 new trades if ordersend succeed make iOpenOrders_Buy new value

if iOpenOrders_Buy becomes 0 we have our 3 trades

 
deVries:

no what happens if ordersend not succeed, how do you check which trade you have to open if one fails

why we are assigning iOpenOrders_Buy = 3; ?

you have to open 3 new trades if ordersend succeed make iOpenOrders_Buy new value

if iOpenOrders_Buy becomes 0 we have our 3 trades

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;

}  

}

} 

Yes, it is very much possible that 'Ordersend()' fails to open order.

Now I understood why

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

was used in your code. As we already set iopendOrders=3, so it'll verify it.

But later on I got confused that how the value of iopendOrders will keep changing.

Probably for my limited brain I failed to figure out the mechanism. So I tried to implement your logic in the above way.

Is this what you have referred into your given code?

Regards

 
Arav007:

Yes, it is very much possible that 'Ordersend()' fails to open order.

Now I understood why

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

was used in your code. As we already set iopendOrders=3, so it'll verify it.

But later on I got confused that how the value of iopendOrders will keep changing.

Probably for my limited brain I failed to figure out the mechanism. So I tried to implement your logic in the above way.

Is this what you have referred into your given code?

Regards


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

{ 

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

 
deVries:



Please don't mind but again I have become confused!

If I use

count=count-1; after

Print( "Buy Order 1 Opened successfully");

that means if the first buy order got opened, then value of count will be decreased by 1.

So when entering into next function, here the next 'if' condition:

if (count==2) {}

the value of count has become '2' for it. [count=3-1=2]

Is this right?

 
Arav007:


Please don't mind but again I have become confused!

If I use

count=count-1; after

Print( "Buy Order 1 Opened successfully");

that means if the first buy order got opened, then value of count will be decreased by 1.

So when entering into next function, here the next 'if' condition:

if (count==2) {}

the value of count has become '2' for it. [count=3-1=2]

Is this right?


try it out.... do some testing
 
deVries:

try it out.... do some testing


Tired and this is the Result.

Probably I couldn't place the 'Closing Sell Order before buying' code at the right place. Hence getting Sell and Buy altogether although it's against the Original code.

And there are '4' buying orders altogether!

I failed :(

                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, forgot to put iOpenOrders_Buy ==0

Now testing it again with this.

 
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