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

My First Indicator [HELP:-)]

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

    My First Indicator [HELP:-)]

    This is working fine but I'd appreciate a quick review only because while I was doing this a message appeared a couple time when I tried compiling that read something about possibly crashing the software. It finally compiled and is working but I don't want any surprises.

    It's just a simple vol alert with sound....Thanks..V.


    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    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>
    /// Alerts you when there is a volume spike
    /// </summary>
    [Description("Alerts you when there is a volume spike")]
    public class HighVolAlert : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int MyInput0 = 1; // Default setting for Volume Alert
    // 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 = true;
    PriceTypeSupported = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;

    if (Volume[0] > MyInput0)
    DrawDiamond("HighVolume" + CurrentBar, 0, Low[0] - TickSize, Color.Gold);
    if (Volume[0] > MyInput0)
    PlaySound(@"C:\Program Files\Kirby Alarm\Trade Alerts\High Volume Alert.wav.");
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Plot0
    {
    get { return Values[0]; }
    }

    [Description("")]
    [Category("Parameters")]
    public int Vol0
    {
    get { return MyInput0; }
    set { MyInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Vinny, OnBarUpdata() is called on every incoming tick - so once the Volume on the bar goes over your treshhold your sound would go off on every tick remaining for that bar .... probably not what you want ...

    Two possible solutions i could think of
    - Use Alert(): Alert("VolAlert", NinjaTrader.Cbi.Priority.High, "High Volume", UpSound, 60, Color.Gray, Color.Lime); .... which limits this type of alert to one per 60 seconds as in this example .... but not really efficient
    - Build some logic around it which tests for the first tick of the new bar and then check the Volume of the previous bar .... or if you want it sooner then raise a flag once you have a positive condition and stop checking for the remainder of the bar

    Is your head spinning yet?


    On another note ... in your Properties Vol0 is the result of the user input which you should use in the code to compare Volume against .... not against MyInput0 which is the default

    public int Vol0
    {
    get { return MyInput0; }
    set { MyInput0 = Math.Max(1, value); }
    }

    hope i´m making sense .... not my native lang

    Comment


      #3
      Thank you so much Toulouse!

      Yes, head is spinning but I followed everything.

      Question:

      Do I just change the MyInput0 below to Vol0 in both spots and no where else?

      public int Vol0
      {
      get { return MyInput0; }
      set { MyInput0 = Math.Max(1, value); }
      }

      MyInput apeears in several other places in the code. Should I change those as well? The indicator seems to be working so I'm not sure what these changes will do but I'll make them if the code is incorrect.

      As far as the sound repeating, it's actually the way I want it...just worked out that way. If you don't think it will cause some kind of conflict, I'll leave it. This is on a small tic chart and happens very quickly so not too many repeats.

      Thanks again,
      V.

      Comment


        #4
        VinnyB,

        You need to be very mindful of what is capitalized and what is not. Because your variable name is actually MyInput0 you should not change it to anything.

        I suggest you open up the MACD indicator and take a look at the relevant areas of code there for examples. Again, be VERY mindful of what letters are capitalized and what are not.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Josh!

          Comment


            #6
            Vinny,

            my bad but i should have pointed it out in the first post ...

            Every indicator can be run in two modes - update on bar close or continuous update:

            CalculateOnBarClose = true : In this mode OnBarUpdate gets called once the bar is complete
            CalculateOnBarClose = false : In this mode OnBarUpdate gets invoked on every tick

            The way you have coded your indicator you are fine as long as the mode stays = true (which is your default).

            Since CalculateOnBarClose is user selectable (in the indicators parameters dialog) the indicator would behave very badly should a user change the setting to false. What i wrote above applies when the indicator is being used in this mode.

            You could, of course also avoid the entire problem altogether by building a
            if (CalculateOnBarClose) {
            ...
            }
            around it.

            Sorry i didnt make this clearer in my first post, hope it will still be of help.

            Comment


              #7
              Toulouse,

              Thanks for the update. Hope I'm not driving you nuts.

              I actually do have that option set to false because I want to be alerted as soon as possible - before the bar closes.

              I know that uses more pc reources, but is there something else that could occur?

              Regards,
              V

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Balage0922, Today, 07:38 AM
              0 responses
              1 view
              0 likes
              Last Post Balage0922  
              Started by JoMoon2024, Today, 06:56 AM
              0 responses
              6 views
              0 likes
              Last Post JoMoon2024  
              Started by Haiasi, 04-25-2024, 06:53 PM
              2 responses
              19 views
              0 likes
              Last Post Massinisa  
              Started by Creamers, Today, 05:32 AM
              0 responses
              6 views
              0 likes
              Last Post Creamers  
              Started by Segwin, 05-07-2018, 02:15 PM
              12 responses
              1,786 views
              0 likes
              Last Post Leafcutter  
              Working...
              X