Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Detect when unRealized PnL cross bellow a certain amount

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Detect when unRealized PnL cross bellow a certain amount

    Hi guys, I am struggling to code this condition

    I want to set a condition when the unRealized cross bellow a certain amount,

    is there anyway to have this condition?​
    Thank you
    Last edited by onlinebusiness; 12-31-2022, 10:04 AM.

    #2
    Hello onlinebusiness,

    Thank you for your post.

    You can get the unrealized PnL for a strategy position with GetUnrealizedProfitLoss():


    One way you could get your strategy to detect if the unrealized PnL has gone above $100 then back down below $100 is to use bools. For example, you could have a condition that checks if it has gone above $100 and set a bool (pnlAbove in the snippet below) to true. Then, a separate condition will check that the bool is true as well as if the unrealized PnL is less than $100 and that would trigger your exit. Once the position is exited, you also need to reset the bool back to false so it is set up for the next position that your strategy opens. Here is an example snippet:

    protected override void OnBarUpdate()
    {
    // If not flat and our unrealized PnL has gone above $100, set pnlAbove to true
    if (Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) > 100)
    {
    pnlAbove = true;
    }
    // now check if not flat, if pnlAbove is true, and if the unrealized PnL is less than $100. if so, close the position
    if (Position.MarketPosition != MarketPosition.Flat && pnlAbove == true && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 100)
    {
    // put your exit method here, such as ExitLong() or ExitShort()
    pnlAbove = false; // setting pnlAbove back to false so it is ready to be checked for the next position
    }
    }

    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily

      Thanks for the guide, it did work. But it's only working on the Long trades. on the Short trades, however, it doesn't.

      Do you think of any reason that might cause that?
      Last edited by onlinebusiness; 12-31-2022, 10:05 AM.

      Comment


        #4
        Hello onlinebusiness,

        Thank you for your reply.

        I suspect you will need to set up two blocks of code, one that checks if the position is long and will exit long, and another that checks if the position is short and will exit short. My example was simplified to only check if the position was not flat, though it could be modified in the following way to be more specific about long vs. short:

        protected override void OnBarUpdate()
        {
        // If not flat and our unrealized PnL has gone above $100, set pnlAbove to true
        if (Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) > 100)
        {
        pnlAbove = true;
        }

        // now check if long, if pnlAbove is true, and if the unrealized PnL is less than $100. if so, close the position
        if (Position.MarketPosition == MarketPosition.Long && pnlAbove == true && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 100)
        {
        // put your exit long method here, such as ExitLong()
        pnlAbove = false; // setting pnlAbove back to false so it is ready to be checked for the next position
        }
        // else if the position is short, pnlAbove is true, and unrealized PnL is less than $100, close the position
        else if (Position.MarketPosition == MarketPosition.Short && pnlAbove == true && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 100)
        {
        // put your exit short method here, such as ExitShort()
        pnlAbove = false; // setting pnlAbove back to false so it is ready to be checked for the next position
        }​
        }

        Thank you for your patience. Please let us know if we may be of further assistance.

        Comment


          #5
          Found the problem, ignore the post

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          56 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          133 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X