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

Dynamically Save NinjaScriptProperty

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

    Dynamically Save NinjaScriptProperty

    Hey team, had a question about dynamically saving a ninjascript property.

    Say you have a random number, and any time you run a backtest via the strategy analyzer you want that number to randomly generate but save for the next backtest. How would you accomplish this?

    I have a NinjaScriptProperty of string and by default it's empty "". When I run a backtest it populates and when I print it the NinjaTrader output shows it but the UI doesn't update nor does it save for the next backtest / optimization. I want to be able to run backtest and optimization where the initial iteration the number is randomly generated but any subsequent backtests I want that randomly generated number to save and persist so I can save it as a template or run it out of sample.

    Below is a simple example :

    Code:
    #region Using declarations
    using System;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {[INDENT]public class RandomPropertyExample : Strategy
    {[/INDENT][INDENT=2]private System.Random rnd = new Random();[/INDENT][INDENT]protected override void OnStateChange()
    {[/INDENT][INDENT=2]if (State == State.SetDefaults)
    {[/INDENT][INDENT=3]randomOutput = "";[/INDENT][INDENT=2]}
    else if (State == State.Configure)
    {
    if (randomOutput == "")[/INDENT][INDENT=3]randomOutput = rnd.Next(1, 7).ToString();[/INDENT][INDENT=2][SIZE=12px][B]Print(randomOutput); //Will print first time but will not save with any subsequent backtests and UI will not update[/B][/SIZE][/INDENT][INDENT]}[/INDENT][INDENT]
    protected override void OnBarUpdate() {}
    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Signal Output", GroupName = "Time Series", Order = 0)]
    public string randomOutput
    { get; set; }
    }[/INDENT]
     
     }

    #2
    Hello JakeOfSpades,

    You could look into using static modifier on the variable which is a C# concept. You can learn about that concept in the following MSDN link: https://docs.microsoft.com/en-us/dot...eywords/static

    The use case you described would fall in line with how static modifier works to make a property at the classes type level rather than at the instance level. Static is not generally used in NinjaScript as you don't generally want to retain values across instances.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by samish18, Today, 01:01 PM
    0 responses
    4 views
    0 likes
    Last Post samish18  
    Started by WHICKED, Today, 12:56 PM
    0 responses
    7 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Spiderbird, Today, 12:15 PM
    2 responses
    11 views
    0 likes
    Last Post Spiderbird  
    Started by WHICKED, Today, 12:45 PM
    0 responses
    7 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by FrazMann, Today, 11:21 AM
    2 responses
    8 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X