[Blotter-commits] r1098 - pkg/quantstrat/demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 3 19:45:36 CEST 2012
Author: opentrades
Date: 2012-07-03 19:45:36 +0200 (Tue, 03 Jul 2012)
New Revision: 1098
Added:
pkg/quantstrat/demo/luxor.R
pkg/quantstrat/demo/luxorParameters.R
Log:
* small changes for luxor.R
* first commit for luxorParameters.R
Added: pkg/quantstrat/demo/luxor.R
===================================================================
--- pkg/quantstrat/demo/luxor.R (rev 0)
+++ pkg/quantstrat/demo/luxor.R 2012-07-03 17:45:36 UTC (rev 1098)
@@ -0,0 +1,175 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - June 2012
+#
+# Tested and found to work correctly using blotter r1082
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.2: luxor without any optimizations, but with $30 tnx costs + slippage
+
+.qty=100000
+.th=0.0005
+.txn=-30
+#.txn=0
+
+initDate = '2002-10-21'
+.from='2002-10-21'
+#.to='2008-07-04'
+.to='2002-10-31'
+#.to='2003-12-31'
+
+####
+
+s = 'luxor'
+p = 'forex'
+a = 'IB1'
+
+options(width = 240)
+#Sys.setenv(TZ="GMT")
+
+###
+
+require(quantstrat)
+
+currency(c('GBP', 'USD'))
+
+exchange_rate(c('GBPUSD'), tick_size=0.0001)
+
+#setSymbolLookup.FI('~/R.symbols/', 'GBPUSD')
+setSymbolLookup.FI('../sandbox/', 'GBPUSD')
+
+###
+
+getSymbols('GBPUSD', from=.from, to=.to, verbose=FALSE)
+GBPUSD = to.minutes30(GBPUSD)
+GBPUSD = align.time(to.minutes30(GBPUSD), 1800)
+
+###
+
+initPortf(p, symbols='GBPUSD', initDate=initDate, currency='USD')
+initAcct(a, portfolios=p, initDate=initDate, currency='USD')
+
+###
+
+initOrders(p, initDate=initDate)
+
+### strategy ######################################################################
+
+strategy(s, store=TRUE)
+
+### indicators
+
+add.indicator(s, name = "SMA",
+ arguments = list(
+ x = quote(Cl(mktdata)),
+ n = 10
+ ),
+ label="nFast"
+)
+
+add.indicator(s, name="SMA",
+ arguments = list(
+ x = quote(Cl(mktdata)),
+ n = 30
+ ),
+ label="nSlow"
+)
+
+### signals
+
+add.signal(s, 'sigCrossover',
+ arguments = list(
+ columns=c("nFast","nSlow"),
+ relationship="gte"
+ ),
+ label='long'
+)
+
+add.signal(s, 'sigCrossover',
+ arguments = list(
+ columns=c("nFast","nSlow"),
+ relationship="lt"
+ ),
+ label='short'
+)
+
+### rules
+
+add.rule(s, 'ruleSignal',
+ arguments=list(sigcol='long' , sigval=TRUE,
+ replace=TRUE,
+ orderside='short',
+ ordertype='market',
+ TxnFees=.txn,
+ orderqty='all',
+ orderset='ocoshort'
+ ),
+ type='exit',
+ label='Exit2LONG'
+)
+
+add.rule(s, 'ruleSignal',
+ arguments=list(sigcol='short', sigval=TRUE,
+ replace=TRUE,
+ orderside='long' ,
+ ordertype='market',
+ TxnFees=.txn,
+ orderqty='all',
+ orderset='ocolong'
+ ),
+ type='exit',
+ label='Exit2SHORT')
+
+add.rule(s, 'ruleSignal',
+ arguments=list(sigcol='long' , sigval=TRUE,
+ replace=FALSE,
+ orderside='long' ,
+ ordertype='stoplimit',
+ prefer='High',
+ threshold=.th,
+ TxnFees=0,
+ orderqty=+.qty,
+ orderset='ocolong'
+ ),
+ type='enter',
+ label='EnterLONG'
+)
+
+add.rule(s, 'ruleSignal',
+ arguments=list(sigcol='short', sigval=TRUE,
+ replace=FALSE,
+ orderside='short',
+ ordertype='stoplimit',
+ prefer='Low',
+ threshold=-.th,
+ TxnFees=0,
+ orderqty=-.qty,
+ orderset='ocoshort'
+ ),
+ type='enter',
+ label='EnterSHORT'
+)
+
+#
+
+###############################################################################
+
+#applyStrategy(s, p, verbose = FALSE)
+applyStrategy(s, p, prefer='Open', verbose = FALSE)
+
+updatePortf(p, Symbols='GBPUSD', ,Dates=paste('::',as.Date(Sys.time()),sep=''))
+
+###############################################################################
+
+chart.Posn(p, "GBPUSD")
+
+print(getOrderBook(p))
+
+txns <- getTxns(p, 'GBPUSD')
+txns
+##txns$Net
+cat('Net profit:', sum(txns$Net.Txn.Realized.PL), '\n')
+
+tradeStats(p, 'GBPUSD')
+
Property changes on: pkg/quantstrat/demo/luxor.R
___________________________________________________________________
Added: svn:executable
+ *
Added: pkg/quantstrat/demo/luxorParameters.R
===================================================================
--- pkg/quantstrat/demo/luxorParameters.R (rev 0)
+++ pkg/quantstrat/demo/luxorParameters.R 2012-07-03 17:45:36 UTC (rev 1098)
@@ -0,0 +1,41 @@
+#!/usr/bin/Rscript --vanilla
+
+#verbose = 0
+verbose = 1
+
+#require(foreach,quietly=TRUE)
+require(doMC)
+
+require(quantstrat)
+
+portfolio.st = 'forex'
+
+source('luxor.R')
+
+s<-getStrategy('luxor')
+
+parameterTable<-getParameterTable(s)
+#parameterTable
+
+tPD2<-setParameterDistribution(type = 'indicator', indexnum = 1, distribution = list(nFast = (1:20)), label = 'nFast')
+tPD2<-setParameterDistribution(tPD2, type = 'indicator', indexnum = 2, distribution = list(nSlow = (21:50)), label = 'nSlow')
+
+pConstraint2<-setParameterConstraint(constraintLabel='luxorPC',paramList=c('nFast','nSlow'),relationship='lt')
+
+registerDoMC(cores=2)
+
+# Please run either random mode or expand mode, since the latter run will overwrite the objects in .blotter.
+laststpar.rnd<-system.time(
+ testPackListPL<-applyParameter(strategy=s,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=20,parameterConstraints=pConstraint2)
+)
+if(verbose >=1) laststpar.rnd
+
+# Please run either random mode or expand mode, since the latter run will overwrite the objects in .blotter.
+#laststpar.exp<-system.time(
+# testPackListPL<-applyParameter(strategy=s,portfolios=portfolio.st,parameterPool=tPD2,method='expand',sampleSize=20,parameterConstrains=pConstraint2)
+#)
+#if(verbose >=1) print(laststpar.exp)
+
+#examine the stats from this parameter run:
+if(verbose >=1) print(testPackListPL$statsTable)
+
Property changes on: pkg/quantstrat/demo/luxorParameters.R
___________________________________________________________________
Added: svn:executable
+ *
More information about the Blotter-commits
mailing list