[Blotter-commits] r1125 - pkg/quantstrat/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 8 15:31:49 CEST 2012


Author: opentrades
Date: 2012-08-08 15:31:48 +0200 (Wed, 08 Aug 2012)
New Revision: 1125

Added:
   pkg/quantstrat/demo/luxor.1.R
   pkg/quantstrat/demo/luxor.2.R
   pkg/quantstrat/demo/luxor.3.Parameters.R
   pkg/quantstrat/demo/luxor.3.Parameters.tradegraphs.R
   pkg/quantstrat/demo/luxor.3.R
   pkg/quantstrat/demo/luxor.4.R
   pkg/quantstrat/demo/luxor.4.Timespans.R
   pkg/quantstrat/demo/luxor.4.Timespans.tradegraphs.R
Removed:
   pkg/quantstrat/demo/luxor/
Modified:
   pkg/quantstrat/demo/00Index
   pkg/quantstrat/demo/Rplots.pdf
Log:
moved the luxor demo's back into the demo dir and updated 00Index



Modified: pkg/quantstrat/demo/00Index
===================================================================
--- pkg/quantstrat/demo/00Index	2012-08-08 12:07:15 UTC (rev 1124)
+++ pkg/quantstrat/demo/00Index	2012-08-08 13:31:48 UTC (rev 1125)
@@ -7,4 +7,11 @@
 rsi        Relative Strength Index (RSI) strategy demonstrating long-only threshold actions
 bbandParameters  example of parameter test on bbands demo strategy
 macdParameters  example of parameter test on macd demo strategy
-luxor subdirectory - several demos using R to implement and analyze luxor strategy from the Jaekle&Tomasini book
+luxor.1.R  Jaekle&Tomasini: Paragraph 3.2: luxor without any optimizations
+luxor.2.R  Jaekle&Tomasini: Paragraph 3.2: luxor with $30 slippage and transaction costs
+luxor.3.R  Jaekle&Tomasini: Paragraph 3.3 Variation of the input parameters: optimisation and stability diagrams
+luxor.3.Parameters.R  Jaekle&Tomasini: Paragraph 3.3 Variation of the input parameters: optimisation and stability diagrams / scan
+luxor.3.Parameters.tradeGraphs.R  Jaekle&Tomasini: 3D graphs of results for sma scan
+luxor.4.R  Jaekle&Tomasini: Paragraph 3.4: inserting an intraday time filter
+luxor.4.Timespans.R  Jaekle&Tomasini: Paragraph 3.4: inserting an intraday time filter / scan
+luxor.4.Timespan.tradeGraphs.R  Jaekle&Tomasini: 3D graphs of results for timespan scan

Modified: pkg/quantstrat/demo/Rplots.pdf
===================================================================
(Binary files differ)

Copied: pkg/quantstrat/demo/luxor.1.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.1.R)
===================================================================
--- pkg/quantstrat/demo/luxor.1.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.1.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,177 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# 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
+
+options(width = 240)
+#Sys.setenv(TZ="GMT")
+
+.fast = 10
+.slow = 30
+
+.qty=100000
+.th=0.0005
+.txn=0
+
+initDate = '2002-10-21'
+.from='2002-10-21'
+#.to='2008-07-04'
+#.to='2003-12-31'
+.to='2002-10-31'
+
+####
+
+s = 'luxor'
+p = 'forex'
+a = 'IB1'
+
+###
+
+require(quantstrat)
+
+currency(c('GBP', 'USD'))
+
+exchange_rate(c('GBPUSD'), tick_size=0.0001)
+
+#setSymbolLookup.FI('~/R.symbols/', 'GBPUSD')
+setSymbolLookup.FI('../data/', '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 = .fast
+	),
+	label="nFast"
+)
+
+add.indicator(s, name="SMA",
+	arguments = list(
+		x = quote(Cl(mktdata)),
+		n = .slow
+	),
+	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')
+

Copied: pkg/quantstrat/demo/luxor.2.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.2.R)
===================================================================
--- pkg/quantstrat/demo/luxor.2.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.2.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,177 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.2: luxor with $30 slippage and transaction costs
+
+options(width = 240)
+#Sys.setenv(TZ="GMT")
+
+.fast = 10
+.slow = 30
+
+.qty=100000
+.th=0.0005
+.txn=-30
+
+initDate = '2002-10-21'
+.from='2002-10-21'
+#.to='2008-07-04'
+#.to='2003-12-31'
+.to='2002-10-31'
+
+####
+
+s = 'luxor'
+p = 'forex'
+a = 'IB1'
+
+###
+
+require(quantstrat)
+
+currency(c('GBP', 'USD'))
+
+exchange_rate(c('GBPUSD'), tick_size=0.0001)
+
+#setSymbolLookup.FI('~/R.symbols/', 'GBPUSD')
+setSymbolLookup.FI('../data/', '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 = .fast
+	),
+	label="nFast"
+)
+
+add.indicator(s, name="SMA",
+	arguments = list(
+		x = quote(Cl(mktdata)),
+		n = .slow
+	),
+	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')
+

Copied: pkg/quantstrat/demo/luxor.3.Parameters.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.3.Parameters.R)
===================================================================
--- pkg/quantstrat/demo/luxor.3.Parameters.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.3.Parameters.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,80 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.3 Variation of the input parameters: optimisation and stability diagrams
+#    uses luxor.3.R
+
+#verbose = 0
+verbose = 1
+
+method='expand'
+#method='random'
+#.sampleSize=20
+
+#.fastRange=(1:20)
+#.slowRange=(21:80)
+
+.fastRange=(1:5)
+.slowRange=(41:45)
+
+###############################################################################
+
+#require(foreach,quietly=TRUE)
+require(doMC)
+require(quantstrat)
+
+portfolio.st = 'forex'
+
+source('luxor.3.R')
+
+s<-getStrategy('luxor')
+
+parameterTable<-getParameterTable(s)
+#parameterTable
+
+tPD2<-setParameterDistribution(type = 'indicator', indexnum = 1, distribution = list(nFast = .fastRange), label = 'nFast')
+tPD2<-setParameterDistribution(tPD2, type = 'indicator', indexnum = 2, distribution = list(nSlow = .slowRange), label = 'nSlow')
+
+pConstraint2<-setParameterConstraint(constraintLabel='luxorPC',paramList=c('nFast','nSlow'),relationship='lt')
+
+registerDoMC(cores=2)
+
+if(method == 'random')
+{
+	laststpar.rnd<-system.time(
+			scan.results<-applyParameter(
+				strategy=s,
+				portfolios=portfolio.st,
+				parameterPool=tPD2,
+				method='random',
+				sampleSize=.sampleSize,
+				parameterConstraints=pConstraint2
+			)
+	)
+	if(verbose >=1) laststpar.rnd
+}
+
+if(method == 'expand')
+{
+	laststpar.exp<-system.time(
+			scan.results<-applyParameter(
+				strategy=s,
+				portfolios=portfolio.st,
+				parameterPool=tPD2,
+				method='expand',
+				parameterConstraints=pConstraint2
+			)
+	)
+	if(verbose >=1) print(laststpar.exp)
+}
+
+#examine the stats from this parameter run:
+if(verbose >=1) print(scan.results$statsTable)
+
+stats <- scan.results$statsTable
+save(stats, file="luxor.parameters.RData")

Copied: pkg/quantstrat/demo/luxor.3.Parameters.tradegraphs.R (from rev 1124, pkg/quantstrat/demo/luxor/luxorParameterTradeGraphs.R)
===================================================================
--- pkg/quantstrat/demo/luxor.3.Parameters.tradegraphs.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.3.Parameters.tradegraphs.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,14 @@
+#!/usr/bin/Rscript --vanilla
+
+require(quantstrat)
+
+load('../data/luxor.parameters.1-10.30-55.RData')
+
+tradeGraphs (
+	stats = stats,
+	free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
+	fixed.params = NULL,
+	statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades", "Profit.Factor"),
+	title = 'Luxor SMA Parameter Scan'
+)
+

Copied: pkg/quantstrat/demo/luxor.3.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.3.R)
===================================================================
--- pkg/quantstrat/demo/luxor.3.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.3.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,178 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.3 Variation of the input parameters: optimisation and stability diagrams
+
+options(width = 240)
+#Sys.setenv(TZ="GMT")
+
+.fast = 1
+.slow = 44
+
+.qty=100000
+.th=0.0005
+.txn=-30
+
+initDate = '2002-10-21'
+.from='2002-10-21'
+#.to='2008-07-04'
+#.to='2003-12-31'
+.to='2002-10-31'
+#.to='2002-12-31'
+
+####
+
+s = 'luxor'
+p = 'forex'
+a = 'IB1'
+
+###
+
+require(quantstrat)
+
+currency(c('GBP', 'USD'))
+
+exchange_rate(c('GBPUSD'), tick_size=0.0001)
+
+#setSymbolLookup.FI('~/R.symbols/', 'GBPUSD')
+setSymbolLookup.FI('../data/', '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 = .fast
+	),
+	label="nFast"
+)
+
+add.indicator(s, name="SMA",
+	arguments = list(
+		x = quote(Cl(mktdata)),
+		n = .slow
+	),
+	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')
+

Copied: pkg/quantstrat/demo/luxor.4.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.4.R)
===================================================================
--- pkg/quantstrat/demo/luxor.4.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.4.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,195 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.4: inserting an intraday time filter
+
+options(width = 240)
+#Sys.setenv(TZ="GMT")
+
+.fast = 1
+.slow = 44
+
+.qty=100000
+.th=0.0005
+.txn=-30
+.timespan = 'T08:00/T12:00'
+
+initDate = '2002-10-21'
+.from='2002-10-21'
+#.to='2008-07-04'
+#.to='2003-12-31'
+.to='2002-10-31'
+#.to='2002-12-31'
+#.from='2006-01-01'
+#.to='2006-12-31'
+#.from='2007-01-01'
+#.to='2007-12-31'
+
+####
+
+s = 'luxor'
+p = 'forex'
+a = 'IB1'
+
+###
+
+require(quantstrat)
+
+currency(c('GBP', 'USD'))
+
+exchange_rate(c('GBPUSD'), tick_size=0.0001)
+
+#setSymbolLookup.FI('~/R.symbols/', 'GBPUSD')
+setSymbolLookup.FI('../data/', '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 ######################################################################
+
+addPosLimit(
+            portfolio=p,
+            symbol='GBPUSD',
+            timestamp=initDate,
+            maxpos=.qty)
+
+strategy(s, store=TRUE)
+
+### indicators
+
+add.indicator(s, name = "SMA",
+	arguments = list(
+		x = quote(Cl(mktdata)),
+		n = .fast
+	),
+	label="nFast"
+)
+
+add.indicator(s, name="SMA",
+	arguments = list(
+		x = quote(Cl(mktdata)),
+		n = .slow
+	),
+	label="nSlow"
+)
+
+### signals
+
+add.signal(s, name = 'sigCrossover',
+	arguments = list(
+		columns=c("nFast","nSlow"),
+		relationship="gte"
+	),
+	label='long'
+)
+
+add.signal(s, name = 'sigCrossover',
+	arguments = list(
+		columns=c("nFast","nSlow"),
+		relationship="lt"
+	),
+	label='short'
+)
+
+### rules
+
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='long' , sigval=TRUE,
+		replace=TRUE,
+		orderside='short',
+		ordertype='market',
+		TxnFees=.txn,
+		orderqty='all',
+		orderset='ocoshort'
+	),
+	type='exit',
+	timespan = .timespan,
+	label='Exit2LONG'
+)
+
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='short', sigval=TRUE,
+		replace=TRUE,
+		orderside='long' ,
+		ordertype='market',
+		TxnFees=.txn,
+		orderqty='all',
+		orderset='ocolong'
+	),
+	type='exit',
+	timespan = .timespan,
+	label='Exit2SHORT')
+
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='long' , sigval=TRUE,
+		replace=FALSE,
+		orderside='long' ,
+		ordertype='stoplimit',
+		prefer='High',
+		threshold=.th,
+		TxnFees=0,
+		orderqty=+.qty,
+		osFUN=osMaxPos,
+		orderset='ocolong'
+	),
+	type='enter',
+	timespan = .timespan,
+	label='EnterLONG'
+)
+
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='short', sigval=TRUE,
+		replace=FALSE,
+		orderside='short',
+		ordertype='stoplimit',
+		prefer='Low',
+		threshold=-.th,
+		TxnFees=0,
+		orderqty=-.qty,
+		osFUN=osMaxPos,
+		orderset='ocoshort'
+	),
+	type='enter',
+	timespan = .timespan,
+	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')
+
+print(tradeStats(p, 'GBPUSD'))
+

Copied: pkg/quantstrat/demo/luxor.4.Timespans.R (from rev 1124, pkg/quantstrat/demo/luxor/luxor.4.Timespans.R)
===================================================================
--- pkg/quantstrat/demo/luxor.4.Timespans.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.4.Timespans.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,79 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012
+#
+# Tested and found to work correctly using blotter r1123
+#
+# From Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.4: inserting an intraday time filter
+#    uses luxor.4.R
+
+source('luxor.4.R')
+
+#verbose = 0
+verbose = 1
+
+method='expand'
+#method='random'
+#.sampleSize=20
+
+# generate 24x24h ISO8601 timespan vector
+
+.timespans.start<-paste(sprintf("T%02d",0:23),':00',sep='')
+.timespans.stop<-paste(sprintf("T%02d",0:23),':59',sep='')
+
+.timespans<-outer(.timespans.start, .timespans.stop, FUN=paste, sep='/')
+
+# in order to run the full 24x24 hour scan above, comment out the following line:
+.timespans<-c('T06:00/T10:00', 'T07:00/T11:00', 'T08:00/T12:00', 'T09:00/T13:00', 'T10:00/T14:00', 'T11:00/T15:00', 'T12:00/T16:00')
+
+###############################################################################
+
+require(doMC)
+require(quantstrat)
+
+portfolio.st = 'forex'
+
+s<-getStrategy('luxor')
+
+parameterTable<-getParameterTable(s)
+
+tPD2<-setParameterDistribution(type = 'enter', indexnum = 1, distribution = list(timespan = .timespans), label = 'Timespan')
+
+registerDoMC(cores=2)
+
+if(method == 'random')
+{
+	laststpar.rnd<-system.time(
+			scan.results<-applyParameter(
+				strategy=s,
+				portfolios=portfolio.st,
+				parameterPool=tPD2,
+				method='random',
+				sampleSize=.sampleSize,
+				parameterConstraints=NULL
+			)
+	)
+	if(verbose >=1) laststpar.rnd
+}
+
+if(method == 'expand')
+{
+	laststpar.exp<-system.time(
+			scan.results<-applyParameter(
+				strategy=s,
+				portfolios=portfolio.st,
+				parameterPool=tPD2,
+				method='expand',
+				parameterConstraints=NULL
+			)
+	)
+	if(verbose >=1) print(laststpar.exp)
+}
+
+#examine the stats from this parameter run:
+if(verbose >=1) print(scan.results$statsTable)
+
+stats <- scan.results$statsTable
+save(stats, file="luxor.timespan.RData")

Copied: pkg/quantstrat/demo/luxor.4.Timespans.tradegraphs.R (from rev 1124, pkg/quantstrat/demo/luxor/luxorTimespanTradeGraphs.R)
===================================================================
--- pkg/quantstrat/demo/luxor.4.Timespans.tradegraphs.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.4.Timespans.tradegraphs.R	2012-08-08 13:31:48 UTC (rev 1125)
@@ -0,0 +1,27 @@
+#!/usr/bin/Rscript --vanilla
+
+require(quantstrat)
+
+load('../data/luxor.timespan.24x24.2002-2008.RData')
+
+names(stats)[names(stats)=='testPackListPRL[[k]]$parameters']<-'timespan'
+
+stats$tmp = strsplit(as.character(stats$timespan),'/')
+
+stats$from<-sapply(stats$tmp,FUN='[',1)
+stats$to<-sapply(stats$tmp,FUN='[',2)
+
+stats$start<-as.numeric(gsub('T([0-9]+):[0-9]+',x=stats$from,'\\1'))
+stats$stop<-(as.numeric(gsub('T([0-9]+):[0-9]+',x=stats$to,'\\1'))+1)%%24
+
+# trading data is in EST (GMT-4): move 4 hours to adjust to GMT
+#stats$start<-(stats$start+4)%%24
+#stats$stop<-(stats$stop+4)%%24
+
+tradeGraphs(
+	stats,
+	free.params=c('start','stop'),
+	statistics=c('Net.Trading.PL','maxDrawdown',"Avg.Trade.PL",'Num.Trades',"Profit.Factor"),
+	title = 'Luxor Intraday TimeWindow Scan'
+)
+



More information about the Blotter-commits mailing list