A Quick Guide To Zorro: What Is Zorro? Variables Code From Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

A quick guide to Zorro

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 #d e f i n e H24 ( 1 4 4 0 / B a r P e r i o d )
x+3 i s below 9 , o r i f y i s e q u a l t o 0 2 #d e f i n e H4 ( 2 4 0 / B a r P e r i o d )
0 when [No] was clicked,
2 z = 10; 3 #d e f i n e H1 ( 6 0 / B a r P e r i o d )
3 else 1 when [Yes] was clicked. 4
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 5 i f ( m y l o n g c o n d i t i o n == t r u e ) {
File functions 6 e x i t S h o r t ( ) ; // no h e d g i n g − c l o s e all
short positions
while file copy (string dest, string src)
7 i f ( NumOpenLong < 3 ) e n t e r l o n g ( ) ;
1 x = 0; file delete (string name) 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
file length
than 100
3 x += 1 ; file date Listing 3: Exit all open trades Friday afternoon GMT
1 i f ( dow ( ) == FRIDAY && hour ( ) >= 1 8 ) {
file select Opens a file dialog box at a given directory that
2 exitLong ( ”∗” ) ;
for loop lets the user select a file to open. Returns the selected
3 e x i t S h o r t ( ”∗” ) ;
file name including path.
1 int i ; 4 }
2 f o r ( i =0; i <5; i ++) // r e p e a t 5 t i m e s file content
3 x += i ;
file read Reads the content of a file into a string or array. Listing 4: Lock 80% profit of all winning trades
Returns the number of read bytes.
1 for ( open trades )
switch file write Stores the content of a string, series, or other data 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
1 int choice ; array in a file. > 0) {
2 ... 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
file append Opens a file and appends text or other data to
3 switch ( choice ) trade costs )
it; can be used to export data to Excel or other
4 { 4 i f ( TradeIsShort )
programs. If the file does not exist, it is created.
5 case 0: 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 ,
6 p r i n t f ( ” Zero ! ” ) ; TradePriceClose ) ;
7 break ; Testing 6 else
8 case 1: Testing 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 ,
9 p r i n t f ( ”One ! ” ) ; TradePriceClose ) ;
10 break ; Debugging 8 }
11 case 2: if(date() == 20150401) set(STEPWISE); watch (”!...”, ...)
12 p r i n t f ( ”Two ! ” ) ;
13 break ; Biasis Listing 5: Calculate the value of all open trades with
14 default : Curve fitting bias, Market fitting bias, Peeking bias, Data the current asset
15 p r i n t f ( ”None o f them ! ” ) ; mining bias, Trend bias, Granularity bias, Sample size bias 1 v a r valOpen ( )
16 } 2 {
Asset Parameters 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
Input/Output Spread, eg Spread = 3*PIP Slippage, default = 10 changed i n t h e f o r l o o p
printf (string format, ...) Commission, taken from AssetsFix.csv RollShort RollLong 4 var val = 0 ;
5 for ( open trades ) Indicators Signals Listing 11: Use a loop to check if something happened
6 i f ( s t r s t r ( Asset , C u r r e n t A s s e t ) && within the last n bars
TradeIsOpen )
Listing 8: Generate an indicator with a different asset 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
7 v a l += T r a d e P r o f i t ;
8 return val ; time frame and shift the l a s t 7 bars
9 } 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 2 ...
and t i m e f r a m e ( i n mi n ut es ) 3 int i ;
2 v a r extATR ( s t r i n g symbol , i n t p e r i o d , i n t 4 f o r ( i = 0 ; i < 7 ; i ++)
Listing 6: Monitoring and modifying a certain trade length , i n t s h i f 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 ) ) { //
3 { c r o s s o v e r , i b a r s ago ?
1 6 enterLong ( ) ;
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
2 TRADE∗ MyTrade = e n t e r l o n g ( ) ; 7 b r e a k ; // a b o r t t h e l o o p
asset
3 ... 8 }
5 i f ( symbol ) a s s e t ( symbol ) ; // s e t new a s s e t
4 ThisTrade = MyTrade ; // c o n n e c t t r a d e 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 ;
v a r i a b l e s t o MyTrade
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 /
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
timeframe
variables
8 vars H = s e r i e s ( priceHigh () ) , Listing 12: Align a time frame to a certain event
6 ...
9 L = s e r i e s ( pr ice Low ( ) ) ,
7 e x i t T r a d e ( MyTrade , 0 , TradeLots / 2 ) ; // e x i t 1 // Let time frame s t a r t when Event == t r u e
10 O = s e r i e s ( priceOpen ( ) ) ,
h a l f the trade 2 // f . i . f r a m e A l i g n ( hour ( ) == 0 ) ; a l i g n s t o
11 C = series ( priceClose () ) ;
8 } midnight
12 TimeFrame = 1 ; // s e t t i m e f r a m e back
13 g−>a s s e t = p r e v i o u s ; // s e t a s s e t back 3 f u n c t i o n f r a m e A l i g n (BOOL Event )
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 , 4 {
Listing 7: Correlation / heatmap length ) ; 5 TimeFrame = 1 ;
1 #d e f i n e DAYS 252 // 1 y e a r 15 } 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
2 #d e f i n e NN 30 // max number o f a s s e t s s t o r i n g Num between c a l l s
3 7 Num [ 0 ] = Num[ 1 ] + 1 ; // count Num up once
4 #i n c l u d e < p r o f i l e . c> Listing 9: Calculate the weekend price change for gap p e r bar
8 i f ( ! Event )
5 trading
6 // p l o t a heatmap o f a s s e t c o r r e l a t i o n s 9 TimeFrame = 0 ; // c o n t i n u e c u r r e n t
1 // u s e 1−hour bars , w a i t u n t i l Sunday Sunday time frame
7 f u n c t i o n run ( )
5pm ET, else {
8 { 10
2 // then g e t t h e p r i c e change from F r i d a y 5pm 11 TimeFrame = −Num [ 0 ] ; // s t a r t a new time
9 BarPeriod = 1440;
ET frame
10 StartDate = 20150101;
3 i f ( dow ( ) == SUNDAY && l h o u r (ET) == 5 ) { 12 Num [ 0 ] = 0 ; // r e s e t t h e c o u n t e r
11 LookBack = DAYS;
4 i n t FridayBar = t i m e O f f s e t (ET,SUNDAY−FRIDAY 13 }
12
,5 ,0) ; 14 }
13 v a r s Returns [NN ] ;
5 v a r PriceChange = p r i c e C l o s e ( 0 ) −
14 v a r C o r r e l a t i o n s [NN ] [ NN ] ; // NN∗NN m a t r i x
p r i c e C l o s e ( FridayBar ) ;
15 int N = 0;
6 }
16 w h i l e ( a s s e t ( l o o p ( . . . /∗ some a s s e t s ∗/ . . ) ) ) Listing 13: Shift a series into the future
17 {
18 Returns [N] = s e r i e s ( ( p r i c e ( 0 )−p r i c e ( 1 ) ) / 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
Listing 10: 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
price (0) ) ;
19 N++; // count number o f a s s e t s within the last n bars value
20 } 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 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 )
21 the l a s t 7 bars 4 {
22 int i , j ; 2 ... 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
23 i f ( ! i s (LOOKBACK) ) { 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 past
24 f o r ( i =0; i <N; i ++) s e r i e s and s e t i t t o 0 6 r e t u r n Data+s h i f t ;
25 f o r ( j =0; j <N; j ++) 4 i f ( crossOver ( Signal1 , Signal2 ) 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
26 C o r r e l a t i o n s [N∗ i+j ] = C o r r e l a t i o n ( Returns 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 8 int i ;
[ i ] , Returns [ j ] ,DAYS) ; series 9 f o r ( i = 1 ; i <= s h i f t ; i ++)
27 6 i f (Sum( c r o s s e s , 7 ) > 0 ) // any c r o s s o v e r 10 Data [ i ] = Data [ 0 ] ;
28 plotHeatmap ( C o r r e l a t i o n s ,N) ; within l a s t 7 bars ? 11 r e t u r n Data ;
29 q u i t ( ”” ) ; 7 enterLong ( ) ; 12 }
30 } 8 ... 13 }
31 }
Listing 14: Use a function from an external DLL 4 a s s e t ( ”EUR/USD” ) ; Listing 20: Print the description of a trade (like ”[AU-
1 // Use t h e f u n c t i o n ” f o o ” from t h e DLL ” bar . 5 i f ( Bar > 0 ) { D/USD:CY:S1234
dll” 6 i f ( LotAmount > 9 9 9 9 9 )
1 void printTradeID ( )
// Copy bar . d l l i n t o t h e Z o r r o f o l d e r 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 ! ”
2 2 {
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 ) ; // );
3 3 s t r i n g l s = ”L” , bo = ” [ ” , bc = ” ] ” ;
foo prototype 8 e l s e i f ( LotAmount > 9 9 9 9 )
4 i f ( T r a d e I s S h o r t ) l s = ”S” ;
API ( f o o , bar ) // u s e f o o from bar . d l l 9 p r i n t f ( ” \ n I have a mini l o t a c c o u n t ! ” ) ;
4 5 i f ( TradeIsPhantom ) { bo = ” { ” ; bc = ” } ” ; }
10 else
5 6 p r i n t f ( ”#\n%s%s :% s :% s %04 i%s ” ,
f u n c t i o n run ( ) 11 p r i n t f ( ” \ n I have a micro l o t a c c o u n t ! ” ) ;
6 7 bo , TradeAsset , TradeAlgo , l s , TradeID %10000 , bc
{ 12 quit () ;
7 );
... 13 }
8 8 }
9 int r e s u l t = foo (1 ,2) ; 14 }
10 ...
11 } Listing 21: Plot equity curves of single assets in a
Listing 18: Download historic price data
multi-asset strategy
1 // C l i c k [ Test ] f o r downloading / u p d a t i n g t h e
l a t e s t ”NZD/USD” p r i c e data 1 c h a r name [ 4 0 ] ; // s t r i n g o f maximal 39
Listing 15: Equity curve trading (skipping trades de- characters
2 // This e x t e n d s t h e l e n g t h o f t h e historical
pendent on strategy success) 2 s t r c p y ( name , A s s e t ) ;
p r i c e data f i l e , t h e r e f o r 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 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 3 s t r c a t ( name , ” : ” ) ;
down trained again afterwards 4 s t r c a t ( name , Algo ) ;
2 // and i s below i t s own l o w p a s s f i l t e r e d 4 f u n c t i o n run ( ) 5 v a r e q u i t y = E q u i t y S h o r t+EquityLong ;
value 5 { 6 i f ( e q u i t y != 0 ) p l o t ( name , e q u i t y ,NEW|AVG,BLUE
3 f u n c t i o n checkEquity ( ) 6 NumYears = 6 ; // download up t o 6 y e a r s );
4 { data
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 l o a d H i s t o r y ( ”NZD/USD” , 1 ) ; // update t h e
trades
7
Listing 22: Set up strategy parameters from a .ini file
price history
6 v a r s EquityCurve = s e r i e s ( EquityLong+ 8 }
at the start
EquityShort ) ; 1 f u n c t i o n run ( )
7 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve 2 {
,10) ) ; Listing 19: Export historic price data to a .csv file 3 s t a t i c v a r Parameter1 = 0 , Parameter2 = 0 ;
8 i f ( EquityLP [ 0 ] < LowPass ( EquityLP , 1 0 0 ) && 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 4 i f ( i s (INITRUN) ) { // r e a d t h e p a r a m e t e r s
f a l l i n g ( EquityLP ) ) . c s v f i l e i n t h e Data f o l d e r o n l y i n t h e f i r s t run
9 L o t s = −1; // drawdown −> phantom t r a d e s 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 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 \\
10 else , open , high , low , c l o s e mysetup . i n i ” ) ;
11 L o t s = 1 ; // p r o f i t a b l e −> normal t r a d e s 3 // f . i . ”31/12/12 0 0 : 0 0 , 1 . 3 2 2 0 5 , 1 . 3 2 3 4 1 , 6 Parameter1 = s t r v a r ( s e t u p , ” Parameter1 ” ) ;
1.32157 , 1.32278” 7 Parameter2 = s t r v a r ( s e t u p , ” Parameter2 ” ) ;
Auxiliary 4 // Dependent on t h e l o c a l e , d a t e and numbers 8 }
might r e q u i r e a d i f f e r e n t f o r m a t 9 }
Listing 16: Debugging a script 5 f u n c t i o n run ( ) 10

6 { 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
1 // D i s p l a y a message box with t h e v a r i a b l e s contains
7 BarPeriod = 1440;
t o be o b s e r v e d // 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
8 StartDate = 2008; 12
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 :
9 EndDate = 2 0 1 2 ;
c l o s i n g t h e box Parameter1 = 123
10 LookBack = 0 ; 13
3 s t a t i c i n t debug = 1 ; 14 Parameter2 = 456
11
4 i f ( debug && Bar > LookBack )
12 string line = strf (
5 debug = msg (
13 ”%02 i /%02 i /%02 i %02 i :%02 i , %.5 f , %.5 f , %.5 f
6 ” High = %.5 f , Low = %.5 f ” , Listing 23: Check every minute in Trade mode if an
, %.5 f \n” ,
7 p r i c e H i g h ( ) , pr ice Low ( ) ) ; .ini file was modified
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 ( ) , 1 v a r Parameter1 = 0 , Parameter2 = 0 ;
Listing 17: Find out if you have a standard mini or priceClose () ) ; 2
16 i f ( i s (INITRUN) ) 3 f u n c t i o n t o c k ( ) // run once p e r minute
micro lot account 17 f i l e d e l e t e ( ” Data \\ e x p o r t . c s v ” ) ; 4 {
1 // C l i c k [ Trade ] with t h e s c r i p t below 18 else 5 s t a t i c i n t LastDate = 0 ;
2 f u n c t i o n run ( ) 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 ) ; 6 i f ( LastDate && ! Trade ) r e t u r n ; // a l r e a d y
3 { 20 } updated
7 i n t NewDate = f i l e d a t e ( ” S t r a t e g y \\ mysetup . 4 a l g o ( ”TRL” ) ; Listing 28: YenTrader System
ini ”) ; 5 ...
8 i f ( LastDate < NewDate ) { 6 } 1
9 LastDate = NewDate ; // f i l e was m o d i f i e d : 7 2 bool UseEquityFilter = f a l s e ;
update new p a r a m e t e r s 8 f u n c t i o n tradeTrendShort ( ) 3
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 \\ 9 { 4 f u n c t i o n TradeYenTrader ( )
mysetup . i n i ” ) ; 10 a l g o ( ”TRS” ) ; 5 {
11 Parameter1 = s t r v a r ( s e t u p , ” Parameter1 ” ) ; 11 ... 6 vars Price = s e r i e s ( price () ) ;
12 Parameter2 = s t r v a r ( s e t u p , ” Parameter2 ” ) ; 12 } 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
13 } 13 (100 , 5 , 200 , 5) ) ) ;
14 } 14 function tradeBollinger () 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 ,
15 { 5 , 200 , 5) ) ) ;
16 a l g o ( ”BOL” ) ; 9
Listing 24: Trade multiple strategies and assets in a17 ... 10 Stop = o p t i m i z e ( 1 5 , 5 , 5 0 ) ∗ PIP ;
single script 18 } 11 T r a i l = o p t i m i z e ( 1 , 1 , 2 0 ) ∗ PIP ;
19 12
1 f u n c t i o n run ( )
20 f u n c t i o n tradeFunc ( ) ; // empty f u n c t i o n 13 // Equity c u r v e f i l t e r
2 {
pointer 14 v a r LotsBackup = L o t s ;
3 BarPeriod = 2 4 0 ;
21 15
4 StartDate = 2010;
22 f u n c t i o n run ( ) 16 i f ( UseEquityFilter )
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
23 { 17 {
flags before calling asset ()
24 while ( loop ( 18 v a r s EquityCurve = s e r i e s ( EquityLong+
6
25 ”EUR/USD” , EquityShort ) ;
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
26 ”USD/CHF” , 19 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve
different assets
27 ”GBP/USD” ) ) // l o o p t h r o u g h 3 a s s e t s ,75) ) ;
8 a s s e t ( ”EUR/USD” ) ;
28 w h i l e ( tradeFunc = l o o p ( 20 i f ( EquityLP [ 0 ] < LowPass ( EquityLP , 1 0 0 ) &&
9 tradeLowpass ( ) ;
29 tradeTrendLong , f a l l i n g ( EquityLP ) )
10 tradeFisher () ;
11
30 tradeTrendShort , 21 L o t s = −1;
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 22 else
12 a s s e t ( ”GBP/USD” ) ;
algorithms 23 Lots = 1 ;
13 tradeAverage ( ) ;
32 { 24 }
14
33 a s s e t ( Loop1 ) ; // s e l e c t a s s e t 25
15 a s s e t ( ”SPX500” ) ;
34 tradeFunc ( ) ; // c a l l t h e t r a d e f u n c t i o n 26 i f ( NumOpenShort + NumOpenLong == 0 )
16 tradeBollinger () ;
35 } 27 {
17 }
36 } 28 i f ( SMAFast [ 0 ] > SMASlow [ 0 ] )
29 {
Plotting Margin = 0 . 5 ∗ OptimalFLong ∗ C a p i t a l ∗ s q r t (
Listing 25: Update price history of many assets 30
max ( 1 , B a l a n c e / C a p i t a l ) ) ;
1 f u n c t i o n run ( ) Listing 27: Plot Triangle above Candle Patterns 31 enterLong ( ) ;
2 {
1 f u n c t i o n run ( ) 32 }
3 NumYears = 2 ;
2 { 33 else
4 w h i l e ( l o o p ( ”AUD/USD” , ”EUR/USD” , ”GBP/USD” , ”
3 s e t (PLOTNOW) ; 34 {
GER30” , ”NAS100” ,
4 PlotBars = 300; 35 Margin = 0 . 5 ∗ OptimalFShort ∗ C a p i t a l ∗ s q r t
5 ”SPX500” , ”UK100” , ”US30” , ”USD/CAD” , ”USD/CHF”
5 PlotScale = 8; (max ( 1 , B a l a n c e / C a p i t a l ) ) ;
, ”USD/JPY” ,
6 i f ( CDLDoji ( ) ) 36 enterShort () ;
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, 37 }
7 {
BLUE) ; 38 }
8 a s s e t H i s t o r y ( Loop1 , 1 ) ;
8 i f ( CDLHikkake ( ) > 0 ) 39
9 }
9 p l o t ( ” Hikkake+” , 0 . 9 9 8 ∗ pr ice Low ( ) ,TRIANGLE, 40 L o t s = LotsBackup ;
10 }
GREEN) ; 41 }
10 i f ( CDLHikkake ( ) < 0 ) 42

Listing 26: 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 43 f u n c t i o n run ( )
,RED) ; 44 {
functions 12 } 45 s e t (PARAMETERS+FACTORS) ;
1 46
2 f u n c t i o n tradeTrendLong ( ) Systems 47 BarPeriod = 1440;
3 { YenTrader System 48 LookBack = 2 0 0 ;
49 StartDate = 20010101; 39 } Listing 31: Mean Variance Optimization System
50 EndDate = 2 0 1 6 0 6 0 1 ;
51 MonteCarlo = 1 0 0 0 ; Profitable System - EUR/USD 1 //
52 Confidence = 100; //////////////////////////////////////////////////
53 Capital = 10000; Listing 30: Profitable System
54 NumWFOCycles = 1 0 ; 1 f u n c t i o n run ( ) 2 // Mean V a r i a n c e O p t i m i z a t i o n
55
2 { ////////////////////////
56 a s s e t ( ”USD/JPY” ) ; 3 NumCores = −2; // use m u l t i p l e c o r e s ( 3 //
57 TradeYenTrader ( ) ; Zorro S only ) //////////////////////////////////////////////////
58 } 4 B a r P e r i o d = 6 0 ; // 1 hour b a r s
4
Simple profit system - EUR/USD 5 LookBack = 1 2 4 0 ; // needed f o r F i s h e r ( )
6 StartDate = 2010; 5 //#d e f i n e DAYS 252 // 1 y e a r
EndDate = 2 0 1 5 ; // fixed simulation 6 #d e f i n e DAYS 6∗22 // 6 Months
Listing 29: Simple profit system 7
7 //#d e f i n e DAYS 4∗22 // 4 Months
period
1
8
8 //#d e f i n e DAYS 2∗22 // 2 Months
2 f u n c t i o n run ( ) 9 s e t (PARAMETERS) ; // g e n e r a t e and u s e 9
3 { 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 10 #d e f i n e WEIGHTCAP . 2 5 // Cap 0 . 1 5 − 0 . 5
4
10
Range
5 B a r P e r i o d = 6 0 ; // 1 hour b a r s 11 Stop = 5 ∗ ATR( 9 0 ) ; 11 #d e f i n e NN 50 // max number o f
6 LookBack = 2 0 ; 12 T a k e P r o f i t =4∗ ATR( 3 0 ) ; assets
7 StartDate = 2010; 13
12 #d e f i n e LEVERAGE 4 // 1 : 4 l e v e r a g e
8 EndDate = 2 0 1 6 0 8 0 4 ; 14 v a r nWI TOP=o p t i m i z e ( 3 0 , 5 , 3 5 ) ; 13
9
15 v a r nWI DOWN=o p t i m i z e ( 7 0 , 6 0 , 1 0 0 ) ; 14 //
10 //NumWFOCycles = 6 ; 16
//////////////////////////////////////////////////
11 // s e t (LOGFILE) ; 17 v a r s aOpen = series ( price () ) ;
12 // C a p i t a l = 5 0 0 0 ; 18 v a r s aAlma = s e r i e s (ALMA( aOpen , 15
13 // Margin = 0 . 0 0 1 ∗ OptimalF ∗ C a p i t a l ∗ s q r t 19 optimize (8.61 ,8 ,30) , 16 f u n c t i o n run ( )
(1 + P r o f i t C l o s e d / Capital ) ; 20 optimize (6.15 ,6 ,19) , 17 {
14 Hedge = 2 ; 21 0.8) ) ; 18 BarPeriod = 1440;
15 L o t s =1; 22
19 LookBack = DAYS;
16 NumOptCycles = 2 ; 23 v a r s MMI Raw = s e r i e s (MMI( aOpen , o p t i m i z e 20 NumYears = 7 ;
17 NumSampleCycles = 2 ; (300 ,100 ,500) ) ) ; 21 s e t (PRELOAD) ; // a l l o w e x t r e m e l y l o n g
18
24 v a r s MMI Smooth = s e r i e s ( LowPass (MMI Raw lookback period
19
,500) ) ; 22 s e t (LOGFILE) ;
20 s e t (PARAMETERS+FACTORS) ; 25
23 Verbose = 0 ;
21 v a r StopL = o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( 26 v a r s aEma1 = s e r i e s (EMA( aOpen , o p t i m i z e 24 s e t ( watch ) ;
optimize (3 ,1 ,10) ) ; (12.6 ,12 ,100) ) ) ; 25
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( 27
26 // A s s e t L i s t = ”ETF2016−OK. c s v ” ;
optimize (3 ,1 ,10) ) ; 28 v a r s aWI1 = s e r i e s ( WillR ( o p t i m i z e 27 A s s e t L i s t = ” AssetsZ8 . csv ” ;
23 28
(19.9 ,18 ,49) ) ) ;
24 var StopS = o p t i m i z e ( 3 , 1 , 1 0 ) ∗ ATRS( 29
29 string Names [NN ] ;
optimize (3 ,1 ,10) ) ; 30
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( 31 i f ( r i s i n g ( MMI Smooth ) && aWI1 [ 0 ] > Code
optimize (3 ,1 ,10) ) ; nWI TOP∗−1 && r i s i n g ( aAlma ) &&r i s i n g ( 31 vars Returns [NN ] ;
26
aEma1 ) ) { 32 var Means [NN ] ;
27 i f ( hour ( ) == 1 3 ) { 32 enterLong ( ) ; 33 var C o v a r i a n c e s [NN ] [ NN ] ;
28 Stop=StopL ; 33 } e l s e i f ( f a l l i n g ( MMI Smooth ) && aWI1 [ 0 ] < 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 ; nWI DOWN∗−1 && f a l l i n g ( aAlma )&&f a l l i n g (35 static int OldLots [NN ] ;
30 enterLong ( ) ; aEma1 ) ) { 36
31 } 34 enterShort () ; 37 var TotalCapital = s l i d e r
32
35 } (1 ,1000 ,1000 ,50000 , ” Capital ” , ” Total
33 i f ( hour ( ) == 21 ) { 36
c a p i t a l to d i s t r i b u t e ” ) ;
34 Stop=StopL ; 37
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 ; 38 } Variance f a c t o r ” ) ;
36 enterShort () ; 39
37 } Mean Variance Optimization 40 int N = 0;
38 41 w h i l e ( Names [N] = l o o p ( A s s e t s ) )
42 { Intraday seasonality in FX market Based on this paper 15 i f ( dow ( ) == 5 && NumOpenLong == 0 &&
43 NumPendingLong == 0 && SMA10 [ 0 ] > SMA40
44 i f ( i s (INITRUN) && s t r s t r ( Names [N] , ”#” )== Listing 32: Intraday seasonality in FX market System [0])
NULL) { 16 e n t e r L o n g ( 0 , BuyStop ) ;
45 a s s e t H i s t o r y ( Names [N] , FROM YAHOO) ; 1 var s e s s i o n 1 T Z = WET; 17 e l s e i f ( dow ( ) == 5 && NumOpenShort == 0 &&
46 Symbols [N] = Symbol ; // S t o r e t h e i s i n 2 var session1Start = 8; NumPendingShort == 0 && SMA10 [ 0 ] <
code f o r q u i c k r e f e r e n z e 3 var session1End = 16; SMA40 [ 0 ] )
47 } 4 var s e s s i o n 2 T Z = ET ; 18 enterShort (0 , SellStop ) ;
48
5 var s e s s i o n 2 S t a r t = 1 1 ; // = 16 WET 19
49 i f ( s t r s t r ( Names [N] , ”#” )== NULL && i s ( 6 var session2End = 17; 20 i f ( dow ( ) != 5 && dow ( ) != 6 && dow ( ) != 7 )
7
RUNNING) ) { {
50 a s s e t ( Names [N ] ) ; 8 function tradeIS () 21 exitLong ( ) ;
51 Returns [N] = s e r i e s ( ( p r i c e C l o s e ( 0 )− 9 { 22 exitShort () ;
priceClose (1) )/ priceClose (1) ) ; 10 i f ( dow ( ) >= 1 && dow ( ) <= 5 ) { 23 }
52 } 11 i f ( NumOpenShort == 0 && l h o u r ( s e s s i o n 1 T Z ) == 24
53 i f ( s t r s t r ( Names [N] , ”#” ) != NULL && i s ( session1Start ) 25 }
RUNNING) ) Returns [N] = s e r i e s ( 0 ) ; 12 enterShort () ; 26
54
13 i f ( NumOpenShort > 0 && l h o u r ( s e s s i o n 1 T Z ) >= 27
55 i f (N++ >= NN) b r e a k ; session1End ) 28 f u n c t i o n run ( ) {
56 } 14 exitShort () ; 29
57
15 i f ( NumOpenLong == 0 && l h o u r ( s e s s i o n 2 T Z ) == 30 BarPeriod = 1440;
58 i f ( tdm ( ) == 1 && ! i s (LOOKBACK) ) { session2Start ) 31
59 int i , j ; 16 enterLong ( ) ; 32 w h i l e ( a s s e t ( l o o p ( ”USD/CHF” , ”USD/JPY” , ”
60 f o r ( i =0; i <N; i ++) { 17 i f ( NumOpenLong > 0 && l h o u r ( s e s s i o n 2 T Z ) >= GBP/USD” , ”EUR/USD” ) ) )
61 Means [ i ] = Moment ( Returns [ i ] , LookBack session2End ) 33 tradeOneNightStand ( ) ;
,1) ; 18 exitLong ( ) ; 34
62 f o r ( j =0; j <N; j ++) 19 } 35 }
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 }
21
[ i ] , Returns [ j ] , LookBack ) ;
64 } 22 f u n c t i o n run ( ) Listing 34: Portfolio trading Trend Counter Trend and
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 { Huck Trend
Means , N, WEIGHTCAP) ; 24 StartDate = 2004; 1 // P o r t f o l i o t r a d i n g : Trend , Counter Trend
66 v a r MinVariance = markowitzReturn ( 0 , 0 ) ; 25 UnstablePeriod = 0; and Huck Trend / / / / / / / / / / / / / / / / / / /
67 markowitzReturn ( Weights , MinVariance+ 26 LookBack = 0 ; 2
VFactor / 1 0 0 . ∗ ( B e s t V a r i a n c e −MinVariance27 BarPeriod = 1 ; 3 f u n c t i o n tradeTrend ( )
)); 28 w h i l e ( a s s e t ( l o o p ( ”EUR/USD” ) ) ) 4 {
68
29 tradeIS () ; 5 TimeFrame = 1 ;
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 30 s e t (LOGFILE) ; 6 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ;
a c c o r d i n g t o new w e i g h t s 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
70 f o r ( i =0; i <N; i ++) One Night Stand System (250 ,100 ,1000) ) ) ;
71 i f ( s t r s t r ( Names [ i ] , ”#” )== NULL) { 8 Stop = o p t i m i z e ( 2 , 1 , 1 0 ) ∗ ATR( 1 0 0 ) ;
72 a s s e t ( Names [ i ] ) ; 9
73 MarginCost = p r i c e C l o s e ( ) /LEVERAGE; Listing 33: One Night Stand System 10 i f ( s t r s t r ( Algo , ” : L” ) and v a l l e y ( Trend ) )
74 i n t NewLots = T o t a l C a p i t a l ∗ Weights [ i ] / 1 11 enterLong ( ) ;
MarginCost ; 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 ) )
75 i f ( NewLots > OldLots [ i ] ) 3 13 enterShort () ;
76 e n t e r L o n g ( NewLots−OldLots [ i ] ) ; 4 vars Price = s e r i e s ( price () ) ; 14 }
77 e l s e i f ( NewLots < OldLots [ i ] ) e x i t L o n g 5 v a r s SMA10 = s e r i e s (SMA( P r i c e , 1 0 ) ) ; 15
( 0 , 0 , OldLots [ i ]−NewLots ) ; 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 ( )
78 // p r i n t f ( ” \ n%s − %s : OldLots 7 17 {
: %d NewLots : %d %.0 f $ ” , Names [ i ] , 8 // Stop = 90 ∗ PIP ; 18 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ;
Symbols [ i ] , OldLots [ i ] , NewLots ) ; 9 19 var Threshold = optimize ( 1 . 0 , 0 . 5 , 2 , 0 . 1 ) ;
79 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 (
80 } 11 Price , 3 0 ) ) ;
81 } 12 BuyStop = HH( 1 0 ) + 1∗PIP ; 21 v a r LowPeriod = LowPass ( DomPeriod , 5 0 0 ) ;
82 } 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 ∗
14 optimize (1 ,0.5 ,2) ) ) ;
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 ) ) ; 71 } 18 v a r s H i s t = s e r i e s ( rMACDHist ) ;
24 Stop = o p t i m i z e ( 2 , 1 , 1 0 ) ∗ ATR( 1 0 0 ) ; 72 19
25 73 // p o r t f o l i o l o o p 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 ,
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 74 w h i l e ( a s s e t ( l o o p ( ”EUR/USD” , ”USD/CHF” ) ) ) SignalPeriod ∗2) ;
,− T h r e s h o l d ) ) 75 w h i l e ( a l g o ( l o o p ( ”TRND: L” , ”TRND: S” , ”CNTR: L” , 21 v a r s MainLine30 = s e r i e s (rMACD) ;
27 enterLong ( ) ; ”CNTR: S” , ” HuckTrend : L” , ” HuckTrend : S” ) ) ) 22 v a r s S i g n a l L i n e 3 0 = s e r i e s ( rMACDSignal ) ;
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 ( 76 { 23 v a r s H i s t 3 0 = s e r i e s ( rMACDHist ) ;
Signal , Threshold ) ) 77 // s e t up t h e o p t i m a l margin 24
29 enterShort () ; 78 i f ( Train ) 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 ,
30 } 79 Lots = 1 ; SignalPeriod ∗4) ;
31 80 e l s e i f ( s t r s t r ( Algo , ” : L” ) and 26 v a r s MainLine60 = s e r i e s (rMACD) ;
32 f u n c t i o n HuckTrend ( ) OptimalFLong > 0 ) { 27 v a r s S i g n a l L i n e 6 0 = s e r i e s ( rMACDSignal ) ;
33 { 81 Lots = 1 ; 28 v a r s H i s t 6 0 = s e r i e s ( rMACDHist ) ;
34 82 Margin = clamp ( ( WinLong−LossLong ) ∗ 29
35 TimeFrame = 1 ; OptimalFLong / 2 , 5 0 , 1 0 0 0 0 ) ; 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 ,
36 var ∗ P r i c e = s e r i e s ( p r i c e ( ) ) ; 83 } e l s e i f ( s t r s t r ( Algo , ” : S” ) and SignalPeriod ∗16) ;
37 v a r ∗LP5 = s e r i e s ( LowPass ( P r i c e , 5 ) ) ; OptimalFShort > 0 ) { 31 v a r s MainLine240 = s e r i e s (rMACD) ;
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 84 Lots = 1 ; 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 ) ;
(10 ,6 ,20) ) ) ; 85 Margin = clamp ( ( WinShort−L o s s S h o r t ) ∗ 33 v a r s H i s t 2 4 0 = s e r i e s ( rMACDHist ) ;
39 v a r ∗ RSI10 = s e r i e s ( RSI ( P r i c e , 1 0 ) ) ; OptimalFShort / 2 , 5 0 , 1 0 0 0 0 ) ; 34
40 Stop = o p t i m i z e ( 5 , 1 , 1 0 ) ∗ATR( 3 0 ) ; 86 } else 35
41 int crossed = SkillLong [ 0 ] ; 87 L o t s = 0 ; // s w i t c h o f f t r a d i n g 36
42 i n t Delay = 3 ; 88 37 i f ( c r o s s O v e r ( MainLine , S i g n a l L i n e ) &&
43 89 i f ( s t r s t r ( Algo , ”TRND” ) ) MainLine30 [ 0 ] > S i g n a l L i n e 3 0 [ 0 ]
44 90 tradeTrend ( ) ; 38 && MainLine30 [ 0 ] > S i g n a l L i n e 6 0 [ 0 ] &&
45 i f ( c r o s s O v e r ( LP5 , LP10 ) ) 91 e l s e i f ( s t r s t r ( Algo , ”CNTR” ) ) MainLine240 [ 0 ] > S i g n a l L i n e 2 4 0 [ 0 ] ) {
46 c r o s s e d = Delay ; 92 tradeCounterTrend ( ) ; 39
47 e l s e i f ( c r o s s U n d e r ( LP5 , LP10 ) ) 93 e l s e i f ( s t r s t r ( Algo , ” HuckTrend ” ) ) 40
48 c r o s s e d = −Delay ; 94 HuckTrend ( ) ; 41 exitShort () ;
49 95 42 enterLong ( ) ;
50 i f ( s t r s t r ( Algo , ” : L” ) and ( c r o s s e d > 0 && 96 } 43 }
c r o s s O v e r ( RSI10 , 5 0 ) ) ) { 97 44
51 enterLong ( ) ; 98 PlotWidth = 1 0 0 0 ; 45 i f ( c r o s s U n d e r ( MainLine , S i g n a l L i n e ) &&
52 crossed = 0; 99 PlotHeight1 = 320; MainLine30 [ 0 ] < S i g n a l L i n e 3 0 [ 0 ]
53 } e l s e i f ( s t r s t r ( Algo , ” : S” ) and ( c r o s s e d < 100 } 46 && MainLine30 [ 0 ] < S i g n a l L i n e 6 0 [ 0 ] &&
0 && c r o s s U n d e r ( RSI10 , 5 0 ) ) ) { MainLine240 [ 0 ] < S i g n a l L i n e 2 4 0 [ 0 ] ) {
54 enterShort () ; 47
55 crossed = 0; Listing 35: System 48
56 } else 1 49 exitLong ( ) ;
57 S k i l l L o n g [ 0 ] −= s i g n ( c r o s s e d ) ; 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 () ;
58 } ? ubb=s h o w f l a t&Number=414386& page=1 51 }
59 3 f u n c t i o n run ( ) { 52
60 f u n c t i o n run ( ) 4 53
61 { 5 s e t (LOGFILE) ; 54 }
62 s e t (PARAMETERS+FACTORS+LOGFILE) ; // u s e 6
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 System
7 i n t FastPeriod = 8;
factors 8 i n t SlowPeriod = 2 1 ;
63 B a r P e r i o d = 2 4 0 ; // 4 hour b a r s 9 int SignalPeriod = 9; Listing 36: System
64 LookBack = 5 0 0 ; // needed f o r F i s h e r ( ) 10 1 //
65 NumWFOCycles = 8 ; // a c t i v a t e WFO 11 BarPeriod = 1 5 ; 2 f u n c t i o n run ( )
66 NumBarCycles = 4 ; // 4 t i m e s o v e r s a m p l i n g 12 vars Close = s e r i e s ( priceClose () ) ; 3
67 13 TimeFrame = 1 ; 4
68 i f ( ReTrain ) { 14 5 {
69 UpdateDays = 3 0 ; // r e l o a d new p r i c e 15 MACD( C l o s e , F a s t P e r i o d , SlowPeriod , 6
data from t h e s e r v e r e v e r y 30 days SignalPeriod ) ; 7 StartDate = 20110601;
70 SelectWFO = −1; // s e l e c t t h e l a s t c y c l e 16 v a r s MainLine = s e r i e s (rMACD) ; 8 EndDate = 2 0 1 3 0 1 2 0 ;
f o r re−o p t i m i z a t i o n 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;
10 22 { 69
11 Stop = 50∗ PIP ; 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 ( ) ;
12 T r a i l = 40∗ PIP ; 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 ( ) ;
13 TrailLock = 1; 24 // c u r v e g o e s down and i s below i t s own 72 }
14 lowpass f i l t e r e d value 73
15 25 74 f u n c t i o n CNTR( )
16 vars d a y l o w = s e r i e s ( pri ceL ow ( ) ) ; 26 i f ( Train ) { L o t s = 1 ; r e t u r n ; } // no 75 {
17 vars day high = s e r i e s ( priceHigh ( ) ) ; phantom t r a d e s i n t r a i n i n g mode 76 TimeFrame = 4 ;
18 vars day close = series ( priceClose () ) ; 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 ) ;
19 vars EMA50 = s e r i e s (EMA( d a y c l o s e , 5 0 ) ) ; ProfitOpen ) ; 78 T r a i l = 4∗ATR( 1 0 0 ) ;
20 28 v a r s EquityLP = s e r i e s ( LowPass ( EquityCurve 79
21 ,10) ) ; 80 vars Price = s e r i e s ( price () ) ;
22 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 ,
23 f a l l i n g ( EquityLP ) ) optimize (30 ,20 ,40) ,0.5) ) ;
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 30 L o t s = −1; // drawdown −> phantom t r a d i n g 82 vars Signal = s e r i e s ( Fisher ( Filtered ,500) ) ;
[ 0 ] < EMA50 [ 1 ] ) 31 else 83 var Threshold = optimize ( 1 , 0 . 5 , 1 . 5 , 0 . 1 ) ;
25 enterShort () ; 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
26 33 } 85 checkEquity ( ) ;
27 34 86
28 p l o t ( ”EMA50” , EMA50 [ 0 ] , 0 , RED) ; 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 ) )
29 36 { enterFSLong ( ) ;
30 } 37 i f ( comp == 1 && ! i s (TRAINMODE) ) { 88 e l s e i f ( crossOver ( Signal , Threshold ) )
38 Margin = OptimalFLong ∗ myMargin ∗ s q r t ( 1 enterFSShort ( ) ;
+ max ( 0 , ( WinLong−LossLong ) / myCapital )89 }
Listing 37: System ); 90
1 // B u i l d 0004 39 enterLong ( ) ; 91 f u n c t i o n TRND( )
2 40 } else { 92 {
3 // #d e f i n e ASSETLOOP ”USD/CHF” , ”EUR/USD” , ” 41 Margin=myMargin ; 93 TimeFrame = 1 ;
GBP/USD” , ”USD/CAD” , ”AUD/USD” , ”USD/JPY” ,42 enterLong ( ) ; 94 Stop = o p t i m i z e ( 4 , 2 , 8 ) ∗ ATR( 1 0 0 ) ;
”XAU/USD” , ”XAG/USD” , ”NAS100 ” , ”SPX500 ” ,43 } 95 Trail = 0;
”GER30” , ”US30 ” , ”UK100” //FOREX SET 44 } 96
4 #d e f i n e ASSETLOOP ”EUR/USD” , ”GBP/USD” , ”USD/ 45 97 vars Price = s e r i e s ( price () ) ;
CAD” , ”AUD/USD” , ”USD/JPY” , ”XAU/USD” , ” 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
XAG/USD” // No S t o c k 47 { (500 ,300 ,800) ) ) ;
5 //#d e f i n e ASSETLOOP ”USD/CHF” , ”EUR/USD” , ”USD 48 i f ( comp == 1 && ! i s (TRAINMODE) ) { 99
/JPY” , ”XAU/USD” , ” SPX500”//MIN FOREX SET 49 Margin = OptimalFShort ∗ myMargin ∗ s q r t 100 checkEquity ( ) ;
6 //#d e f i n e ASSETLOOP ”EUR/USD” // t e s t a s s e t ( 1 + max ( 0 , ( WinShort−L o s s S h o r t ) / 101
7 myCapital ) ) ; 102 i f ( v a l l e y ( Trend ) ) enterFSLong ( ) ;
8 //−−−− G l o b a l VAR −−−− 50 enterLong ( ) ; 103 e l s e i f ( peak ( Trend ) ) e n t e r F S S h o r t ( ) ;
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
120 SelectWFO = −1; // s e l e c t t h e l a s t c y c l e Listing 39: First Hour Breakout System 47 {
f o r re−o p t i m i z a t i o n 48
121 r e s e t (FACTORS) ; // don ’ t g e n e r a t e f a c t o r s 1 // Z o r r o v e r s i o n 49 // p r i n t f ( ” \ n e n t h i g h = %4.1 f ; e n t l o w
when re−t r a i n i n g 2 f u n c t i o n run ( ) = %4.1 f ; Range = %4.1 f ; PIP = %4.1 f ” ,
122 } 3 { e n t h i g h [ 0 ] , e n t l o w [ 0 ] , Range [ 0 ] , PIP ) ;
123 NumWFOCycles = 6 ; // a c t i v a t e WFO 4 50 //
124 5 s e t (TICKS+LOGFILE) ; 51
125 setSlider () ; 6 52 i f ( Range [ 0 ] < 40∗ PIP ) // D o n t
126 7 BarPeriod = 3 0 ; t r a d e i f r a n g e i s h i g h e r than 40
127 // p o r t f o l i o l o o p 8 LookBack = 3 ; points
128 w h i l e ( a s s e t ( l o o p (ASSETLOOP) ) ) 9 Hedge = 2 ; 53 {
129 w h i l e ( a l g o ( l o o p ( ”TRND” , ”CNTR” , ”CLSTR” ) ) ) 10 54 // p r i n t f ( ” \ nRange %4.1 f < 40
130 { 11 pips , e n t e r i n g pending t r a d e s at
131 i f ( Algo == ”TRND” ) 12 StartDate = 20100101; e n t h i g h %4.1 f & e n t l o w %4.1 f , p r i c e
132 TRND( ) ; 13 // EndDate = 20100201; %4.1 f ” , Range [ 0 ] , e n t h i g h [ 0 ] , e n t l o w
133 14 [0] , priceClose () ) ;
134 i f ( Algo == ”CNTR” ) 15 a s s e t ( ”UK100” ) ; 55
135 CNTR( ) ; 16 56 EntryTime = 1 5 ; //
136 17 int marketstarthour = 8; Expire pending t r a d e s at 4 : 3 0
137 i f ( Algo == ”CLSTR” ) 18 int marketstartminute = 0; 57
138 CLSTR( ) ; 19 int marketendhour = 16; 58 e n t e r L o n g ( 0 , e n t h i g h [ 0 ] , Range [ 0 ] , Range
139 } 20 int marketendminute = 30; [0]) ;
140 21 59 e n t e r S h o r t ( 0 , e n t l o w [ 0 ] , Range [ 0 ] , Range
141 PlotWidth = 7 0 0 ; 22 vars hi = s e r i e s ( priceHigh () ) ; [0]) ; //Buy on t h e b r e a k o u t o f
142 PlotHeight1 = 400; 23 vars lo = s e r i e s ( pr ice Low ( ) ) ; t h e l o w e s t low o r t h e h i g h e s t h i g h
143 // ColorUp = ColorDn = ColorWin = C o l o r L o s s 24 o f t h a t f i r s t hour
= 0 ; // don ’ t p l o t c a n d l e s and t r a d e s 25 // Find t h e h i g h low r a n g e o f f i r s t h o u r s 60 }
144 // s e t (TESTNOW+PLOTNOW) ; trading 61
145 } 26 vars enthigh = s e r i e s ( MaxVal ( hi , 2 ) ) ; 62 }
27 vars entlow = s e r i e s ( MinVal ( l o , 2 ) ) ; 63
28 v a r s Range = s e r i e s ( enthigh [0] − entlow 64 i f ( ( ( hour ( ) >= marketendhour )
[0]) ; 65 and ( minute ( ) >= marketendminute ) ) and (
Listing 38: Enter a trade when the RSI12 crosses 29 NumOpenTotal > 0 ) )
1 // Z o r r o v e r s i o n 30 66 {
2 // e n t e r a t r a d e when t h e RSI12 c r o s s e s o v e r 31 67 // p r i n t f ( ” \ n E x i t i n g Open Trades ” ) ;
75 o r under 25 32 68 exitLong ( ) ;
3 f u n c t i o n run ( ) 33 69 exitShort () ;
4 { 34 // p r i n t f ( ” \ n%2.0d : % 2 . 0 d High %4.1 f Low 70 }
5 // g e t t h e RSI s e r i e s %4.1 f e n t h i g h %4.1 f e n t l o w %4.1 f r a n g e 71 }
6 vars Close = s e r i e s ( priceClose () ) ; %4.1 f ” , hour ( ) , minute ( ) , h i [ 0 ] , l o [ 0 ] ,
7 v a r s r s i 1 2 = s e r i e s ( RSI ( C l o s e , 1 2 ) ) ; e n t h i g h [ 0 ] , e n t l o w [ 0 ] , Range [ 0 ] ) ;
8 35 // Listing 40: First Hour Breakout System II
9 // s e t up s t o p / p r o f i t levels 36 1 LookBack = 1 0 0 ;
10 Stop = 200∗ PIP ; 37 // i f ( NumPendingTotal == 1 ) // Take 2 s e t (TICKS) ;
11 T a k e P r o f i t = 200∗ PIP ; o n l y one t r a d e p e r day OCO one c a n c e l s 3
12 o t h e r Not s u r e i f t h i s was p a r t o f 4 vars Price = s e r i e s ( price () ) ;
13 // i f r s i c r o s s e s o v e r buy l e v e l , e x i t s t r a t e g y o r not 5
s h o r t and e n t e r l o n g 38 // { 6 StartMarket = 800;
14 i f ( crossOver ( rsi12 ,75) ) 39 // for ( open trades ) 7 EndMarket = 9 0 0 ;
15 reverseLong (1) ; 40 // { 8
16 // i f r s i c r o s s e s below s e l l l e v e l , e x i t 41 // i f ( TradeIsPending ) exitTrade ( 9 v a r DH = 0 , DL = 0 ;
l o n g and e n t e r s h o r t ThisTrade ) ; 10
17 i f ( crossUnder ( rsi12 , 2 5 ) ) 42 // } 11 i f ( l h o u r (UTC) > EndMarket ) {
18 reverseShort (1) ; 43 // } 12 DH = dayHigh (UTC, 0 ) ;
19 } 44 13 DL = dayLow (UTC, 0 ) ;
45 i f ( ( hour ( ) == m a r k e t s t a r t h o u r +1) 14 }
46 and ( minute ( ) == m a r k e t s t a r t m i n u t e ) ) 15
16 var Range = DH−DL ; Trading auxiallary functions 13 Shift a series into the future . . . . . . . . . . . . 2
17 var C l o s e = t i m e O f f s e t (UTC, 0 , 1 6 , 3 0 ) ; 14 Use a function from an external DLL . . . . . . . 3
18 var Open = t i m e O f f s e t (UTC, 0 , 8 , 0 0 ) ; Listing 41: TrailingStop 15 Equity curve trading (skipping trades dependent
19 var S t a r t T r a d e = t i m e O f f s e t (UTC, 0 , 9 , 0 0 ) ; on strategy success) . . . . . . . . . . . . . . . . 3
20 var Now = t i m e O f f s e t (UTC, 0 , 0 , 0 0 ) ; 1 int TrailingStop () 16 Debugging a script . . . . . . . . . . . . . . . . . 3
21
2 { 17 Find out if you have a standard mini or micro lot
22 i f (Now > C l o s e ) 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 account . . . . . . . . . . . . . . . . . . . . . . . 3
23 exitLong ( ) ; in p r o f i t 18 Download historic price data . . . . . . . . . . . 3
24
4 i f ( TradeProfit > 0) 19 Export historic price data to a .csv file . . . . . . 3
25 i f (Now > C l o s e ) 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 20 Print the description of a trade (like ”[AU-
26 exitShort () ; the previous 3 candles D/USD:CY:S1234 . . . . . . . . . . . . . . . . . 3
27
6 TradeStopLimit = max( TradeStopLimit , LL ( 3 ) ) ; 21 Plot equity curves of single assets in a multi-asset
28 a s s e t ( ”UK100” ) ; 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 strategy . . . . . . . . . . . . . . . . . . . . . . . 3
29
v i s i b l e in the chart 22 Set up strategy parameters from a .ini file at the
30 Stop = Range ; 8 p l o t ( ” Stop ” , TradeStopLimit , MINV,BLACK) ; start . . . . . . . . . . . . . . . . . . . . . . . . . 3
31
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 23 Check every minute in Trade mode if an .ini file
32 T a k e P r o f i t = Range ; 10 return 0; was modified . . . . . . . . . . . . . . . . . . . . 3
33
11 } 24 Trade multiple strategies and assets in a single
34 i f ( Range > 40∗ PIP ) script . . . . . . . . . . . . . . . . . . . . . . . . 4
35 Margin = 0 ; Listings 25 Update price history of many assets . . . . . . . 4
1 Chan1ge stops and profit targets of all open long 26 Portfolio strategy with 3 assets and 3 trade func-
trades with the current algo and asset . . . . . . 1 tions . . . . . . . . . . . . . . . . . . . . . . . . . 4
Trademanagement Function 2 Limit the number of open positions // max. 3 27 Plot Triangle above Candle Patterns . . . . . . . 4
TradePriceOpen The ask price when the trade was opened. open long positions per asset/algo . . . . . . . . 1 28 YenTrader System . . . . . . . . . . . . . . . . . 4
If the trade was not yet opened, it’s the current price of 3 Exit all open trades Friday afternoon GMT . . . 1 29 Simple profit system . . . . . . . . . . . . . . . . 5
the asset. 4 Lock 80% profit of all winning trades . . . . . . . 1 30 Profitable System . . . . . . . . . . . . . . . . . . 5
5 Calculate the value of all open trades with the 31 Mean Variance Optimization System . . . . . . . 5
TradePriceClose
current asset . . . . . . . . . . . . . . . . . . . . 1 32 Intraday seasonality in FX market System . . . . 6
TradeUnits 6 Monitoring and modifying a certain trade . . . . 2 33 One Night Stand System . . . . . . . . . . . . . . 6
7 Correlation / heatmap . . . . . . . . . . . . . . . 2 34 Portfolio trading Trend Counter Trend and Huck
TradeRoll
8 Generate an indicator with a different asset time Trend . . . . . . . . . . . . . . . . . . . . . . . . 6
TradeProfit frame and shift . . . . . . . . . . . . . . . . . . . 2 35 System . . . . . . . . . . . . . . . . . . . . . . . . 7
9 Calculate the weekend price change for gap trading 2 36 System . . . . . . . . . . . . . . . . . . . . . . . . 7
TradeEntryLimit
10 Use a series to check if something happened 37 System . . . . . . . . . . . . . . . . . . . . . . . . 8
TradeLots within the last n bars . . . . . . . . . . . . . . . 2 38 Enter a trade when the RSI12 crosses . . . . . . 9
11 Use a loop to check if something happened within 39 First Hour Breakout System . . . . . . . . . . . . 9
TradeIsShort
the last n bars . . . . . . . . . . . . . . . . . . . 2
TradeIsLong 12 Align a time frame to a certain event . . . . . . . 2 Resources

You might also like