Better Optimization

Embed Size (px)

Citation preview

  • 8/9/2019 Better Optimization

    1/12

    Better Optimization in TradeStation, Part 2 

    In the November 2004 issue

    (http://www.breakoutfutures.com/Newsletters/Newsletter1104.htm) I presente! ametho! for optimi"in# a tra!in# s$stem in %ra!e&tation usin# a custom ob'ectivefunction written in %ra!e&tations as$*an#ua#e scriptin# lan#ua#e. %he purpose of the

    as$*an#ua#e function calle! +t$,orr was to make it possible to fin! s$stemparameter values that pro!uce a relativel$ linear e+uit$ curve with #oo! net

    profitabilit$. %ra!in# s$stems with linear (strai#ht-line) e+uit$ curves have uniformprofitabilit$ over !ifferent time perio!s which shoul! make them more likel$ to

    perform well in the future compare! to s$stems that are more tailore! to specificmarket con!itions. In this months newsletter I present an new ob'ective function that

    has certain a!vanta#es over the metho! previousl$ presente!.

     skin# a tra!in# s$stem to perform e+uall$ well in all time perio!s is a fairl$ arbitrar$

    re+uest #iven that the markets themselves are an$thin# but uniform. It probabl$makes more sense to ask what the potential profit is over a #iven perio! an! then ask

    the s$stem to perform as close to that potential as possible. %his kin! of thinkin# le!me to somethin# I refer to as the i!eal e+uit$ curve optimi"ation metho!.

    %he basic i!ea is not new. In his book esi#n %estin# an! ptimi"ation of %ra!in#&$stems (ohn 3ile$ &ons Inc. New 5ork 1662 pp. 74-78) 9obert ar!o

    !escribes an optimi"ation metho! base! on the perfect profit which he !efines asthe sum of the absolute price !ifferences. tra!in# s$stem that #enerate! perfect

    profit woul! bu$ the close if the ne;t !a$ was #oin# to close hi#her an! sell the close if the ne;t !a$ was #oin# to be lower. bviousl$ no such s$stem is possible but the

    e+uit$ curve #enerate! b$ this h$pothetical s$stem represents the potential profit inthe market. %o use the perfect profit in s$stem optimi"ation ar!o ma;imi"e! the

    correlation coefficient between the e+uit$ curve represente! b$ the perfect profit an!the e+uit$ curve of the tra!in# s$stem. %he optimal s$stem parameter values were

    those that resulte! in the e+uit$ curve closest to the perfect profit.

     9ather than use the sum of the absolute price !ifferences to #enerate a perfect

    e+uit$ curve m$ thou#ht was to i!entif$ a set of i!eal turnin# points on a chart an!use those turnin# points to #enerate an i!eal e+uit$ curve as if we were tra!in# a stop-

    an!-reverse s$stem throu#h those points.

  • 8/9/2019 Better Optimization

    2/12

     Figure 1. Ideal turning points in T-Bond futures, as identified by

    funtion Ideal!"tyOpt#. T$e turning points are used to define a $ypot$etial,ideal trading system t$at represents t$e profit potential in t$e mar%et.

     

    =i#. 1 illustrates what I mean b$ i!eal turnin# points. %he i!eal turnin#points are marke! with bu$ an! sell si#nals from a h$pothetical s$stemthat reverses from lon# to short an! short to lon# at the turnin# points.>ow fre+uentl$ the reversals are tri##ere! will !epen! on how bi# amarket move it takes to reverse the tren!. In the functionI!eal+t$pt8 shown below in =i#. 2 the si"e of the move nee!e!to reverse the tren! can be specifie! either in terms of points or as afraction of the n-!a$ avera#e true ran#e. nce it has been !etermine!that the tren! has reverse! the i!eal turnin# point is locate!. 3hen the

  • 8/9/2019 Better Optimization

    3/12

    tren! reverses from short to lon# the turnin# point is the lowest closesince bein# short. 3hen the tren! reverses from lon# to short the

    turnin# point is the hi#hest close since bein# lon#. s an asi!e the i!eal tra!es forme! b$ the turnin# points mi#ht makea #oo! trainin# set of tra!es for a neural network-base! s$stem. 9atherthan scannin# a chart manuall$ to come up with tra!es to fee! into theneural network the metho! !escribe! here coul! be use! toautomaticall$ fin! the trainin# set. %he I!eal+t$pt8 function keeps track of the e+uit$ from both the i!eals$stem an! the actual tra!in# s$stem. %he e+uit$ curve for the i!eals$stem is base! on reversin# from lon# to short at the hi#h reversalpoints an! from short to lon# at the low reversal points. s with ar!osperfect profit a s$stem base! on these i!eal turnin# points is notpossible in practice but it provi!es a measure of the potential profit inthe market. n the last bar of the chart the function calculates thecorrelation coefficient of the i!eal e+uit$ an! the e+uit$ from the tra!in#s$stem. %he correlation coefficient an! s$stem parameter values areappen!e! to a user-specifie! te;t file for later anal$sis. %o use the I!eal+t$pt8 function to optimi"e a tra!in# s$stem the same approach isuse! as !escribe! for the +t$,orr function presente! previousl$. =irst the function

    call is a!!e! to the en! of the tra!in# s$stem co!e then the s$stem is optimi"e! usin#

    %ra!e&tations optimi"ation feature to iterate over all combinations of the s$stemsparameters. =or each combination of parameter values the I!eal+t$pt8 function will

    write one line to the te;t file specifie! b$ the input parameter =Name. s note! above

    each line in the te;t file will contain the correlation coefficient an! the s$stemparameter values. fter the optimi"ation is complete the lines in the te;t file can be

    sorte! b$ the value of the first column -- the correlation coefficient -- to fin! theparameter values that ma;imi"e the correlation coefficient.

    {

     Function IdealEqtyOpt5

     Calculate an objective function based on the correlation

     between the equity curve generated by a trading system and the

    ideal equity curve! "he ideal equity curve is the equity curvegenerated by a stop and reverse system that reverses at perfect

    reversal points!

     

    "he objective function value and system input parameter values

     are appended to a te#t file for later analysis!

     Copyright $%%5 &rea'out Futures

     www!&rea'outFutures!com

    (

     input) *aram+ ,-umeric.imple/0 { system parameter + (

  • 8/9/2019 Better Optimization

    4/12

      *aram$ ,-umeric.imple/0 { system parameter $ (

      *aram1 ,-umeric.imple/0 { system parameter 1 (

      *aram2 ,-umeric.imple/0 { system parameter 2 (

      *aram5 ,-umeric.imple/0 { system parameter 5 (  *aram3 ,-umeric.imple/0 { system parameter 3 (

      *aram4 ,-umeric.imple/0 { system parameter 4 (

      *aram ,-umeric.imple/0 { system parameter (

      *aram6 ,-umeric.imple/0 { system parameter 6 (

      *aram+% ,-umeric.imple/0 { system parameter +% (

      7ev*ts ,-umeric.imple/0 { points to reverse (

      7evFr ,-umeric.imple/0 { fraction of 8"7 to reverse (

      -7ev ,-umeric.imple/0 { length of 8"7 (

      7ev"ype ,-umeric.imple/0 { % 9 pts: + 9 8"7 (

      F-ame ,.tring.imple/0 { file name to write results to (

      *rint;og ,"rueFalse/0 { flag for writing to print log (

      write optimi?ation:

    false => write equity(

     @ar) ;owC ,%/0 { lowest close (

      AighC ,%/0 { highest close (

      "rBir ,%/0 { direction of ideal trade (

      7ev.? ,%/0 { points needed to reverse (

    Aigh&ar ,%/0 { number of bar with highest close (

      ;ow&ar ,%/0 { number of bar with lowest close (

      ibar ,%/0 { bar counter (

      @al ,%/0 { value of #0 system equity (

      D@al ,%/0 { value of y0 ideal equity (

      .umD ,%/0 { sum of D (

      .um ,%/0 { sum of (

      .umD ,%/0 { sum of D (

      .um ,%/0 { sum of (

      .umDD ,%/0 { sum of D D (

      CC-um ,%/0 { numerator of correlation coefficient (

      CCBen ,%/0 { denominator of correlation coefficient (

      CorrCoef ,%/0 { correlation coefficient (

      ObjectFn ,%/0 { objective function (

      "otalEqty ,%/0 { open plus closed trade equity (

      ii ,%/0 { loop counter (

      -&ars ,%/0 { number of bars (

      -"r.ys ,%/0 { number of system trades (

      -"rIdeal ,%/0 { number of ideal trades (

      .trOut ,/:

    8rray) Eqty.ys5%%%G,%/0 { equity at each bar0 system trades (  EqtyIdeal5%%%G,%/0 { equity at each bar0 ideal trades (

      "r.ys+%%%G,%/0 { equity at end of system trades (

      Bates.ys+%%%G,%/0 { dates of system trades (

      "rIdeal+%%%G,%/0 { equity at end of ideal trades (

      BatesIdeal+%%%G,%/: { dates of ideal trades (

     If &ar-umber 9 + then &egin

      If Close > Close+G then

      "rBir 9 +

      Else

  • 8/9/2019 Better Optimization

    5/12

      "rBir 9 =+:

      AighC 9 Close:

      Aigh&ar 9 &ar-umber:

      ;owC 9 Close:  ;ow&ar 9 &ar-umber:

      Bates.ys%G 9 Bate"oHulian,date/:

      BatesIdeal%G 9 Bate"oHulian,date/:

     End:

     If 7ev"ype 9 % then

      7ev.? 9 7ev*ts

     Else

      7ev.? 9 7evFr average,"rue7ange0 -7ev/:

     If Close > AighC then &egin

      AighC 9 Close:

      Aigh&ar 9 &ar-umber: End:

     If Close ;owC then &egin

      ;owC 9 Close:

      ;ow&ar 9 &ar-umber:

     End:

     { 7everse ideal trade from long to short (

     If "rBir 9 + and AighC = Close >9 7ev.? then &egin

      "rBir 9 =+:

      -"rIdeal 9 -"rIdeal J +:

      If *rint;og then

      *rint,Bate) 0 date&ar-umber = Aigh&arG)%)%0 "ime) 0time&ar-umber = ;ow&arG)%)%0

      Close 9 0 AighC)%)20 Birection) 0 "rBir/:

    ;owC 9 ;owest,Close0 &ar-umber = Aigh&ar J +/:

      ;ow&ar 9 &ar-umber = ;owest&ar,Close0 &ar-umber = Aigh&ar J +/: 

    { 8djust previous equity values from reversal point to current bar (

      For ii 9 ibar = ,&ar-umber = Aigh&ar = +/ to ibar = + begin

      EqtyIdealiiG 9 EqtyIdealii = +G J ,Closeibar = ii J +G =

    Closeibar = iiG/ &ig*oint@alue:

      End:

     

    { 7ecord equity value and date for ideal trade (  "rIdeal-"rIdealG 9 EqtyIdealibar = ,&ar-umber = Aigh&ar/G:

      BatesIdeal-"rIdealG 9 Bate"oHulian,date&ar-umber = Aigh&arG/:

     End:

     { 7everse ideal trade from short to long (

     If "rBir 9 =+ and Close = ;owC >9 7ev.? then &egin

      "rBir 9 +:

      -"rIdeal 9 -"rIdeal J +:

      If *rint;og then

  • 8/9/2019 Better Optimization

    6/12

      *rint,Bate) 0 date&ar-umber = ;ow&arG)%)%0 "ime) 0

    time&ar-umber = ;ow&arG)%)%0

    Close 9 0 ;owC)%)20 Birection) 0 "rBir/:

    AighC 9 Aighest,Close0 &ar-umber = ;ow&ar J +/:

      Aigh&ar 9 &ar-umber = Aighest&ar,Close0 &ar-umber = ;ow&ar J +/:

    { 8djust previous equity values from reversal point to current bar (

      For ii 9 ibar = ,&ar-umber = ;ow&ar = +/ to ibar = + begin

      EqtyIdealiiG 9 EqtyIdealii = +G J ,Closeibar = iiG =

    Closeibar = ii J +G/ &ig*oint@alue:

      End:

     { 7ecord equity value and date for ideal trade (

      "rIdeal-"rIdealG 9 EqtyIdealibar = ,&ar-umber = ;ow&ar/G:

      BatesIdeal-"rIdealG 9 Bate"oHulian,date&ar-umber = ;ow&arG/:

     End: 

    { Keep trac' of equity curve of trading system (

     "otalEqty 9 -et*rofit J Open*osition*rofit:

     If ibar 5%%% then

      Eqty.ysibarG 9 "otalEqty:

     { 7ecord equity value and date for system trade (

     If "otal"rades > -"r.ys then &egin

      -"r.ys 9 -"r.ys J +:

      "r.ys-"r.ysG 9 -et*rofit:

      Bates.ys-"r.ysG 9 Bate"oHulian,date/:

     End:

     { Keep trac' of equity curve from ideal trades (

     If ibar > % and ibar 5%%% then &egin

      If "rBir 9 + then

      EqtyIdealibarG 9 EqtyIdealibar = +G J ,Close = Close+G/

    &ig*oint@alue

      Else If "rBir 9 =+ then

      EqtyIdealibarG 9 EqtyIdealibar = +G J ,Close+G = Close/

    &ig*oint@alue

      Else

      EqtyIdealibarG 9 %:

     End:

     

    ibar 9 ibar J +: { Compute objective function and write to file (

     If ;ast&arOnchart then &egin

      -&ars 9 ibar:

     { Calculate correlation between system and ideal equity curves (

      For ii 9 % to -&ars = + &egin

      @al 9 Eqty.ysiiG:

      D@al 9 EqtyIdealiiG:

      .um 9 .um J @al:

  • 8/9/2019 Better Optimization

    7/12

      .umD 9 .umD J D@al:

      .umD 9 .umD J ,@al D@al/:

      .um 9 .um J ,@al @al/:

      .umDD 9 .umDD J ,D@al D@al/:  End:

      CC-um 9 -&ars .umD = ,.um .umD/:

      CCBen 9 .quare7oot,,-&ars .um = ,.um .um// ,-&ars .umDD

    = ,.umD .umD///:

      if CCBen > % then

      CorrCoef 9 CC-umLCCBen

      else

      CorrCoef 9 %:

    ObjectFn 9 CorrCoef:

     

    {

  • 8/9/2019 Better Optimization

    8/12

    -umto.tr,-"rIdeal0 %/ J trades J -ew;ine/:

      For ii 9 % to -"rIdeal &egin

      .trOut 9 -umto.tr,BatesIdealiiG0 %/ J 0 J

    -umto.tr,"rIdealiiG0 $/ J -ewline:  File8ppend,F-ame0 .trOut/:

      End:

      End:

     End:

     IdealEqtyOpt5 9 ObjectFn:

    Figure 2. !asy&anguage funtion Ideal!"tyOpt# for optimizing tradingsystems in TradeStation. T$e funtion omputes t$e orrelation oeffiient

    bet'een t$e trading system(s e"uity and t$e e"uity from an ideal tradingsystem.

      few features of the I!eal+t$pt8 as$*an#ua#e co!e shoul! probabl$ be e;plaine!.%he first 10 inputs to the function are for trackin# the s$stems parameter values!urin# optimi"ation. %hese inputs shoul! be set to the s$stems inputs when the

    function is calle! (see e;ample below). n$ of these first 10 inputs that are not nee!e!

    can be set to "ero. %he input 9ev%$pe !etermines whether the reversal points are!etermine! b$ points (9ev%$pe ? 0) or b$ a fraction of the avera#e true ran#e

    (9ev%$pe ? 1). If 9ev%$pe ? 1 then the avera#e true ran#e (%9) is compute! overthe last N9ev bars an! reversals are taken at a number of points #iven b$ 9ev=r times

    the %9. If the rint*o# input is %9@ the reversal points (!ate time closin# pricean! reversal !irection) are written to the rint *o#. If the 3ritept input is %9@ the

    correlation coefficient an! s$stem parameter values are written to the te;t file asalrea!$ e;plaine!. If this input is =*& the e+uit$ curves (s$stem an! i!eal) are

    written to the file instea! of the optimi"ation results alon# with the e+uit$ values ateach tra!e entr$ for the both the actual an! i!eal s$stems.

    s written the function will work with up to 8000 bars of !ata an! up to 1000 tra!es.

    *ar#er !ata sets can be han!le! b$ increasin# the arra$ si"es. %he function will work

    with both intra!a$ an! !ail$ bars althou#h the time of the tra!es is not recor!e! inthe optional output #enerate! when 3ritept ? =*&.

     s an e;ample of optimi"in# with I!eal+t$pt8 consi!er the AiniAa; s$stem. %his isa s$stem I ori#inall$ !evelope! for tra!in# the -mini & an! -mini N&B futures.

    It turns out that AiniAa; works well on other futures too such as %-

  • 8/9/2019 Better Optimization

    9/12

     I optimi"e! the s$stem on %-

  • 8/9/2019 Better Optimization

    10/12

     

    Figure 4. Optimal one-contract equity curve for the MiniMax system on daily bars of

    US -!onds "symbol #US.$ in radeStation %& for trades on the optimi'ation segment"() years ending )*+,*()&. /) 0as deducted from each trade for slippage and

    commissions.

    The corresponding performance over the optimization period is as follows:  Net Profit: $86,806Profit Factor: 16Percent !ins: "#" %&'8 trades total()ve Trade: $*1&!in+oss: 1&"-a. /ntrada rawdown: $11,*2" To determine how well the optimal res3lts are li4el to hold 3p going forward, the sstemwas r3n over the last 10 ears of T5ond data, from "+*8+1''" to "+&1+0" These 7o3t5of5sample7 data were not 3sed in the optimization, so the provide a more o9ective meas3reof the sstems performance than the res3lts over the optimization segment )s shown inFig ", the e;3it c3rve in the o3t5of5sample period is also ;3ite linear 3ntil it enters adrawdown period in recent trading 

  • 8/9/2019 Better Optimization

    11/12

    Figure ). Out-of-sample one-contract equity curve for the MiniMax system on daily

    bars of US -!onds "symbol #US.$ in radeStation %& for trades on the (1-year data

    segment ending )*2(*+11) using the optimal parameter values. /) 0as deducted from

    each trade for slippage and commissions.

    The corresponding performance over the o3t5of5sample period is as follows:  Net Profit: $#2,"6'Profit Factor: 1#Percent !ins: "&* %*"* trades total()ve Trade: $18'!in+oss: 1*&-a. /ntrada rawdown: $1",*6' 

  • 8/9/2019 Better Optimization

    12/12

    factors co3ld e 3sed to ias the optimization to the most recent time period ) similareffect co3ld e achieved in /deal=;t>pt" if desired weighting the ideal e;3it c3rve

     )s with an trading sstem optimization, its alwas a good idea to save some data for o3t5of5sample testing and preferal trac4 the sstem in real5time efore committing realmone To learn more ao3t the -ini-a. sstem 3sed in the e.ample, please visithttp:++wwwrea4o3tf3t3rescom+-ini-a.+

    %hats all for now. oo! luck with $our tra!in#.

     Aike