Trailing Stops and Trailing Profit - page 5

 
Arav007:


3rd Trade's TP= 108.536 [ I set this TP at such distance thus it'll work like there is no TP]

Now, as per my requirement, when the price would be say 7 pip [103.603] in profit, the Stop Loss of

1st and 2nd trade will become say 1 pip profit [103.543] and 3rd trade's Stop Loss will remain at 103.386.

Like here in the below image:

1st GBPUSD order got closed at 10 pips Profit.

2nd Order is still running and it's SL got moved.

For 3rd Trade the SL got Moved too which I don't Want.

BuyOrder_3=OrderSend(Symbol(), iOrderType_Buy, LotSize,OpenPrice,Slippage,dStopLossPrice,0, "Buy Order",MagicNumber, 0,Blue);//tp 0
 
deVries:


Thanks. This is another way and Good way indeed to set No TP.

But what about the SL? How can I do it without Variable Order Ticket way?

Regards

 
if(OrdersTotal()>0){
 

for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
        
        if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
        if(OrderSymbol() == Symbol() && OrderMagicNumber() ==  MagicNumber){
                
        
                sl     = OrderStopLoss(); // Stop Loss. 
                tStopLoss = NormalizeDouble(OrderStopLoss(), Digits); // Stop Loss. 
                
                if(OrderType()==OP_BUY){

if (OrdersTotal()==3){

if (cnt==1){

Fixed Stop Loss

}

else{

Trailing Stop Loss

}

}    // end of  if (OrdersTotal()==3)

else{    // Looking for 2nd option                           

if (OrdersTotal()==2){                           // 10 pips TP trade can be closed already

if(cnt== 0){

Fixed Stop Loss

}

else{

Trailing Stop Loss

}

}  // End of  if (OrdersTotal()==2)

}  // End of  else

 

What do you think about this? Is this OK for Stop Loss part?

 
Arav007:

What do you think about this? Is this OK for Stop Loss part?


no, it's wrong

but what i dislike more is how i have to read

it's unreadable coding what you have here change your style writing your code !!!!

empty lines not needed this is same as yours

can you do it like this ??

if(OrdersTotal()>0)
   {
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
     if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
     if(OrderSymbol() == Symbol() && OrderMagicNumber() ==  MagicNumber)
        {
        sl     = OrderStopLoss(); // Stop Loss. 
        tStopLoss = NormalizeDouble(OrderStopLoss(), Digits); // Stop Loss. 
        if(OrderType()==OP_BUY)
           {
           if (OrdersTotal()==3) 
              {
              if (cnt==1)
                 {
                 Fixed Stop Loss     
                 }
               else{
                   Trailing Stop Loss
                   }
              }    // end of  if (OrdersTotal()==3)
           else{    // Looking for 2nd option                           
               if (OrdersTotal()==2)
                 {                           // 10 pips TP trade can be closed already
                 if(cnt== 0)
                   {
                   Fixed Stop Loss
                   }
                   else{
                       Trailing Stop Loss
                       }
                 }  // End of  if (OrdersTotal()==2)
   }  // End of  else

no empty lines

easy to see where '{' begins and for what part of code it is inside where '}' ends

it can happen you have more then 20 trades open you can't use inside the loop if (OrdersTotal()==

or if(cnt==

why do you this orderloop

you do this orderloop to run through all your trades one by one

What you have to code is a BreakEven

what is Fixed Stop Loss doing ????

 
deVries:


no, it's wrong

but what i dislike more is how i have to read

it's unreadable coding what you have here change your style writing your code !!!!

empty lines not needed this is same as yours

can you do it like this ??

no empty lines

easy to see where '{' begins and for what part of code it is inside where '}' ends

it can happen you have more then 20 trades open you can't use inside the loop if (OrdersTotal()==

or if(cnt==

why do you this orderloop

you do this orderloop to run through all your trades one by one

What you have to code is a BreakEven

what is Fixed Stop Loss doing ????


Sorry, actually I feel more comfortable with the empty lines. Anyway, I'll follow it while posting codes here.

I thought that 'OrdersTotal()' will fetch the orders 'Only' from the pair to which the EA is attached.

But it probably fetches all the orders and then we distinguishing the related pair's orders with other orders

by using this line:

if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

Right?

By Fixed Stop Loss I'm referring that the 'initial' stop Loss will not Move.

Regards

 
deVries:


change your style writing your code !!!!

empty lines not needed


It is a matter of opinion, but is generally considered good practice to use empty lines (in the logical places) when coding. In anycase, it's not really something worth pointing out in others code in this manner.

If you look at the Apache or Linux Kernel source code (two very highly collaborated Open projects) you will see plenty of blank lines.


APACHE WEB SERVER:

AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
                                                   void *struct_ptr,
                                                   const char *arg)
{
    int offset = (int)(long)cmd->info;

    *(const char **)((char *)struct_ptr + offset) = arg;

    return NULL;
}

LINUX KERNEL:


static int jz4740_musb_init(struct musb *musb)
{
 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
 if (!musb->xceiv) {
 pr_err("HS UDC: no transceiver configured\n");
 return -ENODEV;
 }

 /* Silicon does not implement ConfigData register.
 * Set dyn_fifo to avoid reading EP config from hardware.
 */
 musb->dyn_fifo = true;

 musb->isr = jz4740_musb_interrupt;

 return 0;
} 
 
Arav007:


sometimes a empty line can be comfartable but not if you do it after every written line

also place of braces makes it more easier to read the code


from MACD to use it as example

   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {

            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {

            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return;
                    }
                 }
              }
           }
        }
     }

now you see how to compare price currency chart with orderopenprice of your trade

you have to do something like that also...

 
ydrol:

It is a matter of opinion, but is generally considered good practice to use empty lines (in the logical places) when coding. In anycase, it's not really something worth pointing out in others code in this manner.

If you look at the Apache or Linux Kernel source code (two very highly collaborated Open projects) you will see plenty of blank lines.


APACHE WEB SERVER:

LINUX KERNEL:

the point is with every written line an empty line

it gets hard to read big codes you keep scrolling up/down to read the code

and when there is also no logic of placing { } then it makes it even harder finding bugs

 
deVries:


from MACD to use it as example

now you see how to compare price currency chart with orderopenprice of your trade

you have to do something like that also...


I have such price comparison function.

How can I compare the selected order's 'Comment' of OrderSend() like we Selected order's Magic Number?

Like if I want to verify whether OrderComment() matches with the comment of the selected order?

SellOrder=OrderSend(Symbol(), iOrderType_Sell, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice, "Sell Order",MagicNumber, 0,Red);

Now can I compare like this:

OrderComment()=="Sell Order" // ?

Also can I define the comment of OrderSend() function like this:

string comment_sell="Sell Order";

SellOrder=OrderSend(Symbol(), iOrderType_Sell, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice, comment_sell ,MagicNumber, 0,Red);

And then compare :

if (OrderComment()=="comment_sell") // ?

Regards

 
Arav007:


I have such price comparison function.

How can I compare the selected order's 'Comment' of OrderSend() like we Selected order's Magic Number?

Like if I want to verify whether OrderComment() matches with the comment of the selected order?

SellOrder=OrderSend(Symbol(), iOrderType_Sell, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice, "Sell Order",MagicNumber, 0,Red);

Now can I compare like this:

OrderComment()=="Sell Order" // ?

Also can I define the comment of OrderSend() function like this:

string comment_sell="Sell Order";

SellOrder=OrderSend(Symbol(), iOrderType_Sell, LotSize,OpenPrice,Slippage,dStopLossPrice,dTakeProfitPrice, comment_sell ,MagicNumber, 0,Red);

And then compare :

if (OrderComment()=="comment_sell") // ?

Regards


OrderComment can be overwritten by your broker

you are wanting... Now, as per my requirement, when the price would be say 7 pip [103.603] in profit, the Stop Loss of

1st and 2nd trade will become say 1 pip profit [103.543] and 3rd trade's Stop Loss will remain at 103.386.

make extern int BreakEven = 7

         if(OrderType()==OP_BUY)
           {

            //--- check for BreakEven
            if(BreakEven>0)
              {
               if(Bid-OrderOpenPrice()>Point(*10)*BreakEven)
                 {
                  if(OrderStopLoss()<OrderOpenPrice())
                    {
                     //--- modify order
                     if(OrderTakeProfit()>Point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point(*10),OrderTakeProfit(),0,Green);
                    }
                 }
              }
           }

(* 10 ) correction at 5 digit broker to get pipvalue

Reason: