Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

value of a variable in strategy reuse in indicator

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

    value of a variable in strategy reuse in indicator

    hello everyone, i am currently stuck in my thinking process ;-) following problem:

    a variable is created in the strategy.cs and filled with a value. now the value of this variable is to be read and used in another .cs file (indicator). in the indicator i can create a new instance of my strategy and also access this variable. unfortunately, however, the value of the variable is not changed when it is changed in the strategy. what am i doing wrong?

    PHP Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SidiUnmanaged : Strategy
       {
        protected override void OnStateChange()
        {
           if (State == State.DataLoaded)
              StrategyId = this.Id;
         }
    
    [XmlIgnore, Browsable(false)]
    public long StrategyId
    {
    get
    {
    return this.strategyId;
    }
    set
    {
    if(value != this.strategyId)
    {
    this.strategyId = value;
    }
    }
    }
    }
    } 
    

    PHP Code:
    namespace NinjaTrader.NinjaScript.Indicators.MyIndicators
    {
        public class SidiHistoricalTrades : Indicator
       {   
         SidiUnmanaged sidiUnmanaged = new SidiUnmanaged();
    
         protected override void OnBarUpdate() 
         { 
            Print(sidiUnmanaged.StrategyId.ToString());
         }
       }
    } 
    


    is there actually no possibility to insert already formatted text here in the forum, or have i not found it yet?
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Greetings!

    is there actually no possibility to insert already formatted text here in the forum, or have i not found it yet ..

    a variable is created in the strategy.cs and filled with a value. now the value of this variable is to be read and used in another .cs file ..
    I don't have good links at the ready (the good NT support does) but here some quick weekend help.

    I believe two methods have been discussed to meet this need.
    1. Store the value to be share in a physical file on the hard drive .. might search for this .. posts exist in this forum and a streamwriter example zip exists in the Guide Learning References.
    2. Store the values to be shared in static fields or dictionaries or etc.. within an AddOn. I think Chelsea, Jim and Jesse have all posted good examples within the forum.

    HedgePlay

    Comment


      #3
      Originally posted by hedgeplay View Post
      Greetings!

      I don't have good links at the ready (the good NT support does) but here some quick weekend help.

      I believe two methods have been discussed to meet this need.
      1. Store the value to be share in a physical file on the hard drive .. might search for this .. posts exist in this forum and a streamwriter example zip exists in the Guide Learning References.
      2. Store the values to be shared in static fields or dictionaries or etc.. within an AddOn. I think Chelsea, Jim and Jesse have all posted good examples within the forum.

      HedgePlay
      Hihedgeplay, that was a good tip with the static field ;-) sometimes the search here in the forum is unfortunately a bit complicated and not always to be found with the right words.
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        Hello sidlercom80,

        The supported approach would be the other way arround. Call the indicator from the strategy, and add this to the chart with AddChartIndicator().
        The strategy can set values in the strategy from the strategy instance.

        The plotting from a NinjaScript Strategy reference sample demonstrates this.


        As hedgeplay has suggested, writing to a text file and reading from a text file with StreamWriter and StreamReader is an alternative, there may be special considerations if multiple script instances are attempting to read and write at the same time.
        Below are links to the reference samples.



        Using static class objects is possible with custom C# but would not be documented in the help guide. There are special considerations for these as well, such as with backtesting.
        Below is a link to an example of this.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          yes, normally it is done the other way round, but in this particular case i cannot call the indicator from the strategy because the indicator also closes when the strategy is closed via "CloseStrategy()". however, i need the data of the last trade and i cannot get this with the conventional method because everything is already closed when the data is delivered. the problem is solved with the static variant.

          sidlercom80
          NinjaTrader Ecosystem Vendor - Sidi Trading

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          51 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          127 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          69 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X