码迷,mamicode.com
首页 > 其他好文 > 详细

Quantitative Trading with R(一):两个简单的策略

时间:2020-02-03 14:06:54      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:ber   color   new   src   ssi   tip   macros   class   初始   

下面是两个使用R中的Quantstrat包进行策略构建的例子,都是对600550.ss、600192.ss、600152.ss、600644.ss、600885.ss、600151.ss六只股票进行投资。第一个是简单的动量策略;第二个是简单的趋势策略。

虽然策略的表现都不太好,但是都有一个较为完整的框架,后面希望整理一下自己关于Quantstrat的学习笔记,刚接触相关方面的R语言小白,希望大家多多批评,欢迎交流!(在这之前要先加载quantstrat包)

一、动量策略:将每个股票过去14天收益率和股票过去30天收益率之间的比较作为投资信号

#设置策略时间
initDate="2018-01-01"
from="2018-04-18"
to="2020-01-23"

#导入市场数据
c=c("600550.ss","600192.ss","600152.ss","600644.ss","600885.ss","600151.ss")
Z=c("A","B","C","D","E","F")
for(i in 1:length(c)){
  name=c[i]
  setSymbolLookup(STOCK=list(name=name,src=‘yahoo‘))
  getSymbols("STOCK",from="2018-04-18",to="2020-01-23")
  assign(paste0("A",Z[i]),na.approx(STOCK))
}

#初始化货币单位、时区和市场
Sys.setenv(TZ="UTC")
currency("RMB")
symbols=c("AA","AB","AC","AD","AE","AF")
stock(symbols,currency="RMB",multiplier=1)

#初始化投资策略环境空间
tradeSize=100000000
initEq=tradeSize*length(symbols)
strategy.st=portfolio.st=account.st="maCross"
rm.strat(strategy.st)
.blotter=.strategy=new.env()
ls(envir=.strategy)

#初始化策略
initPortf(portfolio.st,symbols=symbols,initDate=initDate,currency="USD")
initAcct(account.st,portfolios=portfolio.st,initDate=initDate,currency="USD",initEq=initEq)
initOrders(portfolio.st,initDate=initDate)
strategy(strategy.st,store=T)

#查看策略的基本情况
ls(.blotter)
summary(.blotter$account.maCross)
.blotter$account.maCross
.blotter$portfolio.maCross
ls(.strategy)
str(.strategy$maCross)
str(.strategy$order_book.maCross)

#策略指标设置
add.indicator(strategy.st,name="ROC",
              arguments=list(x=quote(Cl(mktdata)),n=14,type=‘continuous‘),label="PS1")
add.indicator(strategy.st,name="ROC",
              arguments=list(x=quote(Cl(mktdata)),n=30,type=‘continuous‘),label="PS2")

#策略信号设置
add.signal(strategy.st,name="sigComparison",
           arguments=list(columns=c("PS1","PS2"),relationship="gt"),
           label="PS1.gt.PS2")
add.signal(strategy.st,name="sigComparison",
           arguments = list(columns=c("PS1","PS2"),relationship="lt"),
           label="PS1.lt.PS2")

#策略规则设置
add.rule(strategy.st,name=‘ruleSignal‘,
         arguments=list(sigcol="PS1.gt.PS2",sigval=TRUE,prefer=‘Close‘,
                        orderqty=90000,ordertype=‘market‘,orderside=‘long‘),
         type=‘enter‘)
add.rule(strategy.st,name=‘ruleSignal‘,
         arguments=list(sigcol="PS1.lt.PS2",sigval=TRUE,prefer=‘Close‘,
                        orderqty=‘all‘,ordertype=‘market‘,orderside=‘long‘),
         type=‘enter‘)
applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

#策略绩效分析
updatePortf(portfolio.st)
updateAcct(portfolio.st)
updateEndEq(account.st)
chart.Posn(Portfolio=‘maCross‘,Symbol=symbols)
a=getAccount(strategy.st)
equity=a$summary$End.Eq
plot(equity,main="Faber Strategy Equity Curve")
ret=Return.calculate(equity,method="log")
charts.PerformanceSummary(ret,colorset=redfocus,main="Performance")

二、趋势策略:将每个股票的MA50向上突破或向下突破MA200作为投资信号

 

#设置策略时间
initDate="2018-01-01"
from="2018-04-18"
to="2020-01-23"

#导入市场数据
c=c("600550.ss","600192.ss","600152.ss","600644.ss","600885.ss","600151.ss")
Z=c("A","B","C","D","E","F")
for(i in 1:length(c)){
  name=c[i]
  setSymbolLookup(STOCK=list(name=name,src=‘yahoo‘))
  getSymbols("STOCK",from="2018-04-18",to="2020-01-23")
  assign(paste0("A",Z[i]),na.approx(STOCK))
}

#初始化货币单位、时区和市场
Sys.setenv(TZ="UTC")
currency("USD")
symbols=c("AA","AB","AC","AD","AE","AF")
stock(symbols,currency="USD",multiplier=1)

#初始化投资策略环境空间
tradeSize=100000000
initEq=tradeSize*length(symbols)
strategy.st=portfolio.st=account.st="maCross"
rm.strat(strategy.st)
.blotter=.strategy=new.env()
ls(envir=.strategy)

#初始化策略
initPortf(portfolio.st,symbols=symbols,initDate=initDate,currency="USD")
initAcct(account.st,portfolios=portfolio.st,initDate=initDate,currency="USD",initEq=initEq)
initOrders(portfolio.st,initDate=initDate)
strategy(strategy.st,store=T)

#查看策略的基本情况
ls(.blotter)
summary(.blotter$account.maCross)
.blotter$account.maCross
.blotter$portfolio.maCross
ls(.strategy)
str(.strategy$maCross)
str(.strategy$order_book.maCross)

#策略指标设置
pctATR=0.2
period=10
atrOrder=TRUE
nRSI=2
buyThresh=20
sellThresh=80
nSMA=200
add.indicator(strategy.st,name="SMA",arguments=list(x=quote(Cl(mktdata)),n=50),label="ma50")
add.indicator(strategy.st,name="SMA",arguments=list(x=quote(Cl(mktdata)),n=200),label="ma200")
add.indicator(strategy.st,name="ATR",arguments=list(HLC=quote(HLC(mktdata)),n=period),label="atrX")

#策略信号设置
add.signal(strategy.st,name="sigCrossover",
           arguments=list(columns=c("ma50","ma200"),relationship="gte"),
           label="ma50.gt.ma200")
add.signal(strategy.st,name="sigCrossover",
           arguments=list(columns=c("ma50","ma200"),relationship="lt"),
           label="ma50.lt.ma200")

#策略规则设置
add.rule(strategy.st,name=‘ruleSignal‘,
         arguments=list(sigcol="ma50.gt.ma200",sigval=TRUE,prefer=‘Close‘,
                        orderqty=90000,ordertype=‘market‘,orderside=‘long‘),
         type=‘enter‘)
add.rule(strategy.st,name=‘ruleSignal‘,
         arguments=list(sigcol="ma50.lt.ma200",sigval=TRUE,prefer=‘Close‘,
                        orderqty=‘all‘,ordertype=‘market‘,orderside=‘long‘),
         type=‘enter‘)
applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

#策略绩效分析
updatePortf(portfolio.st)
updateAcct(portfolio.st)
updateEndEq(account.st)
chart.Posn(Portfolio=‘maCross‘,Symbol=symbols)
a=getAccount(strategy.st)
equity=a$summary$End.Eq
plot(equity,main="Faber Strategy Equity Curve")
ret=Return.calculate(equity,method="log")
charts.PerformanceSummary(ret,colorset=redfocus,main="Performance")

 

Quantitative Trading with R(一):两个简单的策略

标签:ber   color   new   src   ssi   tip   macros   class   初始   

原文地址:https://www.cnblogs.com/amosding/p/12255274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!