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

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;
         }

    [
    XmlIgnoreBrowsable(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 kujista, Today, 06:23 AM
          4 responses
          14 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by traderqz, Yesterday, 09:06 AM
          2 responses
          16 views
          0 likes
          Last Post traderqz  
          Started by traderqz, Today, 12:06 AM
          3 responses
          6 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by RideMe, 04-07-2024, 04:54 PM
          5 responses
          28 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by f.saeidi, Today, 08:13 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X