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 |

Backtest and Scan for 5dmav Strategy Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
danielbender
Posted : Saturday, December 6, 2014 1:48:40 AM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce

How are you doing? Hope life is treating you well. It has been awhile since I logged on to this forum. 

I have another coding request. Not sure whether it can be done as a backtest or a real code indicator, or a combination of both. I want to be able to use the Indicator in a watchlist scan, and I want to be able to backtest the entry/exit strategies. Open to your advice/suggestions. Perhaps 2 sets of code are needed. One for a backtest of all closed trades, and one to monitor open trades as part of a watchlist scan.

Thank you… Dan

+++++++++++++++++++++++++++++++++++++

RealCode Indicator Name: 5dsma vs. Close

A) User Input:

Output = 1 “plot return” or 2 “plot days since entry”

Track = 1 “Current Trade” or 2 “Last closed Trade” {note: Perhaps this is duplicating backtest function, and is thus unnecessary}

TradeDirection = 1 “calculate Longs” or 2 “calculate shorts”

EntryTrigger = 0.5 “% above or below 5d sma”

ExitTrigger = 1.0% “%above or below 5d sma”

B Enter Long Trade when (Trade Direction = 1):

5 day sma rate of change (today) > 5 day sma rate of change (yesterday) AND

Close > 5d sma AND

(Close - 5d sma ) / close < Pricev5dsma/100

Exit Long Trade when:

(Close - 5d sma ) / close < -Pricev5dsma/100 OR

5 day sma rate of change (today) < 5 day sma rate of change (yesterday)

C) Enter Short Trade when (Trade Direction = 2):

5 day sma rate of change (today) < 5 day sma rate of change (yesterday) AND

Close < 5d sma AND

(Close - 5d sma ) / close < -Pricev5dsma/100

Exit Short Trade when:

(Close - 5d sma ) / close > Pricev5dsma/100 OR

5 day sma rate of change (today) > 5 day sma rate of change (yesterday)

D) Plot:

IF Output = 1, plot % return since trade entry

If Output = 2, plot days since entry (make number positive if trade is open, negative if trade is closed)

Bruce_L
Posted : Friday, December 19, 2014 4:36:43 PM


Worden Trainer

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

I tried to be as literal as possible in converting the description into RealCode. You will need to edit the indicator to actually set the Entry Trigger to 0.5 as the default value will ignore the decimal and fill in as 5.

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com 
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:5dmav Strategy
'|******************************************************************
'# Output = UserInput.Integer = 1
'# Track = UserInput.Integer = 1
'# TradeDirection = UserInput.Integer = 1
'# EntryTrigger = UserInput.Single = .5
'# ExitTrigger = UserInput.Single = 1
Static EntryBar As Integer
Static EntryPrice As Single
Static Old As Single
If isFirstBar Then
	EntryBar = -1
	Old = Single.NaN
End If
If EntryBar = -1 Then
	If TradeDirection = 1 Then
		If Price.AVGC(5) - Price.AVGC(5, 1) > Price.AVGC(5, 1) - Price.AVGC(5, 2) AndAlso _
			Price.Last > Price.AVGC(5) AndAlso _
			(Price.Last - Price.AVGC(5)) / Price.Last < EntryTrigger / 100 Then
			EntryBar = CurrentIndex
			EntryPrice = Price.Last
		End If
	Else If TradeDirection = 2 Then
		If Price.AVGC(5) - Price.AVGC(5, 1) < Price.AVGC(5, 1) - Price.AVGC(5, 2) AndAlso _
			Price.Last < Price.AVGC(5) AndAlso _
			(Price.Last - Price.AVGC(5)) / Price.Last < -EntryTrigger / 100 Then
			EntryBar = CurrentIndex
			EntryPrice = Price.Last
		End If
	End If
Else

End If
If Track = 1 Then
	If EntryBar = -1 Then
		Plot = Single.NaN
	Else
		If Output = 1 Then
			If TradeDirection = 1 Then
				Plot = 100 * (Price.Last / EntryPrice - 1)
			Else If TradeDirection = 2 Then
				Plot = -100 * (Price.Last / EntryPrice - 1)
			End If
		Else If Output = 2 Then
			Plot = CurrentIndex - EntryBar
		End If
	End If
Else If Track = 2 Then
	Plot = Old
End If
If EntryBar <> -1 Then
	If TradeDirection = 1 Then
		If Price.AVGC(5) - Price.AVGC(5, 1) < Price.AVGC(5, 1) - Price.AVGC(5, 2) OrElse _
			(Price.Last - Price.AVGC(5)) / Price.Last < -ExitTrigger / 100 Then
			If Output = 1 Then
				Old = 100 * (Price.Last / EntryPrice - 1)
			Else If Output = 2 Then
				Old = CurrentIndex - EntryBar
			End If	
			EntryBar = -1
		End If
	Else If TradeDirection = 2 Then
		If Price.AVGC(5) - Price.AVGC(5, 1) > Price.AVGC(5, 1) - Price.AVGC(5, 2) OrElse _
			(Price.Last - Price.AVGC(5)) / Price.Last > ExitTrigger / 100 Then
			If Output = 1 Then
				Old = -100 * (Price.Last / EntryPrice - 1)
			Else If Output = 2 Then
				Old = CurrentIndex - EntryBar
			End If	
			EntryBar = -1
		End If
	End If
End If


-Bruce
Personal Criteria Formulas
TC2000 Support Articles
danielbender
Posted : Friday, December 19, 2014 6:06:24 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce

Thank you.  

Dan

Happy Holidays.

Bruce_L
Posted : Monday, December 22, 2014 11:12:00 AM


Worden Trainer

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

You're welcome. Happy Holidays!



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