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

Help with Gapless Moving Average

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

    Help with Gapless Moving Average

    Hello all. I am creating a 'gapless' MA to be used on RTH charts, where there is a natural GAP in between the days.

    What I am wanting to do, is when the bars since the start of the session is LESS THAN the period I am using, use the last x number of values from the prior session... BUT offset by the size of the gap. I am attaching a picture of that called "exhibitA."

    What I did was create a 'synthetic' data series, that is simply the last x number of values from the prior session, but simply shifted by the gap amount. You can see this in the picture 'exhibitA,' as I drew dots on the Synthetic series, and you can see it is shifted by the gap amount.

    Then the plotting, is simply set based on the number of bars since the session start, so if the number of bars is LESS than the period chosen, the MA is built with the synthetic session. If GREATER, it is built with the closing price as is normally the case.

    So the problem is that this works exactly the way I want using "EMA." But when using SMA, or Linear Regression for example, it just doesn't work the way it should? I believe this is just the way the built in NT MAs handle being referenced.

    See the attached pic labeled "ExhibitB." Only change was to reference an SMA instead of EMA, and it handles the series weirdly.

    I am wondering if anyone knows the reason the EMA handles the data the way I want, but not other MA types in NT?

    Thanks!
    Attached Files

    #2
    Just a note....

    Trying out several MAs, EMA,WMA and HMA work in the way that I want.

    It is LinReg and SMA that seem to behave unexpectedly.

    Comment


      #3
      Hello forrestang,

      There are fundamental differences in the way that the EMA and SMA are calculated.

      With the EMA this multiplies the previous bars value in the calculation. This means that as the indicator rolls forward, all of the data on the chart will affect the current bars value.

      With the SMA this basically only uses the bars within the period for calculations. This means data from before the period, is not used in the calculations.

      Have you manually calculated the values to show that these are being calculated incorrectly?

      For example I did this by modifying your script to print the bar values being used.
      I then set the Period1 parameter on the indicator to 5 to make the calculation easier.
      I then added the script to a 1 minute ES 09-16 chart (with 5 days to load).

      The output came out as:
      S | 6/24/2016 4:12:00 PM | ma1_Syn[0]: 2009.25
      S | 6/24/2016 4:13:00 PM | ma1_Syn[0]: 2007.25
      S | 6/24/2016 4:14:00 PM | ma1_Syn[0]: 2004.5
      S | 6/24/2016 4:15:00 PM | ma1_Syn[0]: 2006.5
      S | 6/27/2016 9:31:00 AM | ma1_Syn[0]: 2006.5 | MA1[0]: 2006.8

      So I added 2006.5 + 2006.5 + 2004.5 + 2007.25 + 2009.25 and divided this by 5. I got 2006.8. This appears to be correct to me.

      Attached is your script modified to use the SMA and to add this print.
      Attached Files
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks Chelsea!

        So looking at the numbers from your post, that does seem to be the correct value for those numbers using an SMA.

        But the plot itself winds up being something other than that value?

        When you plotted the SMA, for the first 5 values or so, was your plot somewhere near the price? Or was it like mine and nowhere near the price?

        It seems like the 'Average' should wind up being somewhere near the price?
        Attached Files

        Comment


          #5
          Hi forrestang,

          When I was analyzing the output I was focused on the first session start and not the last session start.
          The first session is the output that I posted in my last post. This session is being calculated correctly. The last session start is where things become incorrect. (I increased the days to load to 10 and noticed that this is basically correct for all session except for the last session)

          I was having some trouble following the logic in your script, so I recreated the script based on what I think you were trying to accomplish. I did this without using your script to avoid further confusion and I think I was able to get what you are looking for.

          Please test this and let me know if this is not what you were trying to accomplish.
          Attached Files
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks Chelsea, but that's not quite it.

            So... at the start of every session, when using a session template that has breaks in it, there is a difference between the closing price of the last session, and the open of the next session.

            So at the Open Bar, I have created a 'synthetic' data series(using the loop), which simply takes x-previous values(the chosen period), and shifts them by that Gap Difference.

            Once I have that data, I would simply like to take an average using those previous values.

            So if using for example a 10 bar average, on the very first bar at the open, it would use the close of the current Bar, and the last 9 values of the synthetic average. On the second bar, it would use the last 8 values of the synthetic series, and the 2 bars since the open and so on. By the 10 bar, it would just be an average of the 10 bars since the open, completely ignoring the synthetic series created.

            I am attaching a picture of what it looks like on the script I have gotten to work. You can see how the one on the left is your standard type of MA and how it looks when it spans across a Gapped session. On the right, you can see how I want my indie to perform. Where each bar is created by taking an average of the shifted closing prices.

            The problem was that, when I switched the EMA to an SMA for example, for some reason it would give an out-of-whack result. And I was trying to figure out why passing the exact same information to an SMA instead of an EMA, it would produce the wrong number.
            Attached Files
            Last edited by forrestang; 06-28-2016, 03:22 PM.

            Comment


              #7
              Hello forrestang,

              This doesn't appear to be an issue with the indicators themselves but rather an issue with the logic in your script.

              The example I have provided does show how to use this secondary series and shows that the SMA can calculate correctly with the given data.

              For further logic assistance, you can also contact one of our professional NinjaScript Consultants who would be eager to create or modify this script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.

              This thread will remain open for any community members that would like to assist.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by fernandobr, Today, 09:11 AM
              0 responses
              0 views
              0 likes
              Last Post fernandobr  
              Started by itrader46, Today, 09:04 AM
              1 response
              3 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by timmbbo, Today, 08:59 AM
              0 responses
              1 view
              0 likes
              Last Post timmbbo
              by timmbbo
               
              Started by bmartz, 03-12-2024, 06:12 AM
              5 responses
              33 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Started by Aviram Y, Today, 05:29 AM
              4 responses
              14 views
              0 likes
              Last Post Aviram Y  
              Working...
              X