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

RSI checks above and below 30 on 2 consecutive days

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

    RSI checks above and below 30 on 2 consecutive days

    Hi,

    I am trying to write code to let me know when the RSI value rises above 30 at the end of the last day's trading but only when it was below 30 on the day before that. So I am basically trying to find stocks where the RSI goes from below to above 30 looking at 2 days worth of trading.

    I am using end of day data for my calculations so my bars are a single day.

    The code I am using is as follows:

    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    if (RSI(14, 3).Avg[0] > 30)
    {
    Value.Set(Variable0 = +
    1);
    }
    if (RSI(14, 3).Avg[1] < 30)
    {
    Value.Set(Variable0 = +
    1);
    }

    else
    {
    Value.Set(
    0.0);
    }
    }

    The above code compiles fine but I am not sure it will give me what I need. Can someone please confirm whether my code is correct based on my requirement and please let me know what I need to change if it isn't.

    I want to be able to output a value of +1 if it meets the 2 criteria and output 0 if it doesn't meet the 2 criteria.

    I have used 2 consecutive if statements. I don't know if that is correct.

    Hopefully one of you will be able to kindly assist me further.

    Many thanks,

    Evan

    #2
    Hello evan.hatch,

    When you mention "when the RSI value rises above 30 at the end of the last day's trading" are you talking about the last day of the expiry month before the rollover?

    Are you referring to the last minute bar of the current day?

    Are you referring to the first minute bar of the current day placing a trade based on the rsi of the last bar of the previous day?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Many thanks for responding to my post. I am not sure what "expiry month" and "rollover" are and I guess for what I am needing I probably don't need to worry about these. I am only focussing on end of day data for each week and not the intraday data.

      I can probably best explain using an example. If we take the end of day stock price for any company. I will have the following RSI values at the end of the trading day:

      Date/Day of the week: RSI value: Value output by my indicator
      21 Nov: 32.2: 0
      22 Nov: 29.3: 0
      23 Nov: 29.8: 0
      24 Nov: 32.4: 1
      25 Nov: 31.6: 0

      I am using the Yahoo EOD feed for my shares listed on the LSE.

      I think what I have done in the code is tried to put in 2 consecutive "if" statements which should in my view be like an "and" statement in other coding languages. I think this is how I should be doing it in NT. Please let me know if there is a more efficient way of doing this.

      To answer your question: Are you referring to the first minute bar of the current day placing a trade based on the rsi of the last bar of the previous day?

      No I am just looking at the daily data at the end of the trading day and comparing the overall value for the RSI from one day to the next. It will be whatever Yahoo provides to me as the final value for the day. I appreciate that this may change, even after the LSE has closed, depending on whether further trades are taking place for some of the shares if they are listed in other markets.

      I hope this gives you more information and answers some of your queries.

      Many thanks,

      Evan

      Comment


        #4
        I think the problem I have is that I am struggling to be able to calculate the RSI value for the previous day (i.e. the day before the last value output)

        I decided to try and output the RSI values for the last known day and the day prior to that. By using the following code I can output the last day's value:

        Value.Set(RSI(14, 3).Avg[0]);

        But if I try and get the day before that one I am using the following code:

        Value.Set(RSI(14, 3).Avg[1]);

        But the program just outputs 0 for all values presumably because it is trying to pick up the previous intraday bar which is not possible using the Yahoo EOD data.
        So if my understanding is correct above, how can I pull back the previous day's RSI value for the end of the day?

        I have got the data series on my indicator set to "days" so I would have thought that it would be able to work, but perhaps not.

        Any pointers gratefully received.

        Many thanks,

        Evan

        Comment


          #5
          Hello Evan,

          I'm not quite sure that I am understanding.

          If the bar type is Day and the value is 1 (a daily chart) and you are not connected to a real-time data feed, then the RSI value will be updated after the current day's bar closes. This means that the value of the RSI is always the value of the previous day. The current day's RSI will only be calculated after the next day's bar opens.

          Print(RSI(14, 3)[0]); // this would print the RSI value for yesterday on a daily chart.

          Print(RSI(14, 3)[1]); // this would print the RSI value for the day previous to yesterday on a daily chart.

          You can add the RSI indicator to a chart and check the value, and compare this to the value printed by your script to verify this.

          To clarify your meaning when you mention ""when the RSI value rises above 30 at the end of the last day's trading", you are meaning, when the value of the RSI rises above 30 on the last bar of the chart, is this correct?

          Are you wanting to trigger an action after the close of a session without a real-time data stream?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea,

            I am using the Yahoo EOD free data for stocks listed on the LSE and in my context I am only focussing on EOD data and not intraday data. I am probably a bit confused as to what data is output by Yahoo, when it is output, for which day and how this can be used in NT.

            I have set up custom indicators which use the code to output the RSI values which I am trying to calculate. I am then including these indicators in the Market Analyser which has cell conditions set to show certain colours based on the values output.

            I tried to use the code you provided but the value output was just share the latest share price so I guess the code is not quite right in the way I am using it.

            I am guessing that for most of the indicators (including the RSI) that you can get the values from a previous day or days (e.g. I could insert any value in the square brackets as you have done to go and fetch the data from say 8 days ago and output it as part of my custom indicator.

            Are there any specific help pages or videos which provide more information on the EOD data so that I can understand what the limitations of this are and what I can expect in the code I write before I move on to paid for data.

            Any further assistance would be gladly received.

            Many thanks,

            Evan

            Comment


              #7
              Hello Evan,

              I am not aware of any documentation specific to data feeds such as the Yahoo, Google connections other than the grid that shows what data is available from which connections.

              Below is a link to the help guide on Historical & Real-Time Data. In the section 'Understanding the data provided by your connectivity provider' there is a chart that show the supported connection technologies and the types of data these connections support. The Real-Time column shows if this connection has real-time data.


              Actions in NinjaScripts are generally driven by market data (unless you are using a timer). OnBarUpdate will only trigger if a bar closes or if a tick is received in real-time. The historical data will cause the bar to close and trigger OnBarUpdate for each historical bar. Once the historical data is processed, there will be no real-time data to drive the bar close of the current day after it closes. When the new session opens, this will trigger a new bar and will close the first bar. However, this action will happen long after the current day has closed.

              That said, if you actually set the CalculateOnBarClose property to false, it will actually give an update to OnBarUpdate for the last bar on the chart even though there is no real-time data to drive it. (I just tested this to confirm)

              The brackets are how an index is selected from a collection. This is a bars ago value.
              Using [0] gives the most recently closed bar. Using [1] gives the bar previous to the most recently close bar. Using [5] would give a value from 5 bars ago.
              RSI(int period, int smooth)[int barsAgo]


              The code I have provided will not print the current price. This will instead print the most recently closed bar's RSI value and the bar previous to the most recently closed bar's RSI value.
              Attached is a screenshot to demonstrate as well as a simple script that I have added this code to and tested.
              Attached Files
              Last edited by NinjaTrader_ChelseaB; 11-28-2016, 12:40 PM.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Waxavi, Today, 02:10 AM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by Kaledus, Today, 01:29 PM
              5 responses
              13 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Waxavi, Today, 02:00 AM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by alifarahani, Today, 09:40 AM
              5 responses
              23 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by gentlebenthebear, Today, 01:30 AM
              3 responses
              17 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X