Triling stop code error - Need Edit

 

‌triling stop working with buy orders but not working with sell orders

can any one help with that to make tsl work for both sell and buy

thanks in advance‌

// Determining the TP, SL and TSL Levels
void Set_Levels(){
  
   double PP=MarketInfo(OrderSymbol(), MODE_POINT);
   double BID=MarketInfo(OrderSymbol(), MODE_BID);
   double ASK=MarketInfo(OrderSymbol(), MODE_ASK);
   double TP = PP*TakeProfit;
   double SL = PP*StopLoss;
   double BUY_TSL  = BID - (TrailingStop*PP);
   double SELL_TSL = ASK + (TrailingStop*PP);
  
   if(Side == -1) return;
  
   if(TakeProfit > 0){
      
      if(Side == OP_BUY  ) TP_Level = Open_Level + TP;
      if(Side == OP_SELL ) TP_Level = Open_Level - TP;
  
   }

   if(StopLoss > 0){
      
      if(Side == OP_BUY  ) SL_Level = Open_Level - SL;
      if(Side == OP_SELL ) SL_Level = Open_Level + SL;
  
   }
  
   bool Order_Modified = false;
   if ((OrderTakeProfit()==0 && TakeProfit > 0) || (OrderStopLoss()==0 && StopLoss > 0)){
      if (OrderModify(OrderTicket(),0,SL_Level,TP_Level,0,clrBlue)){ Order_Modified = true; }
   }
  
   if(TrailingStop){
      
      if(Side == OP_BUY){
         if ((BID-OrderOpenPrice())>TrailingStop*PP){
            if (OrderStopLoss()<BID-(TrailingStop+TrailingStep-1)*PP && BID-TrailingStop*PP>SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),BID-TrailingStop*PP,OrderTakeProfit()+0.0001,0,clrMagenta)){ Order_Modified = true; }
            }
         }
      }
      
      if(Side == OP_SELL){
         if (OrderOpenPrice()-ASK>TrailingStop*PP) {
            if (OrderStopLoss()>ASK+(TrailingStop+TrailingStep-1)*PP && ASK+TrailingStop*PP<SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),ASK+TrailingStop*PP,OrderTakeProfit(),0,clrMagenta)){ Order_Modified = true; }
            }
         }
      }
  
   }
  
   return;
}

-- Edited by moderator to use SRC --

 

What is SL_Level ?

This condition is probably never true :

ASK+TrailingStop*PP<SL_Level

 
Alain Verleyen:

What is SL_Level ?

This condition is probably never true :

ASK+TrailingStop*PP<SL_Level

double Side, Open_Level, TP_Level, SL_Level, TSL_Level = 0;

      // Reset Levels
   TP_Level   = 0;
   SL_Level   = 0;
   TSL_Level  = 0;

            if (Side==OP_BUY)  Order_Label = "Buy Order";
            if (Side==OP_SELL) Order_Label = "Sell Order";
            Open_Level = OrderOpenPrice();
            Set_Levels();
            Orders_Comments+=Order_Label+" #"+OrderTicket()+" @ "+Open_Level+" TP: "+TP_Level+" SL: "+SL_Level+"\n";





   // TP Hit
   if(TP_Level > 0){
      if(Side == OP_BUY && Bid >= TP_Level){
         //ClosePos(OP_BUY);
         LastClose = TimeCurrent();
      }
      else if(Side == OP_SELL && Ask <= TP_Level){
         //ClosePos(OP_SELL);
         LastClose = TimeCurrent();
      }
   }
    
   //SL Hit
   if(SL_Level > 0){
      if(Side == OP_BUY && Bid <= SL_Level){
         //ClosePos(OP_BUY);
         LastClose = TimeCurrent();
      }
      else if(Side == OP_SELL && Ask >= SL_Level){
         //ClosePos(OP_SELL);
         LastClose = TimeCurrent();
      }
   }‌
 
no orderselect
 

2 posts and we still don't have all relevant information.

Forum on trading, automated trading systems and testing trading strategies



Welcome,

  • Usually people who can't code don't receive free help on this forum, though it could happen if you are lucky, be patient.
  • If you show your attempts and describe well your problem, you will most probably receive an answer from the community.
  • If you don't want to learn to code, nothing bad, you can either look at the Codebase if something free already exists, or in the Market for paid products (sometimes free also).
  • Finally, you also have the option to hire a programmer in the Freelance section.

Good luck.


 
      if(Side == OP_BUY){
         if ((BID-OrderOpenPrice())>TrailingStop*PP){
            if (OrderStopLoss()<BID-(TrailingStop+TrailingStep-1)*PP && BID-TrailingStop*PP>SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),BID-TrailingStop*PP,OrderTakeProfit()+0.0001,0,clrMagenta)){ Order_Modified = true; }

      

the buy is working well

      if(Side == OP_SELL){
         if (OrderOpenPrice()-ASK>TrailingStop*PP) {
            if (OrderStopLoss()>ASK+(TrailingStop+TrailingStep-1)*PP && ASK+TrailingStop*PP<SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),ASK+TrailingStop*PP,OrderTakeProfit(),0,clrMagenta)){ Order_Modified = true; }
            

but problem is in sell code

note: i'm not the developer of this code ‌

 
Alain Verleyen:

2 posts and we still don't have all relevant information.


Usually people who can't code don't receive free help on this forum !!!!!!!!!!!!!

ok thank you very very much for your time ‌

 
Ehab.Ali:
      if(Side == OP_BUY){
         if ((BID-OrderOpenPrice())>TrailingStop*PP){
            if (OrderStopLoss()<BID-(TrailingStop+TrailingStep-1)*PP && BID-TrailingStop*PP>SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),BID-TrailingStop*PP,OrderTakeProfit()+0.0001,0,clrMagenta)){ Order_Modified = true; }

      

the buy is working well

      if(Side == OP_SELL){
         if (OrderOpenPrice()-ASK>TrailingStop*PP) {
            if (OrderStopLoss()>ASK+(TrailingStop+TrailingStep-1)*PP && ASK+TrailingStop*PP<SL_Level) {
               if (OrderModify(OrderTicket(),OrderOpenPrice(),ASK+TrailingStop*PP,OrderTakeProfit(),0,clrMagenta)){ Order_Modified = true; }
            

but problem is in sell code

note: i'm not the developer of this code ‌

Talk to the developer ?
 
Marco vd Heijden:
Talk to the developer ?
ok i'll and thanks for your kindness in reply
 
Ehab.Ali: note: i'm not the developer of this code
Then there is no common language for use to communicate. learn to code it, or pay (Freelance) someone to code it. We're not going to code it for you. We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
 

NOT YOUR BUSINESS LEARN OR NOT
I ASKED FOR HELP THEY ANSWER CANNOT HELP I SAY THANKS

‌IF YOU HAVE ANY HELP ABOUT MY ASK WELCOME WITH IT

IF NOT DON'T REPLY

I'M A VISUAL BASIC DEVELOPER AND WHATEVER ANY ONE ASK ME FOR HELP

I'LL NEVER GIVE HIM A LESSON TO DO WHAT OR NOT TO DO

I'LL REPLY WITH HELP IF I KNOW HOW OR NOT

NOW I KNOW THIS FORUM RULES NO HELP FOR NEWBIE SO I SAIED THANKS SO NO NEED FOR PHIOLOSOPHY

AND PLEASE NO MORE ANSWES ABOUT MY QUESTION THANKS FOR ALL 

‌‌

Reason: