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

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.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks it works now!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cre8able, Today, 01:16 PM
      2 responses
      9 views
      0 likes
      Last Post cre8able  
      Started by chbruno, 04-24-2024, 04:10 PM
      3 responses
      48 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by samish18, Today, 01:01 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by WHICKED, Today, 12:56 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by WHICKED, Today, 12:45 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X