Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing open and close price to themselves and indicator values

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

    Comparing open and close price to themselves and indicator values

    Hi,

    I have very little programming experience and have two questions that are probably really simple.

    1. How do you compare the opening or closing price to an indicator (specifically a 50 period DEMA)?

    I tried:

    if (Open[0] > DEMA(50))
    {
    Do the Following
    }
    // This gives me a CS0019 error which says the types of data can't be compared. Wouldn't the opening price be an int and the DEMA value also be an int?

    After I tried to get the top code to work for a while I thought I had a smart idea of building the condition in the strategy builder and copying the code to the indicator I am trying to create. I copied this code from the Strategy Builder.

    protected override void OnStateChange()
    {

    else if (State == State.DataLoaded)
    {
    DEMA1 = DEMA(Close, 50);
    }

    }

    protected override void OnBarUpdate()
    {

    if ((Open[0] > DEMA1[0]) && (Close[0] > DEMA1[0]))
    {
    }
    ​​​​​​​
    }
    // This gives a CS103 error and says that DEMA1 does not exist in the current context.


    2. I am trying to write: if the opening price of the previous candle plus x number of ticks equals the closing price of the current bar. Is the following code right?

    if ((Open[1] + (x * TickSize)) == Close[0])
    {
    }


    Thanks in advance for any help.

    #2
    Hello Tyler7498,

    Thanks for your post.

    1. You were close in both cases:

    Case 1: Open[0] > DEMA(50) you got an error because the compiler thinks you are comparing the Open price of the current bar to the entire DEMA data series. You can write this as Open[0] > DEMA(50)[0] (is the Open price of the current bar greater than the 50 period DEMA current price).

    Case 2: Using the strategy builder, which is a great way to see the code, you likely missed where the strategy builder code included at the top of the generated code something like private DEMA DEMA1; which creates a private instance of the DEMA called DEMA1. It does that to more efficiently use resources although not required as case #1 will also work, this would be the preferred way to write the code.

    2. Yes.

    Comment


      #3
      Thanks it works now!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      81 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      64 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      66 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X