Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Percent Loss Over N Days

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

    Percent Loss Over N Days

    Hi All,

    I'm trying to code a strategy that uses the NBarsDown to specify N down days and then determines the percent loss over those days to execute an exit. I got the NBarsDown to work the way I want, but not sure how to get the percent loss over those days.Here is what I have.

    if(Position.MarketPosition == MarketPosition.Long);
    double value = NBarsDown(DaysOfLoss, true, true, true)[0];
    if (value == 1)
    //Print("Signal");
    {
    if(Position.GetUnrealizedProfitLoss(PerformanceUni t.Percent, Close[0]) <= some%);
    {
    ExitLong("Exit", "Enter Long");
    }
    }



























    What is the best way to get the pecent loss over the specified days (I'm using DaysOfLoss as my parameter)? The GetUnrealizedProfitLoss is not working the way I expected. Is there a another/simper way to get this?

    Thanks,
    Lee


    #2
    Hi lee612801, thanks for your note.

    This part of code is not going anything because there is a terminating ';' at the end of the IF statement:

    if(Position.MarketPosition == MarketPosition.Long);


    All of the code after this will run if you are long or not, so this is possibly the cause of the issues. If you want the code below it to run based on if the position is long, put brackets around it:

    Code:
    if(Position.MarketPosition == MarketPosition.Long)
    {
        double value = NBarsDown(DaysOfLoss, true, true, true)[0];
        if (value == 1)
        {
            if(Position.GetUnrealizedProfitLoss(PerformanceUni t.Percent, Close[0]) <= some%);
            {
                ExitLong("Exit", "Enter Long");
            }
        }
    }

    Comment


      #3
      Hi Chris,

      Thanks for the Reply. I will give that a try.

      Is the GetUnrealizedProfitLoss the best method to determine the loss over the specified days? Is the current code calculating the loss over the days I want (DaysOfLoss) or something else?

      Thanks,
      Lee

      Comment


        #4
        Hi Lee, thanks for your reply.

        GetUnrealizedProfitLoss will only report a value if the strategy is in an open position. If you need to get historical results of trades made in the past see the SystemPerformance class:

        Comment


          #5
          Ok, sounds like this will work. Thanks for input.

          Can I use a Stop Loss command in place of the Exit Long in my code or will that not work?

          Thanks,
          Lee

          Comment


            #6
            Hi Chris,

            I was able to get most of what I wanted working. The one issue I am having is with a parameter. Here is what I have.

            if(Position.MarketPosition == MarketPosition.Long)
            {
            double value = NBarsDown(DaysOfLoss, false, false, false)[0];
            if (value == 1)
            {
            if(((Position.GetUnrealizedProfitLoss(PerformanceU nit.Percent, Close[0])) - (Position.GetUnrealizedProfitLoss(PerformanceUnit. Percent, Close[DaysOfLoss]))) <= DaysOfLossPct)
            {
            ExitLong("Exit", "Enter Long");
            }
            }
            }

            Where DaysOfLoss is an integer and DaysOfLossPct is a double value. If I hard code in a value (like -0.05) for DaysOfLossPct on the GetUnrealizedProfitLoss line, the code works as expected. But as soon as I use the parameter DaysOfLossPct within the code, it does not work correctly and gives me different results.

            What do you think is going on here? I have also included below the property for the parameter DaysOfLossPct which I have set at -0.05.

            #region Parameters
            DaysOfLoss = 3;
            DaysOfLossPct = -0.05;
            #endregion


            [Range(double.MinValue, double.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Loss Percentage Over # of Days", GroupName = "NinjaScriptParameters", Order = 2)]
            public double DaysOfLossPct
            { get; set; }
            Last edited by lee612801; 03-13-2020, 11:59 AM.

            Comment


              #7
              Hi lee612801, thanks for your reply.

              Unfortunately, it's out of the scope of support I can provide to debug custom code. Did you try using the Print statement to see what the values of these two calculations are at run time? The Print statement is the number 1 tool used to debug NinjaScripts:



              Comment


                #8
                Hi Chris,

                No worries, I figured it out. Of course it was user error and the code is working as expected. You beat me to the reponse, but thanks for all the help!

                Regards,
                Lee

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                63 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                139 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 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
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X