Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

First independent community of traders

First independent community of traders Logo First independent community of traders Logo

First independent community of traders Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • Buy Theme
  • Home
  • About Us
  • Blog
  • Contact Us
Home/ Questions/Q 875
In Process
gaiatsu
gaiatsu

gaiatsu

  • 1 Question
  • 2 Answers
  • 0 Best Answers
  • 20 Points
View Profile
  • 0
gaiatsu
Asked: December 18, 20202020-12-18T09:53:01+00:00 2020-12-18T09:53:01+00:00

TrailStop flounders

  • 0

what’s wrong here does anyone know?

void TrailingStopBuy(double Ask)
  {    
      // set the stop loss to 150 points
      double SL=NormalizeDouble(Ask-150*_Point,_Digits);

      // Go through all positions
      for(int i=PositionsTotal()-1; i>=0; i--)  
      {
                  string symbol=PositionGetSymbol(i); // get the symbol of the position    
                  
        if (_Symbol==symbol) // if currency pair is equal      
         // if we have a sell position
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)  
         {
            // get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            
            // calculate the current stop loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
            
            // if current stop loss is more than 150 points
            if (CurrentStopLoss>SL)
            {
            // move the stop loss
            trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0);
            }  
         } // End if loop
          
      } // End for loop
  } // End            
void TrailingStopSell(double Bid)
  {    
      // set the stop loss to 150 points
      double SL=NormalizeDouble(Bid+150*_Point,_Digits);

      // Go through all positions
      for(int i=PositionsTotal()-1; i>=0; i--)  
      {
                  string symbol=PositionGetSymbol(i); // get the symbol of the position    
                  
        if (_Symbol==symbol) // if currency pair is equal      
        
         // if we have a sell position
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)  
         {
            // get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            
            // calculate the current stop loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
            
            // if current stop loss is more than 150 points
            if (CurrentStopLoss>SL)
            {
            // move the stop loss
            trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);
            }  
         } // End if loop
          
      } // End for loop
  } // End

  • 6 6 Answers
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    6 Answers

    • Voted
    • Oldest
    • Recent
    1. russian_hedge_fund

      russian_hedge_fund

      • 1 Question
      • 1 Answer
      • 0 Best Answers
      • 22 Points
      View Profile
      russian_hedge_fund
      2020-12-22T07:03:03+00:00Added an answer on December 22, 2020 at 7:03 am

      Gaiatsu:
      yes checked everything works as under
      nadfilem, thank you all for your help

      Hello everyone

      i can’t get the trailing stop to work.

      please tell me what is wrong where ?

      Thank you in advance

      #include
      CTrade trade;

      void OnTick()
        {

         double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

         double Equity=AccountInfoDouble(ACCOUNT_EQUITY);

         double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);

         double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

         if(PositionsTotal()==0 && OrdersTotal()==0)
           {

               trade.BuyStop(1,Ask+1*_Point,_Symbol,Ask-4*_Point,Ask+11*_Point, ORDER_TIME_GTC,0,0);

               trade.SellStop(1,Bid-1*_Point,_Symbol,Bid+4*_Point,Bid-11*_Point, ORDER_TIME_GTC,0,0);

           }

         if(Balance!=Equity)
           {
              
               CancelOrder();    
              
            }
            
            TrailingStop(Ask, Bid);
      }

      void CancelOrder()
        {
         for(int i=OrdersTotal()-1; i>=0; i--)
           {

            ulong OrderTicket = OrderGetTicket(i);

            trade.OrderDelete(OrderTicket);

           }

        }
        

      void TrailingStop(double Ask, double Bid)
      {

         double SL_Ask = NormalizeDouble(Ask-1*_Point, _Digits);
        
         double SL_Bid = NormalizeDouble(Bid+1*_Point, _Digits);
        
         for (int i=PositionsTotal()-1; i>=0; i--)
            {
        
               string symbol=PositionGetSymbol(i);
            
               if (_Symbol==symbol)
              
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
                  {
            
                     ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
                    
                     double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);
              
                     double CurrentSL = PositionGetDouble(POSITION_SL);
              
               if(CurrentSL>SL_Ask)
                  {
              
                     trade.PositionModify(PositionTicket,(CurrentSL+3*_Point),0);
              
                  }
                  
               if (Ask==(PositionBuyPrice-2*_Point))
                  {
                    
                     trade.PositionModify(PositionTicket,CurrentSL+1*_Point,0);
                    
                  }
              
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
                  {
                  
                     ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
              
                     double CurrentSL = PositionGetDouble(POSITION_SL);  
              
               if(CurrentSL3*_Point),0);
              
                  }
                  
               if (Bid==(PositionBuyPrice-2*_Point))
                  {
                    
                     trade.PositionModify(PositionTicket,CurrentSL+1*_Point,0);
                    
                  }
              
                  }
            
                  }
        
        
            }

      }

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. gaiatsu

      gaiatsu

      • 1 Question
      • 2 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      gaiatsu
      2020-12-18T09:53:42+00:00Added an answer on December 18, 2020 at 9:53 am

      as you can guess, the modification takes place on the server and only then, after confirmation in the terminal, the result changes…in my opinion you
      wrong sorry for my noobism but still

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    3. tara

      tara

      • 0 Questions
      • 9 Answers
      • 0 Best Answers
      • 38 Points
      View Profile
      tara
      2020-12-18T09:53:37+00:00Added an answer on December 18, 2020 at 9:53 am

      Gaiatsu:

      I added the code for myself it turned out like this

      but why when I’m in the black already I have not moved the trailstop stop los as was the original and remains, here are the functions of buy and sell

      I would still modify the order. Well, or in some other way signaled to the server that the order has already been modified – the server itself may not
      guess. 

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. gaiatsu

      gaiatsu

      • 1 Question
      • 2 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      gaiatsu
      2020-12-18T09:53:24+00:00Added an answer on December 18, 2020 at 9:53 am

      I added the code for myself it turned out like this

      //+------------------------------------------------------------------+
      //| TrailingStop Buy                                                 |
      //+------------------------------------------------------------------+
      void TrailingStopBuy(string symbol_, double Ask, int number)
        {
         double CalculatedStopLoss=NormalizeDouble(Ask-50*_Point,_Digits);   // set the stop loss to 150 points
         for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
           {
            string symbol=PositionGetSymbol(i);                               // get the symbol of the position
            if(symbol_==symbol)                                               // if currency pair is equal
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)       // if we have a sell position
                 {
                  ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
                  double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
                  if(CalculatedStopLoss>CurrentStopLoss)                                      // if current stop loss is more than 150 points
                    {
                     trade[number].PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss
                    }
                 } // End if loop
           } // End for loop
        } // End
      //+------------------------------------------------------------------+
      //|  TrailingStop Sell                                               |
      //+------------------------------------------------------------------+
      void TrailingStopSell(string symbol_, double Bid, int number)
        {
         double CalculatedStopLoss=NormalizeDouble(Bid+50*_Point,_Digits);   // set the stop loss to 150 points
         for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
           {
            string symbol=PositionGetSymbol(i);                               // get the symbol of the position
            if(symbol_==symbol)                                               // if currency pair is equal
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)      // if we have a sell position
                 {
                  ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
                  double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
                  if(CalculatedStopLoss// if current stop loss is more than 150 points
                    {
                     trade[number].PositionModify(PositionTicket,(CalculatedStopLoss-10*_Point),0);// move the stop loss
                    }
                 } // End if loop
           } // End for loop
        } // End

      but why when I’m in the black already I have not moved the trailstop stop los as was the original and remains, here are the functions of buy and sell

      bool OpenBuyOrder(const string symbol, int number)
      {
          if (!PositionSelect(symbol))
          {
            double Ask=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),_Digits);
            trade[number].Buy(Lots,symbol,Ask,NormalizeDouble(Ask-100*_Point, _Digits),0,NULL);
            tickets[number] = trade[number].ResultDeal();
          }
          
          return(true);
          
      }
      //+------------------------------------------------------------------+
      bool OpenSellOrder(const string symbol, int number)
      {
          if (!PositionSelect(symbol))
          {
            double Bid=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),_Digits);
            trade[number].Sell(Lots,symbol,Bid,NormalizeDouble(Bid+100*_Point,_Digits),0, NULL);//NormalizeDouble(Bid-100 * _Point, _Digits)
            tickets[number] = trade[number].ResultDeal();
          }
          
          return(true);
          
      }

      //+------------------------------------------------------------------+

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    5. alex_all

      alex_all

      • 0 Questions
      • 13 Answers
      • 0 Best Answers
      • 46 Points
      View Profile
      alex_all
      2020-12-22T07:01:27+00:00Added an answer on December 22, 2020 at 7:01 am

      Vladimir Karputov:

      And now with the corrections:

      trade.PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss

      the error is possible here, in positionmodify
      the price normalization is no longer performed, it is necessary to pass the normalized
      normalizedouble to it
      or in the m_symbol.normalizeprice expert advisor

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    6. barabashkakvn

      barabashkakvn

      • 7 Questions
      • 162 Answers
      • 0 Best Answers
      • 314 Points
      View Profile
      barabashkakvn
      2020-12-18T09:53:06+00:00Added an answer on December 18, 2020 at 9:53 am

      Combed the code to a more beautiful look.

      Note: Forget and get the word “warrant” out of your head. You need to use the words “position”
      or “transaction” (“transaction” can be used by persons who have reached the age of majority).

      So:

      ENUM_POSITION_TYPE

      Identifier

      Description

      POSITION_TYPE_BUY

      Buy

      POSITION_TYPE_SELL

      Sell

      Based on this:

      //+------------------------------------------------------------------+
      //| TrailingStop Buy                                                 |
      //+------------------------------------------------------------------+
      void TrailingStopBuy(double Ask)
        {
         double SL=NormalizeDouble(Ask-150*_Point,_Digits);                   // set the stop loss to 150 points
         for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
           {
            string symbol=PositionGetSymbol(i);                               // get the symbol of the position
            if(_Symbol==symbol)                                               // if currency pair is equal
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)       // if we have a sell position
                 {
                  ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
                  double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
                  if(CurrentStopLoss>SL)                                      // if current stop loss is more than 150 points
                    {
                     trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0); // move the stop loss
                    }
                 } // End if loop
           } // End for loop
        } // End
      //+------------------------------------------------------------------+
      //|  TrailingStop Sell                                               |
      //+------------------------------------------------------------------+
      void TrailingStopSell(double Bid)
        {
         double SL=NormalizeDouble(Bid+150*_Point,_Digits);                   // set the stop loss to 150 points
         for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
           {
            string symbol=PositionGetSymbol(i);                               // get the symbol of the position
            if(_Symbol==symbol)                                               // if currency pair is equal
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)      // if we have a sell position
                 {
                  ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
                  double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
                  if(CurrentStopLoss>SL)                                      // if current stop loss is more than 150 points
                    {
                     trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);// move the stop loss
                    }
                 } // End if loop
           } // End for loop
        } // End

      I didn’t check further.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Sidebar

    Ask A Question

    Stats

    • Questions 504
    • Answers 2k
    • Posts 5
    • Comments 0
    • Best Answers 0
    • Users 673
    • Popular
    • Comments
    • Tags
    • forexcommunity

      Highlighting what’s important about questions & Answers on Discy Community!

      • 0 Comments
    • forexcommunity

      Introducing Keyboard Shortcuts, our first Labs feature

      • 0 Comments
    • forexcommunity

      Defining quality on Discy Engine — what a helpful answer ...

      • 0 Comments
    • forexcommunity

      Organizational and company accounts on Discy Engine the next step

      • 0 Comments
    • forexcommunity

      Hello world!

      • 0 Comments

    Users

    Brus Agell

    Brus Agell

    • 1 Question
    • 0 Answers
    pinupcasinoo

    pinupcasinoo

    • 1 Question
    • 0 Answers
    JamesSwics

    JamesSwics

    • 0 Questions
    • 0 Answers

    Footer

    First independent community of traders

    About

    An independent community of forex traders. This is where traders communicate. You can ask your questions and you will receive an answer to your question.
    • Terms of Use
    • Privacy Policy
    • Cookie Policy
    • Knowledge Base
    • Support

    © 2021 Forexcommunity.net. All Rights Reserved