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

float properties and Math.Min/Math.Max

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

    float properties and Math.Min/Math.Max

    Hi All,

    I am trying to add a float property to my strategy. Specifically I am
    declaring a variable called vProfitTakePercentage that will be added
    to my Initialize() method. My main issue is with the "set" in my
    public float VProfitTakePercentage property declaration.

    set { vProfitTakePercentage = value; }

    works fine however when I attempt to optimise the default value,
    the variable is fixed at 0.03. I want however a MIN and MAX just like
    the other Integers so that when I run the optimiser the best
    SetProfitTarget is found.


    Here are the main components of the code:

    #region Variables
    private float vProfitTakePercentage = 0.03f;
    #endregion

    protected override void Initialize()
    {
    SetProfitTarget(CalculationMode.Percent, vProfitTakePercentage); // 3%
    }

    #region Properties

    [Description("Profit Take Percentage")]
    [Category("Parameters")]
    public float VProfitTakePercentage
    {
    get { return vProfitTakePercentage; }
    //set { vProfitTakePercentage = value; } // set { vProfitTakePercentage = Math.Max(1f, value); }
    set { vProfitTakePercentage = Math.Min(Math.Max(0,value),1000); } //set { yOffset = Math.Min(Math.Max(0,value),1000); }
    }

    #endregion


    many thanks for any assistance.
    Last edited by sgwood; 10-28-2010, 03:28 AM.

    #2
    Welcome to our forums, please set up those properties to optimize as a double and then recheck if you can optimize them as expected.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      thanks for the tip on using double although I still don't understand
      why float isn't supported.

      One more question I have is why my vProfitTakePercentage variable is not being optimised.

      Yes, when I select Optmise it now lets me select the MIN and MAX values
      for my vProfitTakePercentage variable however I get no change in the Optimise results.

      Could it be that only variables in the OnBarUpdate() method are optimised?

      Here is my complete strategy if that helps:

      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Stochastic Crossover
      /// </summary>
      [Description("Stochastic Crossover")]
      public class Stochastic20101009 : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int v1StochD = 5;
      private int v2StochK = 9;
      private int v3Smooth = 5;

      private int vStartLogDate = 20101008; // Trades to log from
      // User defined variables (add any user defined variables below)

      private double vProfitTakePercentage = 0.03d;

      private string vSymbolName;
      #endregion



      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {

      Add(Stochastics(v1StochD,v2StochK,v3Smooth));
      Add(MACD(12, 26, 9));

      CalculateOnBarClose = true;

      SetProfitTarget(CalculationMode.Percent, vProfitTakePercentage); // 3%

      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {

      if (CrossAbove(Stochastics(V1StochD,V2StochK,V3Smooth ).K, Stochastics(V1StochD,V2StochK,V3Smooth).D, 1) )
      {
      DrawDiamond(CurrentBar.ToString()+"Buy", true, 0, Low[0] + TickSize, Color.Green);

      EnterLong(1000, "EnterLong");

      if ( ToDay(Time[0]) > VStartLogDate ) {
      Print(Instrument.FullName + ";" + ToDay(Time[0]) + ";" + "Long Position Entered" + ";" + "V1StochD: " + V1StochD + ";" + "V2StochK: " + V2StochK + ";" + "V3Smooth: " + V3Smooth );
      }

      }


      #region Properties

      [Description("Stochastic D")]
      [Category("Parameters")]
      public int V1StochD
      {
      get { return v1StochD; }
      set { v1StochD = Math.Max(1, value); }
      }

      [Description("Stochastic K")]
      [Category("Parameters")]
      public int V2StochK
      {
      get { return v2StochK; }
      set { v2StochK = Math.Max(1, value); }
      }

      [Description("Stochastic Smooth")]
      [Category("Parameters")]
      public int V3Smooth
      {
      get { return v3Smooth; }
      set { v3Smooth = Math.Max(1, value); }
      }

      [Description("From which date should trades be logged")]
      [Category("Parameters")]
      public int VStartLogDate
      {
      get { return vStartLogDate; }
      set { vStartLogDate = Math.Max(1, value); }
      }

      [Description("Profit Take Percentage")]
      [Category("Parameters")]
      public double VProfitTakePercentage
      {
      get { return vProfitTakePercentage; }
      //set { vProfitTakePercentage = value; } // set { vProfitTakePercentage = Math.Max(1f, value); }
      set { vProfitTakePercentage = Math.Max(0.3d, value); } //set { yOffset = Math.Min(Math.Max(0,value),1000); }
      }

      #endregion
      }
      }

      Comment


        #4
        Great the doubles did it - you would need to refer to the public (capitalized normally), not the private property -

        SetProfitTarget(CalculationMode.Percent, VProfitTakePercentage); // 3%

        If you use the snippet above, it should optimize the target value.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        6 responses
        24 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Waxavi, Today, 02:00 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X