/*
This strategy models 4 relevant indicator levels: Low level on the indicator (i.e weakness, oversold) High level on the indicator (i.e strength, overboght) Long !"it (L#) level on the indicator ($or o%tional indicator&based long trade %ro$it&taking) 'hort !"it ('#) level on the indicator ($or o%tional indicator&based short trade %ro$it&taking) or o%timiation, there are ma+or nmeric dimensions: L'-ase-eriod L'-'mooth-eriod L'-LowLvl L'-HighLvl L#-0bove-Lvl '#-elow-Lvl 'to%Lossct $tTgtct Trail'to%ct along with varios switches and choices: Trading1ode 'ignalTy%e ilterTy%e Long'hortoth 2nvert'ignals 'ingle3ontract ayTrade 'ettings This strategy is designed to allow wide&sco%e or narrow&sco%e o%timiation to be con$igred $rom the 0 arameters ialog. This means o%timiation across any combination o$ the ma+or nmeric dimensions based strictly on ialog settings, withot re5iring code edits. '6, 27 8!7!0L, 96 ;2LL 76T 7!! T6 !2T TH! 36! T6 '36! 96 6T212<0T267. However, not every single %arameter is so con$igred. can be made once the code has been stdied and nderstood.
0ny desired changes
This code im%lements single contract/share ne"t&bar&on&o%en testing and visaliation (when Trading1ode = >). This is only one o$ many ways 0miroker can trade, bt it is the most im%ortant and natral way to model (216), and is also consistent with many other %lat$orms.
7ote: 3ertain abbreviations have been care$lly chosen to s%%ort te"tal sbstittion and avoid varios incom%atibilities (with keywords, with 26, and others).
est all&arond reslts will be obtained by nderstanding and res%ecting these conventions in any $rther edits. ?@ary? is sed to indicate where o%t&im&i&ation may be taking %lace. Likewise $or?6Ty%e?. ?arm? is sed instead o$ %ar&am&eter. To generate a $eatre list $rom the ;indows command line (i$ gre% is installed): gre% ?A*A*A*A*A*? 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode 3ode
$or $or $or $or $or $or $or $or $or $or $or $or $or $or $or $or
B-L'-CDa.a$l
set% o$ coo%erating indicator adding cstom metric(s) 00 window dis%lay o$ non&o%timied %arameters o%timier choice and set% single contract testing choosing a Trading 1ode ( entries/e"its, on&6%en/on&3lose ) technical analysis calclation(s). trading long&only, short&only, or in both directions trading signals in the o%%osite direction a choice o$ trade signal $ilters limiting trading to an intraday time window o%timiable indicator&based e"its o%timiable 'to%Loss and $tTgt determining and dis%laying e"it reason histogram visaliation o$ by/sell/short/cover arrays. %assing 7et$t to coo%erating dis%lay indicator in another chart
%ane. 3ode $or is%laying signals as an !"%loration 3ode $or on&chart %lotting o$ %rice, signal arrows, and trade dration/direction bar coloring 3ode $or creating and dis%laying in$ormative title te"t 3ode $or dis%lay o$ $ilters 2n the %arameters dialog, leading ?EEEEEEE? indicates a ?se? switch, which trns a block o$ $nctionality 6n/6$$. 2n the %arameters dialog, trailing ?&&&&&&&&&&? indicates a ?@ary? switch, which trns +st o%t&im&i&ation 6n/6$$. ;hen altering or e"tending this strategy, to kee% all intended $nctionality, visit every line containing ?BBB? and make edits as/i$ necessary in accord with the instrctions. egarding se with 26: 0s o$ this writing, it is not %ossible to have bilt& in 0 o%timier choice $eatres and se o$ 26 $rom the same $ile. However, every e$$ort has been made to minimie the re5ired di$$erences. 'earch on ?non&26? to $ind the F instrctions (BBB) regarding changes $or se with/withot 26. 0thorGs note: This code is the reslt o$ years o$ backgrond e"%erience, months o$ general investigation, and weeks o$ s%eci$ic research, design, coding, and testing. y beginning with this code, yo get to avoid -alot- o$ trial and error.
2$ what yo see here seems clear and obvios %on reading, that is the ho%ed& $or intent gI. However, getting to this %oint has been a both a +orney and a labor. This code is %rovided in retrn $or a $ee, which is relied %on $or %art o$ my livelihood. Jnowing the time/e$$ort s%ent to %rodce it, 2 can assre yo that this code is a dee%&bargain&at&the&%rice (thatGs my bsiness model). However, 911@. This code is not $reeware, or trialware. This code is a %ro$essionally %rodced commercial %rodct, and yo mst %rchase a license in order to se it or %ossess it. 2$ yo are in %osession o$ this code withot having %aid, yo mst either go to 3odeorTraders.com and %ay the asking %rice, or else delete it immediately. 3aveats and '%%ort olicy: htt%://www.code$ortraders.com/3aveats.htm htt%://www.code$ortraders.com/3stomer-'%%ort.htm rther e"tension/alteration/cstomiation o$ this code is available on a $or&hire, time&available basis to licensed sers. 3odeorTraders.com also o$$ers ecommerce services to 0miroker develo%ers (i.e. earn by selling yor high&5ality 0 code). 2n5iries: %rogsterK3odeorTraders.com Thanks to Tomas anecko $or 0miroker, and to the many in the 0 commnity who have docmented and coded $or 0 be$ore me. This code bilds on everything 2 cold $ind and absorb so $ar. Ho%e$lly, this code will hel% yo to shorten yor +orney, and lighten yor labor ... !n+oyM & rogster */ // 3o%yright NCCO by 'teve ohns, aka rogster, all rights reserved, worldwide. // !"clsive distribtion by 3odeorTraders.com. 6ther distribtion %rohibited. -'!3T267-!827(?B-L'-CDa.a$l?)P /* $nction arm@ary( aramTitle, de$alt@al, minv, ma"v, ste% ) Q retrn 6%timie( aramTitle, aram( aramTitle, de$alt@al, minv, ma"v, ste% ), minv, ma"v, ste% )P R // 7ew $nction by rogster, 4/NC/NCCO // arm3hoice@ary() allows yo a choice to ?lock down? or ?vary? a %arameter at o%timiation rntime. // To get the desired e$$ect, this $nction mst be called nconiditonally $rom the to% level. // i.e. do not call $rom inside an i$()QR block. // &&& nacce%table to 26: ?7on&nmeric I vale in 6%timie 'tatement? &&& $nction arm3hoice@ary( 1ode, armTitle, de$alt@al, minv, ma"v, ste% ) Q
retval = C P i$ (1ode) Q retval = 6%timie( armTitle, aram( armTitle, de$alt@al, minv, ma"v, ste% ), minv, ma"v, ste% ) P R else Q retval = aram( armTitle, de$alt@al, minv, ma"v, ste% ) P R retrn retval P R */ // Trans$orm ate7m (e.g., >C4CON or ON>>>4) to 'tring mmddyyyy (e.g., CO/N/NCC4 or >>/>4/>OON) // rom the 0L Library: htt%://www.amibroker.com/library/detail.%h%Sid=4> // $nction ateTo'tr(nate) Q //i$ yo want string withot se%arators yo have to s%eci$y this in yor //7mTo'tr call htt%://www.amibroker.com/$Snmtostr //string=7mTo'tr('elected@ale(nate),>,alse)P //9o can se 'trormat $or more control htt%://www.amibroker.com/$Sstr$ormat string = 'trormat(?UV.CVg?, nate)P //e"tract string %art yy = 'trLe$t(string, F)P mm = 'tr1id(string, F, N)P dd = 'tright(string, N)P //trans$orm year yy = 'trTo7m(yy) E >OCCP //36!3T !6! 07 0T! NCCC //retrn string retrn mm E ?/? E dd E ?/? E 7mTo'tr(yy, >, alse)P R //
******************** 'tart o$: ******************** // &&&&& 'trategy&s%eci$ic e$alt %arameter vales
%arameters.a$l
// 0ll de$alt vales $or %arameters are assigned to variables here. // The variables will be sed as de$alt@al argments elsewhere so as not to be hard&coded . // This entire block may be se%arated into an inclde $ile i$ desired, // which will likely be the case when im%lementing advanced e"ternal control o$ 0 // $or large scale test de$inition and re%lication. // '$$i"es: -C nmeric setting, -2C toggle or list setting @ary-'ignalTy%e-C = C P @ary-ilterTy%e-C = C P @ary-Trading1ode-C = C P 'ignalTy%e-C = > P ilterTy%e-C = C P Trading1ode-C = > P @ary-L'-ase-eriod-C = > P
@ary-L'-'mooth-eriod-C = > P L'-ase-eriod-C = N P L'-'mooth-eriod-C = F P @ary-L'-LowLvl-C = > P @ary-L'-HighLvl-C = > P L'-LowLvl-C = &. P L'-HighLvl-C = . P Long'hortoth-C = F P 2nvert'ignals-2C = C P 'ingle3ontract-2C = > P !"%loreTy%e-2C = > P !"traecimals-2C = C P !5ity'end-C = > P se2ndLvl!"its-2C = >P @ary-L#-0bove-Lvl-2C = > P @ary-'#-elow-Lvl-2C = > P L#-0bove-Lvl-C = .4 P '#-elow-Lvl-C = &.4 P se'to%s0ndTgts-2C = > P @ary'to%s-2C = CP @aryTgts-2C = CP @aryTrail'to%-2C = CP
// // //
hold constant hold constant hold constant
'to%Lossct-C = > P $tTgtct-C = P Trail'to%ct-C = NCP aytrade6nly-2C = C P 7o!ntriese$ore-C = OFC P !"it0llTime-C = >C P 7o!ntries0$ter-C = >FC P
//
7o, allow overnight trades
6Ty%e-2C = > P @aryns-C = N P @ary1a"!val-C = NCC P
//
>&F:
?310!WTribesW''6?
3stom1etrics-2C = > P 'how'ignalsHistogram-2C = > P // &&&&& end o$: e$alt %arameter vales // ******************** !nd o$: ********************
%arameters.a$l
// BBB 3omment ot this block when not doing %arameter sbstittion via batch atomation // BBB 7ote: This inclsion is only a%%ro%riate $or non&26 se. // %rint$( ?2nclded: 1odle-ataAAB-L'-CDaAA%arameters.a$lIAn? ) P // Binclde 1odle-ataAB-L'-CDaA%arameters.a$lI // override the settings above (e.g. $or batch atomation) /* The 6%t2tereload $ile is sed to overwrite the $ormla %arameter de$alts with a di$$erent set o$ initial vales, generally a set that was written ot by rogsterGs !"cel @ macro G0-6%t-arams-!"%ort()G.
26;, the %arameters sed $or a s%eci$ic o%timier iteration may now be reloaded withot manal ty%ingM Here is the %rocess (in short): >. er$orm an o%timiation and save the o%timiation reslts s%readsheet (atomatically or manally). N. 6%en the s%readsheet in !"cel, select a one&row region o$ %arameters ($or the iteration o$ interest). F. se 0lt&, and rn rogsterGs @0 macro G0-6%t-arams-!"%ort()G. 4. @eri$y the %ath to which yo wish the ot%t $ile (6%t2tereload-C.a$l) to be written, then %ress 6J. The $ile G6%t2tereload-C.a$lG will be written to yor chosen location. 2t will contain 0L to (re)de$ine each o$ yor selected %arameter vales as %er the s%readsheet. 26;, as %er that %articlar o%timiation iteration. y inclding this $ile here, we set the %arameter de$alts vales to same. . 6%en the 0 %arameters dialog and %ress the GesetG btton. %arameters are now set as desired.
9or
The above works $or both chart loading and backtest loading. There$ore, it is now %ossible to 5ickly and easily view/evalate -all- the %ertinent in$o $or -any- iteration o$ any o%timiation. 7ote: 2t is %ossible that the s%readsheet will not contain a colmn $or every o%erative %arameter. ;hen com%aring ?reload? reslts to an o%timiation interation line, beware the e$$ects o$ nre%resented %arameters. 7ote: becase we are inclding this $ile, it mst be %resent $or this 0L to rn. However, it is 6J $or the $ile to be em%ty. 'o, ntil yoGve generated a ?real? 6%t2tereload-C.a$l, +st make sre there is an em%ty one at the inclde location. 0lternatively, +st comment ot the inclde line (which disables this $nctionality). */ // BBB 7ote: This inclsion is only a%%ro%riate $or non&26 se. // %rint$( ?2nclded: 1odle-ataAA6%t2tereload-C.a$lI? ) P // Binclde-once 1odle-ataA6%t2tereload-C.a$lI // overrides de$alts set in %arameters.a$l // &&&&& 3ode $or 'et%/3on$igration // 2ntialie any variables/arrays which shold never be nde$ined 7et$t = CP sing26 = C P L'-'caleactor = > P
//
normally >Ck $or #, > $or stocks and $tres
// These seem to be necessary $or 26 (S) Trading1ode = > P yelay = 'ellelay = 'hortelay = 3overelay = > P // BBB 7ame the indicator(s) in se. !nableTe"t6t%t(alse)P ormla-7ame = ?B-L'-CDa? P L'-7ame = ?L'? P !nableTe"t6t%t(Tre)P s%N = ? // // // // //
This name will be sed in many %laces.
?P // two s%aces, sed $or indenting gro%ed %arameters
Label the list %arameters shown in the 2nter%retation window. 3omment this ot i$ aramList() is 76T %resent anywhere below, or i$ every instance o$ it has been s%%ressed by wra%%ing with !nableTe"t6t%t(alse)P !nableTe"t6t%t(Tre)P %rint$( ?List arameter 3hoices: An?) P
// This works i$ yo want to se a di$$erent %rice than 3lose (bt no o%timiation %rovided) //aserice = aramield(?aserice?, F) P //asericelot'tyle = aramList(?asericelot'tyle?, ?LineW7olot?, >)P // arameter de$initions. // 2n this $ormla, %arameters are gro%ed at the to% and ordered $or sensible dis%lay in the arameter dialog. // 0n alternative (not done here), is to %lace %arameter de$initions throghot the $ile near the code that ses them. // 7ote: This constrction $ails in 26. The brackets -mst not- be on the same line . /* i$ (@ary-'2-'mooth-eriod) Q '2-'mooth-eriod = 6%timie( '2-7ame E ?-'mooth-eriod?, '2-'mooth-eriod-aram, >, NC, >) P R else Q '2-'mooth-eriod = '2-'mooth-eriod-aram P R */ // 1aster switch $or sing or not sing 26 & intended to control sbse5ent incldes. 7ot crrently o%erational. // sing26 = aramToggle( ?sing26?, ?7oW9es?, >)P // 'how the ser that he is sing 26, bt o$$er no choice. // BBB 3omment these ot $or non&26 se. or se with 26, ncomment these lines. // sing26 = aramToggle( ?sing26 (7o 6%t !ngine 3hoice)?, ?9esW9es?, C)P //
sing26 = > P
// 26 needs to know abot yor Binclde directory, i$ yo are inclding $iles // 9o shold be able to tell it in G0mirokerA26A26 e$alts.t"tG. t i$ not, tell it here. ////26: 2ncldeir: c:A0miroker-ormla-ootA2nclde // 3once%tally, this is what we want, bt 0 does not allow $or conditional Binclde // i$ (M sing26) Q Binclde 6%t6%tion3hoices.a$lI R //
BBB ncomment this $or non&26 se. or se with 26, comment this ot.
// Binclde 6%t6%tion3hoices.a$lI below
//
//
6%t6%tion3hoices.a$l
******************** ********************
'tart o$:
contents re%rodced
/* ;hen -76T- sing 26, o$$er a choice o$ the 0miroker bilt&in o%timiers, and their settings. 2$ sing 26, this choice shold not be, and is not, o$$ered. sing26 shold be set in the $ile which is inclding this one. */ %rint$( ?2nclded: %rint$( ?An? ) P s%acing
6%t6%tion3hoices.a$l? ) P // %rint blank line to 2nter%retation window $or visal
i$ (sing26)Q R elseQ // ointless and dangeros when sing 26 6Ty%e = aramList(?&&&& 6Ty%e?, ?!"hastiveW310!WTribesW''6?, 6Ty%e-2C)P // de$alt: 310!, (7ote: ?6Ty%e? instead o$ ?6%ti&mierTy%e? $or 26 com%atibility) @aryns = aram( ?&&&& @aryns?, @aryns-C, C, >C, >)P @ary1a"!val = aram( ?&&&& @ary1a"!val?, @ary1a"!val-C, C, >CCCC, >)P i$ (6Ty%e == ?!"hastive?)Q @aryns = CP @ary1a"!val = CP R R //
******************** ********************
!nd o$:
6%t6%tion3hoices.a$l
lotawrice = aramToggle( ?lot aw rice?, ?7oW9es?, > )P // awricelot'tyle = aramList(?awricelot'tyle?, ?LineW7olot?, >)P ricelot'tyle = aram'tyle(?ricelot'tyle?, stylear, maskrice)P lot!ntry0rrows = aramToggle( ?lot!ntry0rrows?, ?7oW9es?, > )P lot!"it0rrows = aramToggle( ?lot!"it0rrows?, ?7oW9es?, > )P @ary-Trading1ode = aramToggle( ?@ary Trading 1ode &&&&&&&&&& ?, ?7oW9es?, @ary-Trading1ode-C )P // de$alt 7o Trading1ode-aram = aram( ?Trading1ode?, Trading1ode-C, >, 4, >) P i$ (@ary-Trading1ode)Q Trading1ode = 6%timie( ?Trading1ode?, Trading1ode-aram, R elseQ Trading1ode = Trading1ode-aram P R
>,
4,
>) P
@ary-'ignalTy%e = aramToggle( ?@ary 'ignal Ty%e &&&&&&&&&& ?, ?7oW9es?, @ary-'ignalTy%e-C )P // de$alt 7o 'ignalTy%e-aram = aram( ?'ignalTy%e?, 'ignalTy%e-C, >, N, >) P i$ (@ary-'ignalTy%e)Q 'ignalTy%e = 6%timie( ?'ignalTy%e?, 'ignalTy%e-aram, R
>,
N,
>) P
elseQ 'ignalTy%e R
=
'ignalTy%e-aram P
@ary-ilterTy%e = aramToggle( ?@ary ilter Ty%e &&&&&&&&&& ?, ?7oW9es?, @ary-ilterTy%e-C )P // de$alt 7o ilterTy%e-aram = aram( ?ilterTy%e?, ilterTy%e-C, >, N, >) P i$ (@ary-ilterTy%e)Q ilterTy%e = 6%timie( ?ilterTy%e?, ilterTy%e-aram, R elseQ ilterTy%e = ilterTy%e-aram P R
>,
N,
>) P
@ary-L'-ase-eriod = aramToggle( ?@ary ase eriod &&&&&&&&&& ?, ?7oW9es?, @ary-L'-ase-eriod-C )P // de$alt 9es L'-ase-eriod-aram = aram( L'-7ame E ?-ase-eriod?, L'-ase-eriod-C, N, C, >) P i$ (@ary-L'-ase-eriod)Q L'-ase-eriod = 6%timie( L'-7ame E ?-ase-eriod?, C, >) P R elseQ L'-ase-eriod = L'-ase-eriod-aram P R
L'-ase-eriod-aram,
N,
@ary-L'-'mooth-eriod = aramToggle( ?@ary 'mooth-eriod &&&&&&&&&& ?, ?7oW9es?, @ary-L'-'mooth-eriod-C )P // de$alt 9es L'-'mooth-eriod-aram = aram( L'-7ame E ?-'mooth-eriod?, L'-'mooth-eriod-C, >, NC, >) P i$ (@ary-L'-'mooth-eriod) Q L'-'mooth-eriod = 6%timie( L'-7ame E ?-'mooth-eriod?, L'-'mooth-eriod-aram, >, NC, >) P R elseQ L'-'mooth-eriod = L'-'mooth-eriod-aram P R @ary-L'-LowLvl = aramToggle( ?@ary Low Level &&&&&&&&&& ?, ?7oW9es?, @ary-L'-LowLvl-C )P // de$alt 9es L'-LowLvl-aram = aram( L'-7ame E ?-LowLvl?, L'-LowLvl-C, &>C, C, .>) P i$ (@ary-L'-LowLvl)Q L'-LowLvl = 6%timie( L'-7ame E ?-LowLvl?, R elseQ L'-LowLvl = L'-LowLvl-aram P R
L'-LowLvl-aram,
&>C, C, .>) P
@ary-L'-HighLvl = aramToggle( ?@ary High Level &&&&&&&&&& ?, ?7oW9es?, @ary-L'-HighLvl-C )P // de$alt 9es L'-HighLvl-aram = aram( L'-7ame E ?-HighLvl?, L'-HighLvl-C, C, >C, . >) P i$ (@ary-L'-HighLvl)Q L'-HighLvl = 6%timie( L'-7ame E ?-HighLvl?,
L'-HighLvl-aram,
C, >C, .>) P
R elseQ L'-HighLvl R
=
L'-HighLvl-aram P
Long'hortoth = aram( ?Long'hortoth?, Long'hortoth-C, >, F, > )P // F iterations 2nvert'ignals = aramToggle( ?2nvert'ignals?, ?7oW9es?, 2nvert'ignals-2C )P 'ingle3ontract = aramToggle( ?'ingle3ontract?, ?7oW9es?, 'ingle3ontract-2C )P !nableTe"t6t%t(alse)P !"%loreTy%e = aramList(?!"%loreTy%e?, ?3rrentWTodayWHistorical?, !"%loreTy%e-2C)P !nableTe"t6t%t(Tre)P !"traecimals = aramToggle( ?!"traecimals?, ?7oW9es?, !"traecimals-2C )P L'-'caleactor = aram(?L'-'caleactor?, >, >, >CCCC, >CC )P // coordinate with indicator receiver %ane !5ity'end = aram(?!5ity'end?, !5ity'end-C, C, >C, >)P // coordinate with 7et$t receiver %ane, C means no send // 6%tional 2ndicator&level e"its // 7ote that both se 07 @ary settings are %assed to arm3hoice@ary() $or the desired e$$ect. se2ndLvl!"its = aramToggle( ?EEEEEEE se2ndLvl!"its?, ?7oW9es?, se2ndLvl!"its-2C )P // de$alt 9es @ary-L#-0bove-Lvl = aramToggle( s%N E ?@ary L# 0bove Level &&&&&&&&&& ?, ?7oW9es?, @ary-L#-0bove-Lvl-2C )P // de$alt 9es L#-0bove-Lvl-aram = aram( s%N E ?L#-0bove-Lvl?, L#-0bove-Lvl-C, C, >C, . >) P // Long !"it indicator level i$ ((se2ndLvl!"its M= C) 07 (@ary-L#-0bove-Lvl M= C))Q L#-0bove-Lvl = 6%timie( s%N E ?L#-0bove-Lvl?, L#-0bove-Lvl-aram, P // Long !"it indicator level R elseQ L#-0bove-Lvl = L#-0bove-Lvl-aram P R
C, >C,
.>)
@ary-'#-elow-Lvl = aramToggle( s%N E ?@ary '# elow Level &&&&&&&&&& ?, ?7oW9es?, @ary-'#-elow-Lvl-2C )P // de$alt 9es '#-elow-Lvl-aram = aram( s%N E ?'#-elow-Lvl?, '#-elow-Lvl-C, &>C, C, . >) P // 'hort !"it indicator level i$ ((se2ndLvl!"its M= C) 07 (@ary-'#-elow-Lvl M= C))Q '#-elow-Lvl = 6%timie( s%N E ?'#-elow-Lvl?, '#-elow-Lvl-aram, P// 'hort !"it indicator level R elseQ '#-elow-Lvl = '#-elow-Lvl-aram P R
&>C, C, .>)
// 6%tional sto%&loss and %ro$it target e"its. se'to%s0ndTgts = aramToggle( ?EEEEEEE se'to%s0ndTgts?, ?7oW9es?, se'to%s0ndTgts-2C )P // de$alt 9es, which dis%lays/ses 'to%s. sto%s disabled
2$ 7o, all
@ary'to%s = aramToggle( s%N E ?@ary 'to%s &&&&&&&&&& ?, ?7oW9es?, @ary'to%s-2C )P // de$alt 9es 'to%Lossct-aram = aram( s%N E ?'to%Lossct?, 'to%Lossct-C, C, NC, .
C> )P
// o%timie or set sto% loss
i$ ((se'to%s0ndTgts M= C) 07 (@ary'to%s M= C))Q 'to%Lossct = 6%timie( s%N E ?'to%Lossct?, 'to%Lossct-aram, C, NC, .C> )P // o%timied sto% loss R elseQ 'to%Lossct = 'to%Lossct-aram P R @aryTgts = aramToggle( s%N E ?@ary Targets &&&&&&&&&& ?, ?7oW9es?, @aryTgts-2C )P // de$alt 9es $tTgtct-aram = aram( s%N E ?$tTgtct?, $tTgtct-C, C, C, . C> )P // o%timied %ro$it target i$ ((se'to%s0ndTgts M= C) 07 (@aryTgts M= C))Q $tTgtct = 6%timie( s%N E ?$tTgtct?, $tTgtct-aram, C, C, .C> )P // o%timied %ro$it target R elseQ $tTgtct = $tTgtct-aram P R @aryTrail'to% = aramToggle( s%N E ?@ary Trail'to% &&&&&&&&&& ?, ?7oW9es?, @aryTrail'to%-2C )P // de$alt 9es Trail'to%ct-aram = aram( s%N E ?Trail'to%ct?, Trail'to%ct-C, C, NC, .C> )P // o%timie or set trail sto% i$ ((se'to%s0ndTgts M= C) 07 (@aryTrail'to% M= C))Q Trail'to%ct = 6%timie( s%N E ?Trail'to%ct?, Trail'to%ct-aram, C, NC, .C> )P // o%timied trail sto% R elseQ Trail'to%ct = Trail'to%ct-aram P R 3stom1etrics = aramToggle( ?3stom1etrics?, ?7oW9es?, 3stom1etrics-2C )P 'how'ignalsHistogram = aramToggle( ?'how'ignalsHistogram?, ?7oW9es?, 'how'ignalsHistogram-2C )P aytrade6nly = aramToggle(?EEEEEEE aytrade6nly?, ?7oW9es?, aytrade6nly-2C)P 7o!ntriese$ore = >CC * aram(s%N E ?7o!ntriese$ore?, 7o!ntriese$ore-C, OFC, >O, )P !"it0llTime = >CC * aram(s%N E ?!"it0llTime?, !"it0llTime-C, OFC, >O, )P 7o!ntries0$ter = >CC * aram(s%N E ?7o!ntries0$ter?, 7o!ntries0$ter-C, OFC, >O, )P %rint$( ?An?) P
//
ot%t blank line a$ter list %arameter choices
// ***** 3ode $or set% o$ coo%erating indicator // 'end %arameter settings to the coo%erating indicator // !"am%le: rL'.%>.a$l // BBB 3reate any new settings as necessary 'tatic@ar'et(L'-7ame E ?-Length?, L'-ase-eriod)P 'tatic@ar'et(L'-7ame E ?-'mooth?, L'-'mooth-eriod)P 'tatic@ar'et(L'-7ame E ?-LowLvl?, L'-LowLvl)P
'tatic@ar'et(L'-7ame 'tatic@ar'et(L'-7ame 'tatic@ar'et(L'-7ame 'tatic@ar'et(L'-7ame
E E E E
?-HighLvl?, L'-HighLvl)P ?-L#-0bove-Lvl?, L#-0bove-Lvl)P ?-'#-elow-Lvl?, '#-elow-Lvl)P ?-L'-'caleactor?, L'-'caleactor)P
// ***** 3ode $or adding cstom metric(s) // This is a skeleton (with demos). // 0dd yor own desired cstom metrics in this block. i$(3stom1etrics)Q 'et6%tion(?se3stomacktestroc?, Tre )P // !nable cstom backtest %rocedre 'et3stomacktestroc(??, >)P // ?? means se crrent $ormla i$( 'tats(?action?) == actionort$olio )Q bo = 8etacktester6b+ect()P
// retrieve the inter$ace to %ort$olio
backtester // bo.acktest( alse )P (generating a trade list) bo.acktest( Tre )P generate a trade list)
// rn de$alt backtest %rocedre // rn de$alt backtest %rocedre (donGt
// 3stom trade metrics (on closed trade list) $or( trade = bo.8etirstTrade()P tradeP trade = bo.8et7e"tTrade() ) // iterate over closed trade list // $or( o%en%os = bo.8etirst6%enos()P o%en%osP o%en%os = bo.8et7e"t6%enos() ) // iterate over o%en %osition list Q // trade variable now holds Trade ob+ect // 3alclate trade&level cstom metric(s) 7et3hg = trade.!"itrice & trade.!ntryrice P // 0dd trade&level cstom metric(s) to the backtest re%ort trade.0dd3stom1etric( ?7et3hg?, 7et3hg )P R bo.ListTrades()P trade metric(s)
// 76; generate the trades list (a$ter adding cstom
stats = bo.8eter$ormance'tats(C)P
// get stats $or all trades
// !"%ectancy calclations (two versions, same reslt). // 'ee: or$olio acktester 2nter$ace e$erence 8ide //e"%> = stats.8et@ale(?;inners0vgro$it?)*stats.8et@ale(?;innersercent?)/>CC E stats.8et@ale(?Losers0vgLoss?)*stats.8et@ale(?Losersercent?)/>CCP e"%N = stats.8et@ale(?7etro$it?)/ (stats.8et@ale(?;innersXty?) E stats.8et@ale(?LosersXty?)) P // 0dd %ort$olio&level cstom metric(s) to the o%timiation re%ort // bo.0dd3stom1etric( ?!"%> (Y)?, e"%> )P bo.0dd3stom1etric( ?!"%N (Y)?, e"%N )P
// ***** 3ode $or 00 window dis%lay o$ non&varied %arameters // 0dd non&varied %arameters as cstom metrics. // This letGs yo see in the ot%t which $i"ed %arm vales were sed. // The 00 window atomatically dis%lays colmns $or %arms being varied, so the intent here is to // only create cstom metric colmns $or the %arms which 0! 76T being varied. // BBB 2n general, make sre all non&varied %arms are %resent here. // BBB 0bbreviations are sed in some %laces $or colmn titles. 3hange i$ desired. // -T03!( ?se'to%s0ndTgts: ? E 7mTo'tr( se'to%s0ndTgts ) ) P //
8ro% 3ontrol
sb&item
metric
de$inition // is 67 i$ ((se2ndLvl!"its M= C) Q bo.0dd3stom1etric( ?L#-0bove-Lvl?, i$ ((se2ndLvl!"its M= C) Q bo.0dd3stom1etric( ?'#-elow-Lvl?,
not being varied 07 (@ary-L#-0bove-Lvl == C)) L#-0bove-Lvl )P R 07 (@ary-'#-elow-Lvl == C)) '#-elow-Lvl )P R
i$ ((se'to%s0ndTgts M= C) 07 (@ary'to%s == C)) Q bo.0dd3stom1etric( ?'to%Lossct?, 'to%Lossct )P R i$ ((se'to%s0ndTgts M= C) 07 (@aryTgts == C)) Q bo.0dd3stom1etric( ?$tTgtct?, $tTgtct )P R // i$ (( M= C) 07 ( M= C)) Q bo.0dd3stom1etric( ??, //
'ingle 3ontrol
)P
R metric
de$inition i$ (@ary-Trading1ode == C) Q bo.0dd3stom1etric( ?1ode?, Trading1ode, Trading1ode, Trading1ode, C)P R i$ (@ary-'ignalTy%e == C) Q bo.0dd3stom1etric( ?'ignal?, 'ignalTy%e, 'ignalTy%e, 'ignalTy%e, C)P R i$ (@ary-ilterTy%e == C) Q bo.0dd3stom1etric( ?ilter?, ilterTy%e, ilterTy%e, ilterTy%e, C)P R i$ (@ary-L'-ase-eriod == C) Q bo.0dd3stom1etric( L'-7ame E ?-ase-eriod?, L'-ase-eriod )P R i$ (@ary-L'-'mooth-eriod == C) Q bo.0dd3stom1etric( L'-7ame E ?-'mooth-eriod?, L'-'mooth-eriod )P R i$ (@ary-L'-LowLvl == C) Q bo.0dd3stom1etric( L'-7ame E ?-LowLvl?, L'-LowLvl )P R i$ (@ary-L'-HighLvl == C) Q bo.0dd3stom1etric( L'-7ame E ?-HighLvl?, L'-HighLvl )P R i$ ((sing26 M= >) 07 (@aryns M= C)) Q bo.0dd3stom1etric( ?@aryns?, @aryns )P R i$ ((sing26 M= >) 07 (@ary1a"!val M= C)) Q bo.0dd3stom1etric( ?@ary1a"!val?, @ary1a"!val )P R // 7on&o%timied Toggles & always re%ort when relevant bo.0dd3stom1etric( ?L'?, Long'hortoth, Long'hortoth, Long'hortoth, eclaces = C)P bo.0dd3stom1etric( ?2nvert?, 2nvert'ignals, 2nvert'ignals, 2nvert'ignals, eclaces = C)P i$ (sing26 M= >) Q bo.0dd3stom1etric( ?6Ty%e?, 6Ty%e, 6Ty%e, 6Ty%e, eclaces = C)P R i$ (L'-'caleactor M= >) Q bo.0dd3stom1etric( ?L'-'caleactor?, L'-'caleactor, L'-'caleactor, L'-'caleactor, eclaces = C)P R //
aytrade 'ettings crrently not re%orted
// i$ ( == C) Q bo.0dd3stom1etric( ??, R // end o$: R // end o$:
)P
R
i$( 'tats(?action?) == actionort$olio )
i$(3stom1etrics)
// 3once%tally, this is what we want, bt 0 does not allow $or conditional Binclde // i$ (M sing26) Q Binclde 6%t3hoice0ctions.a$lI R // BBB ncomment this $or non&26 se. or se with 26, comment ot this line. // BBB 3omment this ot i$ yo %re$er yor e"tra colmns (%arms) at the end rather than in $ront // BBB 0lso, comment this ot i$ not rnning in 0 .NF or later M 'et6%tion(?!"tra3olmnsLocation?, > )P // %t %arameter colmns %&$ront a$ter o%timiation // ***** 3ode $or single contract testing i$ ('ingle3ontract) osition'ie = 1argine%osit = >P
// Trade a single contract.
// ***** 3ode $or choosing a Trading 1ode ( entries/e"its, on&6%en/on&3lose ) /* Trading1ode = > (the de$alt, as s%%lied) Trade on the 6%en o$ the ne"t bar a$ter a 'ignal. This re5ires setting the trade %rice arrays to 6%en, 07 setting the trade delays to >. The backtester sets the entry array (y or 'ell) at the signal bar, bt $or visal %r%oses, we wold consider the -ne"t bar- to be the bar o$ entry. 2n this case, we cannot re%ort the !ntryrice at the signal bar withot looking into the $tre. ;e can, however, re%ort it > bar later (as done in the Title, below) */ // 'etting delays to > means trade ?ne"t bar at? $ill %riceI. // 'etting delays to C means trade ?this bar at? $ill %riceI // BBB 3hoose desired entry/e"it style here. switch( Trading1ode )Q case >: // ?7e"t bar at 6%en? trading yelay = >P yrice = 6%en P // L! at 6%en %rice o$ ne"t bar 'hortelay = >P 'hortrice = 6%en P // '! at 6%en %rice o$ ne"t bar 'ellelay = >P 'ellrice = 6%en P // L# at 6%en %rice o$ ne"t bar 3overelay = >P 3overrice = 6%en P // '# at 6%en %rice o$ ne"t bar breakP case N: // ?This bar at 3lose? trading yelay = CP yrice = 3lose P // L! at 3lose %rice o$ signal bar 'hortelay = CP 'hortrice = 3lose P // '! at 3lose %rice o$ signal bar 'ellelay = CP 'ellrice = 3lose P // L# at 3lose %rice o$ signal bar 3overelay = CP 3overrice = 3lose P // '# at 3lose %rice o$ signal bar
breakP case F: // 1i"ed mode trading, enter ?7e"t bar at 6%en?, e"it ?This bar at 3lose? yelay = >P yrice = 6%en P // L! at 6%en %rice o$ ne"t bar 'hortelay = >P 'hortrice = 6%en P // '! at 6%en %rice o$ ne"t bar 'ellelay = CP 'ellrice = 3lose P // L# at 3lose %rice o$ signal bar 3overelay = CP 3overrice = 3lose P // '# at 3lose %rice o$ signal bar breakP /* case 4: // bad reslts $or reversal tradesM & this is a bogs caseM // 1i"ed mode trading, , enter ?This bar at 3lose?, e"it ?7e"t bar at 6%en? yelay = CP yrice = 3lose P // L! at 3lose %rice o$ signal bar 'hortelay = CP 'hortrice = 3lose P // '! at 3lose %rice o$ signal bar 'ellelay = >P 'ellrice = 6%en P // L# at 6%en %rice o$ ne"t bar 3overelay = >P 3overrice = 6%en P // '# at 6%en %rice o$ ne"t bar breakP */ R 'etTradeelays( yelay, 'ellelay, 'hortelay, 3overelay ) P 'hort = 3over = C P
// ero these here, they will be calclated below
// ***** 3ode $or technical analysis calclation(s). // BBB 0dd all desired T0 calcs here. theL' = Lineg'lo%e(3lose, L'-ase-eriod) P 'moothedL' = 10(theL', L'-'mooth-eriod) P // e&'cale the T0 %lots (i.e. $or #) when there is a 'caleactor other than > i$ (L'-'caleactor M= >)Q theL' = theL' * L'-'caleactor P 'moothedL' = 'moothedL' * L'-'caleactor P R // 3hoice o$ variations in the root signal calclation // BBB 0dd new signal de$inition choices as desired L'-2sLow = L'-2sHigh = C P // y weakness, sell strength & trade each area as it is entered i$ ('ignalTy%e == >)Q L'-2sLow = ('moothedL' = L'-LowLvl) 07 (e$('moothedL', &>) I // weakness, toch/downcross o$ low threshold L'-2sHigh = ('moothedL' I= L'-HighLvl) 07 (e$('moothedL', &>) P // strength, toch/%cross o$ high threshold R // y weakness, sell strength & trade each area as it is e"ited else i$ ('ignalTy%e == N)Q L'-2sLow = ('moothedL' I= L'-LowLvl) 07 (e$('moothedL', &>) // rebond $rom weakness, toch/%cross o$ low threshold L'-2sHigh = ('moothedL' = L'-HighLvl) 07 (e$('moothedL', &>) P // breakdown $rom strength, toch/downcross o$ high threshold R // 6t%t calclation vales to 2nter%retation window.
L'-LowLvl) P L'-HighLvl)
L'-LowLvl) P I L'-HighLvl)
t s%aces a$ter each
vale. %rint$( ?L': ? E ;rite@al(theL', >.N) E ? ? ) P %rint$( ?'moothedL': ? E ;rite@al('moothedL', >.N) %rint$( ?An?) P
E ?
? ) P
// ***** 3ode $or trading long&only, short&only, or in both directions // 'et% to trade long only, short only, or in both directions. // This block %redicated on working with only N (rather than 4) basic signals. // BBB 1ake sre to always assign y, 'ell, 'hort, and 3over to %revent nde$ined errors later. i$ (Long'hortoth == >)Q 'ell = 'hort = 3over = C P i$ (2nvert'ignals)Q y = L'-2sHighP // 'ell = L'-2sLowP R else Q y = L'-2sLowP // 'ell = L'-2sHighP R R else i$ (Long'hortoth == N)Q y = 'ell = 'hort = C P i$ (2nvert'ignals)Q 'hort = L'-2sLowP // 3over = L'-2sHighP R else Q 'hort = L'-2sHighP // 3over = L'-2sLowP R R else i$ (Long'hortoth == F)Q // 'to%&and&reverse set% & a%%ro%riate when there are only N nderlying signals as here. // 7ote that $or sto%&and&reverse case, 0 re5ires the e"it signal along with the new entry signal. /* y = L'-2sLowP 'ell = L'-2sHighP 'hort = 'ellP 3over = yP */ // eware & incorrectM /* y = L'-2sLowP 'hort = L'-2sHigh P 'ell = 3over = C P */ // 'to%&and&reverse set% & a%%ro%riate when there are only N nderlying signals as here. // 7ote that $or sto%&and&reverse case, 0 re5ires the e"it signal along with the
new entry signal. i$ (2nvert'ignals)Q y = L'-2sHighP 'ell = L'-2sLowP 'hort = 'ellP 3over = yP R else Q y = L'-2sLowP 'ell = L'-2sHighP 'hort = 'ellP 3over = yP R R // ===bg // %rint$( ?a y: ? E y E ? 'ell: ? E 'ell E ? 'hort: ? E 'hort E ? 3over: ? E 3over E ?An? ) P // ***** 3ode $or trading signals in the o%%osite direction // 6%tional inversion o$ trading signals i$ (2nvert'ignals)Q %rint$( ?2nverting 'ignals? E ?An? ) P R // ***** 3ode $or a choice o$ trade signal $ilters // BBB 0dd add new/di$$erent $ilter ty%es as desired // 3hoice o$ $ilters to a%%ly. ilterTy%e C means ?no $ilter?. i$ (ilterTy%e == >)Q Long6J = 'hort6J = C P Long6J = 3lose I 10(3lose, NCC) P 'hort6J = 3lose 10(3lose, NCC) P R i$ (ilterTy%e == N)Q Long6J = 'hort6J = C P Long6J = 3lose I 10(3lose, C) P 'hort6J = 3lose 10(3lose, C) P R // etc. $or di$$erent $ilters, i$ desired i$ (ilterTy%e M= C)Q y = y 07 Long6J P 'hort = 'hort 07 'hort6J P R // ===bg // %rint$( ?b y: ? E y E ? 'ell: ? E 'ell E ? 'hort: ? E 'hort E ? 3over: ? E 3over E ?An? ) P
// ***** 3ode $or limiting trading to an intraday time window (i.e. daytrading) // 2ntraday time restrictions when daytrading i$ (aytrade6nly==>) Q y Z= Time7m() I= 7o!ntriese$ore 07 Time7m() = 7o!ntries0$ter P
'ell Z= Time7m() I= 7o!ntriese$ore 07 Time7m() = 7o!ntries0$ter P 'hort Z= Time7m() I= 7o!ntriese$ore 07 Time7m() = 7o!ntries0$ter P 3over Z= Time7m() I= 7o!ntriese$ore 07 Time7m() = 7o!ntries0$ter P // !6 e"its & $orce a sell or cover, which is ignored i$ there is no %osition on 'ell W= Time7m() I= !"it0llTime P 3over W= Time7m() I= !"it0llTime P R // ***** 3ode $or o%timiable 'to%Loss and $tTgt i$ (se'to%s0ndTgts)Q /* C ) /*
0%%ly'to%( ty%e, mode, amont, e"itatsto%, volatile = alse, e!ntryelay = */ &&&&&&&&&&&&&&&&&&&&&&& dynamic mode: */
0%%ly'to%(sto%Ty%eLoss, 22$( 'to%Lossct I C, sto%1odeercent, sto%1odeisable), 'to%Lossct, C, alse, C )P 0%%ly'to%(sto%Ty%ero$it, 22$( $tTgtct I C, sto%1odeercent, sto%1odeisable), $tTgtct, C, alse, C )P 0%%ly'to%(sto%Ty%eTrailing, 22$( Trail'to%ct I C, sto%1odeercent, sto%1odeisable), Trail'to%ct, C, alse, C )P R // ***** 3ode $or o%timiable indicator&based e"its // BBB 'et yor e"it reason te"t here. This is sed $or on&screen dis%lay. 'elleason = ?? P 3overeason = ?? P i$ (se2ndLvl!"its)Q 'ell
W= ( 'moothedL' I L#-0bove-Lvl ) P
'elleason = ?L#-0bove-Lvl & ? P 3over
W= ( 'moothedL' '#-elow-Lvl ) P
3overeason = ?'#-elow-Lvl & ? P R
// ***** 3ode $or determining and dis%laying e"it reasons // 3alclate e5ity array, sto%s not written to by/sell/short/cover arrays, se 00 window date range. // >-!5ity = !5ity(C, &>)P // 3alclate e5ity array, !@0L0T! 'T6' and write to by/sell/short/cover arrays, se 00 window date range. >-!5ity = !5ity(>, &>)P // generate e"it reasons // 2nter%retation dis%lay o$ date range begin and end. // one near the !5ity() calclation, as this is the range over which e5ity is calclated. ateangeegin = 'tats(?range$romdate?) P
ateange!nd = 'tats(?rangetodate?) P %rint$( ?ateangeegin: ? E ateTo'tr( ateangeegin ) E ?An? E ?ateange!nd: ? E ateTo'tr( ateange!nd ) E ?An? ) P // 3a%tre sto% reason in$ormation to strings. -7( 'elleason = 'elleason E ;rite2$( 'ell == >, ?eglar !"itAn?, ;rite2$( 'ell == N, ?1a" LossAn?, ;rite2$( 'ell == F, ?ro$it TgtAn?, ;rite2$( 'ell == 4, ?Trailing 'to%An?, ;rite2$( 'ell == , ?7&bar 'to%An?, ;rite2$( 'ell == D, ?in sto%An?, ?? ) ) ) ) ) ) )P -7( 3overeason = 3overeason E ;rite2$( 3over == >, ?eglar !"itAn?, ;rite2$( 3over == N, ?1a" LossAn?, ;rite2$( 3over == F, ?ro$it TgtAn?, ;rite2$( 3over == 4, ?Trailing 'to%An?, ;rite2$( 3over == , ?7&bar 'to%An?, ;rite2$( 3over == D, ?in sto%An?, ?? ) ) ) ) ) ) )P // -T03!( ?'ell: ? E 7mTo'tr( 'ell ) ) P // -T03!( ?3over: ? E 7mTo'tr( 3over ) ) P // ;rite to the 2nter%retation ;indow to e"%lain the crrently selected Trading1ode switch( Trading1ode )Q case >: %rint$( ?1ode >: 7e"t bar at 6%en trading (delay entries 07 e"its)? E ?An? ) P breakP case N: %rint$( ?1ode N: This bar at 3lose trading (no delay o$ entries 6 e"its)? E ?An? ) P breakP case F: %rint$( ?1ode F: 1i"ed mode trading, enter 7e"t bar at 6%en, e"it This bar at 3lose (delay entries, 76T e"its)? E ?An? ) P breakP /* case 4: %rint$( ?1ode 4: 1i"ed mode trading, enter This bar at 3lose, e"it 7e"t bar at 6%en (delay e"its, not entries)? E ?An? ) P breakP */ R
// ***** 3ode $or histogram visaliation o$ by/sell/short/cover arrays. // 'im%le histogram visaliation o$ by/sell/short/cover arrays. // i$$erent heights indicate di$$erent e"it reasons, as %er above.
// is%lay range is con$ined to near bottom o$ %ane. // BBB Jee% comments in sync with actal colors i$ yo change them. i$ ('how'ignalsHistogram)Q Histase@al = .P Hist1in@al = CP Hist1a"@al = >P lot( Histase@al E >/y,?y?,color8reen,styleHistogramWstyle6wn'cale, Hist1in@al, Hist1a"@al)P // y & 8reen lot( Histase@al E >/'ell,?'ell?,colored,styleHistogramWstyle6wn'cale, Hist1in@al, Hist1a"@al)P // 'ell & ed lot( Histase@al E >/'hort,?'hort?,color6range,styleHistogramWstyle6wn'cale, Hist1in@al, Hist1a"@al)P // 'hort & 6range lot( Histase@al E >/3over,?3over?,color05a,styleHistogramWstyle6wn'cale, Hist1in@al, Hist1a"@al)P // 3over & 05a R // ***** 3ode $or %assing 7et$t to coo%erating dis%lay indicator in another chart %ane. i$( 'tats(?0ction!"?) == action2ndicator )Q 2! = 8et6%tion(?2nitial!5ity?) P // etrns single&secrity !5ity line based on y/'ell/'hort/3over rles, // y/'ell/'hort/3overrice arrays, all a%%ly sto%s, 07 all other backtester settings. // >-!5ity = !5ity(C, &>)P // +st calclate the e5ity array, se 00 window date range // 0ssign calclated e5ity to a static array so another %ane can %lot it i$ (!5ity'end I C) 'tatic@ar'et(?!5ity-? E !5ity'end, >-!5ity )P 7et$t = >-!5ity & 2!P R // end o$:
// local calclation o$ 7et$t $or se in Title
i$( 'tats(?0ction!"?) == action2ndicator ) B>
// ***** 3ode $or is%laying signals as an !"%loration i$( 'tats(?0ction!"?) == action!"%lore )Q i$ (!"%loreTy%e == ?3rrent?)Q 2sLastar = ateTime() == Last@ale(ateTime()) P // $ilter on this to show crrent bar signals only in !"%loration !"%ate3on = 2sLastar P R else i$ (!"%loreTy%e == ?Today?)Q 2sLastate = ate7m() == Last@ale(ate7m()) P // $ilter on this to show last dayGs signals only in !"%loration !"%ate3on = 2sLastate P R else i$ (!"%loreTy%e == ?Historical?)Q !"%ate3on = > P // allow historical signals in !"%loration R i$ (Long'hortoth == >)Q ilter = !"%ate3on 07 (y 6 'ell) P 0dd3olmn( y, ?y 7e"t ar?, >.C, colore$alt, 22$(y, color8reen, colore$alt) )P 0dd3olmn( 'ell, ?'ell 7e"t ar?, >.C, colore$alt, 22$('ell, colored, colore$alt) )P
R else i$ (Long'hortoth == N)Q ilter = !"%ate3on 07 ('hort 6 3over) P 0dd3olmn( 'hort, ?'hort 7e"t ar?, >.C, colore$alt, 22$('hort, colored, colore$alt) )P 0dd3olmn( 3over, ?3over 7e"t ar?, >.C, colore$alt, 22$(3over, color8reen, colore$alt) )P R else i$ (Long'hortoth == F)Q ilter = !"%ate3on 07 (y 6 'hort) P 0dd3olmn( y, ?y 7e"t ar?, >.C, colore$alt, 22$(y, color8reen, colore$alt) )P 0dd3olmn( 'hort, ?'hort 7e"t ar?, >.C, colore$alt, 22$('hort, colored, colore$alt) )P R // BBB
0dd e"%loration colmns $or all indicators
0dd3olmn( theL', L'-7ame, >.N, colore$alt, 22$(y, color8reen, 22$('hort, colored, colore$alt)) )P 0dd3olmn( 'moothedL', ?'moothed ? E L'-7ame, >.N, colore$alt, 22$(y, color8reen, 22$('hort, colored, colore$alt)) )P R // end o$:
i$( 'tats(?0ction!"?) == action!"%lore )
// ***** 3ode $or on&chart %lotting o$ %rice, signal arrows, and trade dration/direction bar coloring // i$( 'tats(?0ction!"?) == action2ndicator )Q i$( 'tats(?0ction?) == > )Q // 27230T6 // !nable/disable %lotting o$ %/down arrows on the entry and e"it signal bars. // This show location o$ signal generation, regardless o$ actal entry. i$(lot!ntry0rrows)Q lot'ha%es( sha%e%0rrow * (y I C), color8reen,C, L, &NC)P // arrows at long entry bars lot'ha%es( sha%eown0rrow * ('hort I C), colored,C,H, &NC) P // arrows at short entry bars R i$(lot!"it0rrows)Q // onGt %lot e"it arrows $or reversal case & let the entry arrows indicate that instead lot'ha%es( ( y == C ) * sha%e%0rrow * (3over I C) , color9ellow,C, L, &NC)P // arrows at long e"it bars lot'ha%es( ( 'hort == C ) * sha%eown0rrow * ('ell I C), color9ellow,C,H, &NC) P // arrows at short e"it bars R i$ ( (lotawrice) ) Q /* 3olor the %rice bars to indicate: 8reen & %rice %er$ormance a$ter a y 'ignal (i.e. while long) ed & %er$ormance a$ter a sell signal (i.e. while short) 9ellow & signal bars */ // BBB i$ yo want to color the bars in a di$$erent way, this is where it is
done switch( Trading1ode )Q case >: // ?7e"t bar at 6%en? trading (delay entries and e"its) 0mLong = li%( e$(y, &yelay), e$('ell, 22$('ellelay == C, &>, &'ellelay) ) 6 e$('hort, &'hortelay) ) P 0m'hort = li%( e$('hort, &'hortelay), e$(3over, 22$(3overelay == C, &>, &3overelay)) 6 e$(y, &yelay) ) P %rint$( ?7e"t bar at 6%en trading (delay entries 07 e"its)? E ?An? ) P breakP
case N: // ?This bar at 3lose? trading (no delay o$ entries or e"its) 0mLong = li%( e$(y, &>), e$('ell, &>) 6 e$('hort, &>) ) P 0m'hort = li%( e$('hort, &>), e$(3over, &>) 6 e$(y, &>) ) P breakP
case F: // 1i"ed mode trading, enter ?7e"t bar at 6%en?, e"it ?This bar at 3lose? (delay entries, not e"its) 0mLong = li%( e$(y, &yelay), e$('ell, &>) 6 e$('hort, &>) ) P 0m'hort = li%( e$('hort, &'hortelay), e$(3over, &>) 6 e$(y, &>) ) P breakP /* case 4: // 1i"ed mode trading, , enter ?This bar at 3lose?, e"it ?7e"t bar at 6%en? (delay e"its, not entries) 0mLong = li%( e$(y, &>), e$('ell, 22$('ellelay == C, &>, &'ellelay) ) 6 e$('hort, &>) ) P 0m'hort = li%( e$('hort, &>), e$(3over, 22$(3overelay == C, &>, &3overelay)) 6 e$(y, &>) ) P breakP */ R ar3olors = 22$( 0m'hort, colored, 22$( 0mLong, color8reen, color;hite ) ) P i$ (lotawrice) lot( 3lose, ? ?, ar3olors, ricelot'tyle ) P R R 'et3hart6%tions(C,chart'how0rrowsWchart'howates)P // ***** 3ode $or creating and dis%laying in$ormative title te"t // BBB 3on$igre yor chart title as desired // Here is a $airly generic title, all in one line. // -7(Title = 'trormat(?QQ701!RR & QQ27T!@0LRR QQ0T!RR 6%en Ug, Hi Ug, Lo Ug, 3lose Ug (U.>$UU) QQ@0L!'RR?, 6, H, L, 3, 'elected@ale( 63( 3, > ) ) ))P // re%are a Titlerice'tr, in several ste%s
i$ (!"traecimals)Q // &decimal %recision. 8ood $or #. ;old make a clttered dis%lay $or stocks and $tres. // -7() kee%s this silent $or later se. -7(Titlerice'tr = 'trormat( ?6 U.$, H U.$, L U.$, 3 U.$ An7et$t U.C$?, 6, H, L, 3, 7et$t )) P R else Q // N&decimal %recision. 8ood $or stocks and $tres. or #, more decimals are ty%ically needed // -7() kee%s this silent $or later se. -7(Titlerice'tr = 'trormat( ?6 U.N$, H U.N$, L U.N$, 3 U.N$ An7et$t U.C$?, 6, H, L, 3, 7et$t )) P R // 3on$igre a mlti&line chart title, sing the Titlerice'tr, and sometimes more. // nknown & 2n 2nter%retation: why 7et$t is C, why y'ignal disa%%ears ... // This is one giant line, down to the semicolon. Title = ?An? E 7ame() E ? ? E ate() E ?An? E Titlerice'tr E ?An? // 7ote that $or $or sto%&and&reverse case, we donGt want to dis%lay the e"it %art i$ the old&e"it/new&entry %air. // This is accom%lished by s%%ressing e"it dis%lay when entry is also %resent. E ;rite2$(y, ?y 'ignal ?, ??) E ;rite2$('ell 07 M'hort, ?'ell 'ignal & ? E 'elleason, ??) E ;rite2$('hort, ?'hort 'ignal ?, ??) E ;rite2$(3over 07 My, ?3over 'ignal & ? E 3overeason, ??) // These re%ort the trade %rices on the bar a$ter the signal (where they are available). // This is a%%ro%riate when trading ?o%en ne"t bar? E ;rite2$(e$(y, &yelay), 'trormat(?y at Ug?, yrice), ??) E ;rite2$(e$('hort,&'hortelay), 'trormat(?'hort at Ug?, 'hortrice), ??) E ;rite2$(e$('ell,&'ellelay) 07 Me$('hort,&'hortelay), 'trormat(? 'ell at Ug?, 'ellrice), ??) E ;rite2$(e$(3over,&3overelay) 07 Me$(y,&yelay), 'trormat(? 3over at Ug?, 3overrice), ??) E ?An? P
// 'end raw y/'ell/'hort/3over to coo%erating indicator $or veri$ication/debgging %r%oses. 'tatic@ar'et(ormla-7ame 'tatic@ar'et(ormla-7ame 'tatic@ar'et(ormla-7ame 'tatic@ar'et(ormla-7ame
// &&&&& end o$:
E E E E
?-y?, y)P ?-'ell?, 'ell)P ?-'hort?, 'hort)P ?-3over?, 3over)P
B-L'-CDa.a$l
-'!3T267-!7()P
// ***** 3ode $or dis%lay o$ $ilters // BBB 3on$igre (or comment ot) dis%lay o$ $ilters as desired.
// The 10 %lots were added so as to visalie the $ilters. 'how10s = aramToggle( ?'how10s?, ?7oW9es?, > )P the 1oving 0verages
// de$alt 9es, which dis%lays
i$ ('how10s)Q -'!3T267-!827(?10>?)P = aramield(?rice $ield?, F)P eriods = aram(?eriods?, NCC, N, NCC, >, C )P lot( 10( , eriods ), -!0LT-701!(), aram3olor( ?3olor?, color3ycle ), aram'tyle(?'tyle?) )P -'!3T267-!7()P -'!3T267-!827(?10N?)P = aramield(?rice $ield?, F)P eriods = aram(?eriods?, C, N, C, >, C )P lot( 10( , eriods ), -!0LT-701!(), aram3olor( ?3olor?, color3ycle ), aram'tyle(?'tyle?) )P -'!3T267-!7()P R