Zorro is a free trading automation platform that allows users to write scripts to automate trading decisions. The document provides an overview of variables that can be used in Zorro scripts, control structures like if/else statements and for/while loops, and functions for printing output, exiting positions, and working with files. It also includes examples of short code snippets that demonstrate how to use some of these variables, structures, and functions to implement common trading strategies and position management techniques.
Zorro is a free trading automation platform that allows users to write scripts to automate trading decisions. The document provides an overview of variables that can be used in Zorro scripts, control structures like if/else statements and for/while loops, and functions for printing output, exiting positions, and working with files. It also includes examples of short code snippets that demonstrate how to use some of these variables, structures, and functions to implement common trading strategies and position management techniques.
Zorro is a free trading automation platform that allows users to write scripts to automate trading decisions. The document provides an overview of variables that can be used in Zorro scripts, control structures like if/else statements and for/while loops, and functions for printing output, exiting positions, and working with files. It also includes examples of short code snippets that demonstrate how to use some of these variables, structures, and functions to implement common trading strategies and position management techniques.
What is Zorro? print (int to, string format,.) Code
Zorro is a free trading automation plattform. Forum TO WINDOW print in [Test] and [Trade] mode to the message From Manual Variables window and the log file (default for printf). Trade Management Type Size (bytes) Range Step Digits Used for double, var 8 -1.810308to 1.810308 2.210-308 14 prices TO LOG print in [Test] mode only to the log file, in [Trade] float 4 -3.41038to 3.41038 1.210-38 6 storing prices fixed 4 -1048577.999 to 1048576.999 0.001 9 not for trading and in STEPWISE mode also to the message Listing 1: Chan1ge stops and profit targets of all open int, long 4 -2147483648 to 2147483647 1 10 counting window. short char 2 1 0 to 65536 0 to 256 1 1 4 2 not for trading text characters long trades with the current algo and asset bool char*, string 4 characters+1 true, false ”...” - - - - decisions text TO FILE 1 e x i t L o n g ( 0 , NewStop ) ; var*, vars elements*8 -1.810308to 1.810308 2.210-308 14 arrays, series mat rows*cols*8+12 -1.810308to 1.810308 2.210-308 14 vectors, matrices TO ANY 2 e x i t L o n g (0 , − NewTakeProfit ) ; Skript Control TO DIAG Listing 2: Limit the number of open positions // max. TO CSV if .. else 3 open long positions per asset/algo 1 if ( ( ( x+3)<9) o r ( y==0) ) // s e t z t o 10 i f msg (string format, ...): int 1 i f ( m y l o n g c o n d i t i o n == t r u e ) { x+3 i s below 9 , o r i f y i s e q u a l t o 0 2 e x i t S h o r t ( ) ; // no h e d g i n g − c l o s e all 0 when [No] was clicked, 2 z = 10; short positions 3 else 1 when [Yes] was clicked. 3 i f ( NumOpenLong < 3 ) e n t e r l o n g ( ) ; 4 z = 5 ; // s e t z t o 5 i n a l l o t h e r c a s e s 4 } File functions while file copy (string dest, string src) Listing 3: Exit all open trades Friday afternoon GMT 1 x = 0; file delete (string name) 1 i f ( dow ( ) == FRIDAY && hour ( ) >= 1 8 ) { 2 w h i l e ( x < 1 0 0 ) // r e p e a t w h i l e x i s l o w e r 2 exitLong ( ”∗” ) ; file length than 100 3 e x i t S h o r t ( ”∗” ) ; 3 x += 1 ; file date 4 } file select Opens a file dialog box at a given directory that for loop lets the user select a file to open. Returns the selected Listing 4: Lock 80% profit of all winning trades file name including path. 1 int i ; 1 for ( open trades ) 2 f o r ( i =0; i <5; i ++) // r e p e a t 5 t i m e s file content 2 i f ( TradeIsOpen && ! T r a d e I s P o o l && T r a d e P r o f i t 3 x += i ; > 0) { file read Reads the content of a file into a string or array. 3 T r a d e T r a i l L o c k = 0 . 8 0 ; // 80% p r o f i t ( minus Returns the number of read bytes. trade costs ) switch file write Stores the content of a string, series, or other data 4 i f ( TradeIsShort ) 1 int choice ; array in a file. 5 T r a d e T r a i l L i m i t = max( T r a d e T r a i l L i m i t , 2 ... TradePriceClose ) ; file append Opens a file and appends text or other data to 3 switch ( choice ) 6 else it; can be used to export data to Excel or other 4 { 7 T r a d e T r a i l L i m i t = min ( T r a d e T r a i l L i m i t , programs. If the file does not exist, it is created. 5 case 0: TradePriceClose ) ; 6 p r i n t f ( ” Zero ! ” ) ; 8 } 7 break ; Testing 8 case 1: Testing 9 p r i n t f ( ”One ! ” ) ; Listing 5: Calculate the value of all open trades with 10 break ; Debugging the current asset 11 case 2: if(date() ¿= 20150401) set(STEPWISE); watch (”!...”, ...) 1 v a r valOpen ( ) 12 p r i n t f ( ”Two ! ” ) ; 2 { 13 break ; Biasis 3 s t r i n g C u r r e n t A s s e t = A s s e t ; // A s s e t i s 14 default : Curve fitting bias, Market fitting bias, Peeking bias, Data changed i n t h e f o r l o o p 15 p r i n t f ( ”None o f them ! ” ) ; mining bias, Trend bias, Granularity bias, Sample size bias 4 var val = 0 ; 16 } 5 for ( open trades ) Asset Parameters 6 i f ( s t r s t r ( Asset , C u r r e n t A s s e t ) && Input/Output Spread, eg Spread = 3*PIP Slippage, default = 10 TradeIsOpen ) printf (string format, ...) Commission, taken from AssetsFix.csv RollShort RollLong 7 v a l += T r a d e P r o f i t ; 8 return val ; Listing 9: Use a series to check if something happened 2 // a l l unknown e l e m e n t s with t h e c u r r e n t 9 } within the last n bars value 3 v a r s s e r i e s S h i f t ( v a r s Data , i n t s h i f t ) 1 // buy i f S i g n a l 1 c r o s s e d o v e r S i g n a l 2 w i t h i n 4 { Listing 6: Monitoring and modifying a certain trade the l a s t 7 bars 5 i f ( s h i f t >= 0 ) // s h i f t s e r i e s i n t o t h e 2 ... past 1 3 v a r s c r o s s e s = s e r i e s ( 0 ) ; // g e n e r a t e a 6 r e t u r n Data+s h i f t ; 2 TRADE∗ MyTrade = e n t e r l o n g ( ) ; s e r i e s and s e t i t t o 0 3 ... 7 e l s e { // s h i f t s e r i e s i n t o t h e f u t u r e 4 i f ( crossOver ( Signal1 , Signal2 ) 8 int i ; 4 ThisTrade = MyTrade ; // c o n n e c t t r a d e 5 c r o s s e s [ 0 ] = 1 ; // s t o r e t h e c r o s s o v e r i n t h e v a r i a b l e s t o MyTrade 9 f o r ( i = 1 ; i <= s h i f t ; i ++) series 10 Data [ i ] = Data [ 0 ] ; 5 v a r MyResult = T r a d e P r o f i t ; // e v a l u a t e t r a d e 6 i f (Sum( c r o s s e s , 7 ) > 0 ) // any c r o s s o v e r variables 11 r e t u r n Data ; within l a s t 7 bars ? 12 } 6 ... 7 enterLong ( ) ; 7 e x i t T r a d e ( MyTrade , 0 , TradeLots / 2 ) ; // e x i t 13 } 8 ... h a l f the trade 8 } Listing 13: Use a function from an external DLL Listing 10: Use a loop to check if something happened 1 // Use t h e f u n c t i o n ” f o o ” from t h e DLL ” bar . Indicators Signals within the last n bars dll” 2 // Copy bar . d l l i n t o t h e Z o r r o f o l d e r 1 // buy i f S i g n a l 1 c r o s s e d o v e r S i g n a l 2 w i t h i n Listing 7: Generate an indicator with a different asset the l a s t 7 bars 3 int s t d c a l l f o o ( d o u b l e v1 , d o u b l e v2 ) ; // time frame and shift foo prototype 2 . . . 4 API ( f o o , bar ) // u s e f o o from bar . d l l 1 // e x t e n d e d ATR f u n c t i o n with i n d i v i d u a l a s s e t 3 int i ; 5 and t i m e f r a m e ( i n mi n ut es ) 4 f o r ( i = 0 ; i < 7 ; i ++) 6 f u n c t i o n run ( ) v a r extATR ( s t r i n g symbol , i n t p e r i o d , i n t 5 i f ( c r o s s O v e r ( S i g n a l 1+i , S i g n a l 2+i ) ) { // 2 7 { length , i n t s h i f t ) c r o s s o v e r , i b a r s ago ? 8 ... 3 { 6 enterLong ( ) ; 9 int r e s u l t = foo (1 ,2) ; 4 ASSET∗ p r e v i o u s = g−>a s s e t ; // s t o r e p r e v i o u s 7 b r e a k ; // a b o r t t h e l o o p 10 ... asset 8 } 11 } 5 i f ( symbol ) a s s e t ( symbol ) ; // s e t new a s s e t 9 ... 6 i f ( p e r i o d ) TimeFrame = p e r i o d / B a r P e r i o d ; 7 // c r e a t e p r i c e s e r i e s with t h e new a s s e t / Listing 14: Equity curve trading (skipping trades de- timeframe Listing 11: Align a time frame to a certain event pendent on strategy success) 8 vars H = s e r i e s ( priceHigh () ) , 1 // Let time frame s t a r t when Event == t r u e 1 // don ’ t t r a d e when t h e e q u i t y c u r v e g o e s 9 L = s e r i e s ( pr ice Low ( ) ) , 2 // f . i . f r a m e A l i g n ( hour ( ) == 0 ) ; a l i g n s t o down 10 O = s e r i e s ( priceOpen ( ) ) , midnight 2 // and i s below i t s own l o w p a s s f i l t e r e d 11 C = series ( priceClose () ) ; 3 f u n c t i o n f r a m e A l i g n (BOOL Event ) value 12 TimeFrame = 1 ; // s e t t i m e f r a m e back 4 { 3 f u n c t i o n checkEquity ( ) 13 g−>a s s e t = p r e v i o u s ; // s e t a s s e t back 5 TimeFrame = 1 ; 4 { 14 r e t u r n ATR(O+s h i f t ,H+s h i f t , L+s h i f t , C+s h i f t , 6 v a r s Num = s e r i e s ( 0 ) ; // u s e a s e r i e s f o r 5 // g e n e r a t e e q u i t y c u r v e i n c l u d i n g phantom length ) ; s t o r i n g Num between c a l l s trades 15 } 7 Num [ 0 ] = Num[ 1 ] + 1 ; // count Num up once 6 v a r s EquityCurve = s e r i e s ( EquityLong+ p e r bar EquityShort ) ; 8 i f ( ! Event ) 7 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve Listing 8: Calculate the weekend price change for gap 9 TimeFrame = 0 ; // c o n t i n u e c u r r e n t ,10) ) ; trading time frame 8 i f ( EquityLP [ 0 ] < LowPass ( EquityLP , 1 0 0 ) && 1 // u s e 1−hour bar s , w a i t u n t i l Sunday Sunday 10 else { f a l l i n g ( EquityLP ) ) 5pm ET, 11 TimeFrame = −Num [ 0 ] ; // s t a r t a new time 9 L o t s = −1; // drawdown −> phantom t r a d e s 2 // then g e t t h e p r i c e change from F r i d a y 5pm frame 10 else ET 12 Num [ 0 ] = 0 ; // r e s e t t h e c o u n t e r 11 L o t s = 1 ; // p r o f i t a b l e −> normal t r a d e s 3 i f ( dow ( ) == SUNDAY && l h o u r (ET) == 5 ) { 13 } } Auxiliary 4 i n t FridayBar = t i m e O f f s e t (ET,SUNDAY−FRIDAY 14 ,5 ,0) ; 5 v a r PriceChange = p r i c e C l o s e ( 0 ) − Listing 15: Debugging a script p r i c e C l o s e ( FridayBar ) ; Listing 12: Shift a series into the future 1 // D i s p l a y a message box with t h e v a r i a b l e s 6 } 1 // t h e f u t u r e i s unknown , t h e r e f o r e f i l l t o be o b s e r v e d 2 // C l i c k [ Yes ] f o r a s i n g l e s t e p , [ No ] f o r 8 StartDate = 2008; 11 // mysetup . i n i i s a p l a i n t e x t f i l e t h a t c l o s i n g t h e box 9 EndDate = 2 0 1 2 ; contains 3 s t a t i c i n t debug = 1 ; 10 LookBack = 0 ; 12 // t h e p a r a m e t e r v a l u e s i n a f o r m a t l i k e t h i s 4 i f ( debug && Bar > LookBack ) 11 : 5 debug = msg ( 12 string line = strf ( 13 Parameter1 = 123 6 ” High = %.5 f , Low = %.5 f ” , 13 ”%02 i /%02 i /%02 i %02 i :%02 i , %.5 f , %.5 f , %.5 f 14 Parameter2 = 456 7 p r i c e H i g h ( ) , pr ice Low ( ) ) ; , %.5 f \n” , 14 day ( ) , month ( ) , y e a r ( ) %100 , hour ( ) , minute ( ) , 15 p r i c e O p e n ( ) , p r i c e H i g h ( ) , pri ceL ow ( ) , Listing 22: Check every minute in Trade mode if an Listing 16: Find out if you have a standard mini or priceClose () ) ; .ini file was modified micro lot account 16 i f ( i s (INITRUN) ) 1 v a r Parameter1 = 0 , Parameter2 = 0 ; 17 f i l e d e l e t e ( ” Data \\ e x p o r t . c s v ” ) ; 1 // C l i c k [ Trade ] with t h e s c r i p t below 2 18 else 2 f u n c t i o n run ( ) 3 f u n c t i o n t o c k ( ) // run once p e r minute 19 f i l e a p p e n d ( ” Data \\ e x p o r t . c s v ” , l i n e ) ; 3 { 4 { 20 } 4 a s s e t ( ”EUR/USD” ) ; 5 s t a t i c i n t LastDate = 0 ; 5 i f ( Bar > 0 ) { 6 i f ( LastDate && ! Trade ) r e t u r n ; // a l r e a d y 6 i f ( LotAmount > 9 9 9 9 9 ) Listing 19: Print the description of a trade (like ”[AU- updated 7 p r i n t f ( ” \ n I have a s t a n d a r d l o t a c c o u n t ! ” 7 i n t NewDate = f i l e d a t e ( ” S t r a t e g y \\ mysetup . ); D/USD:CY:S1234 ini ”) ; 8 e l s e i f ( LotAmount > 9 9 9 9 ) 1 void printTradeID ( ) 8 i f ( LastDate < NewDate ) { 9 p r i n t f ( ” \ n I have a mini l o t a c c o u n t ! ” ) ; 2 { 9 LastDate = NewDate ; // f i l e was m o d i f i e d : 10 else 3 s t r i n g l s = ”L” , bo = ” [ ” , bc = ” ] ” ; update new p a r a m e t e r s 11 p r i n t f ( ” \ n I have a micro l o t a c c o u n t ! ” ) ; 4 i f ( T r a d e I s S h o r t ) l s = ”S” ; 10 s t r i n g s e t u p = f i l e c o n t e n t ( ” S t r a t e g y \\ 12 quit () ; 5 i f ( TradeIsPhantom ) { bo = ” { ” ; bc = ” } ” ; } mysetup . i n i ” ) ; 13 } 6 p r i n t f ( ”#\n%s%s :% s :% s %04 i%s ” , 11 Parameter1 = s t r v a r ( s e t u p , ” Parameter1 ” ) ; 14 } 7 bo , TradeAsset , TradeAlgo , l s , TradeID %10000 , bc 12 Parameter2 = s t r v a r ( s e t u p , ” Parameter2 ” ) ; ); 13 } 8 } 14 } Listing 17: Download historic price data 1 // C l i c k [ Test ] f o r downloading / u p d a t i n g t h e Listing 20: Plot equity curves of single assets in a Listing 23: Trade multiple strategies and assets in a l a t e s t ”NZD/USD” p r i c e data 2 // This e x t e n d s t h e l e n g t h o f t h e h i s t o r i c a l multi-asset strategy single script p r i c e data f i l e , t h e r e f o r e 1 c h a r name [ 4 0 ] ; // s t r i n g o f maximal 39 1 f u n c t i o n run ( ) 3 // WFO s t r a t e g i e s u s i n g t h a t a s s e t s h o u l d be characters 2 { trained again afterwards 2 s t r c p y ( name , A s s e t ) ; 3 BarPeriod = 2 4 0 ; 4 f u n c t i o n run ( ) 3 s t r c a t ( name , ” : ” ) ; 4 StartDate = 2010; 5 { 4 s t r c a t ( name , Algo ) ; 5 s e t (TICKS) ; // s e t r e l e v a n t v a r i a b l e s and 6 NumYears = 6 ; // download up t o 6 y e a r s 5 v a r e q u i t y = E q u i t y S h o r t+EquityLong ; flags before calling asset () data 6 i f ( e q u i t y != 0 ) p l o t ( name , e q u i t y ,NEW|AVG,BLUE 6 7 l o a d H i s t o r y ( ”NZD/USD” , 1 ) ; // update t h e ); 7 // c a l l d i f f e r e n t s t r a t e g y f u n c t i o n s with price history different assets 8 } 8 a s s e t ( ”EUR/USD” ) ; Listing 21: Set up strategy parameters from a .ini file 9 tradeLowpass ( ) ; at the start 10 tradeFisher () ; Listing 18: Export historic price data to a .csv file 1 f u n c t i o n run ( ) 11
1 // C l i c k [ Test ] f o r e x p o r t i n g p r i c e data t o a 2 { 12 a s s e t ( ”GBP/USD” ) ;
. c s v f i l e i n t h e Data f o l d e r 3 s t a t i c v a r Parameter1 = 0 , Parameter2 = 0 ; 13 tradeAverage ( ) ; 2 // The r e c o r d s a r e s t o r e d i n t h e f o r m a t : time 4 i f ( i s (INITRUN) ) { // r e a d t h e p a r a m e t e r s 14
, open , high , low , c l o s e o n l y i n t h e f i r s t run 15 a s s e t ( ”SPX500” ) ;
3 // f . i . ”31/12/12 0 0 : 0 0 , 1 . 3 2 2 0 5 , 1 . 3 2 3 4 1 , 5 s t r i n g s e t u p = f i l e c o n t e n t ( ” S t r a t e g y \\ 16 tradeBollinger () ; 1.32157 , 1.32278” mysetup . i n i ” ) ; 17 } 4 // Dependent on t h e l o c a l e , d a t e and numbers 6 Parameter1 = s t r v a r ( s e t u p , ” Parameter1 ” ) ; might r e q u i r e a d i f f e r e n t f o r m a t 7 Parameter2 = s t r v a r ( s e t u p , ” Parameter2 ” ) ; 5 f u n c t i o n run ( ) 8 } Listing 24: Update price history of many assets 6 { 9 } 1 f u n c t i o n run ( ) 7 BarPeriod = 1440; 10 2 { 3 NumYears = 2 ; 2 { 32 } 4 w h i l e ( l o o p ( ”AUD/USD” , ”EUR/USD” , ”GBP/USD” , ” 3 s e t (PLOTNOW) ; 33 else GER30” , ”NAS100” , 4 PlotBars = 300; 34 { 5 ”SPX500” , ”UK100” , ”US30” , ”USD/CAD” , ”USD/CHF” 5 PlotScale = 8; 35 Margin = 0 . 5 ∗ OptimalFShort ∗ C a p i t a l ∗ s q r t , ”USD/JPY” , 6 i f ( CDLDoji ( ) ) (max ( 1 , B a l a n c e / C a p i t a l ) ) ; 6 ”XAG/USD” , ”XAU/USD” ) ) 7 p l o t ( ” D o j i ” , 1 . 0 0 2 ∗ p r i c e H i g h ( ) ,TRIANGLE4, 36 enterShort () ; 7 { BLUE) ; 37 } 8 a s s e t H i s t o r y ( Loop1 , 1 ) ; 8 i f ( CDLHikkake ( ) > 0 ) 38 } 9 } 9 p l o t ( ” Hikkake+” , 0 . 9 9 8 ∗ pr ice Low ( ) ,TRIANGLE, 39 10 } GREEN) ; 40 L o t s = LotsBackup ; 10 i f ( CDLHikkake ( ) < 0 ) 41 } Listing 25: Portfolio strategy with 3 assets and 3 trade11 p l o t ( ” Hikkake−” , 1 . 0 0 2 ∗ p r i c e H i g h ( ) ,TRIANGLE4 42 ,RED) ; 43 f u n c t i o n run ( ) functions 12 } 44 { 1 45 s e t (PARAMETERS+FACTORS) ; 2 f u n c t i o n tradeTrendLong ( ) Systems 46 3 { YenTrader System 47 BarPeriod = 1440; 4 a l g o ( ”TRL” ) ; 48 LookBack = 2 0 0 ; 5 ... 49 StartDate = 20010101; 6 } Listing 27: YenTrader System 50 EndDate = 2 0 1 6 0 6 0 1 ; 7 1 51 MonteCarlo = 1 0 0 0 ; 8 f un c ti on tradeTrendShort ( ) 2 bool UseEquityFilter = f a l s e ; 52 Confidence = 100; 9 { 3 53 Capital = 10000; 10 a l g o ( ”TRS” ) ; 4 f u n c t i o n TradeYenTrader ( ) 54 NumWFOCycles = 1 0 ; 11 ... 5 { 55 12 } 6 vars Price = s e r i e s ( price () ) ; 56 a s s e t ( ”USD/JPY” ) ; 13 7 v a r s SMASlow = s e r i e s (SMA( P r i c e , o p t i m i z e 57 TradeYenTrader ( ) ; 14 function tradeBollinger () (100 , 5 , 200 , 5) ) ) ; 58 } 15 { 8 v a r s SMAFast = s e r i e s (SMA( P r i c e , o p t i m i z e ( 5 0 , a l g o ( ”BOL” ) ; Simple profit system - EUR/USD 16 5 , 200 , 5) ) ) ; 17 ... 9 18 } 10 Stop = o p t i m i z e ( 1 5 , 5 , 5 0 ) ∗ PIP ; Listing 28: Simple profit system 19 11 T r a i l = o p t i m i z e ( 1 , 1 , 2 0 ) ∗ PIP ; 1 20 f u n c t i o n tradeFunc ( ) ; // empty f u n c t i o n 12 2 f u n c t i o n run ( ) pointer 13 // Equity c u r v e f i l t e r 3 { 21 14 v a r LotsBackup = L o t s ; 4 22 f u n c t i o n run ( ) 15 5 B a r P e r i o d = 6 0 ; // 1 hour b a r s 23 { 16 i f ( UseEquityFilter ) 6 LookBack = 2 0 ; 24 while ( loop ( 17 { 7 StartDate = 2010; 25 ”EUR/USD” , 18 v a r s EquityCurve = s e r i e s ( EquityLong+ 8 EndDate = 2 0 1 6 0 8 0 4 ; 26 ”USD/CHF” , EquityShort ) ; 9 27 ”GBP/USD” ) ) // l o o p t h r o u g h 3 a s s e t s 19 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve 10 //NumWFOCycles = 6 ; 28 w h i l e ( tradeFunc = l o o p ( ,75) ) ; 11 // s e t (LOGFILE) ; 29 tradeTrendLong , 20 i f ( EquityLP [ 0 ] < LowPass ( EquityLP , 1 0 0 ) && 12 // C a p i t a l = 5 0 0 0 ; 30 tradeTrendShort , f a l l i n g ( EquityLP ) ) 13 // Margin = 0 . 0 0 1 ∗ OptimalF ∗ C a p i t a l ∗ s q r t 31 t r a d e B o l l i n g e r ) ) // and 3 d i f f e r e n t t r a d e 21 L o t s = −1; (1 + P r o f i t C l o s e d / Capital ) ; algorithms 22 else 14 Hedge = 2 ; 32 { 23 Lots = 1 ; 15 L o t s =1; 33 a s s e t ( Loop1 ) ; // s e l e c t a s s e t 24 } 16 NumOptCycles = 2 ; 34 tradeFunc ( ) ; // c a l l t h e t r a d e f u n c t i o n 25 17 NumSampleCycles = 2 ; 35 } 26 i f ( NumOpenShort + NumOpenLong == 0 ) 18 36 } 27 { 19 28 i f ( SMAFast [ 0 ] > SMASlow [ 0 ] ) 20 s e t (PARAMETERS+FACTORS) ; Plotting 29 { 21 v a r StopL = o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( 30 Margin = 0 . 5 ∗ OptimalFLong ∗ C a p i t a l ∗ s q r t ( optimize (3 ,1 ,10) ) ; Listing 26: Plot Triangle above Candle Patterns max ( 1 , B a l a n c e / C a p i t a l ) ) ; 22 v a r T a k e P r o f i t L=o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( 1 f u n c t i o n run ( ) 31 enterLong ( ) ; optimize (3 ,1 ,10) ) ; 23 28 v a r s aWI1 = s e r i e s ( WillR ( o p t i m i z e 28 24 var StopS = o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( (19.9 ,18 ,49) ) ) ; 29 string Names [NN ] ; optimize (3 ,1 ,10) ) ; 29 30 string Symbols [NN ] ; // S t o r e t h e ISIN 25 var T a k e P r o f i t S=o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( 30 Code optimize (3 ,1 ,10) ) ; 31 i f ( r i s i n g ( MMI Smooth ) && aWI1 [ 0 ] > 31 vars Returns [NN ] ; 26 nWI TOP∗−1 && r i s i n g ( aAlma ) &&r i s i n g ( 32 var Means [NN ] ; 27 i f ( hour ( ) == 1 3 ) { aEma1 ) ) { 33 var C o v a r i a n c e s [NN ] [ NN ] ; 28 Stop=StopL ; 32 enterLong ( ) ; 34 var Weights [NN ] ; 29 T a k e P r o f i t=T a k e P r o f i t L ; 33 } e l s e i f ( f a l l i n g ( MMI Smooth ) && aWI1 [ 0 ] < 35 static int OldLots [NN ] ; 30 enterLong ( ) ; nWI DOWN∗−1 && f a l l i n g ( aAlma )&&f a l l i n g (36 31 } aEma1 ) ) { 37 var TotalCapital = s l i d e r 32 34 enterShort () ; (1 ,1000 ,1000 ,50000 , ” Capital ” , ” Total 33 i f ( hour ( ) == 21 ) { 35 } c a p i t a l to d i s t r i b u t e ” ) ; 34 Stop=StopL ; 36 38 v a r VFactor = s l i d e r ( 2 , 10 , 0 , 1 0 0 , ” Risk ” , ” 35 T a k e P r o f i t=T a k e P r o f i t L ; 37 Variance f a c t o r ” ) ; 36 enterShort () ; 38 } 39 37 } 40 int N = 0; 38 Mean Variance Optimization 41 w h i l e ( Names [N] = l o o p ( A s s e t s ) ) 39 } 42 { Listing 30: Mean Variance Optimization System 43
Profitable System - EUR/USD 1 // 44 i f ( i s (INITRUN) && s t r s t r ( Names [N] , ”#” )==
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / NULL) { 45 a s s e t H i s t o r y ( Names [N] , FROM YAHOO) ; Listing 29: Profitable System 2 // Mean V a r i a n c e O p t i m i z a t i o n 46 Symbols [N] = Symbol ; // S t o r e t h e i s i n 1 f u n c t i o n run ( ) //////////////////////// code f o r q u i c k r e f e r e n z e 2 { 3 // 47 } 3 NumCores = −2; // use m u l t i p l e c o r e s ( / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /48 ////////// Zorro S only ) 49 i f ( s t r s t r ( Names [N] , ”#” )== NULL && i s ( 4 B a r P e r i o d = 6 0 ; // 1 hour b a r s 4 RUNNING) ) { 5 LookBack = 1 2 4 0 ; // needed f o r F i s h e r ( ) 5 //#d e f i n e DAYS 252 // 1 y e a r 50 a s s e t ( Names [N ] ) ; 6 StartDate = 2010; 6 #d e f i n e DAYS 6∗22 // 6 Months 51 Returns [N] = s e r i e s ( ( p r i c e C l o s e ( 0 )− 7 EndDate = 2 0 1 5 ; // fixed simulation 7 //#d e f i n e DAYS 4∗22 // 4 Months priceClose (1) )/ priceClose (1) ) ; period 8 //#d e f i n e DAYS 2∗22 // 2 Months 52 } 8 9 53 i f ( s t r s t r ( Names [N] , ”#” ) != NULL && i s ( 9 s e t (PARAMETERS) ; // g e n e r a t e and u s e 10 #d e f i n e WEIGHTCAP . 2 5 // Cap 0 . 1 5 − 0 . 5 RUNNING) ) Returns [N] = s e r i e s ( 0 ) ; o p t i m i z e d p a r a m e t e r s and f a c t o r s Range 54 10 11 #d e f i n e NN 50 // max number o f 55 i f (N++ >= NN) b r e a k ; 11 Stop = 5 ∗ ATR( 9 0 ) ; assets 56 } 12 T a k e P r o f i t =4∗ ATR( 3 0 ) ; 12 #d e f i n e LEVERAGE 4 // 1 : 4 l e v e r a g e 57 13 13 58 i f ( tdm ( ) == 1 && ! i s (LOOKBACK) ) { 14 v a r nWI TOP=o p t i m i z e ( 3 0 , 5 , 3 5 ) ; 14 // 59 int i , j ; 15 v a r nWI DOWN=o p t i m i z e ( 7 0 , 6 0 , 1 0 0 ) ; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /60 / /r /( i =0; i <N; i ++) { / / / / / / /f o 16 61 Means [ i ] = Moment ( Returns [ i ] , LookBack 17 v a r s aOpen = series ( price () ) ; 15 ,1) ; 18 v a r s aAlma = s e r i e s (ALMA( aOpen , 16 f u n c t i o n run ( ) 62 f o r ( j =0; j <N; j ++) 19 optimize (8.61 ,8 ,30) , 17 { 63 C o v a r i a n c e s [N∗ i+j ] = C o v a r i a n c e ( Returns 20 optimize (6.15 ,6 ,19) , 18 BarPeriod = 1440; [ i ] , Returns [ j ] , LookBack ) ; 21 0.8) ) ; 19 LookBack = DAYS; 64 } 22 20 NumYears = 7 ; 65 v a r B e s t V a r i a n c e = markowitz ( C o v a r i a n c e s , 23 v a r s MMI Raw = s e r i e s (MMI( aOpen , o p t i m i z e 21 s e t (PRELOAD) ; // a l l o w e x t r e m e l y l o n g Means , N, WEIGHTCAP) ; (300 ,100 ,500) ) ) ; lookback period 66 v a r MinVariance = markowitzReturn ( 0 , 0 ) ; 24 v a r s MMI Smooth = s e r i e s ( LowPass (MMI Raw 22 s e t (LOGFILE) ; 67 markowitzReturn ( Weights , MinVariance+ ,500) ) ; 23 Verbose = 0 ; VFactor / 1 0 0 . ∗ ( B e s t V a r i a n c e −MinVariance 25 24 s e t ( watch ) ; )); 26 v a r s aEma1 = s e r i e s (EMA( aOpen , o p t i m i z e 25 68 (12.6 ,12 ,100) ) ) ; 26 // A s s e t L i s t = ”ETF2016−OK. c s v ” ; 69 // change t h e p o r t f o l i o c o m p o s i t i o n 27 27 A s s e t L i s t = ” AssetsZ8 . csv ” ; a c c o r d i n g t o new w e i g h t s 70 f o r ( i =0; i <N; i ++) One Night Stand System 8 Stop = o p t i m i z e ( 2 , 1 , 1 0 ) ∗ ATR( 1 0 0 ) ; 71 i f ( s t r s t r ( Names [ i ] , ”#” )== NULL) { 9 72 a s s e t ( Names [ i ] ) ; Listing 32: One Night Stand System 10 i f ( s t r s t r ( Algo , ” : L” ) and v a l l e y ( Trend ) ) 73 MarginCost = p r i c e C l o s e ( ) /LEVERAGE; 1 11 enterLong ( ) ; 74 i n t NewLots = T o t a l C a p i t a l ∗ Weights [ i ] / 2 f u n c t i o n tradeOneNightStand ( ) { 12 e l s e i f ( s t r s t r ( Algo , ” : S” ) and peak ( Trend ) ) MarginCost ; 3 13 enterShort () ; 75 i f ( NewLots > OldLots [ i ] ) 4 vars Price = s e r i e s ( price () ) ; 14 } 76 e n t e r L o n g ( NewLots−OldLots [ i ] ) ; 5 v a r s SMA10 = s e r i e s (SMA( P r i c e , 1 0 ) ) ; 15 77 e l s e i f ( NewLots < OldLots [ i ] ) e x i t L o n g 6 v a r s SMA40 = s e r i e s (SMA( P r i c e , 4 0 ) ) ; 16 f u n c t i o n tradeCounterTrend ( ) ( 0 , 0 , OldLots [ i ]−NewLots ) ; 7 17 { 78 // p r i n t f ( ” \ n%s − %s : OldLots 8 // Stop = 90 ∗ PIP ; 18 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ; : %d NewLots : %d %.0 f $ ” , Names [ i ] , 9 19 var Threshold = optimize ( 1 . 0 , 0 . 5 , 2 , 0 . 1 ) ; Symbols [ i ] , OldLots [ i ] , NewLots ) ; 10 v a r BuyStop , S e l l S t o p ; 20 v a r ∗ DomPeriod = s e r i e s ( DominantPeriod ( 79 OldLots [ i ] = NewLots ; 11 Price , 3 0 ) ) ; 80 } 12 BuyStop = HH( 1 0 ) + 1∗PIP ; 21 v a r LowPeriod = LowPass ( DomPeriod , 5 0 0 ) ; 81 } 13 S e l l S t o p = LL ( 1 0 ) − 1∗PIP ; 22 v a r ∗HP = s e r i e s ( HighPass ( P r i c e , LowPeriod ∗ 82 } 14 optimize (1 ,0.5 ,2) ) ) ; 15 i f ( dow ( ) == 5 && NumOpenLong == 0 && 23 v a r ∗ S i g n a l = s e r i e s ( F i s h e r (HP, 5 0 0 ) ) ; Intraday seasonality in FX market Based on this paper Stop = o p t i m i z e ( 2 , 1 , 1 0 ) ∗ ATR( 1 0 0 ) ; NumPendingLong == 0 && SMA10 [ 0 ] > SMA40 24 [0]) 25 Listing 31: Intraday seasonality in FX market System16 e n t e r L o n g ( 0 , BuyStop ) ; 26 i f ( s t r s t r ( Algo , ” : L” ) and c r o s s U n d e r ( S i g n a l 1 var s e s s i o n 1 T Z = WET; 17 e l s e i f ( dow ( ) == 5 && NumOpenShort == 0 && ,− T h r e s h o l d ) ) 2 var session1Start = 8; NumPendingShort == 0 && SMA10 [ 0 ] < 27 enterLong ( ) ; 3 var session1End = 16; SMA40 [ 0 ] ) 28 e l s e i f ( s t r s t r ( Algo , ” : S” ) and c r o s s O v e r ( 4 var s e s s i o n 2 T Z = ET ; 18 enterShort (0 , SellStop ) ; Signal , Threshold ) ) 5 var s e s s i o n 2 S t a r t = 1 1 ; // = 16 WET 19 29 enterShort () ; 6 var session2End = 17; 20 i f ( dow ( ) != 5 && dow ( ) != 6 && dow ( ) != 7 ) 30 } 7 { 31
8 function tradeIS () 21 exitLong ( ) ; 32 f u n c t i o n HuckTrend ( )
11 i f ( NumOpenShort == 0 && l h o u r ( s e s s i o n 1 T Z ) == 24 35 TimeFrame = 1 ;
session1Start ) 25 } 36 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ; 12 enterShort () ; 26 37 v a r ∗LP5 = s e r i e s ( LowPass ( P r i c e , 5 ) ) ; 13 i f ( NumOpenShort > 0 && l h o u r ( s e s s i o n 1 T Z ) >= 27 38 v a r ∗LP10 = s e r i e s ( LowPass ( P r i c e , o p t i m i z e session1End ) 28 f u n c t i o n run ( ) { (10 ,6 ,20) ) ) ; 14 exitShort () ; 29 39 v a r ∗ RSI10 = s e r i e s ( RSI ( P r i c e , 1 0 ) ) ; 15 i f ( NumOpenLong == 0 && l h o u r ( s e s s i o n 2 T Z ) == 30 BarPeriod = 1440; 40 Stop = o p t i m i z e ( 5 , 1 , 1 0 ) ∗ATR( 3 0 ) ; session2Start ) 31 41 int crossed = SkillLong [ 0 ] ; 16 enterLong ( ) ; 32 w h i l e ( a s s e t ( l o o p ( ”USD/CHF” , ”USD/JPY” , ” 42 i n t Delay = 3 ; 17 i f ( NumOpenLong > 0 && l h o u r ( s e s s i o n 2 T Z ) >= GBP/USD” , ”EUR/USD” ) ) ) 43
session2End ) 33 tradeOneNightStand ( ) ; 44
18 exitLong ( ) ; 34 45 i f ( c r o s s O v e r ( LP5 , LP10 ) )
19 } 35 } 46 c r o s s e d = Delay ; 20 } 47 e l s e i f ( c r o s s U n d e r ( LP5 , LP10 ) ) c r o s s e d = −Delay ; 21 Listing 33: Portfolio trading Trend Counter Trend and48 22 f u n c t i o n run ( ) 49
23 { Huck Trend 50 i f ( s t r s t r ( Algo , ” : L” ) and ( c r o s s e d > 0 &&
24 StartDate = 2004; 1 // P o r t f o l i o t r a d i n g : Trend , Counter Trend c r o s s O v e r ( RSI10 , 5 0 ) ) ) { 25 UnstablePeriod = 0; and Huck Trend / / / / / / / / / / / / / / / / / / / 51 enterLong ( ) ; 26 LookBack = 0 ; 2 52 crossed = 0; 27 BarPeriod = 1 ; 3 f u n c t i o n tradeTrend ( ) 53 } e l s e i f ( s t r s t r ( Algo , ” : S” ) and ( c r o s s e d < 28 w h i l e ( a s s e t ( l o o p ( ”EUR/USD” ) ) ) 4 { 0 && c r o s s U n d e r ( RSI10 , 5 0 ) ) ) { 29 tradeIS () ; 5 TimeFrame = 1 ; 54 enterShort () ; 30 s e t (LOGFILE) ; 6 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ; 55 crossed = 0; 31 } 7 v a r ∗ Trend = s e r i e s ( LowPass ( P r i c e , o p t i m i z e 56 } else (250 ,100 ,1000) ) ) ; 57 S k i l l L o n g [ 0 ] −= s i g n ( c r o s s e d ) ; 58 } 2 // h t t p : / /www. o p s e r v e r . de / ubb7 / u b b t h r e a d s . php 50 enterShort () ; 59 ? ubb=s h o w f l a t&Number=414386& page=1 51 } 60 f u n c t i o n run ( ) 3 f u n c t i o n run ( ) { 52 61 { 4 53 62 s e t (PARAMETERS+FACTORS+LOGFILE) ; // u s e 5 s e t (LOGFILE) ; 54 } o p t i m i z e d p a r a m e t e r s and r e i n v e s t m e n t 6 factors 7 i n t FastPeriod = 8; System 63 B a r P e r i o d = 2 4 0 ; // 4 hour b a r s 8 i n t SlowPeriod = 2 1 ; 64 LookBack = 5 0 0 ; // needed f o r F i s h e r ( ) 9 int SignalPeriod = 9; Listing 35: System 65 NumWFOCycles = 8 ; // a c t i v a t e WFO 10 1 // 66 NumBarCycles = 4 ; // 4 t i m e s o v e r s a m p l i n g 11 BarPeriod = 1 5 ; 2 f u n c t i o n run ( ) 67 12 vars Close = s e r i e s ( priceClose () ) ; 3 68 i f ( ReTrain ) { 13 TimeFrame = 1 ; 4 69 UpdateDays = 3 0 ; // r e l o a d new p r i c e 14 5 { data from t h e s e r v e r e v e r y 30 days 15 MACD( C l o s e , F a s t P e r i o d , SlowPeriod , 6 70 SelectWFO = −1; // s e l e c t t h e l a s t c y c l e SignalPeriod ) ; 7 StartDate = 20110601; f o r re−o p t i m i z a t i o n 16 v a r s MainLine = s e r i e s (rMACD) ; 8 EndDate = 2 0 1 3 0 1 2 0 ; 71 } 17 v a r s S i g n a l L i n e = s e r i e s ( rMACDSignal ) ; 9 BarPeriod = 1440; 72 18 v a r s H i s t = s e r i e s ( rMACDHist ) ; 10 73 // p o r t f o l i o l o o p 19 11 Stop = 50∗ PIP ; 74 w h i l e ( a s s e t ( l o o p ( ”EUR/USD” , ”USD/CHF” ) ) ) 20 MACD( C l o s e , F a s t P e r i o d ∗ 2 , S l o w P e r i o d ∗ 2 , 12 T r a i l = 40∗ PIP ; 75 w h i l e ( a l g o ( l o o p ( ”TRND: L” , ”TRND: S” , ”CNTR: L” , SignalPeriod ∗2) ; 13 TrailLock = 1; ”CNTR: S” , ” HuckTrend : L” , ” HuckTrend : S” ) ) ) 21 v a r s MainLine30 = s e r i e s (rMACD) ; 14 76 { 22 v a r s S i g n a l L i n e 3 0 = s e r i e s ( rMACDSignal ) ; 15 77 // s e t up t h e o p t i m a l margin 23 v a r s H i s t 3 0 = s e r i e s ( rMACDHist ) ; 16 vars d a y l o w = s e r i e s ( pri ceL ow ( ) ) ; 78 i f ( Train ) 24 17 vars day high = s e r i e s ( priceHigh ( ) ) ; 79 Lots = 1 ; 25 MACD( C l o s e , F a s t P e r i o d ∗ 4 , S l o w P e r i o d ∗ 4 , 18 vars day close = series ( priceClose () ) ; 80 e l s e i f ( s t r s t r ( Algo , ” : L” ) and SignalPeriod ∗4) ; 19 vars EMA50 = s e r i e s (EMA( d a y c l o s e , 5 0 ) ) ; OptimalFLong > 0 ) { 26 v a r s MainLine60 = s e r i e s (rMACD) ; 20 81 Lots = 1 ; 27 v a r s S i g n a l L i n e 6 0 = s e r i e s ( rMACDSignal ) ; 21 82 Margin = clamp ( ( WinLong−LossLong ) ∗ 28 v a r s H i s t 6 0 = s e r i e s ( rMACDHist ) ; 22 OptimalFLong / 2 , 5 0 , 1 0 0 0 0 ) ; 29 23 83 } e l s e i f ( s t r s t r ( Algo , ” : S” ) and 30 MACD( C l o s e , F a s t P e r i o d ∗ 1 6 , S l o w P e r i o d ∗ 1 6 , 24 i f ( d a y c l o s e [ 0 ] < d a y l o w [ 1 ] && d a y c l o s e OptimalFShort > 0 ) { SignalPeriod ∗16) ; [ 0 ] < EMA50 [ 1 ] ) 84 Lots = 1 ; 31 v a r s MainLine240 = s e r i e s (rMACD) ; 25 enterShort () ; 85 Margin = clamp ( ( WinShort−L o s s S h o r t ) ∗ 32 v a r s S i g n a l L i n e 2 4 0 = s e r i e s ( rMACDSignal ) ; 26 OptimalFShort / 2 , 5 0 , 1 0 0 0 0 ) ; 33 v a r s H i s t 2 4 0 = s e r i e s ( rMACDHist ) ; 27 86 } else 34 28 p l o t ( ”EMA50” , EMA50 [ 0 ] , 0 , RED) ; 87 L o t s = 0 ; // s w i t c h o f f t r a d i n g 35 29 88 36 30 } 89 i f ( s t r s t r ( Algo , ”TRND” ) ) 37 i f ( c r o s s O v e r ( MainLine , S i g n a l L i n e ) && 90 tradeTrend ( ) ; MainLine30 [ 0 ] > S i g n a l L i n e 3 0 [ 0 ] 91 e l s e i f ( s t r s t r ( Algo , ”CNTR” ) ) 38 && MainLine30 [ 0 ] > S i g n a l L i n e 6 0 [ 0 ] && Listing 36: System 92 tradeCounterTrend ( ) ; MainLine240 [ 0 ] > S i g n a l L i n e 2 4 0 [ 0 ] ) { 1 // B u i l d 0004 93 e l s e i f ( s t r s t r ( Algo , ” HuckTrend ” ) ) 39 2 94 HuckTrend ( ) ; 40 3 // #d e f i n e ASSETLOOP ”USD/CHF” , ”EUR/USD” , ” 95 41 exitShort () ; GBP/USD” , ”USD/CAD” , ”AUD/USD” , ”USD/JPY” , 96 } 42 enterLong ( ) ; ”XAU/USD” , ”XAG/USD” , ”NAS100 ” , ”SPX500 ” , 97 43 } ”GER30” , ”US30 ” , ”UK100” //FOREX SET 98 PlotWidth = 1 0 0 0 ; 44 4 #d e f i n e ASSETLOOP ”EUR/USD” , ”GBP/USD” , ”USD/ 99 PlotHeight1 = 320; 45 i f ( c r o s s U n d e r ( MainLine , S i g n a l L i n e ) && CAD” , ”AUD/USD” , ”USD/JPY” , ”XAU/USD” , ” 100 } MainLine30 [ 0 ] < S i g n a l L i n e 3 0 [ 0 ] XAG/USD” // No S t o c k 46 && MainLine30 [ 0 ] < S i g n a l L i n e 6 0 [ 0 ] && 5 //#d e f i n e ASSETLOOP ”USD/CHF” , ”EUR/USD” , ”USD MainLine240 [ 0 ] < S i g n a l L i n e 2 4 0 [ 0 ] ) { /JPY” , ”XAU/USD” , ” SPX500”//MIN FOREX SET 47 6 //#d e f i n e ASSETLOOP ”EUR/USD” // t e s t a s s e t Listing 34: System 48 7 1 49 exitLong ( ) ; 8 //−−−− G l o b a l VAR −−−− 9 i n t myCapital = 0 ; 51 } else { 104 } 10 v a r myMargin = 0 ; 52 Margin=myMargin ; 105 11 v a r comp = 0 ; 53 enterLong ( ) ; 106 f u n c t i o n run ( ) 12 //−−−−−−−−−−−−−−−−−−−− 54 } 107 { 13 55 } 108 s e t (PARAMETERS+FACTORS) ; // g e n e r a t e and 14 function setSlider () 56 u s e o p t i m i z e d p a r a m e t e r s and f a c t o r s 15 { 57 f u n c t i o n CLSTR( ) 109 16 myCapital = s l i d e r ( 1 , 2 5 0 0 , 0 , 2 5 0 0 0 , ” C a p i t a l ” 58 { 110 B a r P e r i o d = 6 0 ; // 1 hour b a r s , ” I n i t i a l C a p i t a l ” ) ; // used f o r 59 TimeFrame =24; 111 LookBack = 2 5 0 0 ; // needed f o r F i s h e r ( ) compounding c a l c u l a t i o n 60 112 NumWFOCycles = 6 ; // a c t i v a t e WFO 17 myMargin = s l i d e r ( 2 , 5 0 , 0 , 5 0 0 , ” Margin ” , ” 61 Stop = 2∗ATR( 1 4 ) ; 113 I n i t i a l Margin ” ) ; // f i x e d o r i n i t i a l 62 T r a i l = Stop ; 114 StartDate = 2009; Margin 63 TrailLock = 10; 115 EndDate = 2 0 1 4 ; 18 comp= s l i d e r ( 3 , 0 , 0 , 1 , ”Comp . ” , ”0=Fixed 64 116 Hedge =5; Margin 1=Compound Margin ” ) ; 65 checkEquity ( ) ; 117 19 } 66 118 i f ( ReTrain ) { 20 67 v a r dayL = o p t i m i z e ( 4 0 , 1 0 , 8 0 ) ; 119 UpdateDays = −1; // update p r i c e data 21 f u n c t i o n checkEquity ( ) 68 v a r dayS = o p t i m i z e ( 4 0 , 1 0 , 8 0 ) ; from t h e s e r v e r 22 { 69 120 SelectWFO = −1; // s e l e c t t h e l a s t c y c l e 23 // e q u i t y c u r v e t r a d i n g : s w i t c h t o phantom 70 i f ( p r i c e H i g h ( ) >= HH( dayL ) ) enterFSLong ( ) ; f o r re−o p t i m i z a t i o n mode when t h e e q u i t y 71 i f ( pr ice Low ( ) <= LL( dayS ) ) e n t e r F S S h o r t ( ) ; 121 r e s e t (FACTORS) ; // don ’ t g e n e r a t e f a c t o r s 24 // c u r v e g o e s down and i s below i t s own 72 } when re−t r a i n i n g lowpass f i l t e r e d value 73 122 } 25 74 f u n c t i o n CNTR( ) 123 NumWFOCycles = 6 ; // a c t i v a t e WFO 26 i f ( Train ) { L o t s = 1 ; r e t u r n ; } // no 75 { 124 phantom t r a d e s i n t r a i n i n g mode 76 TimeFrame = 4 ; 125 setSlider () ; 27 v a r s EquityCurve = s e r i e s ( P r o f i t C l o s e d+ 77 Stop = o p t i m i z e ( 4 , 2 , 8 ) ∗ ATR( 1 0 0 ) ; 126 ProfitOpen ) ; 78 T r a i l = 4∗ATR( 1 0 0 ) ; 127 // p o r t f o l i o l o o p 28 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve 79 128 w h i l e ( a s s e t ( l o o p (ASSETLOOP) ) ) ,10) ) ; 80 vars Price = s e r i e s ( price () ) ; 129 w h i l e ( a l g o ( l o o p ( ”TRND” , ”CNTR” , ”CLSTR” ) ) ) 29 i f ( EquityLP [ 0 ] < LowPass ( EquityLP , 1 0 0 ) && 81 v a r s F i l t e r e d = s e r i e s ( BandPass ( P r i c e , 130 { f a l l i n g ( EquityLP ) ) optimize (30 ,20 ,40) ,0.5) ) ; 131 i f ( Algo == ”TRND” ) 30 L o t s = −1; // drawdown −> phantom t r a d i n g 82 v a r s S i g n a l = s e r i e s ( F i s h e r ( F i l t e r e d , 5 0 0 ) ) ; 132 TRND( ) ; 31 else 83 var Threshold = optimize ( 1 , 0 . 5 , 1 . 5 , 0 . 1 ) ; 133 32 L o t s = 1 ; // p r o f i t a b l e −> normal t r a d i n g 84 134 i f ( Algo == ”CNTR” ) 33 } 85 checkEquity ( ) ; 135 CNTR( ) ; 34 86 136 35 f u n c t i o n enterFSLong ( ) 87 i f ( c r o s s U n d e r ( S i g n a l ,− T h r e s h o l d ) ) 137 i f ( Algo == ”CLSTR” ) 36 { enterFSLong ( ) ; 138 CLSTR( ) ; 37 i f ( comp == 1 && ! i s (TRAINMODE) ) { 88 e l s e i f ( crossOver ( Signal , Threshold ) ) 139 } 38 Margin = OptimalFLong ∗ myMargin ∗ s q r t ( 1 enterFSShort ( ) ; 140 + max ( 0 , ( WinLong−LossLong ) / myCapital )89 } 141 PlotWidth = 7 0 0 ; ); 90 142 PlotHeight1 = 400; 39 enterLong ( ) ; 91 f u n c t i o n TRND( ) 143 // ColorUp = ColorDn = ColorWin = C o l o r L o s s 40 } else { 92 { = 0 ; // don ’ t p l o t c a n d l e s and t r a d e s 41 Margin=myMargin ; 93 TimeFrame = 1 ; 144 // s e t (TESTNOW+PLOTNOW) ; 42 enterLong ( ) ; 94 Stop = o p t i m i z e ( 4 , 2 , 8 ) ∗ ATR( 1 0 0 ) ; 145 } 43 } 95 Trail = 0; } 44 45 96 97 vars Price = s e r i e s ( price () ) ; Trademanagement Function 46 function enterFSShort ( ) 98 v a r s Trend = s e r i e s ( LowPass ( P r i c e , o p t i m i z e TradePriceOpen The ask price when the trade was opened. 47 { (500 ,300 ,800) ) ) ; If the trade was not yet opened, it’s the current price of 48 i f ( comp == 1 && ! i s (TRAINMODE) ) { 99 the asset. 49 Margin = OptimalFShort ∗ myMargin ∗ s q r t 100 checkEquity ( ) ; TradePriceClose ( 1 + max ( 0 , ( WinShort−L o s s S h o r t ) / 101 TradeUnits myCapital ) ) ; 102 i f ( v a l l e y ( Trend ) ) enterFSLong ( ) ; 50 enterLong ( ) ; 103 e l s e i f ( peak ( Trend ) ) e n t e r F S S h o r t ( ) ; TradeRoll TradeProfit Listings 19 Print the description of a trade (like ”[AU- 1 Chan1ge stops and profit targets of all open long D/USD:CY:S1234 . . . . . . . . . . . . . . . . . 3 TradeEntryLimit trades with the current algo and asset . . . . . . 1 20 Plot equity curves of single assets in a multi-asset TradeLots 2 Limit the number of open positions // max. 3 strategy . . . . . . . . . . . . . . . . . . . . . . . 3 open long positions per asset/algo . . . . . . . . 1 21 Set up strategy parameters from a .ini file at the TradeIsShort 3 Exit all open trades Friday afternoon GMT . . . 1 start . . . . . . . . . . . . . . . . . . . . . . . . . 3 TradeIsLong 4 Lock 80% profit of all winning trades . . . . . . . 1 22 Check every minute in Trade mode if an .ini file 5 Calculate the value of all open trades with the was modified . . . . . . . . . . . . . . . . . . . . 3 Trading auxiallary functions current asset . . . . . . . . . . . . . . . . . . . . 1 23 Trade multiple strategies and assets in a single 6 Monitoring and modifying a certain trade . . . . 2 script . . . . . . . . . . . . . . . . . . . . . . . . 3 7 Generate an indicator with a different asset time 24 Update price history of many assets . . . . . . . 3 Listing 37: TrailingStop frame and shift . . . . . . . . . . . . . . . . . . . 2 25 Portfolio strategy with 3 assets and 3 trade func- 1 int TrailingStop () 8 Calculate the weekend price change for gap trading 2 tions . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 { 9 Use a series to check if something happened 26 YenTrader System . . . . . . . . . . . . . . . . . 4 3 // a d j u s t t h e s t o p o n l y when t h e t r a d e i s within the last n bars . . . . . . . . . . . . . . . 2 27 Simple profit system . . . . . . . . . . . . . . . . 4 in p r o f i t 10 Use a loop to check if something happened within 28 Profitable System . . . . . . . . . . . . . . . . . . 5 4 i f ( TradeProfit > 0) the last n bars . . . . . . . . . . . . . . . . . . . 2 29 Mean Variance Optimization System . . . . . . . 5 5 // p l a c e t h e s t o p a t t h e l o w e s t bottom o f 11 Align a time frame to a certain event . . . . . . . 2 30 Intraday seasonality in FX market System . . . . 6 the previous 3 candles 12 Shift a series into the future . . . . . . . . . . . . 2 31 One Night Stand System . . . . . . . . . . . . . . 6 6 TradeStopLimit = max( TradeStopLimit , LL ( 3 ) ) ; 13 Use a function from an external DLL . . . . . . . 2 32 Portfolio trading Trend Counter Trend and Huck 7 // p l o t a l i n e t o make t h e s t o p l i m i t 14 Equity curve trading (skipping trades dependent Trend . . . . . . . . . . . . . . . . . . . . . . . . 6 v i s i b l e in the chart on strategy success) . . . . . . . . . . . . . . . . 2 33 System . . . . . . . . . . . . . . . . . . . . . . . . 7 8 p l o t ( ” Stop ” , TradeStopLimit , MINV,BLACK) ; 15 Debugging a script . . . . . . . . . . . . . . . . . 2 34 System . . . . . . . . . . . . . . . . . . . . . . . . 7 9 // r e t u r n 0 f o r c h e c k i n g t h e l i m i t s 16 Find out if you have a standard mini or micro lot 35 System . . . . . . . . . . . . . . . . . . . . . . . . 7 10 return 0; account . . . . . . . . . . . . . . . . . . . . . . . 3 36 TrailingStop . . . . . . . . . . . . . . . . . . . . . 8 11 } 17 Download historic price data . . . . . . . . . . . 3 18 Export historic price data to a .csv file . . . . . . 3 Resources