• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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

    Comment


      #3
      Thanks it works now!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Teebone21, Today, 05:03 PM
      1 response
      3 views
      0 likes
      Last Post Teebone21  
      Started by gbulle, Today, 01:17 PM
      4 responses
      14 views
      0 likes
      Last Post gbulle
      by gbulle
       
      Started by CougarMountain, Today, 03:55 PM
      0 responses
      4 views
      0 likes
      Last Post CougarMountain  
      Started by syxforex, Today, 01:13 PM
      3 responses
      13 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by pjsmith, Today, 01:17 PM
      2 responses
      6 views
      0 likes
      Last Post pjsmith
      by pjsmith
       
      Working...
      X