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

Confusion on why "already contains a definition" gets thrown out randomly

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

    Confusion on why "already contains a definition" gets thrown out randomly

    hey there,

    I can't understand why this is happening for one and not for the other. I can set trail percentages and profit percentages, and have them execute the strategy perfectly. However, for optimization purposes, I want to be able to change those around, and thus I have to create #region Properties entries for both. It works for everything, except for profit target, however there don't seem to be any differences, aside from different name.

    public class PersonalStrat : Strategy
    {
    private double stopLossPct = 0.020; // Default setting for StopLossPct
    private double profitTargetPct = 0.020; // 5% profit target expected

    .... and so on with strategy...

    #region Properties

    [Range(0.01, double.MaxValue)]
    [NinjaScriptProperty]
    [Display(Name="ProfitTargetPct", Description="Define how much of profit to stop at", Order=6, GroupName="Parameters")]
    public double profitTargetPct
    {
    get { return profitTargetPct; }
    set { profitTargetPct = Math.Max(0.010, value); }
    }

    [Range(0.001, double.MaxValue)]
    [NinjaScriptProperty]
    [Display(Name="StopLossPct", Description="Define stop loss percent", Order=5, GroupName="Parameters")]
    public double StopLossPct
    {
    get { return stopLossPct; }
    set { stopLossPct = Math.Max(0.010, value); }
    }... and so on...

    If I remove this bit below, the strategy works as intended with a 2% profit target for executions. However then I cannot optimize it. If I leave it in, it's not showing up in parameters, as I'm getting "The type of 'NinjaTrader.NinjaScript.Strategies.PersonalStrat' already contains a definition for 'profitTargetPct') - and it points to THE LINE SHOWN BELOW...But then why I don't get the same issue for stopLossPct and others.
    "[Range(0.01, double.MaxValue)]
    [NinjaScriptProperty]
    [Display(Name="ProfitTargetPct", Description="Define how much of profit to stop at", Order=6, GroupName="Parameters")]
    public double profitTargetPct <<<--------- THIS LINE
    {
    get { return profitTargetPct; }
    set { profitTargetPct = Math.Max(0.010, value); }
    }"

    Thank you for any ideas

    #2
    Hello lmatiukas,

    Thank you for writing in.

    You already have declared a private variable that is called profitTargetPct.

    In your code, you're trying to declare a public variable that is called the exact same thing, profitTargetPct.

    You'll need to capitalize the first letter P. ProfitTargetPct.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      hey there thanks,

      So I guess I can just say below, and not define it again in regions. However, odd thing then, how come if I remove this whole part below, and have profitTargetPct set in stone at 0.02, I don't get the same fault for stopLossPct . I want to understand the reasons behind why one thing is throwing an issue, and the other one is not. If you could shed some light. To me both instances seem to be defined twice then, but the other one has no problems with it compiling and running:

      #region Properties

      [Range(0.01, double.MaxValue)]
      [NinjaScriptProperty]
      [Display(Name="ProfitTargetPct", Description="Define how much of profit to stop at", Order=6, GroupName="Parameters")]
      profitTargetPct
      {
      get { return profitTargetPct; }
      set { profitTargetPct = Math.Max(0.010, value); }
      }

      Comment


        #4
        Hello lmatiukas,

        In your original post, your public property is StopLossPct (capital S) and your private variable is stopLossPct (lower case S). This is why you did not get a compile error. C# is case sensitive and sees these two variables as different variables.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          hey there,

          thanks so much! I noticed it right after I replied, after your initial explanation - DUH!

          Sorry to bother with mediocre issues.

          Either way, thank goodness I took Java for so many years, the more I read into C#, i'm noticing similarities, and the panic is less =)

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ghoul, Today, 06:02 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          44 views
          0 likes
          Last Post jeronymite  
          Started by Barry Milan, Yesterday, 10:35 PM
          7 responses
          20 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by AttiM, 02-14-2024, 05:20 PM
          10 responses
          179 views
          0 likes
          Last Post jeronymite  
          Started by DanielSanMartin, Yesterday, 02:37 PM
          2 responses
          13 views
          0 likes
          Last Post DanielSanMartin  
          Working...
          X