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

Simple Indicator that alerts with every new bar

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

    #16
    Thank you

    I'll give it a try right now.

    Thanks again for all your help

    Travis

    Comment


      #17
      Stepping through code

      Are there any tools available that will allow you to step through your code line by line?

      I'm trying to step through the IF statements to test / learn how they are working.

      Comment


        #18
        Hello,

        You can debug via Visual Studio. Also, the simpler way to debug is to add print statements.





        Let me know if I can be of further assistance.

        Comment


          #19
          New Code

          Below is a copy/paste of my new Code. It still does not seem to be working. I'm the sure the error is probably very glaring LoL.

          *****Copy/Paste Start

          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Gui.Chart;
          #endregion

          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          [Description("This Indicator will send email alerts just before every new bar is created.")]
          public class RangeBarAlerts : Indicator
          {
          #region Variables
          // Wizard generated variables
          // User defined variables (add any user defined variables below)
          bool alerted = false;
          double RangeBarSetting = 10;
          double PercentageSetting = 9;

          #endregion

          /// <summary>
          /// This method is used to configure the indicator and is called once before any bar data is loaded.
          /// </summary>
          protected override void Initialize()
          {
          CalculateOnBarClose = false;
          Overlay = false;
          PriceTypeSupported = false;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (Close[0] >= ((RangeBarSetting - PercentageSetting) + Low[0]) && alerted == false)
          {
          Print("Long");
          // send alert
          SendMail("[email protected]", "[email protected]", "New Bar Alert", "New bar just opened");
          alerted = true;
          }
          if (Close[0] <= ((RangeBarSetting - PercentageSetting) - High[0]) && alerted == false)
          {
          Print("Short");
          // send alert
          SendMail("[email protected]", "[email protected]", "New Bar Alert", "New bar just opened");
          alerted = true;
          }

          if (FirstTickOfBar)
          alerted = false;
          }

          #region Properties

          #endregion
          }
          }

          #region NinjaScript generated code. Neither change nor remove.
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          public partial class Indicator : IndicatorBase
          {
          private RangeBarAlerts[] cacheRangeBarAlerts = null;

          private static RangeBarAlerts checkRangeBarAlerts = new RangeBarAlerts();

          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          public RangeBarAlerts RangeBarAlerts()
          {
          return RangeBarAlerts(Input);
          }

          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          public RangeBarAlerts RangeBarAlerts(Data.IDataSeries input)
          {

          if (cacheRangeBarAlerts != null)
          for (int idx = 0; idx < cacheRangeBarAlerts.Length; idx++)
          if (cacheRangeBarAlerts[idx].EqualsInput(input))
          return cacheRangeBarAlerts[idx];

          RangeBarAlerts indicator = new RangeBarAlerts();
          indicator.BarsRequired = BarsRequired;
          indicator.CalculateOnBarClose = CalculateOnBarClose;
          indicator.Input = input;
          indicator.SetUp();

          RangeBarAlerts[] tmp = new RangeBarAlerts[cacheRangeBarAlerts == null ? 1 : cacheRangeBarAlerts.Length + 1];
          if (cacheRangeBarAlerts != null)
          cacheRangeBarAlerts.CopyTo(tmp, 0);
          tmp[tmp.Length - 1] = indicator;
          cacheRangeBarAlerts = tmp;
          Indicators.Add(indicator);

          return indicator;
          }

          }
          }

          // This namespace holds all market analyzer column definitions and is required. Do not change it.
          namespace NinjaTrader.MarketAnalyzer
          {
          public partial class Column : ColumnBase
          {
          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.RangeBarAlerts RangeBarAlerts()
          {
          return _indicator.RangeBarAlerts(Input);
          }

          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          public Indicator.RangeBarAlerts RangeBarAlerts(Data.IDataSeries input)
          {
          return _indicator.RangeBarAlerts(input);
          }

          }
          }

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          public partial class Strategy : StrategyBase
          {
          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.RangeBarAlerts RangeBarAlerts()
          {
          return _indicator.RangeBarAlerts(Input);
          }

          /// <summary>
          /// This Indicator will send email alerts just before every new bar is created.
          /// </summary>
          /// <returns></returns>
          public Indicator.RangeBarAlerts RangeBarAlerts(Data.IDataSeries input)
          {
          if (InInitialize && input == null)
          throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

          return _indicator.RangeBarAlerts(input);
          }

          }
          }
          #endregion

          ***Copy/Paste End

          Any help from anyone out there would be great.

          Thanks
          Last edited by Aithin; 02-08-2011, 10:21 AM.

          Comment


            #20
            Hello,

            Whats is this:

            (RangeBarSetting - PercentageSetting) + Low[0]


            Not sure this is correct for your formula. Are you getting Print statements to the output window?

            Comment


              #21
              (RangeBarSetting - PercentageSetting) + Low[0]

              is to get the 90% of the Total Range Bar Setting.

              For example , for testing I'm using 10 Range Bars. So the formula is ...
              (10 - 9) + Low of Current Bar
              If the price goes beyond this point it should trigger the alert as 90% of the bar has been created.

              However, I did have my logic wrong , below is the updated lines of code.
              if (Close[0] <= (Low[0] + (RangeBarSetting - PercentageSetting)) && alerted == false)
              if (Close[0] >= (High[0] - (RangeBarSetting - PercentageSetting)) && alerted == false)

              Comment


                #22
                Originally posted by Aithin View Post
                Does the intrabar update and alerts work with Range Bars? If so can I use something that would be

                if currentprice == alertlevel then send the alert?

                (current price obviously being plain english and not the real command)

                Thanks
                You are going to need 2 trigger levels: 1 each for high and low.

                The most efficient way to use your triggers would be to define a percentage level and then trigger off that offset from low or high as the case may be.

                Code:
                if (BarsPeriod.Id == PeriodType.Range)
                    {
                        double _dblBarsRange =  BarsPeriod.Value;
                    }
                should grab you the value of the bars range, so that you do not need to specify it in the code.

                Code:
                double _dblUpperTrigger = Low[0] + dblTriggerPercent * _dblBarsRange  * TickSize;
                double _dblLowerTrigger = High[0] - dblTriggerPercent * ._dblBarsRange * TickSize;
                (where dblTriggerPercent is the property that is set from the config GUI) should give you your trigger levels.

                Code:
                bool boolUpperTriggerViolated = Close[0] > _dblUpperTrigger;
                bool boolLowerTriggerViolated = Close[0] < _dblLowerTrigger;
                would be your mail conditions.

                For Range bars, this last condition will be identically true for all historical bars when dblTriggerPercent < 100 %, so you might want to escape the block with a if (!Historical) condition.

                If used that way, you are going to have to reset the bool triggers to false on FirstTickOfBar.
                Last edited by koganam; 01-26-2011, 12:57 AM.

                Comment


                  #23
                  Thanks everyone, Though still need a little help

                  I decided, for simplicities sake, to go with the first suggestion. which is ...

                  if (FirstTickOfBar) SendMail("[email protected]", "[email protected], "EURUSD - New Bar Alert", "New bar just opened");

                  below is a copy paste of the code ....

                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Gui.Chart;
                  #endregion

                  // This namespace holds all indicators and is required. Do not change it.
                  namespace NinjaTrader.Indicator
                  {
                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  [Description("Email Alerts at the beginning of every new bar")]
                  public class RangeBarAlert_eurusd : Indicator
                  {
                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)
                  #endregion

                  /// <summary>
                  /// This method is used to configure the indicator and is called once before any bar data is loaded.
                  /// </summary>
                  protected override void Initialize()
                  {
                  CalculateOnBarClose = true;
                  Overlay = false;
                  PriceTypeSupported = false;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Use this method for calculating your indicator values. Assign a value to each
                  // plot below by replacing 'Close[0]' with your own formula.
                  if (FirstTickOfBar) SendMail("[email protected]", "[email protected]", "EURUSD - New Bar Alert", "New bar just opened");
                  }

                  #region Properties

                  #endregion
                  }
                  }

                  #region NinjaScript generated code. Neither change nor remove.
                  // This namespace holds all indicators and is required. Do not change it.
                  namespace NinjaTrader.Indicator
                  {
                  public partial class Indicator : IndicatorBase
                  {
                  private RangeBarAlert_eurusd[] cacheRangeBarAlert_eurusd = null;

                  private static RangeBarAlert_eurusd checkRangeBarAlert_eurusd = new RangeBarAlert_eurusd();

                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  public RangeBarAlert_eurusd RangeBarAlert_eurusd()
                  {
                  return RangeBarAlert_eurusd(Input);
                  }

                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  public RangeBarAlert_eurusd RangeBarAlert_eurusd(Data.IDataSeries input)
                  {

                  if (cacheRangeBarAlert_eurusd != null)
                  for (int idx = 0; idx < cacheRangeBarAlert_eurusd.Length; idx++)
                  if (cacheRangeBarAlert_eurusd[idx].EqualsInput(input))
                  return cacheRangeBarAlert_eurusd[idx];

                  RangeBarAlert_eurusd indicator = new RangeBarAlert_eurusd();
                  indicator.BarsRequired = BarsRequired;
                  indicator.CalculateOnBarClose = CalculateOnBarClose;
                  indicator.Input = input;
                  indicator.SetUp();

                  RangeBarAlert_eurusd[] tmp = new RangeBarAlert_eurusd[cacheRangeBarAlert_eurusd == null ? 1 : cacheRangeBarAlert_eurusd.Length + 1];
                  if (cacheRangeBarAlert_eurusd != null)
                  cacheRangeBarAlert_eurusd.CopyTo(tmp, 0);
                  tmp[tmp.Length - 1] = indicator;
                  cacheRangeBarAlert_eurusd = tmp;
                  Indicators.Add(indicator);

                  return indicator;
                  }

                  }
                  }

                  // This namespace holds all market analyzer column definitions and is required. Do not change it.
                  namespace NinjaTrader.MarketAnalyzer
                  {
                  public partial class Column : ColumnBase
                  {
                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.RangeBarAlert_eurusd RangeBarAlert_eurusd()
                  {
                  return _indicator.RangeBarAlert_eurusd(Input);
                  }

                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.RangeBarAlert_eurusd RangeBarAlert_eurusd(Data.IDataSeries input)
                  {
                  return _indicator.RangeBarAlert_eurusd(input);
                  }

                  }
                  }

                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  public partial class Strategy : StrategyBase
                  {
                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.RangeBarAlert_eurusd RangeBarAlert_eurusd()
                  {
                  return _indicator.RangeBarAlert_eurusd(Input);
                  }

                  /// <summary>
                  /// Email Alerts at the beginning of every new bar
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.RangeBarAlert_eurusd RangeBarAlert_eurusd(Data.IDataSeries input)
                  {
                  if (InInitialize && input == null)
                  throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

                  return _indicator.RangeBarAlert_eurusd(input);
                  }

                  }
                  }
                  #endregion

                  ****end paste of code


                  This really doesn't seem like it should be this difficult. However, last night there should have been two emails sent but I did not get any. I'm getting a little frustrated, can anyone see why this isn't working?

                  Thank you very much , and thank you very much for the help thus far!
                  Last edited by Aithin; 02-08-2011, 10:19 AM.

                  Comment


                    #24
                    alerts are sent ... sometimes

                    It seems to be sending alerts with lower range bar settings and not for the longer settings. For example I get alerts with a 25 range chart, but not with a 100 range chart. Any ideas why that could be?

                    Thanks

                    Comment


                      #25
                      Originally posted by Aithin View Post
                      It seems to be sending alerts with lower range bar settings and not for the longer settings. For example I get alerts with a 25 range chart, but not with a 100 range chart. Any ideas why that could be?

                      Thanks
                      This may be a silly question, but are you sure that you actually have any 100 range bars?

                      Comment


                        #26
                        I'm not sure I follow, kog? Are you asking if I'm sure there has been a new bar formed? If so, yes I'm sure I keep a verticle line on the current bar, that way I can tell easily when new bars form, and whether or not the indicator is working.

                        Thanks though for the input ... sometimes the problem really is that simple

                        Comment


                          #27
                          Hello,

                          There is no reason why it would work on a 25 R and not a 100 R.

                          Please note that this will only send a email on a new bar live that NinjaTrader sees. This will not run on historical.

                          This will work without issue on a 100 R bar chart.

                          Let me know if I can be of further assistance.

                          Comment


                            #28
                            Hmm, so there are no errors in the code?

                            This is how I'm testing it ...
                            start off with 5 range bars (in order to get alerts more often, in the interest of testing)
                            Once it appears to be working as intended I bump up to a 25 range bar chart
                            Once that's working I bump it up to 50 range, 75 range, etc...

                            I seem to get alerts consistently up to about 50 range, then nothing.

                            I have a dedicated computer running only ninja trader, 24 hours a day... so its not that i'm looking for historical alerts.

                            This whole thing is very perplexing

                            If I can provide any other info as to how i'm testing this, this ask ... I really want to get this working.

                            Comment


                              #29
                              Hello,

                              No reason why this shouldnt work.

                              In this case please add Print() statements to debug the code.

                              Add a print startment in OnBarUpdate() and add a Print() Statement in the if( FirstTickOfBar ) check to make sure the is executing. To see if its the email thats somehow not making it or the code runnin code.



                              I look forward to assisting you further.

                              Comment


                                #30
                                Thanks, I put the print() commands in both places you asked me to.

                                I'll let you know how it works.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Christopher_R, Today, 12:29 AM
                                0 responses
                                9 views
                                0 likes
                                Last Post Christopher_R  
                                Started by sidlercom80, 10-28-2023, 08:49 AM
                                166 responses
                                2,235 views
                                0 likes
                                Last Post sidlercom80  
                                Started by thread, Yesterday, 11:58 PM
                                0 responses
                                3 views
                                0 likes
                                Last Post thread
                                by thread
                                 
                                Started by jclose, Yesterday, 09:37 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post jclose
                                by jclose
                                 
                                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                                10 responses
                                1,415 views
                                0 likes
                                Last Post Traderontheroad  
                                Working...
                                X