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

站长

发表文章数:4263

python HelloWorld 的 4 种姿势,你知道几种

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

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

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

贪心+huffman编码+模拟退火+分治(一)

#设置策略时间
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")

 

聊一聊 MySQL 中的数据编辑过程中涉及的两阶段提交

未经允许不得转载作者:站长, 转载或复制请以 超链接形式 并注明出处 xss云之家-资源网,新人技术交流平台,一个湖北娃的个人博客
原文地址:《Quantitative Trading with R(一):两个简单的策略》 发布于2020-02-03

分享到:
赞(0) 打赏 生成海报

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

投稿赚钱
2020年在家赚取零花钱
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录
切换登录

注册