Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 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
      545 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