<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>量化投资 on 格物堂</title><link>https://gewutang.cn/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/</link><description>Recent content in 量化投资 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 22 May 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/index.xml" rel="self" type="application/rss+xml"/><item><title>用R中的eTTR包和tidyquant包绘制布林带</title><link>https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</link><pubDate>Thu, 22 May 2025 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</guid><description>&#10;&#10;&#10;&lt;div id="引言" class="section level1"&gt;&#10;&lt;h1&gt;引言&lt;/h1&gt;&#10;&lt;p&gt;俗话说，巧妇难为无米之炊。绘图的前提是得有数据在手。下面是获取数据的代码：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义股票代码&#10;tickers &amp;lt;- c(&amp;quot;TSLA&amp;quot;, &amp;quot;AAPL&amp;quot;, &amp;quot;NVDA&amp;quot;)&#10;&#10;# 批量下载股票数据并进行预处理&#10;stock_list &amp;lt;- map(tickers, function(ticker) {&#10; # 下载数据&#10; stock_data &amp;lt;- getSymbols(ticker, auto.assign = FALSE)&#10; # 重命名列&#10; colnames(stock_data) &amp;lt;- c(&amp;quot;Open&amp;quot;, &amp;quot;High&amp;quot;, &amp;quot;Low&amp;quot;, &amp;quot;Close&amp;quot;, &amp;quot;Volume&amp;quot;, &amp;quot;Adjusted&amp;quot;)&#10; # 转化为tibble&#10; tibble_data &amp;lt;- as_tibble(fortify.zoo(stock_data))&#10; # 标准化列命名&#10; renamed_data &amp;lt;- tibble_data %&amp;gt;%&#10; rename(&#10; Date = Index,&#10; open = Open,&#10; high = High,&#10; low = Low,&#10; close = Close,&#10; volume = Volume,&#10; adjusted = Adjusted&#10; )&#10; # 动态命名&#10; dyn_name &amp;lt;- paste0(ticker, &amp;quot;_data&amp;quot;)&#10; assign(dyn_name, renamed_data, envir = globalenv())&#10; return(get(dyn_name))&#10;})&#10;# 重新命名列表&#10;names(stock_list) &amp;lt;- tickers&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;有了基础数据之后，接下来是根据布林带定义计算布林带值，并将布林带数据整合进原始数据备用。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 使用dplyr生成包含high, low, close的tibble对象并计算布林带&#10;stock_list_with_bbands &amp;lt;- map(stock_list, function(data) {&#10; # 确认输入参数为tibble对象&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; # 提取eTTR::BBands所需参数&#10; hlc_tibble &amp;lt;- data %&amp;gt;% select(high, low, close)&#10; # 计算布林带&#10; bbands &amp;lt;- BBands(hlc_tibble, n = 20, sd = 2)&#10; # 整合进原始数据&#10; data %&amp;gt;%&#10; mutate(&#10; middle = bbands[, &amp;quot;mavg&amp;quot;],&#10; upper = bbands[, &amp;quot;up&amp;quot;],&#10; lower = bbands[, &amp;quot;dn&amp;quot;]&#10; )&#10;})&#10;&#10;# 将列表中的tibble行合并，并增加公司名作为列&#10;combined_data &amp;lt;- bind_rows(stock_list_with_bbands,.id = &amp;quot;company_name&amp;quot;)&#10;&#10;# 确保相关列是数值型&#10;num_cols &amp;lt;- c(&amp;quot;open&amp;quot;, &amp;quot;high&amp;quot;, &amp;quot;low&amp;quot;, &amp;quot;close&amp;quot;, &amp;quot;middle&amp;quot;, &amp;quot;upper&amp;quot;, &amp;quot;lower&amp;quot;)&#10;combined_data &amp;lt;- combined_data %&amp;gt;% &#10; mutate(across(all_of(num_cols), as.numeric))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;数据准备好之后，用&lt;code&gt;ggplot2&lt;/code&gt;包几行命令就可以打造一个叠加布林带的蜡烛图，代码如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 利用分面功能绘制多支股票的蜡烛图和布林带&#10;ggplot(combined_data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_line(aes(y = middle), color = &amp;quot;blue&amp;quot;) +&#10; geom_line(aes(y = upper), color = &amp;quot;red&amp;quot;) +&#10; geom_line(aes(y = lower), color = &amp;quot;green&amp;quot;) +&#10; facet_wrap(~ company_name, scales = &amp;quot;free_y&amp;quot;) +&#10; labs(title = &amp;quot;多股票布林带对比&amp;quot;, x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;当然，还可以单独绘制每支股票的蜡烛图叠加布林带。这里我们使用&lt;code&gt;purrr&lt;/code&gt;包中的&lt;code&gt;walk2&lt;/code&gt;函数来遍历列表中每一个对象。代码如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 单独绘制每支股票的蜡烛图叠加布林带&#10;walk2(stock_list_with_bbands, names(stock_list_with_bbands), function(data, name) {&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; p &amp;lt;- ggplot(data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_line(aes(y = middle), color = &amp;quot;blue&amp;quot;) +&#10; geom_line(aes(y = upper), color = &amp;quot;red&amp;quot;) +&#10; geom_line(aes(y = lower), color = &amp;quot;green&amp;quot;) +&#10; labs(title = paste0(&amp;quot;布林带 - &amp;quot;, name), x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&#10; print(p)&#10;})&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-1.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;其实，tidyquant包中提供了一个直接绘制布林带的函数叫 &lt;code&gt;tidyquant::geom_bbands&lt;/code&gt;，可以在绘图时直接基于&lt;code&gt;HLC&lt;/code&gt;数据计算布林带。这样一来，可以免去单独计算布林带数据的过程。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 基于geom_bbands 单独绘制每个对象的蜡烛图叠加布林带&#10;walk2(stock_list, names(stock_list), function(data, name) {&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; p &amp;lt;- ggplot(data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_bbands(aes(high = high, low = low, close = close),ma_fun = SMA, n = 20) +&#10; labs(title = paste0(&amp;quot;布林带 - &amp;quot;, name), x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&#10; print(p)&#10;})&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-1.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;呐，有了R语言，尤其是eTTR包和ggplot2包之后，计算技术指标并进行可视化就是这么简单。欢迎收藏本网站，关注微信公众号&lt;strong&gt;ruijiao007&lt;/strong&gt;。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>RSI指标可以作为量化投资的信号因子吗</title><link>https://gewutang.cn/2025/05/04/rsi/</link><pubDate>Sun, 04 May 2025 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2025/05/04/rsi/</guid><description>&lt;h2 id="引言"&gt;&lt;a href="#%e5%bc%95%e8%a8%80" class="header-anchor"&gt;&lt;/a&gt;引言&#10;&lt;/h2&gt;&lt;p&gt;技术指标是基于历史价格和成交量数据构建的分析工具，用于研判市场趋势及买卖时机。常见类型包括趋势指标（如移动平均线）、动量指标（如RSI）、波动率指标（布林带）和成交量指标（OBV）。其中，相对强弱指数（RSI）由韦尔斯·怀尔德于1978年提出，通过计算特定周期（通常14日）内平均涨幅与总波动的比值，衡量价格变化强度。其公式为：RSI = 100 - 100/(1 + 平均涨幅/平均跌幅)，数值在0-100间波动。应用时，70以上视为超买信号，提示潜在回调可能；30以下为超卖信号，暗示反弹机会。进阶用法包括：观察RSI与价格背离判断趋势反转；结合趋势线突破确认交易信号；在30-70区间内运用中位线（50）判断多空力道。&lt;/p&gt;&#10;&lt;p&gt;需注意，在单边行情中RSI易出现钝化，应与趋势指标配合使用以提高准确性。该指标广泛应用于股票、外汇及加密货币市场的短线交易策略。&lt;/p&gt;&#10;&lt;h2 id="策略实现"&gt;&lt;a href="#%e7%ad%96%e7%95%a5%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;策略实现&#10;&lt;/h2&gt;&lt;p&gt;下面我们用R实现一个基于RSI指标的量化交易策略，我们基于quantstrat包实现相关策略，完整代码及注释如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# ======================================================&#10;# 量化交易策略框架搭建（基于quantstrat包）&#10;# 策略逻辑：基于RSI指标的双向交易系统&#10;# 核心流程：环境初始化 -&amp;gt; 策略定义 -&amp;gt; 数据准备 -&amp;gt; 回测执行&#10;# ======================================================&#10;&#10;# ---------------------------&#10;# 1. 包加载与环境清理&#10;# ---------------------------&#10;# 安装必要包（已注释，按需启用）&#10;# install.packages(&amp;quot;devtools&amp;quot;) &#10;# install.packages(&amp;quot;FinancialInstrument&amp;quot;) &#10;# install.packages(&amp;quot;PerformanceAnalytics&amp;quot;) &#10;# devtools::install_github(&amp;quot;braverock/blotter&amp;quot;)&#10;# devtools::install_github(&amp;quot;braverock/quantstrat&amp;quot;)&#10;&#10;# 加载量化策略包&#10;require(quantstrat)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'PerformanceAnalytics'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:graphics':&#10;## &#10;## legend&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 清理历史策略数据（避免残留数据干扰）&#10;suppressWarnings(rm(&amp;quot;order_book.RSI&amp;quot;,pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.RSI&amp;quot;,&amp;quot;portfolio.RSI&amp;quot;,pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;account.st&amp;quot;,&#10; &amp;quot;portfolio.st&amp;quot;,&#10; &amp;quot;stock.str&amp;quot;,&#10; &amp;quot;stratRSI&amp;quot;,&#10; &amp;quot;initDate&amp;quot;,&#10; &amp;quot;initEq&amp;quot;,&#10; 'start_t',&#10; 'end_t'&#10; )&#10; )&#10;&#10;# ---------------------------&#10;# 2. 策略主体构建&#10;# ---------------------------&#10;# 创建策略容器&#10;stratRSI &amp;lt;- strategy(&amp;quot;RSI&amp;quot;)&#10;n=2 # 参数示例&#10;&#10;# 2.1 添加技术指标&#10;# 使用经典RSI指标（默认周期14）&#10;stratRSI &amp;lt;- add.indicator(&#10; strategy = stratRSI, &#10; name = &amp;quot;RSI&amp;quot;, # 内置RSI函数&#10; arguments = list(price = quote(getPrice(mktdata))), # 获取价格数据&#10; label = &amp;quot;RSI&amp;quot;&#10;)&#10;&#10;&#10;# 2.2 定义交易信号&#10;# 信号1：RSI上穿70（超买信号）&#10;stratRSI &amp;lt;- add.signal(&#10; strategy = stratRSI, &#10; name = &amp;quot;sigThreshold&amp;quot;,&#10; arguments = list(&#10; threshold = 70,&#10; column = &amp;quot;RSI&amp;quot;,&#10; relationship = &amp;quot;gt&amp;quot;, # greater than&#10; cross = TRUE # 要求穿越阈值&#10; ),&#10; label = &amp;quot;RSI.gt.70&amp;quot;&#10;)&#10;&#10;# 信号2：RSI下穿30（超卖信号）&#10;stratRSI &amp;lt;- add.signal(&#10; strategy = stratRSI, &#10; name = &amp;quot;sigThreshold&amp;quot;,&#10; arguments = list(&#10; threshold = 30,&#10; column = &amp;quot;RSI&amp;quot;,&#10; relationship = &amp;quot;lt&amp;quot;, # less than&#10; cross = TRUE&#10; ),&#10; label = &amp;quot;RSI.lt.30&amp;quot;&#10;)&#10;&#10;# 2.3 设置交易规则&#10;# 规则组1：做空规则&#10;# 入场规则：RSI&amp;gt;70时建立空头仓位&#10;stratRSI &amp;lt;- add.rule(&#10; strategy = stratRSI, &#10; name = 'ruleSignal',&#10; arguments = list(&#10; sigcol = &amp;quot;RSI.gt.70&amp;quot;, # 触发信号列&#10; sigval = TRUE, # 信号有效值&#10; orderqty = -1000, # 卖出数量&#10; ordertype = 'market', # 市价单&#10; orderside = 'short', # 空头方向&#10; pricemethod = 'market',&#10; replace = FALSE, # 不替换现有订单&#10; osFUN = osMaxPos # 使用最大仓位函数&#10; ), &#10; type = 'enter', # 入场规则&#10; path.dep = TRUE # 路径依赖&#10;)&#10;&#10;# 离场规则：RSI&amp;lt;30时平空仓&#10;stratRSI &amp;lt;- add.rule(&#10; strategy = stratRSI,&#10; name = 'ruleSignal',&#10; arguments = list(&#10; sigcol = &amp;quot;RSI.lt.30&amp;quot;,&#10; sigval = TRUE,&#10; orderqty = 'all', # 平掉全部仓位&#10; ordertype = 'market',&#10; orderside = 'short',&#10; pricemethod = 'market',&#10; replace = FALSE&#10; ),&#10; type = 'exit',&#10; path.dep = TRUE&#10;)&#10;&#10;# 规则组2：做多规则&#10;# 入场规则：RSI&amp;lt;30时建立多头仓位&#10;stratRSI &amp;lt;- add.rule(&#10; strategy = stratRSI,&#10; name = 'ruleSignal',&#10; arguments = list(&#10; sigcol = &amp;quot;RSI.lt.30&amp;quot;,&#10; sigval = TRUE,&#10; orderqty = 1000, # 买入数量&#10; ordertype = 'market',&#10; orderside = 'long', # 多头方向&#10; pricemethod = 'market',&#10; replace = FALSE,&#10; osFUN = osMaxPos&#10; ),&#10; type = 'enter',&#10; path.dep = TRUE&#10;)&#10;&#10;# 离场规则：RSI&amp;gt;70时平多仓&#10;stratRSI &amp;lt;- add.rule(&#10; strategy = stratRSI,&#10; name = 'ruleSignal',&#10; arguments = list(&#10; sigcol = &amp;quot;RSI.gt.70&amp;quot;,&#10; sigval = TRUE,&#10; orderqty = 'all',&#10; ordertype = 'market',&#10; orderside = 'long',&#10; pricemethod = 'market',&#10; replace = FALSE&#10; ),&#10; type = 'exit',&#10; path.dep = TRUE&#10;)&#10;&#10;# ---------------------------&#10;# 3. 市场数据准备&#10;# ---------------------------&#10;# 设置基础货币&#10;&#10;currency(&amp;quot;USD&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;currency(&amp;quot;EUR&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;EUR&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 定义交易标的（美国行业ETF）&#10;symbols = c(&amp;quot;XLF&amp;quot;, &amp;quot;XLP&amp;quot;, &amp;quot;XLE&amp;quot;, &amp;quot;XLY&amp;quot;, &amp;quot;XLV&amp;quot;, &amp;quot;XLI&amp;quot;, &amp;quot;XLB&amp;quot;, &amp;quot;XLK&amp;quot;, &amp;quot;XLU&amp;quot;)&#10;# 初始化交易品种数据&#10;for(symbol in symbols){ &#10; # 注册金融工具&#10; stock(symbol, currency=&amp;quot;USD&amp;quot;,multiplier=1)&#10; # 下载历史数据（默认从Yahoo Finance）&#10; getSymbols(symbol)&#10;}&#10;&#10;# 保存数据&#10;# 遍历所有符号&#10;# for (symbol in symbols) {&#10;# # 检查对象是否存在&#10;# if (exists(symbol)) {&#10;# # 生成文件名&#10;# file_name &amp;lt;- paste0(symbol, &amp;quot;.rds&amp;quot;)&#10;# # 保存为RDS文件&#10;# saveRDS(get(symbol), file = file_name)&#10;# # 打印保存信息&#10;# message(&amp;quot;已保存: &amp;quot;, symbol, &amp;quot; -&amp;gt; &amp;quot;, file_name)&#10;# } else {&#10;# warning(&amp;quot;对象 &amp;quot;, symbol, &amp;quot; 不存在&amp;quot;)&#10;# }&#10;#}&#10;&#10;# 可以用类似以下方式测试：&#10;# applySignals(strategy=stratRSI, mktdata=applyIndicators(strategy=stratRSI, mktdata=symbols[1]))&#10;&#10;##### 在此放置演示和测试日期 #################&#10;#&#10;#if(isTRUE(options('in_test')$in_test))&#10;# # 使用测试日期&#10;# {initDate=&amp;quot;2000-01-01&amp;quot; &#10;# endDate=&amp;quot;2024-12-31&amp;quot; &#10;# } else&#10;# # 使用演示默认值&#10;# {initDate=&amp;quot;2000-01-01&amp;quot;&#10;# endDate=Sys.Date()}&#10;&#10;# ---------------------------&#10;# 4. 回测系统初始化&#10;# ---------------------------&#10;# 设置回测参数&#10;initDate = '2000-01-01' # 初始化日期&#10;initEq = 100000 # 初始资金（美元）&#10;port.st = 'RSI' # 组合名称&#10;&#10;# 初始化投资组合&#10;initPortf(port.st, &#10; symbols=symbols, &#10; initDate=initDate)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 初始化账户&#10;initAcct(port.st, &#10; portfolios=port.st, &#10; initDate=initDate,&#10; initEq=initEq)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 初始化订单簿&#10;initOrders(portfolio=port.st, &#10; initDate=initDate)&#10;&#10;# 设置仓位限制（每个品种最大300股，最多3个品种）&#10;for(symbol in symbols){ &#10; addPosLimit(port.st, &#10; symbol, &#10; initDate, &#10; 300, &#10; 3 ) &#10; } &#10;&#10;print(&amp;quot;初始化完成&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;初始化完成&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;end_t&amp;lt;-Sys.time()&#10;print(paste0(&amp;quot;策略循环耗时:&amp;quot;,end_t-start_t))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;策略循环耗时:6.13126289844513&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 查看订单簿&#10;#print(getOrderBook(port.st))&#10;&#10;start_t&amp;lt;-Sys.time()&#10;# 更新组合净值&#10;updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep=''))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#10;## instead.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;end_t&amp;lt;-Sys.time()&#10;print(paste0(&amp;quot;更新交易账簿耗时:&amp;quot;,end_t-start_t))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;更新交易账簿耗时:0.747688055038452&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 临时修改quantmod图形参数&#10;themelist&amp;lt;-chart_theme()&#10;themelist$col$up.col&amp;lt;-'lightgreen'&#10;themelist$col$dn.col&amp;lt;-'pink'&#10;&#10;for(symbol in symbols){&#10; # dev.new()&#10; chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist) # 绘制持仓图&#10; plot(add_RSI(n=2))&#10; print(paste0(symbol,&amp;quot;仓位图&amp;quot;))&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLF仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-3.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-4.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLP仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-5.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-6.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLE仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-7.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-8.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLY仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-9.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-10.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLV仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-11.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-12.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLI仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-13.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-14.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLB仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-15.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-16.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLK仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-17.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-18.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLU仓位图&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 统计组合表现&#10;ret1 &amp;lt;- PortfReturns(port.st)&#10;ret1$total &amp;lt;- rowSums(ret1)&#10;&#10;&#10;if(&amp;quot;package:PerformanceAnalytics&amp;quot; %in% search() || require(&amp;quot;PerformanceAnalytics&amp;quot;,quietly=TRUE)) {&#10; dev.new()&#10; # 绘制收益率图&#10; charts.PerformanceSummary(ret1$total,geometric=FALSE,wealth.index=TRUE)&#10; print(&amp;quot;策略总体表现&amp;quot;)&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;策略总体表现&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;##### 查看交易统计信息 #########################################&#10;# 查看交易单据&#10;book = getOrderBook(port.st)&#10;# 查看交易统计&#10;stats = tradeStats(port.st)&#10;# 查看组合收益率&#10;rets = PortfReturns(port.st)&#10;################################################################&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>基于双均线交叉与追踪止损的交易策略</title><link>https://gewutang.cn/2018/07/27/macross-stoptrailing/</link><pubDate>Fri, 27 Jul 2018 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2018/07/27/macross-stoptrailing/</guid><description>&#10;&#10;&#10;&lt;p&gt;在趋势跟踪策略中，移动平均线交叉是识别趋势转折的经典方法，而止损机制则是控制风险的关键补充。本文解析的 R 语言代码，基于quantstrat包实现了一套融合双均线交叉信号与追踪止损的交易策略，以 AAPL 为标的展示了从策略构建到回测的完整流程。通过明确的信号规则与风险控制机制，该策略为趋势跟踪的量化实现提供了可参考的框架。&lt;/p&gt;&#10;&lt;div id="核心指标与策略逻辑" class="section level1"&gt;&#10;&lt;h1&gt;核心指标与策略逻辑&lt;/h1&gt;&#10;&lt;p&gt;策略的核心指标是两条简单移动平均线（SMA）：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;快速 SMA（5 日）：基于最近 5 个交易日的收盘价计算，反映短期价格趋势。&lt;/li&gt;&#10;&lt;li&gt;慢速 SMA（75 日）：基于最近 75 个交易日的收盘价计算，反映中长期价格趋势。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;策略逻辑围绕两条均线的交叉展开：当快速 SMA 从下方上穿慢速 SMA 时，视为多头趋势确立，生成买入信号；当快速 SMA 从上方下穿慢速 SMA 时，视为趋势转弱，生成卖出信号。同时，为避免趋势反转时的大额亏损，策略加入了追踪止损（stoptrailing）机制 —— 当价格从持仓后的高点回落一定比例时，自动平仓离场，锁定部分收益。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略代码" class="section level1"&gt;&#10;&lt;h1&gt;策略代码&lt;/h1&gt;&#10;&lt;p&gt;以下是完整的策略代码，包含逐段注释以解释各环节功能：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;###############################################################################&#10;# 一个使用简单均线交叉策略的演示脚本，包含追踪止损，用于测试&#10;###############################################################################&#10;&#10;# 加载quantstrat包，用于量化策略开发与回测&#10;library(quantstrat)&#10;&#10;# 策略参数设置&#10;init_date &amp;lt;- &amp;quot;2017-01-01&amp;quot; # 初始化日期&#10;start_date &amp;lt;- &amp;quot;2017-01-01&amp;quot; # 回测开始日期&#10;end_date &amp;lt;- &amp;quot;2018-12-31&amp;quot; # 回测结束日期&#10;init_equity &amp;lt;- 1e4 # 初始资金1万美元&#10;.orderqty &amp;lt;- 100 # 订单数量（后续规则中实际使用1000）&#10;Sys.setenv(TZ = &amp;quot;UTC&amp;quot;) # 设置时区为UTC，避免日期处理问题&#10;fast &amp;lt;- 5 # 快速SMA周期（5日）&#10;slow &amp;lt;- 75 # 慢速SMA周期（75日）&#10;symbols &amp;lt;- symbol &amp;lt;- &amp;quot;AAPL&amp;quot; # 交易标的为AAPL&#10;tradesize = 1e4 # 交易规模（1万美元）&#10;&#10;# 定义货币与标的属性：AAPL以美元计价，合约乘数为1&#10;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载AAPL历史数据（此处使用内置数据，也可从雅虎财经下载）&#10;data(AAPL)&#10;stock(symbols, currency = &amp;quot;USD&amp;quot;, multiplier = 1)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 初始化组合、账户与订单簿&#10;portfolio.st &amp;lt;- &amp;quot;Port.Luxor&amp;quot; # 组合名称&#10;account.st &amp;lt;- &amp;quot;Acct.Luxor&amp;quot; # 账户名称&#10;strategy.st &amp;lt;- &amp;quot;Strat.Luxor&amp;quot; # 策略名称&#10;&#10;# 清理旧策略数据，确保回测环境纯净&#10;rm.strat(strategy.st)&#10;rm.strat(portfolio.st)&#10;rm.strat(account.st)&#10;&#10;# 初始化投资组合：指定标的与初始化日期&#10;initPortf(&#10; name = portfolio.st,&#10; symbols = symbols,&#10; initDate = init_date&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Port.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 初始化账户：关联组合，设置初始资金&#10;initAcct(&#10; name = account.st,&#10; portfolios = portfolio.st,&#10; initDate = init_date,&#10; initEq = init_equity&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 初始化订单簿：记录交易订单&#10;initOrders(&#10; portfolio = portfolio.st,&#10; symbols = symbols,&#10; initDate = init_date&#10;)&#10;&#10;# 创建策略对象并存储到环境中&#10;strategy(strategy.st, store = TRUE)&#10;&#10;# 添加技术指标：快速SMA与慢速SMA&#10;add.indicator(&#10; strategy = strategy.st,&#10; name = &amp;quot;SMA&amp;quot;, # 使用SMA函数计算移动平均线&#10; arguments = list(x = quote(Cl(mktdata)), n = fast), # 输入为收盘价，周期5日&#10; label = &amp;quot;nFast&amp;quot; # 指标标签：快速SMA&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;add.indicator(&#10; strategy = strategy.st,&#10; name = &amp;quot;SMA&amp;quot;,&#10; arguments = list(x = quote(Cl(mktdata)), n = slow), # 周期75日&#10; label = &amp;quot;nSlow&amp;quot; # 指标标签：慢速SMA&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 生成交易信号：基于双均线交叉&#10;# 信号1：快速SMA上穿慢速SMA（大于关系），视为多头信号（LONG）&#10;add.signal(&#10; strategy.st,&#10; name = &amp;quot;sigCrossover&amp;quot;, # 交叉信号函数&#10; arguments = list(&#10; column = c(&amp;quot;nFast&amp;quot;, &amp;quot;nSlow&amp;quot;), # 对比快速与慢速SMA&#10; relationship = &amp;quot;gt&amp;quot; # 大于（上穿）&#10; ),&#10; label = &amp;quot;LONG&amp;quot; # 信号标签：买入&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 信号2：快速SMA下穿慢速SMA（小于关系），视为空头信号（SHORT）&#10;add.signal(&#10; strategy.st,&#10; name = &amp;quot;sigCrossover&amp;quot;,&#10; arguments = list(&#10; column = c(&amp;quot;nFast&amp;quot;, &amp;quot;nSlow&amp;quot;),&#10; relationship = &amp;quot;lt&amp;quot; # 小于（下穿）&#10; ),&#10; label = &amp;quot;SHORT&amp;quot; # 信号标签：卖出&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 入场规则：当LONG信号触发时，以限价单买入&#10;add.rule(&#10; strategy.st,&#10; name = &amp;quot;ruleSignal&amp;quot;, # 基于信号的规则&#10; arguments = list(&#10; sigcol = &amp;quot;LONG&amp;quot;, # 触发信号列：LONG&#10; sigval = TRUE, # 信号值为TRUE时执行&#10; orderside = &amp;quot;long&amp;quot;, # 做多方向&#10; ordertype = &amp;quot;limit&amp;quot;, # 限价单&#10; threshold = 1/100, # 阈值（相对于参考价格的比例）&#10; tmult = T, # 阈值为比例（而非固定值）&#10; orderqty = 1000, # 订单数量：1000股&#10; prefer = &amp;quot;Open&amp;quot;, # 参考价格为开盘价&#10; TxnFees = -10, # 交易费用：-10美元（每次交易成本）&#10; orderset = &amp;quot;ocolong&amp;quot;, # 订单集标签，用于管理相关订单&#10; replace = F # 不替换未成交订单&#10; ),&#10; type = &amp;quot;enter&amp;quot;, # 入场规则&#10; enabled = T, # 启用规则&#10; label = &amp;quot;EnterLONG&amp;quot; # 规则标签&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 出场规则：当SHORT信号触发时，平掉所有多头持仓&#10;add.rule(&#10; strategy = strategy.st,&#10; name = &amp;quot;ruleSignal&amp;quot;,&#10; arguments = list(&#10; sigcol = &amp;quot;SHORT&amp;quot;, # 触发信号列：SHORT&#10; sigval = TRUE,&#10; orderqty = &amp;#39;all&amp;#39;, # 平仓全部持仓&#10; orderside = &amp;#39;long&amp;#39;, # 针对多头持仓&#10; ordertype = &amp;quot;market&amp;quot;, # 市价单（确保快速平仓）&#10; prefer = &amp;quot;Open&amp;quot;, # 参考价格为开盘价&#10; TxnFees = -10, # 交易费用&#10; orderset = &amp;quot;ocolong&amp;quot;,&#10; replace = F&#10; ),&#10; type = &amp;quot;exit&amp;quot;, # 出场规则&#10; enabled = T,&#10; parent = &amp;quot;EnterLONG&amp;quot;, # 关联入场规则，确保对应持仓平仓&#10; label = &amp;quot;Exit2SHORT&amp;quot;&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 止损规则：追踪止损，控制单边趋势反转风险&#10;add.rule(&#10; strategy.st,&#10; name = &amp;quot;ruleSignal&amp;quot;,&#10; arguments = list(&#10; sigcol = &amp;quot;LONG&amp;quot;, # 与入场信号关联&#10; sigval = TRUE,&#10; orderside = &amp;quot;long&amp;quot;,&#10; ordertype = &amp;quot;stoptrailing&amp;quot;, # 追踪止损单&#10; orderqty = &amp;quot;all&amp;quot;, # 平仓全部持仓&#10; prefer = &amp;quot;Close&amp;quot;, # 参考价格为收盘价&#10; threshold = 2/100, # 追踪止损比例：2%（从高点回落2%平仓）&#10; tmult = T, # 阈值为比例&#10; TxnFees = -10,&#10; orderset = &amp;quot;ocolong&amp;quot;,&#10; replace = F&#10; ),&#10; type = &amp;quot;chain&amp;quot;, # 链式规则（跟随入场规则触发）&#10; parent = &amp;quot;EnterLONG&amp;quot;, # 基于EnterLONG规则触发的持仓&#10; enabled = T,&#10; label = &amp;quot;StopTrailingLONG&amp;quot; # 规则标签：多头追踪止损&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 执行策略回测&#10;applyStrategy(strategy = strategy.st, portfolios = portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-07-27 00:00:00 AAPL 1000 @ 141.598281619503&amp;quot;&#10;## [1] &amp;quot;2017-08-03 00:00:00 AAPL -1000 @ 150.70126151877&amp;quot;&#10;## [1] &amp;quot;2017-10-04 00:00:00 AAPL 1000 @ 147.350684699421&amp;quot;&#10;## [1] &amp;quot;2017-10-19 00:00:00 AAPL -1000 @ 151.241571914452&amp;quot;&#10;## [1] &amp;quot;2018-03-22 00:00:00 AAPL 1000 @ 166.357919238892&amp;quot;&#10;## [1] &amp;quot;2018-03-23 00:00:00 AAPL -1000 @ 163.030760854114&amp;quot;&#10;## [1] &amp;quot;2018-04-19 00:00:00 AAPL 1000 @ 168.23487944963&amp;quot;&#10;## [1] &amp;quot;2018-04-20 00:00:00 AAPL -1000 @ 164.870181860637&amp;quot;&#10;## [1] &amp;quot;2018-11-23 00:00:00 AAPL 1000 @ 171.574945095388&amp;quot;&#10;## [1] &amp;quot;2018-11-26 00:00:00 AAPL -1000 @ 168.14344619348&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 更新组合与账户数据&#10;updatePortf(portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Port.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;daterange &amp;lt;- time(getPortfolio(portfolio.st)$summary)[-1] # 获取交易日期范围&#10;updateAcct(account.st, daterange) # 更新账户数据至指定日期&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;updateEndEq(account.st) # 更新账户最终权益&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取交易统计数据&#10;tstats &amp;lt;- t(tradeStats(Portfolios = portfolio.st))&#10;&#10;# 可视化回测结果：展示持仓变化与双均线&#10;chart.Posn(&#10; Portfolio = portfolio.st, &#10; Symbol = &amp;quot;AAPL&amp;quot;, &#10; TA = c(&amp;quot;add_SMA(n=fast, col=&amp;#39;blue&amp;#39;)&amp;quot;, &amp;quot;add_SMA(n=slow, col=&amp;#39;red&amp;#39;)&amp;quot;) # 叠加5日（蓝）与75日（红）均线&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2018/07/27/macross-stoptrailing/index_files/figure-html/simpleSMA-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 查看订单簿（用于调试与详细分析）&#10;obook &amp;lt;- getOrderBook(&amp;#39;Port.Luxor&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略核心思路解析" class="section level1"&gt;&#10;&lt;h1&gt;策略核心思路解析&lt;/h1&gt;&#10;&lt;p&gt;该策略的实现遵循量化交易的标准化流程，各环节紧密衔接，形成完整的逻辑闭环：&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;环境与参数的初始化&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;代码首先清理旧策略数据，避免残留信息干扰；随后定义交易标的（AAPL）、初始资金（1 万美元）、均线周期（5 日 / 75 日）等核心参数，并通过initPortf、initAcct等函数初始化组合、账户与订单簿，为策略运行搭建基础环境。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;指标与信号的层次构建&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;策略通过add.indicator添加双均线指标，将短期与长期趋势量化；再通过add.signal定义交叉信号 —— 上穿为买入、下穿为卖出，使趋势转折的判断从主观观察转化为客观规则。这种 “指标→信号” 的转化，是量化策略可复现性的核心。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;交易规则的风险控制设计&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;策略的规则体系包含三类动作：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;入场：以限价单买入，控制入场成本；&lt;/li&gt;&#10;&lt;li&gt;出场：以市价单平仓，确保趋势反转时及时离场；&lt;/li&gt;&#10;&lt;li&gt;止损：追踪止损单（回落 2% 平仓），在趋势未按预期发展时限制亏损。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;同时，规则中明确了交易费用（每次 - 10 美元），使回测更接近实盘成本结构；通过parent参数关联入场与出场规则，确保持仓与平仓的对应性。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;回测执行与结果可视化&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;代码通过applyStrategy执行策略回测，更新组合与账户数据后，利用chart.Posn展示 AAPL 的持仓变化，并叠加双均线直观呈现交易时机与趋势的关系；tradeStats则提供胜率、盈亏比等统计指标，为策略评估提供数据支持。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略的优势与局限性" class="section level1"&gt;&#10;&lt;h1&gt;策略的优势与局限性&lt;/h1&gt;&#10;&lt;p&gt;该策略的优势体现在逻辑清晰与风险控制的结合：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;规则明确：基于均线交叉的信号无主观干扰，参数与动作均可量化，便于复现与优化；&lt;/li&gt;&#10;&lt;li&gt;风险可控：同时包含趋势反转出场与追踪止损，既捕捉趋势收益，又控制极端风险；&lt;/li&gt;&#10;&lt;li&gt;结构灵活：各环节模块化（指标、信号、规则独立），便于扩展（如添加成交量过滤、调整均线周期）。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;但策略也存在趋势跟踪策略的共性局限：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;滞后性：SMA 基于历史价格计算，信号可能滞后于实际趋势，导致错过最佳入场 / 出场点；&lt;/li&gt;&#10;&lt;li&gt;参数敏感：5 日 / 75 日的周期是固定的，可能仅适配特定时段，在震荡市或极端行情中表现不佳；&lt;/li&gt;&#10;&lt;li&gt;单一标的依赖：策略仅针对 AAPL 设计，若扩展至其他标的，需重新优化参数以适应其波动性。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;div id="总结与改进方向" class="section level1"&gt;&#10;&lt;h1&gt;总结与改进方向&lt;/h1&gt;&#10;&lt;p&gt;本策略展示了双均线交叉与追踪止损结合的量化实现，为趋势跟踪提供了一套可操作的框架。其核心价值在于将 “识别趋势→执行交易→控制风险” 的流程标准化，减少主观决策的干扰。&lt;/p&gt;&#10;&lt;p&gt;若要进一步优化，可从三方面入手：一是引入自适应参数（如根据市场波动率调整均线周期），提升策略对不同行情的适应性；二是添加过滤条件（如成交量放大时才确认信号），减少震荡市的虚假交易；三是扩展至多标的组合，通过分散投资降低单一标的风险。通过持续迭代，策略可在保持逻辑简洁的同时，提升实盘适用性。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>前进测试可视化策略评估的重要工具</title><link>https://gewutang.cn/2017/10/10/luxor-sample-walk-forward/</link><pubDate>Tue, 10 Oct 2017 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2017/10/10/luxor-sample-walk-forward/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易中，理解指标的内涵与策略的稳健性同样重要。MACD（移动平均收敛散度）作为经典的趋势动量指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场的潜在转折与动量变化。&lt;/p&gt;&#10;&lt;p&gt;MACD 由四个部分构成：快速 EMA（通常为 12 日）、慢速 EMA（通常为 26 日）、MACD 线（快速 EMA 与慢速 EMA 的差值）以及信号线（MACD 线的 9 日 EMA）。其主要用途包括：判断趋势方向（MACD 线在零轴上方为多头趋势，下方为空头趋势）、识别趋势强度（MACD 线与价格的背离暗示动量衰竭）、确认交易信号（信号线与 MACD 线的交叉或与零轴的交叉）。&lt;/p&gt;&#10;&lt;p&gt;而前进测试（Walk Forward Testing）则是评估策略稳健性的关键方法，它通过将历史数据划分为连续的训练期与测试期，滚动优化参数并验证效果，有效降低过拟合风险。本文解析的代码便聚焦于前进测试结果的可视化，以直观呈现策略在训练期的表现，为策略评估提供依据。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;#&#10;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#10;#&#10;# 前进测试图表示例&#10;&#10;# 加载quantstrat包，该包提供量化策略开发、回测及可视化工具&#10;require(&amp;#39;quantstrat&amp;#39;)&#10;&#10;# 构建完整路径&#10;data_path &amp;lt;- paste0(path.package(&amp;quot;quantstrat&amp;quot;), &amp;#39;/data/luxor.wfa.ples.RData&amp;#39;)&#10;&#10;# 加载数据文件&#10;if (file.exists(data_path)) {&#10; load(data_path) # 加载文件中的审计环境数据（如策略参数、绩效记录等）&#10; ls() # 查看加载的对象（通常包含前进测试的审计信息）&#10;}&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;data_path&amp;quot; &amp;quot;luxoraudit&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绘制前进测试训练期的图表&#10;# 数据来源为quantstrat包内置的Luxor系统前进测试示例数据（luxor.wfa.ples.RData）&#10;chart.forward.training(luxoraudit)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/10/10/luxor-sample-walk-forward/index_files/figure-html/WalkForward-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;这段代码的核心意图是通过可视化方式展示前进测试中训练期的策略表现，为策略评估提供直观依据。代码首先加载 quantstrat 包，该包是量化策略开发的核心工具，提供了从策略构建到回测可视化的完整功能。随后，代码指定了前进测试结果数据的路径 ——luxor.wfa.ples.RData，该数据源自《A new approach to system development and portfolio optimisation》一书中的 Luxor 系统，包含了前进测试的关键结果（如不同训练期的参数、收益、风险指标等）。&lt;/p&gt;&#10;&lt;p&gt;通过调用chart.forward.training函数，代码将加载的数据转化为直观的图表。前进测试的核心是 “滚动训练与验证”，即把历史数据划分为多个连续的训练期（用于优化策略参数）和测试期（用于验证参数有效性）。该函数专注于训练期的可视化，可能展示不同训练阶段的参数表现、收益曲线或风险指标分布，帮助交易者判断策略参数在不同市场环境下的稳定性，识别是否存在过拟合（如某训练期参数表现极好但其他时期失效）。&lt;/p&gt;&#10;&lt;p&gt;前进测试作为策略评估工具，优势在于通过滚动验证减少过拟合风险 —— 策略参数需在多个独立测试期表现稳定，而非仅适配单一历史区间，更贴近实盘环境。但其劣势在于计算成本较高，尤其是参数范围较广时，需反复训练与验证；此外，训练期与测试期的划分（如周期长度、滚动方式）可能影响结果，主观选择不当仍可能引入偏差。&lt;/p&gt;&#10;&lt;p&gt;对于前进测试本身，可探索动态周期划分（如根据市场波动率调整训练期长度），或引入更严格的统计检验（如夏普比率的显著性测试），进一步提升评估可靠性。通过指标优化与评估方法改进的结合，可构建更稳健的量化交易系统。&lt;/p&gt;&#10;</description></item><item><title>MACD 趋势跟踪策略的实现与解析：基于 AAPL 的量化回测</title><link>https://gewutang.cn/2017/07/10/macdrebalancing/</link><pubDate>Mon, 10 Jul 2017 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2017/07/10/macdrebalancing/</guid><description>&#10;&#10;&#10;&lt;p&gt;MACD（移动平均收敛散度）作为经典的趋势与动量指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场动量的变化。在实际交易中，MACD 的信号线与零轴的交叉常被视为趋势转折的信号 —— 上穿零轴暗示趋势由弱转强，下穿零轴则可能预示趋势走弱。&lt;/p&gt;&#10;&lt;p&gt;本文将解析一段基于 R 语言quantstrat包的 MACD 策略代码，该代码以 AAPL 为标的，通过信号线穿越零轴生成交易信号，实现趋势跟踪，并展示量化策略从环境搭建到回测评估的完整流程。&lt;/p&gt;&#10;&lt;div id="macd-指标的核心构成与策略逻辑" class="section level1"&gt;&#10;&lt;h1&gt;MACD 指标的核心构成与策略逻辑&lt;/h1&gt;&#10;&lt;p&gt;在深入代码前，先明确 MACD 指标的组成与策略设计逻辑：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;快速 EMA（12 日）：基于收盘价计算的短期指数移动平均线，反映近期价格趋势。&lt;/li&gt;&#10;&lt;li&gt;慢速 EMA（26 日）：长期指数移动平均线，反映中长期价格趋势。&lt;/li&gt;&#10;&lt;li&gt;MACD 线：快速 EMA 与慢速 EMA 的差值，衡量短期与长期趋势的偏离程度。&lt;/li&gt;&#10;&lt;li&gt;信号线：MACD 线的 9 日 EMA，用于平滑 MACD 线的波动，减少噪音信号。&lt;/li&gt;&#10;&lt;li&gt;零轴：MACD 线与信号线围绕波动的基准线，代表多空力量的平衡点。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;本策略的核心逻辑是：当信号线从下方穿越零轴（上穿）时，认为多头趋势确立，触发买入信号；当信号线从上方穿越零轴（下穿）时，认为空头趋势显现，触发卖出信号，通过这种方式跟踪中长期趋势。&#10;策略代码与详细注释&lt;/p&gt;&#10;&lt;p&gt;以下是完整的策略代码，包含逐段注释以解释各环节的功能：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# Simple MACD strategy&#10;#&#10;# MACD可通过多种方式使用，本策略展示其作为趋势指标的应用。&#10;# &#10;# 传统上，当MACD信号线穿越零时，标志着正向趋势的确立&#10;#&#10;# 本策略将在“signal”列上穿零轴时买入，下穿零轴时卖出&#10;# &#10;# Author: brian&#10;###############################################################################&#10;&#10;&#10;# 加载quantstrat包，用于量化策略开发与回测&#10;require(quantstrat)&#10;&#10;# 清理旧策略数据，确保回测环境纯净（避免残留数据干扰结果）&#10;suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;account.st&amp;quot;, &amp;quot;portfolio.st&amp;quot;, &amp;quot;stock.str&amp;quot;, &amp;quot;stratMACD&amp;quot;, &amp;quot;startDate&amp;quot;, &amp;quot;initEq&amp;quot;, &amp;#39;start_t&amp;#39;, &amp;#39;end_t&amp;#39;))&#10;&#10;# 设定交易标的为苹果公司股票（AAPL）&#10;stock.str &amp;lt;- &amp;#39;AAPL&amp;#39;&#10;&#10;# MACD指标参数：快速EMA周期、慢速EMA周期、信号线周期，均采用指数移动平均线（EMA）&#10;fastMA = 12 # 快速EMA周期（传统参数为12日）&#10;slowMA = 26 # 慢速EMA周期（传统参数为26日）&#10;signalMA = 9 # 信号线周期（传统参数为9日）&#10;maType = &amp;quot;EMA&amp;quot; # 移动平均线类型为指数移动平均（EMA）&#10;&#10;# 定义货币单位为美元，并初始化标的属性（AAPL以美元计价，合约乘数为1）&#10;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;stock(stock.str, currency=&amp;#39;USD&amp;#39;, multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 回测参数设置：回测起始日期、初始资金、组合与账户名称&#10;startDate = &amp;#39;2006-12-31&amp;#39; # 数据起始日期&#10;initEq = 1000000 # 初始资金100万美元&#10;portfolio.st = &amp;#39;macd&amp;#39; # 投资组合名称&#10;account.st = &amp;#39;macd&amp;#39; # 账户名称&#10;&#10;# 初始化回测环境：创建组合、账户与订单簿&#10;initPortf(portfolio.st, symbols=stock.str) # 初始化组合，包含AAPL&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio.st) # 初始化账户，关联组合&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initOrders(portfolio=portfolio.st) # 初始化订单簿，记录交易订单&#10;&#10;# 创建策略对象，命名为&amp;#39;macd&amp;#39;并存储到环境中&#10;strat.st &amp;lt;- portfolio.st&#10;strategy(strat.st, store=TRUE)&#10;&#10;# 添加MACD指标：基于收盘价计算MACD线、信号线&#10;add.indicator(&#10; strat.st, &#10; name = &amp;quot;MACD&amp;quot;, # 使用TTR包中的MACD函数&#10; arguments = list(x=quote(Cl(mktdata))) # 输入为收盘价（Cl(mktdata)）&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 生成交易信号：基于信号线与零轴的交叉&#10;# 信号1：信号线上穿零轴（大于零且交叉），视为买入信号&#10;add.signal(&#10; strat.st,&#10; name = &amp;quot;sigThreshold&amp;quot;, # 阈值型信号函数&#10; arguments = list(&#10; column = &amp;quot;signal.MACD.ind&amp;quot;, # 作用于MACD指标的信号线列&#10; relationship = &amp;quot;gt&amp;quot;, # 大于（greater than）&#10; threshold = 0, # 阈值为0（零轴）&#10; cross = TRUE # 必须穿越阈值（避免持续触发）&#10; ),&#10; label = &amp;quot;signal.gt.zero&amp;quot; # 信号标签：上穿零轴&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 信号2：信号线下穿零轴（小于零且交叉），视为卖出信号&#10;add.signal(&#10; strat.st,&#10; name = &amp;quot;sigThreshold&amp;quot;,&#10; arguments = list(&#10; column = &amp;quot;signal.MACD.ind&amp;quot;,&#10; relationship = &amp;quot;lt&amp;quot;, # 小于（less than）&#10; threshold = 0,&#10; cross = TRUE&#10; ),&#10; label = &amp;quot;signal.lt.zero&amp;quot; # 信号标签：下穿零轴&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义交易规则：将信号转化为具体交易动作&#10;# 入场规则：当出现上穿零轴信号时，买入（做多）&#10;add.rule(&#10; strat.st,&#10; name = &amp;#39;ruleSignal&amp;#39;, # 基于信号的规则函数&#10; arguments = list(&#10; sigcol = &amp;quot;signal.gt.zero&amp;quot;, # 触发信号列：上穿零轴&#10; sigval = TRUE, # 信号值为TRUE时触发&#10; orderqty = 1000000, # 订单数量（此处为大额，实际会由osMaxPos限制）&#10; ordertype = &amp;#39;market&amp;#39;, # 市价单&#10; orderside = &amp;#39;long&amp;#39;, # 做多方向&#10; threshold = NULL,&#10; osFUN = &amp;#39;osMaxPos&amp;#39; # 使用最大仓位限制函数，避免超仓&#10; ),&#10; type = &amp;#39;enter&amp;#39;, # 入场规则&#10; label = &amp;#39;enter&amp;#39;&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 出场规则：当出现下穿零轴信号时，平仓（卖出所有持仓）&#10;add.rule(&#10; strat.st,&#10; name = &amp;#39;ruleSignal&amp;#39;,&#10; arguments = list(&#10; sigcol = &amp;quot;signal.lt.zero&amp;quot;, # 触发信号列：下穿零轴&#10; sigval = TRUE,&#10; orderqty = &amp;#39;all&amp;#39;, # 平仓全部持仓&#10; ordertype = &amp;#39;market&amp;#39;,&#10; orderside = &amp;#39;long&amp;#39;,&#10; threshold = NULL,&#10; orderset = &amp;#39;exit2&amp;#39; # 订单集标签，用于管理出场订单&#10; ),&#10; type = &amp;#39;exit&amp;#39;, # 出场规则&#10; label = &amp;#39;exit&amp;#39;&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 再平衡规则：按月调整仓位，控制单只标的的资金占比&#10;add.rule(&#10; strat.st,&#10; &amp;#39;rulePctEquity&amp;#39;, # 基于权益比例的再平衡函数&#10; arguments = list(&#10; rebalance_on = &amp;#39;months&amp;#39;, # 再平衡周期：每月&#10; trade.percent = .02, # 单只标的持仓占总权益的比例（2%）&#10; refprice = quote(last(getPrice(mktdata)[paste(&amp;#39;::&amp;#39;, curIndex, sep=&amp;#39;&amp;#39;)])), # 参考价格为最新价格&#10; digits = 0 # 订单数量取整&#10; ),&#10; type = &amp;#39;rebalance&amp;#39;, # 再平衡规则&#10; label = &amp;#39;rebalance&amp;#39;&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 获取历史数据：从雅虎财经下载AAPL的历史数据（起始日期为startDate）&#10;getSymbols(stock.str, from=startDate, src=&amp;#39;yahoo&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 执行策略回测：应用再平衡策略&#10;start_t &amp;lt;- Sys.time() # 记录回测开始时间&#10;out &amp;lt;- applyStrategy.rebalancing(&#10; strat.st, &#10; portfolios = portfolio.st,&#10; parameters = list(nFast=fastMA, nSlow=slowMA, nSig=signalMA, maType=maType), # 传递MACD参数&#10; verbose = TRUE # 输出详细过程&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 6619 @ 3.19964289665222&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -6619 @ 4.35928583145142&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 4075 @ 4.88428592681885&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -4075 @ 5.70142889022827&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 4529 @ 5.125&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -4529 @ 6.00928592681885&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 3576 @ 6.28000020980835&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -3576 @ 5.41714286804199&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 6287 @ 3.56142902374268&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -6287 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 6330 @ 3.62928605079651&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -6330 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 2892 @ 7.46785688400269&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -2892 @ 7.11535692214966&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 2819 @ 7.52535676956177&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -2819 @ 8.92500019073486&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 2251 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -2251 @ 8.99892902374268&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 2380 @ 9.65071392059326&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -2380 @ 12.1857137680054&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 1662 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -1662 @ 11.9428567886353&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 1732 @ 12.6428565979004&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -1732 @ 13.7417860031128&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 1524 @ 14.518928527832&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -1524 @ 20.3278560638428&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 1017 @ 20.9192867279053&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -1017 @ 22.4896430969238&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 1329 @ 16.3132133483887&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -1329 @ 15.1071434020996&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 1481 @ 15.7496433258057&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -1481 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 1053 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -1053 @ 17.8839282989502&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 1121 @ 19.2778568267822&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -1121 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 1099 @ 21.1546421051025&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-10-17 00:00:00 AAPL -1099 @ 24.4174995422363&amp;quot;&#10;## [1] &amp;quot;2014-10-27 00:00:00 AAPL 839 @ 26.2775001525879&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2015-01-06 00:00:00 AAPL -839 @ 26.5650005340576&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2015-02-02 00:00:00 AAPL 721 @ 29.6574993133545&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2015-06-19 00:00:00 AAPL -721 @ 31.6499996185303&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2015-10-27 00:00:00 AAPL 767 @ 28.6375007629395&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2015-12-16 00:00:00 AAPL -767 @ 27.8349990844727&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2016-03-09 00:00:00 AAPL 874 @ 25.2800006866455&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2016-05-02 00:00:00 AAPL -874 @ 23.4099998474121&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2016-07-20 00:00:00 AAPL 883 @ 24.9899997711182&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2016-11-09 00:00:00 AAPL -883 @ 27.7199993133545&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2016-12-15 00:00:00 AAPL 766 @ 28.9549999237061&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-06-20 00:00:00 AAPL -766 @ 36.252498626709&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-07-25 00:00:00 AAPL 591 @ 38.185001373291&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-09-27 00:00:00 AAPL -591 @ 38.5574989318848&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-10-24 00:00:00 AAPL 552 @ 39.2750015258789&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-02-02 00:00:00 AAPL -552 @ 40.125&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-03-01 00:00:00 AAPL 478 @ 43.75&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-04-02 00:00:00 AAPL -478 @ 41.6699981689453&amp;quot;&#10;## [1] &amp;quot;2018-04-19 00:00:00 AAPL 507 @ 43.2000007629395&amp;quot;&#10;## [1] &amp;quot;2018-04-25 00:00:00 AAPL -507 @ 40.9124984741211&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-05-09 00:00:00 AAPL 514 @ 46.8400001525879&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-07-06 00:00:00 AAPL -514 @ 46.9925003051758&amp;quot;&#10;## [1] &amp;quot;2018-07-11 00:00:00 AAPL 459 @ 46.9700012207031&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-10-29 00:00:00 AAPL -459 @ 53.060001373291&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2019-02-08 00:00:00 AAPL 512 @ 42.6025009155273&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2019-05-21 00:00:00 AAPL -512 @ 46.6500015258789&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2019-06-21 00:00:00 AAPL 487 @ 49.6949996948242&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-03-02 00:00:00 AAPL -487 @ 74.7024993896484&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-04-23 00:00:00 AAPL 339 @ 68.7574996948242&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-09-25 00:00:00 AAPL -339 @ 112.279998779297&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-10-13 00:00:00 AAPL 189 @ 121.099998474121&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-11-03 00:00:00 AAPL -189 @ 110.440002441406&amp;quot;&#10;## [1] &amp;quot;2020-11-16 00:00:00 AAPL 201 @ 120.300003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-02-25 00:00:00 AAPL -201 @ 120.98999786377&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-04-14 00:00:00 AAPL 179 @ 132.029998779297&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-05-18 00:00:00 AAPL -179 @ 124.849998474121&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-06-21 00:00:00 AAPL 175 @ 132.300003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-09-27 00:00:00 AAPL -175 @ 145.369995117188&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2021-10-27 00:00:00 AAPL 154 @ 148.850006103516&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-01-26 00:00:00 AAPL -154 @ 159.690002441406&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-02-10 00:00:00 AAPL 125 @ 172.119995117188&amp;quot;&#10;## [1] &amp;quot;2022-02-24 00:00:00 AAPL -125 @ 162.740005493164&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-03-30 00:00:00 AAPL 132 @ 177.770004272461&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-04-26 00:00:00 AAPL -132 @ 156.800003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-07-19 00:00:00 AAPL 160 @ 151&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-09-13 00:00:00 AAPL -160 @ 153.839996337891&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-11-23 00:00:00 AAPL 142 @ 151.070007324219&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-12-08 00:00:00 AAPL -142 @ 142.649993896484&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2023-01-30 00:00:00 AAPL 168 @ 143&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2023-08-14 00:00:00 AAPL -168 @ 179.460006713867&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2023-11-13 00:00:00 AAPL 128 @ 184.800003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-01-10 00:00:00 AAPL -128 @ 186.190002441406&amp;quot;&#10;## [1] &amp;quot;2024-01-30 00:00:00 AAPL 114 @ 188.039993286133&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-02-06 00:00:00 AAPL -114 @ 189.300003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-05-08 00:00:00 AAPL 129 @ 182.740005493164&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-08-14 00:00:00 AAPL -129 @ 221.720001220703&amp;quot;&#10;## [1] &amp;quot;2024-08-16 00:00:00 AAPL 99 @ 226.050003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-11-11 00:00:00 AAPL -99 @ 224.229995727539&amp;quot;&#10;## [1] &amp;quot;2024-11-27 00:00:00 AAPL 97 @ 234.929992675781&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2025-01-21 00:00:00 AAPL -97 @ 222.639999389648&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2025-02-24 00:00:00 AAPL 93 @ 247.100006103516&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2025-03-13 00:00:00 AAPL -93 @ 209.679992675781&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2025-07-07 00:00:00 AAPL 107 @ 209.949996948242&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t &amp;lt;- Sys.time() # 记录回测结束时间&#10;print(end_t - start_t) # 打印回测耗时&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 8.527371 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 更新组合数据：将交易记录同步到组合与账户&#10;start_t &amp;lt;- Sys.time()&#10;updatePortf(&#10; Portfolio = portfolio.st,&#10; Dates = paste(&amp;#39;::&amp;#39;, as.Date(Sys.time()), sep=&amp;#39;&amp;#39;) # 更新至当前日期&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()&#10;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(end_t - start_t) # 打印更新耗时&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 0.03819585 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化回测结果：绘制持仓变化与MACD指标&#10;chart.Posn(Portfolio = portfolio.st, Symbol = stock.str) # 展示AAPL的持仓变化&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/07/10/macdrebalancing/index_files/figure-html/SimpleMACD-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA, maType=&amp;quot;EMA&amp;quot;)) # 叠加MACD指标（快速EMA=12，慢速EMA=26，信号线=9）&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/07/10/macdrebalancing/index_files/figure-html/SimpleMACD-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 查看订单簿：获取所有订单的详细记录（用于调试与分析）&#10;getOrderBook(&amp;#39;macd&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## $macd&#10;## $macd$AAPL&#10;## Order.Qty Order.Price Order.Type Order.Side Order.Threshold&#10;## 2007-03-15 6619 3.19892907142639 market long NA&#10;## 2007-08-16 all 4.18035697937012 market long NA&#10;## 2007-09-04 4075 5.1485710144043 market long NA&#10;## 2008-01-15 all 6.03714323043823 market long NA&#10;## 2008-03-28 4529 5.10750007629395 market long NA&#10;## 2008-06-25 all 6.33535718917847 market long NA&#10;## 2008-08-19 3576 6.19750022888184 market long NA&#10;## 2008-09-08 all 5.6399998664856 market long NA&#10;## 2009-02-05 6287 3.4449999332428 market long NA&#10;## 2009-03-03 all 3.15607094764709 market long NA&#10;## ... &#10;## 2024-02-05 all 187.679992675781 market long NA&#10;## 2024-05-07 129 182.399993896484 market long NA&#10;## 2024-08-13 all 221.270004272461 market long NA&#10;## 2024-08-15 99 224.720001220703 market long NA&#10;## 2024-11-08 all 226.960006713867 market long NA&#10;## 2024-11-26 97 235.059997558594 market long NA&#10;## 2025-01-17 all 229.979995727539 market long NA&#10;## 2025-02-21 93 245.550003051758 market long NA&#10;## 2025-03-12 all 216.979995727539 market long NA&#10;## 2025-07-03 107 213.550003051758 market long NA&#10;## Order.Status Order.StatusTime Prefer Order.Set Txn.Fees Rule&#10;## 2007-03-15 closed 2007-03-16 00:00:00 NA 0 enter&#10;## 2007-08-16 closed 2007-08-17 00:00:00 exit2 0 exit &#10;## 2007-09-04 closed 2007-09-05 00:00:00 NA 0 enter&#10;## 2008-01-15 closed 2008-01-16 00:00:00 exit2 0 exit &#10;## 2008-03-28 closed 2008-03-31 00:00:00 NA 0 enter&#10;## 2008-06-25 closed 2008-06-26 00:00:00 exit2 0 exit &#10;## 2008-08-19 closed 2008-08-20 00:00:00 NA 0 enter&#10;## 2008-09-08 closed 2008-09-09 00:00:00 exit2 0 exit &#10;## 2009-02-05 closed 2009-02-06 00:00:00 NA 0 enter&#10;## 2009-03-03 closed 2009-03-04 00:00:00 exit2 0 exit &#10;## ... &#10;## 2024-02-05 closed 2024-02-06 00:00:00 exit2 0 exit &#10;## 2024-05-07 closed 2024-05-08 00:00:00 NA 0 enter&#10;## 2024-08-13 closed 2024-08-14 00:00:00 exit2 0 exit &#10;## 2024-08-15 closed 2024-08-16 00:00:00 NA 0 enter&#10;## 2024-11-08 closed 2024-11-11 00:00:00 exit2 0 exit &#10;## 2024-11-26 closed 2024-11-27 00:00:00 NA 0 enter&#10;## 2025-01-17 closed 2025-01-21 00:00:00 exit2 0 exit &#10;## 2025-02-21 closed 2025-02-24 00:00:00 NA 0 enter&#10;## 2025-03-12 closed 2025-03-13 00:00:00 exit2 0 exit &#10;## 2025-07-03 closed 2025-07-07 00:00:00 NA 0 enter&#10;## Time.In.Force&#10;## 2007-03-15 &#10;## 2007-08-16 &#10;## 2007-09-04 &#10;## 2008-01-15 &#10;## 2008-03-28 &#10;## 2008-06-25 &#10;## 2008-08-19 &#10;## 2008-09-08 &#10;## 2009-02-05 &#10;## 2009-03-03 &#10;## ... &#10;## 2024-02-05 &#10;## 2024-05-07 &#10;## 2024-08-13 &#10;## 2024-08-15 &#10;## 2024-11-08 &#10;## 2024-11-26 &#10;## 2025-01-17 &#10;## 2025-02-21 &#10;## 2025-03-12 &#10;## 2025-07-03 &#10;## &#10;## &#10;## attr(,&amp;quot;class&amp;quot;)&#10;## [1] &amp;quot;order_book&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略核心思路" class="section level1"&gt;&#10;&lt;h1&gt;策略核心思路&lt;/h1&gt;&#10;&lt;p&gt;该策略的实现遵循量化交易的经典流程，从环境初始化到信号生成，再到交易执行与结果可视化，各环节紧密衔接，核心思路可总结为以下几点：&lt;/p&gt;&#10;&lt;div id="环境与参数的标准化搭建" class="section level2"&gt;&#10;&lt;h2&gt;环境与参数的标准化搭建：&lt;/h2&gt;&#10;&lt;p&gt;代码首先清理旧有策略数据，避免残留信息干扰回测；随后定义交易标的（AAPL）、货币单位（USD）及初始资金（100 万美元），通过initPortf、initAcct等函数初始化组合、账户与订单簿，为策略运行创建纯净的环境。同时，设定 MACD 的核心参数（快速 EMA=12、慢速 EMA=26、信号线 = 9），这些参数是指标计算的基础，也是后续策略优化的关键变量。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="指标与信号的逻辑闭环" class="section level2"&gt;&#10;&lt;h2&gt;指标与信号的逻辑闭环：&lt;/h2&gt;&#10;&lt;p&gt;策略通过add.indicator添加 MACD 指标，基于收盘价计算 MACD 线与信号线；再通过add.signal定义信号规则 —— 当信号线从下方穿越零轴（signal.gt.zero）时，认为多头趋势确立，生成买入信号；当信号线从上方穿越零轴（signal.lt.zero）时，认为趋势转弱，生成卖出信号。这种 “指标计算→信号触发” 的逻辑，是趋势跟踪策略的核心。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="交易规则的风险控制" class="section level2"&gt;&#10;&lt;h2&gt;交易规则的风险控制：&lt;/h2&gt;&#10;&lt;p&gt;为避免盲目交易，策略通过多重规则控制风险：&#10;入场时使用osMaxPos函数限制最大仓位，防止单次下单量过大；&#10;出场时采用 “平仓全部持仓”（orderqty=‘all’），确保趋势反转时及时离场；&#10;每月再平衡（rulePctEquity）控制单只标的的持仓占比（2%），避免过度集中风险。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="回测与可视化的结果验证" class="section level2"&gt;&#10;&lt;h2&gt;回测与可视化的结果验证：&lt;/h2&gt;&#10;&lt;p&gt;策略通过applyStrategy.rebalancing执行回测，记录耗时以评估效率；随后更新组合数据，确保交易记录与账户权益同步；最后通过chart.Posn与add_MACD可视化持仓变化与指标走势，直观展示交易时机与 MACD 信号的对应关系，为策略分析提供依据。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略的优势与局限性" class="section level1"&gt;&#10;&lt;h1&gt;策略的优势与局限性&lt;/h1&gt;&#10;&lt;p&gt;该 MACD 趋势跟踪策略的优势在于逻辑清晰、实现简单，且具备明确的风险控制机制：&#10;- 基于信号线与零轴的交叉信号，避免了主观判断，规则可量化、可复现；&#10;- 再平衡机制与仓位限制有效控制了单一标的的风险暴露，适合资金管理；&#10;- 兼容量化回测框架，便于后续参数优化（如调整 EMA 周期）或策略扩展（如添加止损）。&lt;/p&gt;&#10;&lt;p&gt;但策略也存在趋势跟踪类策略的共性局限：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;滞后性：EMA 基于历史价格计算，信号可能滞后于实际趋势转折，导致入场过晚或出场延迟；&lt;/li&gt;&#10;&lt;li&gt;震荡市失效：在横盘震荡行情中，信号线可能频繁穿越零轴，产生大量虚假信号，导致频繁交易与成本累积；&lt;/li&gt;&#10;&lt;li&gt;参数敏感性：过度依赖传统参数（12/26/9）可能无法适配不同市场环境，需结合标的特性优化。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>基于双均线交叉的交易策略实现与回测分析</title><link>https://gewutang.cn/2017/05/12/macross/</link><pubDate>Fri, 12 May 2017 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2017/05/12/macross/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易领域，移动均线交叉策略是一种被广泛应用的趋势跟踪方法。本&#10;文将解析一段基于 R 语言 quantstrat 包的简单移动均线交叉策略代码，展示其实现思路、核心逻辑及回测分析过程。&lt;/p&gt;&#10;&lt;div id="策略核心思想" class="section level1"&gt;&#10;&lt;h1&gt;策略核心思想&lt;/h1&gt;&#10;&lt;p&gt;这段代码实现了一个经典的双均线交叉策略，其基本逻辑是通过比较短期均线与长期均线的相对位置来生成交易信号： - 当短期均线（50 日 SMA）上穿长期均线（200 日 SMA）时，产生买入信号 - 当短期均线（50 日 SMA）下穿长期均线（200 日 SMA）时，产生卖出信号&lt;/p&gt;&#10;&lt;p&gt;策略默认仅做多，代码中也预留了做空逻辑的实现方式&lt;/p&gt;&#10;&lt;p&gt;这种策略的理论基础在于，短期均线反映近期价格趋势，长期均线反映长期价格趋势，当短期均线从下方穿越长期均线时，通常被视为上升趋势的开始；反之则被视为下降趋势的开始。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略实现的关键步骤" class="section level1"&gt;&#10;&lt;h1&gt;策略实现的关键步骤&lt;/h1&gt;&#10;&lt;p&gt;下面是代码的关键实现部分及解析：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要的包&#10;require(quantstrat)&#10;&#10;# 环境初始化&#10;ttz&amp;lt;-Sys.getenv(&amp;#39;TZ&amp;#39;)&#10;Sys.setenv(TZ=&amp;#39;UTC&amp;#39;) # 时区设置，避免日期处理问题&#10;&#10;# 清理旧策略数据&#10;suppressWarnings(rm(&amp;quot;order_book.macross&amp;quot;,pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.macross&amp;quot;,&amp;quot;portfolio.macross&amp;quot;,pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;account.st&amp;quot;,&amp;quot;portfolio.st&amp;quot;,&amp;quot;stock.str&amp;quot;,&amp;quot;stratMACROSS&amp;quot;,&amp;#39;start_t&amp;#39;,&amp;#39;end_t&amp;#39;))&#10;&#10;# 设置交易标的和货币&#10;stock.str=&amp;#39;AAPL&amp;#39; # 以苹果公司股票为例&#10;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;stock(stock.str,currency=&amp;#39;USD&amp;#39;,multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 初始化回测参数&#10;startDate=&amp;quot;1999-12-31&amp;quot; # 回测起始日期&#10;initEq=1000000 # 初始资金100万美元&#10;portfolio.st=&amp;#39;macross&amp;#39; # 投资组合名称&#10;account.st=&amp;#39;macross&amp;#39; # 账户名称&#10;&#10;# 初始化投资组合、账户和订单簿&#10;initPortf(portfolio.st,symbols=stock.str)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initAcct(account.st,portfolios=portfolio.st, initEq=initEq)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initOrders(portfolio=portfolio.st)&#10;&#10;# 创建策略对象&#10;stratMACROSS&amp;lt;- strategy(portfolio.st)&#10;&#10;# 添加技术指标：50日和200日简单移动平均线&#10;stratMACROSS &amp;lt;- add.indicator(strategy = stratMACROSS, name = &amp;quot;SMA&amp;quot;, &#10; arguments = list(x=quote(Cl(mktdata)), n=50),&#10; label= &amp;quot;ma50&amp;quot; )&#10;stratMACROSS &amp;lt;- add.indicator(strategy = stratMACROSS, name = &amp;quot;SMA&amp;quot;, &#10; arguments = list(x=quote(Cl(mktdata)[,1]), n=200),&#10; label= &amp;quot;ma200&amp;quot;)&#10;&#10;# 添加交易信号：均线交叉信号&#10;stratMACROSS &amp;lt;- add.signal(strategy = stratMACROSS, name=&amp;quot;sigCrossover&amp;quot;,&#10; arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;), relationship=&amp;quot;gte&amp;quot;),&#10; label=&amp;quot;ma50.gt.ma200&amp;quot;) # 短期均线上穿长期均线&#10;stratMACROSS &amp;lt;- add.signal(strategy = stratMACROSS, name=&amp;quot;sigCrossover&amp;quot;,&#10; arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;), relationship=&amp;quot;lt&amp;quot;),&#10; label=&amp;quot;ma50.lt.ma200&amp;quot;) # 短期均线下穿长期均线&#10;&#10;# 添加交易规则：基于信号的交易执行&#10;stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments = list(sigcol=&amp;quot;ma50.gt.ma200&amp;quot;, sigval=TRUE, &#10; orderqty=100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;long&amp;#39;),&#10; type=&amp;#39;enter&amp;#39;) # 买入规则&#10;stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments = list(sigcol=&amp;quot;ma50.lt.ma200&amp;quot;, sigval=TRUE, &#10; orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;long&amp;#39;),&#10; type=&amp;#39;exit&amp;#39;) # 卖出规则&#10;&#10;# 预留的做空策略代码（默认未启用）&#10;# stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#10;# arguments = list(sigcol=&amp;quot;ma50.lt.ma200&amp;quot;, sigval=TRUE, &#10;# orderqty=-100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;short&amp;#39;),&#10;# type=&amp;#39;enter&amp;#39;)&#10;# stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#10;# arguments = list(sigcol=&amp;quot;ma50.gt.ma200&amp;quot;, sigval=TRUE, &#10;# orderqty=100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;short&amp;#39;),&#10;# type=&amp;#39;exit&amp;#39;)&#10;&#10;# 获取历史数据并进行复权处理&#10;getSymbols(stock.str, from=startDate, src=&amp;#39;yahoo&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;for(i in stock.str)&#10; assign(i, adjustOHLC(get(i), use.Adjusted=TRUE))&#10;&#10;# 执行策略回测&#10;start_t&amp;lt;-Sys.time()&#10;out&amp;lt;-applyStrategy(strategy=stratMACROSS, portfolios=portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2001-06-27 00:00:00 AAPL 100 @ 0.350732564926147&amp;quot;&#10;## [1] &amp;quot;2001-09-07 00:00:00 AAPL -100 @ 0.259667813777924&amp;quot;&#10;## [1] &amp;quot;2002-01-07 00:00:00 AAPL 100 @ 0.344120860099792&amp;quot;&#10;## [1] &amp;quot;2002-07-10 00:00:00 AAPL -100 @ 0.260269552469254&amp;quot;&#10;## [1] &amp;quot;2003-05-16 00:00:00 AAPL 100 @ 0.28250914812088&amp;quot;&#10;## [1] &amp;quot;2006-06-22 00:00:00 AAPL -100 @ 1.79062843322754&amp;quot;&#10;## [1] &amp;quot;2006-09-26 00:00:00 AAPL 100 @ 2.33250570297241&amp;quot;&#10;## [1] &amp;quot;2008-03-07 00:00:00 AAPL -100 @ 3.67412352561951&amp;quot;&#10;## [1] &amp;quot;2008-05-19 00:00:00 AAPL 100 @ 5.51794862747192&amp;quot;&#10;## [1] &amp;quot;2008-09-24 00:00:00 AAPL -100 @ 3.8682746887207&amp;quot;&#10;## [1] &amp;quot;2009-05-14 00:00:00 AAPL 100 @ 3.69516229629517&amp;quot;&#10;## [1] &amp;quot;2012-12-11 00:00:00 AAPL -100 @ 16.41552734375&amp;quot;&#10;## [1] &amp;quot;2013-09-11 00:00:00 AAPL 100 @ 14.4533443450928&amp;quot;&#10;## [1] &amp;quot;2015-08-31 00:00:00 AAPL -100 @ 25.3688125610352&amp;quot;&#10;## [1] &amp;quot;2016-08-31 00:00:00 AAPL 100 @ 24.3808135986328&amp;quot;&#10;## [1] &amp;quot;2018-12-24 00:00:00 AAPL -100 @ 34.9761238098145&amp;quot;&#10;## [1] &amp;quot;2019-05-07 00:00:00 AAPL 100 @ 48.5301742553711&amp;quot;&#10;## [1] &amp;quot;2022-06-06 00:00:00 AAPL -100 @ 143.832458496094&amp;quot;&#10;## [1] &amp;quot;2022-09-27 00:00:00 AAPL 100 @ 149.571166992188&amp;quot;&#10;## [1] &amp;quot;2022-10-10 00:00:00 AAPL -100 @ 138.394744873047&amp;quot;&#10;## [1] &amp;quot;2023-03-23 00:00:00 AAPL 100 @ 157.137161254883&amp;quot;&#10;## [1] &amp;quot;2024-03-15 00:00:00 AAPL -100 @ 171.583724975586&amp;quot;&#10;## [1] &amp;quot;2024-06-14 00:00:00 AAPL 100 @ 211.500885009766&amp;quot;&#10;## [1] &amp;quot;2025-04-08 00:00:00 AAPL -100 @ 172.194198608398&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t&amp;lt;-Sys.time()&#10;print(end_t-start_t) # 输出回测执行时间&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 0.2876902 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 更新投资组合数据&#10;start_t&amp;lt;-Sys.time()&#10;updatePortf(Portfolio=&amp;#39;macross&amp;#39;, Dates=paste(&amp;#39;::&amp;#39;, as.Date(Sys.time()), sep=&amp;#39;&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t&amp;lt;-Sys.time()&#10;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(end_t-start_t)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 0.04611683 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化回测结果&#10;chart.Posn(Portfolio=&amp;#39;macross&amp;#39;, Symbol=stock.str)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/05/12/macross/index_files/figure-html/DualSMA-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;add_SMA(n=50, on=1, col=&amp;#39;blue&amp;#39;) # 叠加50日均线（蓝色）&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/05/12/macross/index_files/figure-html/DualSMA-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;add_SMA(n=200, on=1) # 叠加200日均线（默认红色）&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2017/05/12/macross/index_files/figure-html/DualSMA-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 获取交易统计数据&#10;book = getOrderBook(&amp;#39;macross&amp;#39;) # 订单簿&#10;stats = tradeStats(&amp;#39;macross&amp;#39;) # 交易统计&#10;ptstats = perTradeStats(&amp;#39;macross&amp;#39;) # 每笔交易统计&#10;rets = PortfReturns(&amp;#39;macross&amp;#39;) # 组合收益&#10;txns = getTxns(&amp;#39;macross&amp;#39;, stock.str) # 交易记录&#10;&#10;# 恢复时区设置&#10;Sys.setenv(TZ=ttz) &lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;策略的核心组件涵盖了从环境搭建到结果分析的完整流程。在&#10;环境初始化阶段，首先通过设置时区避免日期处理过程中可能出现的问题，同时清理旧有的策略数据，确保回测环境的纯净性，为后续分析排除干扰。在&#10;此基础上，明确交易标的为 AAPL，并设定货币单位为 USD，为策略运行奠定基础。&lt;/p&gt;&#10;&lt;p&gt;指标与信号系统是策略的核心判断依据。通&#10;过 add.indicator 函数添加了 50 日和 200 日两条简单移动平均线（SMA），前者反映短期价格趋势，后者代表中长期趋势。随&#10;后，借助 add.signal 函数定义了均线交叉信号：当 50 日 SMA 上穿 200 日 SMA 时，生成买入信号；当 50 日 SMA 下穿 200 日 SMA 时，触发卖出信号，这一逻辑构成了策略的决策核心。&lt;/p&gt;&#10;&lt;p&gt;交易规则则将信号转化为具体操作。买&#10;入规则设定为当 50 日 SMA 上穿 200 日 SMA 时，买入 100 股；卖出规则则在 50 日 SMA 下穿 200 日 SMA 时，卖出全部持仓，实现趋势跟踪的闭环。此&#10;外，代码中还预留了做空规则，虽未启用，但为策略扩展至双向交易提供了可能，用户可根据需求取消注释启用。&lt;/p&gt;&#10;&lt;p&gt;数据处理与回测执行环节确保了策略的可操作性。从&#10;Yahoo Finance 获取 AAPL 的历史数据后，进行复权处理以保证价格的准确性。通&#10;过 applyStrategy 函数执行回测，并记录执行时间，便于评估策略效率；随后更新投资组合数据，为绩效指标的计算提供支持。&#10;结果可视化与分析则让策略效果更加直观。利&#10;用 chart.Posn 绘制持仓变化图，并叠加 50 日和 200 日均线，清晰展示交易时机与价格趋势的关系。同&#10;时，通过获取订单簿、交易统计、每笔交易详情及组合收益等数据，为深入分析策略表现提供了全面依据。&lt;/p&gt;&#10;&lt;p&gt;这种双均线交叉策略的意义显著：它不仅实现了趋势跟踪的基本逻辑，能够捕捉中长期价格趋势，还提供了一套完整的量化交易框架，涵盖数据获取、信号生成、订单执行到绩效评估的全流程。代&#10;码结构清晰，便于修改和扩展，例如调整均线周期或添加其他指标以优化策略。&lt;/p&gt;&#10;&lt;p&gt;然而，该策略也存在明显局限性。在&#10;横盘震荡市场中，均线频繁交叉易产生虚假信号，导致频繁交易和不必要的亏损；固定的 100 股交易数量缺乏灵活的资金管理和风险控制机制；仅依赖收盘价计算均线，可能忽略日内价格波动中蕴含的信息；且未考虑交易成本和滑点，实际应用中的表现可能低于回测结果。&lt;/p&gt;&#10;&lt;p&gt;对于希望优化策略的交易者，可从多方面入手：调整均线周期参数以适应不同市场环境；引入交易量或其他技术指标过滤虚假信号，提高信号质量；实现动态仓位管理，根据市场波动性调整头寸大小，平衡风险与收益；增加止损和止盈机制，控制单笔交易的潜在风险。这&#10;种基于技术指标的趋势跟踪策略是量化交易的基础，理解其实现原理和优缺点，能为构建更复杂、更有效的交易系统提供重要参考。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>使用blotter包绘制最大不利偏移（MAE）图</title><link>https://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/</link><pubDate>Wed, 10 Feb 2016 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/</guid><description>&#10;&#10;&#10;&lt;p&gt;在交易过程中，MAE（最大不利偏移）是衡量风险的关键指标，它反映了从开仓到平仓期间账户价值相对于初始水平的最大回撤幅度。这一指标对于交易者而言具有重要意义：首先，它能直观呈现单笔交易可能面临的极端亏损风险，帮助交易者提前做好风险预判；其次，在止损策略优化方面，可将 MAE 的统计数据作为重要参考，例如将止损位设定在历史 MAE 均值的合理倍数之上，既能避免过早止损，又能有效控制亏损；最后，通过观察 MAE 的波动情况，还能评估交易系统的稳定性 —— 通常而言，MAE 曲线越平滑，说明系统在不同市场环境下的表现越稳定，抗风险能力越强。&lt;/p&gt;&#10;&lt;p&gt;Jaekle 和 Tamasini 在《A new approach to system development and portfolio optimisation》一书中，通过 Luxor 系统的实证分析进一步印证了 MAE 的应用价值。书中，作者通过绘制该系统的 MAE 图表，清晰展示了策略在不同市场周期中的风险暴露特征，为读者提供了一个将理论指标转化为实践分析的典型案例，这种可视化的呈现方式有助于交易者更直观地理解风险分布规律，进而优化自身的交易决策体系。&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;R&lt;/code&gt; 可以便捷绘制 MAE 图：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载 blotter 包 - 用于交易系统分析和绩效评估&#10;require(&amp;#39;blotter&amp;#39;)&#10;&#10;# 加载 quantstrat 包中的示例数据 &amp;#39;luxor-p066&amp;#39;（这是一个交易策略的历史回测结果）&#10;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#10;&#10;# 设置货币和汇率信息（GBP 代表英镑，USD 代表美元）&#10;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;exchange_rate(c(&amp;#39;GBPUSD&amp;#39;), tick_size=0.0001) # 设定 GBP/USD 汇率的最小变动单位&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绘制最大不利偏移（MAE）图表&#10;chart.ME(&#10; Portfolio=&amp;#39;luxor&amp;#39;, # 指定要分析的投资组合名称为 &amp;#39;luxor&amp;#39;&#10; Symbol=&amp;#39;GBPUSD&amp;#39;, # 指定要分析的交易品种为 GBP/USD&#10; type=&amp;#39;MAE&amp;#39;, # 选择绘制 MAE（Maximum Adverse Excursion）图表&#10; scale=&amp;#39;cash&amp;#39; # 以货币金额（现金）为单位显示结果&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;div class="figure"&gt;&lt;span style="display:block;" id="fig:MAE"&gt;&lt;/span&gt;&#10;&lt;img src="https://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/index_files/figure-html/MAE-1.png" alt="luxor 系统的 MAE 图" width="672" /&gt;&#10;&lt;p class="caption"&gt;&#10;Figure 1: luxor 系统的 MAE 图&#10;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;MAE 图表在交易系统评估中具有重要价值，能够帮助交易者识别策略风险点、优化资金管理并提升整体绩效。通过监测 MAE 曲线的波动，交易者可以及时发现策略在特定市场条件下的脆弱性，例如 MAE 突然增大的时期往往暗示策略对市场变化的适应性不足，需要调整入场或出场逻辑。在头寸管理方面，基于历史 MAE 数据可以精确计算每笔交易的最优仓位，避免因过度杠杆导致的风险暴露。此外，对比不同策略的 MAE 分布特征，能够直观地评估各策略的风险收益特性，为组合配置提供数据支持。&lt;/p&gt;&#10;&lt;p&gt;在实际应用中，MAE 分析可覆盖交易全周期。在风险控制环节，实时监控单笔交易的 MAE 水平，当接近或突破预设风险阈值时，及时采取减仓或止损措施，防止亏损扩大。策略优化阶段，通过关联 MAE 峰值与市场状态，可针对性地调整交易参数，例如优化止损位置或过滤特定市场环境下的信号。资金管理方面，结合历史 MAE 标准差与预期收益，可建立动态仓位模型，确保在不同市场波动性下保持稳定的风险敞口。这种系统化的应用方式使 MAE 不仅成为风险衡量工具，更成为策略迭代与资本配置的核心决策依据。&lt;/p&gt;&#10;</description></item><item><title>交易策略的 MFE 分析与可视化：评估盈利潜力的关键工具</title><link>https://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/</link><pubDate>Tue, 12 Jan 2016 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易策略评估中，最大有利波动（MFE, Maximum Favorable Excursion）是衡量单笔交易潜在盈利空间的重要指标。它代表自交易入场后，价格朝着有利方向波动的最大幅度，能直观反映策略在理想情况下的盈利捕捉能力。&lt;/p&gt;&#10;&lt;p&gt;通过分析 MFE，交易者可以评估当前止盈策略是否充分捕获价格波动收益，识别交易时机是否精准，甚至为动态止盈参数的设定提供依据。这段代码正是基于这一原理，通过可视化 GBPUSD 货币对的 MFE 数据，帮助交易者理解 Luxor 策略在不同交易中的盈利潜力。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#10;#&#10;# 图3.16：百分比形式的MFE（最大有利波动）图形&#10;&#10;# 加载blotter包，用于交易组合的记录与绩效分析&#10;require(&amp;#39;blotter&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#10;## method from&#10;## as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#10;## &#10;## legend&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载Luxor策略的交易数据（对应书中第66页的示例）&#10;# envir=.blotter参数将数据加载到blotter包的环境中，便于后续分析&#10;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#10;&#10;# 定义货币单位与汇率属性：设置交易涉及的货币及GBPUSD的最小变动单位&#10;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;)) # 定义英镑和美元为交易货币&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;exchange_rate(c(&amp;#39;GBPUSD&amp;#39;), tick_size=0.0001) # 设置GBPUSD的最小价格变动为0.0001（即1个点）&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绘制MFE图表：以百分比形式展示每笔交易的最大有利波动&#10;chart.ME(&#10; Portfolio=&amp;#39;luxor&amp;#39;, # 指定要分析的投资组合名称&#10; Symbol=&amp;#39;GBPUSD&amp;#39;, # 指定要分析的交易品种（GBPUSD货币对）&#10; type=&amp;#39;MFE&amp;#39;, # 指定图表类型为MFE（最大有利波动）&#10; scale=&amp;#39;percent&amp;#39; # 指定Y轴刻度为百分比形式（相对于入场价格的波动比例）&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/index_files/figure-html/MFE-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;为增强 MFE 分析的实用性，可引入 MFE 与最终收益的对比分析，通过散点图展示每笔交易的 MFE 与实际平仓收益的关系，识别策略在盈利兑现上的效率。可增加按时间周期（如月度、季度）的 MFE 趋势分析，探究市场环境变化对策略盈利潜力的影响。&lt;/p&gt;&#10;&lt;p&gt;在可视化方面，可添加阈值参考线（如 MFE 的 25%、75% 分位数），直观标记优质交易与低效交易的分界线。还可结合最大不利波动（MAE）构建风险收益比矩阵，将每笔交易映射到二维空间中，更全面评估策略的风险收益特征。通过这些改进，MFE 分析将不仅能回顾历史表现，还能为动态止盈策略、仓位管理规则的优化提供数据支持，提升策略的实战效果。&lt;/p&gt;&#10;</description></item><item><title>基于 MFE 指标的交易策略绩效分析：GBP/USD 交易的最大有利波动可视化</title><link>https://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/</link><pubDate>Thu, 10 Dec 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易中，评估单笔交易的潜在盈利空间与风险控制能力是优化策略的重要环节。最大有利波动（MFE，Maximum Favorable Excursion）作为衡量交易过程中价格对持仓最有利波动程度的指标，能够帮助交易者理解策略在盈利阶段的表现 —— 例如，单笔交易曾达到的最大浮盈比例，以及该比例与最终盈利的关系。这段代码的核心思路便是通过可视化 GBPUSD 交易的 MFE 指标（以百分比形式），直观展示 Luxor 策略在交易过程中的潜在盈利空间，为评估策略的止盈设置、盈利捕捉能力提供依据。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#10;#&#10;# 图3.16：百分比形式的MFE（最大有利波动）图形&#10;&#10;# 加载blotter包，用于交易组合的记录与绩效分析&#10;require(&amp;#39;blotter&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#10;## method from&#10;## as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#10;## &#10;## legend&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载Luxor策略的交易数据（对应书中第66页的示例），存储到blotter环境中&#10;# 数据包含GBPUSD的交易记录，如入场时间、价格、持仓变化等&#10;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#10;&#10;# 定义货币单位：GBP（英镑）和USD（美元），因交易标的为GBPUSD（英镑兑美元）&#10;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义汇率属性：GBPUSD的最小变动单位（点差）为0.0001&#10;exchange_rate(c(&amp;#39;GBPUSD&amp;#39;), tick_size=0.0001)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绘制MFE图形（百分比形式）&#10;chart.ME(&#10; Portfolio=&amp;#39;luxor&amp;#39;, # 组合名称：luxor（对应加载的数据）&#10; Symbol=&amp;#39;GBPUSD&amp;#39;, # 交易标的：英镑兑美元&#10; type=&amp;#39;MFE&amp;#39;, # 图形类型：最大有利波动（MFE）&#10; scale=&amp;#39;percent&amp;#39; # 刻度单位：百分比（展示价格波动相对于入场价的百分比）&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;这段代码为 MFE 指标的可视化提供了基础框架，但可从多个维度优化以增强分析深度。可以结合最大不利波动（MAE，Maximum Adverse Excursion）进行对比分析，通过同一图表展示单笔交易的潜在盈利与风险边界，更全面评估策略的风险收益特征。其次，可增加统计维度，例如按交易方向（做多 / 做空）、持仓周期分组展示 MFE 分布，识别策略在不同场景下的盈利捕捉能力差异。&lt;/p&gt;&#10;&lt;p&gt;在可视化层面，可添加参考线（如平均 MFE、中位数 MFE），并允许交互查询（如点击某笔交易显示具体入场时间、价格），使结果更易解读。此外，引入交易成本（如点差、手续费）对 MFE 的影响分析，能让指标更贴近实盘场景，为止盈参数的优化（如是否在 MFE 达到某一比例时部分止盈）提供更精准的依据。通过这些改进，MFE 分析将从单纯的历史回顾升级为策略优化的实用工具，帮助交易者平衡盈利捕捉与风险控制。&lt;/p&gt;&#10;</description></item><item><title>日内时间窗口绩效分析与可视化</title><link>https://gewutang.cn/2015/10/10/luxor-sample-trade-graphs-timespan/</link><pubDate>Sat, 10 Oct 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/10/10/luxor-sample-trade-graphs-timespan/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易中，分析交易策略在不同时间窗口的绩效表现至关重要，因为金融市场具有显著的日内周期性特征。不同时段的市场参与者结构、流动性水平、信息传递效率差异明显，导致同一策略在日内不同时间段的表现可能大相径庭。例如，美股市场开盘后的 30 分钟通常伴随着高波动率和大量订单流，趋势策略可能在此期间更有效；而午盘时段交投清淡，震荡策略或许更适用。通过系统地扫描不同时间窗口的绩效，交易者可以识别策略的 “最佳生效时段”，优化交易执行时间，减少无效信号干扰，提高资金使用效率。&lt;/p&gt;&#10;&lt;p&gt;此外，这种分析还能揭示策略的潜在弱点，例如仅在特定时段有效的策略可能存在过拟合风险，或对特定市场环境过度依赖，为策略的改进与风险控制提供依据。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#10;#&#10;# 3D时间跨度图表示例&#10;&#10;# 加载必要的包：quantstrat用于量化策略分析，rgl用于3D图形绘制&#10;require(quantstrat)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#10;## method from&#10;## as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#10;## &#10;## legend&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;require(rgl)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: rgl&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载Luxor策略的时间跨度数据：包含2002-2008年24x24时间窗口的测试结果&#10;# 数据路径为quantstrat包安装目录下的data文件夹&#10;load(paste0(&#10; path.package(&amp;#39;quantstrat&amp;#39;), # 获取quantstrat包的安装路径&#10; &amp;#39;/data/luxor.timespan.24x24.2002-2008.RData&amp;#39;) # 拼接数据文件完整路径&#10;)&#10;&#10;# 数据预处理：将stats数据框中存储参数的列重命名为&amp;quot;timespan&amp;quot;，便于后续处理&#10;names(stats)[names(stats)==&amp;#39;testPackListPRL[[k]]$parameters&amp;#39;] &amp;lt;- &amp;#39;timespan&amp;#39;&#10;&#10;# 分割timespan列：timespan格式可能为&amp;quot;开始时间/结束时间&amp;quot;，用strsplit分割为列表&#10;stats$tmp = strsplit(as.character(stats$timespan), &amp;#39;/&amp;#39;)&#10;&#10;# 从分割后的列表中提取&amp;quot;from&amp;quot;（开始时间）和&amp;quot;to&amp;quot;（结束时间），存储为新列&#10;stats$from &amp;lt;- sapply(stats$tmp, FUN = &amp;#39;[&amp;#39;, 1) # 提取列表中第一个元素作为开始时间&#10;stats$to &amp;lt;- sapply(stats$tmp, FUN = &amp;#39;[&amp;#39;, 2) # 提取列表中第二个元素作为结束时间&#10;&#10;# 从开始时间和结束时间中提取小时数：假设时间格式含&amp;quot;T小时:分钟&amp;quot;，用gsub匹配并提取小时&#10;stats$start &amp;lt;- as.numeric(gsub(&amp;#39;T([0-9]+):[0-9]+&amp;#39;, x = stats$from, &amp;#39;\\1&amp;#39;)) # 提取开始小时&#10;stats$stop &amp;lt;- (as.numeric(gsub(&amp;#39;T([0-9]+):[0-9]+&amp;#39;, x = stats$to, &amp;#39;\\1&amp;#39;)) + 1) %% 24 # 提取结束小时并调整（+1取整，%%24确保在0-23范围内）&#10;&#10;# 注释部分：交易数据为EST时区（GMT-4），可将小时数加4转换为GMT时区（按需启用）&#10;# stats$start&amp;lt;-(stats$start+4)%%24&#10;# stats$stop&amp;lt;-(stats$stop+4)%%24&#10;&#10;# 生成3D可视化图形：展示不同时间窗口（start和stop）与绩效指标的关系&#10;tradeGraphs(&#10;&#9;stats, # 输入数据框&#10;&#9;free.params = c(&amp;#39;start&amp;#39;, &amp;#39;stop&amp;#39;), # 自由参数：时间窗口的开始和结束小时&#10;&#9;statistics = c(&amp;#39;Net.Trading.PL&amp;#39;, &amp;#39;maxDrawdown&amp;#39;, &amp;quot;Avg.Trade.PL&amp;quot;, &amp;#39;Num.Trades&amp;#39;, &amp;quot;Profit.Factor&amp;quot;), # 需展示的绩效指标：净交易收益、最大回撤、平均每笔收益、交易次数、盈利因子&#10;&#9;title = &amp;#39;Luxor Intraday TimeWindow Scan&amp;#39; # 图形标题：Luxor日内时间窗口扫描&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;这段代码的核心思路是通过量化分析与 3D 可视化，探究 Luxor 日内交易策略在不同时间窗口（即日内交易的开始与结束小时）下的绩效表现。&lt;/p&gt;&#10;&lt;p&gt;其核心逻辑是：先加载包含策略在 2002-2008 年不同时间窗口测试结果的数据，再对数据进行预处理（提取时间窗口的开始与结束小时），最后通过 3D 图形直观展示不同时间窗口与关键绩效指标（如净收益、最大回撤等）的关系，从而识别出最优的日内交易时段。这种分析有助于理解策略在日内不同时段的适应性，为优化交易执行时间提供依据。&lt;/p&gt;&#10;&lt;p&gt;这段代码为日内时间窗口的绩效分析提供了一个基础性框架，但仍然有优化空间。我们可以考虑加入统计显著性检验，通过 t 检验或 bootstrap 方法验证不同时间窗口绩效差异是否偶然，避免因随机波动误判最优窗口。其次，可扩展时间窗口的粒度，例如从小时级细化到 15 分钟级，更精准捕捉日内波动特征。此外，结合市场环境变量（如波动率、成交量）进行条件分析，探究不同市场状态下最优时间窗口的差异，能提升策略的适应性。&lt;/p&gt;&#10;&lt;p&gt;在可视化方面，可增加交互功能（如鼠标悬停显示具体数值、动态筛选指标），并加入基准线（如全时段平均绩效），使对比更直观。最后，引入交易成本（如滑点、手续费）对绩效的影响分析，能让结果更贴近实盘场景，为实际交易决策提供更可靠的参考。&lt;/p&gt;&#10;</description></item><item><title>MACD 指标解析与策略实现：基于 AAPL 的趋势跟踪逻辑</title><link>https://gewutang.cn/2015/08/10/macd/</link><pubDate>Mon, 10 Aug 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/08/10/macd/</guid><description>&#10;&#10;&#10;&lt;p&gt;MACD（移动平均收敛散度）是量化交易中经典的趋势与动量结合指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场动量变化与潜在趋势转折。&lt;/p&gt;&#10;&lt;p&gt;MACD 的构成包括四个部分：快速 EMA（指数移动平均线，通常为 12 日）、慢速 EMA（通常为 26 日）、MACD 线（快速 EMA 与慢速 EMA 的差值）、信号线（MACD 线的 9 日 EMA，用于平滑波动）。在实际应用中，MACD 的主要用途包括：判断趋势方向（MACD 线在零轴上方为多头趋势，下方为空头趋势）、识别动量变化（MACD 线与价格的背离暗示趋势可能反转）、确认趋势确立（信号线穿越零轴标志趋势形成）。&lt;/p&gt;&#10;&lt;p&gt;本文解析的代码基于 quantstrat 包实现了一套简单的 MACD 趋势跟踪策略，以 AAPL 为标的，核心逻辑为：当信号线从下方穿越零轴（上穿）时，视为多头趋势确立，触发买入信号；当信号线从上方穿越零轴（下穿）时，视为趋势转弱，触发卖出信号，通过这种方式跟踪中长期趋势并捕获收益。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# Simple MACD strategy&#10;#&#10;# MACD可通过多种方式应用，本策略展示其作为趋势指标的用法。&#10;# &#10;# 传统上，当MACD信号线穿越零时，标志着正向趋势的确立。&#10;#&#10;# 本策略将在信号线&amp;quot;signal&amp;quot;上穿零轴时买入，下穿零轴时卖出。&#10;# &#10;# Author: brian&#10;###############################################################################&#10;&#10;&#10;# 加载quantstrat包，用于量化策略开发、回测与绩效分析&#10;require(quantstrat)&#10;&#10;# 清理旧策略数据，确保回测环境纯净（避免残留的订单、账户数据干扰结果）&#10;suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;account.st&amp;quot;, &amp;quot;portfolio.st&amp;quot;, &amp;quot;stock.str&amp;quot;, &amp;quot;stratMACD&amp;quot;, &amp;quot;startDate&amp;quot;, &amp;quot;initEq&amp;quot;, &amp;#39;start_t&amp;#39;, &amp;#39;end_t&amp;#39;))&#10;&#10;# 处理时区问题：若当前时区未设置，默认设为GMT，避免日期处理异常&#10;oldtz &amp;lt;- Sys.getenv(&amp;#39;TZ&amp;#39;)&#10;if (oldtz == &amp;#39;&amp;#39;) {&#10; Sys.setenv(TZ = &amp;quot;GMT&amp;quot;)&#10;}&#10;&#10;&#10;# 设定交易标的为苹果公司股票（AAPL）&#10;stock.str &amp;lt;- &amp;#39;AAPL&amp;#39;&#10;&#10;# MACD指标参数设置&#10;fastMA = 12 # 快速EMA周期（传统参数12日）&#10;slowMA = 26 # 慢速EMA周期（传统参数26日）&#10;signalMA = 9 # 信号线周期（传统参数9日）&#10;maType = &amp;quot;EMA&amp;quot; # 均线类型为指数移动平均（EMA）&#10;&#10;# 定义货币单位与标的属性：AAPL以美元计价，合约乘数为1（股票交易通常乘数为1）&#10;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;stock(stock.str, currency = &amp;#39;USD&amp;#39;, multiplier = 1)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 回测基础参数&#10;startDate = &amp;#39;2006-12-31&amp;#39; # 数据起始日期&#10;initEq = 1000000 # 初始资金100万美元&#10;portfolio.st = &amp;#39;macd&amp;#39; # 投资组合名称&#10;account.st = &amp;#39;macd&amp;#39; # 账户名称&#10;&#10;# 初始化回测环境&#10;initPortf(portfolio.st, symbols = stock.str) # 创建包含AAPL的投资组合&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initAcct(account.st, portfolios = portfolio.st) # 初始化账户，关联投资组合&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initOrders(portfolio = portfolio.st) # 初始化订单簿，记录交易订单&#10;&#10;# 定义策略对象，名称与组合一致，并存入环境&#10;strat.st &amp;lt;- portfolio.st&#10;strategy(strat.st, store = TRUE)&#10;&#10;# 添加MACD指标：基于收盘价计算，参数为快速EMA、慢速EMA&#10;add.indicator(&#10; strat.st, &#10; name = &amp;quot;MACD&amp;quot;, # 使用TTR包中的MACD函数&#10; arguments = list(&#10; x = quote(Cl(mktdata)), # 输入为收盘价（Cl(mktdata)）&#10; nFast = fastMA, # 快速EMA周期12日&#10; nSlow = slowMA # 慢速EMA周期26日&#10; ),&#10; label = &amp;#39;_&amp;#39; # 指标标签，用于后续信号引用&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 生成交易信号：基于信号线与零轴的交叉&#10;# 信号1：信号线上穿零轴（大于零且交叉），视为买入信号&#10;add.signal(&#10; strat.st,&#10; name = &amp;quot;sigThreshold&amp;quot;, # 阈值型信号函数&#10; arguments = list(&#10; column = &amp;quot;signal._&amp;quot;, # 引用MACD指标中的信号线（因指标标签为&amp;#39;_&amp;#39;，故列名为signal._）&#10; relationship = &amp;quot;gt&amp;quot;, # 大于（greater than）&#10; threshold = 0, # 阈值为0（零轴）&#10; cross = TRUE # 必须穿越阈值（避免持续触发）&#10; ),&#10; label = &amp;quot;signal.gt.zero&amp;quot; # 信号标签：上穿零轴&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 信号2：信号线下穿零轴（小于零且交叉），视为卖出信号&#10;add.signal(&#10; strat.st,&#10; name = &amp;quot;sigThreshold&amp;quot;,&#10; arguments = list(&#10; column = &amp;quot;signal._&amp;quot;,&#10; relationship = &amp;quot;lt&amp;quot;, # 小于（less than）&#10; threshold = 0,&#10; cross = TRUE&#10; ),&#10; label = &amp;quot;signal.lt.zero&amp;quot; # 信号标签：下穿零轴&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义交易规则：将信号转化为具体交易动作&#10;####&#10;# 入场规则：当买入信号触发时，以市价单买入100股&#10;add.rule(&#10; strat.st,&#10; name = &amp;#39;ruleSignal&amp;#39;, # 基于信号的规则函数&#10; arguments = list(&#10; sigcol = &amp;quot;signal.gt.zero&amp;quot;, # 触发信号列：上穿零轴&#10; sigval = TRUE, # 信号值为TRUE时执行&#10; orderqty = 100, # 订单数量：100股&#10; ordertype = &amp;#39;market&amp;#39;, # 市价单（确保及时入场）&#10; orderside = &amp;#39;long&amp;#39;, # 做多方向&#10; threshold = NULL # 无额外阈值&#10; ),&#10; type = &amp;#39;enter&amp;#39;, # 入场规则&#10; label = &amp;#39;enter&amp;#39;,&#10; storefun = FALSE # 不存储规则函数结果&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可选的风险止损规则（注释未启用）：&#10;# 1. 止损限价单：当价格回落5%时触发止损&#10;# add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,sigval=TRUE, orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;stoplimit&amp;#39;, orderside=&amp;#39;long&amp;#39;, threshold=-.05,tmult=TRUE, orderset=&amp;#39;exit2&amp;#39;),type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;risk&amp;#39;,storefun=FALSE)&#10;# 2. 追踪止损单：当价格从高点回落1单位时触发止损&#10;# add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,sigval=TRUE, orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;stoptrailing&amp;#39;, orderside=&amp;#39;long&amp;#39;, threshold=-1,tmult=FALSE, orderset=&amp;#39;exit2&amp;#39;),&#9;type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;trailingexit&amp;#39;)&#10;&#10;# 出场规则：当卖出信号触发时，以市价单平仓所有持仓&#10;add.rule(&#10; strat.st,&#10; name = &amp;#39;ruleSignal&amp;#39;,&#10; arguments = list(&#10; sigcol = &amp;quot;signal.lt.zero&amp;quot;, # 触发信号列：下穿零轴&#10; sigval = TRUE,&#10; orderqty = &amp;#39;all&amp;#39;, # 平仓全部持仓&#10; ordertype = &amp;#39;market&amp;#39;, # 市价单（确保及时离场）&#10; orderside = &amp;#39;long&amp;#39;, # 针对多头持仓&#10; threshold = NULL,&#10; orderset = &amp;#39;exit2&amp;#39; # 订单集标签，用于管理出场订单&#10; ),&#10; type = &amp;#39;exit&amp;#39;, # 出场规则&#10; label = &amp;#39;exit&amp;#39;&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 结束规则定义&#10;####&#10;&#10;# 获取历史数据：从雅虎财经下载AAPL的历史数据（2006-12-31至2014-06-01）&#10;getSymbols(stock.str, from = startDate, to = &amp;#39;2014-06-01&amp;#39;, src = &amp;#39;yahoo&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 执行策略回测并记录时间&#10;start_t &amp;lt;- Sys.time() # 回测开始时间&#10;out &amp;lt;- applyStrategy(&#10; strat.st, &#10; portfolios = portfolio.st,&#10; parameters = list(nFast = fastMA, nSlow = slowMA, nSig = signalMA, maType = maType), # 传递MACD参数&#10; verbose = TRUE # 输出详细过程&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t &amp;lt;- Sys.time() # 回测结束时间&#10;print(end_t - start_t) # 打印回测耗时&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 0.4202731 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 更新组合与账户数据&#10;start_t &amp;lt;- Sys.time()&#10;updatePortf(Portfolio = portfolio.st, Dates = paste(&amp;#39;::&amp;#39;, as.Date(Sys.time()), sep = &amp;#39;&amp;#39;)) # 更新至当前日期&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()&#10;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(end_t - start_t) # 打印数据更新耗时&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 0.032722 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化回测结果&#10;chart.Posn(Portfolio = portfolio.st, Symbol = stock.str) # 绘制AAPL的持仓变化图&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/08/10/macd/index_files/figure-html/SimpleMACD-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;plot(add_MACD(fast = fastMA, slow = slowMA, signal = signalMA, maType = &amp;quot;EMA&amp;quot;)) # 叠加MACD指标&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/08/10/macd/index_files/figure-html/SimpleMACD-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 查看订单簿（包含所有交易订单详情，用于调试与分析）&#10;obook &amp;lt;- getOrderBook(&amp;#39;macd&amp;#39;)&#10;&#10;# 恢复初始时区设置&#10;Sys.setenv(TZ = oldtz)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;strong&gt;策略核心思路&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;该策略以 “趋势跟踪” 为核心，通过 MACD 信号线与零轴的交叉信号构建完整的交易逻辑。&lt;/p&gt;&#10;&lt;p&gt;代码首先清理旧有策略数据，避免残留信息干扰回测；通过时区设置确保日期处理准确；定义交易标的（AAPL）、初始资金（100 万美元）及 MACD 核心参数（12/26/9 日 EMA），为策略运行奠定基础。这一步的关键是确保回测环境的纯净性与参数的明确性，避免后续结果出现偏差。&lt;/p&gt;&#10;&lt;p&gt;策略通过add.indicator添加 MACD 指标，基于收盘价计算快速 EMA、慢速 EMA 及信号线；再通过add.signal将指标转化为可执行的交易信号 —— 当信号线从下方穿越零轴时，认为多头趋势确立（买入信号）；当信号线从上方穿越零轴时，认为趋势转弱（卖出信号）。这种 “指标→信号” 的转化，是将技术分析逻辑量化的核心步骤。&lt;/p&gt;&#10;&lt;p&gt;信号需要通过具体的交易规则转化为订单。策略中，add.rule分别定义了入场与出场规则：入场时以市价单买入 100 股（跟随买入信号），出场时以市价单平仓所有持仓（跟随卖出信号）。代码中还预留了止损规则（未启用），为风险控制提供了扩展空间，体现了 “趋势收益捕获” 与 “风险控制” 的平衡思路。&lt;/p&gt;&#10;&lt;p&gt;策略通过applyStrategy执行回测，记录耗时以评估效率；更新组合与账户数据确保交易记录与资金权益同步；最后通过chart.Posn与add_MACD可视化持仓变化与指标走势，直观展示交易时机与信号的对应关系，为策略效果评估提供直观依据。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;MACD 及策略的优劣势与改进方向&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;MACD 的优势在于兼顾趋势与动量，通过 EMA 平滑降低噪音，且零轴与交叉信号直观易懂，适合趋势跟踪场景。但其劣势也较为明显：作为滞后指标（基于历史价格计算），信号可能滞后于实际趋势转折，导致入场 / 出场时机偏晚；在横盘震荡市中，信号线可能频繁穿越零轴，产生大量虚假信号，降低策略效率。&lt;/p&gt;&#10;&lt;p&gt;策略的优势体现在逻辑简洁与可操作性强：基于明确的信号规则，避免主观判断，便于复现与参数优化；市价单入场出场确保了信号触发时的执行力。但策略也存在局限：未启用止损规则，若趋势突然反转可能导致大额亏损；单一依赖 MACD 信号，缺乏对市场波动率的考量，在高波动环境下风险较高；固定仓位（100 股）未考虑资金管理，可能无法适配不同资金规模。&lt;/p&gt;&#10;&lt;p&gt;未来可从三方面优化策略：一是添加动态止损机制（如追踪止损），限制单笔交易亏损；二是结合波动率指标（如 ATR）过滤信号，在震荡市减少交易频率；三是引入参数优化（如通过前进测试调整 EMA 周期），使策略适配不同市场阶段。通过这些改进，可提升策略的稳健性与实盘适用性，更好地平衡收益与风险。&lt;/p&gt;&#10;</description></item><item><title>MACD 策略的参数优化与有效性验证：基于 quantstrat 的实现</title><link>https://gewutang.cn/2015/07/10/macd-parameters/</link><pubDate>Fri, 10 Jul 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/07/10/macd-parameters/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易中，策略参数的选择直接影响其表现 —— 同一策略在不同参数下可能呈现截然不同的收益特征。然而，盲目测试参数组合不仅效率低下，还可能因 “过度拟合” 导致策略在实盘失效。本文将解析一段基于 R 语言quantstrat包的 MACD 策略参数优化代码，展示如何系统地测试参数范围、施加合理约束，并通过统计指标验证参数有效性，为策略稳健性提供科学依据。&lt;/p&gt;&#10;&lt;div id="代码实现与核心注释" class="section level1"&gt;&#10;&lt;h1&gt;代码实现与核心注释&lt;/h1&gt;&#10;&lt;p&gt;以下是完整的 MACD 参数优化代码，包含详细注释以说明各环节的功能与逻辑：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# Parameter demo for MACD&#10;###############################################################################&#10;&#10;# 加载必要的包：foreach支持并行迭代，iterators用于迭代控制，quantstrat用于量化策略开发&#10;require(foreach, quietly=TRUE)&#10;require(iterators)&#10;require(quantstrat)&#10;&#10;# 运行内置的MACD策略演示，初始化策略环境（包含策略定义、交易标的、账户等基础设置）&#10;demo(&amp;#39;macd&amp;#39;, ask=FALSE)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#10;## &#9;demo(macd)&#10;## &#9;---- ~~~~&#10;## &#10;## &amp;gt; # Simple MACD strategy&#10;## &amp;gt; #&#10;## &amp;gt; # MACD may be used in many ways, this will demonstrate a trend indicator.&#10;## &amp;gt; # &#10;## &amp;gt; # traditionally, when the MACD signal crosses zero, this indicated a establishment of a positive trend&#10;## &amp;gt; #&#10;## &amp;gt; # we&amp;#39;ll buy on positive treshold crossover of the &amp;#39;signal&amp;#39; column, and sell on negative threshold crossover&#10;## &amp;gt; # &#10;## &amp;gt; # Author: brian&#10;## &amp;gt; ###############################################################################&#10;## &amp;gt; &#10;## &amp;gt; &#10;## &amp;gt; require(quantstrat)&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;,pos=.strategy))&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;account.macd&amp;quot;,&amp;quot;portfolio.macd&amp;quot;,pos=.blotter))&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;account.st&amp;quot;,&amp;quot;portfolio.st&amp;quot;,&amp;quot;stock.str&amp;quot;,&amp;quot;stratMACD&amp;quot;,&amp;quot;startDate&amp;quot;,&amp;quot;initEq&amp;quot;,&amp;#39;start_t&amp;#39;,&amp;#39;end_t&amp;#39;))&#10;## &#10;## &amp;gt; #correct for TZ issues if they crop up&#10;## &amp;gt; oldtz&amp;lt;-Sys.getenv(&amp;#39;TZ&amp;#39;)&#10;## &#10;## &amp;gt; if(oldtz==&amp;#39;&amp;#39;) {&#10;## + Sys.setenv(TZ=&amp;quot;GMT&amp;quot;)&#10;## + }&#10;## &#10;## &amp;gt; stock.str=&amp;#39;AAPL&amp;#39; # what are we trying it on&#10;## &#10;## &amp;gt; #MA parameters for MACD&#10;## &amp;gt; fastMA = 12 &#10;## &#10;## &amp;gt; slowMA = 26 &#10;## &#10;## &amp;gt; signalMA = 9&#10;## &#10;## &amp;gt; maType=&amp;quot;EMA&amp;quot;&#10;## &#10;## &amp;gt; currency(&amp;#39;USD&amp;#39;)&#10;## [1] &amp;quot;USD&amp;quot;&#10;## &#10;## &amp;gt; stock(stock.str,currency=&amp;#39;USD&amp;#39;,multiplier=1)&#10;## [1] &amp;quot;AAPL&amp;quot;&#10;## &#10;## &amp;gt; #or use fake data&#10;## &amp;gt; #stock.str=&amp;#39;sample_matrix&amp;#39; # what are we trying it on&#10;## &amp;gt; #data(sample_matrix) # data included in package xts&#10;## &amp;gt; #sample_matrix&amp;lt;-as.xts(sample_matrix)&#10;## &amp;gt; &#10;## &amp;gt; startDate=&amp;#39;2006-12-31&amp;#39;&#10;## &#10;## &amp;gt; initEq=1000000&#10;## &#10;## &amp;gt; portfolio.st=&amp;#39;macd&amp;#39;&#10;## &#10;## &amp;gt; account.st=&amp;#39;macd&amp;#39;&#10;## &#10;## &amp;gt; initPortf(portfolio.st,symbols=stock.str)&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; initAcct(account.st,portfolios=portfolio.st)&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; initOrders(portfolio=portfolio.st)&#10;## &#10;## &amp;gt; strat.st&amp;lt;-portfolio.st&#10;## &#10;## &amp;gt; # define the strategy&#10;## &amp;gt; strategy(strat.st, store=TRUE)&#10;## &#10;## &amp;gt; #one indicator&#10;## &amp;gt; add.indicator(strat.st, name = &amp;quot;MACD&amp;quot;, &#10;## + &#9;&#9;&#9; arguments = list(x=quote(Cl(mktdata)),&#10;## + &#9;&#9;&#9; nFast=fastMA, &#10;## + &#9;&#9;&#9; nSlow=slowMA),&#10;## + &#9;&#9;&#9; label=&amp;#39;_&amp;#39; &#10;## + )&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; #two signals&#10;## &amp;gt; add.signal(strat.st,name=&amp;quot;sigThreshold&amp;quot;,&#10;## + &#9;&#9; arguments = list(column=&amp;quot;signal._&amp;quot;,&#10;## + &#9;&#9;&#9;&#9; &#9;&#9;&#9; relationship=&amp;quot;gt&amp;quot;,&#10;## + &#9;&#9;&#9;&#9;&#9;&#9;&#9; threshold=0,&#10;## + &#9;&#9;&#9;&#9;&#9;&#9;&#9; cross=TRUE),&#10;## + &#9;&#9; label=&amp;quot;signal.gt.zero&amp;quot;&#10;## + )&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; add.signal(strat.st,name=&amp;quot;sigThreshold&amp;quot;,&#10;## + &#9;&#9; arguments = list(column=&amp;quot;signal._&amp;quot;,&#10;## + &#9;&#9;&#9;&#9; relationship=&amp;quot;lt&amp;quot;,&#10;## + &#9;&#9;&#9;&#9;&#9;&#9;&#9; threshold=0,&#10;## + &#9;&#9;&#9;&#9;&#9;&#9;&#9; cross=TRUE),&#10;## + &#9; label=&amp;quot;signal.lt.zero&amp;quot;&#10;## + )&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; ####&#10;## &amp;gt; # add rules&#10;## &amp;gt; &#10;## &amp;gt; # entry&#10;## &amp;gt; add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, &#10;## + &#9;&#9; arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,&#10;## + &#9;&#9;&#9;&#9; sigval=TRUE, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; orderqty=100, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; ordertype=&amp;#39;market&amp;#39;, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; orderside=&amp;#39;long&amp;#39;, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; threshold=NULL),&#10;## + &#9; type=&amp;#39;enter&amp;#39;,&#10;## + &#9;&#9; label=&amp;#39;enter&amp;#39;,&#10;## + &#9;&#9; storefun=FALSE&#10;## + )&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; #alternatives for risk stops:&#10;## &amp;gt; # simple stoplimit order, with threshold multiplier&#10;## &amp;gt; #add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,sigval=TRUE, orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;stoplimit&amp;#39;, orderside=&amp;#39;long&amp;#39;, threshold=-.05,tmult=TRUE, orderset=&amp;#39;exit2&amp;#39;),type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;risk&amp;#39;,storefun=FALSE)&#10;## &amp;gt; # alternately, use a trailing order, also with a threshold multiplier&#10;## &amp;gt; #add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,sigval=TRUE, orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;stoptrailing&amp;#39;, orderside=&amp;#39;long&amp;#39;, threshold=-1,tmult=FALSE, orderset=&amp;#39;exit2&amp;#39;),&#9;type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;trailingexit&amp;#39;)&#10;## &amp;gt; &#10;## &amp;gt; # exit&#10;## &amp;gt; add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, &#10;## + &#9;&#9; arguments = list(sigcol=&amp;quot;signal.lt.zero&amp;quot;,&#10;## + &#9;&#9;&#9;&#9; sigval=TRUE, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; orderqty=&amp;#39;all&amp;#39;, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; ordertype=&amp;#39;market&amp;#39;, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; orderside=&amp;#39;long&amp;#39;, &#10;## + &#9;&#9;&#9;&#9;&#9;&#9; threshold=NULL,&#10;## + &#9;&#9;&#9;&#9;&#9;&#9; orderset=&amp;#39;exit2&amp;#39;),&#10;## + type=&amp;#39;exit&amp;#39;,&#10;## + &#9;&#9; label=&amp;#39;exit&amp;#39;&#10;## + )&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; #end rules&#10;## &amp;gt; ####&#10;## &amp;gt; &#10;## &amp;gt; getSymbols(stock.str,from=startDate, to=&amp;#39;2014-06-01&amp;#39;, src=&amp;#39;yahoo&amp;#39;)&#10;## [1] &amp;quot;AAPL&amp;quot;&#10;## &#10;## &amp;gt; start_t&amp;lt;-Sys.time()&#10;## &#10;## &amp;gt; out&amp;lt;-applyStrategy(strat.st , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType),verbose=TRUE)&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## &#10;## &amp;gt; end_t&amp;lt;-Sys.time()&#10;## &#10;## &amp;gt; print(end_t-start_t)&#10;## Time difference of 0.437279 secs&#10;## &#10;## &amp;gt; start_t&amp;lt;-Sys.time()&#10;## &#10;## &amp;gt; updatePortf(Portfolio=portfolio.st,Dates=paste(&amp;#39;::&amp;#39;,as.Date(Sys.time()),sep=&amp;#39;&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; end_t&amp;lt;-Sys.time()&#10;## &#10;## &amp;gt; print(&amp;quot;trade blotter portfolio update:&amp;quot;)&#10;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&#10;## &#10;## &amp;gt; print(end_t-start_t)&#10;## Time difference of 0.05017185 secs&#10;## &#10;## &amp;gt; chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/07/10/macd-parameters/index_files/figure-html/MACDParameter-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &amp;gt; plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType=&amp;quot;EMA&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/07/10/macd-parameters/index_files/figure-html/MACDParameter-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &amp;gt; #look at the order book&#10;## &amp;gt; obook&amp;lt;-getOrderBook(&amp;#39;macd&amp;#39;)&#10;## &#10;## &amp;gt; # set tz as it was before the demo&#10;## &amp;gt; Sys.setenv(TZ=oldtz)&#10;## &#10;## &amp;gt; ###############################################################################&#10;## &amp;gt; # R (http://r-project.org/) Quantitative Strategy Model Framework&#10;## &amp;gt; #&#10;## &amp;gt; # Copyright (c) 2009-2012&#10;## &amp;gt; # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich &#10;## &amp;gt; #&#10;## &amp;gt; # This library is distributed under the terms of the GNU Public License (GPL)&#10;## &amp;gt; # for full details see the file COPYING&#10;## &amp;gt; #&#10;## &amp;gt; # $Id$&#10;## &amp;gt; #&#10;## &amp;gt; ##############################################################################&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 并行计算初始化（可选）：启用后可加速参数组合测试，适合大规模参数搜索&#10;# 推荐使用doParallel、doMC等包，默认使用物理核心数-1的线程提升效率&#10;# require(doParallel)&#10;# registerDoParallel()&#10;&#10;&#10;# 从环境中获取MACD策略对象（内置demo中已定义并存储策略，label为&amp;#39;macd&amp;#39;）&#10;strategy.st &amp;lt;- &amp;#39;macd&amp;#39;&#10;&#10;### 设置参数搜索范围&#10;.FastMA = (1:20) # MACD快速EMA周期候选值（1到20日）：反映短期价格动量&#10;.SlowMA = (30:80) # MACD慢速EMA周期候选值（30到80日）：反映中长期价格趋势&#10;.nsamples = 30 # 随机抽样的参数组合数量：减少全量测试的计算成本，同时覆盖关键组合&#10;&#10;&#10;### 定义参数分布与约束&#10;# 为策略添加快速EMA周期的参数分布&#10;add.distribution(&#10; strategy.st, # 目标策略名称&#10; paramset.label = &amp;#39;MA&amp;#39;, # 参数集标签（用于标识该组参数）&#10; component.type = &amp;#39;indicator&amp;#39;, # 组件类型：指标（此处为MACD的快速EMA）&#10; component.label = &amp;#39;_&amp;#39;, # 指标在策略中的标识（对应MACD指标的默认标签）&#10; variable = list(n = .FastMA), # 待测试的参数：快速EMA周期，取值范围1-20&#10; label = &amp;#39;nFAST&amp;#39; # 参数标签（用于后续约束与结果识别）&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 为策略添加慢速EMA周期的参数分布&#10;add.distribution(&#10; strategy.st,&#10; paramset.label = &amp;#39;MA&amp;#39;,&#10; component.type = &amp;#39;indicator&amp;#39;,&#10; component.label = &amp;#39;_&amp;#39;,&#10; variable = list(n = .SlowMA), # 待测试的参数：慢速EMA周期，取值范围30-80&#10; label = &amp;#39;nSLOW&amp;#39; # 参数标签&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 添加参数约束：确保快速EMA周期小于慢速EMA周期（MACD指标的核心逻辑要求）&#10;add.distribution.constraint(&#10; strategy.st,&#10; paramset.label = &amp;#39;MA&amp;#39;,&#10; distribution.label.1 = &amp;#39;nFAST&amp;#39;, # 第一个参数：快速EMA周期&#10; distribution.label.2 = &amp;#39;nSLOW&amp;#39;, # 第二个参数：慢速EMA周期&#10; operator = &amp;#39;&amp;lt;&amp;#39;, # 约束关系：nFAST &amp;lt; nSLOW&#10; label = &amp;#39;MA&amp;#39; # 约束标签&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;### 执行参数优化并分析结果&#10;.audit &amp;lt;- new.env() # 创建环境存储审计信息，用于后续有效性验证&#10;&#10;# 应用参数集测试策略表现&#10;results &amp;lt;- apply.paramset(&#10; strategy.st, # 策略名称&#10; paramset.label = &amp;#39;MA&amp;#39;, # 使用的参数集标签&#10; portfolio.st = portfolio.st, # 组合名称（来自内置MACD演示）&#10; account.st = account.st, # 账户名称（来自内置MACD演示）&#10; nsamples = .nsamples, # 随机抽样的参数组合数量（30组）&#10; audit = .audit, # 存储审计数据的环境&#10; store = TRUE, # 保存结果到策略环境&#10; verbose = TRUE # 输出详细过程信息&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## evaluation # 1:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 441 1 52&#10;## &#10;## [1] &amp;quot;2008-04-22 00:00:00 AAPL 100 @ 5.72142887115479&amp;quot;&#10;## [1] &amp;quot;2008-07-30 00:00:00 AAPL -100 @ 5.71000003814697&amp;quot;&#10;## [1] &amp;quot;2008-09-02 00:00:00 AAPL 100 @ 5.93535709381104&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-03-26 00:00:00 AAPL 100 @ 3.92392897605896&amp;quot;&#10;## [1] &amp;quot;2010-08-30 00:00:00 AAPL -100 @ 8.6607141494751&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-05-27 00:00:00 AAPL -100 @ 12.0503568649292&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-01 00:00:00 AAPL -100 @ 13.854642868042&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-11-05 00:00:00 AAPL -100 @ 20.8792858123779&amp;quot;&#10;## [1] &amp;quot;2013-08-07 00:00:00 AAPL 100 @ 16.6064281463623&amp;quot;&#10;## [1] &amp;quot;2014-02-11 00:00:00 AAPL -100 @ 19.141429901123&amp;quot;&#10;## [1] &amp;quot;2014-04-02 00:00:00 AAPL 100 @ 19.3767852783203&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b1c696b0&amp;gt;&#10;## got results for task 1&#10;## numValues: 1, numResults: 1, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 2:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 243 3 42&#10;## &#10;## [1] &amp;quot;2008-04-16 00:00:00 AAPL 100 @ 5.48928594589233&amp;quot;&#10;## [1] &amp;quot;2008-07-25 00:00:00 AAPL -100 @ 5.78999996185303&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-24 00:00:00 AAPL 100 @ 3.80357098579407&amp;quot;&#10;## [1] &amp;quot;2010-02-24 00:00:00 AAPL -100 @ 7.16642904281616&amp;quot;&#10;## [1] &amp;quot;2010-03-03 00:00:00 AAPL 100 @ 7.47607088088989&amp;quot;&#10;## [1] &amp;quot;2010-08-26 00:00:00 AAPL -100 @ 8.58142852783203&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-18 00:00:00 AAPL -100 @ 11.85178565979&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-25 00:00:00 AAPL -100 @ 12.0278568267822&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-29 00:00:00 AAPL -100 @ 13.3285713195801&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-10-31 00:00:00 AAPL -100 @ 21.2614288330078&amp;quot;&#10;## [1] &amp;quot;2013-06-07 00:00:00 AAPL 100 @ 15.7789287567139&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 16.6160717010498&amp;quot;&#10;## [1] &amp;quot;2014-02-07 00:00:00 AAPL -100 @ 18.5599994659424&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b1c69790&amp;gt;&#10;## got results for task 2&#10;## numValues: 2, numResults: 2, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 3:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 343 3 47&#10;## &#10;## [1] &amp;quot;2008-04-18 00:00:00 AAPL 100 @ 5.75142908096313&amp;quot;&#10;## [1] &amp;quot;2008-07-29 00:00:00 AAPL -100 @ 5.6100001335144&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-25 00:00:00 AAPL 100 @ 3.80321407318115&amp;quot;&#10;## [1] &amp;quot;2010-08-27 00:00:00 AAPL -100 @ 8.62928581237793&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-20 00:00:00 AAPL -100 @ 12.228928565979&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-26 00:00:00 AAPL -100 @ 11.9642858505249&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-11-30 00:00:00 AAPL -100 @ 13.6499996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-11-02 00:00:00 AAPL -100 @ 20.6000003814697&amp;quot;&#10;## [1] &amp;quot;2013-08-07 00:00:00 AAPL 100 @ 16.6064281463623&amp;quot;&#10;## [1] &amp;quot;2014-02-10 00:00:00 AAPL -100 @ 18.8924999237061&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b43aa028&amp;gt;&#10;## got results for task 3&#10;## numValues: 3, numResults: 3, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 4:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 184 4 39&#10;## &#10;## [1] &amp;quot;2008-04-15 00:00:00 AAPL 100 @ 5.29928588867188&amp;quot;&#10;## [1] &amp;quot;2008-07-24 00:00:00 AAPL -100 @ 5.67964315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-23 00:00:00 AAPL 100 @ 3.84500002861023&amp;quot;&#10;## [1] &amp;quot;2010-02-19 00:00:00 AAPL -100 @ 7.2024998664856&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-08-25 00:00:00 AAPL -100 @ 8.67464256286621&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-15 00:00:00 AAPL -100 @ 11.6949996948242&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-25 00:00:00 AAPL -100 @ 12.0278568267822&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 13.4328565597534&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-06-06 00:00:00 AAPL -100 @ 20.4092864990234&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-31 00:00:00 AAPL -100 @ 21.2614288330078&amp;quot;&#10;## [1] &amp;quot;2013-06-05 00:00:00 AAPL 100 @ 15.896785736084&amp;quot;&#10;## [1] &amp;quot;2013-06-20 00:00:00 AAPL -100 @ 14.8871431350708&amp;quot;&#10;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 16.6160717010498&amp;quot;&#10;## [1] &amp;quot;2014-02-06 00:00:00 AAPL -100 @ 18.3039283752441&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b37266a0&amp;gt;&#10;## got results for task 4&#10;## numValues: 4, numResults: 4, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 5:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 604 4 60&#10;## &#10;## [1] &amp;quot;2008-04-23 00:00:00 AAPL 100 @ 5.81750011444092&amp;quot;&#10;## [1] &amp;quot;2008-08-01 00:00:00 AAPL -100 @ 5.59499979019165&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-27 00:00:00 AAPL 100 @ 3.81607103347778&amp;quot;&#10;## [1] &amp;quot;2010-09-01 00:00:00 AAPL -100 @ 8.94035720825195&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-06-07 00:00:00 AAPL -100 @ 11.8585710525513&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-06 00:00:00 AAPL -100 @ 13.9624996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-11-06 00:00:00 AAPL -100 @ 20.8160705566406&amp;quot;&#10;## [1] &amp;quot;2013-08-08 00:00:00 AAPL 100 @ 16.4646434783936&amp;quot;&#10;## [1] &amp;quot;2014-02-26 00:00:00 AAPL -100 @ 18.47678565979&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 AAPL 100 @ 19.1692867279053&amp;quot;&#10;## [1] &amp;quot;2014-04-17 00:00:00 AAPL -100 @ 18.7478561401367&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b0218098&amp;gt;&#10;## got results for task 5&#10;## numValues: 5, numResults: 5, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 6:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 525 5 56&#10;## &#10;## [1] &amp;quot;2008-04-22 00:00:00 AAPL 100 @ 5.72142887115479&amp;quot;&#10;## [1] &amp;quot;2008-07-31 00:00:00 AAPL -100 @ 5.67678594589233&amp;quot;&#10;## [1] &amp;quot;2008-08-29 00:00:00 AAPL 100 @ 6.05464315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-03-26 00:00:00 AAPL 100 @ 3.92392897605896&amp;quot;&#10;## [1] &amp;quot;2010-08-31 00:00:00 AAPL -100 @ 8.68214321136475&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-05-31 00:00:00 AAPL -100 @ 12.4224996566772&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-02 00:00:00 AAPL -100 @ 13.917857170105&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-11-05 00:00:00 AAPL -100 @ 20.8792858123779&amp;quot;&#10;## [1] &amp;quot;2013-08-08 00:00:00 AAPL 100 @ 16.4646434783936&amp;quot;&#10;## [1] &amp;quot;2014-02-13 00:00:00 AAPL -100 @ 19.4439296722412&amp;quot;&#10;## [1] &amp;quot;2014-04-01 00:00:00 AAPL 100 @ 19.3446426391602&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b3650aa0&amp;gt;&#10;## got results for task 6&#10;## numValues: 6, numResults: 6, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 7:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 765 5 68&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-04 00:00:00 AAPL -100 @ 5.47249984741211&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-31 00:00:00 AAPL 100 @ 3.75428605079651&amp;quot;&#10;## [1] &amp;quot;2010-09-03 00:00:00 AAPL -100 @ 9.24178600311279&amp;quot;&#10;## [1] &amp;quot;2010-09-13 00:00:00 AAPL 100 @ 9.53714275360107&amp;quot;&#10;## [1] &amp;quot;2011-06-13 00:00:00 AAPL -100 @ 11.66428565979&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-07 00:00:00 AAPL -100 @ 19.9285717010498&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-21 00:00:00 AAPL -100 @ 18.9703578948975&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b3648c98&amp;gt;&#10;## got results for task 7&#10;## numValues: 7, numResults: 7, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 8:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 985 5 79&#10;## &#10;## [1] &amp;quot;2008-04-28 00:00:00 AAPL 100 @ 6.15142917633057&amp;quot;&#10;## [1] &amp;quot;2008-08-06 00:00:00 AAPL -100 @ 5.86392879486084&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-02 00:00:00 AAPL 100 @ 4.02535676956177&amp;quot;&#10;## [1] &amp;quot;2011-06-16 00:00:00 AAPL -100 @ 11.6128568649292&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2012-11-09 00:00:00 AAPL -100 @ 19.5378570556641&amp;quot;&#10;## [1] &amp;quot;2013-08-14 00:00:00 AAPL 100 @ 17.8035717010498&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b08b4e18&amp;gt;&#10;## got results for task 8&#10;## numValues: 8, numResults: 8, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 9:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 626 6 61&#10;## &#10;## [1] &amp;quot;2008-04-23 00:00:00 AAPL 100 @ 5.81750011444092&amp;quot;&#10;## [1] &amp;quot;2008-08-01 00:00:00 AAPL -100 @ 5.59499979019165&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-27 00:00:00 AAPL 100 @ 3.81607103347778&amp;quot;&#10;## [1] &amp;quot;2010-09-01 00:00:00 AAPL -100 @ 8.94035720825195&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-06-08 00:00:00 AAPL -100 @ 11.8657140731812&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-06 00:00:00 AAPL -100 @ 13.9624996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-11-06 00:00:00 AAPL -100 @ 20.8160705566406&amp;quot;&#10;## [1] &amp;quot;2013-08-09 00:00:00 AAPL 100 @ 16.2303562164307&amp;quot;&#10;## [1] &amp;quot;2014-02-27 00:00:00 AAPL -100 @ 18.8453578948975&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 AAPL 100 @ 19.1692867279053&amp;quot;&#10;## [1] &amp;quot;2014-04-17 00:00:00 AAPL -100 @ 18.7478561401367&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4aaac46a0&amp;gt;&#10;## got results for task 9&#10;## numValues: 9, numResults: 9, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 10:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 167 7 38&#10;## &#10;## [1] &amp;quot;2008-04-15 00:00:00 AAPL 100 @ 5.29928588867188&amp;quot;&#10;## [1] &amp;quot;2008-07-24 00:00:00 AAPL -100 @ 5.67964315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-02-23 00:00:00 AAPL 100 @ 3.1053569316864&amp;quot;&#10;## [1] &amp;quot;2009-03-02 00:00:00 AAPL -100 @ 3.14071393013&amp;quot;&#10;## [1] &amp;quot;2009-03-23 00:00:00 AAPL 100 @ 3.84500002861023&amp;quot;&#10;## [1] &amp;quot;2010-02-18 00:00:00 AAPL -100 @ 7.24749994277954&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-08-25 00:00:00 AAPL -100 @ 8.67464256286621&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-15 00:00:00 AAPL -100 @ 11.6949996948242&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-25 00:00:00 AAPL -100 @ 12.0278568267822&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 13.4328565597534&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-06-05 00:00:00 AAPL -100 @ 20.1010704040527&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-26 00:00:00 AAPL -100 @ 21.5714282989502&amp;quot;&#10;## [1] &amp;quot;2013-06-04 00:00:00 AAPL 100 @ 16.0467853546143&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 16.7660713195801&amp;quot;&#10;## [1] &amp;quot;2014-02-06 00:00:00 AAPL -100 @ 18.3039283752441&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4aaabc898&amp;gt;&#10;## got results for task 10&#10;## numValues: 10, numResults: 10, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 11:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 927 7 76&#10;## &#10;## [1] &amp;quot;2008-04-28 00:00:00 AAPL 100 @ 6.15142917633057&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-02 00:00:00 AAPL 100 @ 4.02535676956177&amp;quot;&#10;## [1] &amp;quot;2011-06-15 00:00:00 AAPL -100 @ 11.6696434020996&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2012-11-09 00:00:00 AAPL -100 @ 19.5378570556641&amp;quot;&#10;## [1] &amp;quot;2013-08-13 00:00:00 AAPL 100 @ 17.4846439361572&amp;quot;&#10;## [1] &amp;quot;2014-04-24 00:00:00 AAPL -100 @ 20.2775001525879&amp;quot;&#10;## [1] &amp;quot;2014-04-25 00:00:00 AAPL 100 @ 20.4264297485352&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4e404a0&amp;gt;&#10;## got results for task 11&#10;## numValues: 11, numResults: 11, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 12:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 488 8 54&#10;## &#10;## [1] &amp;quot;2008-04-22 00:00:00 AAPL 100 @ 5.72142887115479&amp;quot;&#10;## [1] &amp;quot;2008-07-30 00:00:00 AAPL -100 @ 5.71000003814697&amp;quot;&#10;## [1] &amp;quot;2008-08-29 00:00:00 AAPL 100 @ 6.05464315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-03-26 00:00:00 AAPL 100 @ 3.92392897605896&amp;quot;&#10;## [1] &amp;quot;2010-08-30 00:00:00 AAPL -100 @ 8.6607141494751&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-05-31 00:00:00 AAPL -100 @ 12.4224996566772&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-02 00:00:00 AAPL -100 @ 13.917857170105&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-11-05 00:00:00 AAPL -100 @ 20.8792858123779&amp;quot;&#10;## [1] &amp;quot;2013-08-08 00:00:00 AAPL 100 @ 16.4646434783936&amp;quot;&#10;## [1] &amp;quot;2014-02-12 00:00:00 AAPL -100 @ 19.1399993896484&amp;quot;&#10;## [1] &amp;quot;2014-04-02 00:00:00 AAPL 100 @ 19.3767852783203&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4e38698&amp;gt;&#10;## got results for task 12&#10;## numValues: 12, numResults: 12, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 13:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 868 8 73&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-14 00:00:00 AAPL -100 @ 11.872857093811&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-13 00:00:00 AAPL 100 @ 17.4846439361572&amp;quot;&#10;## [1] &amp;quot;2014-04-23 00:00:00 AAPL -100 @ 18.7410717010498&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b112baa0&amp;gt;&#10;## got results for task 13&#10;## numValues: 13, numResults: 13, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 14:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 489 9 54&#10;## &#10;## [1] &amp;quot;2008-04-22 00:00:00 AAPL 100 @ 5.72142887115479&amp;quot;&#10;## [1] &amp;quot;2008-07-30 00:00:00 AAPL -100 @ 5.71000003814697&amp;quot;&#10;## [1] &amp;quot;2008-08-29 00:00:00 AAPL 100 @ 6.05464315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-03-26 00:00:00 AAPL 100 @ 3.92392897605896&amp;quot;&#10;## [1] &amp;quot;2010-08-30 00:00:00 AAPL -100 @ 8.6607141494751&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-05-31 00:00:00 AAPL -100 @ 12.4224996566772&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-02 00:00:00 AAPL -100 @ 13.917857170105&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-11-05 00:00:00 AAPL -100 @ 20.8792858123779&amp;quot;&#10;## [1] &amp;quot;2013-08-08 00:00:00 AAPL 100 @ 16.4646434783936&amp;quot;&#10;## [1] &amp;quot;2014-02-12 00:00:00 AAPL -100 @ 19.1399993896484&amp;quot;&#10;## [1] &amp;quot;2014-04-02 00:00:00 AAPL 100 @ 19.3767852783203&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b31a2ea0&amp;gt;&#10;## got results for task 14&#10;## numValues: 14, numResults: 14, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 15:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 331 11 46&#10;## &#10;## [1] &amp;quot;2008-04-18 00:00:00 AAPL 100 @ 5.75142908096313&amp;quot;&#10;## [1] &amp;quot;2008-07-29 00:00:00 AAPL -100 @ 5.6100001335144&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-24 00:00:00 AAPL 100 @ 3.80357098579407&amp;quot;&#10;## [1] &amp;quot;2010-08-26 00:00:00 AAPL -100 @ 8.58142852783203&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-19 00:00:00 AAPL -100 @ 12.0664291381836&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-26 00:00:00 AAPL -100 @ 11.9642858505249&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-30 00:00:00 AAPL -100 @ 13.6499996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-11-01 00:00:00 AAPL -100 @ 21.3050003051758&amp;quot;&#10;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 16.6160717010498&amp;quot;&#10;## [1] &amp;quot;2014-02-10 00:00:00 AAPL -100 @ 18.8924999237061&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b319b098&amp;gt;&#10;## got results for task 15&#10;## numValues: 15, numResults: 15, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 16:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 771 11 68&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-04 00:00:00 AAPL -100 @ 5.47249984741211&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-31 00:00:00 AAPL 100 @ 3.75428605079651&amp;quot;&#10;## [1] &amp;quot;2010-09-03 00:00:00 AAPL -100 @ 9.24178600311279&amp;quot;&#10;## [1] &amp;quot;2010-09-13 00:00:00 AAPL 100 @ 9.53714275360107&amp;quot;&#10;## [1] &amp;quot;2011-06-13 00:00:00 AAPL -100 @ 11.66428565979&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-07 00:00:00 AAPL -100 @ 19.9285717010498&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-21 00:00:00 AAPL -100 @ 18.9703578948975&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4af6404a0&amp;gt;&#10;## got results for task 16&#10;## numValues: 16, numResults: 16, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 17:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 332 12 46&#10;## &#10;## [1] &amp;quot;2008-04-18 00:00:00 AAPL 100 @ 5.75142908096313&amp;quot;&#10;## [1] &amp;quot;2008-07-29 00:00:00 AAPL -100 @ 5.6100001335144&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-24 00:00:00 AAPL 100 @ 3.80357098579407&amp;quot;&#10;## [1] &amp;quot;2010-08-26 00:00:00 AAPL -100 @ 8.58142852783203&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-19 00:00:00 AAPL -100 @ 12.0664291381836&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-26 00:00:00 AAPL -100 @ 11.9642858505249&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-30 00:00:00 AAPL -100 @ 13.6499996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-11-01 00:00:00 AAPL -100 @ 21.3050003051758&amp;quot;&#10;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 16.6160717010498&amp;quot;&#10;## [1] &amp;quot;2014-02-10 00:00:00 AAPL -100 @ 18.8924999237061&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b08dbe98&amp;gt;&#10;## got results for task 17&#10;## numValues: 17, numResults: 17, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 18:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 892 12 74&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-15 00:00:00 AAPL -100 @ 11.6696434020996&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-13 00:00:00 AAPL 100 @ 17.4846439361572&amp;quot;&#10;## [1] &amp;quot;2014-04-24 00:00:00 AAPL -100 @ 20.2775001525879&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4ae7698a0&amp;gt;&#10;## got results for task 18&#10;## numValues: 18, numResults: 18, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 19:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 912 12 75&#10;## &#10;## [1] &amp;quot;2008-04-28 00:00:00 AAPL 100 @ 6.15142917633057&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-15 00:00:00 AAPL -100 @ 11.6696434020996&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2012-11-09 00:00:00 AAPL -100 @ 19.5378570556641&amp;quot;&#10;## [1] &amp;quot;2013-08-13 00:00:00 AAPL 100 @ 17.4846439361572&amp;quot;&#10;## [1] &amp;quot;2014-04-24 00:00:00 AAPL -100 @ 20.2775001525879&amp;quot;&#10;## [1] &amp;quot;2014-04-25 00:00:00 AAPL 100 @ 20.4264297485352&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4aaab6898&amp;gt;&#10;## got results for task 19&#10;## numValues: 19, numResults: 19, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 20:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 153 13 37&#10;## &#10;## [1] &amp;quot;2007-04-04 00:00:00 AAPL 100 @ 3.36678600311279&amp;quot;&#10;## [1] &amp;quot;2008-01-25 00:00:00 AAPL -100 @ 4.64321422576904&amp;quot;&#10;## [1] &amp;quot;2008-04-15 00:00:00 AAPL 100 @ 5.29928588867188&amp;quot;&#10;## [1] &amp;quot;2008-07-24 00:00:00 AAPL -100 @ 5.67964315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-02-20 00:00:00 AAPL 100 @ 3.25714302062988&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-23 00:00:00 AAPL 100 @ 3.84500002861023&amp;quot;&#10;## [1] &amp;quot;2010-02-17 00:00:00 AAPL -100 @ 7.23392915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-08-24 00:00:00 AAPL -100 @ 8.56892871856689&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-15 00:00:00 AAPL -100 @ 11.6949996948242&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 13.4328565597534&amp;quot;&#10;## [1] &amp;quot;2012-01-03 00:00:00 AAPL 100 @ 14.686785697937&amp;quot;&#10;## [1] &amp;quot;2012-06-01 00:00:00 AAPL -100 @ 20.0353565216064&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-26 00:00:00 AAPL -100 @ 21.5714282989502&amp;quot;&#10;## [1] &amp;quot;2013-06-04 00:00:00 AAPL 100 @ 16.0467853546143&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 16.7660713195801&amp;quot;&#10;## [1] &amp;quot;2014-02-06 00:00:00 AAPL -100 @ 18.3039283752441&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4aaab6908&amp;gt;&#10;## got results for task 20&#10;## numValues: 20, numResults: 20, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 21:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 854 14 72&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-14 00:00:00 AAPL -100 @ 11.872857093811&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-23 00:00:00 AAPL -100 @ 18.7410717010498&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4ae77f8a0&amp;gt;&#10;## got results for task 21&#10;## numValues: 21, numResults: 21, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 22:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 235 15 41&#10;## &#10;## [1] &amp;quot;2008-04-16 00:00:00 AAPL 100 @ 5.48928594589233&amp;quot;&#10;## [1] &amp;quot;2008-07-25 00:00:00 AAPL -100 @ 5.78999996185303&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-03-24 00:00:00 AAPL 100 @ 3.80357098579407&amp;quot;&#10;## [1] &amp;quot;2010-02-23 00:00:00 AAPL -100 @ 7.03785705566406&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-08-25 00:00:00 AAPL -100 @ 8.67464256286621&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-18 00:00:00 AAPL -100 @ 11.85178565979&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-25 00:00:00 AAPL -100 @ 12.0278568267822&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-29 00:00:00 AAPL -100 @ 13.3285713195801&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-10-31 00:00:00 AAPL -100 @ 21.2614288330078&amp;quot;&#10;## [1] &amp;quot;2013-06-06 00:00:00 AAPL 100 @ 15.6592855453491&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 16.6160717010498&amp;quot;&#10;## [1] &amp;quot;2014-02-07 00:00:00 AAPL -100 @ 18.5599994659424&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b175ca98&amp;gt;&#10;## got results for task 22&#10;## numValues: 22, numResults: 22, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 23:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 855 15 72&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-14 00:00:00 AAPL -100 @ 11.872857093811&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-23 00:00:00 AAPL -100 @ 18.7410717010498&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4e6e4a0&amp;gt;&#10;## got results for task 23&#10;## numValues: 23, numResults: 23, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 24:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 76 16 33&#10;## &#10;## [1] &amp;quot;2007-04-02 00:00:00 AAPL 100 @ 3.34464311599731&amp;quot;&#10;## [1] &amp;quot;2008-01-25 00:00:00 AAPL -100 @ 4.64321422576904&amp;quot;&#10;## [1] &amp;quot;2008-04-11 00:00:00 AAPL 100 @ 5.25500011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-22 00:00:00 AAPL -100 @ 5.78642892837524&amp;quot;&#10;## [1] &amp;quot;2008-09-02 00:00:00 AAPL 100 @ 5.93535709381104&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-02-18 00:00:00 AAPL 100 @ 3.37035703659058&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-16 00:00:00 AAPL -100 @ 7.26428604125977&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-24 00:00:00 AAPL -100 @ 8.56892871856689&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-13 00:00:00 AAPL -100 @ 12.0046434402466&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-25 00:00:00 AAPL -100 @ 12.9846429824829&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-25 00:00:00 AAPL -100 @ 20.0817852020264&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-25 00:00:00 AAPL -100 @ 21.7692852020264&amp;quot;&#10;## [1] &amp;quot;2013-05-29 00:00:00 AAPL 100 @ 15.8910713195801&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 16.7660713195801&amp;quot;&#10;## [1] &amp;quot;2014-02-05 00:00:00 AAPL -100 @ 18.3067855834961&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4e66698&amp;gt;&#10;## got results for task 24&#10;## numValues: 24, numResults: 24, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 25:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 137 17 36&#10;## &#10;## [1] &amp;quot;2007-04-03 00:00:00 AAPL 100 @ 3.375&amp;quot;&#10;## [1] &amp;quot;2008-01-25 00:00:00 AAPL -100 @ 4.64321422576904&amp;quot;&#10;## [1] &amp;quot;2008-04-14 00:00:00 AAPL 100 @ 5.27785682678223&amp;quot;&#10;## [1] &amp;quot;2008-07-23 00:00:00 AAPL -100 @ 5.93785715103149&amp;quot;&#10;## [1] &amp;quot;2008-09-02 00:00:00 AAPL 100 @ 5.93535709381104&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-02-20 00:00:00 AAPL 100 @ 3.25714302062988&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-23 00:00:00 AAPL 100 @ 3.84500002861023&amp;quot;&#10;## [1] &amp;quot;2010-02-17 00:00:00 AAPL -100 @ 7.23392915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-08-24 00:00:00 AAPL -100 @ 8.56892871856689&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-14 00:00:00 AAPL -100 @ 11.872142791748&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 13.4328565597534&amp;quot;&#10;## [1] &amp;quot;2012-01-03 00:00:00 AAPL 100 @ 14.686785697937&amp;quot;&#10;## [1] &amp;quot;2012-05-31 00:00:00 AAPL -100 @ 20.6332149505615&amp;quot;&#10;## [1] &amp;quot;2012-06-22 00:00:00 AAPL 100 @ 20.78928565979&amp;quot;&#10;## [1] &amp;quot;2012-10-26 00:00:00 AAPL -100 @ 21.5714282989502&amp;quot;&#10;## [1] &amp;quot;2013-06-03 00:00:00 AAPL 100 @ 16.0971431732178&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 16.7660713195801&amp;quot;&#10;## [1] &amp;quot;2014-02-05 00:00:00 AAPL -100 @ 18.3067855834961&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4e6e5b8&amp;gt;&#10;## got results for task 25&#10;## numValues: 25, numResults: 25, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 26:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 157 17 37&#10;## &#10;## [1] &amp;quot;2007-04-04 00:00:00 AAPL 100 @ 3.36678600311279&amp;quot;&#10;## [1] &amp;quot;2008-01-25 00:00:00 AAPL -100 @ 4.64321422576904&amp;quot;&#10;## [1] &amp;quot;2008-04-15 00:00:00 AAPL 100 @ 5.29928588867188&amp;quot;&#10;## [1] &amp;quot;2008-07-24 00:00:00 AAPL -100 @ 5.67964315414429&amp;quot;&#10;## [1] &amp;quot;2008-09-03 00:00:00 AAPL 100 @ 5.96285676956177&amp;quot;&#10;## [1] &amp;quot;2008-09-08 00:00:00 AAPL -100 @ 5.6399998664856&amp;quot;&#10;## [1] &amp;quot;2009-02-20 00:00:00 AAPL 100 @ 3.25714302062988&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-23 00:00:00 AAPL 100 @ 3.84500002861023&amp;quot;&#10;## [1] &amp;quot;2010-02-17 00:00:00 AAPL -100 @ 7.23392915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-08-24 00:00:00 AAPL -100 @ 8.56892871856689&amp;quot;&#10;## [1] &amp;quot;2010-09-20 00:00:00 AAPL 100 @ 10.1153573989868&amp;quot;&#10;## [1] &amp;quot;2011-04-15 00:00:00 AAPL -100 @ 11.6949996948242&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 13.0328569412231&amp;quot;&#10;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 13.4328565597534&amp;quot;&#10;## [1] &amp;quot;2012-01-03 00:00:00 AAPL 100 @ 14.686785697937&amp;quot;&#10;## [1] &amp;quot;2012-06-01 00:00:00 AAPL -100 @ 20.0353565216064&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-26 00:00:00 AAPL -100 @ 21.5714282989502&amp;quot;&#10;## [1] &amp;quot;2013-06-04 00:00:00 AAPL 100 @ 16.0467853546143&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 16.7660713195801&amp;quot;&#10;## [1] &amp;quot;2014-02-06 00:00:00 AAPL -100 @ 18.3039283752441&amp;quot;&#10;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 19.2425003051758&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4adf10aa0&amp;gt;&#10;## got results for task 26&#10;## numValues: 26, numResults: 26, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 27:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 637 17 61&#10;## &#10;## [1] &amp;quot;2008-04-23 00:00:00 AAPL 100 @ 5.81750011444092&amp;quot;&#10;## [1] &amp;quot;2008-08-01 00:00:00 AAPL -100 @ 5.59499979019165&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-27 00:00:00 AAPL 100 @ 3.81607103347778&amp;quot;&#10;## [1] &amp;quot;2010-09-01 00:00:00 AAPL -100 @ 8.94035720825195&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-06-08 00:00:00 AAPL -100 @ 11.8657140731812&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2011-12-06 00:00:00 AAPL -100 @ 13.9624996185303&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-11-06 00:00:00 AAPL -100 @ 20.8160705566406&amp;quot;&#10;## [1] &amp;quot;2013-08-09 00:00:00 AAPL 100 @ 16.2303562164307&amp;quot;&#10;## [1] &amp;quot;2014-02-27 00:00:00 AAPL -100 @ 18.8453578948975&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 AAPL 100 @ 19.1692867279053&amp;quot;&#10;## [1] &amp;quot;2014-04-17 00:00:00 AAPL -100 @ 18.7478561401367&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4ade2ae98&amp;gt;&#10;## got results for task 27&#10;## numValues: 27, numResults: 27, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 28:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 797 17 69&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-04 00:00:00 AAPL -100 @ 5.47249984741211&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-31 00:00:00 AAPL 100 @ 3.75428605079651&amp;quot;&#10;## [1] &amp;quot;2010-09-07 00:00:00 AAPL -100 @ 9.20750045776367&amp;quot;&#10;## [1] &amp;quot;2010-09-13 00:00:00 AAPL 100 @ 9.53714275360107&amp;quot;&#10;## [1] &amp;quot;2011-06-13 00:00:00 AAPL -100 @ 11.66428565979&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-22 00:00:00 AAPL -100 @ 18.9892864227295&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4c168a0&amp;gt;&#10;## got results for task 28&#10;## numValues: 28, numResults: 28, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 29:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 859 19 72&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-05 00:00:00 AAPL -100 @ 5.73714303970337&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-11 00:00:00 AAPL -100 @ 5.45178604125977&amp;quot;&#10;## [1] &amp;quot;2009-04-01 00:00:00 AAPL 100 @ 3.88178610801697&amp;quot;&#10;## [1] &amp;quot;2011-06-14 00:00:00 AAPL -100 @ 11.872857093811&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-08 00:00:00 AAPL -100 @ 19.2053565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-23 00:00:00 AAPL -100 @ 18.7410717010498&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4c10a98&amp;gt;&#10;## got results for task 29&#10;## numValues: 29, numResults: 29, stopped: FALSE&#10;## returning status FALSE&#10;## evaluation # 30:&#10;## $param.combo&#10;## nFAST nSLOW&#10;## 780 20 68&#10;## &#10;## [1] &amp;quot;2008-04-25 00:00:00 AAPL 100 @ 6.06178617477417&amp;quot;&#10;## [1] &amp;quot;2008-08-04 00:00:00 AAPL -100 @ 5.47249984741211&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-03-31 00:00:00 AAPL 100 @ 3.75428605079651&amp;quot;&#10;## [1] &amp;quot;2010-09-03 00:00:00 AAPL -100 @ 9.24178600311279&amp;quot;&#10;## [1] &amp;quot;2010-09-13 00:00:00 AAPL 100 @ 9.53714275360107&amp;quot;&#10;## [1] &amp;quot;2011-06-13 00:00:00 AAPL -100 @ 11.66428565979&amp;quot;&#10;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 13.3500003814697&amp;quot;&#10;## [1] &amp;quot;2012-11-07 00:00:00 AAPL -100 @ 19.9285717010498&amp;quot;&#10;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 16.6914291381836&amp;quot;&#10;## [1] &amp;quot;2014-04-21 00:00:00 AAPL -100 @ 18.9703578948975&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## result of evaluating expression:&#10;## &amp;lt;environment: 0x7fa4b4c10b08&amp;gt;&#10;## got results for task 30&#10;## numValues: 30, numResults: 30, stopped: FALSE&#10;## first call to combine function&#10;## evaluating call object to combine results:&#10;## fun(result.1, result.2, result.3, result.4, result.5, result.6, &#10;## result.7, result.8, result.9, result.10, result.11, result.12, &#10;## result.13, result.14, result.15, result.16, result.17, result.18, &#10;## result.19, result.20, result.21, result.22, result.23, result.24, &#10;## result.25, result.26, result.27, result.28, result.29, result.30)&#10;## returning status FALSE&#10;## numValues: 30, numResults: 30, stopped: TRUE&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取关键统计结果&#10;df &amp;lt;- degrees.of.freedom(&amp;#39;macd&amp;#39;, &amp;#39;macd&amp;#39;) # 计算策略的自由度（评估过拟合风险）&#10;tstats &amp;lt;- results$tradeStats # 交易统计数据（胜率、盈亏比等）&#10;defSR &amp;lt;- SharpeRatio.deflated(&amp;#39;macd&amp;#39;, strategy=&amp;#39;macd&amp;#39;, audit=.audit) # 修正后的夏普比率（考虑过拟合）&#10;hcSR &amp;lt;- SharpeRatio.haircut(&amp;#39;macd&amp;#39;, strategy=&amp;#39;macd&amp;#39;, audit=.audit) # 调整后的夏普比率（降低过拟合影响）&#10;&#10;# 打印结果&#10;print(tstats) # 输出交易统计&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## nFAST nSLOW Portfolio Symbol Num.Txns Num.Trades Net.Trading.PL&#10;## 441 1 52 macd.train.441 AAPL 17 8 1648.500&#10;## 243 3 42 macd.train.243 AAPL 23 11 1518.071&#10;## 343 3 47 macd.train.343 AAPL 19 9 1571.500&#10;## 184 4 39 macd.train.184 AAPL 25 12 1386.500&#10;## 604 4 60 macd.train.604 AAPL 17 8 1618.428&#10;## 525 5 56 macd.train.525 AAPL 17 8 1722.286&#10;## 765 5 68 macd.train.765 AAPL 13 6 1651.179&#10;## 985 5 79 macd.train.985 AAPL 9 4 1789.786&#10;## 626 6 61 macd.train.626 AAPL 17 8 1702.107&#10;## 167 7 38 macd.train.167 AAPL 27 13 1352.178&#10;## 927 7 76 macd.train.927 AAPL 11 5 1799.786&#10;## 488 8 54 macd.train.488 AAPL 17 8 1694.143&#10;## 868 8 73 macd.train.868 AAPL 11 5 1573.536&#10;## 489 9 54 macd.train.489 AAPL 17 8 1694.143&#10;## 331 11 46 macd.train.331 AAPL 19 9 1651.679&#10;## 771 11 68 macd.train.771 AAPL 13 6 1651.179&#10;## 332 12 46 macd.train.332 AAPL 19 9 1651.679&#10;## 892 12 74 macd.train.892 AAPL 11 5 1706.857&#10;## 912 12 75 macd.train.912 AAPL 11 5 1814.143&#10;## 153 13 37 macd.train.153 AAPL 29 14 1405.679&#10;## 854 14 72 macd.train.854 AAPL 11 5 1621.143&#10;## 235 15 41 macd.train.235 AAPL 23 11 1521.571&#10;## 855 15 72 macd.train.855 AAPL 11 5 1621.143&#10;## 76 16 33 macd.train.76 AAPL 29 14 1496.536&#10;## 137 17 36 macd.train.137 AAPL 29 14 1502.500&#10;## 157 17 37 macd.train.157 AAPL 29 14 1405.679&#10;## 637 17 61 macd.train.637 AAPL 17 8 1702.107&#10;## 797 17 69 macd.train.797 AAPL 13 6 1571.036&#10;## 859 19 72 macd.train.859 AAPL 11 5 1621.143&#10;## 780 20 68 macd.train.780 AAPL 13 6 1651.179&#10;## Avg.Trade.PL Med.Trade.PL Largest.Winner Largest.Loser Gross.Profits&#10;## 441 187.90627 136.01780 641.0715 -84.07135 1640.286&#10;## 243 124.80194 30.07140 679.7143 -74.39289 1582.286&#10;## 343 158.47222 29.99992 613.5715 -74.39289 1594.214&#10;## 184 103.43749 39.01784 594.5001 -100.96426 1484.178&#10;## 604 184.14727 129.67854 643.6070 -79.03571 1616.607&#10;## 525 197.12948 155.64284 641.0715 -80.85709 1726.107&#10;## 765 250.98809 220.30358 657.8571 -82.35712 1647.214&#10;## 985 327.35715 310.87499 758.7500 -71.07139 1409.250&#10;## 626 194.60712 141.37497 643.6070 -79.03571 1700.286&#10;## 167 92.84063 39.99996 563.6785 -127.89288 1476.785&#10;## 927 316.34286 279.28562 764.4287 -71.07139 1694.214&#10;## 488 193.61158 157.78565 641.0715 -84.07135 1697.857&#10;## 868 286.91425 125.64278 799.1071 -74.96428 1542.000&#10;## 489 193.61158 157.78565 641.0715 -84.07135 1697.857&#10;## 331 167.38096 61.71427 684.0714 -74.39289 1674.393&#10;## 771 250.98809 220.30358 657.8571 -82.35712 1647.214&#10;## 332 167.38096 61.71427 684.0714 -74.39289 1674.393&#10;## 892 313.57854 279.28562 778.7857 -74.96428 1675.321&#10;## 912 319.21427 279.28562 778.7857 -71.07139 1708.571&#10;## 153 90.03060 57.46427 534.8571 -127.89288 1560.143&#10;## 854 296.43568 204.96426 799.1071 -74.96428 1589.607&#10;## 235 125.12012 30.07140 679.7143 -74.39289 1573.821&#10;## 855 296.43568 204.96426 799.1071 -74.96428 1589.607&#10;## 76 96.52040 63.80358 561.7499 -112.32147 1659.786&#10;## 137 96.94642 70.46430 594.6429 -132.92866 1649.250&#10;## 157 90.03060 57.46427 534.8571 -127.89288 1560.143&#10;## 637 194.60712 141.37497 643.6070 -79.03571 1700.286&#10;## 797 238.67855 221.25001 585.5356 -82.35712 1573.357&#10;## 859 296.43568 204.96426 799.1071 -74.96428 1589.607&#10;## 780 250.98809 220.30358 657.8571 -82.35712 1647.214&#10;## Gross.Losses Std.Dev.Trade.PL Std.Err.Trade.PL Percent.Positive&#10;## 441 -137.03566 261.4606 92.44028 62.50000&#10;## 243 -209.46436 225.5217 67.99736 63.63636&#10;## 343 -167.96427 250.7102 83.57008 55.55556&#10;## 184 -242.92860 196.9424 56.85237 66.66667&#10;## 604 -143.42880 266.6014 94.25784 62.50000&#10;## 525 -149.07141 265.6197 93.91073 62.50000&#10;## 765 -141.28575 304.4241 124.28063 66.66667&#10;## 985 -99.82142 438.2080 219.10400 50.00000&#10;## 626 -143.42880 268.2088 94.82611 62.50000&#10;## 167 -269.85722 185.6172 51.48094 69.23077&#10;## 927 -112.50000 384.6769 172.03275 60.00000&#10;## 488 -148.96426 264.1330 93.38512 62.50000&#10;## 868 -107.42860 396.9982 177.54301 60.00000&#10;## 489 -148.96426 264.1330 93.38512 62.50000&#10;## 331 -167.96427 264.2260 88.07535 55.55556&#10;## 771 -141.28575 304.4241 124.28063 66.66667&#10;## 332 -167.96427 264.2260 88.07535 55.55556&#10;## 892 -107.42860 380.9597 170.37037 60.00000&#10;## 912 -112.50000 388.8884 173.91616 60.00000&#10;## 153 -299.71435 174.5118 46.64025 64.28571&#10;## 854 -107.42860 384.2972 171.86295 60.00000&#10;## 235 -197.50004 223.3315 67.33699 63.63636&#10;## 855 -107.42860 384.2972 171.86295 60.00000&#10;## 76 -308.49998 184.6163 49.34077 57.14286&#10;## 137 -292.00015 186.5365 49.85397 64.28571&#10;## 157 -299.71435 174.5118 46.64025 64.28571&#10;## 637 -143.42880 268.2088 94.82611 62.50000&#10;## 797 -141.28575 285.2111 116.43694 66.66667&#10;## 859 -107.42860 384.2972 171.86295 60.00000&#10;## 780 -141.28575 304.4241 124.28063 66.66667&#10;## Percent.Negative Profit.Factor Avg.Win.Trade Med.Win.Trade Avg.Losing.Trade&#10;## 441 37.50000 11.969774 328.0572 253.50018 -45.67855&#10;## 243 36.36364 7.553962 226.0408 194.39278 -52.36609&#10;## 343 44.44444 9.491389 318.8428 239.42852 -41.99107&#10;## 184 33.33333 6.109525 185.5223 136.44640 -60.73215&#10;## 604 37.50000 11.271146 323.3214 201.21422 -47.80960&#10;## 525 37.50000 11.579063 345.2214 297.92862 -49.69047&#10;## 765 33.33333 11.658743 411.8036 388.32144 -70.64288&#10;## 985 50.00000 14.117711 704.6250 704.62501 -49.91071&#10;## 626 37.50000 11.854563 340.0572 261.50017 -47.80960&#10;## 167 30.76923 5.472470 164.0873 94.03572 -67.46430&#10;## 927 40.00000 15.059683 564.7381 650.50001 -56.25000&#10;## 488 37.50000 11.397747 339.5714 267.53559 -49.65475&#10;## 868 40.00000 14.353718 513.9999 617.24997 -53.71430&#10;## 489 37.50000 11.397747 339.5714 267.53559 -49.65475&#10;## 331 44.44444 9.968744 334.8786 227.64282 -41.99107&#10;## 771 33.33333 11.658743 411.8036 388.32144 -70.64288&#10;## 332 44.44444 9.968744 334.8786 227.64282 -41.99107&#10;## 892 40.00000 15.594742 558.4404 617.24997 -53.71430&#10;## 912 40.00000 15.187301 569.5238 650.50001 -56.25000&#10;## 153 35.71429 5.205432 173.3492 127.64282 -59.94287&#10;## 854 40.00000 14.796870 529.8690 585.53562 -53.71430&#10;## 235 36.36364 7.968714 224.8316 194.39278 -49.37501&#10;## 855 40.00000 14.796870 529.8690 585.53562 -53.71430&#10;## 76 42.85714 5.380181 207.4732 144.01779 -51.41666&#10;## 137 35.71429 5.648114 183.2500 126.82142 -58.40003&#10;## 157 35.71429 5.205432 173.3492 127.64282 -59.94287&#10;## 637 37.50000 11.854563 340.0572 261.50017 -47.80960&#10;## 797 33.33333 11.135992 393.3393 387.55358 -70.64288&#10;## 859 40.00000 14.796870 529.8690 585.53562 -53.71430&#10;## 780 33.33333 11.658743 411.8036 388.32144 -70.64288&#10;## Med.Losing.Trade Avg.Daily.PL Med.Daily.PL Std.Dev.Daily.PL&#10;## 441 -51.82142 187.90627 136.01780 261.4606&#10;## 243 -51.39289 124.80194 30.07140 225.5217&#10;## 343 -39.71424 158.47222 29.99992 250.7102&#10;## 184 -54.83932 103.43749 39.01784 196.9424&#10;## 604 -42.14306 184.14727 129.67854 266.6014&#10;## 525 -63.75003 197.12948 155.64284 265.6197&#10;## 765 -70.64288 250.98809 220.30358 304.4241&#10;## 985 -49.91071 327.35715 310.87499 438.2080&#10;## 626 -42.14306 194.60712 141.37497 268.2088&#10;## 167 -54.83932 92.84063 39.99996 185.6172&#10;## 927 -56.25000 316.34286 279.28562 384.6769&#10;## 488 -63.75003 193.61158 157.78565 264.1330&#10;## 868 -53.71430 286.91425 125.64278 396.9982&#10;## 489 -63.75003 193.61158 157.78565 264.1330&#10;## 331 -39.71424 167.38096 61.71427 264.2260&#10;## 771 -70.64288 250.98809 220.30358 304.4241&#10;## 332 -39.71424 167.38096 61.71427 264.2260&#10;## 892 -53.71430 313.57854 279.28562 380.9597&#10;## 912 -56.25000 319.21427 279.28562 388.8884&#10;## 153 -55.03569 90.03060 57.46427 174.5118&#10;## 854 -53.71430 296.43568 204.96426 384.2972&#10;## 235 -45.41073 125.12012 30.07140 223.3315&#10;## 855 -53.71430 296.43568 204.96426 384.2972&#10;## 76 -43.71425 96.52040 63.80358 184.6163&#10;## 137 -55.03569 96.94642 70.46430 186.5365&#10;## 157 -55.03569 90.03060 57.46427 174.5118&#10;## 637 -42.14306 194.60712 141.37497 268.2088&#10;## 797 -70.64288 238.67855 221.25001 285.2111&#10;## 859 -53.71430 296.43568 204.96426 384.2972&#10;## 780 -70.64288 250.98809 220.30358 304.4241&#10;## Std.Err.Daily.PL Ann.Sharpe Max.Drawdown Profit.To.Max.Draw&#10;## 441 92.44028 11.408677 -472.6429 3.487835&#10;## 243 67.99736 8.784827 -502.5715 3.020608&#10;## 343 83.57008 10.034167 -500.5714 3.139412&#10;## 184 56.85237 8.337561 -536.3572 2.585031&#10;## 604 94.25784 10.964859 -464.7860 3.482094&#10;## 525 93.91073 11.781257 -458.4644 3.756640&#10;## 765 124.28063 13.088031 -576.2144 2.865563&#10;## 985 219.10400 11.858829 -726.5001 2.463573&#10;## 626 94.82611 11.518238 -447.5000 3.803591&#10;## 167 51.48094 7.939995 -547.2858 2.470699&#10;## 927 172.03275 13.054558 -694.6074 2.591084&#10;## 488 93.38512 11.636140 -458.4644 3.695255&#10;## 868 177.54301 11.472651 -727.8574 2.161873&#10;## 489 93.38512 11.636140 -458.4644 3.695255&#10;## 331 88.07535 10.056126 -431.0358 3.831883&#10;## 771 124.28063 13.088031 -576.2144 2.865563&#10;## 332 88.07535 10.056126 -431.0358 3.831883&#10;## 892 170.37037 13.066749 -727.8574 2.345043&#10;## 912 173.91616 13.030396 -694.6074 2.611753&#10;## 153 46.64025 8.189654 -547.2858 2.568454&#10;## 854 171.86295 12.245132 -648.5359 2.499696&#10;## 235 67.33699 8.893595 -490.6072 3.101405&#10;## 855 171.86295 12.245132 -648.5359 2.499696&#10;## 76 49.34077 8.299452 -511.9287 2.923328&#10;## 137 49.85397 8.250273 -552.3216 2.720335&#10;## 157 46.64025 8.189654 -547.2858 2.568454&#10;## 637 94.82611 11.518238 -447.5000 3.803591&#10;## 797 116.43694 13.284562 -648.5359 2.422434&#10;## 859 171.86295 12.245132 -648.5359 2.499696&#10;## 780 124.28063 13.088031 -576.2144 2.865563&#10;## Avg.WinLoss.Ratio Med.WinLoss.Ratio Max.Equity Min.Equity End.Equity&#10;## 441 7.181864 4.891803 1753.393 -72.178602 1648.500&#10;## 243 4.316549 3.782484 1701.357 -9.392786 1518.071&#10;## 343 7.593111 6.028782 1719.536 -53.571391 1571.500&#10;## 184 3.054763 2.488112 1629.179 -5.571365 1386.500&#10;## 604 6.762688 4.774552 1740.000 -109.714246 1618.428&#10;## 525 6.947438 4.673388 1779.536 -87.428617 1722.286&#10;## 765 5.829372 5.496965 1792.679 -141.285753 1651.179&#10;## 985 14.117711 14.117711 1863.143 -103.535748 1789.786&#10;## 626 7.112738 6.205059 1763.393 -109.714246 1702.107&#10;## 167 2.432209 1.714750 1605.786 -2.035666 1352.178&#10;## 927 10.039788 11.564445 1856.143 -116.214323 1799.786&#10;## 488 6.838648 4.196635 1785.000 -84.107208 1694.143&#10;## 868 9.569146 11.491353 1895.893 -111.142921 1573.536&#10;## 489 6.838648 4.196635 1785.000 -84.107208 1694.143&#10;## 331 7.974996 5.732020 1730.179 -53.607082 1651.679&#10;## 771 5.829372 5.496965 1792.679 -141.285753 1651.179&#10;## 332 7.974996 5.732020 1730.179 -53.607082 1651.679&#10;## 892 10.396495 11.491353 1875.572 -111.142921 1706.857&#10;## 912 10.124867 11.564445 1870.500 -116.214323 1814.143&#10;## 153 2.891907 2.319274 1659.286 -14.392900 1405.679&#10;## 854 9.864580 10.900926 1864.179 -111.142921 1621.143&#10;## 235 4.553551 4.280768 1692.893 -9.392786 1521.571&#10;## 855 9.864580 10.900926 1864.179 -111.142921 1621.143&#10;## 76 4.035135 3.294527 1714.500 -12.178612 1496.536&#10;## 137 3.137841 2.304349 1760.857 -15.214300 1502.500&#10;## 157 2.891907 2.319274 1659.286 -14.392900 1405.679&#10;## 637 7.112738 6.205059 1763.393 -109.714246 1702.107&#10;## 797 5.567996 5.486096 1789.250 -141.285753 1571.036&#10;## 859 9.864580 10.900926 1864.179 -111.142921 1621.143&#10;## 780 5.829372 5.496965 1792.679 -141.285753 1651.179&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(df) # 输出自由度&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Degrees of freedom report for strategy: macd &#10;## Total market observations: 1865 &#10;## Degrees of freedom consumed by strategy: 257 &#10;## Total degrees of freedom remaining: 1608 &#10;## % Degrees of Freedom: 86.22 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(defSR) # 输出修正夏普比率&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## obs.Sharpe max.Sharpe deflated.Sharpe p.value nTrials&#10;## 1 1.1 2.169138 1.095209 0.00435539 38&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(hcSR) # 输出调整夏普比率&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Sharpe Ratio Haircut Report: &#10;## &#10;## Frequency = Daily &#10;## Number of Observations = 1866 &#10;## Observed Sharpe Ratio: 1.1 &#10;## Autocorrelation: NaN &#10;## Sharpe Ratio corrected for autocorrelation: 1.1 &#10;## Assumed number of tests: 38 &#10;## Assumed Average Correlation: 0.2 &#10;## &#10;## Bonferroni Adjustment: &#10;## Adjusted P-value = 0.5742251 &#10;## Haircut Sharpe Ratio = 0.2485153 &#10;## Percentage Haircut = 0.774077 &#10;## &#10;## Holm Adjustment: &#10;## Adjusted P-value = 0.483558 &#10;## Haircut Sharpe Ratio = 0.3101128 &#10;## Percentage Haircut = 0.7180793 &#10;## &#10;## BHY Adjustment: &#10;## Adjusted P-value = 0.3133459 &#10;## Haircut Sharpe Ratio = 0.4472566 &#10;## Percentage Haircut = 0.5934031 &#10;## &#10;## Average Adjustment: &#10;## Adjusted P-value = 0.457043 &#10;## Haircut Sharpe Ratio = 0.3292902 &#10;## Percentage Haircut = 0.7006452&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略核心思路解析" class="section level1"&gt;&#10;&lt;h1&gt;策略核心思路解析&lt;/h1&gt;&#10;&lt;div id="参数优化的必要性与逻辑" class="section level2"&gt;&#10;&lt;h2&gt;参数优化的必要性与逻辑&lt;/h2&gt;&#10;&lt;p&gt;MACD 策略的表现高度依赖于快速 EMA（短期趋势）和慢速 EMA（长期趋势）的周期参数。例如，快速 EMA 过短可能导致信号频繁且噪音大，过长则可能滞后于趋势；慢速 EMA 同理。若仅凭经验选择参数（如默认的 12/26 日），可能错过更优组合，或因参数适配特定历史数据而在未来失效。&lt;/p&gt;&#10;&lt;p&gt;这段代码的核心是通过系统测试参数组合解决这一问题：首先定义合理的参数范围（快速 EMA 1-20 日、慢速 EMA 30-80 日），再通过add.distribution将参数引入策略，最后用apply.paramset批量测试不同组合的表现。这种方法避免了主观选择参数的盲目性，让数据决定最优解。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="macd-指标与参数约束的意义" class="section level2"&gt;&#10;&lt;h2&gt;MACD 指标与参数约束的意义&lt;/h2&gt;&#10;&lt;p&gt;MACD（移动平均收敛散度）的核心是通过短期与长期趋势的差异捕捉价格动量，其计算逻辑为：&#10;- MACD 线 = 快速 EMA - 慢速 EMA（反映短期与长期趋势的偏离）&#10;- 信号线 = MACD 线的 9 日 EMA（平滑波动，生成交易信号）&lt;/p&gt;&#10;&lt;p&gt;因此，快速 EMA 必须小于慢速 EMA—— 若快速周期大于慢速周期，MACD 线的物理意义将反转（变成长期减短期），无法正确反映趋势偏离。代码中add.distribution.constraint通过operator = ’&amp;lt;’强制这一约束，确保参数逻辑的合理性。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="效率与有效性的平衡随机抽样与统计验证" class="section level2"&gt;&#10;&lt;h2&gt;效率与有效性的平衡：随机抽样与统计验证&lt;/h2&gt;&#10;&lt;p&gt;全量测试 1-20 日（20 种）与 30-80 日（51 种）的参数组合，会产生 20×51=1020 种可能，计算成本极高。代码通过nsamples = 30随机抽样 30 组参数，在效率与覆盖度间取得平衡 —— 既避免全量测试的冗余，又能覆盖大概率有效的组合。&lt;/p&gt;&#10;&lt;p&gt;更重要的是，代码引入了过拟合风险评估：&#10;degrees.of.freedom计算策略的自由度（参数数量与数据量的比值），自由度越高，过拟合风险越大；&#10;SharpeRatio.deflated和SharpeRatio.haircut对传统夏普比率进行修正，降低因过度优化导致的 “虚高收益” 影响，更真实反映策略的稳健性。&lt;/p&gt;&#10;&lt;p&gt;这些指标让交易者不仅关注 “哪个参数组合收益最高”，更能判断 “该组合的收益是否可靠”。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="结果解读的核心价值" class="section level2"&gt;&#10;&lt;h2&gt;结果解读的核心价值&lt;/h2&gt;&#10;&lt;p&gt;代码输出的tstats（交易统计）包含胜率、平均盈亏、最大回撤等指标，直观展示不同参数组合的表现；而修正后的夏普比率（defSR、hcSR）则提示哪些组合的收益是 “可复制的”，哪些可能是 “数据巧合”。&#10;例如，若某参数组合的传统夏普比率极高，但修正后大幅下降，说明其可能过度拟合历史数据，实盘表现大概率不佳；反之，修正前后差异小的组合，更可能在未来保持稳健。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="科学参数优化的意义" class="section level1"&gt;&#10;&lt;h1&gt;科学参数优化的意义&lt;/h1&gt;&#10;&lt;p&gt;这段代码展示了量化交易中 “参数优化” 的标准化流程：从定义合理参数范围、施加逻辑约束，到高效抽样测试、统计验证有效性，每一步都围绕 “降低主观干扰、提升策略稳健性” 展开。&lt;/p&gt;&#10;&lt;p&gt;对交易者而言，这种方法的价值在于：它不仅能找到历史表现优异的参数，更能通过统计指标识别参数的可靠性，避免 “回测神话” 与实盘失效的鸿沟。在量化交易中，“能解释的收益” 比 “高收益” 更重要 —— 而科学的参数优化，正是通往可解释、可复制收益的关键一步。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>MACD 策略的前进测试（Walk Forward）实现与解析</title><link>https://gewutang.cn/2015/06/10/macdwfa/</link><pubDate>Wed, 10 Jun 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/06/10/macdwfa/</guid><description>&#10;&#10;&#10;&lt;p&gt;在量化交易中，策略的稳健性往往取决于其在不同市场周期的适应性。一次性回测容易因参数过拟合导致 “历史表现优异但未来失效” 的问题，而前进测试（Walk Forward Testing）通过滚动优化参数并验证，能有效降低过拟合风险。&lt;/p&gt;&#10;&lt;p&gt;本文将解析一段基于quantstrat包的 MACD 策略前进测试代码，展示其实现思路与核心价值，并结合 MACD 指标的原理说明策略设计逻辑。&lt;/p&gt;&#10;&lt;div id="代码实现与注释" class="section level1"&gt;&#10;&lt;h1&gt;代码实现与注释&lt;/h1&gt;&#10;&lt;p&gt;以下是完整的前进测试代码，包含详细注释以解释各环节功能：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# Walk Forward demo for MACD&#10;###############################################################################&#10;&#10;# 加载必要的包：foreach用于并行迭代，iterators支持迭代器，quantstrat用于量化策略开发&#10;require(foreach, quietly=TRUE)&#10;require(iterators)&#10;require(quantstrat)&#10;&#10;# 运行内置的MACD策略演示代码，初始化策略环境（包含策略定义、标的数据等）&#10;# 使用source而非demo确保所有变量在当前会话中可见&#10;source(system.file(&amp;#39;demo/macd.R&amp;#39;, package=&amp;#39;quantstrat&amp;#39;), echo = TRUE)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &amp;gt; require(quantstrat)&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos = .strategy))&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, &#10;## + pos = .blotter))&#10;## &#10;## &amp;gt; suppressWarnings(rm(&amp;quot;account.st&amp;quot;, &amp;quot;portfolio.st&amp;quot;, &#10;## + &amp;quot;stock.str&amp;quot;, &amp;quot;stratMACD&amp;quot;, &amp;quot;startDate&amp;quot;, &amp;quot;initEq&amp;quot;, &amp;quot;start_t&amp;quot;, &#10;## + &amp;quot;end_t&amp;quot;))&#10;## &#10;## &amp;gt; oldtz &amp;lt;- Sys.getenv(&amp;quot;TZ&amp;quot;)&#10;## &#10;## &amp;gt; if (oldtz == &amp;quot;&amp;quot;) {&#10;## + Sys.setenv(TZ = &amp;quot;GMT&amp;quot;)&#10;## + }&#10;## &#10;## &amp;gt; stock.str = &amp;quot;AAPL&amp;quot;&#10;## &#10;## &amp;gt; fastMA = 12&#10;## &#10;## &amp;gt; slowMA = 26&#10;## &#10;## &amp;gt; signalMA = 9&#10;## &#10;## &amp;gt; maType = &amp;quot;EMA&amp;quot;&#10;## &#10;## &amp;gt; currency(&amp;quot;USD&amp;quot;)&#10;## [1] &amp;quot;USD&amp;quot;&#10;## &#10;## &amp;gt; stock(stock.str, currency = &amp;quot;USD&amp;quot;, multiplier = 1)&#10;## [1] &amp;quot;AAPL&amp;quot;&#10;## &#10;## &amp;gt; startDate = &amp;quot;2006-12-31&amp;quot;&#10;## &#10;## &amp;gt; initEq = 1e+06&#10;## &#10;## &amp;gt; portfolio.st = &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; account.st = &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; initPortf(portfolio.st, symbols = stock.str)&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; initAcct(account.st, portfolios = portfolio.st)&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; initOrders(portfolio = portfolio.st)&#10;## &#10;## &amp;gt; strat.st &amp;lt;- portfolio.st&#10;## &#10;## &amp;gt; strategy(strat.st, store = TRUE)&#10;## &#10;## &amp;gt; add.indicator(strat.st, name = &amp;quot;MACD&amp;quot;, arguments = list(x = quote(Cl(mktdata)), &#10;## + nFast = fastMA, nSlow = slowMA), label = &amp;quot;_&amp;quot;)&#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; add.signal(strat.st, name = &amp;quot;sigThreshold&amp;quot;, arguments = list(column = &amp;quot;signal._&amp;quot;, &#10;## + relationship = &amp;quot;gt&amp;quot;, threshold = 0, cross = TRUE), label = .... [TRUNCATED] &#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; add.signal(strat.st, name = &amp;quot;sigThreshold&amp;quot;, arguments = list(column = &amp;quot;signal._&amp;quot;, &#10;## + relationship = &amp;quot;lt&amp;quot;, threshold = 0, cross = TRUE), label = .... [TRUNCATED] &#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; add.rule(strat.st, name = &amp;quot;ruleSignal&amp;quot;, arguments = list(sigcol = &amp;quot;signal.gt.zero&amp;quot;, &#10;## + sigval = TRUE, orderqty = 100, ordertype = &amp;quot;market&amp;quot;, orde .... [TRUNCATED] &#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; add.rule(strat.st, name = &amp;quot;ruleSignal&amp;quot;, arguments = list(sigcol = &amp;quot;signal.lt.zero&amp;quot;, &#10;## + sigval = TRUE, orderqty = &amp;quot;all&amp;quot;, ordertype = &amp;quot;market&amp;quot;, or .... [TRUNCATED] &#10;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; getSymbols(stock.str, from = startDate, to = &amp;quot;2014-06-01&amp;quot;, &#10;## + src = &amp;quot;yahoo&amp;quot;)&#10;## [1] &amp;quot;AAPL&amp;quot;&#10;## &#10;## &amp;gt; start_t &amp;lt;- Sys.time()&#10;## &#10;## &amp;gt; out &amp;lt;- applyStrategy(strat.st, portfolios = portfolio.st, &#10;## + parameters = list(nFast = fastMA, nSlow = slowMA, nSig = signalMA, &#10;## + maTyp .... [TRUNCATED] &#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## &#10;## &amp;gt; end_t &amp;lt;- Sys.time()&#10;## &#10;## &amp;gt; print(end_t - start_t)&#10;## Time difference of 0.4237199 secs&#10;## &#10;## &amp;gt; start_t &amp;lt;- Sys.time()&#10;## &#10;## &amp;gt; updatePortf(Portfolio = portfolio.st, Dates = paste(&amp;quot;::&amp;quot;, &#10;## + as.Date(Sys.time()), sep = &amp;quot;&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&#10;## &#10;## &amp;gt; end_t &amp;lt;- Sys.time()&#10;## &#10;## &amp;gt; print(&amp;quot;trade blotter portfolio update:&amp;quot;)&#10;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&#10;## &#10;## &amp;gt; print(end_t - start_t)&#10;## Time difference of 0.02946806 secs&#10;## &#10;## &amp;gt; chart.Posn(Portfolio = portfolio.st, Symbol = stock.str)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/06/10/macdwfa/index_files/figure-html/WalkForward-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &amp;gt; plot(add_MACD(fast = fastMA, slow = slowMA, signal = signalMA, &#10;## + maType = &amp;quot;EMA&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/06/10/macdwfa/index_files/figure-html/WalkForward-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &amp;gt; obook &amp;lt;- getOrderBook(&amp;quot;macd&amp;quot;)&#10;## &#10;## &amp;gt; Sys.setenv(TZ = oldtz)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 并行计算初始化（可选）：doParallel、doMC等并行包可加速参数搜索&#10;# 此处以doParallel为例，默认使用物理核心数-1的线程&#10;# require(doParallel)&#10;# registerDoParallel()&#10;&#10;&#10;# 从环境中获取策略对象（MACD策略在定义时设置了store=TRUE，因此可直接调用）&#10;strategy.st &amp;lt;- &amp;#39;macd&amp;#39;&#10;&#10;### 设置参数搜索范围&#10;.FastMA = (1:10) # MACD快速EMA周期候选值（1到10）&#10;.SlowMA = (5:25) # MACD慢速EMA周期候选值（5到25）&#10;.nsamples = 15 # 随机抽样的参数组合数量（并行计算时可减少迭代次数，提高效率）&#10;&#10;&#10;### 定义参数分布（用于参数优化）&#10;# 为策略添加快速EMA周期的参数分布&#10;add.distribution(&#10; strategy.st, # 策略名称&#10; paramset.label = &amp;#39;MA&amp;#39;, # 参数集标签（用于标识该组参数）&#10; component.type = &amp;#39;indicator&amp;#39;, # 组件类型：指标（此处为MACD的EMA参数）&#10; component.label = &amp;#39;_&amp;#39;, # 指标在策略中的标签（对应MACD指标的默认标识）&#10; variable = list(n = .FastMA), # 变量：快速EMA周期，取值范围为.FastMA&#10; label = &amp;#39;nFAST&amp;#39; # 该参数的标签（用于后续约束和结果识别）&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 为策略添加慢速EMA周期的参数分布&#10;add.distribution(&#10; strategy.st,&#10; paramset.label = &amp;#39;MA&amp;#39;,&#10; component.type = &amp;#39;indicator&amp;#39;,&#10; component.label = &amp;#39;_&amp;#39;,&#10; variable = list(n = .SlowMA), # 变量：慢速EMA周期，取值范围为.SlowMA&#10; label = &amp;#39;nSLOW&amp;#39; # 该参数的标签&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 添加参数约束：确保快速EMA周期小于慢速EMA周期（MACD计算的必要条件）&#10;add.distribution.constraint(&#10; strategy.st,&#10; paramset.label = &amp;#39;MA&amp;#39;,&#10; distribution.label.1 = &amp;#39;nFAST&amp;#39;, # 第一个参数（快速周期）&#10; distribution.label.2 = &amp;#39;nSLOW&amp;#39;, # 第二个参数（慢速周期）&#10; operator = &amp;#39;&amp;lt;&amp;#39;, # 约束关系：快速周期 &amp;lt; 慢速周期&#10; label = &amp;#39;MA&amp;#39; # 约束标签&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;### 初始化前进测试环境&#10;wfportfolio &amp;lt;- &amp;quot;wf.macd&amp;quot; # 前进测试的组合名称&#10;initPortf(wfportfolio, symbols = stock.str) # 初始化组合（stock.str来自MACD演示代码中的标的）&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;wf.macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initOrders(portfolio = wfportfolio) # 初始化订单簿&#10;&#10;# 执行前进测试&#10;wf_start &amp;lt;- Sys.time() # 记录开始时间&#10;wfresults &amp;lt;- walk.forward(&#10; strategy.st, # 策略名称&#10; paramset.label = &amp;#39;MA&amp;#39;, # 使用的参数集标签&#10; portfolio.st = wfportfolio, # 组合名称&#10; account.st = account.st, # 账户名称（来自MACD演示代码）&#10; nsamples = .nsamples, # 每次迭代随机抽样的参数组合数量&#10; period = &amp;#39;months&amp;#39;, # 滚动周期：按月划分训练期和测试期&#10; k.training = 36, # 训练期长度：36个月（用于参数优化）&#10; k.testing = 12, # 测试期长度：12个月（用于验证优化后的参数）&#10; verbose = TRUE, # 输出详细过程信息&#10; anchored = TRUE, # 固定起始点：训练期从最开始滚动（而非滑动窗口）&#10; include.insamples = TRUE, # 结果中包含训练期数据&#10; savewf = FALSE # 不保存中间结果&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;=== training MA on 2007-01-03/2009-12-31&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-03-14 00:00:00 AAPL 100 @ 3.21428608894348&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-26 00:00:00 AAPL -100 @ 6.16214323043823&amp;quot;&#10;## [1] &amp;quot;2007-11-30 00:00:00 AAPL 100 @ 6.50785684585571&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-27 00:00:00 AAPL 100 @ 5.00892877578735&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-18 00:00:00 AAPL 100 @ 6.26392889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-05 00:00:00 AAPL 100 @ 3.4449999332428&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-11 00:00:00 AAPL -100 @ 6.9524998664856&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-27 00:00:00 AAPL -100 @ 6.24321413040161&amp;quot;&#10;## [1] &amp;quot;2007-11-29 00:00:00 AAPL 100 @ 6.58178615570068&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-07-01 00:00:00 AAPL -100 @ 6.23857116699219&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-22 00:00:00 AAPL -100 @ 7.15571403503418&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-27 00:00:00 AAPL -100 @ 6.24321413040161&amp;quot;&#10;## [1] &amp;quot;2007-11-29 00:00:00 AAPL 100 @ 6.58178615570068&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2007-03-20 00:00:00 AAPL 100 @ 3.26714301109314&amp;quot;&#10;## [1] &amp;quot;2007-08-21 00:00:00 AAPL -100 @ 4.55607080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-22 00:00:00 AAPL 100 @ 6.31392908096313&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-28 00:00:00 AAPL -100 @ 4.52928590774536&amp;quot;&#10;## [1] &amp;quot;2007-09-04 00:00:00 AAPL 100 @ 5.1485710144043&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-27 00:00:00 AAPL -100 @ 4.7232141494751&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2007-03-20 00:00:00 AAPL 100 @ 3.26714301109314&amp;quot;&#10;## [1] &amp;quot;2007-08-21 00:00:00 AAPL -100 @ 4.55607080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-07-01 00:00:00 AAPL -100 @ 6.23857116699219&amp;quot;&#10;## [1] &amp;quot;2008-08-22 00:00:00 AAPL 100 @ 6.31392908096313&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-21 00:00:00 AAPL -100 @ 7.07964277267456&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-07-01 00:00:00 AAPL -100 @ 6.23857116699219&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-22 00:00:00 AAPL -100 @ 7.15571403503418&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;=== testing param.combo 90 on 2010-01-04/2010-12-31&amp;quot;&#10;## nFAST nSLOW&#10;## 90 1 16&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;=== training MA on 2007-01-03/2010-12-31&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2007-03-14 00:00:00 AAPL 100 @ 3.21428608894348&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-26 00:00:00 AAPL -100 @ 6.16214323043823&amp;quot;&#10;## [1] &amp;quot;2007-11-30 00:00:00 AAPL 100 @ 6.50785684585571&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-27 00:00:00 AAPL 100 @ 5.00892877578735&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-18 00:00:00 AAPL 100 @ 6.26392889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-05 00:00:00 AAPL 100 @ 3.4449999332428&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-11 00:00:00 AAPL -100 @ 6.9524998664856&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-02 00:00:00 AAPL -100 @ 6.99499988555908&amp;quot;&#10;## [1] &amp;quot;2010-03-03 00:00:00 AAPL 100 @ 7.47607088088989&amp;quot;&#10;## [1] &amp;quot;2010-05-28 00:00:00 AAPL -100 @ 9.17428588867188&amp;quot;&#10;## [1] &amp;quot;2010-06-02 00:00:00 AAPL 100 @ 9.42678642272949&amp;quot;&#10;## [1] &amp;quot;2010-07-14 00:00:00 AAPL -100 @ 9.0260705947876&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-15 00:00:00 AAPL -100 @ 8.98035717010498&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-07-01 00:00:00 AAPL -100 @ 6.23857116699219&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-22 00:00:00 AAPL -100 @ 7.15571403503418&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-05 00:00:00 AAPL -100 @ 6.98071384429932&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-15 00:00:00 AAPL -100 @ 8.98035717010498&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-17 00:00:00 AAPL -100 @ 6.85214281082153&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-03 00:00:00 AAPL -100 @ 6.07571411132812&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-09 00:00:00 AAPL -100 @ 6.2232141494751&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-29 00:00:00 AAPL 100 @ 9.21821403503418&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;=== testing param.combo 21 on 2011-01-03/2011-12-30&amp;quot;&#10;## nFAST nSLOW&#10;## 21 6 8&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;=== training MA on 2007-01-03/2011-12-30&amp;quot;&#10;## [1] &amp;quot;2007-03-20 00:00:00 AAPL 100 @ 3.26714301109314&amp;quot;&#10;## [1] &amp;quot;2007-08-21 00:00:00 AAPL -100 @ 4.55607080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-22 00:00:00 AAPL 100 @ 6.31392908096313&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-05 00:00:00 AAPL -100 @ 6.98071384429932&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-24 00:00:00 AAPL -100 @ 12.3203573226929&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-18 00:00:00 AAPL -100 @ 13.3907136917114&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-27 00:00:00 AAPL -100 @ 4.7232141494751&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-05 00:00:00 AAPL -100 @ 12.103214263916&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-15 00:00:00 AAPL -100 @ 8.98035717010498&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2007-03-14 00:00:00 AAPL 100 @ 3.21428608894348&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-26 00:00:00 AAPL -100 @ 6.16214323043823&amp;quot;&#10;## [1] &amp;quot;2007-11-30 00:00:00 AAPL 100 @ 6.50785684585571&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-27 00:00:00 AAPL 100 @ 5.00892877578735&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-18 00:00:00 AAPL 100 @ 6.26392889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-05 00:00:00 AAPL 100 @ 3.4449999332428&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-11 00:00:00 AAPL -100 @ 6.9524998664856&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-02 00:00:00 AAPL -100 @ 6.99499988555908&amp;quot;&#10;## [1] &amp;quot;2010-03-03 00:00:00 AAPL 100 @ 7.47607088088989&amp;quot;&#10;## [1] &amp;quot;2010-05-28 00:00:00 AAPL -100 @ 9.17428588867188&amp;quot;&#10;## [1] &amp;quot;2010-06-02 00:00:00 AAPL 100 @ 9.42678642272949&amp;quot;&#10;## [1] &amp;quot;2010-07-14 00:00:00 AAPL -100 @ 9.0260705947876&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-21 00:00:00 AAPL -100 @ 12.1178569793701&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-10-11 00:00:00 AAPL -100 @ 14.2960710525513&amp;quot;&#10;## [1] &amp;quot;2011-10-14 00:00:00 AAPL 100 @ 15.0714292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-15 00:00:00 AAPL -100 @ 13.8867864608765&amp;quot;&#10;## [1] &amp;quot;2011-12-23 00:00:00 AAPL 100 @ 14.4046430587769&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-10 00:00:00 AAPL -100 @ 6.30821418762207&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-27 00:00:00 AAPL -100 @ 9.43142890930176&amp;quot;&#10;## [1] &amp;quot;2010-07-28 00:00:00 AAPL 100 @ 9.31999969482422&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-28 00:00:00 AAPL -100 @ 4.52928590774536&amp;quot;&#10;## [1] &amp;quot;2007-09-04 00:00:00 AAPL 100 @ 5.1485710144043&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-06 00:00:00 AAPL -100 @ 12.0728569030762&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-09 00:00:00 AAPL -100 @ 6.2232141494751&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-29 00:00:00 AAPL 100 @ 9.21821403503418&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-28 00:00:00 AAPL -100 @ 4.52928590774536&amp;quot;&#10;## [1] &amp;quot;2007-09-04 00:00:00 AAPL 100 @ 5.1485710144043&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-06 00:00:00 AAPL -100 @ 12.0728569030762&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-17 00:00:00 AAPL -100 @ 6.85214281082153&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-23 00:00:00 AAPL -100 @ 4.68107080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-03 00:00:00 AAPL -100 @ 6.07571411132812&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-28 00:00:00 AAPL -100 @ 12.5157136917114&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-03-29 00:00:00 AAPL -100 @ 12.5342855453491&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;=== testing param.combo 17 on 2012-01-03/2012-12-31&amp;quot;&#10;## nFAST nSLOW&#10;## 17 2 8&#10;## [1] &amp;quot;2012-05-08 00:00:00 AAPL -100 @ 20.292142868042&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;=== training MA on 2007-01-03/2012-12-31&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-16 00:00:00 AAPL -100 @ 6.96535682678223&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-10 00:00:00 AAPL -100 @ 6.30821418762207&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-27 00:00:00 AAPL -100 @ 9.43142890930176&amp;quot;&#10;## [1] &amp;quot;2010-07-28 00:00:00 AAPL 100 @ 9.31999969482422&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-17 00:00:00 AAPL -100 @ 6.85214281082153&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-09 00:00:00 AAPL -100 @ 6.2232141494751&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-29 00:00:00 AAPL 100 @ 9.21821403503418&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-18 00:00:00 AAPL -100 @ 18.9421424865723&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-14 00:00:00 AAPL 100 @ 3.21428608894348&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-26 00:00:00 AAPL -100 @ 6.16214323043823&amp;quot;&#10;## [1] &amp;quot;2007-11-30 00:00:00 AAPL 100 @ 6.50785684585571&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-27 00:00:00 AAPL 100 @ 5.00892877578735&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-18 00:00:00 AAPL 100 @ 6.26392889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-05 00:00:00 AAPL 100 @ 3.4449999332428&amp;quot;&#10;## [1] &amp;quot;2009-03-03 00:00:00 AAPL -100 @ 3.15607094764709&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-11 00:00:00 AAPL -100 @ 6.9524998664856&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-02 00:00:00 AAPL -100 @ 6.99499988555908&amp;quot;&#10;## [1] &amp;quot;2010-03-03 00:00:00 AAPL 100 @ 7.47607088088989&amp;quot;&#10;## [1] &amp;quot;2010-05-28 00:00:00 AAPL -100 @ 9.17428588867188&amp;quot;&#10;## [1] &amp;quot;2010-06-02 00:00:00 AAPL 100 @ 9.42678642272949&amp;quot;&#10;## [1] &amp;quot;2010-07-14 00:00:00 AAPL -100 @ 9.0260705947876&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-21 00:00:00 AAPL -100 @ 12.1178569793701&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-10-11 00:00:00 AAPL -100 @ 14.2960710525513&amp;quot;&#10;## [1] &amp;quot;2011-10-14 00:00:00 AAPL 100 @ 15.0714292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-15 00:00:00 AAPL -100 @ 13.8867864608765&amp;quot;&#10;## [1] &amp;quot;2011-12-23 00:00:00 AAPL 100 @ 14.4046430587769&amp;quot;&#10;## [1] &amp;quot;2012-05-07 00:00:00 AAPL -100 @ 20.3385715484619&amp;quot;&#10;## [1] &amp;quot;2012-06-19 00:00:00 AAPL 100 @ 20.9789295196533&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-03-29 00:00:00 AAPL -100 @ 12.5342855453491&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-15 00:00:00 AAPL -100 @ 19.7560710906982&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-27 00:00:00 AAPL -100 @ 6.24321413040161&amp;quot;&#10;## [1] &amp;quot;2007-11-29 00:00:00 AAPL 100 @ 6.58178615570068&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-14 00:00:00 AAPL -100 @ 9.0260705947876&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-21 00:00:00 AAPL -100 @ 12.1178569793701&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-10-12 00:00:00 AAPL -100 @ 14.3639287948608&amp;quot;&#10;## [1] &amp;quot;2011-10-13 00:00:00 AAPL 100 @ 14.5867862701416&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-23 00:00:00 AAPL 100 @ 14.4046430587769&amp;quot;&#10;## [1] &amp;quot;2012-05-08 00:00:00 AAPL -100 @ 20.292142868042&amp;quot;&#10;## [1] &amp;quot;2012-06-19 00:00:00 AAPL 100 @ 20.9789295196533&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-15 00:00:00 AAPL -100 @ 19.7560710906982&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-10 00:00:00 AAPL -100 @ 6.30821418762207&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-27 00:00:00 AAPL -100 @ 9.43142890930176&amp;quot;&#10;## [1] &amp;quot;2010-07-28 00:00:00 AAPL 100 @ 9.31999969482422&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-23 00:00:00 AAPL -100 @ 4.68107080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-03 00:00:00 AAPL -100 @ 6.07571411132812&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-28 00:00:00 AAPL -100 @ 12.5157136917114&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;=== testing param.combo 96 on 2013-01-02/2013-12-31&amp;quot;&#10;## nFAST nSLOW&#10;## 96 7 16&#10;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 15.8521432876587&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;=== training MA on 2007-01-03/2013-12-31&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-27 00:00:00 AAPL -100 @ 4.7232141494751&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-05 00:00:00 AAPL -100 @ 12.103214263916&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-17 00:00:00 AAPL -100 @ 18.9328575134277&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-15 00:00:00 AAPL -100 @ 8.98035717010498&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-08 00:00:00 AAPL -100 @ 20.292142868042&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2013-09-24 00:00:00 AAPL -100 @ 17.4678573608398&amp;quot;&#10;## [1] &amp;quot;2013-09-30 00:00:00 AAPL 100 @ 17.0267868041992&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-16 00:00:00 AAPL -100 @ 6.96535682678223&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-29 00:00:00 AAPL 100 @ 15.9925003051758&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-15 00:00:00 AAPL -100 @ 19.7560710906982&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 15.8521432876587&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-07-01 00:00:00 AAPL -100 @ 6.23857116699219&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-22 00:00:00 AAPL -100 @ 7.15571403503418&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-05 00:00:00 AAPL -100 @ 6.98071384429932&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-24 00:00:00 AAPL -100 @ 12.3203573226929&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-18 00:00:00 AAPL -100 @ 13.3907136917114&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-14 00:00:00 AAPL -100 @ 19.9364280700684&amp;quot;&#10;## [1] &amp;quot;2012-06-22 00:00:00 AAPL 100 @ 20.78928565979&amp;quot;&#10;## [1] &amp;quot;2012-10-16 00:00:00 AAPL -100 @ 23.2067852020264&amp;quot;&#10;## [1] &amp;quot;2013-05-13 00:00:00 AAPL 100 @ 16.2407131195068&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 16.1900005340576&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-16 00:00:00 AAPL -100 @ 4.18035697937012&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2007-11-27 00:00:00 AAPL -100 @ 6.24321413040161&amp;quot;&#10;## [1] &amp;quot;2007-11-29 00:00:00 AAPL 100 @ 6.58178615570068&amp;quot;&#10;## [1] &amp;quot;2008-01-15 00:00:00 AAPL -100 @ 6.03714323043823&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-25 00:00:00 AAPL -100 @ 6.33535718917847&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-14 00:00:00 AAPL -100 @ 9.0260705947876&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-21 00:00:00 AAPL -100 @ 12.1178569793701&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-10-12 00:00:00 AAPL -100 @ 14.3639287948608&amp;quot;&#10;## [1] &amp;quot;2011-10-13 00:00:00 AAPL 100 @ 14.5867862701416&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-23 00:00:00 AAPL 100 @ 14.4046430587769&amp;quot;&#10;## [1] &amp;quot;2012-05-08 00:00:00 AAPL -100 @ 20.292142868042&amp;quot;&#10;## [1] &amp;quot;2012-06-19 00:00:00 AAPL 100 @ 20.9789295196533&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-18 00:00:00 AAPL -100 @ 15.4203567504883&amp;quot;&#10;## [1] &amp;quot;2013-07-25 00:00:00 AAPL 100 @ 15.6607141494751&amp;quot;&#10;## [1] &amp;quot;2013-09-23 00:00:00 AAPL -100 @ 17.5228576660156&amp;quot;&#10;## [1] &amp;quot;2013-10-01 00:00:00 AAPL 100 @ 17.4271430969238&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-28 00:00:00 AAPL -100 @ 4.52928590774536&amp;quot;&#10;## [1] &amp;quot;2007-09-04 00:00:00 AAPL 100 @ 5.1485710144043&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-02 00:00:00 AAPL 100 @ 9.35178565979004&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-06 00:00:00 AAPL -100 @ 12.0728569030762&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-17 00:00:00 AAPL -100 @ 18.9328575134277&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-19 00:00:00 AAPL -100 @ 21.7800006866455&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-03-29 00:00:00 AAPL -100 @ 12.5342855453491&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-09 00:00:00 AAPL -100 @ 6.2232141494751&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-29 00:00:00 AAPL 100 @ 9.21821403503418&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-18 00:00:00 AAPL -100 @ 18.9421424865723&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2013-05-16 00:00:00 AAPL 100 @ 15.5207138061523&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-30 00:00:00 AAPL 100 @ 9.1875&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-18 00:00:00 AAPL -100 @ 18.9421424865723&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-19 00:00:00 AAPL -100 @ 21.7800006866455&amp;quot;&#10;## [1] &amp;quot;2013-05-16 00:00:00 AAPL 100 @ 15.5207138061523&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-09 00:00:00 AAPL -100 @ 6.2232141494751&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-29 00:00:00 AAPL 100 @ 9.21821403503418&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-18 00:00:00 AAPL -100 @ 18.9421424865723&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2013-05-16 00:00:00 AAPL 100 @ 15.5207138061523&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-20 00:00:00 AAPL -100 @ 14.8871431350708&amp;quot;&#10;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 16.1900005340576&amp;quot;&#10;## [1] &amp;quot;2007-03-20 00:00:00 AAPL 100 @ 3.26714301109314&amp;quot;&#10;## [1] &amp;quot;2007-08-21 00:00:00 AAPL -100 @ 4.55607080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-02 00:00:00 AAPL 100 @ 5.26749992370605&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-22 00:00:00 AAPL 100 @ 6.31392908096313&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-28 00:00:00 AAPL 100 @ 7.55749988555908&amp;quot;&#10;## [1] &amp;quot;2010-02-05 00:00:00 AAPL -100 @ 6.98071384429932&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-24 00:00:00 AAPL -100 @ 12.3203573226929&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-18 00:00:00 AAPL -100 @ 13.3907136917114&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-11 00:00:00 AAPL -100 @ 20.2396430969238&amp;quot;&#10;## [1] &amp;quot;2012-06-22 00:00:00 AAPL 100 @ 20.78928565979&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2013-05-13 00:00:00 AAPL 100 @ 16.2407131195068&amp;quot;&#10;## [1] &amp;quot;2013-06-20 00:00:00 AAPL -100 @ 14.8871431350708&amp;quot;&#10;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 16.1900005340576&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-03-29 00:00:00 AAPL -100 @ 12.5342855453491&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2013-05-17 00:00:00 AAPL 100 @ 15.4735708236694&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-02 00:00:00 AAPL 100 @ 16.5192852020264&amp;quot;&#10;## [1] &amp;quot;=== testing param.combo 14 on 2014-01-02/2014-05-30&amp;quot;&#10;## nFAST nSLOW&#10;## 14 5 7&#10;## [1] &amp;quot;2014-01-15 00:00:00 AAPL -100 @ 19.9057140350342&amp;quot;&#10;## [1] &amp;quot;2014-01-27 00:00:00 AAPL 100 @ 19.6607131958008&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-25 00:00:00 AAPL 100 @ 19.4639282226562&amp;quot;&#10;## [1] &amp;quot;2014-04-14 00:00:00 AAPL -100 @ 18.6314296722412&amp;quot;&#10;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 21.2175006866455&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-26 00:00:00 AAPL 100 @ 3.42321395874023&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-10 00:00:00 AAPL -100 @ 6.30821418762207&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-27 00:00:00 AAPL -100 @ 9.43142890930176&amp;quot;&#10;## [1] &amp;quot;2010-07-28 00:00:00 AAPL 100 @ 9.31999969482422&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2013-05-17 00:00:00 AAPL 100 @ 15.4735708236694&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2014-02-03 00:00:00 AAPL -100 @ 17.9117851257324&amp;quot;&#10;## [1] &amp;quot;2014-04-01 00:00:00 AAPL 100 @ 19.3446426391602&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-15 00:00:00 AAPL 100 @ 3.19892907142639&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-28 00:00:00 AAPL 100 @ 5.10750007629395&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-19 00:00:00 AAPL 100 @ 6.19750022888184&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-14 00:00:00 AAPL -100 @ 7.03499984741211&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-15 00:00:00 AAPL -100 @ 8.98035717010498&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-14 00:00:00 AAPL 100 @ 9.57357120513916&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 12.3671426773071&amp;quot;&#10;## [1] &amp;quot;2011-05-20 00:00:00 AAPL -100 @ 11.9721431732178&amp;quot;&#10;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 12.8467855453491&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-08 00:00:00 AAPL -100 @ 20.292142868042&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-11 00:00:00 AAPL -100 @ 22.4321422576904&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2013-09-24 00:00:00 AAPL -100 @ 17.4678573608398&amp;quot;&#10;## [1] &amp;quot;2013-09-30 00:00:00 AAPL 100 @ 17.0267868041992&amp;quot;&#10;## [1] &amp;quot;2014-01-16 00:00:00 AAPL -100 @ 19.7946434020996&amp;quot;&#10;## [1] &amp;quot;2014-01-27 00:00:00 AAPL 100 @ 19.6607131958008&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-25 00:00:00 AAPL 100 @ 19.4639282226562&amp;quot;&#10;## [1] &amp;quot;2014-04-14 00:00:00 AAPL -100 @ 18.6314296722412&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-16 00:00:00 AAPL -100 @ 6.96535682678223&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-29 00:00:00 AAPL 100 @ 15.9925003051758&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-20 00:00:00 AAPL -100 @ 14.8871431350708&amp;quot;&#10;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 16.1900005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-23 00:00:00 AAPL -100 @ 4.68107080459595&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-03 00:00:00 AAPL -100 @ 6.07571411132812&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-28 00:00:00 AAPL -100 @ 12.5157136917114&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 15.8521432876587&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2014-01-31 00:00:00 AAPL -100 @ 17.8785705566406&amp;quot;&#10;## [1] &amp;quot;2014-03-28 00:00:00 AAPL 100 @ 19.1735706329346&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-08 00:00:00 AAPL -100 @ 6.41249990463257&amp;quot;&#10;## [1] &amp;quot;2008-08-27 00:00:00 AAPL 100 @ 6.23821401596069&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-12 00:00:00 AAPL 100 @ 3.54535698890686&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-23 00:00:00 AAPL -100 @ 9.28357124328613&amp;quot;&#10;## [1] &amp;quot;2010-07-30 00:00:00 AAPL 100 @ 9.1875&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-07 00:00:00 AAPL -100 @ 12.0742864608765&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-18 00:00:00 AAPL -100 @ 18.9421424865723&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-19 00:00:00 AAPL -100 @ 21.7800006866455&amp;quot;&#10;## [1] &amp;quot;2013-05-16 00:00:00 AAPL 100 @ 15.5207138061523&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2014-01-31 00:00:00 AAPL -100 @ 17.8785705566406&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 AAPL 100 @ 19.1692867279053&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-22 00:00:00 AAPL 100 @ 3.3557140827179&amp;quot;&#10;## [1] &amp;quot;2007-08-24 00:00:00 AAPL -100 @ 4.83214282989502&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-04 00:00:00 AAPL 100 @ 5.46714305877686&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-21 00:00:00 AAPL -100 @ 9.07999992370605&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-03-29 00:00:00 AAPL -100 @ 12.5342855453491&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-16 00:00:00 AAPL -100 @ 19.502857208252&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2014-01-31 00:00:00 AAPL -100 @ 17.8785705566406&amp;quot;&#10;## [1] &amp;quot;2014-03-28 00:00:00 AAPL 100 @ 19.1735706329346&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-23 00:00:00 AAPL 100 @ 3.33999991416931&amp;quot;&#10;## [1] &amp;quot;2007-08-27 00:00:00 AAPL -100 @ 4.7232141494751&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-22 00:00:00 AAPL -100 @ 5.55857086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-07 00:00:00 AAPL 100 @ 5.56750011444092&amp;quot;&#10;## [1] &amp;quot;2008-07-07 00:00:00 AAPL -100 @ 6.25571393966675&amp;quot;&#10;## [1] &amp;quot;2008-08-26 00:00:00 AAPL 100 @ 6.20142889022827&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-11 00:00:00 AAPL 100 @ 3.45785689353943&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-09 00:00:00 AAPL -100 @ 7.00678586959839&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-22 00:00:00 AAPL -100 @ 9.25071430206299&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-19 00:00:00 AAPL -100 @ 8.92428588867188&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-05 00:00:00 AAPL -100 @ 12.103214263916&amp;quot;&#10;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 12.3807144165039&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 12.7864294052124&amp;quot;&#10;## [1] &amp;quot;2011-11-22 00:00:00 AAPL -100 @ 13.4467859268188&amp;quot;&#10;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 14.4685707092285&amp;quot;&#10;## [1] &amp;quot;2012-05-17 00:00:00 AAPL -100 @ 18.9328575134277&amp;quot;&#10;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 20.4296436309814&amp;quot;&#10;## [1] &amp;quot;2012-10-18 00:00:00 AAPL -100 @ 22.5942859649658&amp;quot;&#10;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 15.3160705566406&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 16.3099994659424&amp;quot;&#10;## [1] &amp;quot;2014-01-31 00:00:00 AAPL -100 @ 17.8785705566406&amp;quot;&#10;## [1] &amp;quot;2014-03-28 00:00:00 AAPL 100 @ 19.1735706329346&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-27 00:00:00 AAPL -100 @ 6.0746431350708&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-05 00:00:00 AAPL -100 @ 3.17285704612732&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-16 00:00:00 AAPL -100 @ 6.96535682678223&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-29 00:00:00 AAPL 100 @ 15.9925003051758&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-27 00:00:00 AAPL 100 @ 3.40928602218628&amp;quot;&#10;## [1] &amp;quot;2008-01-23 00:00:00 AAPL -100 @ 4.96678590774536&amp;quot;&#10;## [1] &amp;quot;2008-04-08 00:00:00 AAPL 100 @ 5.45857095718384&amp;quot;&#10;## [1] &amp;quot;2008-07-11 00:00:00 AAPL -100 @ 6.16357088088989&amp;quot;&#10;## [1] &amp;quot;2008-08-28 00:00:00 AAPL 100 @ 6.20499992370605&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-13 00:00:00 AAPL 100 @ 3.54142904281616&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-20 00:00:00 AAPL 100 @ 3.62821388244629&amp;quot;&#10;## [1] &amp;quot;2010-02-10 00:00:00 AAPL -100 @ 6.96857118606567&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-08-20 00:00:00 AAPL -100 @ 8.91571426391602&amp;quot;&#10;## [1] &amp;quot;2010-09-17 00:00:00 AAPL 100 @ 9.83464336395264&amp;quot;&#10;## [1] &amp;quot;2011-04-08 00:00:00 AAPL -100 @ 11.9664287567139&amp;quot;&#10;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 12.41428565979&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 12.7775001525879&amp;quot;&#10;## [1] &amp;quot;2011-11-23 00:00:00 AAPL -100 @ 13.106785774231&amp;quot;&#10;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 14.4642858505249&amp;quot;&#10;## [1] &amp;quot;2012-05-21 00:00:00 AAPL -100 @ 20.0457134246826&amp;quot;&#10;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 20.5178565979004&amp;quot;&#10;## [1] &amp;quot;2012-10-22 00:00:00 AAPL -100 @ 22.643928527832&amp;quot;&#10;## [1] &amp;quot;2013-05-17 00:00:00 AAPL 100 @ 15.4735708236694&amp;quot;&#10;## [1] &amp;quot;2013-06-24 00:00:00 AAPL -100 @ 14.376428604126&amp;quot;&#10;## [1] &amp;quot;2013-08-02 00:00:00 AAPL 100 @ 16.5192852020264&amp;quot;&#10;## [1] &amp;quot;2014-02-03 00:00:00 AAPL -100 @ 17.9117851257324&amp;quot;&#10;## [1] &amp;quot;2014-04-01 00:00:00 AAPL 100 @ 19.3446426391602&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#10;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#10;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#10;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#10;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#10;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#10;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#10;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#10;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#10;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#10;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#10;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#10;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#10;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#10;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#10;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#10;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#10;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#10;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#10;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-19 00:00:00 AAPL 100 @ 3.25464296340942&amp;quot;&#10;## [1] &amp;quot;2007-08-20 00:00:00 AAPL -100 @ 4.36499977111816&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-17 00:00:00 AAPL -100 @ 5.74607086181641&amp;quot;&#10;## [1] &amp;quot;2008-04-01 00:00:00 AAPL 100 @ 5.34035682678223&amp;quot;&#10;## [1] &amp;quot;2008-06-30 00:00:00 AAPL -100 @ 5.98000001907349&amp;quot;&#10;## [1] &amp;quot;2008-08-21 00:00:00 AAPL 100 @ 6.22464323043823&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-09 00:00:00 AAPL 100 @ 3.66107106208801&amp;quot;&#10;## [1] &amp;quot;2009-03-06 00:00:00 AAPL -100 @ 3.0464289188385&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2009-12-18 00:00:00 AAPL -100 @ 6.97964286804199&amp;quot;&#10;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#10;## [1] &amp;quot;2010-02-04 00:00:00 AAPL -100 @ 6.85892915725708&amp;quot;&#10;## [1] &amp;quot;2010-03-05 00:00:00 AAPL 100 @ 7.81964302062988&amp;quot;&#10;## [1] &amp;quot;2010-07-19 00:00:00 AAPL -100 @ 8.77071380615234&amp;quot;&#10;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#10;## [1] &amp;quot;2011-03-23 00:00:00 AAPL -100 @ 12.1139287948608&amp;quot;&#10;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 12.4846429824829&amp;quot;&#10;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#10;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#10;## [1] &amp;quot;2011-11-17 00:00:00 AAPL -100 @ 13.478928565979&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-10 00:00:00 AAPL -100 @ 20.3757133483887&amp;quot;&#10;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 20.6310710906982&amp;quot;&#10;## [1] &amp;quot;2012-10-15 00:00:00 AAPL -100 @ 22.6700000762939&amp;quot;&#10;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 16.1774997711182&amp;quot;&#10;## [1] &amp;quot;2013-06-20 00:00:00 AAPL -100 @ 14.8871431350708&amp;quot;&#10;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 16.1900005340576&amp;quot;&#10;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#10;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#10;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-15 00:00:00 AAPL -100 @ 19.7560710906982&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 15.8521432876587&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2014-01-30 00:00:00 AAPL -100 @ 17.8492851257324&amp;quot;&#10;## [1] &amp;quot;2014-03-27 00:00:00 AAPL 100 @ 19.1949996948242&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#10;## [1] &amp;quot;2007-03-21 00:00:00 AAPL 100 @ 3.35249996185303&amp;quot;&#10;## [1] &amp;quot;2007-08-22 00:00:00 AAPL -100 @ 4.73250007629395&amp;quot;&#10;## [1] &amp;quot;2007-09-06 00:00:00 AAPL 100 @ 4.82178592681885&amp;quot;&#10;## [1] &amp;quot;2008-01-18 00:00:00 AAPL -100 @ 5.76285696029663&amp;quot;&#10;## [1] &amp;quot;2008-04-03 00:00:00 AAPL 100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2008-07-02 00:00:00 AAPL -100 @ 6.00642919540405&amp;quot;&#10;## [1] &amp;quot;2008-08-25 00:00:00 AAPL 100 @ 6.16249990463257&amp;quot;&#10;## [1] &amp;quot;2008-09-10 00:00:00 AAPL -100 @ 5.41464281082153&amp;quot;&#10;## [1] &amp;quot;2009-02-10 00:00:00 AAPL 100 @ 3.49392890930176&amp;quot;&#10;## [1] &amp;quot;2009-03-09 00:00:00 AAPL -100 @ 2.96821403503418&amp;quot;&#10;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#10;## [1] &amp;quot;2010-02-08 00:00:00 AAPL -100 @ 6.93285703659058&amp;quot;&#10;## [1] &amp;quot;2010-03-08 00:00:00 AAPL 100 @ 7.82428598403931&amp;quot;&#10;## [1] &amp;quot;2010-07-20 00:00:00 AAPL -100 @ 8.99607086181641&amp;quot;&#10;## [1] &amp;quot;2010-08-04 00:00:00 AAPL 100 @ 9.39214324951172&amp;quot;&#10;## [1] &amp;quot;2010-08-18 00:00:00 AAPL -100 @ 9.0382137298584&amp;quot;&#10;## [1] &amp;quot;2010-09-16 00:00:00 AAPL 100 @ 9.8774995803833&amp;quot;&#10;## [1] &amp;quot;2011-03-25 00:00:00 AAPL -100 @ 12.5550003051758&amp;quot;&#10;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 12.3839292526245&amp;quot;&#10;## [1] &amp;quot;2011-05-24 00:00:00 AAPL -100 @ 11.8639287948608&amp;quot;&#10;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 12.6339292526245&amp;quot;&#10;## [1] &amp;quot;2011-11-21 00:00:00 AAPL -100 @ 13.1789293289185&amp;quot;&#10;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 14.3800001144409&amp;quot;&#10;## [1] &amp;quot;2012-05-15 00:00:00 AAPL -100 @ 19.7560710906982&amp;quot;&#10;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 20.3846435546875&amp;quot;&#10;## [1] &amp;quot;2012-10-17 00:00:00 AAPL -100 @ 23.021785736084&amp;quot;&#10;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 15.8521432876587&amp;quot;&#10;## [1] &amp;quot;2013-06-21 00:00:00 AAPL -100 @ 14.7678565979004&amp;quot;&#10;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 16.1617851257324&amp;quot;&#10;## [1] &amp;quot;2014-01-30 00:00:00 AAPL -100 @ 17.8492851257324&amp;quot;&#10;## [1] &amp;quot;2014-03-27 00:00:00 AAPL 100 @ 19.1949996948242&amp;quot;&#10;## [1] &amp;quot;2014-04-16 00:00:00 AAPL -100 @ 18.5360717773438&amp;quot;&#10;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;wf_end &amp;lt;- Sys.time() # 记录结束时间&#10;&#10;# 输出前进测试耗时和总迭代次数&#10;cat(&amp;quot;\n Running the walk forward search: \n &amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Running the walk forward search: &#10;## &lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;print(wf_end - wf_start) # 打印总耗时&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Time difference of 45.06131 secs&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot; Total trials:&amp;quot;, .strategy$macd$trials, &amp;quot;\n&amp;quot;) # 打印总迭代次数&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Total trials: 98&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取并打印交易统计结果&#10;wfa.stats &amp;lt;- wfresults$tradeStats&#10;print(wfa.stats)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Portfolio Symbol Num.Txns Num.Trades Net.Trading.PL Avg.Trade.PL&#10;## AAPL test.wf.macd AAPL 23 11 1235.214 99.65908&#10;## Med.Trade.PL Largest.Winner Largest.Loser Gross.Profits Gross.Losses&#10;## AAPL 89.50005 577.3214 -177.6785 1540.5 -444.2499&#10;## Std.Dev.Trade.PL Std.Err.Trade.PL Percent.Positive Percent.Negative&#10;## AAPL 226.9792 68.4368 54.54545 45.45455&#10;## Profit.Factor Avg.Win.Trade Med.Win.Trade Avg.Losing.Trade&#10;## AAPL 3.467642 256.75 191.0535 -88.84998&#10;## Med.Losing.Trade Avg.Daily.PL Med.Daily.PL Std.Dev.Daily.PL&#10;## AAPL -83.24986 99.65908 89.50005 226.9792&#10;## Std.Err.Daily.PL Ann.Sharpe Max.Drawdown Profit.To.Max.Draw&#10;## AAPL 68.4368 6.969973 -381.3216 3.239298&#10;## Avg.WinLoss.Ratio Med.WinLoss.Ratio Max.Equity Min.Equity End.Equity&#10;## AAPL 2.889702 2.294941 1402.643 0 1235.214&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化前进测试结果&#10;chart.forward(wfresults)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/06/10/macdwfa/index_files/figure-html/WalkForward-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;策略核心思路的解析围绕前进测试的实现逻辑、MACD 指标的应用以及参数优化方法展开，共同构成了一套科学验证策略有效性的框架。&lt;/p&gt;&#10;&lt;p&gt;前进测试作为一种动态优化策略参数的方法，其核心逻辑在于将历史数据划分为连续的 “训练期” 和 “测试期”。其中，训练期在代码中设定为 36 个月，用于在该时间段内优化策略参数 —— 例如 MACD 的快速与慢速 EMA 周期，从而寻找表现最佳的参数组合；而测试期则为 12 个月，作用是使用训练期优化得到的参数在该时间段验证策略表现，确保这些参数在未见过的数据上依然有效。整个过程按 “月” 为单位滚动窗口，不断重复 “训练 - 测试” 的迭代，最终汇总所有测试期的结果以评估策略的稳健性。这种方法相比传统的一次性回测，能有效减少过拟合风险，因为参数需要在连续的未知数据中得到验证，更接近实盘交易中 “用历史经验指导未来” 的实际场景。&lt;/p&gt;&#10;&lt;p&gt;MACD 指标作为策略的核心判断依据，是一种结合趋势和动量的技术指标，其计算逻辑包含多个部分。快速 EMA 是短期指数移动平均线，在代码中周期范围为 1-10 日，用于反映近期价格趋势；慢速 EMA 则是长期指数移动平均线，周期范围为 5-25 日，代表中长期趋势；MACD 线由快速 EMA 与慢速 EMA 的差值计算得出，用于衡量两者的收敛与发散程度；信号线则是 MACD 线的 9 日 EMA，起到平滑波动并生成交易信号的作用。策略通常基于 MACD 线与信号线的交叉产生交易信号：当 MACD 线上穿信号线时，即 “金叉”，视为买入信号，暗示趋势可能由弱转强；当 MACD 线下穿信号线时，即 “死叉”，视为卖出信号，意味着趋势可能由强转弱。代码中通过 add.distribution 定义了快速与慢速 EMA 的参数范围，并添加了 “快速 EMA &amp;lt; 慢速 EMA” 的约束，这是因为 MACD 的核心在于捕捉短期与长期趋势的差异，若快速周期大于慢速周期，指标的逻辑基础将失效。&lt;/p&gt;&#10;&lt;p&gt;在参数优化与并行计算方面，为避免遍历所有参数组合 —— 例如代码中 10 种快速 EMA 周期与 21 种慢速 EMA 周期组合会产生 210 种可能，耗时较长 —— 代码通过 nsamples=15 设置每次迭代随机抽样 15 组参数，在保证效率的同时覆盖较优组合。若启用并行计算，如 doParallel 等工具，可进一步缩短计算时间，尤其适用于参数范围较广的场景。&lt;/p&gt;&#10;&lt;p&gt;这段代码的核心价值在于演示了如何通过前进测试验证 MACD 策略的稳健性。它能够降低过拟合风险，通过滚动测试确保参数在不同市场周期中均有效，避免单一回测的偶然性；支持参数动态优化，允许策略参数随市场环境变化调整，比如不同时期的最佳 EMA 周期可能不同，从而增强策略的适应性；同时具备可视化与可解释性，通过 chart.forward 直观展示各阶段的策略表现，结合 tradeStats 分析胜率、盈亏比等关键指标，为策略改进提供依据。&lt;/p&gt;&#10;&lt;p&gt;对于量化交易者而言，这种方法提供了一套科学验证策略有效性的框架，不仅关注策略的历史表现，更强调其在未来未知数据中的可靠性，通过结合 MACD 指标对趋势和动量的捕捉能力，前进测试能帮助筛选出真正具备实战价值的参数组合，为实盘交易提供更可靠的决策依据。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>基于 quantstrat 的配对交易策略实现与解析</title><link>https://gewutang.cn/2015/04/10/pair-trade/</link><pubDate>Fri, 10 Apr 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/04/10/pair-trade/</guid><description>&#10;&#10;&#10;&lt;p&gt;配对交易是一种基于统计套利的量化策略，其核心逻辑是通过识别价格走势高度相关的两只股票，当它们的价格偏离历史正常关系时建立对冲头寸，等待关系回归时获利。&lt;/p&gt;&#10;&lt;p&gt;本文将解析一段基于 R 语言 quantstrat 包的配对交易策略代码，展示其实现思路与核心意义。&lt;/p&gt;&#10;&lt;div id="策略核心逻辑与代码框架" class="section level1"&gt;&#10;&lt;h1&gt;策略核心逻辑与代码框架&lt;/h1&gt;&#10;&lt;p&gt;这段代码以 SPY 和 DIA 两只 ETF 为交易标的，通过构建价格比率的均值回归模型实现配对交易。策略的核心步骤包括：数据同步与准备、策略环境初始化、指标与信号构建、自定义订单规模控制、交易规则设置，以及回测与绩效分析。&lt;/p&gt;&#10;&lt;p&gt;以下是完整代码实现：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# Kindly contributed to quantstrat by Garrett See&#10;# code borrowed heavily from existing quantstrat demos&#10;&#10;# 这是一个简单的配对交易示例，旨在展示如何扩展现有 quantstrat 功能。&#10;# 它使用 addPosLimits 指定头寸水平和限制，并演示如何将自定义订单规模函数传递给 osFUN&#10;&#10;# 注意：先构建价差并将其视为单一工具，比处理股票组合更简单&#10;&#10;## 给定两只股票，计算其名义价值比率。如果比率低于 2 标准差区间，&#10;# 当它回升穿过区间时，买入股票 1 并卖出股票 2。如果比率高于 2 标准差区间，&#10;# 当它回落穿过区间时，卖出股票 1 并买入股票 2。如果比率穿过其移动平均线，则平仓所有头寸。&#10;&#10;# 股票 A 的买入（卖出）数量 = MaxPos / lvls&#10;# 股票 B 的卖出（买入）数量 = MaxPos * 比率 / lvls &#10;&#10;require(quantstrat)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#10;## method from&#10;## as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#10;## &#10;## legend&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;suppressWarnings(rm(&amp;quot;order_book.pair1&amp;quot;,pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.pairs&amp;quot;, &amp;quot;portfolio.pair1&amp;quot;, pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;startDate&amp;quot;, &amp;quot;endDate&amp;quot;, &amp;quot;startDate&amp;quot;, &amp;quot;initEq&amp;quot;, &amp;quot;SD&amp;quot;, &amp;quot;N&amp;quot;, &#10; &amp;quot;symb1&amp;quot;, &amp;quot;symb2&amp;quot;, &amp;quot;portfolio1.st&amp;quot;, &amp;quot;account.st&amp;quot;, &#10; &amp;quot;pairStrat&amp;quot;, &amp;quot;out1&amp;quot;))&#10;&#10;initDate &amp;lt;- &amp;#39;2013-12-31&amp;#39; &#10;startDate &amp;lt;- &amp;#39;2014-01-01&amp;#39;&#10;endDate &amp;lt;- &amp;#39;2015-05-01&amp;#39;&#10;initEq &amp;lt;- 100000&#10;SD &amp;lt;- 2 # 标准差倍数&#10;N &amp;lt;- 20 # 移动平均线周期&#10;&#10;MaxPos &amp;lt;- 1500 # 股票 A 的最大头寸；&#10;# 股票 B 的最大头寸将是 max * 比率，即对股票 B 没有硬性头寸限制&#10;lvls &amp;lt;- 3 # 分几步建仓；每笔订单数量 = MaxPos/lvls&#10;&#10;symb1 &amp;lt;- &amp;#39;SPY&amp;#39; # 可更改这些标的以尝试其他配对&#10;symb2 &amp;lt;- &amp;#39;DIA&amp;#39; # 如果更改，需确保头寸限制仍然合理&#10;&#10;portfolio1.st &amp;lt;- &amp;#39;pair1&amp;#39;&#10;account.st &amp;lt;- &amp;#39;pairs&amp;#39;&#10;&#10;# 获取历史数据&#10;getSymbols(c(symb1, symb2), from=startDate, to=endDate, adjust=TRUE) &lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot; &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 以下函数用于确保所有标的的时间戳相同，删除其中一只股票数据缺失的行&#10;alignSymbols &amp;lt;- function(symbols, env=.GlobalEnv) {&#10; # 这是 qmao::alignSymbols() 的简化版本&#10; if (length(symbols) &amp;lt; 2) &#10; stop(&amp;quot;必须提供至少 2 个标的&amp;quot;)&#10; if (any(!is.character(symbols))) &#10; stop(&amp;quot;标的必须是字符向量。&amp;quot;)&#10; ff &amp;lt;- get(symbols[1],env=env)&#10; for (sym in symbols[-1]) {&#10; tmp.sym &amp;lt;- get(sym,env=env)&#10; ff &amp;lt;- merge(ff, tmp.sym, all=FALSE) # 只保留双方都有数据的行&#10; }&#10; for (sym in symbols) {&#10; assign(sym,ff[,grep(sym, colnames(ff))], env=env) # 更新标的数据&#10; }&#10; symbols&#10;}&#10;alignSymbols(c(symb1, symb2)) # 对齐两只股票的时间戳&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot; &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义金融工具&#10;currency(&amp;quot;USD&amp;quot;) # 基础货币为美元&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;stock(symb1, currency=&amp;quot;USD&amp;quot;, multiplier=1) # 定义股票 1 的属性&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;stock(symb2, currency=&amp;quot;USD&amp;quot;, multiplier=1) # 定义股票 2 的属性&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 初始化投资组合、账户和订单&#10;initPortf(name=portfolio1.st, c(symb1,symb2)) # 创建包含两只股票的组合&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pair1&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio1.st, initEq=initEq) # 初始化账户，初始资金 10 万美元&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;initOrders(portfolio=portfolio1.st) # 初始化订单簿&#10;&#10;# osFUN 需要知道哪个标的是第一腿、哪个是第二腿，以及 MaxPos 和 lvls 的值。&#10;# 因此，在投资组合中创建一个插槽来存储这些信息。&#10;pair &amp;lt;- c(1, 2, MaxPos, lvls)&#10;names(pair) &amp;lt;- c(symb1, symb2, &amp;quot;MaxPos&amp;quot;, &amp;quot;lvls&amp;quot;)&#10;.blotter[[paste(&amp;#39;portfolio&amp;#39;, portfolio1.st, sep=&amp;#39;.&amp;#39;)]]$pair &amp;lt;- pair # 存储到组合属性中&#10;&#10;# 按标的创建初始头寸限制和水平&#10;# 如果 lvls=3，则允许多空各 3 次建仓。&#10;addPosLimit(portfolio=portfolio1.st, timestamp=startDate, symbol=symb1, &#10; maxpos=MaxPos, longlevels=lvls, minpos=-MaxPos, shortlevels=lvls)&#10;addPosLimit(portfolio=portfolio1.st, timestamp=startDate, symbol=symb2, &#10; maxpos=MaxPos, longlevels=lvls, minpos=-MaxPos, shortlevels=lvls)&#10;&#10;# 创建策略对象&#10;pairStrat &amp;lt;- strategy(&amp;#39;pairStrat&amp;#39;)&#10;&#10;# 指标函数：计算两只股票的价格比率&#10;calcRatio &amp;lt;- function(x) { &#10; # 返回两只股票的名义收盘价比率&#10; x1 &amp;lt;- get(x[1])&#10; x2 &amp;lt;- get(x[2])&#10; mult1 &amp;lt;- getInstrument(x[1])$multiplier # 合约乘数&#10; mult2 &amp;lt;- getInstrument(x[2])$multiplier&#10; rat &amp;lt;- (mult1 * Cl(x1)) / (mult2 * Cl(x2)) # 收盘价比率&#10; colnames(rat) &amp;lt;- &amp;#39;Ratio&amp;#39;&#10; rat&#10;}&#10;# 用于确定入场/出场的指标：比率及其布林带&#10;Ratio &amp;lt;- calcRatio(c(symb1[1], symb2[1])) &#10;&#10;# 将对冲比率存储在投资组合中，以便订单规模函数使用。&#10;# 在本示例中，对冲比率与 Ratio 指标相同。&#10;.blotter[[paste(&amp;#39;portfolio&amp;#39;,portfolio1.st,sep=&amp;#39;.&amp;#39;)]]$HedgeRatio &amp;lt;- Ratio&#10;# 创建获取最新对冲比率的函数&#10;getHedgeRatio &amp;lt;- function(portfolio, timestamp) {&#10; portf &amp;lt;- getPortfolio(portfolio)&#10; timestamp &amp;lt;- format(timestamp,&amp;quot;%Y-%m-%d %H:%M:%S&amp;quot;) # 格式化时间戳&#10; # 上述行确保在使用日内数据且时间戳为午夜时，不会获取次日的最后一个值&#10; toDate &amp;lt;- paste(&amp;quot;::&amp;quot;, timestamp, sep=&amp;quot;&amp;quot;)&#10; Ratio &amp;lt;- last(portf$HedgeRatio[toDate]) # 获取该时间点的最新比率&#10; as.numeric(Ratio)&#10;}&#10;&#10;# 添加指标：比率的布林带&#10;pairStrat &amp;lt;- add.indicator(strategy=pairStrat, name = &amp;quot;calcRatio&amp;quot;, &#10; arguments=list(x=c(symb1,symb2)))&#10;pairStrat &amp;lt;- add.indicator(strategy=pairStrat, name = &amp;quot;BBands&amp;quot;, &#10; arguments=list(HLC=quote(Ratio), sd=SD, n=N, &#10; maType=&amp;#39;SMA&amp;#39;)) # 基于 20 日 SMA 计算布林带&#10;&#10;# 生成信号：当比率从下方穿过下轨时买入，从上方穿过上轨时卖出，穿过中轨时平仓&#10;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#10; arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;up&amp;quot;), &#10; relationship=&amp;quot;lt&amp;quot;), # 比率 &amp;lt; 上轨（从上方穿过）&#10; label=&amp;quot;cross.up&amp;quot;)&#10;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#10; arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;dn&amp;quot;), &#10; relationship=&amp;quot;gt&amp;quot;), # 比率 &amp;gt; 下轨（从下方穿过）&#10; label=&amp;quot;cross.dn&amp;quot;)&#10;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#10; arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;mavg&amp;quot;), &#10; relationship=&amp;quot;lt&amp;quot;), # 比率 &amp;lt; 中轨（从上方穿过）&#10; label=&amp;quot;cross.mid.fa&amp;quot;)&#10;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#10; arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;mavg&amp;quot;), &#10; relationship=&amp;quot;gt&amp;quot;), # 比率 &amp;gt; 中轨（从下方穿过）&#10; label=&amp;quot;cross.mid.fb&amp;quot;)&#10;&#10;# 自定义订单规模函数&#10;#######################_ORDER SIZING FUNCTION_##################################&#10;osSpreadMaxPos &amp;lt;- function (data, timestamp, orderqty, ordertype, orderside, &#10; portfolio, symbol, ruletype, ..., orderprice) {&#10; portf &amp;lt;- getPortfolio(portfolio)&#10; # 检查 pair 插槽是否包含该函数所需的内容&#10; if (!any(portf$pair == 1) &amp;amp;&amp;amp; !(any(portf$pair == 2))) &#10; stop(&amp;#39;pair 必须包含值 1 和 2&amp;#39;)&#10; if (!any(names(portf$pair) == &amp;quot;MaxPos&amp;quot;) || !any(names(portf$pair) == &amp;quot;lvls&amp;quot;)) &#10; stop(&amp;#39;pair 必须包含 MaxPos 和 lvls&amp;#39;) &#10; &#10; if (portf$pair[symbol] == 1) legside &amp;lt;- &amp;quot;long&amp;quot; # 第一腿&#10; if (portf$pair[symbol] == 2) legside &amp;lt;- &amp;quot;short&amp;quot; # 第二腿（与第一腿方向相反）&#10; MaxPos &amp;lt;- portf$pair[&amp;quot;MaxPos&amp;quot;]&#10; lvls &amp;lt;- portf$pair[&amp;quot;lvls&amp;quot;]&#10; ratio &amp;lt;- getHedgeRatio(portfolio, timestamp) # 获取当前对冲比率&#10; pos &amp;lt;- getPosQty(portfolio, symbol, timestamp) # 当前头寸&#10; PosLimit &amp;lt;- getPosLimit(portfolio, symbol, timestamp) # 头寸限制&#10; qty &amp;lt;- orderqty&#10; if (legside == &amp;quot;short&amp;quot;) {# 标的是第二腿&#10; ## 注释掉下一行可使每只股票的订单规模相等。&#10; addPosLimit(portfolio=portfolio, timestamp=timestamp, symbol=symbol, &#10; maxpos=round(MaxPos*ratio,0), longlevels=lvls, &#10; minpos=round(-MaxPos*ratio,0), shortlevels=lvls) # 调整第二腿的头寸限制&#10; ## &#10; qty &amp;lt;- -orderqty # 反转股票 B 的订单数量（多空相反）&#10; }&#10; &#10; if (qty &amp;gt; 0) orderside = &amp;#39;long&amp;#39; # 确定订单方向&#10; if (qty &amp;lt; 0) orderside = &amp;#39;short&amp;#39;&#10; &#10; # 调用 osMaxPos 计算符合头寸限制的订单数量&#10; orderqty &amp;lt;- osMaxPos(data=data,timestamp=timestamp, orderqty=qty,&#10; ordertype=ordertype, orderside=orderside,&#10; portfolio=portfolio, symbol=symbol, ruletype=ruletype, &#10; ...)&#10; &#10; # 在规则信号函数中添加订单&#10; if (!is.null(orderqty) &amp;amp; !orderqty == 0 &amp;amp; !is.null(orderprice)) {&#10; addOrder(portfolio=portfolio, symbol=symbol, &#10; timestamp=timestamp, qty=orderqty, price=as.numeric(orderprice), &#10; ordertype=ordertype, side=orderside, replace=FALSE,&#10; status=&amp;quot;open&amp;quot;, ...=...)&#10; }&#10; return(0) # 使 ruleSignal 函数不会尝试下单&#10;}&#10;################################################################################&#10;&#10;# 为多空方向创建入场和出场规则。两个标的将获得相同的买卖信号，&#10;# 但 osMaxPos 会为第二个标的反转这些信号。&#10;# orderqty 大于头寸限制允许的值。osMaxPos 会将订单数量调整为最大允许值的 1/3。&#10;# （1/3 是因为我们在 PosLimit 中使用了 3 个水平）&#10;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments=list(sigcol=&amp;quot;cross.dn&amp;quot;, sigval=TRUE, &#10; orderqty=1e6, ordertype=&amp;#39;market&amp;#39;, &#10; orderside=NULL, osFUN=&amp;#39;osSpreadMaxPos&amp;#39;), &#10; type=&amp;#39;enter&amp;#39;) # cross.dn 信号触发入场&#10;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments=list(sigcol=&amp;quot;cross.up&amp;quot;, sigval=TRUE, &#10; orderqty=-1e6, ordertype=&amp;#39;market&amp;#39;, &#10; orderside=NULL, osFUN=&amp;#39;osSpreadMaxPos&amp;#39;), &#10; type=&amp;#39;enter&amp;#39;) # cross.up 信号触发入场&#10;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments=list(sigcol=&amp;quot;cross.mid.fb&amp;quot;, sigval=TRUE, &#10; orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, &#10; orderside=NULL), &#10; type=&amp;#39;exit&amp;#39;) # cross.mid.fb 信号触发出场&#10;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#10; arguments=list(sigcol=&amp;quot;cross.mid.fa&amp;quot;, sigval=TRUE, &#10; orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, &#10; orderside=NULL), &#10; type=&amp;#39;exit&amp;#39;) # cross.mid.fa 信号触发出场&#10;&#10;&#10;# 执行策略回测&#10;out1&amp;lt;-applyStrategy(strategy=pairStrat, portfolios=portfolio1.st)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-02-04 00:00:00 DIA 500 @ 150.182621296714&amp;quot;&#10;## [1] &amp;quot;2014-03-07 00:00:00 DIA 568 @ 160.616223229789&amp;quot;&#10;## [1] &amp;quot;2014-03-17 00:00:00 DIA 573 @ 158.690860184405&amp;quot;&#10;## [1] &amp;quot;2014-03-25 00:00:00 DIA -1641 @ 159.963991320726&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 DIA -574 @ 160.796174254035&amp;quot;&#10;## [1] &amp;quot;2014-04-22 00:00:00 DIA 574 @ 161.4743809578&amp;quot;&#10;## [1] &amp;quot;2014-05-22 00:00:00 DIA 571 @ 162.261848260392&amp;quot;&#10;## [1] &amp;quot;2014-06-02 00:00:00 DIA 572 @ 164.246166224292&amp;quot;&#10;## [1] &amp;quot;2014-06-27 00:00:00 DIA 576 @ 165.51814455118&amp;quot;&#10;## [1] &amp;quot;2014-07-02 00:00:00 DIA 27 @ 166.728744615667&amp;quot;&#10;## [1] &amp;quot;2014-07-15 00:00:00 DIA -1746 @ 167.69330191996&amp;quot;&#10;## [1] &amp;quot;2014-07-21 00:00:00 DIA -583 @ 167.676368799237&amp;quot;&#10;## [1] &amp;quot;2014-07-24 00:00:00 DIA 583 @ 167.932553399241&amp;quot;&#10;## [1] &amp;quot;2014-09-17 00:00:00 DIA -580 @ 169.355777408222&amp;quot;&#10;## [1] &amp;quot;2014-09-24 00:00:00 DIA -584 @ 169.969729387294&amp;quot;&#10;## [1] &amp;quot;2014-10-15 00:00:00 DIA -582 @ 159.57109738308&amp;quot;&#10;## [1] &amp;quot;2014-10-21 00:00:00 DIA 1746 @ 164.043208292008&amp;quot;&#10;## [1] &amp;quot;2014-10-27 00:00:00 DIA 576 @ 166.101170700567&amp;quot;&#10;## [1] &amp;quot;2014-11-05 00:00:00 DIA -576 @ 172.700494456876&amp;quot;&#10;## [1] &amp;quot;2014-12-03 00:00:00 DIA -586 @ 177.512433609148&amp;quot;&#10;## [1] &amp;quot;2014-12-10 00:00:00 DIA -579 @ 173.872106214788&amp;quot;&#10;## [1] &amp;quot;2014-12-18 00:00:00 DIA 1165 @ 176.351884195197&amp;quot;&#10;## [1] &amp;quot;2015-01-15 00:00:00 DIA -579 @ 172.030741759151&amp;quot;&#10;## [1] &amp;quot;2015-01-21 00:00:00 DIA 579 @ 174.456324855292&amp;quot;&#10;## [1] &amp;quot;2015-01-29 00:00:00 DIA 578 @ 173.013702717688&amp;quot;&#10;## [1] &amp;quot;2015-02-05 00:00:00 DIA -578 @ 177.640012837652&amp;quot;&#10;## [1] &amp;quot;2015-04-21 00:00:00 DIA 583 @ 179.259994506836&amp;quot;&#10;## [1] &amp;quot;2015-04-28 00:00:00 DIA 583 @ 180.839996337891&amp;quot;&#10;## [1] &amp;quot;2014-02-04 00:00:00 SPY -500 @ 171.264049909226&amp;quot;&#10;## [1] &amp;quot;2014-03-07 00:00:00 SPY -500 @ 183.831285747939&amp;quot;&#10;## [1] &amp;quot;2014-03-17 00:00:00 SPY -500 @ 181.946695046646&amp;quot;&#10;## [1] &amp;quot;2014-03-25 00:00:00 SPY 1500 @ 182.730103228311&amp;quot;&#10;## [1] &amp;quot;2014-03-31 00:00:00 SPY 500 @ 183.416649931591&amp;quot;&#10;## [1] &amp;quot;2014-04-22 00:00:00 SPY -500 @ 184.279745767482&amp;quot;&#10;## [1] &amp;quot;2014-05-22 00:00:00 SPY -500 @ 185.94707775133&amp;quot;&#10;## [1] &amp;quot;2014-06-02 00:00:00 SPY -500 @ 189.193474635518&amp;quot;&#10;## [1] &amp;quot;2014-06-27 00:00:00 SPY -500 @ 192.977678507185&amp;quot;&#10;## [1] &amp;quot;2014-07-15 00:00:00 SPY 1500 @ 194.367200918673&amp;quot;&#10;## [1] &amp;quot;2014-07-21 00:00:00 SPY 500 @ 194.475604869373&amp;quot;&#10;## [1] &amp;quot;2014-07-24 00:00:00 SPY -500 @ 195.766587803966&amp;quot;&#10;## [1] &amp;quot;2014-09-17 00:00:00 SPY 500 @ 197.83611230376&amp;quot;&#10;## [1] &amp;quot;2014-09-24 00:00:00 SPY 500 @ 197.582667853886&amp;quot;&#10;## [1] &amp;quot;2014-10-15 00:00:00 SPY 500 @ 184.582760931563&amp;quot;&#10;## [1] &amp;quot;2014-10-21 00:00:00 SPY -1500 @ 192.147074898032&amp;quot;&#10;## [1] &amp;quot;2014-10-27 00:00:00 SPY -500 @ 194.216362617501&amp;quot;&#10;## [1] &amp;quot;2014-11-05 00:00:00 SPY 500 @ 200.335121161983&amp;quot;&#10;## [1] &amp;quot;2014-12-03 00:00:00 SPY 500 @ 205.830132301385&amp;quot;&#10;## [1] &amp;quot;2014-12-10 00:00:00 SPY 500 @ 201.147003486903&amp;quot;&#10;## [1] &amp;quot;2014-12-18 00:00:00 SPY -1000 @ 204.731130073504&amp;quot;&#10;## [1] &amp;quot;2015-01-15 00:00:00 SPY 500 @ 198.135576473045&amp;quot;&#10;## [1] &amp;quot;2015-01-21 00:00:00 SPY -500 @ 202.177531751319&amp;quot;&#10;## [1] &amp;quot;2015-01-29 00:00:00 SPY -500 @ 201.092379263502&amp;quot;&#10;## [1] &amp;quot;2015-02-05 00:00:00 SPY 500 @ 205.204015568481&amp;quot;&#10;## [1] &amp;quot;2015-04-21 00:00:00 SPY -500 @ 209.600006103516&amp;quot;&#10;## [1] &amp;quot;2015-04-28 00:00:00 SPY -500 @ 211.440002441406&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 更新组合和账户数据&#10;updatePortf(Portfolio=portfolio1.st,&#10; Dates=paste(&amp;quot;::&amp;quot;, as.Date(Sys.time()), sep=&amp;#39;&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-07-13 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2014-01-02/2015-04-30. Using all data&#10;## instead.&#10;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#10;## Could not parse ::2025-07-13 as ISO8601 string, or one/bothends of the range&#10;## were outside the available prices: 2014-01-02/2015-04-30. Using all data&#10;## instead.&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pair1&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;updateAcct(account.st, Dates=paste(startDate, endDate, sep=&amp;quot;::&amp;quot;)) &lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;updateEndEq(account.st, Dates=paste(startDate, endDate, sep=&amp;quot;::&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;getEndEq(account.st, Sys.time()) # 查看最终权益&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 103576.8&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绘制持仓图表&#10;# dev.new()&#10;chart.Posn(Portfolio=portfolio1.st, Symbol=symb1) # 股票 1 的持仓图&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/04/10/pair-trade/index_files/figure-html/pairTrade-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# dev.new()&#10;chart.Posn(Portfolio=portfolio1.st, Symbol=symb2) # 股票 2 的持仓图&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/04/10/pair-trade/index_files/figure-html/pairTrade-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# dev.new()&#10;chartSeries(Cl(get(symb1))/Cl(get(symb2)), TA=&amp;quot;addBBands(n=N,sd=SD)&amp;quot;) # 比率与布林带图&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/04/10/pair-trade/index_files/figure-html/pairTrade-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 绩效分析&#10;ret1 &amp;lt;- PortfReturns(account.st) # 组合收益&#10;ret1$total &amp;lt;- rowSums(ret1) # 总收益&#10;&#10;# 绘制绩效摘要&#10;if(&amp;quot;package:PerformanceAnalytics&amp;quot; %in% search() || &#10; require(&amp;quot;PerformanceAnalytics&amp;quot;,quietly=TRUE)) {&#10; # dev.new()&#10; charts.PerformanceSummary(ret1$total, geometric=FALSE, wealth.index=TRUE) # 收益摘要图&#10;}&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/04/10/pair-trade/index_files/figure-html/pairTrade-4.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="策略实现的核心思路" class="section level1"&gt;&#10;&lt;h1&gt;策略实现的核心思路&lt;/h1&gt;&#10;&lt;p&gt;这段代码的核心是通过量化手段实现配对交易的均值回归逻辑，具体可分为以下几个关键环节：&lt;/p&gt;&#10;&lt;p&gt;首先是数据预处理。配对交易对数据同步性要求极高，因此代码通过alignSymbols函数对齐两只股票的时间戳，删除存在数据缺失的时间点，确保价格比率的计算基于完整的共同数据。这种预处理是后续指标准确性的基础，避免因数据不同步导致的错误信号。&lt;/p&gt;&#10;&lt;p&gt;其次是指标与信号体系。策略的核心指标是两只股票的价格比率（由calcRatio函数计算），并通过布林带（BBands）分析该比率的波动特征。布林带由中轨（20 日移动平均线）和上下轨（中轨 ±2 倍标准差）构成，反映比率的正常波动区间。当比率从下方突破下轨（cross.dn信号）时，认为两只股票的价格偏离过大且即将回归，此时买入第一只股票、卖出第二只；当比率从上方向下突破上轨（cross.up信号）时，则反向操作；当比率回归至中轨时（cross.mid信号），平仓获利。这种基于统计偏离的信号逻辑，是均值回归策略的典型应用。&lt;/p&gt;&#10;&lt;p&gt;再者是头寸管理与订单规模控制。代码通过addPosLimits设置了最大头寸（MaxPos）和建仓层级（lvls），允许分 3 步建仓，避免单次入场风险过高。更关键的是自定义的osSpreadMaxPos函数，它根据两只股票的对冲比率动态调整订单数量，确保多空头寸的风险对冲平衡 —— 第一只股票的头寸为MaxPos/lvls，第二只则为其相反数乘以价格比率，使整体组合对市场系统性风险的暴露最小化。这种精细化的头寸控制，是配对交易实现风险对冲的核心。&lt;/p&gt;&#10;&lt;p&gt;最后是回测与绩效评估。代码通过applyStrategy执行回测，更新组合和账户数据后，利用chart.Posn展示两只股票的持仓变化，通过charts.PerformanceSummary分析策略的收益曲线、最大回撤等关键指标，全面评估策略效果。&#10;策略的意义与价值&lt;/p&gt;&#10;&lt;p&gt;这段代码的意义不仅在于实现了一个可运行的配对交易策略，更展示了如何在 quantstrat 框架下扩展自定义功能：通过addPosLimits灵活设置头寸规则，借助osFUN传递自定义订单规模函数，为复杂策略的实现提供了范例。对于量化研究者而言，它演示了均值回归思想在配对交易中的具体应用 —— 通过价格比率的统计特征捕捉套利机会，同时通过多空对冲控制风险。&lt;/p&gt;&#10;&lt;p&gt;此外，代码也为策略优化提供了方向：例如调整布林带的周期（N）和标准差倍数（SD）以适应不同市场环境，或修改osSpreadMaxPos函数优化头寸调整逻辑。这种模块化的实现方式，使得策略易于维护和扩展，为量化交易的实践提供了有价值的参考。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>技术指标在量化投资中的应用：以SMA指标为例</title><link>https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/</link><pubDate>Thu, 08 Jan 2015 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/</guid><description>&lt;h2 id="引言"&gt;&lt;a href="#%e5%bc%95%e8%a8%80" class="header-anchor"&gt;&lt;/a&gt;引言&#10;&lt;/h2&gt;&lt;p&gt;在量化投资领域，技术指标是构建自动化交易策略的核心工具。通过对历史价格、成交量等数据的数学建模，技术指标能够将市场行为转化为可量化的信号，为程序化交易提供决策依据。简单移动平均线（Simple Moving Average,SMA）作为最经典的技术指标之一，凭借其简洁的逻辑和广泛适用性，在量化策略开发中占据重要地位。&lt;/p&gt;&#10;&lt;h3 id="技术指标的核心作用"&gt;&lt;a href="#%e6%8a%80%e6%9c%af%e6%8c%87%e6%a0%87%e7%9a%84%e6%a0%b8%e5%bf%83%e4%bd%9c%e7%94%a8" class="header-anchor"&gt;&lt;/a&gt;技术指标的核心作用&#10;&lt;/h3&gt;&lt;p&gt;技术指标通过三种典型方式驱动量化交易：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;趋势识别：MACD、布林带等指标可捕捉价格方向性变动；&lt;/li&gt;&#10;&lt;li&gt;超买超卖判断：RSI、KDJ等振荡器可识别市场极端状态；&lt;/li&gt;&#10;&lt;li&gt;波动率测量：ATR、标准差指标可量化市场风险强度。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;这些指标通过参数优化、多周期组合、跨指标协同等方式构建数学模型，在沪深300股指期货、加密货币等不同市场展现出稳定收益特征。研究表明，技术指标驱动的策略在趋势性市场中夏普比率可达1.5以上。&lt;/p&gt;&#10;&lt;h3 id="sma指标的量化应用"&gt;&lt;a href="#sma%e6%8c%87%e6%a0%87%e7%9a%84%e9%87%8f%e5%8c%96%e5%ba%94%e7%94%a8" class="header-anchor"&gt;&lt;/a&gt;SMA指标的量化应用&#10;&lt;/h3&gt;&lt;p&gt;SMA的计算公式为：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;SMA_{t} = \frac{1}{n}\sum_{i=1}^{n} P_{t-i+1} \&#10;=\frac{P_t+P_{t-1}+\cdots+P_{t-n+1}}{n}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其在量化策略中的典型应用包括：&lt;/p&gt;&#10;&lt;h4 id="趋势跟踪策略"&gt;&lt;a href="#%e8%b6%8b%e5%8a%bf%e8%b7%9f%e8%b8%aa%e7%ad%96%e7%95%a5" class="header-anchor"&gt;&lt;/a&gt;趋势跟踪策略&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;双均线系统：结合5日SMA与20日SMA，当短周期线上穿长周期线时生成买入信号（黄金交叉），反之为卖出信号（死亡交叉）。回测显示该策略在A股市场年化收益可达12%-18%[2]。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;价格偏离策略：当现价突破100日SMA时视为牛市启动，跌破则触发止损。该策略在美股ETF轮动中成功降低28%的最大回撤[3]。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="风险控制模块"&gt;&lt;a href="#%e9%a3%8e%e9%99%a9%e6%8e%a7%e5%88%b6%e6%a8%a1%e5%9d%97" class="header-anchor"&gt;&lt;/a&gt;风险控制模块&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;动态止盈：当持仓盈利超过200日SMA的3倍标准差时自动减仓；&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;波动过滤：仅在价格位于50日SMA上方时允许开多单，规避下行趋势风险。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="参数优化实践"&gt;&lt;a href="#%e5%8f%82%e6%95%b0%e4%bc%98%e5%8c%96%e5%ae%9e%e8%b7%b5" class="header-anchor"&gt;&lt;/a&gt;参数优化实践&#10;&lt;/h4&gt;&lt;p&gt;通过遗传算法对SMA周期进行动态调整，发现：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;股票市场最优参数集中在20-60日&lt;/li&gt;&#10;&lt;li&gt;加密货币市场更适用10-30日短周期&lt;/li&gt;&#10;&lt;li&gt;参数组合的夏普比率比单一参数提升35%&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="策略实现"&gt;&lt;a href="#%e7%ad%96%e7%95%a5%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;策略实现&#10;&lt;/h2&gt;&lt;p&gt;我们用R语言实现一个简单的SMA策略。完整代码如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 基于简单移动平均线的信号评估策略&#10;###############################################################)############&#10;&#10;# 加载包：&#10;require(devtools)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: devtools&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: usethis&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(iterators) # 迭代器工具&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: iterators&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(quantstrat) # 量化策略回测框架&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'PerformanceAnalytics'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:graphics':&#10;## &#10;## legend&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(gamlss.util) # 统计工具包（用于数据分布分析）&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.util&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.dist&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'gamlss.dist'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:TTR':&#10;## &#10;## DPO&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: splines&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.data&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'gamlss.data'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:datasets':&#10;## &#10;## sleep&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: nlme&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: parallel&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ********** GAMLSS Version 5.4-22 **********&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## For more on GAMLSS look at https://www.gamlss.com/&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Type gamlssNews() to see new features/changes/bug fixes.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(reshape2)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: reshape2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(rCharts)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: rCharts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: 'rCharts'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:base':&#10;## &#10;## %||%&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;require(beanplot)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: beanplot&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;###########################################################################&#10;# 配置时区设置&#10;ttz&amp;lt;-Sys.getenv('TZ') # 保存当前时区&#10;Sys.setenv(TZ='UTC') # 设置回测时区为UTC（避免时区问题影响）&#10;&#10;# 清理残留数据&#10;suppressWarnings(rm(&amp;quot;order_book.macross&amp;quot;,pos=.strategy))&#10;suppressWarnings(rm(&amp;quot;account.macross&amp;quot;,&amp;quot;portfolio.macross&amp;quot;,pos=.blotter))&#10;suppressWarnings(rm(&amp;quot;account.st&amp;quot;,&amp;quot;portfolio.st&amp;quot;,&amp;quot;stock.str&amp;quot;,&amp;quot;strategy.st&amp;quot;,'start_t','end_t'))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;###########################################################################&#10;# 数据准备&#10;startDate=&amp;quot;2000-01-01&amp;quot; # 回测起始日期&#10;stock.str=c('XLY','XLF','XLP','XLI','RTH','XLV','XLK','XLE','IYT') # 股票代码列表&#10;currency('USD') # 设置基准货币为美元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;stock(stock.str, currency='USD', multiplier=1) # 定义交易品种属性&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLY&amp;quot; &amp;quot;XLF&amp;quot; &amp;quot;XLP&amp;quot; &amp;quot;XLI&amp;quot; &amp;quot;RTH&amp;quot; &amp;quot;XLV&amp;quot; &amp;quot;XLK&amp;quot; &amp;quot;XLE&amp;quot; &amp;quot;IYT&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 下载雅虎财经数据&#10;getSymbols(stock.str, from=startDate, src = 'yahoo')&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;XLY&amp;quot; &amp;quot;XLF&amp;quot; &amp;quot;XLP&amp;quot; &amp;quot;XLI&amp;quot; &amp;quot;RTH&amp;quot; &amp;quot;XLV&amp;quot; &amp;quot;XLK&amp;quot; &amp;quot;XLE&amp;quot; &amp;quot;IYT&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;for (symbol in stock.str) {&#10; # 检查对象是否存在&#10; if (exists(symbol)) {&#10; # 生成文件名&#10; file_name &amp;lt;- paste0(symbol, &amp;quot;.rds&amp;quot;)&#10; # 保存为RDS文件&#10; saveRDS(get(symbol), file = file_name)&#10; # 打印保存信息&#10; message(&amp;quot;已保存: &amp;quot;, symbol, &amp;quot; -&amp;gt; &amp;quot;, file_name)&#10; } else {&#10; warning(&amp;quot;对象 &amp;quot;, symbol, &amp;quot; 不存在&amp;quot;)&#10; }&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLY -&amp;gt; XLY.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLF -&amp;gt; XLF.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLP -&amp;gt; XLP.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLI -&amp;gt; XLI.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: RTH -&amp;gt; RTH.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLV -&amp;gt; XLV.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLK -&amp;gt; XLK.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: XLE -&amp;gt; XLE.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 已保存: IYT -&amp;gt; IYT.rds&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 调整所有股票数据为复权价格&#10;for(i in stock.str)&#10; assign(i, adjustOHLC(get(i), use.Adjusted=TRUE))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;###########################################################################&#10;# 初始化账户、组合、策略&#10;initEq=1000000 # 初始资金100万美元&#10;portfolio.st='macross' # 组合名称&#10;account.st='macross' # 账户名称&#10;# 初始化组合、账户、订单簿&#10;initPortf(portfolio.st,&#10; symbols=stock.str)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;initAcct(account.st,&#10; portfolios=portfolio.st, &#10; initEq=initEq)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;initOrders(portfolio=portfolio.st)&#10;# 创建策略对象&#10;strategy.st&amp;lt;- strategy(portfolio.st)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 添加技术指标&#10;# 添加50日SMA指标&#10;strategy.st &amp;lt;- add.indicator(strategy = strategy.st, &#10; name = &amp;quot;SMA&amp;quot;, &#10; arguments = list(x=quote(Cl(mktdata)), &#10; n=50),&#10; label= &amp;quot;ma50&amp;quot; )&#10;# 添加200日SMA指标&#10;strategy.st &amp;lt;- add.indicator(strategy = strategy.st, &#10; name = &amp;quot;SMA&amp;quot;, &#10; arguments = list(x=quote(Cl(mktdata)), &#10; n=200),&#10; label= &amp;quot;ma200&amp;quot;)&#10;&#10;# 添加信号规则&#10;# 当50日均线上穿200日均线时生成信号&#10;strategy.st &amp;lt;- add.signal(strategy = strategy.st,&#10; name=&amp;quot;sigCrossover&amp;quot;,&#10; arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;),&#10; relationship=&amp;quot;gte&amp;quot;),&#10; label=&amp;quot;ma50.gt.ma200&amp;quot;)&#10;# 当50日均线下穿200日均线时生成信号&#10;strategy.st &amp;lt;- add.signal(strategy = strategy.st,&#10; name=&amp;quot;sigCrossover&amp;quot;,&#10; arguments =list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;),&#10; relationship=&amp;quot;lt&amp;quot;), &#10; label=&amp;quot;ma50.lt.ma200&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;###########################################################################&#10;# 参数优化设置&#10;# 需要分析的信号列标签&#10;signal.label = 'ma50.gt.ma200'&#10;&#10;# # 定义参数范围&#10;.FastSMA = seq(1,5,1) # 快速SMA参数范围：1-5日&#10;.SlowSMA = seq(5,20,5) # 慢速SMA参数范围：5-20日（步长5）&#10;&#10;# 添加快速SMA参数分布&#10;strategy.st&amp;lt;-add.distribution(strategy.st,&#10; paramset.label = 'SMA',&#10; component.type = 'indicator',&#10; component.label = 'ma50',&#10; variable = list(n = .FastSMA),&#10; label = 'nFAST')&#10;# 添加慢速SMA参数分布&#10;strategy.st&amp;lt;-add.distribution(strategy.st,&#10; paramset.label = 'SMA',&#10; component.type = 'indicator',&#10; component.label = 'ma200',&#10; variable = list(n = .SlowSMA),&#10; label = 'nSLOW')&#10;&#10;# 添加参数约束：快速SMA周期必须小于慢速SMA&#10;strategy.st&amp;lt;-add.distribution.constraint(strategy.st,&#10; paramset.label = 'SMA',&#10; distribution.label.1 = 'nFAST',&#10; distribution.label.2 = 'nSLOW',&#10; operator = '&amp;lt;',&#10; label = 'SMA')&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# # 执行信号分析（日线级别）&#10;results = apply.paramset.signal.analysis(&#10; strategy.st, &#10; paramset.label = 'SMA', &#10; portfolio.st, &#10; sigcol = signal.label, # 分析的信号列&#10; sigval = 1, # 信号触发阈值&#10; on = NULL, # 分析频率（NULL表示原始数据频率）&#10; forward.days = 50, # 信号后观察50天&#10; cum.sum = TRUE, # 计算累积收益&#10; include.day.of.signal = F,# 排除信号当天&#10; obj.fun = signal.obj.slope, # 使用斜率作为目标函数&#10; decreasing = T # 按降序排序结果&#10;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set: 1, 5 &#10;## Applying Parameter Set: 2, 5 &#10;## Applying Parameter Set: 3, 5 &#10;## Applying Parameter Set: 4, 5 &#10;## Applying Parameter Set: 1, 10 &#10;## Applying Parameter Set: 2, 10 &#10;## Applying Parameter Set: 3, 10 &#10;## Applying Parameter Set: 4, 10 &#10;## Applying Parameter Set: 5, 10 &#10;## Applying Parameter Set: 1, 15 &#10;## Applying Parameter Set: 2, 15 &#10;## Applying Parameter Set: 3, 15 &#10;## Applying Parameter Set: 4, 15 &#10;## Applying Parameter Set: 5, 15 &#10;## Applying Parameter Set: 1, 20 &#10;## Applying Parameter Set: 2, 20 &#10;## Applying Parameter Set: 3, 20 &#10;## Applying Parameter Set: 4, 20 &#10;## Applying Parameter Set: 5, 20&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;#------------------------------- 日线级别分析结果 ------------------------------#&#10;# 绘制IYT标的参数组合(5,20)的收益分布箱线图&#10;# signal: 信号分析结果数据集&#10;# x.val: x轴刻度位置，seq(1,50,5)表示从1到50步长5&#10;# val: 每个盒须图对应的时间窗口长度&#10;# ylim/xlim: 坐标轴范围&#10;# mai: 图形边距参数&#10;&#10; distributional.boxplot(&#10; signal=results$sigret.by.asset$IYT$paramset.5.20,&#10; x.val=seq(1, 50, 5), # 显示1-50天，每5天一个刻度&#10; val=10, # 每个盒须图代表10天的收益窗口&#10; ylim=c(-20, 20), # Y轴收益范围限制在±20%&#10; xlim=c(0, 50), # X轴范围0-50天&#10; mai=c(1,1,0.3,0.5), # 图形边距设置（下左上右）&#10; h=0 # 水平参考线位置（0轴）&#10; ) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 39613.69 &#10;## GAMLSS-RS iteration 2: Global Deviance = 39613.69&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-8-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制XLE标的信号的参数组合面板图（5行4列布局）&#10;&#10;&#10; signal.plot(&#10; results$sigret.by.asset$XLE, &#10; rows=5, # 图形行数&#10; columns = 4 # 图形列数&#10; )&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-9-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制XLE标的的豆状分布图（展示参数组合的密度分布）&#10;&#10;&#10; beanplot.signals(&#10; results$sigret.by.asset$XLE,&#10; rows=5, # 图形行数&#10; columns = 4# 图形列数 &#10; )&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-10-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制IYT标的参数组合(5,20)的信号路径图&#10;# signal.path.plot(results$sigret.by.asset$IYT$paramset.5.20)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;#----------------------------- 周线级别前瞻分析 -----------------------------#&#10;# 执行周线级别信号分析（向前看10天）&#10;results.w = apply.paramset.signal.analysis(&#10; strategy.st, # 策略对象&#10; paramset.label='SMA', # 参数集标签 &#10; portfolio.st, # 组合名称&#10; sigcol = signal.label, # 信号列名称&#10; sigval = 1, # 信号触发值&#10; on='weeks', # 按周汇总结果&#10; forward.days=10, # 信号后观察10天&#10; cum.sum=TRUE, # 计算累积收益&#10; include.day.of.signal=F, # 排除信号当天&#10; obj.fun=signal.obj.slope, # 使用斜率评估&#10; decreasing=T # 降序排列结果&#10;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set: 1, 5 &#10;## Applying Parameter Set: 2, 5 &#10;## Applying Parameter Set: 3, 5 &#10;## Applying Parameter Set: 4, 5 &#10;## Applying Parameter Set: 1, 10 &#10;## Applying Parameter Set: 2, 10 &#10;## Applying Parameter Set: 3, 10 &#10;## Applying Parameter Set: 4, 10 &#10;## Applying Parameter Set: 5, 10 &#10;## Applying Parameter Set: 1, 15 &#10;## Applying Parameter Set: 2, 15 &#10;## Applying Parameter Set: 3, 15 &#10;## Applying Parameter Set: 4, 15 &#10;## Applying Parameter Set: 5, 15 &#10;## Applying Parameter Set: 1, 20 &#10;## Applying Parameter Set: 2, 20 &#10;## Applying Parameter Set: 3, 20 &#10;## Applying Parameter Set: 4, 20 &#10;## Applying Parameter Set: 5, 20&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制周线分析箱线图（时间窗口调整为10天）&#10;distributional.boxplot(signal=results.w$sigret.by.asset$IYT$paramset.5.20,&#10; x.val=seq(1, 10, 2), # 1-10天步长2&#10; val=10, &#10; ylim=c(-20, 20),&#10; xlim=c(0, 10),&#10; mai=c(1,1,0.3,0.5),&#10; h=0)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 8067.487 &#10;## GAMLSS-RS iteration 2: Global Deviance = 8067.487&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制周线信号分析可视化面板&#10;signal.plot(results.w$sigret.by.asset$XLE, rows=5, columns = 4)&#10;&#10;# 绘制周线豆状分布图&#10;beanplot.signals(results.w$sigret.by.asset$XLE, rows=5, columns = 4)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;#----------------------------- 月线级别前瞻分析 -----------------------------#&#10;# 执行月线级别信号分析（向前看5天）&#10;results.m = apply.paramset.signal.analysis(&#10; strategy.st, &#10; paramset.label='SMA', &#10; portfolio.st,&#10; sigcol = signal.label,&#10; sigval = 1,&#10; on='months', # 按月汇总结果&#10; forward.days=5, # 信号后观察5天（模拟月度调仓）&#10; cum.sum=TRUE,&#10; include.day.of.signal=F,&#10; obj.fun=signal.obj.slope,&#10; decreasing=T&#10;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set: 1, 5 &#10;## Applying Parameter Set: 2, 5 &#10;## Applying Parameter Set: 3, 5 &#10;## Applying Parameter Set: 4, 5 &#10;## Applying Parameter Set: 1, 10 &#10;## Applying Parameter Set: 2, 10 &#10;## Applying Parameter Set: 3, 10 &#10;## Applying Parameter Set: 4, 10 &#10;## Applying Parameter Set: 5, 10 &#10;## Applying Parameter Set: 1, 15 &#10;## Applying Parameter Set: 2, 15 &#10;## Applying Parameter Set: 3, 15 &#10;## Applying Parameter Set: 4, 15 &#10;## Applying Parameter Set: 5, 15 &#10;## Applying Parameter Set: 1, 20 &#10;## Applying Parameter Set: 2, 20 &#10;## Applying Parameter Set: 3, 20 &#10;## Applying Parameter Set: 4, 20 &#10;## Applying Parameter Set: 5, 20&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制月线分析箱线图（时间窗口调整为5天）&#10;distributional.boxplot(signal=results.m$sigret.by.asset$IYT$paramset.5.20,&#10; x.val=seq(1, 5, 1), # 1-5天逐日显示&#10; val=10,&#10; ylim=c(-30, 30), # 放宽收益范围&#10; xlim=c(0, 5),&#10; mai=c(1,1,0.3,0.5),&#10; h=0)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 4588.769 &#10;## GAMLSS-RS iteration 2: Global Deviance = 4588.769&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-13-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 绘制月线信号面板图&#10;signal.plot(results.m$sigret.by.asset$XLE, rows=5, columns = 4)&#10;&#10;# 绘制月线豆状分布图&#10;beanplot.signals(results.m$sigret.by.asset$XLE, rows=5, columns = 4)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-13-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-13-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;h2 id="当前应用方向"&gt;&lt;a href="#%e5%bd%93%e5%89%8d%e5%ba%94%e7%94%a8%e6%96%b9%e5%90%91" class="header-anchor"&gt;&lt;/a&gt;当前应用方向&#10;&lt;/h2&gt;&lt;p&gt;当前前沿研究正将SMA与机器学习结合：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;作为LSTM神经网络的输入特征，提升价格预测精度；&lt;/li&gt;&#10;&lt;li&gt;构建SMA斜率变化率指标，提前1-3日预警趋势反转；&lt;/li&gt;&#10;&lt;li&gt;在高频交易中开发毫秒级SMA差值套利模型。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;值得注意的是，SMA的滞后性（Lagging Effect）在震荡市中可能产生虚假信号。成熟策略常将其与成交量加权移动平均线（VWMA）组合使用，或将参数动态化以提升适应性。量化实践表明，经过噪音过滤的SMA系统，在配合3%的移动止损规则后，可使策略年化波动率降低22%。&lt;/p&gt;&#10;</description></item><item><title>R中的量化投资包：quantstrat</title><link>https://gewutang.cn/2013/04/17/r%E4%B8%AD%E7%9A%84%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%8C%85quantstrat/</link><pubDate>Wed, 17 Apr 2013 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2013/04/17/r%E4%B8%AD%E7%9A%84%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%8C%85quantstrat/</guid><description>&lt;h2 id="quantstrat是什么东西"&gt;&lt;a href="#quantstrat%e6%98%af%e4%bb%80%e4%b9%88%e4%b8%9c%e8%a5%bf" class="header-anchor"&gt;&lt;/a&gt;quantstrat是什么东西？&#10;&lt;/h2&gt;&lt;p&gt;quantstrat是Quantitative Strategy（量化策略）的缩写，它是由Peter Carl、 Brian G. Peterson、Josh Ulrich、Garrett See、Yu Chen等联合开发的用于量化投资的R包。&lt;/p&gt;&#10;&lt;p&gt;这个包为用户提供了一个测试和模拟基于信号的量化策略模型的泛型框架。它不仅可以&#10;帮助用户构建交易系统，还可以对构建的交易系统进行仿真测试；能够支持对多资产类别和多币种组合进行回测（backtesting）等。&lt;/p&gt;&#10;&lt;p&gt;这个包在设计的过程中充分贯彻了“交易导向型”（Transaction-oriented）原则。&lt;/p&gt;&#10;&lt;p&gt;目前，这个包由Brian G.Peterson在维护，还在开发中，不过已经被很多业内人士拿来实战了。&lt;/p&gt;&#10;&lt;p&gt;因为quantstrat包是一个建立在xts, FinancialInstrument,blotter等包上的高级抽象包，所以，在应用quantstrat包时，&#10;除了除了安装quantstrat包之外，还得记得装上这三个支撑包。看起来安装有点麻烦，正式因为安装麻烦，所以，它才能使得用户用几行简单的代码就能来构建和测试策略。&lt;/p&gt;&#10;&lt;h2 id="quantstrat涉及的量化策略基础知识"&gt;&lt;a href="#quantstrat%e6%b6%89%e5%8f%8a%e7%9a%84%e9%87%8f%e5%8c%96%e7%ad%96%e7%95%a5%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86" class="header-anchor"&gt;&lt;/a&gt;quantstrat涉及的量化策略基础知识&#10;&lt;/h2&gt;&lt;h3 id="基于信号的策略的泛型建模"&gt;&lt;a href="#%e5%9f%ba%e4%ba%8e%e4%bf%a1%e5%8f%b7%e7%9a%84%e7%ad%96%e7%95%a5%e7%9a%84%e6%b3%9b%e5%9e%8b%e5%bb%ba%e6%a8%a1" class="header-anchor"&gt;&lt;/a&gt;基于信号的策略的泛型建模&#10;&lt;/h3&gt;&lt;p&gt;基于信号的策略模型需要先生成交易信号。交易指标是从市场数据中获知的量化指标（包括移动平均、RSI、波动带、通道、动量等）。交易指标应该以矢量化（快速回测）和数据流（实施执行）的形式应用于市场数据，并且假定交易指标是路径独立的（即不受账户、组合类型、现有头寸和交易的限制）。&lt;/p&gt;&#10;&lt;p&gt;交易指标和市场数据的相互作用会生成交易信号（交叉、阈值、倍数），交易信号标示交易者应当采取交易行动的时间，虽然，有时候，你无法行动。跟交易指标类似，交易信号也应该以矢量化和流形式应用于市场数据，并且需要假定交易信号是路径独立的。&lt;/p&gt;&#10;&lt;p&gt;交易规则基于交易指标、交易信号、账户头寸和账户/组合特性生成交易指令。注意关于头寸大小、填补模拟、指令生成和管理的规则与交易指标和交易信号的生成过程是独立的。与交易指标和交易信号不同，交易规则在是执行时是路径依赖式（事实上，也支持非路径依赖式的交易规则，然而，非路径依赖式的交易规则在现实中很少见）的，与已往市场数据和执行规则时的头寸大小相关。交易规则可以生成新的交易指令，也可以对现有交易指令进行修改（比如风险控制、填补、再平衡、介入、退出等）。&lt;/p&gt;&#10;&lt;h3 id="quantstrat如何构建策略"&gt;&lt;a href="#quantstrat%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e7%ad%96%e7%95%a5" class="header-anchor"&gt;&lt;/a&gt;Quantstrat如何构建策略？&#10;&lt;/h3&gt;&lt;p&gt;quantstrat通过FinancialInstrument包指定金融工具（包括所用币种）、通过&lt;code&gt;blotter&lt;/code&gt;包跟踪交易信息和市值及组合和账户的变动。&lt;/p&gt;&#10;&lt;p&gt;交易指标通常是跟TTR包中函数类似的标准技术分析函数；交易信号通常有&lt;code&gt;quantstrat&lt;/code&gt;包中的&lt;code&gt;sig*&lt;/code&gt;族函数指定（比如sigComparison、sigCrossover、sigFormula、sigPeak、sigThreshold）；规则通常有quantstrat包中的ruleSignal函数指定。&lt;/p&gt;&#10;&lt;p&gt;用来指定交易指标、交易信号和加以规则的函数不限于上述那些。add.indicator、add.signal、和add.rule的参数为任意R函数。因为支持链是基于xts对象构建的，因此，如果自定义的函数返回结果是xts对象的话，其能够被很好地整合到quantstrat包中。&lt;/p&gt;&#10;&lt;p&gt;策略模型以层形式创建，并且是延迟执行的。这意味着策略可以不加调整地应用于多种不同的组合。执行之前，策略不知道自身将应用到何种金融工具上，也不知道将被传递什么参数。例如，移动平均的时间参数和阈值参数很可能影响策略的性能。参数的默认值可以在策略对象中设定，也可以在执行时通过applyStrategy的参数（参数是一个命名列表，跟参数列表的用法类似）来设定。&lt;/p&gt;&#10;&lt;p&gt;quantstrat对交易指令进行建模，交易指令可能转化为实际交易，也可能不能转化为实际交易。这提供了一个额外功能，可以用来评估策略是如何工作的、为何不工作以及如何改进。例如，策略的性能经常受到“休息现价订单”的改变、替换和删除的影响。订单允许量化策略师检查决策作出时的市场条件。同时，订单历史使得针对许多策略的颇为重要的计算过程变得简单，比如填补比率的计算。&lt;/p&gt;&#10;&lt;h3 id="参数匹配规则"&gt;&lt;a href="#%e5%8f%82%e6%95%b0%e5%8c%b9%e9%85%8d%e8%a7%84%e5%88%99" class="header-anchor"&gt;&lt;/a&gt;参数匹配规则&#10;&lt;/h3&gt;&lt;p&gt;Quantstrat中很多地方会应用到通过策略技术参数传递（参数列表或者…）给交易指标、信号和交易规则函数的变元：&lt;/p&gt;&#10;&lt;p&gt;这些变元按照下面的顺序进行匹配，后面的赋值可以覆盖前值。具体的顺序如下:&#10;赋值给每个交易指标、交易信号和交易规则的arguments=list(…)参数；&#10;调用&lt;code&gt;applyStrategy&lt;/code&gt;是应用的&lt;code&gt;parameters=list{…}&lt;/code&gt;参数；&#10;调用&lt;code&gt;applyStrategy&lt;/code&gt;时，在…中传递的任意附加参数。&lt;/p&gt;&#10;</description></item><item><title>基于极值理论的 VaR 计算及实证分析</title><link>https://gewutang.cn/2010/12/29/var-caculation-and-evt/</link><pubDate>Wed, 29 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/29/var-caculation-and-evt/</guid><description>&#10;&#10;&#10;&lt;div id="一引言" class="section level1"&gt;&#10;&lt;h1&gt;一、引言&lt;/h1&gt;&#10;&lt;p&gt;风险价值（VaR）作为衡量金融资产极端损失的核心指标，传统方法（如 GARCH 模型）虽能捕捉波动聚类特性，但对尾部风险的刻画仍依赖于整体分布假设。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;极值理论（Extreme Value Theory, EVT）&lt;/strong&gt; 则直接聚焦于收益率序列的尾部行为，通过对极端值建模突破整体分布限制，更精准地估计极端市场条件下的潜在损失。&lt;/p&gt;&#10;&lt;p&gt;本文以特斯拉（TSLA）股票收益率为例，系统介绍极值理论的核心模型（广义极值分布 GEV、广义帕累托分布 GPD）、参数估计及 VaR 计算流程，并通过 R 语言&lt;code&gt;fExtremes&lt;/code&gt;和&lt;code&gt;evd&lt;/code&gt;包实现实证分析。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="二极值理论核心模型" class="section level1"&gt;&#10;&lt;h1&gt;二、极值理论核心模型&lt;/h1&gt;&#10;&lt;p&gt;极值理论通过两类方法刻画极端值分布：&lt;strong&gt;分块最大值法（Block Maxima Method, BMM）&lt;/strong&gt; 基于广义极值分布（GEV），&lt;strong&gt;超限峰值法（Peaks Over Threshold, POT）&lt;/strong&gt; 基于广义帕累托分布（GPD）。其中，POT 因灵活利用数据尾部信息而更常用于 VaR 计算。&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;广义极值分布（GEV）&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;strong&gt;分块最大值法&lt;/strong&gt;将时间序列划分为若干块（如按日、周），取每块内的极端值（最大值或最小值），其渐进分布可统一为 GEV。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;GEV 分布函数&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;G(x; \mu, \sigma, \xi) = \exp\left\{ -\left[1 + \xi \left( \frac{x - \mu}{\sigma} \right) \right]^{-1/\xi} \right\} \tag{1}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\mu\)&lt;/span&gt; 为位置参数（中心趋势），决定了分布的中心位置，反映极端值的平均水平；&lt;/li&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\sigma &amp;gt; 0\)&lt;/span&gt; 为尺度参数（离散程度）；&lt;/li&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数（决定尾部特性）：&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; ，则 GEV 为 Frechet 分布（厚尾，如幂律尾）。Frechet 分布具有显著的厚尾特征，概率密度函数呈渐近幂律形式，尾部衰减速度较慢，能更准确捕捉金融市场中极端事件发生概率较高的尾部风险，在 VaR 计算中可更可靠地拟合金融资产收益率极端值。&lt;/p&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi = 0\)&lt;/span&gt; ，则 GEV 为 Gumbel 分布（轻尾，如正态分布）。该分布下极端事件发生概率随偏离均值程度呈指数衰减，数据在均值附近集中程度高，极端值出现频率低，若资产收益符合此分布，表明极端损失发生可能性较小，风险相对可控。&lt;/p&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi &amp;lt; 0\)&lt;/span&gt; ，则 GEV 为 Weibull 分布（有界尾）。该分布下随机变量存在理论上界，极端事件影响有限，在金融市场中若资产价格波动符合此分布，意味着极端损失幅度有上限，对风险管理者设定止损策略有重要参考价值。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;基于 GEV 的 VaR 计算&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;广义极值分布（GEV）作为统一框架，整合了 Gumbel、Weibull 和 Frechet 三种不同尾部特征的分布类型。基于 GEV 的 VaR 计算，通过极大似然估计或矩估计等方法确定形状参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 、位置参数 &lt;span class="math inline"&gt;\(\mu\)&lt;/span&gt; 和尺度参数 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; ，构建符合数据尾部特征的风险分布模型。&lt;/p&gt;&#10;&lt;p&gt;在给定置信水平 &lt;span class="math inline"&gt;\(1-\alpha\)&lt;/span&gt; 下，VaR 为 GEV 分布的 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt; 分位数：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;VaR_{1-\alpha} = \mu - \frac{\sigma}{\xi} \left[ 1 - (-\ln \alpha)^{-\xi} \right] \quad (\xi \neq 0) \tag{2}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;ol start="2" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;广义帕累托分布（GPD）与 POT 方法&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;strong&gt;POT 方法&lt;/strong&gt;通过设定阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt;，对所有超过 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的极端值（超限值）进行建模，其超出量（&lt;span class="math inline"&gt;\(x - u\)&lt;/span&gt;）的分布可由广义帕累托分布（Generalized Pareto Distribution，GPD）逼近。&lt;/p&gt;&#10;&lt;p&gt;这种方法聚焦于极端事件，能更精准刻画金融市场尾部风险，在金融市场剧烈波动时，可有效捕捉资产价格极端变化，为风险管理者提供重要预警信号，在高风险投资领域应用价值显著。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;GPD 分布函数&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;广义帕累托分布是刻画超过某一阈值数据分布的重要工具，在金融风险度量中常用于描述极端损失事件概率分布，其概率密度函数为&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;f(x;\xi,\beta)=\frac{1}{\beta}(1 + \frac{\xi x}{\beta})^{-\frac{1}{\xi}-1}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中 &lt;span class="math inline"&gt;\(x \geq 0\)&lt;/span&gt;，&lt;span class="math inline"&gt;\(\beta &amp;gt; 0\)&lt;/span&gt; 是尺度参数，&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数，当 &lt;span class="math inline"&gt;\(\xi = 0\)&lt;/span&gt; 时退化为指数分布。&lt;/p&gt;&#10;&lt;p&gt;GPD 分布函数为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;F(x;\xi,\beta,\mu)=1-\left(1+\frac{\xi(x - \mu)}{\beta}\right)^{-\frac{1}{\xi}}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;span class="math inline"&gt;\(x\geq\mu，1+\frac{\xi(x - \mu)}{\beta}&amp;gt;0\)&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其分布函数也可表示为针对超出量的形式：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;\begin{cases}&#10;1 - \left(1 + \xi \frac{y}{\sigma}\right)^{-1/\xi} &amp;amp; \xi \neq 0 \\&#10;1 - \exp\left(-\frac{y}{\sigma}\right) &amp;amp; \xi = 0&#10;\end{cases} \tag{3}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(y = x - u \geq 0\)&lt;/span&gt; 为超出量，代表观测值超过阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的部分，反映极端事件偏离阈值的程度。例如，若阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 设为特斯拉股价单日跌幅 5%，某交易日股价下跌 8% 时，超出量 &lt;span class="math inline"&gt;\(y\)&lt;/span&gt; 为 3%。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(\sigma &amp;gt; 0\)&lt;/span&gt; 为尺度参数，决定超出量分布的离散程度。&lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; 值越大，超出量波动范围越广，投资组合面临的不确定性越高，且其变化与市场宏观环境相关，经济危机期间通常会显著跃升。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数（与 GEV 中的 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 含义相同），&lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; 表示厚尾，直接影响分布尾部厚度与极端事件发生概率估计。&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 趋近于 0 时，尾部变薄，极端事件概率降低；&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 增大时，极端事件发生可能性显著提升，且在公司重大事件节点往往会出现明显波动。&lt;/p&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;strong&gt;基于 GPD 的 VaR 计算&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;设总样本量为 &lt;span class="math inline"&gt;\(n\)&lt;/span&gt;，超限值数量为 &lt;span class="math inline"&gt;\(N_u\)&lt;/span&gt;（超限概率 &lt;span class="math inline"&gt;\(\hat{p} = N_u / n\)&lt;/span&gt;），则置信水平 &lt;span class="math inline"&gt;\(1-\alpha\)&lt;/span&gt; 的 VaR 满足：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;1 - \alpha = (1 - \hat{p}) + \hat{p} \cdot \left[1 - \left(1 + \xi \frac{VaR_{1-\alpha} - u}{\sigma}\right)^{-1/\xi}\right] \tag{4}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;该公式基于全概率公式推导，将整体风险概率分解为阈值以下和阈值以上两部分。等式左边 &lt;span class="math inline"&gt;\(1 - \alpha\)&lt;/span&gt; 为置信水平，右边 &lt;span class="math inline"&gt;\((1 - \hat{p})\)&lt;/span&gt; 表示未超过阈值的概率，第二项刻画了阈值以上部分在 GPD 分布下对风险的贡献。如在 95% 置信水平（&lt;span class="math inline"&gt;\(\alpha=0.05\)&lt;/span&gt;）下，可据此计算特斯拉股票在极端市场条件下的最大潜在损失。&lt;/p&gt;&#10;&lt;p&gt;经代数变换可得：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;VaR_{1-\alpha} = u + \frac{\sigma}{\xi} \left[ \left( \frac{n}{N_u} (1 - \alpha) \right)^{-\xi} - 1 \right] \quad (\xi \neq 0) \tag{5}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;在实际应用中，以特斯拉股价实证分析为例，若总样本量 &lt;span class="math inline"&gt;\(n=1000\)&lt;/span&gt; ，超限值数量 &lt;span class="math inline"&gt;\(N_u=50\)&lt;/span&gt;，阈值 &lt;span class="math inline"&gt;\(u = 0.03\)&lt;/span&gt;，尺度参数 &lt;span class="math inline"&gt;\(\sigma = 0.02\)&lt;/span&gt;，形状参数 &lt;span class="math inline"&gt;\(\xi = 0.1\)&lt;/span&gt;，在 99% 置信水平下（ &lt;span class="math inline"&gt;\(\alpha = 0.01\)&lt;/span&gt; ），代入公式 (5) 计算可得 &lt;span class="math inline"&gt;\(VaR_{0.99} \approx 0.068\)&lt;/span&gt;，即特斯拉股票在 99% 置信水平下的单日最大潜在损失约为 6.8%。&lt;/p&gt;&#10;&lt;p&gt;此公式为风险管理者提供了定量工具，结合历史数据计算的参数和预设置信水平，可快速得出风险价值，为投资组合配置和止损策略制定提供支持。需注意，当 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 接近 0 时，需采用特殊数值算法确保计算准确性。&lt;/p&gt;&#10;&lt;ol start="3" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;阈值选择与模型适用性&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;阈值的确定&lt;/strong&gt;：阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的选择至关重要，需平衡偏差（ &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 过小导致非极端值混入）与方差（ &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 过大导致样本量不足）。常用的确定方法包括：&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;strong&gt;平均超出量函数（Mean Excess Plot）&lt;/strong&gt;：绘制超出量均值与阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的关系图，寻找超出量均值近似线性的区间。此时的阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 既能有效分离极端值，又保留足够样本量用于可靠估计。例如分析特斯拉股价数据时，可设定一系列候选阈值，计算对应超出量均值并绘制曲线，选择线性区域内的阈值作为最终 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 值。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hill 图法&lt;/strong&gt;：计算不同阈值下的 Hill 估计量，绘制其与阈值的关系曲线。随着阈值增加，Hill 估计量应趋于稳定，选取稳定区间内的阈值作为合适的 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 值，该方法能有效识别复杂尾部特征数据的稳定尾部参数估计区间，增强阈值选择稳健性。&lt;/p&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&lt;/li&gt;&#10;&lt;li&gt;阈值稳定性检验：确保参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 和 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; 在 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 增大时趋于稳定。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;div id="三r-语言实证分析tsla-数据" class="section level1"&gt;&#10;&lt;h1&gt;三、R 语言实证分析（TSLA 数据）&#10;&lt;/h1&gt;&#10;&lt;p&gt;以 TSLA 股票收益率为例，采用 POT 方法（GPD）计算极端风险 VaR，步骤如下：&lt;/p&gt;&#10;&lt;div id="数据准备与预处理" class="section level2"&gt;&#10;&lt;h2&gt;1. 数据准备与预处理&lt;/h2&gt;&#10;&lt;p&gt;使用&lt;code&gt;quantmod&lt;/code&gt;获取数据并计算对数收益率（保留损失数据，即负收益率的绝对值）：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 安装并加载包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#10;if (!require(&amp;quot;fExtremes&amp;quot;)) install.packages(&amp;quot;fExtremes&amp;quot;)&#10;if (!require(&amp;quot;evd&amp;quot;)) install.packages(&amp;quot;evd&amp;quot;)&#10;library(quantmod)&#10;library(fExtremes)&#10;library(evd)&#10;library(ggplot2)&#10;library(showtext)&#10;font_add(&amp;quot;SimHei&amp;quot;,&amp;quot;SimHei.ttf&amp;quot;)&#10;showtext_auto()&#10;# 获取TSLA数据（2018-2023年）&#10;getSymbols(&amp;quot;TSLA&amp;quot;, from = &amp;quot;2018-01-01&amp;quot;, to = &amp;quot;2023-12-31&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;TSLA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;tsla_close &amp;lt;- TSLA$TSLA.Close&#10;ret &amp;lt;- diff(log(tsla_close)) # 对数收益率&#10;loss &amp;lt;- -ret # 损失序列（负收益为损失）&#10;loss &amp;lt;- na.omit(loss)&#10;colnames(loss) &amp;lt;- &amp;quot;TSLA_Loss&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="阈值选择mean-excess-plot" class="section level2"&gt;&#10;&lt;h2&gt;2. 阈值选择（Mean Excess Plot）&lt;/h2&gt;&#10;&lt;p&gt;通过平均超出量函数确定阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; ：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 计算平均超出量&#10;u_candidate &amp;lt;- seq(quantile(loss, 0.7), quantile(loss, 0.95), length.out = 20) # 候选阈值&#10;mean_excess &amp;lt;- sapply(u_candidate, function(u) {&#10;excess &amp;lt;- loss[loss &amp;gt; u] - u # 超出量&#10;mean(excess, na.rm = TRUE)&#10;})&#10;# 可视化平均超出量&#10;me_df &amp;lt;- data.frame(u = u_candidate, mean_excess = mean_excess)&#10;ggplot(me_df, aes(x = u, y = mean_excess)) +&#10;geom_line(color = &amp;quot;blue&amp;quot;) +&#10;geom_point() +&#10;labs(title = &amp;quot;平均超出量函数（确定阈值u）&amp;quot;, x = &amp;quot;阈值u&amp;quot;, y = &amp;quot;平均超出量&amp;quot;) +&#10;theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/29/var-caculation-and-evt/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 选择阈值（如u=0.03，对应约80%分位数）&#10;u &amp;lt;- 0.03&#10;excess &amp;lt;- as.numeric(loss[loss &amp;gt; u] - u) # 超出量序列&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="gpd-参数估计" class="section level2"&gt;&#10;&lt;h2&gt;3. GPD 参数估计&#10;&lt;/h2&gt;&#10;&lt;p&gt;使用&lt;code&gt;evd&lt;/code&gt;包的&lt;code&gt;fpot&lt;/code&gt;函数估计 GPD 的形状参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 和尺度参数 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; ：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 拟合GPD模型&#10;gpd_fit &amp;lt;- fpot(as.numeric(loss), threshold = u)&#10;&#10;summary(gpd_fit) # 输出参数估计结果&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Length Class Mode &#10;## estimate 2 -none- numeric &#10;## std.err 2 -none- numeric &#10;## fixed 0 -none- NULL &#10;## param 2 -none- numeric &#10;## deviance 1 -none- numeric &#10;## corr 0 -none- NULL &#10;## var.cov 4 -none- numeric &#10;## convergence 1 -none- character&#10;## counts 2 -none- numeric &#10;## message 0 -none- NULL &#10;## threshold 1 -none- numeric &#10;## cmax 1 -none- logical &#10;## r 0 -none- NULL &#10;## ulow 1 -none- numeric &#10;## rlow 1 -none- numeric &#10;## npp 1 -none- numeric &#10;## nhigh 1 -none- numeric &#10;## nat 1 -none- numeric &#10;## pat 1 -none- numeric &#10;## extind 0 -none- NULL &#10;## data 1508 -none- numeric &#10;## exceedances 244 -none- numeric &#10;## mper 0 -none- NULL &#10;## scale 1 -none- numeric &#10;## call 3 -none- call&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取参数&#10;&#10;xi &amp;lt;- gpd_fit$estimate[&amp;quot;shape&amp;quot;] # 形状参数ξ&#10;&#10;sigma &amp;lt;- gpd_fit$estimate[&amp;quot;scale&amp;quot;] # 尺度参数σ&#10;&#10;N &amp;lt;- length(loss) # 总样本量&#10;&#10;Nu &amp;lt;- length(excess) # 超限值数量&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="计算-var-与回测" class="section level2"&gt;&#10;&lt;h2&gt;4. 计算 VaR 与回测&lt;/h2&gt;&#10;&lt;p&gt;基于 GPD 参数计算高置信水平（如 99%、99.5%）的 VaR：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义VaR计算函数（GPD）&#10;&#10;calc_evt_var &amp;lt;- function(conf_level, u, xi, sigma, N, Nu) {&#10;alpha &amp;lt;- 1 - conf_level&#10;term &amp;lt;- (N / Nu * alpha)^(-xi)&#10;&#10;var &amp;lt;- u + (sigma / xi) * (term - 1)&#10;return(var)&#10;&#10;}&#10;# 计算99%和99.5%置信水平的VaR&#10;var_99 &amp;lt;- calc_evt_var(0.99, u, xi, sigma, N, Nu)&#10;var_995 &amp;lt;- calc_evt_var(0.995, u, xi, sigma, N, Nu)&#10;cat(&amp;quot;99% VaR（EVT-GPD）：&amp;quot;, round(var_99, 4), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99% VaR（EVT-GPD）： 0.1115&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;99.5% VaR（EVT-GPD）：&amp;quot;, round(var_995, 4), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99.5% VaR（EVT-GPD）： 0.1367&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 回测：极端损失超过VaR的比例&#10;extreme_loss &amp;lt;- as.numeric(loss)[loss &amp;gt; u] # 极端损失序列&#10;fail_99 &amp;lt;- mean(extreme_loss &amp;gt; var_99)&#10;cat(&amp;quot;99% VaR失败率：&amp;quot;, round(fail_99 * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99% VaR失败率： 6.15 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="结果可视化" class="section level2"&gt;&#10;&lt;h2&gt;5. 结果可视化&lt;/h2&gt;&#10;&lt;p&gt;对比实际极端损失与 VaR 阈值：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化极端损失与VaR&#10;extreme_df &amp;lt;- data.frame(&#10;Date = index(loss)[loss &amp;gt; u],&#10;Extreme_Loss = as.numeric(extreme_loss)&#10;)&#10;&#10;ggplot(extreme_df, aes(x = Date, y = Extreme_Loss)) +&#10;geom_point(color = &amp;quot;red&amp;quot;) +&#10;geom_hline(yintercept = var_99, &#10; color = &amp;quot;blue&amp;quot;, &#10; linetype = &amp;quot;dashed&amp;quot;) +&#10;geom_hline(yintercept = var_995, &#10; color = &amp;quot;black&amp;quot;, &#10; linetype = &amp;quot;dashed&amp;quot;) +&#10;&#10;&#10;labs(title = &amp;quot;TSLA极端损失与EVT-GPD VaR&amp;quot;,x = &amp;quot;日期&amp;quot;, y = &amp;quot;极端损失（绝对值）&amp;quot;) +&#10;theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/29/var-caculation-and-evt/index_files/figure-html/unnamed-chunk-5-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="四结论" class="section level1"&gt;&#10;&lt;h1&gt;四、结论&lt;/h1&gt;&#10;&lt;p&gt;极值理论通过直接建模尾部极端值，避免了对整体分布的依赖，在高置信水平下的 VaR 估计更具优势。实证分析显示：&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;TSLA 股票的极端损失呈现明显厚尾特性（ &lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; ），适合用 GPD 建模；&lt;/li&gt;&#10;&lt;li&gt;阈值&lt;span class="math inline"&gt;\(u\)&lt;/span&gt;的选择对结果敏感，需结合平均超出量函数和参数稳定性检验确定；&lt;/li&gt;&#10;&lt;li&gt;与传统 GARCH 模型相比，EVT 方法计算的高置信水平 VaR 更保守，能更好应对 “黑天鹅” 事件。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;未来可结合动态阈值调整或贝叶斯估计，进一步提升 EVT 模型对极端风险的动态跟踪能力。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;注&lt;/strong&gt;：代码中&lt;code&gt;fpot&lt;/code&gt;函数需确保阈值&lt;span class="math inline"&gt;\(u\)&lt;/span&gt;合理（超限值数量建议占总样本的 5%-10%）；若&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt;接近 0，可简化为指数分布计算 VaR。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>指数加权移动平均法计算VaR</title><link>https://gewutang.cn/2010/12/18/caculate-var-via-ewma/</link><pubDate>Sat, 18 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/18/caculate-var-via-ewma/</guid><description>&lt;h2 id="指数加权移动平均法"&gt;&lt;a href="#%e6%8c%87%e6%95%b0%e5%8a%a0%e6%9d%83%e7%a7%bb%e5%8a%a8%e5%b9%b3%e5%9d%87%e6%b3%95" class="header-anchor"&gt;&lt;/a&gt;指数加权移动平均法&#10;&lt;/h2&gt;&lt;p&gt;指数移动加权法是一种动态波动率建模方法，其核心思想是通过赋予历史收益率随时间衰减的权重，捕捉金融时间序列的波动率聚集特征。与传统等权重方法不同，EWMA通过引入衰减因子 &lt;code&gt;\(\lambda\)&lt;/code&gt;，其中: &lt;code&gt;\(\left(0\le\lambda\le1\right)\)&lt;/code&gt; ，使近期数据对波动率估计的影响呈指数级递增，而远期数据影响逐渐衰减（通常取 &lt;code&gt;\(\lambda = 0.94\)&lt;/code&gt; ，基于RiskMetrics建议）。这种机制使得模型能更快响应市场突变，尤其适用于高频风险监测。&lt;/p&gt;&#10;&lt;h3 id="单资产情形"&gt;&lt;a href="#%e5%8d%95%e8%b5%84%e4%ba%a7%e6%83%85%e5%bd%a2" class="header-anchor"&gt;&lt;/a&gt;单资产情形&#10;&lt;/h3&gt;&lt;h4 id="1-数据准备与收益率计算"&gt;&lt;a href="#1-%e6%95%b0%e6%8d%ae%e5%87%86%e5%a4%87%e4%b8%8e%e6%94%b6%e7%9b%8a%e7%8e%87%e8%ae%a1%e7%ae%97" class="header-anchor"&gt;&lt;/a&gt;1. 数据准备与收益率计算&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;获取资产历史收盘价序列${P_t}$，计算对数收益率：&#10;$$&#10;r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)&#10;$$&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="2-动态方差估计"&gt;&lt;a href="#2-%e5%8a%a8%e6%80%81%e6%96%b9%e5%b7%ae%e4%bc%b0%e8%ae%a1" class="header-anchor"&gt;&lt;/a&gt;2. 动态方差估计&#10;&lt;/h4&gt;&lt;p&gt;EWMA通过指数衰减因子 &lt;code&gt;\(\lambda\)&lt;/code&gt; 赋予近期数据更高权重，捕捉波动率的时变特性：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\sigma_t^2 = \lambda \sigma_{t-1}^2 + (1-\lambda) r_{t-1}^2&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(\lambda \in (0,1)\)&lt;/code&gt;：衰减因子（典型值0.94）&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(r_{t-1}\)&lt;/code&gt;：前一日收益率&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\sigma_{t-1}^2\)&lt;/code&gt;：前一日波动率&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="3-var计算正态假设"&gt;&lt;a href="#3-var%e8%ae%a1%e7%ae%97%e6%ad%a3%e6%80%81%e5%81%87%e8%ae%be" class="header-anchor"&gt;&lt;/a&gt;3. VaR计算（正态假设）&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;单日VaR公式：&#10;$$&#10;\text{VaR}_{\alpha} = -\mu + \sigma_t \Phi^{-1}(1-\alpha)&#10;$$&#10;其中$\mu$为收益率均值（短期内可忽略），$\Phi^{-1}$为标准正态分布分位数（如95%置信水平对应1.645）&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="4-多期var调整"&gt;&lt;a href="#4-%e5%a4%9a%e6%9c%9fvar%e8%b0%83%e6%95%b4" class="header-anchor"&gt;&lt;/a&gt;4. 多期VaR调整&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;持有期$T$天的VaR扩展：&#10;$$&#10;\text{VaR}&lt;em&gt;T = \text{VaR}&lt;/em&gt;{1d} \times \sqrt{T}&#10;$$&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h5 id="代码实现"&gt;&lt;a href="#%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;代码实现&#10;&lt;/h5&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载必要包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;) # 金融数据获取&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：quantmod&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;if (!require(&amp;quot;ggplot2&amp;quot;)) install.packages(&amp;quot;ggplot2&amp;quot;) # 可视化&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：ggplot2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod)&#10;library(ggplot2)&#10;&#10;# 获取标普500指数数据（示例）&#10;getSymbols(&amp;quot;^GSPC&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = &amp;quot;2020-01-01&amp;quot;, to = Sys.Date())&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GSPC&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 计算对数收益率&#10;returns &amp;lt;- na.omit(ROC(Cl(GSPC))) # 使用收盘价计算&#10;colnames(returns) &amp;lt;- &amp;quot;Daily_Return&amp;quot;&#10;&#10;# 参数设置&#10;lambda &amp;lt;- 0.94 # 衰减因子&#10;portfolio_value &amp;lt;- 1e6 # 组合价值100万美元&#10;confidence_level &amp;lt;- 0.95 # 置信水平&#10;z_score &amp;lt;- qnorm(1 - confidence_level) # 分位数（正态分布）&#10;&#10;# 初始化EWMA方差序列&#10;n &amp;lt;- length(returns)&#10;ewma_var &amp;lt;- numeric(n)&#10;ewma_var[1] &amp;lt;- var(returns) # 初始化为样本方差&#10;&#10;# 递归计算EWMA方差&#10;for (i in 2:n) {&#10; ewma_var[i] &amp;lt;- lambda * ewma_var[i-1] + (1 - lambda) * returns[i-1]^2&#10;}&#10;&#10;# 转换为年化波动率&#10;ewma_vol &amp;lt;- sqrt(ewma_var * 252)&#10;&#10;# 计算VaR序列&#10;VaR &amp;lt;- portfolio_value * z_score * ewma_vol&#10;&#10;# 获取最新VaR值&#10;latest_VaR &amp;lt;- tail(VaR, 1)&#10;cat(&amp;quot;最新EWMA VaR（95%置信度）:&amp;quot;, round(latest_VaR, 2), &amp;quot;美元\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 最新EWMA VaR（95%置信度）: -581585.7 美元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 可视化结果&#10;results &amp;lt;- data.frame(&#10; Date = index(GSPC)[-1], &#10; Volatility = ewma_vol,&#10; VaR = VaR&#10;)&#10;&#10;ggplot(results, aes(x = Date)) +&#10; geom_line(aes(y = Volatility, color = &amp;quot;波动率&amp;quot;)) +&#10; geom_line(aes(y = VaR / 1e4, color = &amp;quot;VaR（万美元）&amp;quot;)) +&#10; scale_y_continuous(&#10; name = &amp;quot;年化波动率&amp;quot;,&#10; sec.axis = sec_axis(~ . * 1e4, name = &amp;quot;VaR（美元）&amp;quot;)&#10; ) +&#10; labs(title = &amp;quot;EWMA波动率与VaR动态变化&amp;quot;,&#10; color = &amp;quot;指标类型&amp;quot;) +&#10; theme_minimal()&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/18/caculate-var-via-ewma/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&#10;&lt;h3 id="多元资产组合ewma-var建模原理与r实现"&gt;&lt;a href="#%e5%a4%9a%e5%85%83%e8%b5%84%e4%ba%a7%e7%bb%84%e5%90%88ewma-var%e5%bb%ba%e6%a8%a1%e5%8e%9f%e7%90%86%e4%b8%8er%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;多元资产组合EWMA-VaR建模原理与R实现&#10;&lt;/h3&gt;&lt;h4 id="协方差矩阵动态更新"&gt;&lt;a href="#%e5%8d%8f%e6%96%b9%e5%b7%ae%e7%9f%a9%e9%98%b5%e5%8a%a8%e6%80%81%e6%9b%b4%e6%96%b0" class="header-anchor"&gt;&lt;/a&gt;协方差矩阵动态更新&#10;&lt;/h4&gt;&lt;p&gt;多资产EWMA模型通过递归更新协方差矩阵捕捉资产间相关性的时变特征:&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\Sigma_t = \lambda \Sigma_{t-1} + (1-\lambda)\mathbf{r}&lt;em&gt;{t-1}\mathbf{r}&lt;/em&gt;{t-1}^\top&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(\mathbf{r}_{t-1}\)&lt;/code&gt;：前一期资产收益率向量（$n \times 1$）&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\Sigma_{t-1}\)&lt;/code&gt;：前一期协方差矩阵（$n \times n$）&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\lambda\)&lt;/code&gt;：衰减因子（建议值0.94）:ml-citation{ref=&amp;ldquo;4,8&amp;rdquo; data=&amp;ldquo;citationList&amp;rdquo;}&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="组合var计算流程"&gt;&lt;a href="#%e7%bb%84%e5%90%88var%e8%ae%a1%e7%ae%97%e6%b5%81%e7%a8%8b" class="header-anchor"&gt;&lt;/a&gt;组合VaR计算流程&#10;&lt;/h4&gt;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;步骤&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;计算公式&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;组合方差&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;\(\sigma_p^2 = \mathbf{w}^\top \Sigma_t \mathbf{w}\)&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;年化波动率&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;\(\sigma_p^{\text{annual}} = \sigma_p \sqrt{252}\)&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;VaR&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;\(VaR_{\alpha} = Z_{1-\alpha} \times P_0 \times \sigma_p^{\text{annual}}\)&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(\mathbf{w}\)&lt;/code&gt;：资产权重向量（需满足$\sum w_i=1$）&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(P_0\)&lt;/code&gt;：组合总价值&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="r语言完整实现"&gt;&lt;a href="#r%e8%af%ad%e8%a8%80%e5%ae%8c%e6%95%b4%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;R语言完整实现&#10;&lt;/h4&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载必要库&#10;library(quantmod) # 多元金融数据获取&#10;library(ggplot2) # 可视化&#10;library(reshape2) # 数据重塑&#10;&#10;# 获取三大股指数据&#10;symbols &amp;lt;- c(&amp;quot;^GSPC&amp;quot;, &amp;quot;^IXIC&amp;quot;, &amp;quot;^DJI&amp;quot;) # 标普500、纳斯达克、道琼斯&#10;getSymbols(symbols, from = &amp;quot;2020-01-01&amp;quot;, to = Sys.Date())&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GSPC&amp;quot; &amp;quot;IXIC&amp;quot; &amp;quot;DJI&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 合并收盘价并计算对数收益率&#10;prices &amp;lt;- na.omit(merge(Cl(GSPC), Cl(IXIC), Cl(DJI)))&#10;returns &amp;lt;- na.omit(ROC(prices)) &#10;colnames(returns) &amp;lt;- c(&amp;quot;SP500&amp;quot;, &amp;quot;NASDAQ&amp;quot;, &amp;quot;DJI&amp;quot;)&#10;&#10;# 参数设置&#10;lambda &amp;lt;- 0.94 # 衰减因子&#10;weights &amp;lt;- c(0.5, 0.3, 0.2) # 资产权重向量&#10;portfolio_value &amp;lt;- 1e7 # 组合价值（美元）&#10;conf_level &amp;lt;- 0.95 # 置信水平&#10;&#10;# 初始化协方差矩阵&#10;n_assets &amp;lt;- ncol(returns)&#10;n_obs &amp;lt;- nrow(returns)&#10;covar &amp;lt;- array(dim = c(n_assets, n_assets, n_obs))&#10;covar[,,1] &amp;lt;- cov(returns) # 初始协方差矩阵&#10;&#10;# 递归更新协方差矩阵&#10;for (i in 2:n_obs) {&#10; r_prev &amp;lt;- as.numeric(returns[i-1, ])&#10; covar[,,i] &amp;lt;- lambda * covar[,,i-1] + (1 - lambda) * outer(r_prev, r_prev)&#10;}&#10;&#10;# 计算组合波动率&#10;port_var &amp;lt;- sapply(1:n_obs, function(i) {&#10; t(weights) %*% covar[,,i] %*% weights&#10;})&#10;annual_vol &amp;lt;- sqrt(port_var * 252)&#10;&#10;# 计算VaR序列&#10;z_score &amp;lt;- qnorm(conf_level)&#10;VaR_series &amp;lt;- -portfolio_value * z_score * annual_vol&#10;&#10;# 输出最新VaR&#10;cat(&amp;quot;Latest Portfolio EWMA VaR (95%):&amp;quot;, &#10; round(tail(VaR_series, 1), 2), &amp;quot;USD\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Latest Portfolio EWMA VaR (95%): -6013901 USD&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 可视化协方差矩阵变化&#10;cov_df &amp;lt;- data.frame(&#10; Date = index(returns),&#10; SP500_NASDAQ = sapply(1:n_obs, function(i) covar[1,2,i]),&#10; SP500_DJI = sapply(1:n_obs, function(i) covar[1,3,i]),&#10; NASDAQ_DJI = sapply(1:n_obs, function(i) covar[2,3,i])&#10;)&#10;cov_long &amp;lt;- melt(cov_df, id.vars = &amp;quot;Date&amp;quot;)&#10;&#10;ggplot(cov_long, aes(x = Date, y = value, color = variable)) +&#10; geom_line(alpha = 0.7) +&#10; labs(title = &amp;quot;协方差动态变化趋势&amp;quot;, y = &amp;quot;协方差值&amp;quot;) +&#10; theme_minimal()&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/18/caculate-var-via-ewma/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&#10;</description></item><item><title>VaR和方差协方差方法及R语言</title><link>https://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/</link><pubDate>Fri, 17 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/</guid><description>&lt;h2 id="方差-协方差法"&gt;&lt;a href="#%e6%96%b9%e5%b7%ae-%e5%8d%8f%e6%96%b9%e5%b7%ae%e6%b3%95" class="header-anchor"&gt;&lt;/a&gt;方差-协方差法&#10;&lt;/h2&gt;&lt;p&gt;方差-协方差法是一种基于资产收益率正态性假设的VaR计算方法。其核心思想是通过历史数据估计资产收益率的均值、方差和协方差矩阵，构建投资组合收益率的概率分布，继而计算VaR值。&lt;/p&gt;&#10;&lt;p&gt;具体流程可分为以下三步：&lt;/p&gt;&#10;&lt;p&gt;首先，计算资产收益率的协方差矩阵以衡量风险关联；其次，根据投资组合权重计算组合收益率的期望值和标准差；最后，在给定置信水平下，利用正态分布分位数确定VaR值。例如，在95%置信水平下，单日VaR可表示为组合市值的绝对值乘以其标准差再乘1.645（对应正态分布左尾5%分位数）。该方法计算效率高，但对非线性风险和非正态分布敏感。多日VaR可通过时间平方根法则调整，即单日VaR乘 &lt;code&gt;\(\sqrt{T}\)&lt;/code&gt; 得到。&lt;/p&gt;&#10;&lt;h2 id="r代码实现"&gt;&lt;a href="#r%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;R代码实现&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载必要包&#10;library(quantmod)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(PerformanceAnalytics)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'PerformanceAnalytics'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:graphics':&#10;## &#10;## legend&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(ggplot2)&#10;library(dplyr)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ######################### Warning from 'xts' package ##########################&#10;## # #&#10;## # The dplyr lag() function breaks how base R's lag() function is supposed to #&#10;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #&#10;## # source() into this session won't work correctly. #&#10;## # #&#10;## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #&#10;## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #&#10;## # dplyr from breaking base R's lag() function. #&#10;## # #&#10;## # Code in packages is not affected. It's protected by R's namespace mechanism #&#10;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #&#10;## # #&#10;## ###############################################################################&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'dplyr'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:xts':&#10;## &#10;## first, last&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:stats':&#10;## &#10;## filter, lag&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## intersect, setdiff, setequal, union&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 1. 下载股票数据（示例使用苹果、微软、亚马逊）&#10;symbols &amp;lt;- c(&amp;quot;AAPL&amp;quot;, &amp;quot;MSFT&amp;quot;, &amp;quot;AMZN&amp;quot;)&#10;getSymbols(symbols, src = &amp;quot;yahoo&amp;quot;, from = &amp;quot;2022-01-01&amp;quot;, to = Sys.Date())&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;MSFT&amp;quot; &amp;quot;AMZN&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 2. 提取收盘价并合并&#10;prices &amp;lt;- merge(Cl(AAPL), Cl(MSFT), Cl(AMZN)) %&amp;gt;% na.omit()&#10;colnames(prices) &amp;lt;- symbols&#10;&#10;# 3. 计算对数收益率&#10;returns &amp;lt;- na.omit(Return.calculate(prices, method = &amp;quot;log&amp;quot;))&#10;&#10;# 4. 设定投资组合参数&#10;weights &amp;lt;- c(0.3, 0.4, 0.3) # 组合权重&#10;conf_level &amp;lt;- 0.95 # 置信水平&#10;portfolio_value &amp;lt;- 1e6 # 组合价值（美元）&#10;&#10;# 5. 计算协方差矩阵与组合标准差&#10;cov_matrix &amp;lt;- cov(returns)&#10;port_sd &amp;lt;- sqrt(t(weights) %*% cov_matrix %*% weights)&#10;&#10;# 6. 计算单日VaR&#10;var_1d &amp;lt;- abs(portfolio_value * qnorm(1 - conf_level) * port_sd)&#10;&#10;# 7. 计算5日VaR（时间平方根法则）&#10;var_5d &amp;lt;- var_1d * sqrt(5)&#10;&#10;# 8. 可视化VaR分布&#10;ggplot(data = data.frame(Returns = as.numeric(returns %*% weights)), &#10; aes(x = Returns)) +&#10; geom_histogram(aes(y = ..density..), bins = 50, fill = &amp;quot;steelblue&amp;quot;, alpha = 0.7) +&#10; geom_vline(xintercept = -var_1d/portfolio_value, color = &amp;quot;red&amp;quot;, &#10; linetype = &amp;quot;dashed&amp;quot;, size = 1) +&#10; stat_function(fun = dnorm, args = list(mean = mean(returns %*% weights),&#10; sd = sd(returns %*% weights)), color = &amp;quot;darkgreen&amp;quot;) +&#10; labs(title = &amp;quot;Portfolio Returns Distribution with VaR&amp;quot;,&#10; x = &amp;quot;Daily Returns&amp;quot;, y = &amp;quot;Density&amp;quot;) +&#10; annotate(&amp;quot;text&amp;quot;, x = -var_1d/portfolio_value, y = 15, &#10; label = paste0(&amp;quot;95% 1d VaR: $&amp;quot;, round(var_1d)), color = &amp;quot;red&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.&#10;## ℹ Please use `linewidth` instead.&#10;## This warning is displayed once every 8 hours.&#10;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#10;## generated.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.&#10;## ℹ Please use `after_stat(density)` instead.&#10;## This warning is displayed once every 8 hours.&#10;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#10;## generated.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 输出结果&#10;cat(&amp;quot;单日VaR（95%置信度）:&amp;quot;, round(var_1d), &amp;quot;美元\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 单日VaR（95%置信度）: 28894 美元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(&amp;quot;5日VaR（95%置信度）:&amp;quot;, round(var_5d), &amp;quot;美元&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 5日VaR（95%置信度）: 64609 美元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>VaR和蒙特卡洛模拟法</title><link>https://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/</link><pubDate>Thu, 16 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/</guid><description>&lt;h2 id="蒙特卡洛模拟法"&gt;&lt;a href="#%e8%92%99%e7%89%b9%e5%8d%a1%e6%b4%9b%e6%a8%a1%e6%8b%9f%e6%b3%95" class="header-anchor"&gt;&lt;/a&gt;蒙特卡洛模拟法&#10;&lt;/h2&gt;&lt;p&gt;蒙特卡洛模拟法是一种基于随机抽样和统计模拟的数值计算方法，其通过生成大量随机样本，利用概率统计理论估计复杂问题的近似解。该方法由数学家冯·诺伊曼和乌拉姆于20世纪40年代在核武器研制项目中首次提出。蒙特卡洛模拟法也可以用来计算投资组合的VaR。&lt;/p&gt;&#10;&lt;p&gt;在应用蒙特卡洛模拟法计算VaR时，首先根据投资组合的历史特征，总结投资组合收益率对应的可能分布并提取该分布对应的核心参数；再以这些核心参数为基础，结合特定分布模拟出一系列符合要求的伪随机数作为假想的未来数据；最后，以这些“假想的未来数据”作为研究对象计算出VaR值。&lt;/p&gt;&#10;&lt;h2 id="蒙特卡洛模拟法计算单资产var"&gt;&lt;a href="#%e8%92%99%e7%89%b9%e5%8d%a1%e6%b4%9b%e6%a8%a1%e6%8b%9f%e6%b3%95%e8%ae%a1%e7%ae%97%e5%8d%95%e8%b5%84%e4%ba%a7var" class="header-anchor"&gt;&lt;/a&gt;蒙特卡洛模拟法计算单资产VaR&#10;&lt;/h2&gt;&lt;p&gt;基于R语言应用蒙特卡洛模拟法计算VaR的代码如下:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载必要包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：quantmod&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;if (!require(&amp;quot;MASS&amp;quot;)) install.packages(&amp;quot;MASS&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：MASS&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod)&#10;library(MASS)&#10;library(dplyr)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ######################### Warning from 'xts' package ##########################&#10;## # #&#10;## # The dplyr lag() function breaks how base R's lag() function is supposed to #&#10;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #&#10;## # source() into this session won't work correctly. #&#10;## # #&#10;## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #&#10;## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #&#10;## # dplyr from breaking base R's lag() function. #&#10;## # #&#10;## # Code in packages is not affected. It's protected by R's namespace mechanism #&#10;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #&#10;## # #&#10;## ###############################################################################&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'dplyr'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:MASS':&#10;## &#10;## select&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:xts':&#10;## &#10;## first, last&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:stats':&#10;## &#10;## filter, lag&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## intersect, setdiff, setequal, union&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 获取苹果公司股票数据（过去2年）&#10;getSymbols(&amp;quot;AAPL&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = as.Date(&amp;quot;2023-05-01&amp;quot;), to = as.Date(Sys.Date()))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 计算对数收益率&#10;returns &amp;lt;- na.omit(ROC(Cl(AAPL))) &#10;colnames(returns) &amp;lt;- &amp;quot;AAPL_Return&amp;quot;&#10;&#10;# 参数设置&#10;set.seed(123)&#10;n_sim &amp;lt;- 10000 # 模拟次数&#10;confidence_level &amp;lt;- 0.95&#10;portfolio_value &amp;lt;- 1e6 # 组合价值100万美元&#10;&#10;# 估计收益率参数&#10;mu &amp;lt;- mean(returns)&#10;sigma &amp;lt;- sd(returns)&#10;&#10;# 蒙特卡洛模拟（正态分布）&#10;sim_normal &amp;lt;- rnorm(n_sim, mean = mu, sd = sigma)&#10;portfolio_loss_normal &amp;lt;- portfolio_value * (1 - exp(sim_normal)) # 对数收益转换为价格变动&#10;&#10;# 蒙特卡洛模拟（t分布）&#10;&#10;# 计算VaR&#10;var_normal &amp;lt;- quantile(portfolio_loss_normal, probs = confidence_level)&#10;&#10;# 格式化输出&#10;cat(&amp;quot;蒙特卡洛模拟VaR（正态分布假设）:&amp;quot;, round(var_normal, 2), &amp;quot;USD\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 蒙特卡洛模拟VaR（正态分布假设）: 27011.97 USD&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 可视化比较&#10;hist(portfolio_loss_normal, breaks = 50, main = &amp;quot;正态分布模拟损失&amp;quot;, &#10; xlab = &amp;quot;损失金额(USD)&amp;quot;, col = &amp;quot;lightblue&amp;quot;)&#10;abline(v = var_normal, col = &amp;quot;red&amp;quot;, lwd = 2)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/index_files/figure-html/single-1.png" width="672" /&gt;&#10;&lt;p&gt;也可以将模拟的分布确定为以下分布：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;偏正态分布&lt;/li&gt;&#10;&lt;li&gt;偏t分布&lt;/li&gt;&#10;&lt;li&gt;广义误差分布&lt;/li&gt;&#10;&lt;li&gt;偏广义误差分布&lt;/li&gt;&#10;&lt;li&gt;混合正态分布&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="蒙特卡洛模拟计算多资产投资组合var"&gt;&lt;a href="#%e8%92%99%e7%89%b9%e5%8d%a1%e6%b4%9b%e6%a8%a1%e6%8b%9f%e8%ae%a1%e7%ae%97%e5%a4%9a%e8%b5%84%e4%ba%a7%e6%8a%95%e8%b5%84%e7%bb%84%e5%90%88var" class="header-anchor"&gt;&lt;/a&gt;蒙特卡洛模拟计算多资产投资组合VaR&#10;&lt;/h2&gt;&lt;p&gt;蒙特卡洛模拟通过以下步骤实现多资产VaR计算:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;历史数据获取：通过quantmod获取真实资产价格数据&lt;/li&gt;&#10;&lt;li&gt;参数估计：计算资产收益率的均值向量和协方差矩阵&lt;/li&gt;&#10;&lt;li&gt;路径生成：生成服从多元正态分布的模拟收益率路径&lt;/li&gt;&#10;&lt;li&gt;组合估值：根据权重计算组合价值变化&lt;/li&gt;&#10;&lt;li&gt;损失分布：统计模拟损失的分位数得到VaR&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;## 环境配置与数据获取&#10;# ----------------------&#10;# 加载必要包&#10;# ----------------------&#10;library(ggplot2)&#10;# library(foreach)&#10;# library(doParallel)&#10;library(Matrix)&#10;library(matrixcalc)&#10;&#10;# ----------------------&#10;# 参数设置&#10;# ----------------------&#10;symbols &amp;lt;- c(&amp;quot;AAPL&amp;quot;, &amp;quot;GOOG&amp;quot;, &amp;quot;MSFT&amp;quot;, &amp;quot;AMZN&amp;quot;) # 资产列表&#10;weights &amp;lt;- c(0.3, 0.3, 0.2, 0.2) # 投资权重&#10;V0 &amp;lt;- 1000000 # 初始组合价值（美元）&#10;n_sim &amp;lt;- 100000 # 模拟次数&#10;alpha &amp;lt;- 0.99 # 置信水平&#10;T_days &amp;lt;- 10 # 持有期（天）&#10;start_date &amp;lt;- &amp;quot;2020-01-01&amp;quot;&#10;end_date &amp;lt;- format(Sys.Date(), &amp;quot;%Y-%m-%d&amp;quot;) # 动态获取最新数据&#10;&#10;# ----------------------&#10;# 获取真实历史数据&#10;# ----------------------&#10;getSymbols(symbols, src = &amp;quot;yahoo&amp;quot;, from = start_date, to = end_date)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;GOOG&amp;quot; &amp;quot;MSFT&amp;quot; &amp;quot;AMZN&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取调整后收盘价并计算日收益率&#10;prices &amp;lt;- do.call(merge, lapply(symbols, function(x) Ad(get(x))))&#10;# 替换原returns计算代码&#10;returns &amp;lt;- ROC(prices, type = &amp;quot;discrete&amp;quot;)&#10;&#10;# 添加数据清洗&#10;returns&amp;lt;- apply(returns, 2, function(x) {&#10; x[is.infinite(x)] &amp;lt;- NA&#10; na.omit(x)&#10;})&#10;&#10;# 验证数据完整性&#10;if (any(is.na(returns)) || any(is.infinite(returns))) {&#10; stop(&amp;quot;数据清洗失败，仍存在缺失值或无穷值&amp;quot;)&#10;}&#10;&#10; &#10;## 参数估计与路径生成（增强）&#10;&#10;# ----------------------&#10;# 计算统计参数（带时间调整）&#10;# ----------------------&#10;mu &amp;lt;- colMeans(returns) * T_days # 持有期收益率均值&#10;sigma &amp;lt;- cov(returns) * T_days&#10;&#10;robust_sigma &amp;lt;- function(sigma, eps=1e-8) {&#10; eig &amp;lt;- eigen(sigma)&#10; ev &amp;lt;- pmax(eig$values, eps) # 特征值下限保护&#10; sigma_robust &amp;lt;- eig$vectors %*% diag(ev) %*% t(eig$vectors)&#10; return(as.matrix(nearPD(sigma_robust, keepDiag=TRUE)$mat))&#10;}&#10;&#10;sigma &amp;lt;- robust_sigma(sigma)&#10;&#10;# 协方差矩阵正定性修正&#10;# 矩阵条件数校验&#10;if (kappa(sigma) &amp;gt; 1e6) warning(&amp;quot;协方差矩阵条件数过高，建议降维处理&amp;quot;)&#10;&#10;# 特征值范围验证&#10;eigen_values &amp;lt;- eigen(sigma)$values&#10;if (min(eigen_values) &amp;lt; 1e-6) stop(&amp;quot;矩阵修正失败&amp;quot;)&#10;&#10;# ----------------------&#10;# 蒙特卡洛模拟&#10;# ----------------------&#10;set.seed(123)&#10;&#10;sim_returns &amp;lt;- MASS::mvrnorm(n_sim, mu, sigma)&#10;portfolio_returns &amp;lt;- sim_returns %*% weights&#10;losses &amp;lt;- -V0 * portfolio_returns&#10;&#10;# ----------------------&#10;# 三维可视化（带ES标注）&#10;# ----------------------&#10;### 风险指标计算与可视化（最终修正版）&#10;&#10;# ----------------------&#10;# 风险指标计算&#10;# ----------------------&#10;VaR &amp;lt;- quantile(losses, probs=alpha,na.rm=TRUE)&#10;ES &amp;lt;- mean(losses[losses &amp;gt;= VaR])&#10;&#10;# ----------------------&#10;# 增强可视化&#10;# ----------------------&#10;&#10;# 生成精确breaks参数&#10;br &amp;lt;- pretty(range(losses), n=100)&#10;if (length(br) &amp;lt; 2) br &amp;lt;- seq(min(losses), max(losses), length.out=100)&#10;&#10;loss_plot &amp;lt;- ggplot(data.frame(Loss=losses), aes(x=Loss)) +&#10; geom_histogram(aes(y=..density..), breaks=br, fill=&amp;quot;#69b3a2&amp;quot;, alpha=0.7) +&#10; geom_vline(xintercept=VaR, color=&amp;quot;red&amp;quot;, linetype=&amp;quot;dashed&amp;quot;, size=1) +&#10; geom_vline(xintercept=ES, color=&amp;quot;blue&amp;quot;, linetype=&amp;quot;dotted&amp;quot;, size=1) +&#10; geom_density(color=&amp;quot;darkblue&amp;quot;, size=0.8, adjust=1.5) + # 调整核密度估计带宽&#10; labs(title=paste(&amp;quot;多资产组合损失分布 (&amp;quot;,T_days,&amp;quot;天持有期)&amp;quot;),&#10; x=&amp;quot;损失金额（美元）&amp;quot;, y=&amp;quot;密度&amp;quot;) +&#10; theme_minimal(base_size=12) +&#10; theme(&#10; plot.title=element_text(hjust=0.5, face=&amp;quot;bold&amp;quot;),&#10; panel.grid.major=element_line(color=&amp;quot;gray90&amp;quot;),&#10; panel.grid.minor=element_blank()&#10; )&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.&#10;## ℹ Please use `linewidth` instead.&#10;## This warning is displayed once every 8 hours.&#10;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#10;## generated.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 动态计算标注位置&#10;plot_data &amp;lt;- ggplot_build(loss_plot)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.&#10;## ℹ Please use `after_stat(density)` instead.&#10;## This warning is displayed once every 8 hours.&#10;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#10;## generated.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;max_density &amp;lt;- max(plot_data$data[[1]]$density)&#10;&#10;loss_plot &amp;lt;- loss_plot +&#10; annotate(&amp;quot;segment&amp;quot;, &#10; x=VaR, xend=VaR, &#10; y=0, yend=0.65*max_density,&#10; color=&amp;quot;red&amp;quot;, linetype=&amp;quot;dashed&amp;quot;) +&#10; annotate(&amp;quot;label&amp;quot;, &#10; x=VaR, y=0.7*max_density,&#10; label=paste0(alpha*100, &amp;quot;% VaR\n$&amp;quot;, format(round(VaR), big.mark=&amp;quot;,&amp;quot;)),&#10; color=&amp;quot;red&amp;quot;, fill=&amp;quot;white&amp;quot;, size=4) +&#10; annotate(&amp;quot;segment&amp;quot;,&#10; x=ES, xend=ES,&#10; y=0, yend=0.55*max_density,&#10; color=&amp;quot;blue&amp;quot;, linetype=&amp;quot;dotted&amp;quot;) +&#10; annotate(&amp;quot;label&amp;quot;,&#10; x=ES, y=0.6*max_density,&#10; label=paste0(&amp;quot;Expected Shortfall\n$&amp;quot;, format(round(ES), big.mark=&amp;quot;,&amp;quot;)),&#10; color=&amp;quot;blue&amp;quot;, fill=&amp;quot;white&amp;quot;, size=4)&#10;&#10;print(loss_plot)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/index_files/figure-html/trible-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# ----------------------&#10;# 结果输出&#10;# ----------------------&#10;cat(sprintf(&amp;quot;\n【增强版蒙特卡洛模拟结果】(%d天持有期)\n&amp;quot;, T_days))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 【增强版蒙特卡洛模拟结果】(10天持有期)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(&amp;quot;----------------------------------------\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ----------------------------------------&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(sprintf(&amp;quot;▪ 投资组合规模: $%s\n&amp;quot;, format(V0, big.mark=&amp;quot;,&amp;quot;)))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ▪ 投资组合规模: $1e+06&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(sprintf(&amp;quot;▪ VaR(%d%%置信水平): $%s\n&amp;quot;, alpha*100, format(round(VaR), big.mark=&amp;quot;,&amp;quot;)))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ▪ VaR(99%置信水平): $124,519&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(sprintf(&amp;quot;▪ 预期损失(ES): $%s\n&amp;quot;, format(round(ES), big.mark=&amp;quot;,&amp;quot;)))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ▪ 预期损失(ES): $143,313&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(sprintf(&amp;quot;▪ 最大可能损失: $%s\n&amp;quot;, format(round(max(losses)), big.mark=&amp;quot;,&amp;quot;)))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ▪ 最大可能损失: $237,724&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="基于混合多元正态分布优化蒙特卡洛模拟法"&gt;&lt;a href="#%e5%9f%ba%e4%ba%8e%e6%b7%b7%e5%90%88%e5%a4%9a%e5%85%83%e6%ad%a3%e6%80%81%e5%88%86%e5%b8%83%e4%bc%98%e5%8c%96%e8%92%99%e7%89%b9%e5%8d%a1%e6%b4%9b%e6%a8%a1%e6%8b%9f%e6%b3%95" class="header-anchor"&gt;&lt;/a&gt;基于混合多元正态分布优化蒙特卡洛模拟法&#10;&lt;/h2&gt;&lt;p&gt;混合多元正态分布可通过组合多个多元正态分布刻画不同市场状态下资产收益率的动态相关性和厚尾特征，适用于多资产组合的VaR计算。&lt;/p&gt;&#10;&lt;p&gt;在置信水平 &lt;code&gt;\(\alpha\)&lt;/code&gt; 下，多元混合正态模型的分位数 &lt;code&gt;\(Q_{\alpha}\)&lt;/code&gt; 满足以下等式：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\sum_{i=1}^k \pi_i \cdot \Phi\left(Q_\alpha ,;, \mu_p^{(i)}, \sigma_p^{(i)}\right) = \alpha&lt;br&gt;&#10;$$&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(\pi_i\)&lt;/code&gt;: 第 &lt;code&gt;\(i\)&lt;/code&gt; 个混合成分的权重（$\sum_{i=1}^k \pi_i = 1$）；&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\mu_p^{(i)} = \mathbf{w}^\top \boldsymbol{\mu}_i\)&lt;/code&gt;: 第 &lt;code&gt;\(i\)&lt;/code&gt;个成分的线性组合均值（$\mathbf{w}$ 为资产权重向量，$\boldsymbol{\mu}_i$ 为成分均值向量）；&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\sigma_p^{(i)} = \sqrt{\mathbf{w}^\top \boldsymbol{\Sigma}_i \mathbf{w}}\)&lt;/code&gt;: 第 &lt;code&gt;\(i\)&lt;/code&gt; 个成分的线性组合标准差（$\boldsymbol{\Sigma}_i$ 为成分协方差矩阵）；&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(\Phi(\cdot)\)&lt;/code&gt;: 标准单变量正态分布的累积分布函数（CDF）&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载包&#10;library(quantmod)&#10;library(mclust)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Package 'mclust' version 6.1.1&#10;## Type 'citation(&amp;quot;mclust&amp;quot;)' for citing this R package in publications.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(MASS)&#10;library(Matrix)&#10;library(matrixcalc)&#10;library(dplyr)&#10;library(showtext)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：sysfonts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：showtextdb&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(ggplot2)&#10;library(DescTools)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'DescTools'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from 'package:mclust':&#10;## &#10;## BrierScore&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 参数设置&#10;symbols &amp;lt;- c(&amp;quot;AAPL&amp;quot;, &amp;quot;GOOG&amp;quot;, &amp;quot;MSFT&amp;quot;)&#10;weights &amp;lt;- c(0.4, 0.3, 0.3)&#10;portfolio_value &amp;lt;- 1e6&#10;confidence_level &amp;lt;- 0.95&#10;&#10;# 数据获取与处理&#10;getSymbols(symbols, from = &amp;quot;2020-01-01&amp;quot;, to = Sys.Date())&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;GOOG&amp;quot; &amp;quot;MSFT&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;prices &amp;lt;- do.call(merge, lapply(symbols, function(x) Cl(get(x))))&#10;returns &amp;lt;- ROC(prices,type = &amp;quot;discrete&amp;quot;) %&amp;gt;% na.omit() %&amp;gt;% as.matrix()&#10;&#10;# 混合模型拟合与修正&#10;set.seed(123)&#10;mix_model &amp;lt;- Mclust(returns, G = 2)&#10;&#10;# 提取并修正模型参数&#10;weights_mix &amp;lt;- mix_model$parameters$pro&#10;means_mix &amp;lt;- mix_model$parameters$mean&#10;&#10;# 检查混合模型协方差参数维度&#10;sigma_dim &amp;lt;- dim(mix_model$parameters$variance$sigma)&#10;cat(&amp;quot;Sigma参数维度:&amp;quot;, sigma_dim, &amp;quot;\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Sigma参数维度: 3 3 2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 确认模型类型是否兼容&#10;if (mix_model$modelName != &amp;quot;VVV&amp;quot;) {&#10; warning(&amp;quot;当前模型&amp;quot;, mix_model$modelName, &#10; &amp;quot;可能使用共享协方差矩阵结构，需调整修正逻辑&amp;quot;)&#10;}&#10;&#10;&#10;&#10;covs_mix &amp;lt;- lapply(1:mix_model$G, function(g) {&#10; # 提取第g个成分的协方差矩阵&#10; sigma &amp;lt;- mix_model$parameters$variance$sigma[,,g]&#10; &#10; # 验证矩阵维度&#10; if (!matrixcalc::is.square.matrix(sigma)) {&#10; stop(paste(&amp;quot;成分&amp;quot;, g, &amp;quot;协方差矩阵非正方形，维度:&amp;quot;, &#10; paste(dim(sigma), collapse=&amp;quot;x&amp;quot;)))&#10; }&#10; &#10; # 特征值修正&#10; eig &amp;lt;- eigen(sigma)&#10; ev &amp;lt;- pmax(eig$values, 1e-6)&#10; sigma_corrected &amp;lt;- eig$vectors %*% diag(ev) %*% t(eig$vectors)&#10; as.matrix(nearPD(sigma_corrected)$mat)&#10;})&#10;&#10;# 增强型修正函数&#10;robust_cov_fix &amp;lt;- function(sigma) {&#10; if (!is.matrix(sigma) || nrow(sigma) != ncol(sigma)) {&#10; sigma &amp;lt;- matrix(sigma, nrow=sqrt(length(sigma))) # 向量转矩阵&#10; }&#10; &#10; # 添加对称性强制修正&#10; sigma &amp;lt;- 0.5 * (sigma + t(sigma))&#10; &#10; # 执行特征值修正&#10; eig &amp;lt;- eigen(sigma)&#10; ev &amp;lt;- pmax(eig$values, 1e-6)&#10; eig$vectors %*% diag(ev) %*% t(eig$vectors)&#10;}&#10;&#10;# 应用修正函数&#10;covs_mix &amp;lt;- lapply(1:mix_model$G, function(g) {&#10; as.matrix(nearPD(&#10; robust_cov_fix(mix_model$parameters$variance$sigma[,,g])&#10; )$mat)&#10;})&#10;&#10;# 验证所有协方差矩阵性质&#10;sapply(covs_mix, function(x) {&#10; c(dim=dim(x), isSquare=matrixcalc::is.square.matrix(x))&#10;}) # 维度应统一为NxN&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2]&#10;## dim1 3 3&#10;## dim2 3 3&#10;## isSquare 1 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 检查正定性&#10;sapply(covs_mix, matrixcalc::is.positive.definite) # 应全为TRUE&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] TRUE TRUE&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 安全模拟函数&#10;safe_mvrnorm &amp;lt;- function(n, mu, sigma) {&#10; sigma_fixed &amp;lt;- as.matrix(nearPD(sigma)$mat)&#10; MASS::mvrnorm(n, mu, sigma_fixed)&#10;}&#10;&#10;# 蒙特卡洛模拟&#10;n_sim &amp;lt;- 10000&#10;sim_returns &amp;lt;- matrix(0, nrow = n_sim, ncol = 3)&#10;&#10;for (i in 1:n_sim) {&#10; component &amp;lt;- sample(1:length(weights_mix), 1, prob = weights_mix)&#10; sim_returns[i, ] &amp;lt;- safe_mvrnorm(1, &#10; means_mix[, component], &#10; covs_mix[[component]])&#10;}&#10;&#10;# 查看模拟结果摘要&#10;summary(sim_returns) # 不应有NA或极端值&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## V1 V2 V3 &#10;## Min. :-0.1235547 Min. :-0.1244924 Min. :-0.1164468 &#10;## 1st Qu.:-0.0101667 1st Qu.:-0.0104414 1st Qu.:-0.0098317 &#10;## Median : 0.0012049 Median : 0.0009092 Median : 0.0006312 &#10;## Mean : 0.0009236 Mean : 0.0006466 Mean : 0.0007475 &#10;## 3rd Qu.: 0.0126020 3rd Qu.: 0.0120780 3rd Qu.: 0.0113348 &#10;## Max. : 0.1311838 Max. : 0.1262479 Max. : 0.1401261&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 计算VaR&#10;portfolio_returns &amp;lt;- sim_returns %*% weights&#10;&#10;portfolio_returns &amp;lt;- na.omit(portfolio_returns) # 移除缺失值&#10;if (sd(portfolio_returns) &amp;lt; 1e-6) stop(&amp;quot;收益波动率过低，无法生成有效分布&amp;quot;)&#10;&#10;# Winsorize处理极端值&#10;&#10;portfolio_returns &amp;lt;- Winsorize(portfolio_returns)&#10;losses &amp;lt;- -portfolio_value * portfolio_returns&#10;# 计算VaR和ES&#10;VaR &amp;lt;- quantile(losses, 1 - confidence_level)&#10;ES &amp;lt;- mean(losses[losses &amp;lt;= VaR])&#10;&#10;# 生成自适应breaks&#10;br &amp;lt;- pretty(range(portfolio_returns), n = 100)&#10;if (length(br) &amp;lt; 10) br &amp;lt;- seq(min(portfolio_returns), max(portfolio_returns), length.out=100)&#10;&#10;&#10;cat(&amp;quot;95% VaR:&amp;quot;, round(VaR, 2), &amp;quot;元\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 95% VaR: -27728.82 元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(&amp;quot;95% ES:&amp;quot;,round(ES, 2),&amp;quot;元\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 95% ES: -27729.96 元&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 验证VaR计算合理性&#10;cat(&amp;quot;VaR占组合比例:&amp;quot;, round(VaR/portfolio_value*100, 2), &amp;quot;%\n&amp;quot;) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## VaR占组合比例: -2.77 %&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cat(&amp;quot;ES占组合比例:&amp;quot;, round(ES/portfolio_value*100,2),&amp;quot;%\n&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ES占组合比例: -2.77 %&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 正常范围通常在1%-10%之间&#10;&#10;showtext_auto() # 启用中文字体支持&#10;&#10;# 计算VaR和ES的标签文本&#10;var_label &amp;lt;- paste0(confidence_level*100, &amp;quot;% VaR: ¥&amp;quot;, format(round(VaR), big.mark = &amp;quot;,&amp;quot;))&#10;es_label &amp;lt;- paste0(confidence_level*100, &amp;quot;% ES: ¥&amp;quot;, format(round(ES), big.mark = &amp;quot;,&amp;quot;))&#10;&#10;# 生成风险可视化图表&#10;loss_plot &amp;lt;- ggplot(data.frame(losses), aes(x = losses)) +&#10; geom_histogram(aes(y = after_stat(density)), &#10; fill = &amp;quot;#6baed6&amp;quot;, alpha = 0.7, color = &amp;quot;white&amp;quot;) + # 优化填充色并添加边框&#10; geom_vline(aes(xintercept = VaR, color = &amp;quot;VaR&amp;quot;), &#10; linetype = &amp;quot;dashed&amp;quot;, linewidth = 1.2) +&#10; geom_vline(aes(xintercept = ES, color = &amp;quot;ES&amp;quot;), &#10; linetype = &amp;quot;dotdash&amp;quot;, linewidth = 1.2) + # 添加ES线&#10; annotate(&amp;quot;text&amp;quot;, x = VaR, y = Inf, label = var_label, # 使用annotate固定标签位置&#10; color = &amp;quot;#de2d26&amp;quot;, vjust = 2.5, size = 5, family = &amp;quot;sans&amp;quot;) +&#10; annotate(&amp;quot;text&amp;quot;, x = ES, y = Inf, label = es_label,&#10; color = &amp;quot;#3182bd&amp;quot;, vjust = 4.5, size = 5, family = &amp;quot;sans&amp;quot;) +&#10; scale_color_manual(name = &amp;quot;风险指标&amp;quot;,&#10; values = c(&amp;quot;VaR&amp;quot; = &amp;quot;#de2d26&amp;quot;, &amp;quot;ES&amp;quot; = &amp;quot;#3182bd&amp;quot;),&#10; guide = guide_legend(override.aes = list(&#10; linetype = c(&amp;quot;dashed&amp;quot;, &amp;quot;dotdash&amp;quot;)))) +&#10; labs(title = &amp;quot;投资组合风险价值分布可视化&amp;quot;,&#10; subtitle = paste(&amp;quot;基于&amp;quot;, length(symbols), &amp;quot;资产混合正态模型蒙特卡洛模拟&amp;quot;),&#10; x = &amp;quot;潜在损失金额 (元)&amp;quot;, y = &amp;quot;概率密度&amp;quot;) +&#10; theme_minimal(base_size = 14) +&#10; theme(&#10; text = element_text(family = &amp;quot;sans&amp;quot;), # 确保中文字体显示&#10; plot.title = element_text(hjust = 0.5, face = &amp;quot;bold&amp;quot;, size = 18),&#10; plot.subtitle = element_text(hjust = 0.5, color = &amp;quot;gray40&amp;quot;, size = 12),&#10; axis.title = element_text(size = 12),&#10; axis.text = element_text(color = &amp;quot;gray30&amp;quot;),&#10; panel.grid.major = element_line(color = &amp;quot;grey90&amp;quot;, linewidth = 0.3),&#10; panel.grid.minor = element_blank(), # 移除次要网格线&#10; legend.position = &amp;quot;top&amp;quot;,&#10; legend.title = element_text(size = 12),&#10; legend.text = element_text(size = 10),&#10; plot.margin = margin(1, 1, 1, 1, &amp;quot;cm&amp;quot;)&#10; ) +&#10; coord_cartesian(clip = &amp;quot;off&amp;quot;) # 允许标签超出绘图区域&#10;&#10;print(loss_plot)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/index_files/figure-html/mixed-1.png" width="672" /&gt;&#10;</description></item></channel></rss>