79
Optimization 201 – Optimization 201 – Bruce Robinson [email protected] March 11, 2006 March 11, 2006

Optimization 201 Seminar

Embed Size (px)

DESCRIPTION

opt201a

Citation preview

Page 1: Optimization 201 Seminar

Optimization 201 –Optimization 201 –

Bruce [email protected]

March 11, 2006March 11, 2006

Page 2: Optimization 201 Seminar

Presentation OverviewPresentation Overview Optimization 101 ReviewOptimization 101 Review Signal FormsSignal Forms How the Amibroker Backtester WorksHow the Amibroker Backtester Works Selection, Timing, and Trading VariablesSelection, Timing, and Trading Variables Using the Optimize FunctionUsing the Optimize Function How the Intelligent Optimizer worksHow the Intelligent Optimizer works IO Directives and OptionsIO Directives and Options Example 1 – tuned sector trading systemExample 1 – tuned sector trading system Example 2 – weighted position portfolio systemExample 2 – weighted position portfolio system Methodology and ConclusionsMethodology and Conclusions

Page 3: Optimization 201 Seminar
Page 4: Optimization 201 Seminar

Optimization 101 ReviewOptimization 101 Review

Page 5: Optimization 201 Seminar

Why do we model, backtest, and Why do we model, backtest, and

optimizeoptimize - - ModelModel

To define an “edge” in timing, selection, money To define an “edge” in timing, selection, money managementmanagement

A model approximates historyA model approximates history Accuracy is required, but not exactnessAccuracy is required, but not exactness

Back-testBack-test To verify that edge, know the odd’sTo verify that edge, know the odd’s To test across different marketsTo test across different markets To provide confidence going forwardTo provide confidence going forward

OptimizeOptimize To maximize or minimize those factors that are To maximize or minimize those factors that are

most importantmost important CAR, MDD, Sharpe, UPI, etc.CAR, MDD, Sharpe, UPI, etc. The combination is called “fitness”The combination is called “fitness”

Page 6: Optimization 201 Seminar

Back-testingBack-testing

Big QuestionBig Question – What percentage of the – What percentage of the back-test return, MDD, etc. should you back-test return, MDD, etc. should you expect going forward ?expect going forward ?

TODAYTODAY2003200320012001

BACKTEST AND BACKTEST AND OPTIMIZEOPTIMIZE

??????

Page 7: Optimization 201 Seminar

The back-fitting sensitivity The back-fitting sensitivity problem -problem -

Page 8: Optimization 201 Seminar

Other issues -Other issues -

Back-testingBack-testing Different market action vs. recent market Different market action vs. recent market

actionaction Test over bull, bear, sideways marketsTest over bull, bear, sideways markets

Number of signal samples - statistical Number of signal samples - statistical significance significance Issue for intermediate term systemsIssue for intermediate term systems

Survivorship biasSurvivorship bias Bad performing stocks fold, or mergeBad performing stocks fold, or merge Good funds’ record is boughtGood funds’ record is bought Bad funds record is buriedBad funds record is buried

Selection biasSelection bias Testing from current watch listsTesting from current watch lists

Page 9: Optimization 201 Seminar

AI optimization -AI optimization - AI optimization offers many new capabilitiesAI optimization offers many new capabilities To date we have fitted as much data as To date we have fitted as much data as

possible and “hoped”possible and “hoped” Mechanization is one of the most important Mechanization is one of the most important

capabilitiescapabilities Eliminates biases - selection, event knowledge, etc.Eliminates biases - selection, event knowledge, etc.

Less sensitive solutions can be found for a Less sensitive solutions can be found for a point in timepoint in time

We can “time travel” to points in the past and We can “time travel” to points in the past and look at walk forward performancelook at walk forward performance

A “spliced” equity curve can be assembled to A “spliced” equity curve can be assembled to look at what would have happened if the look at what would have happened if the system had been traded forward OOS.system had been traded forward OOS.

Page 10: Optimization 201 Seminar

Keywords –Keywords –

Near optimalNear optimal RobustRobust Out of sample, walk-forwardOut of sample, walk-forward

Page 11: Optimization 201 Seminar

Near optimality -Near optimality -

Page 12: Optimization 201 Seminar

Robustness -Robustness -

Page 13: Optimization 201 Seminar

Rolling walk forward Rolling walk forward testingtesting

TODAYTODAY2002200219981998 20002000

Green segments represent equity that Green segments represent equity that COULD have been achieved !COULD have been achieved !

Page 14: Optimization 201 Seminar

Anchored walk forward Anchored walk forward testingtesting

TODAYTODAY2002200219981998 20002000

Green segments represent equity Green segments represent equity that COULD have been achieved !that COULD have been achieved !

Page 15: Optimization 201 Seminar

Goals and penalties -Goals and penalties - Goal assigns a minimum desired value to a Goal assigns a minimum desired value to a

fitness field name (AB optimization result fitness field name (AB optimization result column)column)

Values below the goal result in an adjustment Values below the goal result in an adjustment factor that is applied to fitness.factor that is applied to fitness.

Two types of scaling – multiplier and Two types of scaling – multiplier and exponentialexponential

Example of multiplierExample of multiplier Fitness is UPIFitness is UPI UPI of solution point = 2UPI of solution point = 2 CAR = 15, CAR goal = 20CAR = 15, CAR goal = 20 Fitness adjustment is .75 * 2 = 1.5Fitness adjustment is .75 * 2 = 1.5

Page 16: Optimization 201 Seminar

SensitivitySensitivity

Definition - change in fitness with Definition - change in fitness with respect to “distance” from solutionrespect to “distance” from solution

Rationale – since change in inputs Rationale – since change in inputs can’t be accurately predicted, can’t be accurately predicted, approach is to measure change in approach is to measure change in output based on change in parametersoutput based on change in parameters

An average percentage is determined An average percentage is determined based on samplesbased on samples

The solution point fitness is adjusted The solution point fitness is adjusted by a sensitivity goalby a sensitivity goal

Page 17: Optimization 201 Seminar

The sensitivity adjusted The sensitivity adjusted fitness -fitness -

Page 18: Optimization 201 Seminar

Signal FormsSignal Forms

Page 19: Optimization 201 Seminar

Amibroker signals - Amibroker signals -

Trading signal logic in Amibroker Trading signal logic in Amibroker involves basic Boolean logicinvolves basic Boolean logic

Signals can be in two forms – level Signals can be in two forms – level and impulseand impulse

But, it also involves manipulation of But, it also involves manipulation of signal statesignal state

This results in many special casesThis results in many special cases Rather than try to detail these cases, Rather than try to detail these cases,

guidelines will be offered to avoid guidelines will be offered to avoid themthem

Page 20: Optimization 201 Seminar

Boolean logic - Boolean logic - Amibroker Amibroker There are a few special considerations for There are a few special considerations for

Boolean logic in AmibrokerBoolean logic in Amibroker In AB, the value 0 is FALSE, any other value In AB, the value 0 is FALSE, any other value

is TRUEis TRUE Boolean logic is most often applied to a vectorBoolean logic is most often applied to a vector Boolean logic where Nulls are involved are Boolean logic where Nulls are involved are

special cases special cases True AND Null = FalseTrue AND Null = False True OR Null = TrueTrue OR Null = True NOT Null = NullNOT Null = Null

Why is this important –Why is this important – It allows indicators derived from tickers that start It allows indicators derived from tickers that start

at different dates to have Boolean operators at different dates to have Boolean operators appliedapplied

Page 21: Optimization 201 Seminar

Impulse form examples -Impulse form examples - One bar event – Cross() function at 50 level of One bar event – Cross() function at 50 level of

StochasticStochastic

Data by www.FastTrack.netData by www.FastTrack.net

Page 22: Optimization 201 Seminar

Impulse formImpulse form

Impulse form can be thought of as Impulse form can be thought of as an eventan event

Also, think of impulse form as the Also, think of impulse form as the leading edge of the level formleading edge of the level form

It is generated by functions that It is generated by functions that detect events such as Cross()detect events such as Cross()

It becomes very useful in generating It becomes very useful in generating trading signal combinationstrading signal combinations

Page 23: Optimization 201 Seminar

The need for stateThe need for state

What is state ?What is state ? It is a form of digital memoryIt is a form of digital memory It retains that value of the last Buy or Sell It retains that value of the last Buy or Sell

impulse signalimpulse signal It is usually in level formIt is usually in level form

Main reasons why it is needed ?Main reasons why it is needed ? Not all signal conditions can be described by Not all signal conditions can be described by

Boolean logicBoolean logic In certain situations, “redundant” signals are In certain situations, “redundant” signals are

desirabledesirable Let’s look at examples of eachLet’s look at examples of each

Page 24: Optimization 201 Seminar

Example 2 of state Example 2 of state requirementrequirement Take the earlier example, but of a stochastic, but Buy on going Take the earlier example, but of a stochastic, but Buy on going

up thru 60 and sell on going down thru 40 – can you describe up thru 60 and sell on going down thru 40 – can you describe the area between 40 and 60 with a Boolean statement ?the area between 40 and 60 with a Boolean statement ?

Data by www.FastTrack.netData by www.FastTrack.net

Page 25: Optimization 201 Seminar

Example 2 of state Example 2 of state requirementrequirement For example, the Buy or Sell result of a For example, the Buy or Sell result of a

stochastic of 50 depends on how you got therestochastic of 50 depends on how you got there

Data by www.FastTrack.netData by www.FastTrack.net

Page 26: Optimization 201 Seminar

State and Impulse FormState and Impulse Form

Since there are two forms of signals, Since there are two forms of signals, how do we get from one form to the how do we get from one form to the otherother

Flip() is used to implement the state Flip() is used to implement the state “memory”“memory”

The output of Flip() is a level signalThe output of Flip() is a level signal

Page 27: Optimization 201 Seminar

State and Impulse FormState and Impulse Form

ExRem( Array1, Array2 ) is used to ExRem( Array1, Array2 ) is used to convert from state form to impulse convert from state form to impulse formform

From the Amibroker help –From the Amibroker help – removes excessive signals:removes excessive signals: returns 1 on the first occurence of "true" signal in Array1returns 1 on the first occurence of "true" signal in Array1 then returns 0 until Array2 is true even if there are "true" then returns 0 until Array2 is true even if there are "true"

signals in Array1 signals in Array1

The output of ExRem() is an impulse The output of ExRem() is an impulse signalsignal

Page 28: Optimization 201 Seminar

Guidelines for combining Guidelines for combining signals -signals - These guidelines have an overall goal of These guidelines have an overall goal of

keeping as many redundant signals as keeping as many redundant signals as possiblepossible Perform the And operation in level/state formPerform the And operation in level/state form Perform the Or operation in impulse formPerform the Or operation in impulse form Not operation should be done in level/state Not operation should be done in level/state

formform Postpone the Flip() that is used to get state Postpone the Flip() that is used to get state

form as long as possibleform as long as possible Try to make buy and sell signal combinations Try to make buy and sell signal combinations

symmetric to minimize lockupsymmetric to minimize lockup

Page 29: Optimization 201 Seminar

How the Amibroker How the Amibroker Backtester WorksBacktester Works

Page 30: Optimization 201 Seminar

Backtester(s) -Backtester(s) -

There are really 3 types of current There are really 3 types of current backtesters in Amibrokerbacktesters in Amibroker Individual issueIndividual issue Portfolio trading with signalsPortfolio trading with signals Portfolio trading with rotationPortfolio trading with rotation

Plus the “Old backtester”Plus the “Old backtester” It is important to first decide on your It is important to first decide on your

goalgoal

Page 31: Optimization 201 Seminar

Functional Functional considerationsconsiderations The AFL program will have at least one The AFL program will have at least one

execution per issueexecution per issue There may be other executions if There may be other executions if

parameters are involved, for example.parameters are involved, for example. If the custom backtester is enabled, it will If the custom backtester is enabled, it will

have a final post-processing pass that can have a final post-processing pass that can interface to the backtesting engine and its interface to the backtesting engine and its objectsobjects

The equity and stat’s results are not The equity and stat’s results are not available until the backtester is completeavailable until the backtester is complete

Page 32: Optimization 201 Seminar

Data flow modelData flow model

Data initialization

Automatic

Analysis

Code

All

Stocks

Current

Stock

Filter

Back-Tester

RESULTS

Page 33: Optimization 201 Seminar

Portfolio backtesterPortfolio backtester The backtester can be used to perform The backtester can be used to perform

selection and timing on a filter list on selection and timing on a filter list on input issuesinput issues

Number of positions and position size Number of positions and position size can be specifiedcan be specified

The condition of buys for more than the The condition of buys for more than the number of positions must be resolvednumber of positions must be resolved PositionScore can be used to assign PositionScore can be used to assign

prioritiespriorities Only evaluated at the buy impulse for non-Only evaluated at the buy impulse for non-

rotational systemsrotational systems

Page 34: Optimization 201 Seminar

www.amibroker.com/gifs/bt_regular.gif

Page 35: Optimization 201 Seminar

Selection, Timing, and Selection, Timing, and Trading VariablesTrading Variables

Page 36: Optimization 201 Seminar

Boilerplate 1 settings Boilerplate 1 settings code -code -

//EnableRotationalTrading();//EnableRotationalTrading();

BuyPriceBuyPrice = = SellPriceSellPrice = = ShortPriceShortPrice = = CoverPriceCoverPrice = = OpenOpen;;

SetTradeDelaysSetTradeDelays( 1, 1, 1, 1 );( 1, 1, 1, 1 );

SetFormulaNameSetFormulaName("TEST");("TEST");

Page 37: Optimization 201 Seminar

Boilerplate 2 settings Boilerplate 2 settings code -code -

SetOption(SetOption( "InitialEquity","InitialEquity",10001000 ););SetOption(SetOption( "MinShares", "MinShares", .0001.0001 ););SetOption(SetOption( "MinPosValue","MinPosValue", 00

););SetOption(SetOption( "FuturesMode", "FuturesMode", FalseFalse ););

SetOption(SetOption( "AllowPositionShrinking", "AllowPositionShrinking", TrueTrue ););SetOption(SetOption( "ActivateStopsImmediately","ActivateStopsImmediately",TrueTrue ););SetOption(SetOption( "ReverseSignalForcesExit", "ReverseSignalForcesExit", TrueTrue ););SetOption(SetOption( "AllowSameBarExit","AllowSameBarExit", TrueTrue ););

Page 38: Optimization 201 Seminar

Boilerplate 2 code Boilerplate 2 code considerations -considerations - Initial equity needs to be large enough to Initial equity needs to be large enough to

accommodate MinShare and number of positionsaccommodate MinShare and number of positions AllowPositionShrinking is usually enabled to AllowPositionShrinking is usually enabled to

allow the buy less than the PositionSize and to allow the buy less than the PositionSize and to account for round-off erroraccount for round-off error

ActivateStopsImmediately has some interactions ActivateStopsImmediately has some interactions with other settings and will be explained in detail with other settings and will be explained in detail laterlater

ReverseSignalForcesExit allows for long/short ReverseSignalForcesExit allows for long/short switches without an explicit Sell of Coverswitches without an explicit Sell of Cover

AllowSameBarExit can be used for single day AllowSameBarExit can be used for single day trade, but is also useful for special purposestrade, but is also useful for special purposes

Page 39: Optimization 201 Seminar

Boilerplate 3 settings Boilerplate 3 settings code -code -

SetOptionSetOption(( "CommissionMode", "CommissionMode", 22 ););SetOptionSetOption(( "CommissionAmount", "CommissionAmount", 00

););SetOptionSetOption(( "InterestRate", "InterestRate", 00

););SetOptionSetOption(( "MarginRequirement", "MarginRequirement", 100100 ););SetOptionSetOption(( "MaxOpenPositions", "MaxOpenPositions", 11

););SetOptionSetOption(( "WorstRankHeld", "WorstRankHeld", 44 ););SetOptionSetOption(( "PriceBoundChecking","PriceBoundChecking",TrueTrue ););SetOptionSetOption(( "UsePrevBarEquityForPosSizing","UsePrevBarEquityForPosSizing", TrueTrue ););

Page 40: Optimization 201 Seminar

Boilerplate 3 code Boilerplate 3 code considerations -considerations - Commission mode should be modeled as Commission mode should be modeled as

closely as possible, but is usually no closely as possible, but is usually no longer a major considerationlonger a major consideration

Percentage commission can be used to Percentage commission can be used to model “slippage”model “slippage”

Interest rate is fixed for the MM (Sell, Interest rate is fixed for the MM (Sell, Cover) positionCover) position

Margin requirement is the percentage of Margin requirement is the percentage of initial equity required. It is applied when initial equity required. It is applied when PositionSize <= 100 which may be a PositionSize <= 100 which may be a modeling considerationmodeling consideration

MaxOpenPositions applies to the Portfolio MaxOpenPositions applies to the Portfolio backtesterbacktester

Page 41: Optimization 201 Seminar

Boilerplate 3 code Boilerplate 3 code considerations -considerations - WorstRankHeld applies to rotational WorstRankHeld applies to rotational

mode and refers to the rank below mode and refers to the rank below which a current position is soldwhich a current position is sold

PriceBoundChecking is a special PriceBoundChecking is a special consideration if you want to consideration if you want to manipulate the trade price arrays manipulate the trade price arrays outside of the bar limitsoutside of the bar limits

UsePrevBarEquityForPosSizing is self-UsePrevBarEquityForPosSizing is self-explanatory, and is the typical mode explanatory, and is the typical mode for re-balancing used in fund portfoliosfor re-balancing used in fund portfolios

Page 42: Optimization 201 Seminar

Boilerplate 4 settings Boilerplate 4 settings code -code -

MaxPosMaxPos = = 100 * 100 /100 * 100 / GetOptionGetOption("MarginRequirement");("MarginRequirement");PositionSizePositionSize = -MaxPos / = -MaxPos / GetOptionGetOption("MaxOpenPositions");("MaxOpenPositions");

RoundLotSizeRoundLotSize = 0; = 0; // 0 for Funds, 100 for Stocks// 0 for Funds, 100 for StocksTickSizeTickSize = 0;= 0; // 0 for no min. size// 0 for no min. sizeMarginDepositMarginDeposit = 0;= 0;PointValuePointValue = 1; = 1; // For futures// For futures

ExitAtTradePrice ExitAtTradePrice = 0;= 0;ExitAtStopExitAtStop = 1;= 1;ExitNextBarExitNextBar = 2;= 2;ApplyStopApplyStop( ( stopTypeLossstopTypeLoss, , stopModeDisablestopModeDisable, 0, , 0, ExitAtTradePrice );ExitAtTradePrice );ApplyStopApplyStop( ( stopTypeProfitstopTypeProfit, , stopModeDisablestopModeDisable, 0, , 0, ExitAtTradePrice );ExitAtTradePrice );ApplyStopApplyStop( ( stopTypeTrailingstopTypeTrailing, , stopModeDisablestopModeDisable, 0, , 0, ExitAtTradePrice );ExitAtTradePrice );ApplyStopApplyStop( ( stopTypeNBarstopTypeNBar, , stopModeDisablestopModeDisable, 0, ExitAtTradePrice , 0, ExitAtTradePrice ););

Page 43: Optimization 201 Seminar

Boilerplate 4 code Boilerplate 4 code considerations -considerations - PositionSize is coded to account for PositionSize is coded to account for

marginmargin RoundLotSize is typically 0 for funds and RoundLotSize is typically 0 for funds and

100 for stocks, although odd-lots are not 100 for stocks, although odd-lots are not that un-economical and fractional stock that un-economical and fractional stock shares are possible at one brokerageshares are possible at one brokerage

A detailed explanation of stops is beyond A detailed explanation of stops is beyond the scope of this talkthe scope of this talk The main point to remember is that they do The main point to remember is that they do

NOT obey trade delaysNOT obey trade delays

Page 44: Optimization 201 Seminar

Using the Optimize Using the Optimize FunctionFunction

Page 45: Optimization 201 Seminar

The Optimize Statement - The general form of the optimize statement is The general form of the optimize statement is

––variable = Optimize( “description ID”, default, min, max, step )variable = Optimize( “description ID”, default, min, max, step )

When multiple optimize statements are executed by When multiple optimize statements are executed by the Optimize function –the Optimize function – A pass of the AFL code is made for each possible A pass of the AFL code is made for each possible

combinationcombination For example –For example –

x = Optimize( “X”, 5, 1, 10, 1 );x = Optimize( “X”, 5, 1, 10, 1 );y = Optimize( “Y”, 50, 100, 1000, 100 );y = Optimize( “Y”, 50, 100, 1000, 100 );

Passes –Passes –( 1, 100 )( 1, 100 )( 1, 200 )( 1, 200 )etc.etc.

Results are sorted by Net ProfitResults are sorted by Net Profit

Page 46: Optimization 201 Seminar

The Optimize Statement - Anything that can be represented by a Anything that can be represented by a

number can be assigned to an Optimize() number can be assigned to an Optimize() function result !function result ! Parameters to functionsParameters to functions True/False (1/0) to gate a conditionTrue/False (1/0) to gate a condition Comparison to Status(“stocknum”) to choose Comparison to Status(“stocknum”) to choose

from watchlistfrom watchlist SetOption() parameterSetOption() parameter PositionSizePositionSize

Optimize() parameters are cached – they Optimize() parameters are cached – they are not dynamicare not dynamic

Page 47: Optimization 201 Seminar

The Optimize Statement - GuidelinesGuidelines

Variables that are unique to tickers in a Variables that are unique to tickers in a watchlist must be defined separatelywatchlist must be defined separately

Variables have no knowledge on each otherVariables have no knowledge on each other So, there is no way to limit the total of N parameters So, there is no way to limit the total of N parameters

to a maximumto a maximum For example, For example,

Individual PositionSize variables for each ticker Individual PositionSize variables for each ticker cannot be calculated directlycannot be calculated directly

They are coded as weights that may vary from 0 to They are coded as weights that may vary from 0 to 100100

The sum is calculated The sum is calculated The scaled value for each ticker is its position sizeThe scaled value for each ticker is its position size

Page 48: Optimization 201 Seminar

How the Intelligent How the Intelligent Optimizer WorksOptimizer Works

Page 49: Optimization 201 Seminar

What IO does – the details - IO generates a number of “tuples” of IO generates a number of “tuples” of

variable combinations that are designed variable combinations that are designed to explore the problem spaceto explore the problem space For example –For example –

( 2, 200 )( 2, 200 )( 3, 200 )( 3, 200 )( 2, 300 )( 2, 300 )etc.etc.

It replaces each call to Optimize() with a It replaces each call to Optimize() with a call to IOptimize()call to IOptimize()

IOptimize() returns the value of each IOptimize() returns the value of each variable in the tuplevariable in the tuple

Page 50: Optimization 201 Seminar

What IO does – the big picture - IO must “drive” Amibroker from an IO must “drive” Amibroker from an

outside scriptoutside script Creates a modified version of the Creates a modified version of the

AFL to perform housekeeping and AFL to perform housekeeping and call IOptimize()call IOptimize()

Invoke the Optimize function Invoke the Optimize function repeatedly to test a set of tuplesrepeatedly to test a set of tuples

Analyze the set results to guide the Analyze the set results to guide the next search iterationnext search iteration

Page 51: Optimization 201 Seminar

IO Directives and IO Directives and OptionsOptions

Page 52: Optimization 201 Seminar

IO directives for traditional IO directives for traditional optimization optimization

//IO: StatusWindow: N//IO: StatusWindow: N//IO: SaveCancelled: Y//IO: SaveCancelled: Y

// Remove in final "production" run// Remove in final "production" run//xIO: FitnessTime: 300//xIO: FitnessTime: 300//xIO: SenFinalTests: 1000//xIO: SenFinalTests: 1000

//IO: SenOptGoalPct: 85//IO: SenOptGoalPct: 85//IO: SenOptTests: 10//IO: SenOptTests: 10//IO: Fitness: UPI//IO: Fitness: UPI//IO: Goal: CAR: 15//IO: Goal: CAR: 15

//xIO: BegISDate: 01/01/1995//xIO: BegISDate: 01/01/1995//xIO: EndISDate: 01/01/2001//xIO: EndISDate: 01/01/2001//xIO: EndOSDate: 9/15/2005//xIO: EndOSDate: 9/15/2005//xIO: WFAuto: Anchored: Every: Year//xIO: WFAuto: Anchored: Every: Year

5 minute limit1000 samples for sensitivity

Disable the directive

Penalize result < 85% sensitivity10 samples to determine sensitiviyCAR goal to prevent high UPI, low CAR

Walk-forward directives disabled

Page 53: Optimization 201 Seminar

Example of a Tuned, Sector Example of a Tuned, Sector Trading SystemTrading System

Page 54: Optimization 201 Seminar

Sector Trading -

Elevator pitch –Elevator pitch – Buy a sector fund when it reaches its “N” Buy a sector fund when it reaches its “N”

day lowday low Hold for a minimum of “H” daysHold for a minimum of “H” days Take profits when it reached “P” percentTake profits when it reached “P” percent

This is an oversold strategyThis is an oversold strategy Works well in non-bear marketsWorks well in non-bear markets BUT, it is a filter tuned to the swing BUT, it is a filter tuned to the swing

cycle frequency and the volatilitycycle frequency and the volatility

Page 55: Optimization 201 Seminar

Sector Trading Code -//IO: SenOptGoalPct: 85//IO: SenOptGoalPct: 85//IO: SenOptTests: 10//IO: SenOptTests: 10//IO: Fitness: CAR//IO: Fitness: CAR//xIO: Goal: CAR: 15//xIO: Goal: CAR: 15

//xIO: BegISDate: 01/01/1995//xIO: BegISDate: 01/01/1995//xIO: EndISDate: 01/01/2001//xIO: EndISDate: 01/01/2001//xIO: EndOSDate: 9/15/2005//xIO: EndOSDate: 9/15/2005//xIO: WFAuto: Anchored: Every: Year//xIO: WFAuto: Anchored: Every: Year

HoldDaysHoldDays = Optimize( "HoldDays", 10, 2, 12, 1 );= Optimize( "HoldDays", 10, 2, 12, 1 );

ProfitStopProfitStop = Optimize( "ProfitStop", 5, 2, 12, 1 );= Optimize( "ProfitStop", 5, 2, 12, 1 );

Page 56: Optimization 201 Seminar

Sector Trading Code -ExitAtTradePrice ExitAtTradePrice = 0;= 0;ExitAtStopIntraDayExitAtStopIntraDay = 1;= 1;ExitAtStopNextExitAtStopNext = 2;= 2;ApplyStop(ApplyStop(stopTypeLossstopTypeLoss, , stopModeDisablestopModeDisable, 0, ExitAtTradePrice);, 0, ExitAtTradePrice);ApplyStop(ApplyStop(stopTypeProfitstopTypeProfit, , stopModePercentstopModePercent, ProfitStop, , ProfitStop, ExitAtStopNext);ExitAtStopNext); // ExitAtTradePrice for same day// ExitAtTradePrice for same dayApplyStop(ApplyStop(stopTypeTrailingstopTypeTrailing, , stopModeDisablestopModeDisable, 0, , 0, ExitAtTradePrice);ExitAtTradePrice);ApplyStop(ApplyStop(stopTypeNBarstopTypeNBar, , stopModeBarsstopModeBars, HoldDays, , HoldDays, ExitAtTradePrice); //ExitAtStopNext);ExitAtTradePrice); //ExitAtStopNext);

NumDaysNumDays = Optimize( "NumDays", 10, 10, 20, 1 );= Optimize( "NumDays", 10, 10, 20, 1 );

LowDaysLowDays = IIf( LLV( = IIf( LLV( CC, NumDays ) == , NumDays ) == CC, 1, 0 );, 1, 0 );BuyBuy = LowDays;= LowDays;SellSell = 0;= 0; // Will be exited by N-bar stop or by // Will be exited by N-bar stop or by

Profit targetProfit targetShortShort = = CoverCover = 0; = 0;

Page 57: Optimization 201 Seminar

Optimization Net Profit from 4/1/03 -Optimization Net Profit from 4/1/03 -

Page 58: Optimization 201 Seminar

Optimization Net Profit from 4/1/00 Optimization Net Profit from 4/1/00 with 03 parms -with 03 parms -

Page 59: Optimization 201 Seminar

Optimization Net Profit from 4/1/00 -Optimization Net Profit from 4/1/00 -

Page 60: Optimization 201 Seminar

Optimization CAR/MDD from 4/1/03 Optimization CAR/MDD from 4/1/03 with IO -with IO -

Page 61: Optimization 201 Seminar

Optimization CAR/MDD from 4/1/ 00 Optimization CAR/MDD from 4/1/ 00 with 03 parms and IO -with 03 parms and IO -

Page 62: Optimization 201 Seminar

Sector System Optimization Sector System Optimization SummarySummary ( HoldDays, ProfitStop, NumDays )( HoldDays, ProfitStop, NumDays ) Amibroker optimized from 4/1/03 on Net Amibroker optimized from 4/1/03 on Net

ProfitProfit

( 8, 10, 11 )( 8, 10, 11 )

Amibroker optimized from 4/1/00 on Net Amibroker optimized from 4/1/00 on Net ProfitProfit

( 5, 6, 10 )( 5, 6, 10 ) IO optimized from 4/1/03 on CAR/MDDIO optimized from 4/1/03 on CAR/MDD

( 6, 5, 18 )( 6, 5, 18 )

Page 63: Optimization 201 Seminar

Optimization CAR/MDD from 4/1/ 03 – Optimization CAR/MDD from 4/1/ 03 –

Page 64: Optimization 201 Seminar

Optimization CAR/MDD from 4/1/ 03 – Optimization CAR/MDD from 4/1/ 03 –

Page 65: Optimization 201 Seminar

Optimization CAR/MDD from 4/1/ 03 – Optimization CAR/MDD from 4/1/ 03 –

Page 66: Optimization 201 Seminar

Optimization from CAR/MDD 4/1/ 03 Optimization from CAR/MDD 4/1/ 03 with IO -with IO -

Page 67: Optimization 201 Seminar

Sector Trading.afl

Implements this sector trading Implements this sector trading systemsystem

Runs against a watchlist of sectorsRuns against a watchlist of sectors Profunds Sectors – Japan-Europe.tlsProfunds Sectors – Japan-Europe.tls

Program and watchlist will be posted Program and watchlist will be posted to siteto site

Play with it !Play with it !

Page 68: Optimization 201 Seminar

Example os a Portfolio Example os a Portfolio Optimization SystemOptimization System

Page 69: Optimization 201 Seminar

The main portfolio problem - How to optimize the the PositionSize How to optimize the the PositionSize

for each tickerfor each ticker Let’s say the we have 5 long tickers, Let’s say the we have 5 long tickers,

and we want to optimize the portfolio and we want to optimize the portfolio percentages in a hedgepercentages in a hedge Hedge percentage is 30%Hedge percentage is 30% Long percentage is 70%Long percentage is 70%

Percentages of 5 long tickers must add Percentages of 5 long tickers must add to 70%to 70%

Issue is that variables are independentIssue is that variables are independent

Page 70: Optimization 201 Seminar

A solution - This “design pattern” is known as This “design pattern” is known as normalizingnormalizing First, we optimize First, we optimize weightingsweightings

fund1wtfund1wt = Optimize( "fund1wt", 10, 0, 100, = Optimize( "fund1wt", 10, 0, 100, 1 );1 );fund2wtfund2wt = Optimize( "fund2wt", 10, 0, 100, = Optimize( "fund2wt", 10, 0, 100, 1 );1 );fund3wtfund3wt = Optimize( "fund3wt", 10, 0, 100, = Optimize( "fund3wt", 10, 0, 100, 1 );1 );fund4wtfund4wt = Optimize( "fund4wt", 10, 0, 100, = Optimize( "fund4wt", 10, 0, 100, 1 );1 );fund5wtfund5wt = Optimize( "fund5wt", 10, 0, 100, = Optimize( "fund5wt", 10, 0, 100, 1 );1 );

Page 71: Optimization 201 Seminar

A solution - Next, we get the sum of the weightsNext, we get the sum of the weights

fundwtsumfundwtsum = fund1wt + fund2wt + fund3wt + fund4wt + = fund1wt + fund2wt + fund3wt + fund4wt + fund5wt;fund5wt;

Then, set the total percentage of the long fundsThen, set the total percentage of the long fundsfundpcntfundpcnt = 70;= 70;

Lastly, we normalize the percentagesLastly, we normalize the percentages

fund1pcntfund1pcnt = fund1wt / fundwtsum * fundpcnt;= fund1wt / fundwtsum * fundpcnt;fund2pcntfund2pcnt = fund2wt / fundwtsum * fundpcnt;= fund2wt / fundwtsum * fundpcnt;fund3pcntfund3pcnt = fund3wt / fundwtsum * fundpcnt;= fund3wt / fundwtsum * fundpcnt;fund4pcntfund4pcnt = fund4wt / fundwtsum * fundpcnt;= fund4wt / fundwtsum * fundpcnt;fund5pcntfund5pcnt = fund5wt / fundwtsum * fundpcnt;= fund5wt / fundwtsum * fundpcnt;

Page 72: Optimization 201 Seminar

Why it works - The weights are all allowed to vary over the same The weights are all allowed to vary over the same

rangerange IO optimizes the weights based on the IO optimizes the weights based on the

percentages assigned to PositionSizepercentages assigned to PositionSize

ifif ( Name() == fund1 ) ( Name() == fund1 )PositionSizePositionSize = -fund1pcnt;= -fund1pcnt;

ifif ( Name() == fund2 ) ( Name() == fund2 )PositionSizePositionSize = -fund2pcnt;= -fund2pcnt;

ifif ( Name() == fund3 ) ( Name() == fund3 )PositionSizePositionSize = -fund3pcnt;= -fund3pcnt;

ifif ( Name() == fund4 ) ( Name() == fund4 )PositionSizePositionSize = -fund4pcnt;= -fund4pcnt;

ifif ( Name() == fund5 ) ( Name() == fund5 )PositionSizePositionSize = -fund5pcnt;= -fund5pcnt;

Page 73: Optimization 201 Seminar

PortHedge2IO.afl-

Portfolio and hedging optimization modelPortfolio and hedging optimization model Originally presented in Clearwater in 2005Originally presented in Clearwater in 2005 Has two modes –Has two modes –

Finds near efficient frontier for up to 5 fundsFinds near efficient frontier for up to 5 funds Optimizes the mix for an augmented hedge with Optimizes the mix for an augmented hedge with

up to 3 long funds, 3 short funds, and money up to 3 long funds, 3 short funds, and money marketmarket

Designed to use IODesigned to use IO Uses FastTrack symbolsUses FastTrack symbols Hedging mode uses FastTrack based signalsHedging mode uses FastTrack based signals

Page 74: Optimization 201 Seminar

Methodology and Methodology and ConclusionsConclusions

Page 75: Optimization 201 Seminar

Steps of the MethodologySteps of the Methodology

Conceptualize – Conceptualize – Describe the system in an “elevator pitch”Describe the system in an “elevator pitch”

CodeCode Be aware of Amibroker backtester Be aware of Amibroker backtester

considerationsconsiderations Check the tradesCheck the trades

Refine and optimizeRefine and optimize Select in-sample data appropriate to the style Select in-sample data appropriate to the style

and time frame of tradingand time frame of trading Identify the parameters and degrees of freedomIdentify the parameters and degrees of freedom

Be aware of implicit constraintsBe aware of implicit constraints

Page 76: Optimization 201 Seminar

Steps of the MethodologySteps of the Methodology

Analyze in-sample resultsAnalyze in-sample results Perform sensitivity analysisPerform sensitivity analysis Analyze drawdowns and other factors that Analyze drawdowns and other factors that

would result in suspension of tradingwould result in suspension of trading Decide if further testing is warrantedDecide if further testing is warranted

Don’t be afraid to discard systems if Don’t be afraid to discard systems if solutions to the identified issues are solutions to the identified issues are elusiveelusive

Remember – discarding an inferior system Remember – discarding an inferior system frees up time to investigate others that frees up time to investigate others that may prove superiormay prove superior

Page 77: Optimization 201 Seminar

System LifetimeSystem Lifetime

Should systems work over different market Should systems work over different market characteristics ? characteristics ?

If so, compromises are inevitableIf so, compromises are inevitable Is it a viable strategy to target the system to Is it a viable strategy to target the system to

certain market conditions ?certain market conditions ? If we can If we can detectdetect the transitions in market types the transitions in market types If we have a “circuit breaker” to If we have a “circuit breaker” to suspend suspend trading trading

An approach to detection and suspension is An approach to detection and suspension is to trade the equity curveto trade the equity curve

Page 78: Optimization 201 Seminar

Steps of the MethodologySteps of the Methodology

Perform walk-forward testingPerform walk-forward testing Look for results that are less than optimized, Look for results that are less than optimized,

but within reasonbut within reason Walk-forward frequency should be looked at as Walk-forward frequency should be looked at as

an additional parameteran additional parameter Re-optimization frequency is being identifiedRe-optimization frequency is being identified Don’t assume that more is betterDon’t assume that more is better System stability with respect to re-optimization is an System stability with respect to re-optimization is an

issueissue

“ “ The future ain't what it used to be “ – The future ain't what it used to be “ – Yogi BerraYogi Berra

Page 79: Optimization 201 Seminar

Conclusion -Tool are now available to optimize, evaluate, and perform a walk-forward analysis – don’t accept less