Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

How do you shift a moving average left? Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
gwoods
Posted : Thursday, October 23, 2008 10:27:59 AM
Registered User
Joined: 12/1/2006
Posts: 25
In Telechart, there was an option that allowed a user to shift a moving average left.  What is the real code to accomplish the same thing?
Bruce_L
Posted : Thursday, October 23, 2008 10:51:01 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm not sure I would generally use RealCode for this. The built in Moving Average that can be added by selecting Add Indicator | Select... | Moving Average has an Offset setting that can be be adjusted by left-clicking on the Moving Average to bring up the Main tab of the Edit window. You will want to set this Offset to either -Period/2 or -(Period-1)/2 to implement Shift Left.

That said, you could manually create a Shifted Left Simple Moving Average using the following RealCode:

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Plot = AvgC
Else
    Plot = Single.NaN
End If

The negative Offset is what allows the access of future data in the RealCode.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 11:01:20 AM
Registered User
Joined: 12/1/2006
Posts: 25
Yes, I saw the offset; however, I was recalling a conversation I had with you about developing a pcf in Telechart that would create a moving average shifted left; and you said that it was not possible because you could not create a negative variable.  So, you told me to get Stockfinder to perform this task.  I will study the formula above.
Bruce_L
Posted : Thursday, October 23, 2008 11:09:12 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The RealCode above is basically doing the following for a 21-Period Shifted Left Simple Moving Average (and it's probably a more obvious example of how negative Offsets work in RealCode):

Plot = (Price.Last(-10) + _
    Price.Last(-9) + _
    Price.Last(-8) + _
    Price.Last(-7) + _
    Price.Last(-6) + _
    Price.Last(-5) + _
    Price.Last(-4) + _
    Price.Last(-3) + _
    Price.Last(-2) + _
    Price.Last(-1) + _
    Price.Last + _
    Price.Last(1) + _
    Price.Last(2) + _
    Price.Last(3) + _
    Price.Last(4) + _
    Price.Last(5) + _
    Price.Last(6 ) + _
    Price.Last(7) + _
    Price.Last(8) + _
    Price.Last(9) + _
    Price.Last(10)) / 21

It's just doing it in a more efficient way that subtracts the oldest value and adds the newest value in the Moving Average at each Bar instead of adding together all 21 values.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 11:23:01 AM
Registered User
Joined: 12/1/2006
Posts: 25
I will study your code.  Are sitting up typing all this out when someone has a question?  If so, that is a lot of typing.
Bruce_L
Posted : Thursday, October 23, 2008 11:26:03 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Yes, I am sitting here typing it all out (but I use Copy and Paste when possible to cut down the actual amount of typing required and sometimes use spreadsheets to create really long bits of repetitive code).

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 11:33:53 AM
Registered User
Joined: 12/1/2006
Posts: 25

Bruce, how would I add a couple of calculations to the end of real code; so real code will calculate the coding above and then add the value derived from the appended coding to produce a total value?  Ex.  your code + (21+120+MJ/2) = total     [This is made up coding ---> (21+120+MJ/2).  I needed some kind of example.] 

Bruce_L
Posted : Thursday, October 23, 2008 11:41:40 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Let's say MJ/2 is something fairly straightforward like Typical Price, (High+Low+Close)/3. You would just change the following line:

    Plot = AvgC

To:

    Plot = AvgC + (Price.High + Price.Low + Price.Last) / 3

Or you could assign it to a variable and then use that variable in the Plot line (this would allow you to do something more complicated without explicitely writing it into the Plot line:

    Dim MJ As Single = (Price.High + Price.Low + Price.Last) / 3
    Plot = AvgC + MJ

A real example that uses subtraction instead of addition is contained in Detrended Price Ocillator not current. The only difference between the second DPO in my Wednesday, October 22, 2008 9:54:12 AM ET post in that topic and the RealCode in my Thursday, October 23, 2008 10:51:01 AMET post in this topic is that the Plot line in the other topic is:

    Plot = Price.Last - AvgC

While the Plot line in this topic is:

    Plot = AvgC

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 11:47:59 AM
Registered User
Joined: 12/1/2006
Posts: 25
How would you append that coding to this?

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Plot = AvgC
Else
    Plot = Single.NaN
End If
Bruce_L
Posted : Thursday, October 23, 2008 11:51:53 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Changing:

    Plot = AvgC

To:

    Plot = AvgC + (Price.High + Price.Low + Price.Last) / 3

Would result in:

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Plot = AvgC + (Price.High + Price.Low + Price.Last) / 3
Else
    Plot = Single.NaN
End If

Changing:

    Plot = AvgC

To:

    Dim MJ As Single = (Price.High + Price.Low + Price.Last) / 3
    Plot = AvgC + MJ

Would result in:

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Dim MJ As Single = (Price.High + Price.Low + Price.Last) / 3
    Plot = AvgC + MJ
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 12:15:04 PM
Registered User
Joined: 12/1/2006
Posts: 25
So, this statement 'Static AvgC As Single' in the coding allows for AvgC to reference the coding below:

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Dim MJ As Single = (Price.High + Price.Low + Price.Last) / 3
    Plot = AvgC + MJ
Else
    Plot = Single.NaN
End If

If that is true, then how would Plot = AvgC know that it references the above code if it is not within the same realcode?  Ex.  1+1=2  but if Plot=2; how would it know it derived from 1+1=2 
Bruce_L
Posted : Thursday, October 23, 2008 12:27:07 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
You have to declare a variable to use it. If you don't declare the variable, you can't use it.

If you want to use the results of another Indicator or Rule is to select the Indicator/Rules button at the top of the RealCode Editor. This will add a line to the RealCode generating a reference. For example if you selected Price History, you would get a line looking something like the following (this example isn't very useful since you can already reference Price in RealCode):

'# PH = indicator.PriceHistory

You could then reference the previous value of the Price History plot as PH.Value(1).

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 1:05:29 PM
Registered User
Joined: 12/1/2006
Posts: 25
I see you are creating an indicator or rule that reference a real code formula.  For instance, you may say # happy = (21+120+MJ/2) and # gogo =  Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then

If we add happy + gogo = total, is this correct?
Bruce_L
Posted : Thursday, October 23, 2008 1:16:04 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Maybe it's because I'm not a programmer, but I'm really not understanding your question other than to know that your syntax is not valid (I really didn't entirely understand your last question either).

Let's say you have a RealCode Indicator called Shift Left SMA that you Plot on the Chart:

'# Period = UserInput.Integer = 21
Static AvgC As Single
Static Offset(1) As Integer
Static Start(2) As Integer
If isFirstBar Then
    AvgC = 0
    Offset(0) = -(Period - 1) / 2
    Start(0) = System.Math.Max(Offset(0), 0)
    Offset(1) = Offset(0) + Period
    Start(1) = System.Math.Max(Offset(1), Period)
    Start(2) = Start(0) + Start(1) - 1
End If
If CurrentIndex >= Start(0) Then AvgC += Price.Last(Offset(0)) / Period
If CurrentIndex >= Start(1) Then AvgC -= Price.Last(Offset(1)) / Period
If CurrentIndex >= Start(2) Then
    Plot = AvgC
Else
    Plot = Single.NaN
End If

If you select the Indicator/Rules button at the top of the RealCode Editor in another RealCode Indicator and select the Shift Left SMA, you'll get a line that looks something like the following:

'# SLS = indicator.ShiftLeftSMA

If you wanted to Plot this plus twenty-one, you could create something like the following:

Plot = SLS.Value + 21

So this entire new RealCode Indicator would look like:

'# SLS = indicator.ShiftLeftSMA
Plot = SLS.Value + 21

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gwoods
Posted : Thursday, October 23, 2008 1:21:46 PM
Registered User
Joined: 12/1/2006
Posts: 25
Yes, you understood!  Thanks    The only delima is that I have to study your code until your company develops a manual for realcode.

I appreciate your tact.  You have assisted me on one other occassion; I am impressed with your skill and ability to comprehend.
Bruce_L
Posted : Thursday, October 23, 2008 1:29:25 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Well hopefully that won't be too long in coming. I'm in the process of reviewing the newest version of the RealCode Programmers Reference right now (as are others who are a lot better at programming).

-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.