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 790
In Process
crooked
crooked

crooked

  • 1 Question
  • 1 Answer
  • 0 Best Answers
  • 20 Points
View Profile
  • 0
crooked
Asked: December 18, 20202020-12-18T09:47:44+00:00 2020-12-18T09:47:44+00:00

Transfer to breakeven for a few positions

  • 0

Hi.

There is a breakeven transfer function breakevenpoint(). I open three positions, and when the first one is closed by take, this function is activated. That is,
there are two positions left, but this function transfers the stop of only one position to breakeven, the second one remains with the old stop. It seems that everything is
written correctly, but it does not work correctly. What is the error. I would appreciate a hint.

void BreakEvenPoint()
{
   if(BreakEven == 0)
      return;
   for(int i=PositionsTotal()-1; i>=0; i--)  
      if(iPosition.SelectByIndex(i))      
         if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
         {
            if(PositionSelect(iSymbol.Name())==true) // there is an open position
            {
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
               {
                  if(iSymbol.Bid() > (iPosition.PriceOpen() + iSprd * iSymbol.Point()))
                  {
                     double sl = NormalizeDouble(iPosition.PriceOpen() + iSprd, iSymbol.Digits());
                     double tp = iPosition.TakeProfit();
                     if(iPosition.StopLoss() < sl || iPosition.StopLoss()==0.0)
                     {
                        //--- modify position
                        if(iTrade.PositionModify(iSymbol.Name(), sl, tp))
                           printf("Long position by %s to be modified", iSymbol.Name());
                        else
                        {
                           printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
                           printf("Modify parameters : SL=%f,TP=%f", sl, tp);
                        }
                        //--- modified and must exit from expert
                     }
                  }
               }
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
               {
                  if(iPosition.PriceOpen() > (iSymbol.Ask() - BreakEven * iSymbol.Point()))
                  {
                     double sl=NormalizeDouble(iPosition.PriceOpen() - iSprd, iSymbol.Digits());
                     double tp=iPosition.TakeProfit();
                     if(iPosition.StopLoss() > sl || iPosition.StopLoss()==0.0)
                     {
                        //--- modify position
                        if(iTrade.PositionModify(iSymbol.Name(),sl,tp))
                           printf("Short position by %s to be modified", iSymbol.Name());
                        else
                        {
                           printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
                           printf("Modify parameters : SL=%f,TP=%f", sl, tp);
                        }
                        //--- modified and must exit from expert
                     }
                  }
               }
            }
         }    
}

  • 2 2 Answers
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. crooked

      crooked

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

      The primer says “To
      ensure that you get fresh data about the position, it is recommended to call
      the positionselect function() immediately
      before applying for them. ” I’ll leave it, it won’t get any worse.

      In general, he got confused in three birches. For hedged accounts, you need to sort through the pickets. The correct code is below.

      void BreakEvenPoint()
      {
         if(BreakEven == 0)
            return;
         for(int i=PositionsTotal()-1; i>=0; i--)  // returns the number of current positions
            if(iPosition.SelectByIndex(i))         // selects the position by index for further access to its properties
               if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
               {
                  if(PositionSelect(iSymbol.Name())==true)
                  {
                     if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
                     {
                        if(iSymbol.Bid() > (iPosition.PriceOpen() + BreakEven * iSymbol.Point()))
                        {
                           double sl = NormalizeDouble(iPosition.PriceOpen() + iSprd, iSymbol.Digits());
                           double tp = iPosition.TakeProfit();
                           ulong  iTicket = PositionGetTicket(i);
                           if(iPosition.StopLoss() < sl || iPosition.StopLoss()==0.0)
                           {
                              //--- modify position
                              if(iTrade.PositionModify(iTicket, sl, tp))
                                 printf("Long position by %s to be modified", iSymbol.Name());
                              else
                              {
                                 printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
                                 printf("Modify parameters : SL=%f,TP=%f", sl, tp);
                              }
                              //--- modified and must exit from expert
                           }
                        }
                     }
                     if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
                     {
                        if(iPosition.PriceOpen() > (iSymbol.Ask() - BreakEven * iSymbol.Point()))
                        {
                           double sl=NormalizeDouble(iPosition.PriceOpen() - iSprd, iSymbol.Digits());
                           double tp=iPosition.TakeProfit();
                           ulong  iTicket = PositionGetTicket(i);
                           if(iPosition.StopLoss() > sl || iPosition.StopLoss()==0.0)
                           {
                              //--- modify position
                              if(iTrade.PositionModify(iTicket,sl,tp))
                                 printf("Short position by %s to be modified", iSymbol.Name());
                              else
                              {
                                 printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
                                 printf("Modify parameters : SL=%f,TP=%f", sl, tp);
                              }
                              //--- modified and must exit from expert
                           }
                        }
                     }
                  }
               }    
      }

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

      barabashkakvn

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

      Konstantin Ivanov:

      Hi.

      There is a breakeven transfer function breakevenpoint(). I open three positions, and when the first one is closed by take, this function is activated. that
      is, there are two positions left, but this function transfers the stop of only one position to breakeven, the second one remains with the old stop. It seems that
      everything is written correctly, but it does not work correctly. What is the error. I would appreciate a hint.

      The second time to select a position is unnecessary.

            if(iPosition.SelectByIndex(i))
               if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
                 {
                  if(PositionSelect(iSymbol.Name())==true) // there is an open position

      • 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 506
    • Answers 2k
    • Posts 5
    • Comments 0
    • Best Answers 0
    • Users 683
    • 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

    JeffreySer

    JeffreySer

    • 1 Question
    • 0 Answers
    Jerrynug

    Jerrynug

    • 1 Question
    • 0 Answers
    Leonardcig

    Leonardcig

    • 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