From the February 01, 2011 issue of Futures Magazine • Subscribe!

Additional Codes

CLOSE RANGE BREAKOUT

This opening range breakout system — with a built-in trend filter — manufactures an opening price for reference.

Sub CloseRangeBreakout(RePointNumber,Mult,Misc,Hold)

Dim Center As BarArray

Dim HLRatio As BarArray

If RePointNumber=-1 Then

Center=NextOpen(0)

End If

If RePointNumber=0 Then

Center=Close

End If

If RePointNumber=1 Then

Center=(High+Low+Close)/3

End If

If RePointNumber=2 Then

If High-Low<>0 Then

HLRatio=(Close-Low)/(High-Low)

Else

HLRatio=.5

End If

If HLRatio>.7 Then

Center=Close-Misc*Range

End If

If HLRatio<.3 Then

Center=Close+Misc*Range

End If

End If

If RePointNumber=3 Then

Center=Close+Misc*Range

End If

' Commented out will be uncommented and be enabled for in some tests

'If CloseClose[40] Then Buy("LE",1,Center+Mult*Average(TrueRange,3,0),Stop,Day)

'If Close>Close[1] And CloseMult*Average(TrueRange,3,0),Stop,Day)

Buy("LE",1,Center+Mult*Average(TrueRange,3,0),Stop,Day)

Sell("SE",1,Center-Mult*Average(TrueRange,3,0),Stop,Day)

If BarsSinceEntryplus("LE")>Hold And marketposition=1 Then

ExitLong("LXTime","LE",1,Close,Limit,Day)

End If

If BarsSinceEntryplus("SE")>Hold And marketposition=-1 Then

ExitShort("SXTime","SE",1,Close,Limit,Day)

End if

'ExitShort("SX","SE",1,Center+Mult*Average(TrueRange,3,0),Stop,Day)

'ExitLong("LX","LE",1,Center-Mult*Average(TrueRange,3,0),Stop,Day)

marketbreakdown2()

End Sub

SHELL SHOCKED

This code includes the shell to test the trading systems that make up the overall model. Using this technology, we can look at multiple training and out-of-sample windows to adjust our rules.

Sub SAATest(PercentNear, TrainingSize, OutOfSampleSize, Report)

' Declare vars

Dim i As Integer

Dim TrAve1, InterAve1 As Double

Dim TrAve2, InterAve2 As Double

Dim Mode1, Mode2 As String

Dim Sum1, Sum2 As Double

Dim PP1, PP2 As Double

Dim DD1, DD2 As Double

' On first step create systems

If BarNumber=FirstBar Then

' Reinit

SBOT_Initialize()

Prepare System1

SBOT_StartPrepareSystem("System1")

SBOT_AddParamRange("TrLen",4,40,2)

SBOT_AddParamRange("InterLen",4,40,2)

SBOT_FinishPrepareSystem()

SBOT_SetCurrentSystem("System1")

SBOT_PrepareNeighborhoodRating(PercentNear)

' Prepare System2

SBOT_StartPrepareSystem("System2")

SBOT_AddParamRange("TrLen",4,40,2)

SBOT_AddParamRange("InterLen",4,40,2)

SBOT_FinishPrepareSystem()

SBOT_SetCurrentSystem("System2")

SBOT_PrepareNeighborhoodRating(PercentNear)

' Prepare Walk Forward

SBOT_PrepareWalkForward(TrainingSize, OutOfSampleSize)

End If

' Start new bar (here yesterday's orders are processed)

SBOT_SetCurrentBarInfo(Date[0], Open[0], High[0], Low[0], Close[0], Vol[0])

' Process all subsystems for System1

SBOT_SetCurrentSystem("System1")

For i = 0 To SBOT_SubsystemCount - 1

' Place subsystem orders

TrAve1 = Average(Close, SBOT_SubsystemParam(i, "TrLen"), 0)

InterAve1 = Average(Close Of independent1, SBOT_SubsystemParam(i, "InterLen"), 0)

If Close < TrAve1 And Close Of independent1 < InterAve1 Then

SBOT_Buy(i, 0, Market)

End If

If Close > TrAve1 And Close Of independent1 > InterAve1 Then

SBOT_Sell(i, 0, Market)

End If

Next

' Process all subsystems for System2

SBOT_SetCurrentSystem("System2")

For i = 0 To SBOT_SubsystemCount - 1

' Place subsystem orders

TrAve2 = Average(Close, SBOT_SubsystemParam(i, "TrLen"), 0)

InterAve2 = Average(Close Of independent2, SBOT_SubsystemParam(i, "InterLen"), 0)

If Close > TrAve2 And Close Of independent2 > InterAve2 Then

SBOT_Buy(i, 0, Market)

End If

If Close < TrAve2 And Close Of independent2 < InterAve2 Then

SBOT_Sell(i, 0, Market)

End If

Next

' Select best subsystem on the end of window using Neighborhood Rating

If SBOT_WalkForwardWindowStartOffset(SBOT_CurrentWalkForwardWindowIndex) - TrainingSize = 0 Then

' Select System1 and select best subsystem as current

SBOT_SetCurrentSystem("System1")

i = SBOT_BestNeighborhoodIndex(SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

SBOT_CurrentSubsystemIndex =

SBOT_NeighborhoodMaxSumSubsystemIndex(i, SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Sum1 = SBOT_TradeReportSum

PP1 = SBOT_TradeReportPercentPositive

DD1 = SBOT_TradeReportMaxDrawDown

' Select System2 and select best subsystem as current

SBOT_SetCurrentSystem("System2")

i = SBOT_BestNeighborhoodIndex(SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

SBOT_CurrentSubsystemIndex =

SBOT_NeighborhoodMaxSumSubsystemIndex(i, SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Sum2 = SBOT_TradeReportSum

PP2 = SBOT_TradeReportPercentPositive

DD2 = SBOT_TradeReportMaxDrawDown

End If

' Trade

If SBOT_WalkForwardWindowStartOffset(SBOT_CurrentWalkForwardWindowIndex) - TrainingSize > 0 Then

' Select System1 and get current mode

SBOT_SetCurrentSystem("System1")

Mode1 = SBOT_SubsystemMode(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, SBOT_USEBARS)

' Select System2 and get current mode

SBOT_SetCurrentSystem("System2")

Mode2 = SBOT_SubsystemMode(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, SBOT_USEBARS)

' Place orders

If (Mode1 = "Long") or(Mode2 = "Long") Then

Buy("", 1, 0, Market, Day)

Else

If (Mode1 = "Short" ) and (Mode2 = "Short") Then

Sell("", 1, 0, Market, Day)

Else

ExitShort("", "", 1, 0, Market, Day)

ExitLong("", "", 1, 0, Market, Day)

End If

End If

End If

' Report

If SBOT_WalkForwardWindowStartOffset(SBOT_CurrentWalkForwardWindowIndex) - TrainingSize - OutOfSampleSize = 0 Then

If Report Then

Print "Training Start: ", SBOT_WalkForwardWindowTrainingStartDate(SBOT_CurrentWalkForwardWindowIndex)

Print "Training End: ", SBOT_WalkForwardWindowTrainingEndDate(SBOT_CurrentWalkForwardWindowIndex)

Print "Run Start: ", SBOT_WalkForwardWindowRunStartDate(SBOT_CurrentWalkForwardWindowIndex)

Print "Run End: ", SBOT_WalkForwardWindowRunEndDate(SBOT_CurrentWalkForwardWindowIndex)

' Select System1 and report

SBOT_SetCurrentSystem("System1")

Print "System1:"

Print "Optimal parameters: TrLen = ", SBOT_SubsystemParam(SBOT_CurrentSubsystemIndex, "TrLen"), " InterLen = ", SBOT_SubsystemParam(SBOT_CurrentSubsystemIndex, "InterLen")

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_WalkForwardWindowStartOffset(SBOT_CurrentWalkForwardWindowIndex) - TrainingSize, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Print "Training NetProfit: ", SBOT_TradeReportSum * bigpointvalue

Print "Training Win%: ", SBOT_TradeReportPercentPositive

Print "Training DD: ", SBOT_TradeReportMaxDrawDown * bigpointvalue

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, OutOfSampleSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Print "Run NetProfit: ", SBOT_TradeReportSum * bigpointvalue

Print "Run Win%: ", SBOT_TradeReportPercentPositive

Print "Run DD: ", SBOT_TradeReportMaxDrawDown * bigpointvalue

' Select System2 and report

SBOT_SetCurrentSystem("System2")

Print "System2:"

Print "Optimal parameters: TrLen = ",

SBOT_SubsystemParam(SBOT_CurrentSubsystemIndex, "TrLen"), " InterLen = ", SBOT_SubsystemParam(SBOT_CurrentSubsystemIndex, "InterLen")

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_WalkForwardWindowStartOffset(SBOT_CurrentWalkForwardWindowIndex) - TrainingSize, TrainingSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Print "Training NetProfit: ", SBOT_TradeReportSum * bigpointvalue

Print "Training Win%: ", SBOT_TradeReportPercentPositive

Print "Training DD: ", SBOT_TradeReportMaxDrawDown * bigpointvalue

SBOT_PrepareTradeReport(SBOT_CurrentSubsystemIndex, SBOT_CURRENT, OutOfSampleSize, SBOT_USEBARS, SBOT_SKIPOPENTRADES)

Print "Run NetProfit: ", SBOT_TradeReportSum * bigpointvalue

Print "Run Win%: ", SBOT_TradeReportPercentPositive

Print "Run DD: ", SBOT_TradeReportMaxDrawDown * bigpointvalue

Print ""

End If

' Switch to next window

SBOT_CurrentWalkForwardWindowIndex =

SBOT_CurrentWalkForwardWindowIndex + 1

End If

End Sub

EQUITY SWITCH

This code allows us to trade based on our analysis of the equity curve of a second, identical system.

Sub CHANBREAKOUTFeedback (SLen,EQLB,EQMult)

Dim MinMove

Dim virprofit As BarArray

Dim virlongprofit As BarArray

Dim virshortprofit As BarArray

MinMove=GetActiveMinMove()

If ((BarNumber<500) Or (virlongprofit-virlongprofit[EQLB]>-EQMult*Average(Range,EQLB,0)*bigpointvalue)) Then

Buy("ChanBuy",1,Highest(High,SLen,0)+MinMove ,Stop,Day)

End If

If ((BarNumber<500) Or (virshortprofit-virshortprofit[EQLB]>-EQMult*Average(Range,EQLB,0)*bigpointvalue)) Then

Sell("ChanSell",1,Lowest(Low,SLen,0)-MinMove,Stop,Day)

End if

VirtualBuy("ChanBuy",1,Highest(High,SLen,0)+MinMove ,Stop,Day)

VirtualSell("ChanSell",1,Lowest(Low,SLen,0)-MinMove,Stop,Day)

ExitShort("SX","ChanSell",1,Highest(High,SLen,0)+MinMove ,Stop,Day)

ExitLong("LX","ChanBuy",1,Lowest(Low,SLen,0)-MinMove,Stop,Day)

virprofit=VirGetPerformance("NetProfit")

virlongprofit=VirGetSysPerformance("LN",0)

virshortprofit=VirGetSysPerformance("SN",0)

End Sub

Page 1 of 3
Comments
comments powered by Disqus

eNewsletter Signup

Get the latest news and timely trading strategies for stock, options, forex, commodity, and financial derivatives markets with Futures' Daily Market Focus - FREE!