Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing Close to SMA value failing - why?

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

    Comparing Close to SMA value failing - why?

    Hi everyone. I'm hoping someone can help me with a simple code snippet here.

    I want to know when the Close price is great than the SMA(50). Here is the code I'm using to do it. I'm simply sending the info to the output window for now.

    The output gives me the Close price and the SMA(50) value for each bar. It is then suppose to compare the values and tell me if Close is greater than SMA(50) (if Close is less than SMA(50), nothing should be printed in the Outpute Window).

    Problem is, it tells me that Close is ALWAYS greater than SMA(50) even when the output of the variables clearly show cases when Close is less than SMA(50).

    ---> Code Snippet <---

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    double myCloseValue = Close[0];
    double mySMA50Value = SMA(50)[0];

    Print("The current Close value is " + myCloseValue.ToString());
    Print("The current SMA 50 value is " + mySMA50Value.ToString());

    // Condition set 1
    if (myCloseValue > mySMA50Value);
    {
    Print("Close is > than SMA50");
    }
    }

    ---> End Code Snippet <---

    ---> Output Window Snippet (comments added for this post and do not appear in actual Output Window output <---

    The current Close value is 1.5633
    The current SMA 50 value is 1.563874
    Close is > than SMA50 .................comment added - this is an error
    The current Close value is 1.5565
    The current SMA 50 value is 1.56362
    Close is > than SMA50 .................comment added - this is an error
    The current Close value is 1.5634
    The current SMA 50 value is 1.563456
    Close is > than SMA50
    The current Close value is 1.5665
    The current SMA 50 value is 1.563496
    Close is > than SMA50

    ---> End Output Window Snippet <---

    What am I doing wrong here?

    Much thanks.

    - Iggy

    #2
    The problem is this line
    Code:
    if (myCloseValue > mySMA50Value)[COLOR=Red];[/COLOR]
    You are not suppose to have the semicolon there.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      D'OH!

      Much thanks.

      That did it.

      It's the little things that are so very important (and that screw everything up!!).

      Best,

      - Iggy

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      574 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      333 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
      553 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X