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

Variables Not getting Set Properly ?

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

    Variables Not getting Set Properly ?

    I am trying to code a very simple indicator that smooths the Input. It seems my smoothing variable never gets set correctly.

    In the following code the "smoothing" variable is always zero when it should be 1/period... What am I doing wrong here....

    Code:
     
     public class ibdMEMA : Indicator
     {
     //#region Variables
     
     private int period = 12;
     double smoothing = 1.0; 
     private int debugLevel = 1; 
     
     //SMA lo_SMA; 
     //#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()
     {
     Add(new Plot(Color.Orange, "ibdMEMAVal"));
     Overlay = false;
     PriceTypeSupported = true; 
     }
     
     protected override void OnStartUp()
     {
     
     }
     
     /// <summary>
     /// Called on each bar update event (incoming tick)
     /// </summary>
     protected override void OnBarUpdate()
     {
     smoothing = (double)(1 / period);
     
     if (CurrentBar == period)
     {
     //smoothing = Math.Min(1, 1 / period); 
     
     ibdMEMAVal.Set(Input[0]); 
     }
     if (CurrentBar > period)
     { 
     Print("smoothing = " + smoothing.ToString() + " CB: Input[0] = " + Input[0]);
     //IbdDebug("Value1 = " + Value[1] + " Input0 = " + Input[0], debugLevel, 1) ; 
     //ibdMEMAVal.Set(( smoothing * Input[0] ) + ( ( 1 - smoothing ) * ibdMEMAVal[1]));
     ibdMEMAVal.Set(( .75 * Input[0] ) + ( ( 1 - .75 ) * ibdMEMAVal[1]));
     //IbdDebug("ibdMEMA Value during MEMA = " + ibdMEMAVal[0], debugLevel, 1) ; 
     }
     }
     
     #region Properties
     [Browsable(false)]
     [XmlIgnore()]
     public DataSeries ibdMEMAVal
     {
     get { return Values[0]; }
     }
     /// <summary>
     /// </summary>
     [Description("Numbers of bars used for calculations")]
     [GridCategory("Parameters")]
     public int Period
     {
     get { return period; }
     set { period = Math.Max(1, value); }
     }
     #endregion
     }

    #2
    Originally posted by tornadoatc View Post
    I am trying to code a very simple indicator that smooths the Input. It seems my smoothing variable never gets set correctly.

    In the following code the "smoothing" variable is always zero when it should be 1/period... What am I doing wrong here....

    Code:
     
     public class ibdMEMA : Indicator
     {
     //#region Variables
     
     private int period = 12;
     double smoothing = 1.0; 
     private int debugLevel = 1; 
     
     //SMA lo_SMA; 
     //#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()
     {
     Add(new Plot(Color.Orange, "ibdMEMAVal"));
     Overlay = false;
     PriceTypeSupported = true; 
     }
     
     protected override void OnStartUp()
     {
     
     }
     
     /// <summary>
     /// Called on each bar update event (incoming tick)
     /// </summary>
     protected override void OnBarUpdate()
     {
     smoothing = (double)(1 / period);
     
     if (CurrentBar == period)
     {
     //smoothing = Math.Min(1, 1 / period); 
     
     ibdMEMAVal.Set(Input[0]); 
     }
     if (CurrentBar > period)
     { 
     Print("smoothing = " + smoothing.ToString() + " CB: Input[0] = " + Input[0]);
     //IbdDebug("Value1 = " + Value[1] + " Input0 = " + Input[0], debugLevel, 1) ; 
     //ibdMEMAVal.Set(( smoothing * Input[0] ) + ( ( 1 - smoothing ) * ibdMEMAVal[1]));
     ibdMEMAVal.Set(( .75 * Input[0] ) + ( ( 1 - .75 ) * ibdMEMAVal[1]));
     //IbdDebug("ibdMEMA Value during MEMA = " + ibdMEMAVal[0], debugLevel, 1) ; 
     }
     }
     
     #region Properties
     [Browsable(false)]
     [XmlIgnore()]
     public DataSeries ibdMEMAVal
     {
     get { return Values[0]; }
     }
     /// <summary>
     /// </summary>
     [Description("Numbers of bars used for calculations")]
     [GridCategory("Parameters")]
     public int Period
     {
     get { return period; }
     set { period = Math.Max(1, value); }
     }
     #endregion
     }
    Code:
     smoothing = (1.0 / (double)period);

    Comment


      #3
      Hello tornadoatc,

      Koganam is correct. You need to change the type of the divisor (by casting it to a double because it is an int) as well as the type of the dividend (using 1.0 instead of 1) so you are dividing two doubles into a double as opposed to changing the type of the quotient of an int divided by an int to a double which you are doing in your original code.

      Please let me know if this does not resolve the issue or if we may be of further assistance.
      Michael M.NinjaTrader Quality Assurance

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by FrancisMorro, Today, 03:24 AM
      0 responses
      1 view
      0 likes
      Last Post FrancisMorro  
      Started by Segwin, 05-07-2018, 02:15 PM
      10 responses
      1,770 views
      0 likes
      Last Post Leafcutter  
      Started by Rapine Heihei, 04-23-2024, 07:51 PM
      2 responses
      31 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by Shansen, 08-30-2019, 10:18 PM
      24 responses
      944 views
      0 likes
      Last Post spwizard  
      Started by Max238, Today, 01:28 AM
      0 responses
      11 views
      0 likes
      Last Post Max238
      by Max238
       
      Working...
      X