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 hazylizard, Today, 08:38 AM
      2 responses
      8 views
      0 likes
      Last Post hazylizard  
      Started by geddyisodin, Today, 05:20 AM
      2 responses
      18 views
      0 likes
      Last Post geddyisodin  
      Started by Max238, Today, 01:28 AM
      5 responses
      47 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by giulyko00, Yesterday, 12:03 PM
      3 responses
      13 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by habeebft, Today, 07:27 AM
      1 response
      16 views
      0 likes
      Last Post NinjaTrader_ChristopherS  
      Working...
      X