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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      131 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      74 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      117 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      111 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      89 views
      0 likes
      Last Post CarlTrading  
      Working...
      X