我想学习如何使用这些软件包,但我似乎找不到任何提供除了大量代码片段之外的其他内容的小插图.我想了解它们如何融合在一起,以及类似于"走过去"的东西.
我在网上找到了一些像这个系列的例子:http://timelyportfolio.blogspot.com/2011/06/quantstrat-to-build-on.html但是我正在深入了解一些事情(比如"PerformanceAnalytics"包中的晕影/示例
任何来源?
Python中有类似于quantstrat的东西吗?
我的quanstrat策略返回一个错误,我还没有发现它正在讨论.
策略非常简单:计算给定时间段内的滚动总和.如果滚动金额超过某个阈值,请输入long并提交siultanesouly两个oco订单,止盈和止损在+/- 5%的距离.
代码是:
require("quantstrat")
from <- "2014-09-25"
to <- "2014-10-01"
rm(strategy.st)
try(rm("account.st","portfolio.st"),silent=TRUE)
.blotter <- new.env()
.strategy <- new.env()
initDate <- as.character(as.Date(from) - 1)
currency("USD")
Sys.setenv(TZ = "UTC")
symbols <- "data"
stock(symbols, currency = "USD", multiplier = 1) # Initialisation of the instrument
tradeSize <- 1 # Initialisation of trade size
initEq <- 1000 # Initialisation of initial equity
strategy.st <- "btc" # Initialisation of the strategy
portfolio.st <- "btc" # Initialisation of the strategy, must be after strategy
account.st …
Run Code Online (Sandbox Code Playgroud) 许多量子计和附带的例子似乎是通过交叉某种技术指标来围绕进入和退出交易.
但是,假设你有一个任意指标,你用它来触发交易进入,但是你想在第二天开盘或收盘时解除交易.你最好如何实现这个例子?
我们来看下面的例子:
例如,使用blotter
这样的东西写相对容易:
假设ratio
用列调用xts对象:
我们的代码将是:
for( i in 1:nrow(ratio) ) {
## Define the dates:
CurrentDate <- index(ratio[i,])
NextDate <- index(ratio[i+1,])
## Define the prices:
XYZClosePrice <- as.numeric(ratio$XYZ.Close[i,])
ABCClosePrice <- as.numeric(ratio$ABC.Close[i,])
XYZOpenPrice <- as.numeric(ratio$XYZ.Open[i+1,])
ABCOpenPrice <- as.numeric(ratio$ABC.Open[i+1,])
CCYClosePrice <- as.numeric(ratio$CCY.Close[i,])
CCYOpenPrice <- as.numeric(ratio$CCY.Open[i+1,])
## Define the spread:
SpreadOp <- ABCOpenPrice/XYZOpenPrice
SpreadCl <- ABCClosePrice/XYZClosePrice
## Define the …
Run Code Online (Sandbox Code Playgroud) 我注意到Quantstrat通常采用基于价格的指标.但是,我想加载一些外部计算的指标以及价格数据.例如,我在csv文件中有2个额外的列,其中包含我的指标(编号为1-9).我想根据这些列中的数字生成一个信号.
到目前为止,我无法让Quantstrat读取csv文件中的列.我在下面附上了我的代码:
library(quantmod)
library(quantstrat)
library(PerformanceAnalytics)
library(foreach)
library(FinancialInstrument)
getSymbols("SPY", from = "2015-12-21", to = "2016-12-20", src = "yahoo", adjust =TRUE)
read.csv("/Users/tylerdrust/Downloads/2016 Demark Indicators.csv",
stringsAsFactors = FALSE)
Date Open High Low Close Volume Adj.Close TD.Sell.Count TD.Buy.Count
1 12/21/15 201.41 201.88 200.09 201.67 99094300 197.43 0 0
2 12/22/15 202.72 203.85 201.55 203.50 111026200 199.22 0 0
3 12/23/15 204.69 206.07 204.58 206.02 110987200 201.69 0 0
4 12/24/15 205.72 206.33 205.42 205.68 48539600 201.36 0 0
5 12/28/15 204.86 205.26 203.94 205.21 …
Run Code Online (Sandbox Code Playgroud) 以下是我正在使用quantstrat进行多时间帧策略的示例.这是做多时间框架策略的正确方法还是我做错了?我没有遇到任何其他在quantstrat演示或谷歌搜索中做多时间帧的例子.
为了保持策略部分简单(这不是某人会交易的策略)并将重点放在多时间框架方面,我将演示一个使用刻度数据和 5分钟OHLC数据的简单策略.策略逻辑是当滴答数据超过5分钟数据的30周期SMA时买入,并在滴答数据低于同一SMA时关闭仓位.
例如:如果策略是平坦的,则时间是13:02,并且之前观察到的5分钟数据的30周期SMA是90.55(对于12:55-结束13:00的时段),以及刻度数据从低于90.55到高于它(90.56)的交叉是买入,当蜱数据再次低于它时,它退出该位置.
为了达到这个目的,我需要将tick数据和5分钟30周期SMA同时放入同一个对象中进行量子处理.我得到了5分钟的OHLC xts并计算了它的30周期SMA.然后我将它合并到刻度数据xts对象中,这将给我一个包含所有刻度数据的对象,然后每隔5分钟,我将获得最后一次观察到的5分钟,30周期SMA的行.
如果在13:00有30个周期的SMA值,则为5分12:55-13:00.由于下次更新SMA是5分钟后,我需要填充行直到观察到下一个值(在13:05),依此类推.
这是head
刻度数据(我有的刻度数据不包括毫秒,但我使用make.index.unique(clemtick)
以下方法使行唯一:
head(clemtick)
Price Volume
2013-01-15 09:00:00 93.90 1
2013-01-15 09:00:00 93.89 1
2013-01-15 09:00:00 93.89 1
2013-01-15 09:00:00 93.88 2
2013-01-15 09:00:00 93.89 1
2013-01-15 09:00:00 93.89 2
Run Code Online (Sandbox Code Playgroud)
这是head
1分钟数据(每分钟代表前一分钟的数据,例如时间戳09:01:00 ==数据从09:00:00 - 09:01:00):
head(clemin)
Open High Low Close Volume
2013-01-15 09:01:00 93.90 94.04 93.87 93.97 1631
2013-01-15 09:02:00 93.97 93.98 93.90 93.91 522
2013-01-15 09:03:00 93.91 93.97 93.90 93.96 248
2013-01-15 09:04:00 93.95 93.98 93.93 93.95 138 …
Run Code Online (Sandbox Code Playgroud) 在quantstrat包中,我找到了applyRule函数缓慢的主要罪魁祸首之一,并想知道是否有更高效的写入while循环.任何反馈都会有所帮助.任何人都可以将这部分包装成并行R.
作为一种选择,申请会有效吗?或者我应该将此部分重新编写为新函数,例如ruleProc和nextIndex?我也在沉迷于Rcpp,但这可能是一个特殊的问题.非常感谢任何帮助和建设性的建议?
while (curIndex) {
timestamp = Dates[curIndex]
if (isTRUE(hold) & holdtill < timestamp) {
hold = FALSE
holdtill = NULL
}
types <- sort(factor(names(strategy$rules), levels = c("pre",
"risk", "order", "rebalance", "exit", "enter", "entry",
"post")))
for (type in types) {
switch(type, pre = {
if (length(strategy$rules[[type]]) >= 1) {
ruleProc(strategy$rules$pre, timestamp = timestamp,
path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
symbol = symbol, ruletype = type, mktinstr = mktinstr)
}
}, risk = {
if (length(strategy$rules$risk) >= 1) …
Run Code Online (Sandbox Code Playgroud) 如何在quantstrat中实施“以开盘价在下一栏买入”?
这是我对maCross.R示例的实验。
添加prefer='Open'
RuleSignal
stratMACROSS <- add.rule(strategy = stratMACROSS, name='ruleSignal',
arguments = list(sigcol="ma50.gt.ma200", sigval=TRUE, orderqty=100000, ordertype='market', orderside='long', prefer='Open'), type='enter')
stratMACROSS <- add.rule(strategy = stratMACROSS, name='ruleSignal',
arguments = list(sigcol="ma50.lt.ma200", sigval=TRUE, orderqty=-100000, ordertype='market', orderside='long', prefer='Open'), type='exit')
Run Code Online (Sandbox Code Playgroud)订单以当前Open
价格生成,但在下一个栏执行Close
。
> orders <- getOrderBook(portfolio.st)
> head(orders)
Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime
2011-05-22 00:00:00 "0" NA "init" "long" "0" "closed" "2011-05-22"
2011-05-24 04:30:00 "1e+05" "1.61297" "market" "long" NA "closed" "2011-05-24 05:00:00"
2011-05-25 03:00:00 "-1e+05" "1.61523" "market" "long" NA "closed" …
Run Code Online (Sandbox Code Playgroud)我仍在使用 Guy Yollins quantstrat 示例。在此示例中,当 SPY 穿越 10 日均线时,他买入 1000 股。由于我们定义了初始股本,是否可以始终购买整个投资组合金额而不仅仅是 900 股?“all”不适用于进入,仅适用于退出..
if (!exists('.blotter')) .blotter <- new.env()
if (!exists('.strategy')) .strategy <- new.env()
if (!exists('.instrument')) .instrument <- new.env()
currency("USD")
stock("SPY",currency="USD",multiplier=1)
ls(envir=FinancialInstrument:::.instrument)
initDate <- '1997-12-31'
startDate <- '1998-01-01'
endDate <- '2013-07-31'
initEq <- 1e6
Sys.setenv(TZ="UTC")
getSymbols('SPY', from=startDate, to=endDate, adjust=T)
SPY=to.monthly(SPY, indexAt='endof')
SPY$SMA10m <- SMA(Cl(SPY), 10)
# inz portfolio, account
qs.strategy <- "qsFaber"
rm.strat(qs.strategy) # remove strategy etc. if this is a re-run
initPortf(qs.strategy,'SPY', initDate=initDate)
initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate, initEq=initEq)
initOrders(portfolio=qs.strategy,initDate=initDate)
# instantiate a …
Run Code Online (Sandbox Code Playgroud) 我需要修改这个示例代码,以便将它与日常数据一起使用,我应该从这里和此处获取.据我所知,该示例中的代码适用于任何历史数据(或不是?),因此我的问题归结为以必要的格式(我的意思是每日或日内)加载初始数据的问题.
正如我也从这个问题的答案中理解的那样,不可能加载日内数据getSymbols()
.我试图将这些数据下载到我的硬盘驱动器中,然后使用一个read.csv()
函数来获取它,但这种方法不能正常工作.最后,我在各种文章(例如这里)中找到了这个问题的解决方案,但是所有这些解决方案看起来都非常复杂和"人为".
所以,我的问题是如何从程序员的角度优雅而正确地将给定的日内数据加载到给定的代码中,而不重新发明轮子?
PS我对R和quantstrat中的时间序列分析很新,因此如果我的问题看起来很模糊,请告诉我你需要知道什么才能回答它.