Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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");
            }
        }
    }
    Chris L.NinjaTrader Customer Service

    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:
        Chris L.NinjaTrader Customer Service

        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:



              Chris L.NinjaTrader Customer Service

              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 frankthearm, Today, 09:08 AM
                0 responses
                1 view
                0 likes
                Last Post frankthearm  
                Started by funk10101, Today, 08:14 AM
                3 responses
                4 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by samish18, Yesterday, 08:57 AM
                11 responses
                28 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by yertle, Today, 08:38 AM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by love2code2trade, Yesterday, 01:45 PM
                3 responses
                22 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Working...
                X