Announcement

Collapse
No announcement yet.

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.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    62 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X