Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Plot slope of daily sma5 onto intraday 10min chart? Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
m_badger
Posted : Wednesday, July 22, 2009 10:35:11 AM
Registered User
Joined: 9/13/2008
Posts: 99
When I plot a 5 DAY SMA into my 10min intraday chart as described by Julia here (thank you Julia!), then click on the chart to see the value of the SMA5 in the popup-box for the bar I am clicking on, the value for the SMA5 is the same for every bar on that day.  The printed value does not change, although visually it is easy to see that it does change.

RealCode also thinks the daily sma5 is the same value for the entire day on the intra-day chart.

This makes it difficult to plot the slope of the 5 day sma into an intraday chart, because as far as RealCode is concerned, the value of the 5 day sma does not change in the middle of the day.

Any suggestions?  How do I plot intra-day the slope of a daily sma 5?
Bruce_L
Posted : Wednesday, July 22, 2009 3:51:17 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
When you Plot a Daily Moving Average on an Intraday Chart, a straight line will by drawn between the Values of the Daily Moving Average that may appear to reflect the Values of the Daily Moving Average throughout the course of the day. The intermediate "values" of this line do not in fact the actual Values of the Daily Moving Averege at any point during the day.

In fact, the final level at which the line is drawn cannot be known until the Close of the Day when the final value of the Daily Moving Average can be determined. Up until this point, the line drawn will connect the Value of the Moving Average at the end of the previous day and the and the Value of what the Moving Average at the end of the day would be if the current Price was the Close of the Day.

It is possible to create a RealCode Indicator of what the Daily Moving Average would be if the Prices throughout the Day were the Close of the Day (this is the Value at which the Moving Average of the previous Day would be connected by a straight line at that point in time) by replacing everything in the Class tab of the RealCode Editor after the Inherits line with the following RealCode:

    Sub New
        AutoLoop = False
        '# Period = UserInput.Integer = 5
    End Sub
    Public Overrides function Plot() as System.Single
        If TimeFrame.TotalDays <= 1 AndAlso Period >= 2 Then
            Dim Sum As Single
            Dim Past(Period - 2) As Integer
            Dim Days As Integer = 0
            Dim Start As Boolean = False
            For i As Integer = 1 To Price.Count - 1
                If Price.Bar.DateValue(i).DayOfYear <> _
                    Price.Bar.DateValue(i - 1).DayOfYear Then
                    Days += 1
                    Sum += Price.Bar.Value(i - 1)
                    If Days > Period - 1 Then
                        Sum -= Price.Bar.Value(Past(Days Mod (Period - 1)))
                    Else If Days = Period - 1 Then
                        Start = True
                    End If
                    Past(Days Mod (Period - 1)) = i - 1
                End If
                If Start = True Then
                    AddToOutput(Price.Bar.DateValue(i), _
                        (Sum + Price.Bar.OpenValue(i)) / Period, _
                        (Sum + Price.Bar.HighValue(i)) / Period, _
                        (Sum + Price.Bar.LowValue(i)) / Period, _
                        (Sum + Price.Bar.Value(i)) / Period)
                End If
            Next
        End If
    End Function
End Class

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Wednesday, July 22, 2009 7:46:54 PM
Registered User
Joined: 9/13/2008
Posts: 99
Bruce,
Thank you for the response.
I cut and pasted it in but I am confused with the results:

SMA5 vs RealCode SMA5 (daily on intraday chart)




Why does the RealCode daily SMA5 not look like the "Julia" version?
Did I do something wrong?
_mad_badger

http://xs841.xs.to/xs841/09303/sf_dailysma5vsrealcodesma5928.jpg
Bruce_L
Posted : Thursday, July 23, 2009 8:52:57 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
QUOTE (m_badger)
Did I do something wrong?

No. The RealCode Indicator looks exactly like it should.

QUOTE (m_badger)
Why does the RealCode daily SMA5 not look like the "Julia" version?

I thought I'd explained that, but it's probably not obvious from my explanation that the Plots would look different. The "Julia" version will match my version only at the Close of each day because the Daily Moving Average really only has one value per day.

The intraday "visual values" you are seeing for the Daily Moving Average are just the result of absolutely straight lines connecting the one value the Daily Moving Average has in a day to the one value for the previous day and the one value for the next day.

In many cases, these "visual values" do not represent values that the Daily Moving Average held at any point during the day.

In almost all cases, these "visual values" do not represent the value that the Daily Moving Average held at the time of day at which the "visual value" is plotted. The primary exception to this is the last bar of the day.

Finding the "visual value" is possible, but not desirable. The reason it is not desirable is that it requires knowing the Close of the Daily Bar (or Close of the last intraday Bar of the day). This data would not be available at the time of day at which the "visual value" is plotted. It is future data. You would need to be psychic to know this information.

On an Intraday basis (just like on an end of day basis), the last "visual value" of the day does not need future data (but all points prior to the last bar still do). There is still just one value for the Daily Moving Average and the rest of the "visual values" are just a straight line connecting this one value for the Daily Moving Average to the one value for the previous Daily Moving Average.

This brings us back to what my Plot is and why it looks different. My Plot is what this last "visual value" of the day actual was at any given point during the day. It is the actual value of the Daily Moving Average at that point during the day instead of being a straight line connecting together instances of the one final value the Daily Moving Average ended up having at the end of the day.

It might be clearer if you switch the Plot style of the RealCode Indicator I provided from a Line Plot Style to a Candlestick, HLC Bar or OHLC Bar Plot Style. If you switch to a 1-Day Time Frame, the provided RealCode Indicator will match a 5-Period Simple Moving Average added as a Child Indicator to Price History but without the straight lines connecting each value and with a display of the range of Values that the Moving Average actually had throughout the day.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Thursday, July 23, 2009 4:24:55 PM
Registered User
Joined: 9/13/2008
Posts: 99
Bruce,
Thank you for explaining this.  I was looking to mimic the visual representation of the smooth version.
Here is my crack at it:

' 5 days of 10 min bars
Static numBars As Integer = 195
' total sum of price over last 5 days
Dim sum As Single = 0
For i As Integer = 0 To numBars - 1
    ' add closing price to sum
    sum += price.close(i)
Next
' plot total price summation by number of 10 min bars
plot = sum / numBars



It is not perfect but it is pretty close and I can calculate the slope for my RealCode.
Thanks again,
_mad
Bruce_L
Posted : Thursday, July 23, 2009 5:10:03 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I think you might get slightly quicker results by just adding a 195-Period Simple Moving Average to the Charts. I'm pretty sure the built in Simple Moving Average Indicator uses an algorithm that avoids the use of a loop at each bar.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.