Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

User defined values reset to 0 after NinjaTrader closes

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

    User defined values reset to 0 after NinjaTrader closes

    Hi,
    I have this custom made indicator, that is working as needed:
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class EMAX2Alert : Indicator
        {
    private EMA ema20;
    private EMA ema50;
    private EMA ema200;
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    public int EMAPeriod1 { get; set; }
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    public int EMAPeriod2 { get; set; }
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    public int EMAPeriod3 { get; set; }
    
            [NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Start Time", GroupName = "Trading Period 1", Order = 0)]
            public TimeSpan TradingPeriodStartTime1 { get; set; }
    
            [NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "End Time", GroupName = "Trading Period 1", Order = 1)]
            public TimeSpan TradingPeriodEndTime1 { get; set; }
    
            [NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Start Time", GroupName = "Trading Period 2", Order = 0)]
            public TimeSpan TradingPeriodStartTime2 { get; set; }
    
            [NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "End Time", GroupName = "Trading Period 2", Order = 1)]
            public TimeSpan TradingPeriodEndTime2 { get; set; }
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name = "EMA X2 Alert";
                    IsOverlay = true;
                    EMAPeriod1 = 20;
                    EMAPeriod2 = 50;
                    EMAPeriod3 = 200;
                    TradingPeriodStartTime1 = new TimeSpan(3, 00, 0);
                    TradingPeriodEndTime1 = new TimeSpan(8, 0, 0);
                    TradingPeriodStartTime2 = new TimeSpan(12, 00, 0);
                    TradingPeriodEndTime2 = new TimeSpan(14, 00, 0);
    
        }
        ema20 = EMA(EMAPeriod1);
        ema50 = EMA(EMAPeriod2);
        ema200 = EMA(EMAPeriod3);
    
    }
    
    protected override void OnBarUpdate()
    {
    
    
    
            if (Time[0].TimeOfDay >= new TimeSpan(3, 00, 0) && Time[0].TimeOfDay <= new TimeSpan(17, 00, 0))
          // OVERALL TRADING TIMES with paramter user defined:  if (Time[0].TimeOfDay >= StartTime1 && Time[0].TimeOfDay <= EndTime1)
    
        {
            if (Time[0].TimeOfDay >= TradingPeriodStartTime1 && Time[0].TimeOfDay <= TradingPeriodEndTime1 ||
                Time[0].TimeOfDay >= TradingPeriodStartTime2 && Time[0].TimeOfDay <= TradingPeriodEndTime2)
            {
                if(CurrentBar == Bars.Count -2 && ((CrossAbove(ema20, ema50, 1) && ema20[0] > ema200[0] && ema50[0] > ema200[0]) ||
                    (CrossBelow(ema20, ema50, 1) && ema20[0] < ema200[0] && ema50[0] < ema200[0]) ||
                    (CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0]) ||
                    (CrossBelow(ema50, ema200, 1) && ema20[0] < ema50[0])))
                {
                    Print("EMA X2 on:  " + Instrument.FullName + "  at: " + Time[0]);
                    Alert("EMA X2", Priority.High, "EMA X2", NinjaTrader.Core.Globals.InstallDir+@"\sounds\AlertX2.wav", 20, Brushes.SpringGreen, Brushes.Black);
                    System.Windows.Forms.MessageBox.Show("Get ready to make some MONEY! EMAs just did the X2!                 " + Instrument.FullName + "  at  " + Time[0].ToString("HH:mm"));
                }
            }
            else if (CurrentBar == Bars.Count -2 && ((CrossAbove(ema20, ema50, 1) && ema20[0] > ema200[0] && ema50[0] > ema200[0]) ||
                    (CrossBelow(ema20, ema50, 1) && ema20[0] < ema200[0] && ema50[0] < ema200[0]) ||
                    (CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0]) ||
                    (CrossBelow(ema50, ema200, 1) && ema20[0] < ema50[0])))
            {
                Print("EMA X2 off period:  " + Instrument.FullName + "  at: " + Time[0]);
                Alert("EMA X2 off", Priority.High, "EMA X2 off", NinjaTrader.Core.Globals.InstallDir+@"\sounds\AlertX2off.wav", 20, Brushes.Gainsboro, Brushes.Black);
                System.Windows.Forms.MessageBox.Show("EMAs X2 off trading period:          " + Instrument.FullName + "  at  " + Time[0].ToString("HH:mm"));        
    
            }
          }
        }
      }
    }​
    But the values I set in the indiactor Properties section Trading Period values (Start Time and End Time) are always deleted and set to 00:00:00 when I close NinjaTrader. That happens although I save any changes made to the workspace. I hope there is just a simple syntax error or part of the code needed to store the values even after shut down that is not present.
    Anything else works with no issues.
    Hope anyone can help.
    Thanks
    Peter



    #2
    I think your problem is solved here.

    I found that link by doing this search.

    (Google is your friend. That is, Google
    has indexed the NinjaTrader forums quite
    well -- use prefix 'NinjaTrader' in the search
    box and investigate the results.)

    Just my 2˘.

    Last edited by bltdavid; 02-19-2023, 11:21 AM.

    Comment


      #3
      Btw, telling you to use Google is not condescending.

      It's actually a very good tip.

      Especially useful when you get stuck on something
      late at night around 2am and no one is around to help.

      I'm just saying ...

      Using Google could save your bacon.

      Comment


        #4
        Hello Pete,

        Thanks for your post.

        bltdavid is correct. The forum thread that bltdavid linked to (also linked below) could be viewed to see a solution for this behavior. You would need to serialize the property as seen in the forum thread linked below.

        I've added TimeSpan properties to my strategy to allow users to control when to start, pause, resume and finish trading. The values are interpreted by the strategy as a delta from the session start or session close. I was previously using a number of minutes but that is difficult to work with as a user of the script. The


        Let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thanks a lot bltdavid and Brandon!
          I was able to fix the issue!
          bltdavid you are right, google is very helpful, if you know the right phrase to search for

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          561 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          325 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X