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

Dynamic moving average Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
ansinzhou
Posted : Monday, July 20, 2015 11:14:06 AM
Registered User
Joined: 10/6/2014
Posts: 34

Hi, 

While building indicators, in one I need to use dynamic moving average, I couldn't find it in the coding.  So I tried builiding the DMA by calculating it. 

 

'# YCAA = chart.YC_AA
 
Dim DMA As Single 
 
DMA = ((volume.value / (volume.value(1) + volume.value(2) + volume.value(3))) * YCAA.value) + (( 1 - (volume.value / (volume.value(1) + volume.value(2) + volume.value(3)))) * DMA(1))
 
In which case I'm trying to calculate DMA(YCAA, Volume/(volume(1)+volume(2)+volume(3))
 
However,  I realized it wouldn't work because I Can't use DMA(1) in the formula, since DMA has no value yet. 
 
How would I code to use dynamic moving average of YCAA? In  DMA(YCAA, Volume/(volume(1)+volume(2)+volume(3))
 
 
Thank You.
 
 
Bruce_L
Posted : Thursday, July 23, 2015 10:36:26 AM


Worden Trainer

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

You just need to have a way to initialize the moving average and either not start calculating until CurrentIndex = 3 or have some way to calculate prior to CurrentIndex = 3.

Maybe something like the following (note this is for price, not YCAA)?

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com 
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Dynamic Moving Average
'|******************************************************************
'#Cumulative
Static DMA As Double
Static Sum As Double
If isFirstBar Then
	DMA = 0
	Sum = 0
End If
If Sum > 0 Then
	DMA = Volume.Value / Sum * Price.Last + (1 - Volume.Value / Sum) * DMA
Else
	DMA = Price.Last
End If
Sum += Volume.Value
If CurrentIndex > 2 Then
	Sum -= Volume.Value(3)
End If
Plot = DMA

I am probably doing something wrong because you can get spikes and even negative values when volume over three bars in a row is zero. That said, I don't know if it is just something with parentheses or because I'm setting DMA to price when the volume over the previous 3 bars is 0.

I am not familiar with the indicator or its calculation however, so it could also just be the way it would normally work. Besides these spikes, the indicator seems to behave which would seem to be desirable for a moving average type indicator.



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