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 kujista, Today, 05:44 AM
    0 responses
    5 views
    0 likes
    Last Post kujista
    by kujista
     
    Started by ZenCortexCLICK, Today, 04:58 AM
    0 responses
    5 views
    0 likes
    Last Post ZenCortexCLICK  
    Started by sidlercom80, 10-28-2023, 08:49 AM
    172 responses
    2,281 views
    0 likes
    Last Post sidlercom80  
    Started by Irukandji, Yesterday, 02:53 AM
    2 responses
    18 views
    0 likes
    Last Post Irukandji  
    Started by adeelshahzad, Today, 03:54 AM
    0 responses
    8 views
    0 likes
    Last Post adeelshahzad  
    Working...
    X