[Blotter-commits] r1437 - pkg/quantstrat/demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Apr 18 22:08:22 CEST 2013
Author: opentrades
Date: 2013-04-18 22:08:22 +0200 (Thu, 18 Apr 2013)
New Revision: 1437
Added:
pkg/quantstrat/demo/luxor.1.strategy.R
pkg/quantstrat/demo/luxor.2.add.paramsets.R
Removed:
pkg/quantstrat/demo/luxor.strategy.R
Modified:
pkg/quantstrat/demo/luxor.3.Parameters.R
Log:
- renamed luxor.strategy.R to luxor.1.strategy.R
- introduced luxor.2.add.paramsets.R
Copied: pkg/quantstrat/demo/luxor.1.strategy.R (from rev 1436, pkg/quantstrat/demo/luxor.strategy.R)
===================================================================
--- pkg/quantstrat/demo/luxor.1.strategy.R (rev 0)
+++ pkg/quantstrat/demo/luxor.1.strategy.R 2013-04-18 20:08:22 UTC (rev 1437)
@@ -0,0 +1,198 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012, revised April 2013
+#
+# Tested and found to work correctly using blotter r1420
+#
+# 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="UTC")
+
+###
+
+.fast = 10
+.slow = 30
+
+.orderqty = 100000
+.threshold = 0.0005
+.txn.fees = -30
+
+##### PLACE DEMO AND TEST DATES HERE #################
+#
+#if(isTRUE(options('in_test')$in_test))
+# # use test dates
+# {initDate="2011-01-01"
+# endDate="2012-12-31"
+# } else
+# # use demo defaults
+# {initDate="1999-12-31"
+# endDate=Sys.Date()}
+
+initDate = '2002-10-21'
+
+.from='2002-10-21'
+#.to='2008-07-04'
+.to='2002-10-31'
+
+####
+
+strategy.st = 'luxor'
+portfolio.st = 'forex'
+account.st = 'IB1'
+
+### packages
+#
+# quantstrat package will pull in some other packages:
+# FinancialInstrument, quantmod, blotter, xts
+
+require(quantstrat)
+
+### FinancialInstrument
+
+currency(c('GBP', 'USD'))
+
+exchange_rate('GBPUSD', tick_size=0.0001)
+
+### quantmod
+
+getSymbols.FI(Symbols='GBPUSD',
+ dir=system.file('extdata',package='quantstrat'),
+ from=.from, to=.to
+)
+
+# ALTERNATIVE WAY TO FETCH SYMBOL DATA
+#setSymbolLookup.FI(system.file('extdata',package='quantstrat'), 'GBPUSD')
+#getSymbols('GBPUSD', from=.from, to=.to, verbose=FALSE)
+
+### xts
+
+GBPUSD = to.minutes30(GBPUSD)
+GBPUSD = align.time(to.minutes30(GBPUSD), 1800)
+
+### blotter
+
+initPortf(portfolio.st, symbols='GBPUSD', initDate=initDate, currency='USD')
+initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD')
+
+### quantstrat
+
+initOrders(portfolio.st, initDate=initDate)
+
+### define strategy
+
+strategy(strategy.st, store=TRUE)
+
+### indicators
+
+add.indicator(strategy.st, name = "SMA",
+ arguments = list(
+ x = quote(Cl(mktdata)[,1]),
+ n = .fast
+ ),
+ label="nFast"
+)
+
+add.indicator(strategy.st, name="SMA",
+ arguments = list(
+ x = quote(Cl(mktdata)[,1]),
+ n = .slow
+ ),
+ label="nSlow"
+)
+
+### signals
+
+add.signal(strategy.st, name='sigCrossover',
+ arguments = list(
+ columns=c("nFast","nSlow"),
+ relationship="gte"
+ ),
+ label='long'
+)
+
+add.signal(strategy.st, name='sigCrossover',
+ arguments = list(
+ columns=c("nFast","nSlow"),
+ relationship="lt"
+ ),
+ label='short'
+)
+
+### rules
+
+add.rule(strategy.st, name='ruleSignal',
+ arguments=list(sigcol='long' , sigval=TRUE,
+ orderside='short',
+ ordertype='market',
+ orderqty='all',
+ TxnFees=.txn.fees,
+ replace=TRUE
+ ),
+ type='exit',
+ label='Exit2LONG'
+)
+
+add.rule(strategy.st, name='ruleSignal',
+ arguments=list(sigcol='short', sigval=TRUE,
+ orderside='long' ,
+ ordertype='market',
+ orderqty='all',
+ TxnFees=.txn.fees,
+ replace=TRUE
+ ),
+ type='exit',
+ label='Exit2SHORT'
+)
+
+add.rule(strategy.st, name='ruleSignal',
+ arguments=list(sigcol='long' , sigval=TRUE,
+ orderside='long' ,
+ ordertype='stoplimit', prefer='High', threshold=.threshold,
+ orderqty=+.orderqty,
+ replace=FALSE
+ ),
+ type='enter',
+ label='EnterLONG'
+)
+
+add.rule(strategy.st, name='ruleSignal',
+ arguments=list(sigcol='short', sigval=TRUE,
+ orderside='short',
+ ordertype='stoplimit', prefer='Low', threshold=-.threshold,
+ orderqty=-.orderqty,
+ replace=FALSE
+ ),
+ type='enter',
+ label='EnterSHORT'
+)
+
+###############################################################################
+
+applyStrategy(strategy.st, portfolio.st)
+
+View(getOrderBook(portfolio.st)[[portfolio.st]]$GBPUSD)
+
+###############################################################################
+
+updatePortf(portfolio.st, Symbols='GBPUSD', Dates=paste('::',as.Date(Sys.time()),sep=''))
+
+chart.Posn(portfolio.st, "GBPUSD")
+
+###############################################################################
+
+View(tradeStats(portfolio.st, 'GBPUSD'))
+
+###############################################################################
+
+# save the strategy in an .RData object for later retrieval
+
+save.strategy(strategy.st)
+
+##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
+# book = getOrderBook(port)
+# stats = tradeStats(port)
+# rets = PortfReturns(acct)
+################################################################
Added: pkg/quantstrat/demo/luxor.2.add.paramsets.R
===================================================================
--- pkg/quantstrat/demo/luxor.2.add.paramsets.R (rev 0)
+++ pkg/quantstrat/demo/luxor.2.add.paramsets.R 2013-04-18 20:08:22 UTC (rev 1437)
@@ -0,0 +1,147 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - August 2012, revised April 2013
+#
+# Tested and found to work correctly using blotter r1420
+
+require(quantstrat)
+
+##### PLACE DEMO AND TEST DATES HERE #################
+#
+#if(isTRUE(options('in_test')$in_test))
+# # use test dates
+# {initDate="2011-01-01"
+# endDate="2012-12-31"
+# } else
+# # use demo defaults
+# {initDate="1999-12-31"
+# endDate=Sys.Date()}
+
+###
+
+.FastSMA = (1:20)
+.SlowSMA = (30:80)
+
+.StopLoss = seq(0.1, 2.0, length.out=20)/100
+.StopTrailing = seq(0.1, 2.0, length.out=20)/100
+.TakeProfit = seq(0.1, 2.0, length.out=20)/100
+
+strategy.st <- 'luxor'
+
+###
+
+load.strategy(strategy.st)
+
+### SMA paramset
+
+add.distribution(strategy.st,
+ paramset.label = 'SMA',
+ component.type = 'indicator',
+ component.label = 'nFast',
+ variable = list(n = .FastSMA),
+ label = 'nFAST'
+)
+
+add.distribution(strategy.st,
+ paramset.label = 'SMA',
+ component.type = 'indicator',
+ component.label = 'nSlow',
+ variable = list(n = .SlowSMA),
+ label = 'nSLOW'
+)
+
+add.constraint(strategy.st,
+ paramset.label = 'SMA',
+ distribution.label.1 = 'nFAST',
+ distribution.label.2 = 'nSLOW',
+ operator = '<',
+ label = 'SMA'
+)
+
+### Stop Loss paramset
+
+add.distribution(strategy.st,
+ paramset.label = 'StopLoss',
+ component.type = 'chain',
+ component.label = 'StopLossLONG',
+ variable = list(threshold = .StopLoss),
+ label = 'StopLossLONG'
+)
+
+add.distribution(strategy.st,
+ paramset.label = 'StopLoss',
+ component.type = 'chain',
+ component.label = 'StopLossSHORT',
+ variable = list(threshold = .StopLoss),
+ label = 'StopLossSHORT'
+)
+
+add.constraint(strategy.st,
+ paramset.label = 'StopLoss',
+ distribution.label.1 = 'StopLossLONG',
+ distribution.label.2 = 'StopLossSHORT',
+ operator = '==',
+ label = 'StopLoss'
+)
+
+### Stop Trailing paramset
+
+add.distribution(strategy.st,
+ paramset.label = 'StopTrailing',
+ component.type = 'chain',
+ component.label = 'StopTrailingLONG',
+ variable = list(threshold = .StopTrailing),
+ label = 'StopTrailingLONG'
+)
+
+add.distribution(strategy.st,
+ paramset.label = 'StopTrailing',
+ component.type = 'chain',
+ component.label = 'StopTrailingSHORT',
+ variable = list(threshold = .StopTrailing),
+ label = 'StopTrailingSHORT'
+)
+
+add.constraint(strategy.st,
+ paramset.label = 'StopTrailing',
+ distribution.label.1 = 'StopTrailingLONG',
+ distribution.label.2 = 'StopTrailingSHORT',
+ operator = '==',
+ label = 'StopTrailing'
+)
+
+### Take Profit paramset
+
+add.distribution(strategy.st,
+ paramset.label = 'TakeProfit',
+ component.type = 'chain',
+ component.label = 'TakeProfitLONG',
+ variable = list(threshold = .TakeProfit),
+ label = 'TakeProfitLONG'
+)
+
+add.distribution(strategy.st,
+ paramset.label = 'TakeProfit',
+ component.type = 'chain',
+ component.label = 'TakeProfitSHORT',
+ variable = list(threshold = .TakeProfit),
+ label = 'TakeProfitSHORT'
+)
+
+add.constraint(strategy.st,
+ paramset.label = 'TakeProfit',
+ distribution.label.1 = 'TakeProfitLONG',
+ distribution.label.2 = 'TakeProfitSHORT',
+ operator = '==',
+ label = 'TakeProfit'
+)
+
+###
+
+save.strategy(strategy.st)
+
+##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
+# book = getOrderBook(port)
+# stats = tradeStats(port)
+# rets = PortfReturns(acct)
+################################################################
Property changes on: pkg/quantstrat/demo/luxor.2.add.paramsets.R
___________________________________________________________________
Added: svn:executable
+ *
Modified: pkg/quantstrat/demo/luxor.3.Parameters.R
===================================================================
--- pkg/quantstrat/demo/luxor.3.Parameters.R 2013-04-17 11:01:33 UTC (rev 1436)
+++ pkg/quantstrat/demo/luxor.3.Parameters.R 2013-04-18 20:08:22 UTC (rev 1437)
@@ -25,7 +25,7 @@
###############################################################################
require(foreach,quietly=TRUE)
-require(doMC)
+#require(doMC)
# example parallel initialization for doParallel. this or doMC, or doRedis are
# most probably preferable to doSMP
#require(doParallel)
Deleted: pkg/quantstrat/demo/luxor.strategy.R
===================================================================
--- pkg/quantstrat/demo/luxor.strategy.R 2013-04-17 11:01:33 UTC (rev 1436)
+++ pkg/quantstrat/demo/luxor.strategy.R 2013-04-18 20:08:22 UTC (rev 1437)
@@ -1,198 +0,0 @@
-#!/usr/bin/Rscript --vanilla
-#
-# Jan Humme (@opentrades) - August 2012, revised April 2013
-#
-# Tested and found to work correctly using blotter r1420
-#
-# 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="UTC")
-
-###
-
-.fast = 10
-.slow = 30
-
-.orderqty = 100000
-.threshold = 0.0005
-.txn.fees = -30
-
-##### PLACE DEMO AND TEST DATES HERE #################
-#
-#if(isTRUE(options('in_test')$in_test))
-# # use test dates
-# {initDate="2011-01-01"
-# endDate="2012-12-31"
-# } else
-# # use demo defaults
-# {initDate="1999-12-31"
-# endDate=Sys.Date()}
-
-initDate = '2002-10-21'
-
-.from='2002-10-21'
-#.to='2008-07-04'
-.to='2002-10-31'
-
-####
-
-strategy.st = 'luxor'
-portfolio.st = 'forex'
-account.st = 'IB1'
-
-### packages
-#
-# quantstrat package will pull in some other packages:
-# FinancialInstrument, quantmod, blotter, xts
-
-require(quantstrat)
-
-### FinancialInstrument
-
-currency(c('GBP', 'USD'))
-
-exchange_rate('GBPUSD', tick_size=0.0001)
-
-### quantmod
-
-getSymbols.FI(Symbols='GBPUSD',
- dir=system.file('extdata',package='quantstrat'),
- from=.from, to=.to
-)
-
-# ALTERNATIVE WAY TO FETCH SYMBOL DATA
-#setSymbolLookup.FI(system.file('extdata',package='quantstrat'), 'GBPUSD')
-#getSymbols('GBPUSD', from=.from, to=.to, verbose=FALSE)
-
-### xts
-
-GBPUSD = to.minutes30(GBPUSD)
-GBPUSD = align.time(to.minutes30(GBPUSD), 1800)
-
-### blotter
-
-initPortf(portfolio.st, symbols='GBPUSD', initDate=initDate, currency='USD')
-initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD')
-
-### quantstrat
-
-initOrders(portfolio.st, initDate=initDate)
-
-### define strategy
-
-strategy(strategy.st, store=TRUE)
-
-### indicators
-
-add.indicator(strategy.st, name = "SMA",
- arguments = list(
- x = quote(Cl(mktdata)[,1]),
- n = .fast
- ),
- label="nFast"
-)
-
-add.indicator(strategy.st, name="SMA",
- arguments = list(
- x = quote(Cl(mktdata)[,1]),
- n = .slow
- ),
- label="nSlow"
-)
-
-### signals
-
-add.signal(strategy.st, name='sigCrossover',
- arguments = list(
- columns=c("nFast","nSlow"),
- relationship="gte"
- ),
- label='long'
-)
-
-add.signal(strategy.st, name='sigCrossover',
- arguments = list(
- columns=c("nFast","nSlow"),
- relationship="lt"
- ),
- label='short'
-)
-
-### rules
-
-add.rule(strategy.st, name='ruleSignal',
- arguments=list(sigcol='long' , sigval=TRUE,
- orderside='short',
- ordertype='market',
- orderqty='all',
- TxnFees=.txn.fees,
- replace=TRUE
- ),
- type='exit',
- label='Exit2LONG'
-)
-
-add.rule(strategy.st, name='ruleSignal',
- arguments=list(sigcol='short', sigval=TRUE,
- orderside='long' ,
- ordertype='market',
- orderqty='all',
- TxnFees=.txn.fees,
- replace=TRUE
- ),
- type='exit',
- label='Exit2SHORT'
-)
-
-add.rule(strategy.st, name='ruleSignal',
- arguments=list(sigcol='long' , sigval=TRUE,
- orderside='long' ,
- ordertype='stoplimit', prefer='High', threshold=.threshold,
- orderqty=+.orderqty,
- replace=FALSE
- ),
- type='enter',
- label='EnterLONG'
-)
-
-add.rule(strategy.st, name='ruleSignal',
- arguments=list(sigcol='short', sigval=TRUE,
- orderside='short',
- ordertype='stoplimit', prefer='Low', threshold=-.threshold,
- orderqty=-.orderqty,
- replace=FALSE
- ),
- type='enter',
- label='EnterSHORT'
-)
-
-###############################################################################
-
-applyStrategy(strategy.st, portfolio.st, verbose = FALSE)
-
-View(getOrderBook(portfolio.st)[[portfolio.st]]$GBPUSD)
-
-###############################################################################
-
-updatePortf(portfolio.st, Symbols='GBPUSD', Dates=paste('::',as.Date(Sys.time()),sep=''))
-
-chart.Posn(portfolio.st, "GBPUSD")
-
-###############################################################################
-
-View(tradeStats(portfolio.st, 'GBPUSD'))
-
-###############################################################################
-
-# save the strategy in an .RData object for later retrieval
-
-save.strategy(strategy.st)
-
-##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
-# book = getOrderBook(port)
-# stats = tradeStats(port)
-# rets = PortfReturns(acct)
-################################################################
More information about the Blotter-commits
mailing list