<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>格物堂</title>
    <link>http://gewutang.cn/</link>
    <description>Recent content on 格物堂</description>
    <generator>Hugo</generator>
    <language>en-US</language>
    <lastBuildDate>Tue, 23 Dec 2025 00:00:00 +0000</lastBuildDate>
    <atom:link href="http://gewutang.cn/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>全天候动量ETF策略</title>
      <link>http://gewutang.cn/2025/12/23/all-weather-momentum/</link>
      <pubDate>Tue, 23 Dec 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/12/23/all-weather-momentum/</guid>
      <description>&lt;pre&gt;&lt;code&gt;## 正在从Yahoo Finance获取数据...&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;513100.SS&amp;quot; &amp;quot;510300.SS&amp;quot; &amp;quot;518880.SS&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 2. 计算指标：动量、波动率、调整动量&#xA;calculate_metrics &amp;lt;- function(price_series, window = 20) {&#xA;  # 计算日收益率&#xA;  returns &amp;lt;- dailyReturn(price_series, type = &amp;quot;arithmetic&amp;quot;)&#xA;  colnames(returns) &amp;lt;- &amp;quot;Return&amp;quot;&#xA;  &#xA;  # 计算20日平均收益率（动量）&#xA;  momentum &amp;lt;- rollapply(returns, width = window, FUN = mean, align = &amp;quot;right&amp;quot;, fill = NA)&#xA;  colnames(momentum) &amp;lt;- &amp;quot;Momentum&amp;quot;&#xA;  &#xA;  # 计算20日收益率方差（波动率）&#xA;  volatility &amp;lt;- rollapply(returns, width = window, FUN = var, align = &amp;quot;right&amp;quot;, fill = NA)&#xA;  colnames(volatility) &amp;lt;- &amp;quot;Volatility&amp;quot;&#xA;  &#xA;  # 计算调整动量（动量/波动率）&#xA;  # 注意：使用标准差进行标准化（方差的平方根）&#xA;  adj_momentum &amp;lt;- momentum / sqrt(volatility)&#xA;  colnames(adj_momentum) &amp;lt;- &amp;quot;Adj_Momentum&amp;quot;&#xA;  &#xA;  return(list(returns = returns, &#xA;              momentum = momentum, &#xA;              volatility = volatility, &#xA;              adj_momentum = adj_momentum))&#xA;}&#xA;&#xA;# 为每个ETF计算指标&#xA;# cat(&amp;quot;正在计算动量指标...\n&amp;quot;)&#xA;metrics_list &amp;lt;- list()&#xA;for (etf in etf_names) {&#xA;  metrics_list[[etf]] &amp;lt;- calculate_metrics(price_df[, etf])&#xA;}&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in to_period(xx, period = on.opts[[period]], ...): missing values&#xA;## removed from data&#xA;## Warning in to_period(xx, period = on.opts[[period]], ...): missing values&#xA;## removed from data&#xA;## Warning in to_period(xx, period = on.opts[[period]], ...): missing values&#xA;## removed from data&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 3. 生成交易信号和权重&#xA;generate_weights &amp;lt;- function(adj_momentum_df) {&#xA;  # 初始化权重数据框&#xA;  weights_df &amp;lt;- as.data.frame(adj_momentum_df)&#xA;  weights_df[] &amp;lt;- 0  # 所有值初始化为0&#xA;  &#xA;  # 对每一行（每个交易日）计算权重&#xA;  for (i in 1:nrow(adj_momentum_df)) {&#xA;    # 获取当日的调整动量值&#xA;    current_momentum &amp;lt;- as.numeric(adj_momentum_df[i, ])&#xA;    &#xA;    # 筛选调整动量大于0的标的&#xA;    positive_idx &amp;lt;- which(current_momentum &amp;gt; 0 &amp;amp; !is.na(current_momentum))&#xA;    &#xA;    if (length(positive_idx) &amp;gt; 0) {&#xA;      # 获取正的调整动量值&#xA;      positive_momentum &amp;lt;- current_momentum[positive_idx]&#xA;      &#xA;      # 按调整动量大小归一化计算权重&#xA;      normalized_weights &amp;lt;- positive_momentum / sum(positive_momentum)&#xA;      &#xA;      # 分配权重&#xA;      weights_df[i, positive_idx] &amp;lt;- normalized_weights&#xA;    }&#xA;  }&#xA;  &#xA;  # 转换为时间序列&#xA;  weights_xts &amp;lt;- xts(weights_df, order.by = index(adj_momentum_df))&#xA;  colnames(weights_xts) &amp;lt;- colnames(adj_momentum_df)&#xA;  &#xA;  return(weights_xts)&#xA;}&#xA;&#xA;# 提取所有ETF的调整动量&#xA;adj_momentum_all &amp;lt;- do.call(merge, lapply(metrics_list, function(x) x$adj_momentum))&#xA;colnames(adj_momentum_all) &amp;lt;- etf_names&#xA;&#xA;# 生成权重序列&#xA;# cat(&amp;quot;正在计算每日权重...\n&amp;quot;)&#xA;weights &amp;lt;- generate_weights(adj_momentum_all)&#xA;&#xA;# 4. 将权重数据整理到数据框中&#xA;# 创建每日权重数据框（包含日期）&#xA;daily_weights_df &amp;lt;- data.frame(Date = index(weights))&#xA;for (etf in etf_names) {&#xA;  daily_weights_df[[etf]] &amp;lt;- as.numeric(weights[, etf])&#xA;}&#xA;&#xA;# 计算每个交易日的权重合计（应为1或0）&#xA;daily_weights_df$权重合计 &amp;lt;- rowSums(daily_weights_df[, etf_names], na.rm = TRUE)&#xA;&#xA;# 创建每个ETF的单独权重数据框&#xA;etf_weight_dfs &amp;lt;- list()&#xA;for (etf in etf_names) {&#xA;  etf_weight_dfs[[etf]] &amp;lt;- data.frame(&#xA;    Date = index(weights),&#xA;    ETF = etf,&#xA;    Weight = as.numeric(weights[, etf])&#xA;  )&#xA;}&#xA;&#xA;# 合并所有ETF权重数据框&#xA;all_etf_weights_df &amp;lt;- do.call(rbind, etf_weight_dfs)&#xA;&#xA;# 5. 回测计算&#xA;# 计算每个ETF的日收益率&#xA;returns_all &amp;lt;- do.call(merge, lapply(metrics_list, function(x) x$returns))&#xA;colnames(returns_all) &amp;lt;- etf_names&#xA;&#xA;# 调整权重的时间索引，确保与收益率对齐&#xA;# 使用T日的权重在T+1日交易&#xA;lagged_weights &amp;lt;- lag(weights, 1)  # 权重滞后一天&#xA;lagged_weights &amp;lt;- lagged_weights[index(returns_all)]  # 对齐索引&#xA;&#xA;# 处理缺失值&#xA;lagged_weights[is.na(lagged_weights)] &amp;lt;- 0&#xA;&#xA;# 计算策略日收益率（按T+1日开盘价计算，这里用平均价近似）&#xA;strategy_returns &amp;lt;- rowSums(returns_all * lagged_weights, na.rm = TRUE)&#xA;strategy_returns &amp;lt;- xts(strategy_returns, order.by = index(returns_all))&#xA;colnames(strategy_returns) &amp;lt;- &amp;quot;动量策略&amp;quot;&#xA;&#xA;# 6. 绩效指标计算&#xA;# cat(&amp;quot;正在计算绩效指标...\n&amp;quot;)&#xA;# 策略绩效&#xA;strategy_perf &amp;lt;- table.AnnualizedReturns(strategy_returns)&#xA;strategy_dd &amp;lt;- table.DownsideRisk(strategy_returns)&#xA;max_dd &amp;lt;- maxDrawdown(strategy_returns)&#xA;strategy_calmar &amp;lt;- CalmarRatio(strategy_returns)&#xA;strategy_sortino &amp;lt;- SortinoRatio(strategy_returns)&#xA;strategy_positive_returns &amp;lt;- sum(strategy_returns &amp;gt; 0, na.rm = TRUE)&#xA;strategy_total_returns &amp;lt;- sum(!is.na(strategy_returns))&#xA;strategy_win_rate &amp;lt;- strategy_positive_returns / strategy_total_returns&#xA;&#xA;# 基准绩效（沪深300ETF）&#xA;benchmark_returns &amp;lt;- returns_all[, &amp;quot;沪深300ETF&amp;quot;]&#xA;colnames(benchmark_returns) &amp;lt;- &amp;quot;沪深300ETF&amp;quot;&#xA;benchmark_perf &amp;lt;- table.AnnualizedReturns(benchmark_returns)&#xA;benchmark_dd &amp;lt;- table.DownsideRisk(benchmark_returns)&#xA;benchmark_max_dd &amp;lt;- maxDrawdown(benchmark_returns)&#xA;benchmark_calmar &amp;lt;- CalmarRatio(benchmark_returns)&#xA;benchmark_sortino &amp;lt;- SortinoRatio(benchmark_returns)&#xA;benchmark_positive_returns &amp;lt;- sum(benchmark_returns &amp;gt; 0, na.rm = TRUE)&#xA;benchmark_total_returns &amp;lt;- sum(!is.na(benchmark_returns))&#xA;benchmark_win_rate &amp;lt;- benchmark_positive_returns / benchmark_total_returns&#xA;&#xA;# 等权重组合绩效&#xA;equal_weights &amp;lt;- matrix(1/3, nrow = nrow(returns_all), ncol = ncol(returns_all))&#xA;equal_weights &amp;lt;- xts(equal_weights, order.by = index(returns_all))&#xA;colnames(equal_weights) &amp;lt;- etf_names&#xA;equal_returns &amp;lt;- rowSums(returns_all * equal_weights, na.rm = TRUE)&#xA;equal_returns &amp;lt;- xts(equal_returns, order.by = index(returns_all))&#xA;colnames(equal_returns) &amp;lt;- &amp;quot;等权重组合&amp;quot;&#xA;equal_perf &amp;lt;- table.AnnualizedReturns(equal_returns)&#xA;equal_max_dd &amp;lt;- maxDrawdown(equal_returns)&#xA;equal_calmar &amp;lt;- CalmarRatio(equal_returns)&#xA;equal_sortino &amp;lt;- SortinoRatio(equal_returns)&#xA;equal_positive_returns &amp;lt;- sum(equal_returns &amp;gt; 0, na.rm = TRUE)&#xA;equal_total_returns &amp;lt;- sum(!is.na(equal_returns))&#xA;equal_win_rate &amp;lt;- equal_positive_returns / equal_total_returns&#xA;&#xA;# 7. 创建绩效指标汇总数据框&#xA;# 格式化日期字符串&#xA;start_date_str &amp;lt;- format(start_date, &amp;quot;%Y-%m-%d&amp;quot;)&#xA;end_date_str &amp;lt;- format(end_date, &amp;quot;%Y-%m-%d&amp;quot;)&#xA;period_str &amp;lt;- paste(start_date_str, &amp;quot;至&amp;quot;, end_date_str)&#xA;&#xA;performance_metrics_df &amp;lt;- data.frame(&#xA;  策略 = c(&amp;quot;动量策略&amp;quot;, &amp;quot;沪深300ETF&amp;quot;, &amp;quot;等权重组合&amp;quot;),&#xA;  年化收益率 = c(&#xA;    round(strategy_perf[1, 1] * 100, 2),&#xA;    round(benchmark_perf[1, 1] * 100, 2),&#xA;    round(equal_perf[1, 1] * 100, 2)&#xA;  ),&#xA;  年化波动率 = c(&#xA;    round(strategy_perf[2, 1] * 100, 2),&#xA;    round(benchmark_perf[2, 1] * 100, 2),&#xA;    round(equal_perf[2, 1] * 100, 2)&#xA;  ),&#xA;  夏普比率 = c(&#xA;    round(strategy_perf[3, 1], 3),&#xA;    round(benchmark_perf[3, 1], 3),&#xA;    round(equal_perf[3, 1], 3)&#xA;  ),&#xA;  最大回撤 = c(&#xA;    round(max_dd * 100, 2),&#xA;    round(benchmark_max_dd * 100, 2),&#xA;    round(equal_max_dd * 100, 2)&#xA;  ),&#xA;  卡尔马比率 = c(&#xA;    round(strategy_calmar, 3),&#xA;    round(benchmark_calmar, 3),&#xA;    round(equal_calmar, 3)&#xA;  ),&#xA;  索提诺比率 = c(&#xA;    round(strategy_sortino, 3),&#xA;    round(benchmark_sortino, 3),&#xA;    round(equal_sortino, 3)&#xA;  ),&#xA;  胜率 = c(&#xA;    round(strategy_win_rate * 100, 2),&#xA;    round(benchmark_win_rate * 100, 2),&#xA;    round(equal_win_rate * 100, 2)&#xA;  ),&#xA;  正收益天数 = c(&#xA;    strategy_positive_returns,&#xA;    benchmark_positive_returns,&#xA;    equal_positive_returns&#xA;  ),&#xA;  总交易天数 = c(&#xA;    strategy_total_returns,&#xA;    benchmark_total_returns,&#xA;    equal_total_returns&#xA;  ),&#xA;  回测期间 = c(period_str, period_str, period_str)&#xA;)&#xA;&#xA;# 8. 绩效指标输出&#xA;# cat(&amp;quot;==================== 绩效指标汇总数据框 ====================\n\n&amp;quot;)&#xA;print(performance_metrics_df)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##         策略 年化收益率 年化波动率 夏普比率 最大回撤 卡尔马比率 索提诺比率&#xA;## 1   动量策略      45.13      17.08    2.643     8.72      5.178      0.227&#xA;## 2 沪深300ETF      20.31      19.27    1.054    16.25      1.250      0.103&#xA;## 3 等权重组合      31.84      12.89    2.470     9.29      3.429      0.201&#xA;##    胜率 正收益天数 总交易天数                 回测期间&#xA;## 1 53.04        253        477 2024-01-01 至 2025-12-23&#xA;## 2 51.26        244        476 2024-01-01 至 2025-12-23&#xA;## 3 59.33        283        477 2024-01-01 至 2025-12-23&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# cat(&amp;quot;\n&amp;quot;)&#xA;&#xA;# 9. 输出权重数据&#xA;&#xA;# cat(&amp;quot;每日组合权重数据框（后10行）:\n&amp;quot;)&#xA;print(tail(daily_weights_df, 10))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##           Date    纳指ETF 沪深300ETF   黄金ETF 权重合计&#xA;## 468 2025-12-08 0.09379501 0.00000000 0.9062050        1&#xA;## 469 2025-12-09 0.00000000 0.00000000 0.0000000        0&#xA;## 470 2025-12-10 0.00000000 0.00000000 1.0000000        1&#xA;## 471 2025-12-11 0.00000000 0.00000000 0.0000000        0&#xA;## 472 2025-12-12 0.05889600 0.00000000 0.9411040        1&#xA;## 473 2025-12-15 0.00000000 0.00000000 1.0000000        1&#xA;## 474 2025-12-16 0.16844695 0.00000000 0.8315530        1&#xA;## 475 2025-12-17 0.31823928 0.01791372 0.6638470        1&#xA;## 476 2025-12-18 0.00000000 0.00000000 1.0000000        1&#xA;## 477 2025-12-19 0.22970999 0.26353990 0.5067501        1&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# cat(&amp;quot;\n&amp;quot;)&#xA;&#xA;&#xA;# 10. 绘制图表&#xA;# cat(&amp;quot;正在生成图表...\n&amp;quot;)&#xA;# 准备累计收益率数据&#xA;cumulative_returns &amp;lt;- merge(&#xA;  cumprod(1 + na.fill(strategy_returns, 0)) - 1,&#xA;  cumprod(1 + na.fill(benchmark_returns, 0)) - 1,&#xA;  cumprod(1 + na.fill(equal_returns, 0)) - 1&#xA;)&#xA;colnames(cumulative_returns) &amp;lt;- c(&amp;quot;动量策略&amp;quot;, &amp;quot;沪深300ETF&amp;quot;, &amp;quot;等权重组合&amp;quot;)&#xA;&#xA;# 转换为数据框用于ggplot&#xA;cumulative_df &amp;lt;- data.frame(&#xA;  Date = index(cumulative_returns),&#xA;  as.data.frame(cumulative_returns)&#xA;)&#xA;cumulative_long &amp;lt;- pivot_longer(cumulative_df, &#xA;                                cols = -Date, &#xA;                                names_to = &amp;quot;Portfolio&amp;quot;, &#xA;                                values_to = &amp;quot;CumulativeReturn&amp;quot;)&#xA;&#xA;# 图表1：策略 vs 基准累计收益率和最大回撤对比&#xA;p1 &amp;lt;- ggplot(cumulative_long %&amp;gt;% filter(Portfolio %in% c(&amp;quot;动量策略&amp;quot;, &amp;quot;沪深300ETF&amp;quot;)), &#xA;             aes(x = Date, y = CumulativeReturn, color = Portfolio)) +&#xA;  geom_line(size = 1.2) +&#xA;  labs(title = &amp;quot;动量策略 vs 沪深300ETF: 累计收益率对比&amp;quot;,&#xA;       x = &amp;quot;日期&amp;quot;, y = &amp;quot;累计收益率&amp;quot;) +&#xA;  scale_y_continuous(labels = percent_format()) +&#xA;  scale_color_manual(values = c(&amp;quot;动量策略&amp;quot; = &amp;quot;blue&amp;quot;, &amp;quot;沪深300ETF&amp;quot; = &amp;quot;red&amp;quot;)) +&#xA;  theme_minimal() +&#xA;  theme(legend.position = &amp;quot;bottom&amp;quot;,&#xA;        plot.title = element_text(hjust = 0.5, size = 14, face = &amp;quot;bold&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.&#xA;## ℹ Please use `linewidth` instead.&#xA;## This warning is displayed once every 8 hours.&#xA;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#xA;## generated.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 图表2：策略 vs 各ETF Buy &amp;amp; Hold对比&#xA;# 计算各ETF的累计收益率&#xA;etf_cumulative &amp;lt;- do.call(merge, &#xA;                          lapply(1:length(etf_names), &#xA;                                 function(i) cumprod(1 + na.fill(returns_all[, i], 0)) - 1))&#xA;colnames(etf_cumulative) &amp;lt;- paste0(etf_names, &amp;quot; (Buy&amp;amp;Hold)&amp;quot;)&#xA;&#xA;all_cumulative &amp;lt;- merge(cumulative_returns[, &amp;quot;动量策略&amp;quot;], etf_cumulative)&#xA;all_cumulative_df &amp;lt;- data.frame(Date = index(all_cumulative), &#xA;                                as.data.frame(all_cumulative))&#xA;all_cumulative_long &amp;lt;- pivot_longer(all_cumulative_df, &#xA;                                    cols = -Date, &#xA;                                    names_to = &amp;quot;Strategy&amp;quot;, &#xA;                                    values_to = &amp;quot;CumulativeReturn&amp;quot;)&#xA;&#xA;p2 &amp;lt;- ggplot(all_cumulative_long, aes(x = Date, y = CumulativeReturn, color = Strategy)) +&#xA;  geom_line(size = 1) +&#xA;  labs(title = &amp;quot;动量策略 vs 各ETF买入持有策略: 累计收益率对比&amp;quot;,&#xA;       x = &amp;quot;日期&amp;quot;, y = &amp;quot;累计收益率&amp;quot;) +&#xA;  scale_y_continuous(labels = percent_format()) +&#xA;  theme_minimal() +&#xA;  theme(legend.position = &amp;quot;bottom&amp;quot;,&#xA;        plot.title = element_text(hjust = 0.5, size = 14, face = &amp;quot;bold&amp;quot;))&#xA;&#xA;# 图表3：策略 vs 等权重组合对比&#xA;p3 &amp;lt;- ggplot(cumulative_long %&amp;gt;% filter(Portfolio %in% c(&amp;quot;动量策略&amp;quot;, &amp;quot;等权重组合&amp;quot;)), &#xA;             aes(x = Date, y = CumulativeReturn, color = Portfolio)) +&#xA;  geom_line(size = 1.2) +&#xA;  labs(title = &amp;quot;动量策略 vs 等权重组合: 累计收益率对比&amp;quot;,&#xA;       x = &amp;quot;日期&amp;quot;, y = &amp;quot;累计收益率&amp;quot;) +&#xA;  scale_y_continuous(labels = percent_format()) +&#xA;  scale_color_manual(values = c(&amp;quot;动量策略&amp;quot; = &amp;quot;blue&amp;quot;, &amp;quot;等权重组合&amp;quot; = &amp;quot;green&amp;quot;)) +&#xA;  theme_minimal() +&#xA;  theme(legend.position = &amp;quot;bottom&amp;quot;,&#xA;        plot.title = element_text(hjust = 0.5, size = 14, face = &amp;quot;bold&amp;quot;))&#xA;&#xA;# 图表4：权重随时间变化图&#xA;# 准备权重数据&#xA;weights_long &amp;lt;- pivot_longer(daily_weights_df, &#xA;                             cols = all_of(etf_names),&#xA;                             names_to = &amp;quot;ETF&amp;quot;, &#xA;                             values_to = &amp;quot;Weight&amp;quot;)&#xA;&#xA;p4 &amp;lt;- ggplot(weights_long, aes(x = Date, y = Weight, fill = ETF)) +&#xA;  geom_area(position = &amp;quot;stack&amp;quot;, alpha = 0.7) +&#xA;  labs(title = &amp;quot;动量策略: 每日权重分配&amp;quot;,&#xA;       x = &amp;quot;日期&amp;quot;, y = &amp;quot;权重&amp;quot;) +&#xA;  scale_y_continuous(labels = percent_format()) +&#xA;  scale_fill_manual(values = c(&amp;quot;纳指ETF&amp;quot; = &amp;quot;blue&amp;quot;, &#xA;                               &amp;quot;沪深300ETF&amp;quot; = &amp;quot;red&amp;quot;, &#xA;                               &amp;quot;黄金ETF&amp;quot; = &amp;quot;gold&amp;quot;)) +&#xA;  theme_minimal() +&#xA;  theme(legend.position = &amp;quot;bottom&amp;quot;,&#xA;        plot.title = element_text(hjust = 0.5, size = 14, face = &amp;quot;bold&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 12. 显示图表&#xA;# cat(&amp;quot;\n正在显示图表...\n&amp;quot;)&#xA;print(p1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/12/23/all-weather-momentum/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>《并非有效的市场》笔记</title>
      <link>http://gewutang.cn/2025/12/22/inefficient-markets/</link>
      <pubDate>Mon, 22 Dec 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/12/22/inefficient-markets/</guid>
      <description>&lt;p&gt;安德瑞·史莱佛撰写 &lt;strong&gt;《并非有效的金融市场》&lt;/strong&gt; 的核心目的在于对传统金融领域的 &lt;strong&gt;有效市场假说&lt;/strong&gt; （EMH）提出系统性批判。史&#xA;莱佛通过大量实证研究与逻辑推演，打破了&lt;em&gt;金融市场是完全理性、信息充分且价格能即时反映所有有效信息&lt;/em&gt;的经典认知，本书的议题围绕&lt;em&gt;市场无效性的根源&lt;/em&gt;、&lt;em&gt;表现形式&lt;/em&gt;及&lt;em&gt;对投资实践的启示&lt;/em&gt;展开，为我们理解金融市场的复杂性、非理性提供了全新视角，尤其对主动投资策略的合理性提供了理论支撑。&lt;/p&gt;&#xA;&lt;p&gt;本书并非全盘否定金融市场的合理性，而是指出&lt;strong&gt;有效市场&lt;/strong&gt;只是理论抽象的理想状态，现实市场中存在诸多扭曲价格的因素，这些因素共同构成了市场无效的基础，也为投资者创造了获取超额收益的可能。&lt;/p&gt;&#xA;&lt;h1 id=&#34;对有效市场假说的批判&#34;&gt;对“有效市场假说”的批判&lt;/h1&gt;&#xA;&lt;p&gt;有效市场假说的核心逻辑是：市场参与者是完全理性的，信息能无成本、即时地被所有参与者获取，因此资产价格始终等于其内在价值，投资者无法通过主动分析获得超额收益。&lt;/p&gt;&#xA;&lt;p&gt;史莱佛从三个维度对这一假说提出反驳：&lt;/p&gt;&#xA;&lt;p&gt;1.市场参与者并非完全理性——非理性行为的普遍性&lt;/p&gt;&#xA;&lt;p&gt;史莱佛结合行为金融学的研究成果，指出现实中的投资者普遍存在“有限理性”特征，而非经典理论中的“完全理性人”。投&#xA;资者的决策往往受到情绪、认知偏差、从众心理等因素影响，而非纯粹基于对信息的客观分析。&lt;/p&gt;&#xA;&lt;p&gt;书中列举的典型非理性行为包括：锚定效应（以某一初始价格为基准判断资产价值，忽视真实基本面）、羊群效应（盲目跟随大众投资决策，导致市场过度反应）、过度自信（高估自身分析能力，导致频繁交易与决策失误）。&lt;/p&gt;&#xA;&lt;p&gt;这些非理性行为并非个别现象，而是形成了群体合力，最终导致资产价格偏离内在价值，形成市场泡沫或过度下跌。&lt;/p&gt;&#xA;&lt;p&gt;2.信息存在不对称与传递成本——信息并非完全充分&lt;/p&gt;&#xA;&lt;p&gt;有效市场假说假设“信息免费且即时共享”，但现实中信息存在显著的不对称性：内部人（如企业管理层、核心机构投资者）往往掌握比普通投资者更全面、准确的信息，且信息传递过程中存在时间差、解读成本等问题。普通投资者不仅难以第一时间获取信息，还可能因专业能力不足对信息产生误读。&lt;/p&gt;&#xA;&lt;p&gt;通过实证数据证明，信息不对称会导致资产价格对信息的反应滞后或过度：部分机构利用信息优势提前布局，待信息公开后获利离场，而普通投资者则可能在价格波动的末端跟风，成为损失的承担者。这&#xA;种信息壁垒直接打破了“价格即时反映所有信息”的有效市场前提。&lt;/p&gt;&#xA;&lt;p&gt;3.市场存在交易成本与制度缺陷——价格无法即时调整至合理水平&lt;/p&gt;&#xA;&lt;p&gt;有效市场假说忽略了交易成本（如手续费、税费、流动性成本）与制度设计对市场的影响。现&#xA;实中，即使投资者发现了价格与价值的偏离，也可能因交易成本过高、市场流动性不足或制度限制（如做空机制不完善）而无法及时通过交易修正价格，导致价格扭曲长期存在。&lt;/p&gt;&#xA;&lt;p&gt;此外，制度缺陷还可能引发“非理性的市场定价”，例如退市机制不健全导致劣质资产长期占据市场资源，政策干预的不确定性引发市场恐慌或盲目乐观，这些因素都让市场难以自发达到“有效均衡”。&lt;/p&gt;&#xA;&lt;h1 id=&#34;市场无效性的典型表现与案例佐证&#34;&gt;市场无效性的典型表现与案例佐证&lt;/h1&gt;&#xA;&lt;p&gt;史莱佛通过多个经典金融市场案例，直观呈现了市场无效性的具体表现，其中最具代表性的包括：&lt;/p&gt;&#xA;&lt;p&gt;1.股市泡沫与崩盘：非理性情绪驱动的价格偏离&lt;/p&gt;&#xA;&lt;p&gt;书中详细分析了互联网泡沫（20世纪90年代末）、美国次贷危机前的房地产泡沫等案例。以互联网泡沫为例，当时大量缺乏盈利支撑、甚至没有明确商业模式的互联网企业，股价被疯狂炒作，市盈率远超合理水平，这并非基于企业内在价值的理性定价，而是源于“对新技术的盲目乐观”“从众投机”等非理性情绪的叠加。最终泡沫破裂，股价暴跌，证明市场价格并未反映资产的真实价值。&lt;/p&gt;&#xA;&lt;p&gt;2.小盘股效应与价值股效应：被忽视的定价偏差&lt;/p&gt;&#xA;&lt;p&gt;史莱佛通过长期数据追踪发现，小盘股（市值较小的公司股票）和价值股（市盈率低、市净率低的股票）在长期内的收益率显著高于大盘股和成长股，这一现象无法用有效市场假说解释——若市场有效，这种收益率差异应通过套利行为迅速消失。&lt;/p&gt;&#xA;&lt;p&gt;史莱佛认为，小盘股被忽视是因为机构投资者因资金规模限制不愿关注，价值股被低估则是因为投资者过度追捧成长股的“美好预期”，忽视了价值股的真实盈利能力。这种定价偏差的长期存在，直接证明了市场的无效性。&lt;/p&gt;&#xA;&lt;p&gt;3.金融衍生品市场的定价扭曲：复杂工具加剧市场无效&lt;/p&gt;&#xA;&lt;p&gt;在金融衍生品（如期权、CDO等）市场，史莱佛指出，由于产品结构复杂，投资者难以准确评估其内在价值，往往依赖简化的定价模型（如布莱克-斯科尔斯模型），而这些模型的前提假设（如无风险利率恒定、波动率稳定）与现实市场存在差异，导致衍生品定价出现严重偏差。次贷危机中，CDO等衍生品的错误定价就是引发危机的重要原因之一，凸显了复杂市场中无效性的放大效应。&lt;/p&gt;&#xA;&lt;h1 id=&#34;书籍带来的投资启示与实践建议&#34;&gt;书籍带来的投资启示与实践建议&lt;/h1&gt;&#xA;&lt;p&gt;基于对市场无效性的论证，史莱佛并非倡导“盲目投机”，而是为投资者提供了基于“市场无效”的投资逻辑，核心启示包括：&lt;/p&gt;&#xA;&lt;p&gt;1.主动投资策略具有合理性，精选价值是核心&lt;/p&gt;&#xA;&lt;p&gt;既然市场存在定价偏差，投资者就可以通过深入分析企业基本面（如盈利能力、资产负债表、行业竞争力），发现被低估的资产，通过长期持有获取超额收益。这与有效市场假说倡导的“被动指数投资”形成鲜明对比，为主动管理型基金的存在提供了理论依据。&lt;/p&gt;&#xA;&lt;p&gt;2.警惕非理性情绪，坚持逆向投资&lt;/p&gt;&#xA;&lt;p&gt;市场无效的核心驱动力之一是群体非理性，因此投资者应避免跟风从众，在市场过度乐观（泡沫期）保持冷静，在市场过度悲观（恐慌期）积极寻找被错杀的优质资产。逆向投资的本质，就是利用市场的非理性偏差获利。&lt;/p&gt;&#xA;&lt;p&gt;3.重视信息优势与研究能力，降低信息不对称风险&lt;/p&gt;&#xA;&lt;p&gt;对普通投资者而言，应通过提升专业能力、深入研究行业与企业，减少对“二手信息”的依赖，尽可能降低信息不对称带来的劣势；同时，要警惕“内幕信息”陷阱，坚守合规投资的底线。&lt;/p&gt;&#xA;&lt;p&gt;4.合理评估交易成本与流动性，避免盲目套利&lt;/p&gt;&#xA;&lt;p&gt;即使发现了价格偏差，也需评估交易成本、流动性等因素——若套利成本过高或无法及时变现，定价偏差可能长期存在，甚至导致套利失败。因此，投资决策需综合考虑“价值偏差”与“实现收益的可行性”。&lt;/p&gt;&#xA;&lt;p&gt;5.投资者形态模型简单分析与适配建议&lt;/p&gt;&#xA;&lt;p&gt;书籍虽未直接提出“投资者形态模型”，但结合其对市场参与者非理性行为的分析，可提炼出三类核心投资者形态及适配策略：一是“理性套利型”，这类投资者具备专业研究能力，能精准识别价格与价值的偏离，可通过前文提及的主动价值投资策略获利，但需规避交易成本过高、流动性不足的套利陷阱；二是“噪声交易型”，这类投资者易受情绪、短期信息影响，是市场无效性的主要推动者，需警惕自身的认知偏差，通过建立长期投资框架、减少频繁交易规避损失；三是“被动跟随型”，这类投资者缺乏专业分析能力，若盲目跟随市场情绪易成为损失承担者，可在理解市场无效性的基础上，选择兼顾主动筛选与风险分散的投资产品，而非单纯被动跟踪指数。简&#xA;言之，投资者需先明确自身形态特征，再结合市场无效性规律制定适配策略，才能降低非理性决策带来的风险。&lt;/p&gt;&#xA;&lt;h1 id=&#34;投资者心态模型分析&#34;&gt;投资者心态模型分析&lt;/h1&gt;&#xA;&lt;p&gt;结合《并非有效的金融市场》对市场参与者有限理性的核心论述，可提炼出三类与市场无效性紧密相关的投资者心态模型，这些心态是导致资产价格偏离内在价值的关键心理动因，也为理解投资决策偏差提供了核心视角：&lt;/p&gt;&#xA;&lt;p&gt;1.贪婪与恐惧驱动的情绪主导心态&lt;/p&gt;&#xA;&lt;p&gt;这是书中重点强调的非理性心态核心，也是市场泡沫与恐慌形成的主要推手。在贪婪心态主导下，投资者会过度关注短期收益，忽视资产基本面的真实价值，如互联网泡沫时期对新技术企业的盲目追捧、比特币等虚拟资产的投机热潮，均是贪婪心态引发的“追涨”行为；而在恐惧心态主导下，投资者会因市场短期波动、负面信息过度反应，出现“割肉出逃”的非理性抛售，导致资产价格被过度低估，例如次贷危机中优质企业股票随市场整体暴跌的现象。这种心态的本质是“短期情绪凌驾于长期价值判断”，打破了有效市场假说中“理性定价”的前提。&lt;/p&gt;&#xA;&lt;p&gt;2.过度自信的认知偏差心态&lt;/p&gt;&#xA;&lt;p&gt;书中明确指出，过度自信是多数投资者的共性心态，也是导致频繁交易、决策失误的重要原因。这类心态模型的核心特征是投资者高估自身的信息解读能力、择时能力和风险控制能力，认为自己能超越市场平均水平。例如，部分投资者仅凭碎片化信息就做出投资决策，忽视市场的复杂性和信息的不完整性；还有些投资者频繁调整持仓，认为能精准把握市场波动，最终却因交易成本增加、决策失误导致收益受损。过度自信心态会放大个体非理性行为，进而形成群体层面的定价偏差，加剧市场无效性。&lt;/p&gt;&#xA;&lt;p&gt;3.锚定与从众的惯性依赖心态&lt;/p&gt;&#xA;&lt;p&gt;锚定心态与从众心态常相互交织，共同影响投资者决策。锚定心态表现为投资者将某一初始信息（如历史最高价、近期平均价、权威机构的预测值）作为“锚点”，固化对资产价值的判断，忽视后续基本面的变化，例如投资者因某只股票历史最高价较高，就认为当前价格仍“有上涨空间”，无视企业盈利下滑的现实；从众心态则是投资者盲目跟随大众决策，认为“多数人的选择一定正确”，这种心态会形成“羊群效应”，导致市场价格向同一方向过度偏离，如某类主题基金的扎堆申购、某只股票的集中炒作，均是从众心态的体现。这&#xA;两种心态的核心是“放弃独立思考，依赖外部信号或群体共识”，进一步强化了市场的无效性。&lt;/p&gt;&#xA;&lt;p&gt;从实践意义来看，识别这些心态模型是规避投资失误的关键。书中隐含的应对逻辑是：投资者需建立“心态自检机制”，在决策前区分“情绪驱动”与“价值驱动”，通过长期投资框架、基本面分析工具对冲心态偏差，同时警惕锚定信息的局限性和群体决策的盲目性，避免成为加剧市场无效性的“噪声交易者”。&lt;/p&gt;&#xA;&lt;p&gt;4.投资者心态模型的转换路径与实践要点&lt;/p&gt;&#xA;&lt;p&gt;基于书中对投资者非理性行为的批判，心态模型并非固定不变，从非理性心态向理性心态的转换，是降低投资决策偏差、利用市场无效性获利的核心前提。具体转换路径可分为三个核心阶段：&lt;/p&gt;&#xA;&lt;p&gt;第一阶段：心态觉醒与偏差识别。这是转换的基础，核心是投资者主动意识到自身的心态局限。可通过“决策复盘”实现：每次投资决策后，记录决策时的核心依据、情绪状态（如是否受市场热点、他人建议影响），对照前文三类非理性心态模型，标注是否存在情绪主导、过度自信或锚定从众的倾向。例如，若某笔投资是因“看到身边人都在买”而跟风，即可明确识别为“从众心态”，为后续修正提供靶向。&lt;/p&gt;&#xA;&lt;p&gt;第二阶段：工具对冲与行为约束。针对识别出的心态偏差，借助具体工具建立行为边界，避免非理性情绪主导决策。对应不同心态的对冲工具的：针对“情绪主导心态”，可建立“止损/止盈规则”和“冷静期机制”，如遇到市场剧烈波动时，强制暂停交易24小时再决策；针对“过度自信心态”，可采用“组合投资策略”，通过分散投资降低对自身“择时/选股能力”的过度依赖，同时限制单只资产的持仓比例；针对“锚定从众心态”，可建立“基本面分析清单”，将资产的盈利能力、估值水平等核心指标作为决策核心，而非依赖历史价格、群体共识等锚点信息。&lt;/p&gt;&#xA;&lt;p&gt;第三阶段：长期强化与习惯养成。心态转换的关键是形成稳定的理性决策习惯，而非单次修正。可&#xA;通过“长期投资目标锚定”实现：明确自身的投资周期（如3年、5年）和核心目标（如资产保值、子女教育储备），将短期市场波动与长期目标脱钩，减少因短期情绪波动导致的非理性操作。同时，定期更新对市场无效性的认知，结合书中的案例与实证研究，强化“理性定价优于情绪定价”的认知，逐步弱化非理性心态的影响。&lt;/p&gt;&#xA;&lt;p&gt;需要注意的是，心态模型转换并非“完全摒弃情绪”，而是实现“情绪可控、理性主导”的平衡。书&#xA;中强调，现实中的投资者难以达到绝对理性，但通过持续的觉醒、对冲与强化，可无限接近理性心态，进而在市场无效性中找到精准的投资机会。&lt;/p&gt;&#xA;&lt;h1 id=&#34;个人感悟与思考&#34;&gt;个人感悟与思考&lt;/h1&gt;&#xA;&lt;p&gt;读完《并非有效的市场》，更坚定我对“市场并非完全有效”的认知，理解了金融市场的本质是“理性与非理性的交织体”。有&#xA;效市场假说作为简化的理论模型，为我们理解市场提供了基准，但现实市场的复杂性远超出理论范畴，这也正是金融市场的魅力所在。&lt;/p&gt;&#xA;&lt;p&gt;对个人投资者而言，这本书的启示在于：不要盲目相信“市场永远正确”，也不要过度自信于自己的“择时能力”。真&#xA;正有效的投资，应该是“基于基本面的价值判断”与“对市场情绪的理性规避”相结合——既通过深入研究发现价值，又通过逆向思维避开泡沫。同时，这本书也提醒我们，金融市场的稳定需要完善的制度设计（如信息披露制度、退市机制、做空机制）来降低无效性，保护普通投资者的利益。&lt;/p&gt;&#xA;&lt;p&gt;此外，书中的观点也并非绝对，市场无效与相对有效之间可能存在动态平衡：在某些成熟市场、某些时段，市场可能接近有效；而在新兴市场、市场剧烈波动期，无效性则更为显著。因此，投资者需要根据具体市场环境灵活调整投资策略，而非机械套用某一理论。&lt;/p&gt;&#xA;&lt;h1 id=&#34;总结&#34;&gt;总结&lt;/h1&gt;&#xA;&lt;p&gt;《并非有效的市场》通过对有效市场假说的系统性批判，揭示了金融市场的无效性本质，其核心观点在于：市场参与者的有限理性、信息不对称、交易成本与制度缺陷，共同导致资产价格偏离内在价值，市场无法自发达到有效均衡。&lt;/p&gt;</description>
    </item>
    <item>
      <title>投资笔记 1 ：先立认知，再选策略</title>
      <link>http://gewutang.cn/2025/11/14/notes-on-investments/</link>
      <pubDate>Fri, 14 Nov 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/11/14/notes-on-investments/</guid>
      <description>&lt;p&gt;投资成功需以三大核心为前提：系统的投资知识储备、知名机构与大师的实践参考、贴合自身的个性化策略。脱离这些前提，即便偶遇短期行情，也可能因方向偏差、风险误判导致亏损。&lt;/p&gt;&#xA;&lt;p&gt;本 “投资必读” 系列共 10&#xA;篇文章，后续将系统拆解各类策略的逻辑框架、实操方法与适用范围，结合领域内知名机构与投资大师的实践案例提供参考。本篇作为开篇，核心是建立&#xA;“先学知识、再判机会、后选策略”&#xA;的底层认知，厘清被动与主动策略的本质差异与适配逻辑。&lt;/p&gt;&#xA;&lt;div id=&#34;一投资的底层共识&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;一、投资的底层共识&lt;/h2&gt;&#xA;&lt;p&gt;投资策略选择的优先级远高于具体产品筛选。合理的策略能在控制风险的同时最大化收益；若策略逻辑偏差，即便选择优质标的，也可能因市场适配错误或偏离收益目标导致亏损。&lt;/p&gt;&#xA;&lt;p&gt;盲目投资本质是 “无准备入场”，必然面临三重风险：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;市场类型错配：将弱有效市场（如 A&#xA;股创业板）的主动策略逻辑，套用到强有效市场（如美股标普&#xA;500），用高费率主动基金博弈市场平均收益，最终&#xA;“费力不讨好”；&lt;/li&gt;&#xA;&lt;li&gt;操作逻辑混淆：误将被动跟踪的长期逻辑用于短期波段操作，如用沪深&#xA;300ETF&#xA;频繁追涨杀跌，既增加交易成本，又错失指数长期复利；&lt;/li&gt;&#xA;&lt;li&gt;资金需求偏离：中短期低风险需求（如 3&#xA;年后子女教育资金）却配置高波动主动权益基金，市场回撤时被迫割肉离场。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;因此，投资的第一步是&#xA;“学知识、定目标”，明确收益目标、风险容忍度、资金周期，再选择适配的投资策略。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;二策略核心分类被动与主动的本质差异-依据&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;二、策略核心分类：被动与主动的本质差异 依据&lt;/h2&gt;&#xA;&lt;p&gt;“是否主动追求超额收益（Alpha&#xA;收益）”，投资策略可划分为被动管理与主动管理两大体系，适配不同市场环境与投资需求。&lt;/p&gt;&#xA;&lt;p&gt;1.被动管理策略：赚市场平均的确定性收益&lt;/p&gt;&#xA;&lt;p&gt;被动管理策略的本质是&#xA;“以指数为基准，通过复制指数成分股构成与权重，实现与基准收益（Beta&#xA;收益）的高度重合”，核心目标是获取市场平均收益。&lt;/p&gt;&#xA;&lt;p&gt;其理论基础是&#xA;“市场有效性假说（EMH）”：在信息充分反映的强有效市场中，主动决策难以持续创造超额收益，被动策略成为最优选择。先锋集团创始人约翰・博格尔提出的&#xA;“低成本、长期持有”，是被动策略的核心原则。&lt;/p&gt;&#xA;&lt;p&gt;主流产品类型与操作要点&#xA;* 纯被动指数基金 /&#xA;ETF：完全复制指数成分股与权重，无主动调整，跟踪误差通常低于&#xA;0.5%。优先选择低费率（管理费率低于&#xA;0.15%）、高流动性（日均成交额超 1&#xA;亿元）的产品，例如华夏沪深&#xA;300ETF（510330）、易方达标普 500ETF（513500）。&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;指数增强基金：在被动跟踪基础上小幅调整，通过量化模型或基本面分析对成分股进行不超过&#xA;5% 的超配 / 低配，控制跟踪误差（通常低于&#xA;2%）并追求少量超额收益。选择时重点关注连续 3&#xA;年跑赢基准的稳定性，例如富国沪深 300&#xA;增强（100038）。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;被动型&#xA;FOF：通过配置多只被动基金实现跨资产、跨市场分散，降低单一指数波动风险。关注资产配置比例与自身风险偏好的契合度，以及总费率（通常不超过&#xA;1%），例如南方智诚 FOF（008988）。 适配投资者画像&#xA;投资周期超 3&#xA;年、风险偏好中低（年化波动率容忍度低于&#xA;10%）、无专业研究能力的个人投资者，核心需求是&#xA;“稳定获取长期收益”。&lt;/p&gt;</description>
    </item>
    <item>
      <title>A 股最近的表现怎么样</title>
      <link>http://gewutang.cn/2025/08/23/performance-of-ashare/</link>
      <pubDate>Sat, 23 Aug 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/08/23/performance-of-ashare/</guid>
      <description>&lt;p&gt;本轮A股行情从2024年9月份启动到现在，上证指数已累计上涨了9.4%，深成指累计上涨12.9%，创业板指数累计上涨了33.7%。可谓汹涌上涨、波澜壮阔。&lt;/p&gt;&#xA;&lt;p&gt;抓住了这波行情的投资者不用说自然是喜不胜收，那些错失前期行情的投资者，也开始抵挡不住赚钱的诱惑准备上车了。不过，千万不要太过性急！&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 安装必要包（若未安装）&#xA;# install.packages(c(&amp;quot;quantmod&amp;quot;, &amp;quot;ggplot2&amp;quot;, &amp;quot;TTR&amp;quot;, &amp;quot;scales&amp;quot;, &amp;quot;gridExtra&amp;quot;, &amp;quot;cowplot&amp;quot;, &amp;quot;showtext&amp;quot;))&#xA;&#xA;library(quantmod)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggplot2)&#xA;library(TTR)&#xA;library(scales)&#xA;library(gridExtra)&#xA;library(cowplot)&#xA;library(showtext)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: sysfonts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: showtextdb&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 设置中文字体&#xA;font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;SimHei.ttf&amp;quot;)  # 请确保系统中有SimHei字体，或指定正确路径&#xA;showtext_auto()&#xA;&#xA;# 设置指数代码和名称&#xA;indices &amp;lt;- list(&#xA;  &amp;quot;上证指数&amp;quot; = &amp;quot;000001.SS&amp;quot;,&#xA;  &amp;quot;沪深300&amp;quot; = &amp;quot;000300.SS&amp;quot;,&#xA;  &amp;quot;深证成指&amp;quot; = &amp;quot;399001.SZ&amp;quot;&#xA;)&#xA;&#xA;# 获取数据（最近365天）&#xA;get_stock_data &amp;lt;- function(symbol) {&#xA;  data &amp;lt;- tryCatch({&#xA;    getSymbols(symbol, src = &amp;quot;yahoo&amp;quot;, from = Sys.Date() - 365, auto.assign = FALSE)&#xA;  }, error = function(e) {&#xA;    message(paste(&amp;quot;获取数据失败:&amp;quot;, symbol, &amp;quot;-&amp;quot;, e$message))&#xA;    return(NULL)&#xA;  })&#xA;  return(data)&#xA;}&#xA;&#xA;# 计算技术指标并准备数据&#xA;prepare_data &amp;lt;- function(data, index_name) {&#xA;  if (is.null(data) || nrow(data) &amp;lt; 30) {&#xA;    message(paste(&amp;quot;数据不足或为空:&amp;quot;, index_name))&#xA;    return(NULL)&#xA;  }&#xA;  &#xA;  df &amp;lt;- data.frame(Date = index(data), coredata(data))&#xA;  colnames(df) &amp;lt;- c(&amp;quot;Date&amp;quot;, &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;)&#xA;  &#xA;  # 计算收益率&#xA;  df$Return &amp;lt;- c(NA, diff(log(df$Close)))&#xA;  &#xA;  # 计算移动平均线&#xA;  df$SMA5 &amp;lt;- SMA(df$Close, n = 5)&#xA;  df$SMA20 &amp;lt;- SMA(df$Close, n = 20)&#xA;  df$SMA60 &amp;lt;- SMA(df$Close, n = 60)&#xA;  &#xA;  # 确保有足够的数据计算指标&#xA;  if (nrow(df) &amp;gt; 26) {&#xA;    # 计算MACD&#xA;    macd_data &amp;lt;- tryCatch({&#xA;      MACD(df$Close, nFast = 12, nSlow = 26, nSig = 9)&#xA;    }, error = function(e) {&#xA;      message(paste(&amp;quot;计算MACD失败:&amp;quot;, index_name, &amp;quot;-&amp;quot;, e$message))&#xA;      return(NULL)&#xA;    })&#xA;    &#xA;    if (!is.null(macd_data)) {&#xA;      df$MACD &amp;lt;- macd_data[, &amp;quot;macd&amp;quot;]&#xA;      df$Signal &amp;lt;- macd_data[, &amp;quot;signal&amp;quot;]&#xA;      df$Histogram &amp;lt;- df$MACD - df$Signal&#xA;    }&#xA;  }&#xA;  &#xA;  if (nrow(df) &amp;gt; 14) {&#xA;    # 计算KDJ指标&#xA;    kdj &amp;lt;- tryCatch({&#xA;      stoch(df[, c(&amp;quot;High&amp;quot;, &amp;quot;Low&amp;quot;, &amp;quot;Close&amp;quot;)], nFastK = 14, nFastD = 3, nSlowD = 3)&#xA;    }, error = function(e) {&#xA;      message(paste(&amp;quot;计算KDJ失败:&amp;quot;, index_name, &amp;quot;-&amp;quot;, e$message))&#xA;      return(NULL)&#xA;    })&#xA;    &#xA;    if (!is.null(kdj)) {&#xA;      df$K &amp;lt;- kdj[, &amp;quot;fastK&amp;quot;] * 100  # 转换为百分比&#xA;      df$D &amp;lt;- kdj[, &amp;quot;fastD&amp;quot;] * 100  # 转换为百分比&#xA;      df$J &amp;lt;- 3 * df$K - 2 * df$D   # J线计算&#xA;    }&#xA;    &#xA;    # 计算RSI&#xA;    df$RSI &amp;lt;- tryCatch({&#xA;      RSI(df$Close, n = 14)&#xA;    }, error = function(e) {&#xA;      message(paste(&amp;quot;计算RSI失败:&amp;quot;, index_name, &amp;quot;-&amp;quot;, e$message))&#xA;      return(rep(NA, nrow(df)))&#xA;    })&#xA;  }&#xA;  &#xA;  df$Index &amp;lt;- index_name&#xA;  return(df)&#xA;}&#xA;&#xA;# 获取所有指数数据&#xA;all_data &amp;lt;- list()&#xA;for (index_name in names(indices)) {&#xA;  symbol &amp;lt;- indices[[index_name]]&#xA;  data &amp;lt;- get_stock_data(symbol)&#xA;  if (!is.null(data)) {&#xA;    prepared_data &amp;lt;- prepare_data(data, index_name)&#xA;    if (!is.null(prepared_data)) {&#xA;      all_data[[index_name]] &amp;lt;- prepared_data&#xA;    }&#xA;  }&#xA;}&#xA;&#xA;# 如果没有获取到任何数据，则停止执行&#xA;if (length(all_data) == 0) {&#xA;  stop(&amp;quot;未能获取到任何指数数据，请检查网络连接和代码设置&amp;quot;)&#xA;}&#xA;&#xA;# 合并所有数据&#xA;combined_data &amp;lt;- do.call(rbind, all_data)&#xA;&#xA;# 1. 单独绘制价格走势图&#xA;price_plot &amp;lt;- ggplot(combined_data, aes(x = Date, y = Close, color = Index)) +&#xA;  geom_line(size = 0.8) +&#xA;  theme_bw() +&#xA;  theme(&#xA;    axis.title.x = element_blank(),&#xA;    axis.title.y = element_blank(),&#xA;    legend.position = &amp;quot;bottom&amp;quot;,&#xA;    text = element_text(family = &amp;quot;SimHei&amp;quot;)&#xA;  ) +&#xA;  scale_color_brewer(palette = &amp;quot;Set1&amp;quot;) +&#xA;  scale_y_continuous(labels = comma) +&#xA;  # 为深证成指添加右侧Y轴&#xA;  scale_y_continuous(&#xA;    sec.axis = sec_axis(&#xA;      ~./max(combined_data$Close[combined_data$Index == &amp;quot;深证成指&amp;quot;], na.rm = TRUE) * &#xA;        max(combined_data$Close[combined_data$Index != &amp;quot;深证成指&amp;quot;], na.rm = TRUE),&#xA;      name = &amp;quot;深证成指&amp;quot;&#xA;    )&#xA;  )&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.&#xA;## ℹ Please use `linewidth` instead.&#xA;## This warning is displayed once every 8 hours.&#xA;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#xA;## generated.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Scale for y is already present.&#xA;## Adding another scale for y, which will replace the existing scale.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;print(price_plot)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/08/23/performance-of-ashare/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>使用卡尔曼滤波计算个股的动态贝塔</title>
      <link>http://gewutang.cn/2025/08/23/kalman-beta/</link>
      <pubDate>Sat, 23 Aug 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/08/23/kalman-beta/</guid>
      <description>&lt;div id=&#34;引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;引言&lt;/h1&gt;&#xA;&lt;p&gt;在金融领域，贝塔（β）系数是资本资产定价模型（CAPM）中的核心参数，用于衡量单个资产或证券组合相对于整个市场（通常以市场指数代表）的系统性风险或波动性。具体而言，贝塔反映了市场收益率变动一个单位时，个体资产收益率的预期变动幅度。准确估计贝塔对于投资组合管理、风险控制、资产定价以及绩效评估都具有重要意义。例如，投资者可通过贝值判断资产的风险属性，进行合理的资产配置；基金经理也可依据贝塔调整投资组合的市场暴露程度。&lt;/p&gt;&#xA;&lt;p&gt;传统的贝塔估计方法主要依赖于最小二乘（OLS）回归。通过在给定时间窗口（如1年、3年或5年）内对资产收益率与市场收益率进行线性回归，所得斜率系数即为贝塔估计值。此外，为降低短期波动带来的误差，也常采用滚动窗口回归或调整贝塔（Adjusted Beta）等方法。&lt;/p&gt;&#xA;&lt;p&gt;然而，这些传统方法存在明显不足。首先，它们隐含假设贝塔在估计期间内是固定不变的，但事实上，受公司资本结构、经营策略、行业竞争及宏观经济环境等因素影响，资产的系统性风险可能随时间变化，即贝塔具有时变性。其次，滚动窗口回归中窗口长度的选择缺乏统一标准，较短窗口对噪声敏感，较长窗口则可能无法及时捕捉最新变化。再者，这些方法无法提供实时更新的贝塔估计，难以满足动态风险管理的需求。&lt;/p&gt;&#xA;&lt;p&gt;为克服上述不足，本研究引入卡尔曼滤波（Kalman Filter）方法进行时变贝塔的估计。卡尔曼滤波是一种递归状态估计算法，能够从带有噪声的观测数据中最优地估计动态系统的内部状态。其核心优势在于能够实时更新状态估计，并通过建模状态变量的动态演化过程（如随机游走）来捕捉参数的时变特性。将卡尔曼滤波应用于贝塔估计，不仅可以得到随时间平滑变化的贝塔序列，还能通过状态空间模型灵活描述贝塔的动态行为，从而提供更准确、适应性的风险度量。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;卡尔曼滤波原理及其在计算贝塔中的应用&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;卡尔曼滤波原理及其在计算贝塔中的应用&lt;/h1&gt;&#xA;&lt;p&gt;卡尔曼滤波是一种最优递归数据处理算法，用于从一系列存在噪声的观测数据中估计动态系统的内部状态。其基本思想是通过结合系统模型的预测（先验估计）和当前观测值的更新，递归地得到状态的最优估计（后验估计）。该算法主要包含两个步骤：预测（时间更新）和更新（测量更新）。&lt;/p&gt;&#xA;&lt;div id=&#34;基本原理&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;基本原理&lt;/h2&gt;&#xA;&lt;ol style=&#34;list-style-type: decimal&#34;&gt;&#xA;&lt;li&gt;&lt;strong&gt;状态空间模型&lt;/strong&gt;&lt;br /&gt;&#xA;卡尔曼滤波建立在状态空间模型之上，包括状态方程和观测方程：&#xA;&lt;ul&gt;&#xA;&lt;li&gt;状态方程（系统模型）：描述状态向量如何随时间演化&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[x_t = F_t x_{t-1} + B_t u_t + w_t\]&lt;/span&gt;&lt;br /&gt;&#xA;其中，&lt;span class=&#34;math inline&#34;&gt;\(x_t\)&lt;/span&gt;为&lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;时刻的状态向量，&lt;span class=&#34;math inline&#34;&gt;\(F_t\)&lt;/span&gt;为状态转移矩阵，&lt;span class=&#34;math inline&#34;&gt;\(u_t\)&lt;/span&gt;为控制输入向量（可选），&lt;span class=&#34;math inline&#34;&gt;\(B_t\)&lt;/span&gt;为控制输入矩阵，&lt;span class=&#34;math inline&#34;&gt;\(w_t \sim N(0, Q_t)\)&lt;/span&gt;为过程噪声。&lt;br /&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;观测方程（测量模型）：描述观测值与状态向量的关系&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[z_t = H_t x_t + v_t\]&lt;/span&gt;&lt;br /&gt;&#xA;其中，&lt;span class=&#34;math inline&#34;&gt;\(z_t\)&lt;/span&gt;为&lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;时刻的观测向量，&lt;span class=&#34;math inline&#34;&gt;\(H_t\)&lt;/span&gt;为观测矩阵，&lt;span class=&#34;math inline&#34;&gt;\(v_t \sim N(0, R_t)\)&lt;/span&gt;为观测噪声。&lt;/li&gt;&#xA;&lt;/ul&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;算法步骤&lt;/strong&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;预测步骤&lt;/strong&gt;：&lt;br /&gt;&#xA;先验状态估计：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[\hat{x}_t^{-} = F_t \hat{x}_{t-1} + B_t u_t\]&lt;/span&gt;&lt;br /&gt;&#xA;先验误差协方差：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[P_t^{-} = F_t P_{t-1} F_t^T + Q_t\]&lt;/span&gt;&lt;br /&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;更新步骤&lt;/strong&gt;：&lt;br /&gt;&#xA;卡尔曼增益：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[K_t = P_t^{-} H_t^T (H_t P_t^{-} H_t^T + R_t)^{-1}\]&lt;/span&gt;&lt;br /&gt;&#xA;后验状态估计：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[\hat{x}_t = \hat{x}_t^{-} + K_t (z_t - H_t \hat{x}_t^{-})\]&lt;/span&gt;&lt;br /&gt;&#xA;后验误差协方差：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[P_t = (I - K_t H_t) P_t^{-}\]&lt;/span&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;在计算贝塔时的应用&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;在计算贝塔时的应用&lt;/h2&gt;&#xA;&lt;p&gt;在时变贝塔估计中，我们将CAPM模型置于状态空间框架下。假设在时刻&lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;，资产超额收益&lt;span class=&#34;math inline&#34;&gt;\(r_{a,t}\)&lt;/span&gt;与市场超额收益&lt;span class=&#34;math inline&#34;&gt;\(r_{m,t}\)&lt;/span&gt;满足：&lt;br /&gt;&#xA;&lt;span class=&#34;math display&#34;&gt;\[r_{a,t} = \alpha_t + \beta_t r_{m,t} + \epsilon_t\]&lt;/span&gt;&lt;br /&gt;&#xA;其中，&lt;span class=&#34;math inline&#34;&gt;\(\alpha_t\)&lt;/span&gt;和&lt;span class=&#34;math inline&#34;&gt;\(\beta_t\)&lt;/span&gt;为时变参数，&lt;span class=&#34;math inline&#34;&gt;\(\epsilon_t\)&lt;/span&gt;为观测噪声。&lt;/p&gt;</description>
    </item>
    <item>
      <title>如何根据事件研究来推测市场上涨幅度</title>
      <link>http://gewutang.cn/2025/07/26/casestudy-etf-up/</link>
      <pubDate>Sat, 26 Jul 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/07/26/casestudy-etf-up/</guid>
      <description>&lt;p&gt;先获取稀土ETF（代码为516780）的历史行情数据：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(quantmod)&#xA;XT &amp;lt;- getSymbols(&amp;quot;516780.ss&amp;quot;, src = &amp;quot;yahoo&amp;quot;, auto.assign = FALSE)&#xA;# 获取子集数据&#xA;XT &amp;lt;- round(XT[&amp;quot;2025::&amp;quot;],2)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;绘制蜡烛图：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;chartSeries(XT, theme = &amp;quot;white&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/26/casestudy-etf-up/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;下面标记重大事件。包括：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;2025年3月，份政府工作报告中提及要“整治行业内卷式竞争”。&lt;/li&gt;&#xA;&lt;li&gt;2025年6月17日，全国人大公布修订后的《反不正当竞争法》。&lt;/li&gt;&#xA;&lt;li&gt;2025年7月1日，中央财经委员会第七次会议公布要治理企业低价无序竞争。&lt;/li&gt;&#xA;&lt;li&gt;2025年7月9日，工信部召开企业公平竞争座谈会。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;在图上标记重大事件：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义事件日期&#xA;event_dates &amp;lt;- as.Date(c(&amp;quot;2025-03-07&amp;quot;, &#xA;                         &amp;quot;2025-06-27&amp;quot;,&#xA;                         &amp;quot;2025-07-01&amp;quot;,&#xA;                         &amp;quot;2025-07-09&amp;quot;)&#xA;                       )&#xA;&#xA;# 计算事件日期在子集中的相对索引&#xA;event_index &amp;lt;- numeric(length(event_dates))&#xA;for (i in seq_along(event_dates)) {&#xA;  event_index[i] &amp;lt;- which(index(XT) == event_dates[i])&#xA;}&#xA;&#xA;# 添加标记点&#xA;addPoints(&#xA;  x = event_index,&#xA;  y = Ad(XT[event_dates]),&#xA;  col = &amp;quot;blue&amp;quot;,&#xA;  pch = 24&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/26/casestudy-etf-up/index_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(qmao)&#xA;addVLine(index(XT[c(&amp;quot;2025-03-05::2025-03-12&amp;quot;,&#xA;                    &amp;quot;2025-06-17::2025-06-30&amp;quot;,&#xA;                    &amp;quot;2025-07-01::2025-07-25&amp;quot;)&#xA;                  ]),&#xA;         col = &amp;quot;lightblue&amp;quot;,&#xA;         border = &amp;quot;lightblue&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/26/casestudy-etf-up/index_files/figure-html/unnamed-chunk-3-2.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>大同</title>
      <link>http://gewutang.cn/2025/07/15/datong/</link>
      <pubDate>Tue, 15 Jul 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/07/15/datong/</guid>
      <description>&lt;p&gt;决定去山西一趟。虽然地图上山西和北京近在咫尺，不知为啥脑海中总觉得千山万水似的。&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/15/datong/index_files/figure-html/shanxi-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;这次要去的目的地是大同市。拜《黑神话·悟空》所赐，大同火了。查了查大同的景点还是很多的。&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/15/datong/index_files/figure-html/datong-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;最主要的景点是&lt;strong&gt;云冈石窟&lt;/strong&gt;，在大同市云冈区，当然不能只去&lt;strong&gt;云冈石窟&lt;/strong&gt;，把周围的景点也一并视察了。&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;云冈石窟&lt;/strong&gt;：是云冈区最著名的景点，距今已有 1500 年历史，是佛教艺术东传中国后，第一次由一个民族用一个朝代雕琢而成皇家风范的佛教艺术宝库，也是公元 5 世纪中西文化融合的历史丰碑。1&#xA;961 年被列为首批全国重点文物保护单位，2001 年列入 “世界文化遗产” 名录，2007 年成为国家首批 5A 级旅游景区。石&#xA;窟内佛像雕刻精美，如第 5 窟的主尊坐佛是云冈的第一大佛，第 6 窟是云冈石窟中规模最大、设计最完整的洞窟。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;晋华宫国家矿山公园&lt;/strong&gt;：我国首批国家矿山公园之一，也是 “中国工业旅游示范点”“全国科普教育基地” 和国家 4A 级旅游景区。公&#xA;园与云冈石窟隔河相望，以 “煤都井下探秘游” 项目为依托，游客可在此体验煤矿开采的历史文化，观赏罕见的侏罗纪煤层地质奇观。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;焦山寺石窟&lt;/strong&gt;：位于高山镇高山村北约 1000 米处，依山而建，前临十里河。石&#xA;窟开凿于北魏年间，辽金时期在窟前增建木构窟檐，明嘉靖年间有所修复。寺&#xA;址坐北朝南，依山势辟五层平地，窟龛分布在崖面上，窟内有壁画和泥塑佛像，第五层建有六角三层楼阁砖塔。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;高山古城&lt;/strong&gt;：位于高山镇高山村，与云冈石窟同处云冈域峪，在云冈石窟以西 16 公里。古&#xA;城是明清时期九边大同重镇十三卫所之一，占地约 1200 亩，是大同市重要的古城遗址。这&#xA;里有 400 余米的明城墙、瓮城和怀德桥等，见证了曾经的辉煌。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;魏都水世界&lt;/strong&gt;：坐落于西韩岭乡马辛庄村北，总占地面积 34 万平方米。景&#xA;区集亲水娱乐、药浴理疗、主题游乐、特色美食等功能于一体，是山西省乃至华北地区规模最大、项目设施最完备的室内水上乐园，也是华北地区最全面的生态旅游休闲综合体。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;大同煤矿 “万人坑” 纪念馆&lt;/strong&gt;：是全国爱国主义教育基地、全国重点文物保护单位，座落在煤峪口南沟。纪&#xA;念馆占地 33.7 万平方米，分为苦难展示区、文物保护区、煤炭历史展览区等部分，展现了侵华日军的残暴罪行。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;strong&gt;禅房寺砖塔&lt;/strong&gt;：是云冈区的国家级重点文物保护单位之一。该&#xA;塔具有较高的历史文化价值，其建筑风格独特，体现了当时的建筑工艺和艺术水平，吸引着众多专家学者和游客前来参观。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/07/15/datong/index_files/figure-html/yungang-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;古城区也值得一看。景点众多。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;景点&lt;/strong&gt;：&lt;/p&gt;</description>
    </item>
    <item>
      <title>使用机器学习模型(xgboost）来预测纳斯达克 100 指数</title>
      <link>http://gewutang.cn/2025/07/14/xboost/</link>
      <pubDate>Mon, 14 Jul 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/07/14/xboost/</guid>
      <description>&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/core-js/shim.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/react/react.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/react/react-dom.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/reactwidget/react-tools.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/htmltools-fill/fill.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/reactable/reactable.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2025/07/14/xboost/index_files/reactable-binding/reactable.js&#34;&gt;&lt;/script&gt;&#xA;&#xA;&#xA;&lt;div id=&#34;引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;引言&lt;/h1&gt;&#xA;&lt;p&gt;XGBoost（eXtreme Gradient Boosting）是一种高效的梯度提升算法，在数据科学和机器学习领域广泛应用，尤其擅长处理结构化数据的回归和分类问题。以下从数学原理、发展历程、使用方法和竞争算法四个方面进行详细介绍：&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;一数学原理&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;一、数学原理&lt;/h1&gt;&#xA;&lt;p&gt;XGBoost 是梯度提升框架（Gradient Boosting）的优化实现，其核心思想是迭代地训练多个弱学习器（通常是决策树），并将它们组合成一个强学习器。&lt;/p&gt;&#xA;&lt;p&gt;具体数学原理如下：&lt;/p&gt;&#xA;&lt;p&gt;1.目标函数&lt;/p&gt;&#xA;&lt;p&gt;XGBoost 的目标函数由两部分组成：&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;损失函数&lt;/strong&gt;：衡量模型预测值与真实值之间的误差（如均方误差 MSE、对数损失 log loss 等）。&#xA;&lt;strong&gt;正则化项&lt;/strong&gt;：控制模型复杂度，防止过拟合，包括树的数量、深度、叶子节点数等。&lt;/p&gt;&#xA;&lt;p&gt;目标函数表达式：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;\text{Obj}(\theta) = \sum_{i=1}^n L(y_i, \hat{y}_i) + \sum_{k=1}^K \Omega(f_k)&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(L(y_i, \hat{y}_i)\)&lt;/span&gt; 是样本 &lt;span class=&#34;math inline&#34;&gt;\(i\)&lt;/span&gt; 的损失函数，&lt;span class=&#34;math inline&#34;&gt;\(\hat{y}_i\)&lt;/span&gt; 是预测值，&lt;span class=&#34;math inline&#34;&gt;\(y_i\)&lt;/span&gt; 是真实值。&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\Omega(f_k)\)&lt;/span&gt; 是第 &lt;span class=&#34;math inline&#34;&gt;\(k\)&lt;/span&gt; 棵树的正则化项，通常包括树的复杂度（如叶子节点数、叶子节点权重的 L2 正则化）。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;2.梯度提升迭代&lt;/p&gt;&#xA;&lt;p&gt;XGBoost 通过迭代方式逐步优化目标函数：&lt;/p&gt;&#xA;&lt;p&gt;初始化模型为常数预测： &lt;span class=&#34;math inline&#34;&gt;\(\hat{y}_i^{(0)} = \text{argmin}_c \sum_{i=1}^n L(y_i, c)\)&lt;/span&gt; 。&lt;/p&gt;&#xA;&lt;p&gt;对于每一轮迭代 &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; ：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;计算损失函数关于当前预测值的梯度（一阶导数）和海森矩阵（二阶导数）。&lt;/li&gt;&#xA;&lt;li&gt;训练一棵新的决策树 &lt;span class=&#34;math inline&#34;&gt;\(f_t\)&lt;/span&gt; ，拟合这些梯度（即预测梯度方向）。&lt;/li&gt;&#xA;&lt;li&gt;更新预测值：&lt;span class=&#34;math inline&#34;&gt;\(\hat{y}_i^{(t)} = \hat{y}_i^{(t-1)} + \eta f_t(x_i)\)&lt;/span&gt; ，其中 &lt;span class=&#34;math inline&#34;&gt;\(\eta\)&lt;/span&gt; 是学习率（控制每次迭代的步长）。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;3.决策树优化&lt;/p&gt;</description>
    </item>
    <item>
      <title>《人的全景》读书笔记：洞察行为逻辑，突破认知牢笼</title>
      <link>http://gewutang.cn/2025/07/08/the-panorama-of-human/</link>
      <pubDate>Tue, 08 Jul 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/07/08/the-panorama-of-human/</guid>
      <description>&lt;p&gt;池宇峰的 &lt;strong&gt;《人的全景：弹簧人、思维体操与进步》&lt;/strong&gt; 并非一本传统的心理学或管理学著作。它是一位成功的企业家、连续创业者（洪恩教育、完美世界等创始人）基于多年实践观察、深度思考，融合心理学、行为学、神经科学等知识，构建的一套解释人类思维与行为规律的原创性框架。&lt;/p&gt;&#xA;&lt;p&gt;本书的精华之处在于提供一个简洁而有力的模型——“&lt;strong&gt;全因模型&lt;/strong&gt;”——用以拆解我们绝大多数行为背后的底层逻辑，并针对阻碍个人（尤其是职场）进步的常见思维陷阱，提供系统性的诊断工具（&lt;strong&gt;思维错误树&lt;/strong&gt;）和实战解决方案（&lt;strong&gt;7层阶梯与26个思维体操&lt;/strong&gt;）。&lt;/p&gt;&#xA;&lt;h2 id=&#34;一-全因模型解码行为动机的万能钥匙&#34;&gt;一、 全因模型：解码行为动机的万能钥匙&lt;/h2&gt;&#xA;&lt;p&gt;池宇峰开篇即提出核心命题：人的一切思想和行为，其根本动机皆可归结为一点——&lt;strong&gt;维持内在的“平衡态”&lt;/strong&gt; 。这不仅是生理的（如体温、血糖稳定），更是心理的（安全感、归属感、尊重、自我实现的需求满足）。&lt;/p&gt;&#xA;&lt;p&gt;全因模型由&lt;strong&gt;五大模块&lt;/strong&gt;构成一个动态循环系统：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;平衡态：&lt;/strong&gt; 人生存与感知良好的基准状态。这是所有行为的起点与终极目标。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;失衡源：&lt;/strong&gt; 打破平衡态的力量。分为四类：&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;内部生理失衡：&lt;/strong&gt; 如饥饿（代谢需求）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;内部心理失衡：&lt;/strong&gt; 未被满足的&lt;strong&gt;欲望&lt;/strong&gt;（储存于“压力欲望库”）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;外部生理失衡：&lt;/strong&gt; 环境刺激（如强光、噪音）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;外部心理失衡：&lt;/strong&gt; 源于&lt;strong&gt;比较器&lt;/strong&gt;——模型中最关键的心理失衡触发器。无论是&lt;strong&gt;自我比较&lt;/strong&gt;（纵向，与过去的自己比）、&lt;strong&gt;社会比较&lt;/strong&gt;（横向，与他人比财富、地位、能力）还是&lt;strong&gt;自然比较&lt;/strong&gt;（生存层面），比较的结果（优劣感）是心理失衡的主要来源。发奖金时的心理波动是典型例证。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;负反馈：&lt;/strong&gt; 恢复平衡的&lt;strong&gt;行动引擎&lt;/strong&gt;。分为三种：&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;自动负反馈：&lt;/strong&gt; 生理层面的无意识调节（如笑一阵子会自然停止）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;行为负反馈：&lt;/strong&gt; 有意识的外在行动（如饿了去找吃的，遇到危险躲避）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;认知负反馈：&lt;/strong&gt; 当行动困难或代价过高时，通过&lt;strong&gt;改变认知&lt;/strong&gt;来恢复平衡（即启动&lt;strong&gt;平衡补偿机制&lt;/strong&gt;）。这是心理调适的核心，也是诸多“自欺”行为的根源。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;经验价值清单：&lt;/strong&gt; 负反馈行动的&lt;strong&gt;决策依据库&lt;/strong&gt;。它并非天生，而是由四部分动态构建：&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;经历清单：&lt;/strong&gt; 亲身经历或外界输入的信息库，每个经历被打上“&lt;strong&gt;利益分数&lt;/strong&gt;”（正/负向感受）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;经验清单：&lt;/strong&gt; 从经历中归纳的规律，或通过“&lt;strong&gt;非逻辑接受&lt;/strong&gt;”直接植入的观念（如父母教诲）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;价值清单：&lt;/strong&gt; 在特定场景下，被调用的、利益分数为正或负的经验集合，用于计算当下行动的利弊。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;价值观清单：&lt;/strong&gt; 更为稳定和核心的底层信念体系，由重要经历和环境塑造，随年龄增长趋于固化。它深刻影响前三者的形成与调用。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;平衡补偿机制：&lt;/strong&gt; 当行为负反馈难以实施或无效时，认知负反馈的主要执行者，被形象地称为“&lt;strong&gt;弹簧人&lt;/strong&gt;”。其工作方式极具弹性（亦具欺骗性）：&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;改变经验的重要性：&lt;/strong&gt; 调整价值清单中某条经验的“利益分数”权重（如将“必须每周健身5天”逐步降级为“不运动也行”）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;强行改变经验的确定性：&lt;/strong&gt; 调高或调低某条经验生效的概率（如买彩票者高估中奖概率）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;寻找合理理由/台阶：&lt;/strong&gt; 归因于外部不可抗力（100%合理理由），启用新比较维度（平衡补偿），或彻底降低标准/期望（认命）。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;&lt;strong&gt;全因模型的运行逻辑：&lt;/strong&gt; 信息（含比较结果）通过感官（眼耳鼻舌身意）输入，引发平衡态失衡，产生“需要”和动机。个体依据“经验价值清单”分析现状，计算利弊，选择能耗最低的“负反馈”路径（优先行为，其次认知）来恢复平衡。若行为无效或不愿行动，“弹簧人”（平衡补偿机制）即介入，通过认知调整达成心理平衡。&lt;strong&gt;“利益脂肪”&lt;/strong&gt;（储存名利权智爱等资源）的概念，则类比生物储存脂肪，被视为应对未来不确定性、保障未来负反馈顺利进行的储备。&lt;/p&gt;&#xA;&lt;h2 id=&#34;二-思维错误地图进步路上的隐形陷阱&#34;&gt;二、 思维错误地图：进步路上的隐形陷阱&lt;/h2&gt;&#xA;&lt;p&gt;基于全因模型，池宇峰在实践篇系统性地绘制了阻碍进步的“&lt;strong&gt;思维错误树&lt;/strong&gt;”，主要归结为三大根源及衍生的枝节错误：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;strong&gt;本能之错：&lt;/strong&gt; 源于生物本能与情绪。&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;情绪失控：&lt;/strong&gt; 冲动、暴怒（延迟法应对）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;过度比较：&lt;/strong&gt; 引发&lt;strong&gt;嫉妒&lt;/strong&gt;（向上比时）与&lt;strong&gt;傲慢&lt;/strong&gt;（向下比时）（应对：专注自我提升/培养同喜心；直面缺点）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;欲望无限/贪婪：&lt;/strong&gt; (应对：学会感恩)。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;变化惰性：&lt;/strong&gt; 抗拒离开舒适区。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;囤积“利益脂肪”&lt;/strong&gt;。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;strong&gt;不知之障：&lt;/strong&gt; 源于认知局限与信息边界。&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;信息边界限制：&lt;/strong&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;假设信息填补：&lt;/strong&gt; 沟通中，人们会用自身经验（75%）填补对他人的未知（25%），导致误解（“你眼中的他人，是75%的自己”）。应对：建立人群行为模型，多参考第三方意见。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;沟通ABCD理论：&lt;/strong&gt; 信息从A（想法）到B（表达）到C（接收）到D（解读）必然层层流失。应对：充分多角度表达(B)，多确认理解(C)，职业化直面误解(D)。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;高山俯瞰效应：&lt;/strong&gt; 低层级者难以理解高层级的视野与行为（“站在山脚下的人看不懂山顶的人”）。。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;不知有不知无：&lt;/strong&gt; “&lt;strong&gt;不知道自己不知道&lt;/strong&gt;”，导致盲目自信（应对：放大镜思维，看清差距细节）。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;雪山困境：&lt;/strong&gt; 人至高处，弱点暴露风险更大。&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;错误价值观。&lt;/strong&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;strong&gt;弹簧迷雾：&lt;/strong&gt; 源于“弹簧人”维护判断力自信的扭曲机制。&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于 LPPL 模型的纳斯达克指数泡沫检测与预测分析</title>
      <link>http://gewutang.cn/2025/07/04/lppl/</link>
      <pubDate>Fri, 04 Jul 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/07/04/lppl/</guid>
      <description>&lt;div id=&#34;摘要&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;摘要&lt;/h1&gt;&#xA;&lt;p&gt;本文运用 Log-Periodic Power Law (LPPL) 模型对纳斯达克指数进行分析，旨在识别市场中的泡沫特征并预测潜在的趋势转折点。LPPL 模型结合了幂律和对数周期振荡，能够捕捉金融市场中的非线性特征和临界现象。&lt;/p&gt;&#xA;&lt;p&gt;通过 R 语言实现模型拟合与参数估计，我们对纳斯达克指数在不同历史时期的泡沫形成与破裂过程进行了实证研究，并验证了模型在市场极端波动情况下的应用价值。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;lppl-模型理论基础&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;LPPL 模型理论基础&lt;/h1&gt;&#xA;&lt;p&gt;LPPL 模型由 Didier Sornette 提出的一种用于描述金融市场泡沫和崩溃的数学模型，其核心思想是将价格的加速上涨（或下跌）与对数周期性振荡相结合，反映市场参与者的从众心理和正反馈机制。&lt;/p&gt;&#xA;&lt;p&gt;LPPL 模型的数学表达式为：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;\ln P(t) = A + B(t_c - t)^\beta + C(t_c - t)^\beta \cos\left[\omega \ln(t_c - t) + \phi\right]&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(P(t)\)&lt;/span&gt; 是资产价格&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(t_c\)&lt;/span&gt; 是临界时间点（泡沫破裂的理论时间）&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\beta\)&lt;/span&gt; 是幂律指数（ &lt;span class=&#34;math inline&#34;&gt;\(0 &amp;lt; \beta &amp;lt; 1\)&lt;/span&gt; ），控制价格加速上涨的速率&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\omega\)&lt;/span&gt; 是振荡频率，控制对数周期振荡的周期&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\phi\)&lt;/span&gt; 是相位角，控制振荡的起始位置&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(A, B, C\)&lt;/span&gt; 是常数参数，分别表示价格基准水平、趋势方向和振荡幅度&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;&lt;strong&gt;模型各部分的经济学解释&lt;/strong&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>百折千转折腾网站的日子</title>
      <link>http://gewutang.cn/2025/06/06/building-a-website-through-countless-twists-and-turns/</link>
      <pubDate>Fri, 06 Jun 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/06/06/building-a-website-through-countless-twists-and-turns/</guid>
      <description>&lt;p&gt;一直以来我都想创建一个单独的&lt;a href=&#34;https://rfinance.org.cn&#34;&gt;&lt;em&gt;r-finance&lt;/em&gt;&lt;/a&gt;网站来向国内的人推广&lt;em&gt;R&lt;/em&gt;语言在金融领域的知识和实践。之前苦于没有精力和时间以及缺乏建设网站的经验没能去做。毕竟作为财务管理出身的人，虽然一直以来也会跟R、Python、SQL等编程语言打交道，但从未与搭建网站单刀相对。&lt;/p&gt;&#xA;&lt;p&gt;转机就在最近。我在学习Hugo搭建网站的过程中，发现github和netlify结合可以很便捷地搭建网站。加之Hugo提供的主题非常丰富，完全可以满足我的网站需求。无需再等，我决定尝试一下。&lt;/p&gt;&#xA;&lt;p&gt;这次搭建网站我使用的基础工具是Rstudio以及R包bolgdown。有了工具之后，我开始在脑海中构思网站的结构，并在茫茫模板之海中打捞自己中意的模板。终于天不负我，我找到一个叫 &lt;a href=&#34;https://github.com/Vimux/Mainroad&#34;&gt;mainroad&lt;/a&gt;的主题，简介大方，十分贴合我的想象。&lt;/p&gt;&#xA;&lt;p&gt;以下是我的建站步骤，十分丝滑。&lt;/p&gt;&#xA;&lt;div id=&#34;创建和克隆github库&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;创建和克隆github库&lt;/h1&gt;&#xA;&lt;p&gt;我先在github上建立一个名为&lt;a href=&#34;https://github.com/dengyishuo/r-finance&#34;&gt;r-finance&lt;/a&gt;的库。创建库时选择自动生成readme文件、gitignore文件和license文件，同时选择master作为默认分支。之后，在Rstudio中操作克隆项目到本地。具体操作为：&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Rstudio -&amp;gt;&amp;gt; tools -&amp;gt;&amp;gt; Terminal -&amp;gt;&amp;gt; New Terminal&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;输入以下命令将r-finance库克隆到本地。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;git clone https://github.com/dengyishuo/r-finance.git&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;进入r-finance目录，设置.gitignore文件。这个文件作用极大，可以帮助我们忽略一些不必要的文件，避免提交到github上。以下是我设置的.gitignore文件内容：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;public/&#xA;resources/&#xA;.hugo_build.lock&#xA;# History files&#xA;.Rhistory&#xA;.Rapp.history&#xA;*.Rproj&#xA;.Rprofile&#xA;# Session Data files&#xA;.RData&#xA;.RDataTmp&#xA;# User-specific files&#xA;.Ruserdata&#xA;# Output files from R CMD check&#xA;/*.Rcheck/&#xA;# RStudio files&#xA;.Rproj.user/&#xA;# knitr and R markdown default cache directories&#xA;*_cache/&#xA;/cache/&#xA;# R Environment Variables&#xA;.Renviron&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;如果未安装blogdown包的话，先在Rstudio中安装blogdown包，命令如下：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;install.packages(&amp;quot;blogdown&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;接下来在Rstudio中，将r-finance文件夹设定为当前工作目录，再使用blogdown包创建一个网站。命令如下：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;blogdown::new_site()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;在bash中使用以下命令更新主题为mainroad：&lt;/p&gt;</description>
    </item>
    <item>
      <title>五分钟搞定 SMA 指标的量化策略开发和回测</title>
      <link>http://gewutang.cn/2025/05/25/sma/</link>
      <pubDate>Sun, 25 May 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/05/25/sma/</guid>
      <description>&lt;p&gt;常有人问我能用几分钟时间写一段代码进行一个简易的量化投资策略的开发和回测吗，答案是当然可以。对于量化投资而言，门槛永远不是编程和代码，而是投资思路和模型解读。下面我们用五分钟左右的时间来开发一个基于 SMA 指标的量化投资策略并进行回测。&lt;/p&gt;&#xA;&lt;div id=&#34;引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;引言&lt;/h1&gt;&#xA;&lt;p&gt;本文的代码基于&lt;a href=&#34;https://r-project.org&#34;&gt;R&lt;/a&gt;软件和 &lt;a href=&#34;https://posit.co&#34;&gt;Rstuido&lt;/a&gt; 软件运行，如果未安装上述软件，请自行安装。&lt;/p&gt;&#xA;&lt;p&gt;策略开发还需要基于以下 R 包来实现，可以运行如下代码安装并加载相关 R 包。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 安装依赖包，如果未安装&#xA;install.packages(c(&amp;quot;quantstrat&amp;quot;, &amp;quot;Tushare&amp;quot;, &amp;quot;xts&amp;quot;, &amp;quot;dplyr&amp;quot;, &amp;quot;showtext&amp;quot;, &amp;quot;zoo&amp;quot;,&amp;quot;PerformanceAnalytics&amp;quot;,&amp;quot;tidyverse&amp;quot;))&#xA;# 加载软件包&#xA;suppressMessages({&#xA;  library(quantmod)&#xA;  library(quantstrat)&#xA;  library(PerformanceAnalytics)&#xA;  library(Tushare)&#xA;  library(xts)&#xA;  library(dplyr)&#xA;  library(zoo)&#xA;  library(tidyverse)&#xA;})&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;清理环境&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;清理环境&lt;/h1&gt;&#xA;&lt;p&gt;第一步，先清理历史环境，避免历史环境的干扰。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 清理历史环境&#xA;rm(list=ls(.blotter), envir=.blotter)&#xA;rm(list=ls(.strategy), envir=.strategy)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;策略开发&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;策略开发&lt;/h1&gt;&#xA;&lt;p&gt;下面进入正式开发流程。&lt;/p&gt;&#xA;&lt;div id=&#34;初始化设置&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;初始化设置&lt;/h2&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 参数设置&#xA;initDate &amp;lt;- &amp;quot;2009-12-31&amp;quot;&#xA;fromDate &amp;lt;- &amp;quot;2010-01-01&amp;quot;&#xA;endDate &amp;lt;- Sys.Date()&#xA;symbol &amp;lt;- &amp;quot;600519.SS&amp;quot;&#xA;initEq &amp;lt;- 100000&#xA;fastMA &amp;lt;- 5&#xA;slowMA &amp;lt;- 20&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;获取数据&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;获取数据&lt;/h2&gt;&#xA;&lt;p&gt;使用quantmod获取贵州茅台数据&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 获取数据&#xA;getSymbols(symbol, from=fromDate, to=endDate, src=&amp;quot;yahoo&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;600519.SS&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;maotai &amp;lt;- `600519.SS`&#xA;colnames(maotai) &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;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;初始化策略&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;初始化策略&lt;/h2&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;currency(&amp;quot;RMB&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RMB&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(symbol, currency=&amp;quot;RMB&amp;quot;, multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;X600519.SS&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;strategy.st &amp;lt;- &amp;quot;maotai_sma&amp;quot;&#xA;portfolio.st &amp;lt;- &amp;quot;maotai_portfolio&amp;quot;&#xA;account.st &amp;lt;- &amp;quot;maotai_account&amp;quot;&#xA;&#xA;# 清除旧策略(如果存在)&#xA;if(exists(&amp;quot;strategy.st&amp;quot;, where=.strategy)) {&#xA;  rm.strat(strategy.st)&#xA;}&#xA;&#xA;# 创建组合/账户/策略&#xA;initPortf(portfolio.st, symbols=symbol, initDate=initDate)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;maotai_portfolio&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio.st, initDate=initDate, initEq=initEq)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;maotai_account&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio.st, initDate=initDate)&#xA;strategy(strategy.st, store=TRUE)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;添加指标&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;添加指标&lt;/h2&gt;&#xA;&lt;p&gt;这里添加的简单移动平均线(SMA)：&lt;/p&gt;</description>
    </item>
    <item>
      <title>用R中的eTTR包和tidyquant包绘制布林带</title>
      <link>http://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</link>
      <pubDate>Thu, 22 May 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</guid>
      <description>&lt;div id=&#34;引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;引言&lt;/h1&gt;&#xA;&lt;p&gt;俗话说，巧妇难为无米之炊。绘图的前提是得有数据在手。下面是获取数据的代码：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义股票代码&#xA;tickers &amp;lt;- c(&amp;quot;TSLA&amp;quot;, &amp;quot;AAPL&amp;quot;, &amp;quot;NVDA&amp;quot;)&#xA;&#xA;# 批量下载股票数据并进行预处理&#xA;stock_list &amp;lt;- map(tickers, function(ticker) {&#xA;  # 下载数据&#xA;  stock_data &amp;lt;- getSymbols(ticker, auto.assign = FALSE)&#xA;  # 重命名列&#xA;  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;)&#xA;  # 转化为tibble&#xA;  tibble_data &amp;lt;- as_tibble(fortify.zoo(stock_data))&#xA;  # 标准化列命名&#xA;  renamed_data &amp;lt;- tibble_data %&amp;gt;%&#xA;    rename(&#xA;      Date = Index,&#xA;      open = Open,&#xA;      high = High,&#xA;      low = Low,&#xA;      close = Close,&#xA;      volume = Volume,&#xA;      adjusted = Adjusted&#xA;    )&#xA;  # 动态命名&#xA;  dyn_name &amp;lt;- paste0(ticker, &amp;quot;_data&amp;quot;)&#xA;  assign(dyn_name, renamed_data, envir = globalenv())&#xA;  return(get(dyn_name))&#xA;})&#xA;# 重新命名列表&#xA;names(stock_list) &amp;lt;- tickers&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;有了基础数据之后，接下来是根据布林带定义计算布林带值，并将布林带数据整合进原始数据备用。&lt;/p&gt;</description>
    </item>
    <item>
      <title>用R实现滚动单因子模型</title>
      <link>http://gewutang.cn/2025/05/15/roll-smf/</link>
      <pubDate>Thu, 15 May 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/05/15/roll-smf/</guid>
      <description>&lt;div id=&#34;引言&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;引言&lt;/h2&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;单因子模型&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;单因子模型&lt;/h2&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;滚动单因子模型的-r-代码&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;滚动单因子模型的 R 代码&lt;/h2&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 获取最新市场数据(2024-01-01至2025-05-15)&#xA;getSymbols(c(&amp;quot;TSLA&amp;quot;, &amp;quot;^IXIC&amp;quot;), from=&amp;quot;2024-01-01&amp;quot;, src=&amp;quot;yahoo&amp;quot;)&#xA;tsla_ret &amp;lt;- dailyReturn(Ad(TSLA))&#xA;nasdaq_ret &amp;lt;- dailyReturn(Ad(IXIC))&#xA;&#xA;# 改进版滚动SFM函数&#xA;# 高效版滚动SFM函数&#xA;rollSFM &amp;lt;- function(Ra, Rb, window = 60) {&#xA;  # 输入验证&#xA;  n &amp;lt;- length(Ra)&#xA;  if (n != length(Rb)) stop(&amp;quot;Ra和Rb的长度必须相同&amp;quot;)&#xA;  if (n &amp;lt; window) stop(&amp;quot;数据长度必须大于或等于窗口大小&amp;quot;)&#xA;  &#xA;  # 预计算累积量用于快速计算&#xA;  cum_sum_x &amp;lt;- cumsum(Rb)&#xA;  cum_sum_y &amp;lt;- cumsum(Ra)&#xA;  cum_sum_xy &amp;lt;- cumsum(Ra * Rb)&#xA;  cum_sum_x2 &amp;lt;- cumsum(Rb^2)&#xA;  &#xA;  # 初始化结果向量&#xA;  result_size &amp;lt;- n - window + 1&#xA;  alpha &amp;lt;- numeric(result_size)&#xA;  beta &amp;lt;- numeric(result_size)&#xA;  r_squared &amp;lt;- numeric(result_size)&#xA;  &#xA;  # 计算每个窗口的统计量&#xA;  for (i in 1:result_size) {&#xA;    end_idx &amp;lt;- i + window - 1&#xA;    start_idx &amp;lt;- end_idx - window + 1&#xA;    &#xA;    # 快速计算窗口内的统计量&#xA;    sum_x &amp;lt;- cum_sum_x[end_idx] - if(start_idx &amp;gt; 1) cum_sum_x[start_idx - 1] else 0&#xA;    sum_y &amp;lt;- cum_sum_y[end_idx] - if(start_idx &amp;gt; 1) cum_sum_y[start_idx - 1] else 0&#xA;    sum_xy &amp;lt;- cum_sum_xy[end_idx] - if(start_idx &amp;gt; 1) cum_sum_xy[start_idx - 1] else 0&#xA;    sum_x2 &amp;lt;- cum_sum_x2[end_idx] - if(start_idx &amp;gt; 1) cum_sum_x2[start_idx - 1] else 0&#xA;    &#xA;    # 计算均值&#xA;    mean_x &amp;lt;- sum_x / window&#xA;    mean_y &amp;lt;- sum_y / window&#xA;    &#xA;    # 计算回归系数&#xA;    numerator &amp;lt;- sum_xy - sum_x * mean_y&#xA;    denominator &amp;lt;- sum_x2 - sum_x * mean_x&#xA;    &#xA;    # 避免除以零&#xA;    if (denominator == 0) {&#xA;      beta[i] &amp;lt;- NA&#xA;      alpha[i] &amp;lt;- NA&#xA;      r_squared[i] &amp;lt;- NA&#xA;    } else {&#xA;      beta[i] &amp;lt;- numerator / denominator&#xA;      alpha[i] &amp;lt;- mean_y - beta[i] * mean_x&#xA;      &#xA;      # 计算R²&#xA;      pred_y &amp;lt;- alpha[i] + beta[i] * Rb[start_idx:end_idx]&#xA;      ss_total &amp;lt;- sum((Ra[start_idx:end_idx] - mean_y)^2)&#xA;      ss_residual &amp;lt;- sum((Ra[start_idx:end_idx] - pred_y)^2)&#xA;      r_squared[i] &amp;lt;- 1 - (ss_residual / ss_total)&#xA;    }&#xA;  }&#xA;  &#xA;  # 返回结果数据框&#xA;  data.frame(&#xA;    Date = if(is.ts(Ra) || is.zoo(Ra)) index(Ra)[window:n] else window:n,&#xA;    Alpha = alpha,&#xA;    Beta = beta,&#xA;    R_squared = r_squared&#xA;  )&#xA;}&#xA;&#xA;# 计算滚动参数&#xA;results &amp;lt;- rollSFM(tsla_ret, nasdaq_ret)&#xA;tail(results)  # 查看最近6个窗口结果&#xA;&#xA;&#xA;&#xA;# 创建绘图主题&#xA;finance_theme &amp;lt;- theme_minimal() +&#xA;  theme(&#xA;    plot.title = element_text(size=14, face=&amp;quot;bold&amp;quot;),&#xA;    axis.title = element_text(size=12),&#xA;    panel.grid.minor = element_blank(),&#xA;    legend.position = &amp;quot;top&amp;quot;&#xA;  )&#xA;&#xA;# Beta系数时序图&#xA;ggplot(results, aes(x=Date, y=Beta)) +&#xA;  geom_line(color=&amp;quot;#2b8cbe&amp;quot;, size=1) +&#xA;  geom_hline(yintercept=1, linetype=&amp;quot;dashed&amp;quot;, color=&amp;quot;#e34a33&amp;quot;) +&#xA;  labs(title=&amp;quot;TSLA相对NASDAQ的60日滚动Beta系数&amp;quot;,&#xA;       x=&amp;quot;日期&amp;quot;, y=&amp;quot;Beta系数&amp;quot;) +&#xA;  scale_x_date(labels=date_format(&amp;quot;%Y-%m&amp;quot;)) +&#xA;  finance_theme&#xA;&#xA;# Alpha-Beta联合分布图&#xA;ggplot(results, aes(x=Beta, y=Alpha)) +&#xA;  geom_point(aes(color=R_squared), size=3) +&#xA;  scale_color_gradient(low=&amp;quot;#fee8c8&amp;quot;, high=&amp;quot;#e34a33&amp;quot;, &#xA;                      name=&amp;quot;R平方值&amp;quot;) +&#xA;  geom_vline(xintercept=1, linetype=&amp;quot;dotted&amp;quot;) +&#xA;  geom_hline(yintercept=0, linetype=&amp;quot;dotted&amp;quot;) +&#xA;  labs(title=&amp;quot;TSLA Alpha-Beta分布（60日窗口）&amp;quot;,&#xA;       x=&amp;quot;系统性风险(Beta)&amp;quot;, y=&amp;quot;超额收益(Alpha)&amp;quot;) +&#xA;  finance_theme&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/div&gt;</description>
    </item>
    <item>
      <title>RSI指标可以作为量化投资的信号因子吗</title>
      <link>http://gewutang.cn/2025/05/04/rsi/</link>
      <pubDate>Sun, 04 May 2025 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2025/05/04/rsi/</guid>
      <description>&lt;h2 id=&#34;引言&#34;&gt;引言&lt;/h2&gt;&#xA;&lt;p&gt;技术指标是基于历史价格和成交量数据构建的分析工具，用于研判市场趋势及买卖时机。常见类型包括趋势指标（如移动平均线）、动量指标（如RSI）、波动率指标（布林带）和成交量指标（OBV）。其中，相对强弱指数（RSI）由韦尔斯·怀尔德于1978年提出，通过计算特定周期（通常14日）内平均涨幅与总波动的比值，衡量价格变化强度。其公式为：RSI = 100 - 100/(1 + 平均涨幅/平均跌幅)，数值在0-100间波动。应用时，70以上视为超买信号，提示潜在回调可能；30以下为超卖信号，暗示反弹机会。进阶用法包括：观察RSI与价格背离判断趋势反转；结合趋势线突破确认交易信号；在30-70区间内运用中位线（50）判断多空力道。&lt;/p&gt;&#xA;&lt;p&gt;需注意，在单边行情中RSI易出现钝化，应与趋势指标配合使用以提高准确性。该指标广泛应用于股票、外汇及加密货币市场的短线交易策略。&lt;/p&gt;&#xA;&lt;h2 id=&#34;策略实现&#34;&gt;策略实现&lt;/h2&gt;&#xA;&lt;p&gt;下面我们用R实现一个基于RSI指标的量化交易策略，我们基于quantstrat包实现相关策略，完整代码及注释如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# ======================================================&#xA;# 量化交易策略框架搭建（基于quantstrat包）&#xA;# 策略逻辑：基于RSI指标的双向交易系统&#xA;# 核心流程：环境初始化 -&amp;gt; 策略定义 -&amp;gt; 数据准备 -&amp;gt; 回测执行&#xA;# ======================================================&#xA;&#xA;# ---------------------------&#xA;# 1. 包加载与环境清理&#xA;# ---------------------------&#xA;# 安装必要包（已注释，按需启用）&#xA;# install.packages(&amp;quot;devtools&amp;quot;) &#xA;# install.packages(&amp;quot;FinancialInstrument&amp;quot;) &#xA;# install.packages(&amp;quot;PerformanceAnalytics&amp;quot;) &#xA;# devtools::install_github(&amp;quot;braverock/blotter&amp;quot;)&#xA;# devtools::install_github(&amp;quot;braverock/quantstrat&amp;quot;)&#xA;&#xA;# 加载量化策略包&#xA;require(quantstrat)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;PerformanceAnalytics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:graphics&#39;:&#xA;## &#xA;##     legend&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 清理历史策略数据（避免残留数据干扰）&#xA;suppressWarnings(rm(&amp;quot;order_book.RSI&amp;quot;,pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.RSI&amp;quot;,&amp;quot;portfolio.RSI&amp;quot;,pos=.blotter))&#xA;suppressWarnings(rm(&amp;quot;account.st&amp;quot;,&#xA;                    &amp;quot;portfolio.st&amp;quot;,&#xA;                    &amp;quot;stock.str&amp;quot;,&#xA;                    &amp;quot;stratRSI&amp;quot;,&#xA;                    &amp;quot;initDate&amp;quot;,&#xA;                    &amp;quot;initEq&amp;quot;,&#xA;                    &#39;start_t&#39;,&#xA;                    &#39;end_t&#39;&#xA;                    )&#xA;                 )&#xA;&#xA;# ---------------------------&#xA;# 2. 策略主体构建&#xA;# ---------------------------&#xA;# 创建策略容器&#xA;stratRSI &amp;lt;- strategy(&amp;quot;RSI&amp;quot;)&#xA;n=2  # 参数示例&#xA;&#xA;# 2.1 添加技术指标&#xA;# 使用经典RSI指标（默认周期14）&#xA;stratRSI &amp;lt;- add.indicator(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;RSI&amp;quot;,    # 内置RSI函数&#xA;  arguments = list(price = quote(getPrice(mktdata))), # 获取价格数据&#xA;  label = &amp;quot;RSI&amp;quot;&#xA;)&#xA;&#xA;&#xA;# 2.2 定义交易信号&#xA;# 信号1：RSI上穿70（超买信号）&#xA;stratRSI &amp;lt;- add.signal(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;sigThreshold&amp;quot;,&#xA;  arguments = list(&#xA;    threshold = 70,&#xA;    column = &amp;quot;RSI&amp;quot;,&#xA;    relationship = &amp;quot;gt&amp;quot;,  # greater than&#xA;    cross = TRUE          # 要求穿越阈值&#xA;  ),&#xA;  label = &amp;quot;RSI.gt.70&amp;quot;&#xA;)&#xA;&#xA;# 信号2：RSI下穿30（超卖信号）&#xA;stratRSI &amp;lt;- add.signal(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;sigThreshold&amp;quot;,&#xA;  arguments = list(&#xA;    threshold = 30,&#xA;    column = &amp;quot;RSI&amp;quot;,&#xA;    relationship = &amp;quot;lt&amp;quot;,  # less than&#xA;    cross = TRUE&#xA;  ),&#xA;  label = &amp;quot;RSI.lt.30&amp;quot;&#xA;)&#xA;&#xA;# 2.3 设置交易规则&#xA;# 规则组1：做空规则&#xA;# 入场规则：RSI&amp;gt;70时建立空头仓位&#xA;stratRSI &amp;lt;- add.rule(&#xA;  strategy = stratRSI, &#xA;  name = &#39;ruleSignal&#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;RSI.gt.70&amp;quot;,   # 触发信号列&#xA;    sigval = TRUE,          # 信号有效值&#xA;    orderqty = -1000,       # 卖出数量&#xA;    ordertype = &#39;market&#39;,   # 市价单&#xA;    orderside = &#39;short&#39;,    # 空头方向&#xA;    pricemethod = &#39;market&#39;,&#xA;    replace = FALSE,        # 不替换现有订单&#xA;    osFUN = osMaxPos        # 使用最大仓位函数&#xA;  ), &#xA;  type = &#39;enter&#39;,           # 入场规则&#xA;  path.dep = TRUE           # 路径依赖&#xA;)&#xA;&#xA;# 离场规则：RSI&amp;lt;30时平空仓&#xA;stratRSI &amp;lt;- add.rule(&#xA;  strategy = stratRSI,&#xA;  name = &#39;ruleSignal&#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;RSI.lt.30&amp;quot;,&#xA;    sigval = TRUE,&#xA;    orderqty = &#39;all&#39;,       # 平掉全部仓位&#xA;    ordertype = &#39;market&#39;,&#xA;    orderside = &#39;short&#39;,&#xA;    pricemethod = &#39;market&#39;,&#xA;    replace = FALSE&#xA;  ),&#xA;  type = &#39;exit&#39;,&#xA;  path.dep = TRUE&#xA;)&#xA;&#xA;# 规则组2：做多规则&#xA;# 入场规则：RSI&amp;lt;30时建立多头仓位&#xA;stratRSI &amp;lt;- add.rule(&#xA;  strategy = stratRSI,&#xA;  name = &#39;ruleSignal&#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;RSI.lt.30&amp;quot;,&#xA;    sigval = TRUE,&#xA;    orderqty = 1000,        # 买入数量&#xA;    ordertype = &#39;market&#39;,&#xA;    orderside = &#39;long&#39;,     # 多头方向&#xA;    pricemethod = &#39;market&#39;,&#xA;    replace = FALSE,&#xA;    osFUN = osMaxPos&#xA;  ),&#xA;  type = &#39;enter&#39;,&#xA;  path.dep = TRUE&#xA;)&#xA;&#xA;# 离场规则：RSI&amp;gt;70时平多仓&#xA;stratRSI &amp;lt;- add.rule(&#xA;  strategy = stratRSI,&#xA;  name = &#39;ruleSignal&#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;RSI.gt.70&amp;quot;,&#xA;    sigval = TRUE,&#xA;    orderqty = &#39;all&#39;,&#xA;    ordertype = &#39;market&#39;,&#xA;    orderside = &#39;long&#39;,&#xA;    pricemethod = &#39;market&#39;,&#xA;    replace = FALSE&#xA;  ),&#xA;  type = &#39;exit&#39;,&#xA;  path.dep = TRUE&#xA;)&#xA;&#xA;# ---------------------------&#xA;# 3. 市场数据准备&#xA;# ---------------------------&#xA;# 设置基础货币&#xA;&#xA;currency(&amp;quot;USD&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;currency(&amp;quot;EUR&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;EUR&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 定义交易标的（美国行业ETF）&#xA;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;)&#xA;# 初始化交易品种数据&#xA;for(symbol in symbols){ &#xA;   # 注册金融工具&#xA;    stock(symbol, currency=&amp;quot;USD&amp;quot;,multiplier=1)&#xA;  # 下载历史数据（默认从Yahoo Finance）&#xA;    getSymbols(symbol)&#xA;}&#xA;&#xA;# 保存数据&#xA;# 遍历所有符号&#xA;# for (symbol in symbols) {&#xA;#  # 检查对象是否存在&#xA;#  if (exists(symbol)) {&#xA;#    # 生成文件名&#xA;#    file_name &amp;lt;- paste0(symbol, &amp;quot;.rds&amp;quot;)&#xA;#    # 保存为RDS文件&#xA;#    saveRDS(get(symbol), file = file_name)&#xA;#    # 打印保存信息&#xA;#    message(&amp;quot;已保存: &amp;quot;, symbol, &amp;quot; -&amp;gt; &amp;quot;, file_name)&#xA;#  } else {&#xA;#    warning(&amp;quot;对象 &amp;quot;, symbol, &amp;quot; 不存在&amp;quot;)&#xA;#  }&#xA;#}&#xA;&#xA;# 可以用类似以下方式测试：&#xA;# applySignals(strategy=stratRSI, mktdata=applyIndicators(strategy=stratRSI, mktdata=symbols[1]))&#xA;&#xA;##### 在此放置演示和测试日期 #################&#xA;#&#xA;#if(isTRUE(options(&#39;in_test&#39;)$in_test))&#xA;#  # 使用测试日期&#xA;#  {initDate=&amp;quot;2000-01-01&amp;quot; &#xA;#  endDate=&amp;quot;2024-12-31&amp;quot;   &#xA;#  } else&#xA;#  # 使用演示默认值&#xA;#  {initDate=&amp;quot;2000-01-01&amp;quot;&#xA;#  endDate=Sys.Date()}&#xA;&#xA;# ---------------------------&#xA;# 4. 回测系统初始化&#xA;# ---------------------------&#xA;# 设置回测参数&#xA;initDate = &#39;2000-01-01&#39;  # 初始化日期&#xA;initEq = 100000          # 初始资金（美元）&#xA;port.st = &#39;RSI&#39;          # 组合名称&#xA;&#xA;# 初始化投资组合&#xA;initPortf(port.st, &#xA;          symbols=symbols, &#xA;          initDate=initDate)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 初始化账户&#xA;initAcct(port.st, &#xA;         portfolios=port.st, &#xA;         initDate=initDate,&#xA;         initEq=initEq)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 初始化订单簿&#xA;initOrders(portfolio=port.st, &#xA;           initDate=initDate)&#xA;&#xA;# 设置仓位限制（每个品种最大300股，最多3个品种）&#xA;for(symbol in symbols){ &#xA;  addPosLimit(port.st, &#xA;              symbol, &#xA;              initDate, &#xA;              300, &#xA;              3 ) &#xA;  } &#xA;&#xA;print(&amp;quot;初始化完成&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;初始化完成&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;end_t&amp;lt;-Sys.time()&#xA;print(paste0(&amp;quot;策略循环耗时:&amp;quot;,end_t-start_t))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;策略循环耗时:6.13126289844513&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 查看订单簿&#xA;#print(getOrderBook(port.st))&#xA;&#xA;start_t&amp;lt;-Sys.time()&#xA;# 更新组合净值&#xA;updatePortf(Portfolio=port.st,Dates=paste(&#39;::&#39;,as.Date(Sys.time()),sep=&#39;&#39;))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-05-08 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2007-01-03/2025-05-07. Using all data&#xA;## instead.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;end_t&amp;lt;-Sys.time()&#xA;print(paste0(&amp;quot;更新交易账簿耗时:&amp;quot;,end_t-start_t))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;更新交易账簿耗时:0.747688055038452&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 临时修改quantmod图形参数&#xA;themelist&amp;lt;-chart_theme()&#xA;themelist$col$up.col&amp;lt;-&#39;lightgreen&#39;&#xA;themelist$col$dn.col&amp;lt;-&#39;pink&#39;&#xA;&#xA;for(symbol in symbols){&#xA;    # dev.new()&#xA;    chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)  # 绘制持仓图&#xA;    plot(add_RSI(n=2))&#xA;    print(paste0(symbol,&amp;quot;仓位图&amp;quot;))&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;672&#34; /&gt;&lt;img src=&#34;http://gewutang.cn/2025/05/04/rsi/index_files/figure-html/unnamed-chunk-3-2.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>CVaR 和 ES 补充 VaR 的风险度量方法解析（含 R 包应用）</title>
      <link>http://gewutang.cn/2019/07/13/cvar-es/</link>
      <pubDate>Sat, 13 Jul 2019 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2019/07/13/cvar-es/</guid>
      <description>&lt;div id=&#34;理论基础概述&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;理论基础概述&lt;/h1&gt;&#xA;&lt;p&gt;在金融风险管理领域，风险价值（VaR）是一种广泛使用的风险度量工具，它衡量在一定置信水平下，某一金融资产或投资组合在未来特定时期内可能遭受的最大损失。然而，VaR 存在一些局限性，主要体现在以下几个方面：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;缺乏次可加性：在某些情况下，投资组合的 VaR 可能大于其各组成部分 VaR 之和，这与分散投资降低风险的直觉相悖。&lt;/li&gt;&#xA;&lt;li&gt;对尾部风险的刻画不足：VaR 只关注损失分布的分位数点，而不考虑超过该点的损失程度。&lt;/li&gt;&#xA;&lt;li&gt;不满足一致性风险度量的要求：VaR 不满足一致性风险度量公理中的次可加性、正齐次性、单调性和平移不变性。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;为了克服 VaR 的这些局限性，条件风险价值（CVaR）和预期短缺（ES）作为更高级的风险度量方法被提出。CVaR 与 ES 的基本概念条件风险价值（Conditional Value at Risk, CVaR），也称为平均短缺（Average Shortfall, AS）或预期短缺（Expected Shortfall, ES），是指在给定置信水平下，超过 VaR 的损失的期望值。&lt;/p&gt;&#xA;&lt;p&gt;它不仅考虑了损失超过 VaR 的概率，还考虑了超过部分的大小，因此能更全面地反映尾部风险。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;数学公式解析&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;数学公式解析&lt;/h1&gt;&#xA;&lt;div id=&#34;var-的数学定义&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;VaR 的数学定义&lt;/h2&gt;&#xA;&lt;p&gt;对于给定的投资组合收益率 R 和置信水平 &lt;span class=&#34;math inline&#34;&gt;\(\alpha\)&lt;/span&gt;，VaR 可以定义为：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;\text{VaR}_\alpha = -\inf \{ x \in \mathbb{R} : F_R(x) \geq \alpha \}&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中，&lt;span class=&#34;math inline&#34;&gt;\(F_R(x)\)&lt;/span&gt; 是收益率 &lt;span class=&#34;math inline&#34;&gt;\(R\)&lt;/span&gt; 的累积分布函数。&lt;/p&gt;&#xA;&lt;p&gt;在实际应用中，VaR 通常表示为：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;P(R \leq -\text{VaR}_\alpha) = 1 - \alpha&#xA;\]&lt;/span&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于双均线交叉与追踪止损的交易策略</title>
      <link>http://gewutang.cn/2018/07/27/macross-stoptrailing/</link>
      <pubDate>Fri, 27 Jul 2018 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2018/07/27/macross-stoptrailing/</guid>
      <description>&lt;p&gt;在趋势跟踪策略中，移动平均线交叉是识别趋势转折的经典方法，而止损机制则是控制风险的关键补充。本文解析的 R 语言代码，基于quantstrat包实现了一套融合双均线交叉信号与追踪止损的交易策略，以 AAPL 为标的展示了从策略构建到回测的完整流程。通过明确的信号规则与风险控制机制，该策略为趋势跟踪的量化实现提供了可参考的框架。&lt;/p&gt;&#xA;&lt;div id=&#34;核心指标与策略逻辑&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;核心指标与策略逻辑&lt;/h1&gt;&#xA;&lt;p&gt;策略的核心指标是两条简单移动平均线（SMA）：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;快速 SMA（5 日）：基于最近 5 个交易日的收盘价计算，反映短期价格趋势。&lt;/li&gt;&#xA;&lt;li&gt;慢速 SMA（75 日）：基于最近 75 个交易日的收盘价计算，反映中长期价格趋势。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;策略逻辑围绕两条均线的交叉展开：当快速 SMA 从下方上穿慢速 SMA 时，视为多头趋势确立，生成买入信号；当快速 SMA 从上方下穿慢速 SMA 时，视为趋势转弱，生成卖出信号。同时，为避免趋势反转时的大额亏损，策略加入了追踪止损（stoptrailing）机制 —— 当价格从持仓后的高点回落一定比例时，自动平仓离场，锁定部分收益。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;策略代码&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;策略代码&lt;/h1&gt;&#xA;&lt;p&gt;以下是完整的策略代码，包含逐段注释以解释各环节功能：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;###############################################################################&#xA;# 一个使用简单均线交叉策略的演示脚本，包含追踪止损，用于测试&#xA;###############################################################################&#xA;&#xA;# 加载quantstrat包，用于量化策略开发与回测&#xA;library(quantstrat)&#xA;&#xA;# 策略参数设置&#xA;init_date &amp;lt;- &amp;quot;2017-01-01&amp;quot;  # 初始化日期&#xA;start_date &amp;lt;- &amp;quot;2017-01-01&amp;quot;  # 回测开始日期&#xA;end_date &amp;lt;- &amp;quot;2018-12-31&amp;quot;    # 回测结束日期&#xA;init_equity &amp;lt;- 1e4          # 初始资金1万美元&#xA;.orderqty &amp;lt;- 100            # 订单数量（后续规则中实际使用1000）&#xA;Sys.setenv(TZ = &amp;quot;UTC&amp;quot;)      # 设置时区为UTC，避免日期处理问题&#xA;fast &amp;lt;- 5                   # 快速SMA周期（5日）&#xA;slow &amp;lt;- 75                  # 慢速SMA周期（75日）&#xA;symbols &amp;lt;- symbol &amp;lt;- &amp;quot;AAPL&amp;quot; # 交易标的为AAPL&#xA;tradesize = 1e4             # 交易规模（1万美元）&#xA;&#xA;# 定义货币与标的属性：AAPL以美元计价，合约乘数为1&#xA;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载AAPL历史数据（此处使用内置数据，也可从雅虎财经下载）&#xA;data(AAPL)&#xA;stock(symbols, currency = &amp;quot;USD&amp;quot;, multiplier = 1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 初始化组合、账户与订单簿&#xA;portfolio.st &amp;lt;- &amp;quot;Port.Luxor&amp;quot;  # 组合名称&#xA;account.st &amp;lt;- &amp;quot;Acct.Luxor&amp;quot;    # 账户名称&#xA;strategy.st &amp;lt;- &amp;quot;Strat.Luxor&amp;quot;  # 策略名称&#xA;&#xA;# 清理旧策略数据，确保回测环境纯净&#xA;rm.strat(strategy.st)&#xA;rm.strat(portfolio.st)&#xA;rm.strat(account.st)&#xA;&#xA;# 初始化投资组合：指定标的与初始化日期&#xA;initPortf(&#xA;  name = portfolio.st,&#xA;  symbols = symbols,&#xA;  initDate = init_date&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Port.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 初始化账户：关联组合，设置初始资金&#xA;initAcct(&#xA;  name = account.st,&#xA;  portfolios = portfolio.st,&#xA;  initDate = init_date,&#xA;  initEq = init_equity&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 初始化订单簿：记录交易订单&#xA;initOrders(&#xA;  portfolio = portfolio.st,&#xA;  symbols = symbols,&#xA;  initDate = init_date&#xA;)&#xA;&#xA;# 创建策略对象并存储到环境中&#xA;strategy(strategy.st, store = TRUE)&#xA;&#xA;# 添加技术指标：快速SMA与慢速SMA&#xA;add.indicator(&#xA;  strategy = strategy.st,&#xA;  name = &amp;quot;SMA&amp;quot;,  # 使用SMA函数计算移动平均线&#xA;  arguments = list(x = quote(Cl(mktdata)), n = fast),  # 输入为收盘价，周期5日&#xA;  label = &amp;quot;nFast&amp;quot;  # 指标标签：快速SMA&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;add.indicator(&#xA;  strategy = strategy.st,&#xA;  name = &amp;quot;SMA&amp;quot;,&#xA;  arguments = list(x = quote(Cl(mktdata)), n = slow),  # 周期75日&#xA;  label = &amp;quot;nSlow&amp;quot;  # 指标标签：慢速SMA&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 生成交易信号：基于双均线交叉&#xA;# 信号1：快速SMA上穿慢速SMA（大于关系），视为多头信号（LONG）&#xA;add.signal(&#xA;  strategy.st,&#xA;  name = &amp;quot;sigCrossover&amp;quot;,  # 交叉信号函数&#xA;  arguments = list(&#xA;    column = c(&amp;quot;nFast&amp;quot;, &amp;quot;nSlow&amp;quot;),  # 对比快速与慢速SMA&#xA;    relationship = &amp;quot;gt&amp;quot;  # 大于（上穿）&#xA;  ),&#xA;  label = &amp;quot;LONG&amp;quot;  # 信号标签：买入&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 信号2：快速SMA下穿慢速SMA（小于关系），视为空头信号（SHORT）&#xA;add.signal(&#xA;  strategy.st,&#xA;  name = &amp;quot;sigCrossover&amp;quot;,&#xA;  arguments = list(&#xA;    column = c(&amp;quot;nFast&amp;quot;, &amp;quot;nSlow&amp;quot;),&#xA;    relationship = &amp;quot;lt&amp;quot;  # 小于（下穿）&#xA;  ),&#xA;  label = &amp;quot;SHORT&amp;quot;  # 信号标签：卖出&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 入场规则：当LONG信号触发时，以限价单买入&#xA;add.rule(&#xA;  strategy.st,&#xA;  name = &amp;quot;ruleSignal&amp;quot;,  # 基于信号的规则&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;LONG&amp;quot;,  # 触发信号列：LONG&#xA;    sigval = TRUE,  # 信号值为TRUE时执行&#xA;    orderside = &amp;quot;long&amp;quot;,  # 做多方向&#xA;    ordertype = &amp;quot;limit&amp;quot;,  # 限价单&#xA;    threshold = 1/100,  # 阈值（相对于参考价格的比例）&#xA;    tmult = T,  # 阈值为比例（而非固定值）&#xA;    orderqty = 1000,  # 订单数量：1000股&#xA;    prefer = &amp;quot;Open&amp;quot;,  # 参考价格为开盘价&#xA;    TxnFees = -10,  # 交易费用：-10美元（每次交易成本）&#xA;    orderset = &amp;quot;ocolong&amp;quot;,  # 订单集标签，用于管理相关订单&#xA;    replace = F  # 不替换未成交订单&#xA;  ),&#xA;  type = &amp;quot;enter&amp;quot;,  # 入场规则&#xA;  enabled = T,  # 启用规则&#xA;  label = &amp;quot;EnterLONG&amp;quot;  # 规则标签&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 出场规则：当SHORT信号触发时，平掉所有多头持仓&#xA;add.rule(&#xA;  strategy = strategy.st,&#xA;  name = &amp;quot;ruleSignal&amp;quot;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;SHORT&amp;quot;,  # 触发信号列：SHORT&#xA;    sigval = TRUE,&#xA;    orderqty = &amp;#39;all&amp;#39;,  # 平仓全部持仓&#xA;    orderside = &amp;#39;long&amp;#39;,  # 针对多头持仓&#xA;    ordertype = &amp;quot;market&amp;quot;,  # 市价单（确保快速平仓）&#xA;    prefer = &amp;quot;Open&amp;quot;,  # 参考价格为开盘价&#xA;    TxnFees = -10,  # 交易费用&#xA;    orderset = &amp;quot;ocolong&amp;quot;,&#xA;    replace = F&#xA;  ),&#xA;  type = &amp;quot;exit&amp;quot;,  # 出场规则&#xA;  enabled = T,&#xA;  parent = &amp;quot;EnterLONG&amp;quot;,  # 关联入场规则，确保对应持仓平仓&#xA;  label = &amp;quot;Exit2SHORT&amp;quot;&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 止损规则：追踪止损，控制单边趋势反转风险&#xA;add.rule(&#xA;  strategy.st,&#xA;  name = &amp;quot;ruleSignal&amp;quot;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;LONG&amp;quot;,  # 与入场信号关联&#xA;    sigval = TRUE,&#xA;    orderside = &amp;quot;long&amp;quot;,&#xA;    ordertype = &amp;quot;stoptrailing&amp;quot;,  # 追踪止损单&#xA;    orderqty = &amp;quot;all&amp;quot;,  # 平仓全部持仓&#xA;    prefer = &amp;quot;Close&amp;quot;,  # 参考价格为收盘价&#xA;    threshold = 2/100,  # 追踪止损比例：2%（从高点回落2%平仓）&#xA;    tmult = T,  # 阈值为比例&#xA;    TxnFees = -10,&#xA;    orderset = &amp;quot;ocolong&amp;quot;,&#xA;    replace = F&#xA;  ),&#xA;  type = &amp;quot;chain&amp;quot;,  # 链式规则（跟随入场规则触发）&#xA;  parent = &amp;quot;EnterLONG&amp;quot;,  # 基于EnterLONG规则触发的持仓&#xA;  enabled = T,&#xA;  label = &amp;quot;StopTrailingLONG&amp;quot;  # 规则标签：多头追踪止损&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Strat.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 执行策略回测&#xA;applyStrategy(strategy = strategy.st, portfolios = portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2017-07-27 00:00:00 AAPL 1000 @ 141.598281619503&amp;quot;&#xA;## [1] &amp;quot;2017-08-03 00:00:00 AAPL -1000 @ 150.70126151877&amp;quot;&#xA;## [1] &amp;quot;2017-10-04 00:00:00 AAPL 1000 @ 147.350684699421&amp;quot;&#xA;## [1] &amp;quot;2017-10-19 00:00:00 AAPL -1000 @ 151.241571914452&amp;quot;&#xA;## [1] &amp;quot;2018-03-22 00:00:00 AAPL 1000 @ 166.357919238892&amp;quot;&#xA;## [1] &amp;quot;2018-03-23 00:00:00 AAPL -1000 @ 163.030760854114&amp;quot;&#xA;## [1] &amp;quot;2018-04-19 00:00:00 AAPL 1000 @ 168.23487944963&amp;quot;&#xA;## [1] &amp;quot;2018-04-20 00:00:00 AAPL -1000 @ 164.870181860637&amp;quot;&#xA;## [1] &amp;quot;2018-11-23 00:00:00 AAPL 1000 @ 171.574945095388&amp;quot;&#xA;## [1] &amp;quot;2018-11-26 00:00:00 AAPL -1000 @ 168.14344619348&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 更新组合与账户数据&#xA;updatePortf(portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Port.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;daterange &amp;lt;- time(getPortfolio(portfolio.st)$summary)[-1]  # 获取交易日期范围&#xA;updateAcct(account.st, daterange)  # 更新账户数据至指定日期&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updateEndEq(account.st)  # 更新账户最终权益&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Acct.Luxor&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 提取交易统计数据&#xA;tstats &amp;lt;- t(tradeStats(Portfolios = portfolio.st))&#xA;&#xA;# 可视化回测结果：展示持仓变化与双均线&#xA;chart.Posn(&#xA;  Portfolio = portfolio.st, &#xA;  Symbol = &amp;quot;AAPL&amp;quot;, &#xA;  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日（红）均线&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2018/07/27/macross-stoptrailing/index_files/figure-html/simpleSMA-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>前进测试可视化策略评估的重要工具</title>
      <link>http://gewutang.cn/2017/10/10/luxor-sample-walk-forward/</link>
      <pubDate>Tue, 10 Oct 2017 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2017/10/10/luxor-sample-walk-forward/</guid>
      <description>&lt;p&gt;在量化交易中，理解指标的内涵与策略的稳健性同样重要。MACD（移动平均收敛散度）作为经典的趋势动量指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场的潜在转折与动量变化。&lt;/p&gt;&#xA;&lt;p&gt;MACD 由四个部分构成：快速 EMA（通常为 12 日）、慢速 EMA（通常为 26 日）、MACD 线（快速 EMA 与慢速 EMA 的差值）以及信号线（MACD 线的 9 日 EMA）。其主要用途包括：判断趋势方向（MACD 线在零轴上方为多头趋势，下方为空头趋势）、识别趋势强度（MACD 线与价格的背离暗示动量衰竭）、确认交易信号（信号线与 MACD 线的交叉或与零轴的交叉）。&lt;/p&gt;&#xA;&lt;p&gt;而前进测试（Walk Forward Testing）则是评估策略稳健性的关键方法，它通过将历史数据划分为连续的训练期与测试期，滚动优化参数并验证效果，有效降低过拟合风险。本文解析的代码便聚焦于前进测试结果的可视化，以直观呈现策略在训练期的表现，为策略评估提供依据。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;#&#xA;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#xA;#&#xA;# 前进测试图表示例&#xA;&#xA;# 加载quantstrat包，该包提供量化策略开发、回测及可视化工具&#xA;require(&amp;#39;quantstrat&amp;#39;)&#xA;&#xA;# 构建完整路径&#xA;data_path &amp;lt;- paste0(path.package(&amp;quot;quantstrat&amp;quot;), &amp;#39;/data/luxor.wfa.ples.RData&amp;#39;)&#xA;&#xA;# 加载数据文件&#xA;if (file.exists(data_path)) {&#xA;  load(data_path)  # 加载文件中的审计环境数据（如策略参数、绩效记录等）&#xA;  ls()  # 查看加载的对象（通常包含前进测试的审计信息）&#xA;}&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;data_path&amp;quot;  &amp;quot;luxoraudit&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 绘制前进测试训练期的图表&#xA;# 数据来源为quantstrat包内置的Luxor系统前进测试示例数据（luxor.wfa.ples.RData）&#xA;chart.forward.training(luxoraudit)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2017/10/10/luxor-sample-walk-forward/index_files/figure-html/WalkForward-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>MACD 趋势跟踪策略的实现与解析：基于 AAPL 的量化回测</title>
      <link>http://gewutang.cn/2017/07/10/macdrebalancing/</link>
      <pubDate>Mon, 10 Jul 2017 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2017/07/10/macdrebalancing/</guid>
      <description>&lt;p&gt;MACD（移动平均收敛散度）作为经典的趋势与动量指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场动量的变化。在实际交易中，MACD 的信号线与零轴的交叉常被视为趋势转折的信号 —— 上穿零轴暗示趋势由弱转强，下穿零轴则可能预示趋势走弱。&lt;/p&gt;&#xA;&lt;p&gt;本文将解析一段基于 R 语言quantstrat包的 MACD 策略代码，该代码以 AAPL 为标的，通过信号线穿越零轴生成交易信号，实现趋势跟踪，并展示量化策略从环境搭建到回测评估的完整流程。&lt;/p&gt;&#xA;&lt;div id=&#34;macd-指标的核心构成与策略逻辑&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;MACD 指标的核心构成与策略逻辑&lt;/h1&gt;&#xA;&lt;p&gt;在深入代码前，先明确 MACD 指标的组成与策略设计逻辑：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;快速 EMA（12 日）：基于收盘价计算的短期指数移动平均线，反映近期价格趋势。&lt;/li&gt;&#xA;&lt;li&gt;慢速 EMA（26 日）：长期指数移动平均线，反映中长期价格趋势。&lt;/li&gt;&#xA;&lt;li&gt;MACD 线：快速 EMA 与慢速 EMA 的差值，衡量短期与长期趋势的偏离程度。&lt;/li&gt;&#xA;&lt;li&gt;信号线：MACD 线的 9 日 EMA，用于平滑 MACD 线的波动，减少噪音信号。&lt;/li&gt;&#xA;&lt;li&gt;零轴：MACD 线与信号线围绕波动的基准线，代表多空力量的平衡点。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;本策略的核心逻辑是：当信号线从下方穿越零轴（上穿）时，认为多头趋势确立，触发买入信号；当信号线从上方穿越零轴（下穿）时，认为空头趋势显现，触发卖出信号，通过这种方式跟踪中长期趋势。&#xA;策略代码与详细注释&lt;/p&gt;&#xA;&lt;p&gt;以下是完整的策略代码，包含逐段注释以解释各环节的功能：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Simple MACD strategy&#xA;#&#xA;# MACD可通过多种方式使用，本策略展示其作为趋势指标的应用。&#xA;# &#xA;# 传统上，当MACD信号线穿越零时，标志着正向趋势的确立&#xA;#&#xA;# 本策略将在“signal”列上穿零轴时买入，下穿零轴时卖出&#xA;# &#xA;# Author: brian&#xA;###############################################################################&#xA;&#xA;&#xA;# 加载quantstrat包，用于量化策略开发与回测&#xA;require(quantstrat)&#xA;&#xA;# 清理旧策略数据，确保回测环境纯净（避免残留数据干扰结果）&#xA;suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, pos=.blotter))&#xA;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;))&#xA;&#xA;# 设定交易标的为苹果公司股票（AAPL）&#xA;stock.str &amp;lt;- &amp;#39;AAPL&amp;#39;&#xA;&#xA;# MACD指标参数：快速EMA周期、慢速EMA周期、信号线周期，均采用指数移动平均线（EMA）&#xA;fastMA = 12  # 快速EMA周期（传统参数为12日）&#xA;slowMA = 26  # 慢速EMA周期（传统参数为26日）&#xA;signalMA = 9  # 信号线周期（传统参数为9日）&#xA;maType = &amp;quot;EMA&amp;quot;  # 移动平均线类型为指数移动平均（EMA）&#xA;&#xA;# 定义货币单位为美元，并初始化标的属性（AAPL以美元计价，合约乘数为1）&#xA;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(stock.str, currency=&amp;#39;USD&amp;#39;, multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 回测参数设置：回测起始日期、初始资金、组合与账户名称&#xA;startDate = &amp;#39;2006-12-31&amp;#39;  # 数据起始日期&#xA;initEq = 1000000  # 初始资金100万美元&#xA;portfolio.st = &amp;#39;macd&amp;#39;  # 投资组合名称&#xA;account.st = &amp;#39;macd&amp;#39;  # 账户名称&#xA;&#xA;# 初始化回测环境：创建组合、账户与订单簿&#xA;initPortf(portfolio.st, symbols=stock.str)  # 初始化组合，包含AAPL&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio.st)  # 初始化账户，关联组合&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio=portfolio.st)  # 初始化订单簿，记录交易订单&#xA;&#xA;# 创建策略对象，命名为&amp;#39;macd&amp;#39;并存储到环境中&#xA;strat.st &amp;lt;- portfolio.st&#xA;strategy(strat.st, store=TRUE)&#xA;&#xA;# 添加MACD指标：基于收盘价计算MACD线、信号线&#xA;add.indicator(&#xA;  strat.st, &#xA;  name = &amp;quot;MACD&amp;quot;,  # 使用TTR包中的MACD函数&#xA;  arguments = list(x=quote(Cl(mktdata)))  # 输入为收盘价（Cl(mktdata)）&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 生成交易信号：基于信号线与零轴的交叉&#xA;# 信号1：信号线上穿零轴（大于零且交叉），视为买入信号&#xA;add.signal(&#xA;  strat.st,&#xA;  name = &amp;quot;sigThreshold&amp;quot;,  # 阈值型信号函数&#xA;  arguments = list(&#xA;    column = &amp;quot;signal.MACD.ind&amp;quot;,  # 作用于MACD指标的信号线列&#xA;    relationship = &amp;quot;gt&amp;quot;,  # 大于（greater than）&#xA;    threshold = 0,  # 阈值为0（零轴）&#xA;    cross = TRUE  # 必须穿越阈值（避免持续触发）&#xA;  ),&#xA;  label = &amp;quot;signal.gt.zero&amp;quot;  # 信号标签：上穿零轴&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 信号2：信号线下穿零轴（小于零且交叉），视为卖出信号&#xA;add.signal(&#xA;  strat.st,&#xA;  name = &amp;quot;sigThreshold&amp;quot;,&#xA;  arguments = list(&#xA;    column = &amp;quot;signal.MACD.ind&amp;quot;,&#xA;    relationship = &amp;quot;lt&amp;quot;,  # 小于（less than）&#xA;    threshold = 0,&#xA;    cross = TRUE&#xA;  ),&#xA;  label = &amp;quot;signal.lt.zero&amp;quot;  # 信号标签：下穿零轴&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义交易规则：将信号转化为具体交易动作&#xA;# 入场规则：当出现上穿零轴信号时，买入（做多）&#xA;add.rule(&#xA;  strat.st,&#xA;  name = &amp;#39;ruleSignal&amp;#39;,  # 基于信号的规则函数&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;signal.gt.zero&amp;quot;,  # 触发信号列：上穿零轴&#xA;    sigval = TRUE,  # 信号值为TRUE时触发&#xA;    orderqty = 1000000,  # 订单数量（此处为大额，实际会由osMaxPos限制）&#xA;    ordertype = &amp;#39;market&amp;#39;,  # 市价单&#xA;    orderside = &amp;#39;long&amp;#39;,  # 做多方向&#xA;    threshold = NULL,&#xA;    osFUN = &amp;#39;osMaxPos&amp;#39;  # 使用最大仓位限制函数，避免超仓&#xA;  ),&#xA;  type = &amp;#39;enter&amp;#39;,  # 入场规则&#xA;  label = &amp;#39;enter&amp;#39;&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 出场规则：当出现下穿零轴信号时，平仓（卖出所有持仓）&#xA;add.rule(&#xA;  strat.st,&#xA;  name = &amp;#39;ruleSignal&amp;#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;signal.lt.zero&amp;quot;,  # 触发信号列：下穿零轴&#xA;    sigval = TRUE,&#xA;    orderqty = &amp;#39;all&amp;#39;,  # 平仓全部持仓&#xA;    ordertype = &amp;#39;market&amp;#39;,&#xA;    orderside = &amp;#39;long&amp;#39;,&#xA;    threshold = NULL,&#xA;    orderset = &amp;#39;exit2&amp;#39;  # 订单集标签，用于管理出场订单&#xA;  ),&#xA;  type = &amp;#39;exit&amp;#39;,  # 出场规则&#xA;  label = &amp;#39;exit&amp;#39;&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 再平衡规则：按月调整仓位，控制单只标的的资金占比&#xA;add.rule(&#xA;  strat.st,&#xA;  &amp;#39;rulePctEquity&amp;#39;,  # 基于权益比例的再平衡函数&#xA;  arguments = list(&#xA;    rebalance_on = &amp;#39;months&amp;#39;,  # 再平衡周期：每月&#xA;    trade.percent = .02,  # 单只标的持仓占总权益的比例（2%）&#xA;    refprice = quote(last(getPrice(mktdata)[paste(&amp;#39;::&amp;#39;, curIndex, sep=&amp;#39;&amp;#39;)])),  # 参考价格为最新价格&#xA;    digits = 0  # 订单数量取整&#xA;  ),&#xA;  type = &amp;#39;rebalance&amp;#39;,  # 再平衡规则&#xA;  label = &amp;#39;rebalance&amp;#39;&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 获取历史数据：从雅虎财经下载AAPL的历史数据（起始日期为startDate）&#xA;getSymbols(stock.str, from=startDate, src=&amp;#39;yahoo&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 执行策略回测：应用再平衡策略&#xA;start_t &amp;lt;- Sys.time()  # 记录回测开始时间&#xA;out &amp;lt;- applyStrategy.rebalancing(&#xA;  strat.st, &#xA;  portfolios = portfolio.st,&#xA;  parameters = list(nFast=fastMA, nSlow=slowMA, nSig=signalMA, maType=maType),  # 传递MACD参数&#xA;  verbose = TRUE  # 输出详细过程&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -6287 @ 3.25607109069824&amp;quot;&#xA;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 6330 @ 3.62928605079651&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -6330 @ 6.93464279174805&amp;quot;&#xA;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 2892 @ 7.46785688400269&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 2251 @ 9.35464286804199&amp;quot;&#xA;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -2251 @ 8.99892902374268&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 1662 @ 12.4357137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -1662 @ 11.9428567886353&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -1481 @ 19.6967868804932&amp;quot;&#xA;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 1053 @ 19.5025005340576&amp;quot;&#xA;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -1053 @ 17.8839282989502&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -1121 @ 18.498571395874&amp;quot;&#xA;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 1099 @ 21.1546421051025&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-10-17 00:00:00 AAPL -1099 @ 24.4174995422363&amp;quot;&#xA;## [1] &amp;quot;2014-10-27 00:00:00 AAPL 839 @ 26.2775001525879&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-04-02 00:00:00 AAPL -478 @ 41.6699981689453&amp;quot;&#xA;## [1] &amp;quot;2018-04-19 00:00:00 AAPL 507 @ 43.2000007629395&amp;quot;&#xA;## [1] &amp;quot;2018-04-25 00:00:00 AAPL -507 @ 40.9124984741211&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2018-07-06 00:00:00 AAPL -514 @ 46.9925003051758&amp;quot;&#xA;## [1] &amp;quot;2018-07-11 00:00:00 AAPL 459 @ 46.9700012207031&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-11-03 00:00:00 AAPL -189 @ 110.440002441406&amp;quot;&#xA;## [1] &amp;quot;2020-11-16 00:00:00 AAPL 201 @ 120.300003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2022-02-10 00:00:00 AAPL 125 @ 172.119995117188&amp;quot;&#xA;## [1] &amp;quot;2022-02-24 00:00:00 AAPL -125 @ 162.740005493164&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-01-10 00:00:00 AAPL -128 @ 186.190002441406&amp;quot;&#xA;## [1] &amp;quot;2024-01-30 00:00:00 AAPL 114 @ 188.039993286133&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-08-14 00:00:00 AAPL -129 @ 221.720001220703&amp;quot;&#xA;## [1] &amp;quot;2024-08-16 00:00:00 AAPL 99 @ 226.050003051758&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2024-11-11 00:00:00 AAPL -99 @ 224.229995727539&amp;quot;&#xA;## [1] &amp;quot;2024-11-27 00:00:00 AAPL 97 @ 234.929992675781&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&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;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()  # 记录回测结束时间&#xA;print(end_t - start_t)  # 打印回测耗时&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 8.527371 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 更新组合数据：将交易记录同步到组合与账户&#xA;start_t &amp;lt;- Sys.time()&#xA;updatePortf(&#xA;  Portfolio = portfolio.st,&#xA;  Dates = paste(&amp;#39;::&amp;#39;, as.Date(Sys.time()), sep=&amp;#39;&amp;#39;)  # 更新至当前日期&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()&#xA;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;print(end_t - start_t)  # 打印更新耗时&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 0.03819585 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 可视化回测结果：绘制持仓变化与MACD指标&#xA;chart.Posn(Portfolio = portfolio.st, Symbol = stock.str)  # 展示AAPL的持仓变化&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2017/07/10/macdrebalancing/index_files/figure-html/SimpleMACD-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于双均线交叉的交易策略实现与回测分析</title>
      <link>http://gewutang.cn/2017/05/12/macross/</link>
      <pubDate>Fri, 12 May 2017 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2017/05/12/macross/</guid>
      <description>&lt;p&gt;在量化交易领域，移动均线交叉策略是一种被广泛应用的趋势跟踪方法。本&#xA;文将解析一段基于 R 语言 quantstrat 包的简单移动均线交叉策略代码，展示其实现思路、核心逻辑及回测分析过程。&lt;/p&gt;&#xA;&lt;div id=&#34;策略核心思想&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;策略核心思想&lt;/h1&gt;&#xA;&lt;p&gt;这段代码实现了一个经典的双均线交叉策略，其基本逻辑是通过比较短期均线与长期均线的相对位置来生成交易信号： - 当短期均线（50 日 SMA）上穿长期均线（200 日 SMA）时，产生买入信号 - 当短期均线（50 日 SMA）下穿长期均线（200 日 SMA）时，产生卖出信号&lt;/p&gt;&#xA;&lt;p&gt;策略默认仅做多，代码中也预留了做空逻辑的实现方式&lt;/p&gt;&#xA;&lt;p&gt;这种策略的理论基础在于，短期均线反映近期价格趋势，长期均线反映长期价格趋势，当短期均线从下方穿越长期均线时，通常被视为上升趋势的开始；反之则被视为下降趋势的开始。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;策略实现的关键步骤&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;策略实现的关键步骤&lt;/h1&gt;&#xA;&lt;p&gt;下面是代码的关键实现部分及解析：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载必要的包&#xA;require(quantstrat)&#xA;&#xA;# 环境初始化&#xA;ttz&amp;lt;-Sys.getenv(&amp;#39;TZ&amp;#39;)&#xA;Sys.setenv(TZ=&amp;#39;UTC&amp;#39;)  # 时区设置，避免日期处理问题&#xA;&#xA;# 清理旧策略数据&#xA;suppressWarnings(rm(&amp;quot;order_book.macross&amp;quot;,pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.macross&amp;quot;,&amp;quot;portfolio.macross&amp;quot;,pos=.blotter))&#xA;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;))&#xA;&#xA;# 设置交易标的和货币&#xA;stock.str=&amp;#39;AAPL&amp;#39;  # 以苹果公司股票为例&#xA;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(stock.str,currency=&amp;#39;USD&amp;#39;,multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 初始化回测参数&#xA;startDate=&amp;quot;1999-12-31&amp;quot;  # 回测起始日期&#xA;initEq=1000000          # 初始资金100万美元&#xA;portfolio.st=&amp;#39;macross&amp;#39;  # 投资组合名称&#xA;account.st=&amp;#39;macross&amp;#39;    # 账户名称&#xA;&#xA;# 初始化投资组合、账户和订单簿&#xA;initPortf(portfolio.st,symbols=stock.str)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st,portfolios=portfolio.st, initEq=initEq)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio=portfolio.st)&#xA;&#xA;# 创建策略对象&#xA;stratMACROSS&amp;lt;- strategy(portfolio.st)&#xA;&#xA;# 添加技术指标：50日和200日简单移动平均线&#xA;stratMACROSS &amp;lt;- add.indicator(strategy = stratMACROSS, name = &amp;quot;SMA&amp;quot;, &#xA;                              arguments = list(x=quote(Cl(mktdata)), n=50),&#xA;                              label= &amp;quot;ma50&amp;quot; )&#xA;stratMACROSS &amp;lt;- add.indicator(strategy = stratMACROSS, name = &amp;quot;SMA&amp;quot;, &#xA;                              arguments = list(x=quote(Cl(mktdata)[,1]), n=200),&#xA;                              label= &amp;quot;ma200&amp;quot;)&#xA;&#xA;# 添加交易信号：均线交叉信号&#xA;stratMACROSS &amp;lt;- add.signal(strategy = stratMACROSS, name=&amp;quot;sigCrossover&amp;quot;,&#xA;                          arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;), relationship=&amp;quot;gte&amp;quot;),&#xA;                          label=&amp;quot;ma50.gt.ma200&amp;quot;)  # 短期均线上穿长期均线&#xA;stratMACROSS &amp;lt;- add.signal(strategy = stratMACROSS, name=&amp;quot;sigCrossover&amp;quot;,&#xA;                          arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;), relationship=&amp;quot;lt&amp;quot;),&#xA;                          label=&amp;quot;ma50.lt.ma200&amp;quot;)  # 短期均线下穿长期均线&#xA;&#xA;# 添加交易规则：基于信号的交易执行&#xA;stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                        arguments = list(sigcol=&amp;quot;ma50.gt.ma200&amp;quot;, sigval=TRUE, &#xA;                                        orderqty=100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;long&amp;#39;),&#xA;                        type=&amp;#39;enter&amp;#39;)  # 买入规则&#xA;stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                        arguments = list(sigcol=&amp;quot;ma50.lt.ma200&amp;quot;, sigval=TRUE, &#xA;                                        orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;long&amp;#39;),&#xA;                        type=&amp;#39;exit&amp;#39;)   # 卖出规则&#xA;&#xA;# 预留的做空策略代码（默认未启用）&#xA;# stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#xA;#                         arguments = list(sigcol=&amp;quot;ma50.lt.ma200&amp;quot;, sigval=TRUE, &#xA;#                                         orderqty=-100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;short&amp;#39;),&#xA;#                         type=&amp;#39;enter&amp;#39;)&#xA;# stratMACROSS &amp;lt;- add.rule(strategy = stratMACROSS, name=&amp;#39;ruleSignal&amp;#39;, &#xA;#                         arguments = list(sigcol=&amp;quot;ma50.gt.ma200&amp;quot;, sigval=TRUE, &#xA;#                                         orderqty=100, ordertype=&amp;#39;market&amp;#39;, orderside=&amp;#39;short&amp;#39;),&#xA;#                         type=&amp;#39;exit&amp;#39;)&#xA;&#xA;# 获取历史数据并进行复权处理&#xA;getSymbols(stock.str, from=startDate, src=&amp;#39;yahoo&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;for(i in stock.str)&#xA;  assign(i, adjustOHLC(get(i), use.Adjusted=TRUE))&#xA;&#xA;# 执行策略回测&#xA;start_t&amp;lt;-Sys.time()&#xA;out&amp;lt;-applyStrategy(strategy=stratMACROSS, portfolios=portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2001-06-27 00:00:00 AAPL 100 @ 0.350732564926147&amp;quot;&#xA;## [1] &amp;quot;2001-09-07 00:00:00 AAPL -100 @ 0.259667813777924&amp;quot;&#xA;## [1] &amp;quot;2002-01-07 00:00:00 AAPL 100 @ 0.344120860099792&amp;quot;&#xA;## [1] &amp;quot;2002-07-10 00:00:00 AAPL -100 @ 0.260269552469254&amp;quot;&#xA;## [1] &amp;quot;2003-05-16 00:00:00 AAPL 100 @ 0.28250914812088&amp;quot;&#xA;## [1] &amp;quot;2006-06-22 00:00:00 AAPL -100 @ 1.79062843322754&amp;quot;&#xA;## [1] &amp;quot;2006-09-26 00:00:00 AAPL 100 @ 2.33250570297241&amp;quot;&#xA;## [1] &amp;quot;2008-03-07 00:00:00 AAPL -100 @ 3.67412352561951&amp;quot;&#xA;## [1] &amp;quot;2008-05-19 00:00:00 AAPL 100 @ 5.51794862747192&amp;quot;&#xA;## [1] &amp;quot;2008-09-24 00:00:00 AAPL -100 @ 3.8682746887207&amp;quot;&#xA;## [1] &amp;quot;2009-05-14 00:00:00 AAPL 100 @ 3.69516229629517&amp;quot;&#xA;## [1] &amp;quot;2012-12-11 00:00:00 AAPL -100 @ 16.41552734375&amp;quot;&#xA;## [1] &amp;quot;2013-09-11 00:00:00 AAPL 100 @ 14.4533443450928&amp;quot;&#xA;## [1] &amp;quot;2015-08-31 00:00:00 AAPL -100 @ 25.3688125610352&amp;quot;&#xA;## [1] &amp;quot;2016-08-31 00:00:00 AAPL 100 @ 24.3808135986328&amp;quot;&#xA;## [1] &amp;quot;2018-12-24 00:00:00 AAPL -100 @ 34.9761238098145&amp;quot;&#xA;## [1] &amp;quot;2019-05-07 00:00:00 AAPL 100 @ 48.5301742553711&amp;quot;&#xA;## [1] &amp;quot;2022-06-06 00:00:00 AAPL -100 @ 143.832458496094&amp;quot;&#xA;## [1] &amp;quot;2022-09-27 00:00:00 AAPL 100 @ 149.571166992188&amp;quot;&#xA;## [1] &amp;quot;2022-10-10 00:00:00 AAPL -100 @ 138.394744873047&amp;quot;&#xA;## [1] &amp;quot;2023-03-23 00:00:00 AAPL 100 @ 157.137161254883&amp;quot;&#xA;## [1] &amp;quot;2024-03-15 00:00:00 AAPL -100 @ 171.583724975586&amp;quot;&#xA;## [1] &amp;quot;2024-06-14 00:00:00 AAPL 100 @ 211.500885009766&amp;quot;&#xA;## [1] &amp;quot;2025-04-08 00:00:00 AAPL -100 @ 172.194198608398&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t&amp;lt;-Sys.time()&#xA;print(end_t-start_t)  # 输出回测执行时间&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 0.2876902 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 更新投资组合数据&#xA;start_t&amp;lt;-Sys.time()&#xA;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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t&amp;lt;-Sys.time()&#xA;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;print(end_t-start_t)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 0.04611683 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 可视化回测结果&#xA;chart.Posn(Portfolio=&amp;#39;macross&amp;#39;, Symbol=stock.str)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2017/05/12/macross/index_files/figure-html/DualSMA-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title></title>
      <link>http://gewutang.cn/about/</link>
      <pubDate>Thu, 05 May 2016 21:48:51 -0700</pubDate>
      <guid>http://gewutang.cn/about/</guid>
      <description>&lt;h2 id=&#34;关于我&#34;&gt;关于我&lt;/h2&gt;&#xA;&lt;p&gt;前金融业高管，现今游走于投资、读书和美食之间，偶尔写随笔。喜欢维修宇宙和抚慰人心；&#xA;喜欢简单；喜欢活力和宁静之间的平衡。&lt;/p&gt;&#xA;&lt;h3 id=&#34;教育经历&#34;&gt;教育经历&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;2006年-2010年，求学于中央财经大学数学与统计学院。&lt;/li&gt;&#xA;&lt;li&gt;2019年-2021年，求学于北京大学光华管理学院。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;工作经历&#34;&gt;工作经历&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;2010年-2017年，先后就职于首钢集团、Dawanjia Inc.等。&lt;/li&gt;&#xA;&lt;li&gt;2017年-，先后就职于华睿创盈投资管理有限公司、润安国际保险经纪及字节跳动。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;社会经历&#34;&gt;社会经历&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;2010年-2020年，R 语言专家，统计之都理事会成员，R 沙龙理事会成员。&lt;/li&gt;&#xA;&lt;li&gt;2015年-，创立响马读书（微信公众号：&lt;em&gt;i响马读书&lt;/em&gt;）。&lt;/li&gt;&#xA;&lt;li&gt;2020年-，R 语言/量化投资专家，创立&lt;a href=&#34;https://rfinance.org.cn&#34;&gt;R-Finance中国&lt;/a&gt;和 &lt;a href=&#34;https://x-quant.com.cn&#34;&gt;X-Quant&lt;/a&gt;。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;出版书籍&#34;&gt;出版书籍&lt;/h3&gt;&#xA;&lt;h4 id=&#34;已出版&#34;&gt;已出版&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;《R核心技术手册》（译者）&lt;/li&gt;&#xA;&lt;li&gt;《R数据可视化手册》（译者）&lt;/li&gt;&#xA;&lt;li&gt;《R语言统计入门》（译者）&lt;/li&gt;&#xA;&lt;li&gt;《量化交易与R语言》（译者）&lt;/li&gt;&#xA;&lt;li&gt;《金融风险建模及投资组合优化：使用R语言》（译者）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;撰写中&#34;&gt;撰写中&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;《AI时代R与技术分析全景应用指南》（作者）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;计划内&#34;&gt;计划内&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;《AI时代R与蜡烛图分析全景应用指南》（作者）&lt;/li&gt;&#xA;&lt;li&gt;《AI时代R与量化投资全景应用指南》（作者）&lt;/li&gt;&#xA;&lt;li&gt;《AI时代R与金融数据可视化全景应用指南》（作者）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;软件包&#34;&gt;软件包&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://github.com/dengyishuo/eTTR&#34;&gt;eTTR&lt;/a&gt; 包&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://github.com/dengyishuo/eTushare&#34;&gt;eTushare&lt;/a&gt;包&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title></title>
      <link>http://gewutang.cn/home/</link>
      <pubDate>Thu, 05 May 2016 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/home/</guid>
      <description>&lt;center&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/images/avatar.png&#34; width=&#34;100px&#34; height=&#34;100px&#34;&gt;&#xA;&lt;/center&gt;&#xA;&lt;p&gt;欢迎来访！我是&lt;strong&gt;MatrixSpk&lt;/strong&gt;，这是我的小窝。我在经历了读书、工作、创业、跳回大厂等一&#xA;系列事情之后，似乎又回到了从前。现在我作为一个经历过支付、私募、券商、保险经纪等&#xA;业务的前金融高管，热爱的事情终于确定，那就是游走在读书、投资、跑步和美食之间，&#xA;偶尔写随笔。我喜欢维修宇宙，喜欢抚慰人心，喜欢简单，喜欢活力和宁静之间的平衡。&lt;/p&gt;&#xA;&lt;p&gt;更多关于我的信息，请猛戳&lt;a href=&#34;https://gewutang.cn/about&#34;&gt;关于我&lt;/a&gt;。&lt;/p&gt;</description>
    </item>
    <item>
      <title>使用blotter包绘制最大不利偏移（MAE）图</title>
      <link>http://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/</link>
      <pubDate>Wed, 10 Feb 2016 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/</guid>
      <description>&lt;p&gt;在交易过程中，MAE（最大不利偏移）是衡量风险的关键指标，它反映了从开仓到平仓期间账户价值相对于初始水平的最大回撤幅度。这一指标对于交易者而言具有重要意义：首先，它能直观呈现单笔交易可能面临的极端亏损风险，帮助交易者提前做好风险预判；其次，在止损策略优化方面，可将 MAE 的统计数据作为重要参考，例如将止损位设定在历史 MAE 均值的合理倍数之上，既能避免过早止损，又能有效控制亏损；最后，通过观察 MAE 的波动情况，还能评估交易系统的稳定性 —— 通常而言，MAE 曲线越平滑，说明系统在不同市场环境下的表现越稳定，抗风险能力越强。&lt;/p&gt;&#xA;&lt;p&gt;Jaekle 和 Tamasini 在《A new approach to system development and portfolio optimisation》一书中，通过 Luxor 系统的实证分析进一步印证了 MAE 的应用价值。书中，作者通过绘制该系统的 MAE 图表，清晰展示了策略在不同市场周期中的风险暴露特征，为读者提供了一个将理论指标转化为实践分析的典型案例，这种可视化的呈现方式有助于交易者更直观地理解风险分布规律，进而优化自身的交易决策体系。&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;R&lt;/code&gt; 可以便捷绘制 MAE 图：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载 blotter 包 - 用于交易系统分析和绩效评估&#xA;require(&amp;#39;blotter&amp;#39;)&#xA;&#xA;# 加载 quantstrat 包中的示例数据 &amp;#39;luxor-p066&amp;#39;（这是一个交易策略的历史回测结果）&#xA;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#xA;&#xA;# 设置货币和汇率信息（GBP 代表英镑，USD 代表美元）&#xA;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;exchange_rate(c(&amp;#39;GBPUSD&amp;#39;), tick_size=0.0001)  # 设定 GBP/USD 汇率的最小变动单位&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 绘制最大不利偏移（MAE）图表&#xA;chart.ME(&#xA;  Portfolio=&amp;#39;luxor&amp;#39;,  # 指定要分析的投资组合名称为 &amp;#39;luxor&amp;#39;&#xA;  Symbol=&amp;#39;GBPUSD&amp;#39;,    # 指定要分析的交易品种为 GBP/USD&#xA;  type=&amp;#39;MAE&amp;#39;,         # 选择绘制 MAE（Maximum Adverse Excursion）图表&#xA;  scale=&amp;#39;cash&amp;#39;        # 以货币金额（现金）为单位显示结果&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;div class=&#34;figure&#34;&gt;&lt;span style=&#34;display:block;&#34; id=&#34;fig:MAE&#34;&gt;&lt;/span&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2016/02/10/luxor-sample-mae-stoploss/index_files/figure-html/MAE-1.png&#34; alt=&#34;luxor 系统的 MAE 图&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p class=&#34;caption&#34;&gt;&#xA;Figure 1: luxor 系统的 MAE 图&#xA;&lt;/p&gt;</description>
    </item>
    <item>
      <title>交易策略的 MFE 分析与可视化：评估盈利潜力的关键工具</title>
      <link>http://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/</link>
      <pubDate>Tue, 12 Jan 2016 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/</guid>
      <description>&lt;p&gt;在量化交易策略评估中，最大有利波动（MFE, Maximum Favorable Excursion）是衡量单笔交易潜在盈利空间的重要指标。它代表自交易入场后，价格朝着有利方向波动的最大幅度，能直观反映策略在理想情况下的盈利捕捉能力。&lt;/p&gt;&#xA;&lt;p&gt;通过分析 MFE，交易者可以评估当前止盈策略是否充分捕获价格波动收益，识别交易时机是否精准，甚至为动态止盈参数的设定提供依据。这段代码正是基于这一原理，通过可视化 GBPUSD 货币对的 MFE 数据，帮助交易者理解 Luxor 策略在不同交易中的盈利潜力。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#xA;#&#xA;# 图3.16：百分比形式的MFE（最大有利波动）图形&#xA;&#xA;# 加载blotter包，用于交易组合的记录与绩效分析&#xA;require(&amp;#39;blotter&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载Luxor策略的交易数据（对应书中第66页的示例）&#xA;# envir=.blotter参数将数据加载到blotter包的环境中，便于后续分析&#xA;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#xA;&#xA;# 定义货币单位与汇率属性：设置交易涉及的货币及GBPUSD的最小变动单位&#xA;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;))  # 定义英镑和美元为交易货币&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 绘制MFE图表：以百分比形式展示每笔交易的最大有利波动&#xA;chart.ME(&#xA;  Portfolio=&amp;#39;luxor&amp;#39;,  # 指定要分析的投资组合名称&#xA;  Symbol=&amp;#39;GBPUSD&amp;#39;,    # 指定要分析的交易品种（GBPUSD货币对）&#xA;  type=&amp;#39;MFE&amp;#39;,         # 指定图表类型为MFE（最大有利波动）&#xA;  scale=&amp;#39;percent&amp;#39;     # 指定Y轴刻度为百分比形式（相对于入场价格的波动比例）&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2016/01/12/luxor-sample-mae-stop-trailing/index_files/figure-html/MFE-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于 MFE 指标的交易策略绩效分析：GBP/USD 交易的最大有利波动可视化</title>
      <link>http://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/</link>
      <pubDate>Thu, 10 Dec 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/</guid>
      <description>&lt;p&gt;在量化交易中，评估单笔交易的潜在盈利空间与风险控制能力是优化策略的重要环节。最大有利波动（MFE，Maximum Favorable Excursion）作为衡量交易过程中价格对持仓最有利波动程度的指标，能够帮助交易者理解策略在盈利阶段的表现 —— 例如，单笔交易曾达到的最大浮盈比例，以及该比例与最终盈利的关系。这段代码的核心思路便是通过可视化 GBPUSD 交易的 MFE 指标（以百分比形式），直观展示 Luxor 策略在交易过程中的潜在盈利空间，为评估策略的止盈设置、盈利捕捉能力提供依据。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#xA;#&#xA;# 图3.16：百分比形式的MFE（最大有利波动）图形&#xA;&#xA;# 加载blotter包，用于交易组合的记录与绩效分析&#xA;require(&amp;#39;blotter&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载Luxor策略的交易数据（对应书中第66页的示例），存储到blotter环境中&#xA;# 数据包含GBPUSD的交易记录，如入场时间、价格、持仓变化等&#xA;data(&amp;#39;luxor-p066&amp;#39;, package=&amp;#39;quantstrat&amp;#39;, envir=.blotter)&#xA;&#xA;# 定义货币单位：GBP（英镑）和USD（美元），因交易标的为GBPUSD（英镑兑美元）&#xA;currency(c(&amp;#39;GBP&amp;#39;, &amp;#39;USD&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBP&amp;quot; &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义汇率属性：GBPUSD的最小变动单位（点差）为0.0001&#xA;exchange_rate(c(&amp;#39;GBPUSD&amp;#39;), tick_size=0.0001)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GBPUSD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 绘制MFE图形（百分比形式）&#xA;chart.ME(&#xA;  Portfolio=&amp;#39;luxor&amp;#39;,  # 组合名称：luxor（对应加载的数据）&#xA;  Symbol=&amp;#39;GBPUSD&amp;#39;,    # 交易标的：英镑兑美元&#xA;  type=&amp;#39;MFE&amp;#39;,         # 图形类型：最大有利波动（MFE）&#xA;  scale=&amp;#39;percent&amp;#39;     # 刻度单位：百分比（展示价格波动相对于入场价的百分比）&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/12/10/luxor-sample-mfe-takeprofit/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>日内时间窗口绩效分析与可视化</title>
      <link>http://gewutang.cn/2015/10/10/luxor-sample-trade-graphs-timespan/</link>
      <pubDate>Sat, 10 Oct 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/10/10/luxor-sample-trade-graphs-timespan/</guid>
      <description>&lt;p&gt;在量化交易中，分析交易策略在不同时间窗口的绩效表现至关重要，因为金融市场具有显著的日内周期性特征。不同时段的市场参与者结构、流动性水平、信息传递效率差异明显，导致同一策略在日内不同时间段的表现可能大相径庭。例如，美股市场开盘后的 30 分钟通常伴随着高波动率和大量订单流，趋势策略可能在此期间更有效；而午盘时段交投清淡，震荡策略或许更适用。通过系统地扫描不同时间窗口的绩效，交易者可以识别策略的 “最佳生效时段”，优化交易执行时间，减少无效信号干扰，提高资金使用效率。&lt;/p&gt;&#xA;&lt;p&gt;此外，这种分析还能揭示策略的潜在弱点，例如仅在特定时段有效的策略可能存在过拟合风险，或对特定市场环境过度依赖，为策略的改进与风险控制提供依据。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 基于Jaekle &amp;amp; Tamasini所著《A new approach to system development and portfolio optimisation》&#xA;#&#xA;# 3D时间跨度图表示例&#xA;&#xA;# 加载必要的包：quantstrat用于量化策略分析，rgl用于3D图形绘制&#xA;require(quantstrat)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;require(rgl)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: rgl&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载Luxor策略的时间跨度数据：包含2002-2008年24x24时间窗口的测试结果&#xA;# 数据路径为quantstrat包安装目录下的data文件夹&#xA;load(paste0(&#xA;        path.package(&amp;#39;quantstrat&amp;#39;),  # 获取quantstrat包的安装路径&#xA;        &amp;#39;/data/luxor.timespan.24x24.2002-2008.RData&amp;#39;)  # 拼接数据文件完整路径&#xA;)&#xA;&#xA;# 数据预处理：将stats数据框中存储参数的列重命名为&amp;quot;timespan&amp;quot;，便于后续处理&#xA;names(stats)[names(stats)==&amp;#39;testPackListPRL[[k]]$parameters&amp;#39;] &amp;lt;- &amp;#39;timespan&amp;#39;&#xA;&#xA;# 分割timespan列：timespan格式可能为&amp;quot;开始时间/结束时间&amp;quot;，用strsplit分割为列表&#xA;stats$tmp = strsplit(as.character(stats$timespan), &amp;#39;/&amp;#39;)&#xA;&#xA;# 从分割后的列表中提取&amp;quot;from&amp;quot;（开始时间）和&amp;quot;to&amp;quot;（结束时间），存储为新列&#xA;stats$from &amp;lt;- sapply(stats$tmp, FUN = &amp;#39;[&amp;#39;, 1)  # 提取列表中第一个元素作为开始时间&#xA;stats$to &amp;lt;- sapply(stats$tmp, FUN = &amp;#39;[&amp;#39;, 2)    # 提取列表中第二个元素作为结束时间&#xA;&#xA;# 从开始时间和结束时间中提取小时数：假设时间格式含&amp;quot;T小时:分钟&amp;quot;，用gsub匹配并提取小时&#xA;stats$start &amp;lt;- as.numeric(gsub(&amp;#39;T([0-9]+):[0-9]+&amp;#39;, x = stats$from, &amp;#39;\\1&amp;#39;))  # 提取开始小时&#xA;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范围内）&#xA;&#xA;# 注释部分：交易数据为EST时区（GMT-4），可将小时数加4转换为GMT时区（按需启用）&#xA;# stats$start&amp;lt;-(stats$start+4)%%24&#xA;# stats$stop&amp;lt;-(stats$stop+4)%%24&#xA;&#xA;# 生成3D可视化图形：展示不同时间窗口（start和stop）与绩效指标的关系&#xA;tradeGraphs(&#xA;&#x9;stats,  # 输入数据框&#xA;&#x9;free.params = c(&amp;#39;start&amp;#39;, &amp;#39;stop&amp;#39;),  # 自由参数：时间窗口的开始和结束小时&#xA;&#x9;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;),  # 需展示的绩效指标：净交易收益、最大回撤、平均每笔收益、交易次数、盈利因子&#xA;&#x9;title = &amp;#39;Luxor Intraday TimeWindow Scan&amp;#39;  # 图形标题：Luxor日内时间窗口扫描&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;这段代码的核心思路是通过量化分析与 3D 可视化，探究 Luxor 日内交易策略在不同时间窗口（即日内交易的开始与结束小时）下的绩效表现。&lt;/p&gt;</description>
    </item>
    <item>
      <title>MACD 指标解析与策略实现：基于 AAPL 的趋势跟踪逻辑</title>
      <link>http://gewutang.cn/2015/08/10/macd/</link>
      <pubDate>Mon, 10 Aug 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/08/10/macd/</guid>
      <description>&lt;p&gt;MACD（移动平均收敛散度）是量化交易中经典的趋势与动量结合指标，其核心价值在于通过短期与长期价格趋势的差异，捕捉市场动量变化与潜在趋势转折。&lt;/p&gt;&#xA;&lt;p&gt;MACD 的构成包括四个部分：快速 EMA（指数移动平均线，通常为 12 日）、慢速 EMA（通常为 26 日）、MACD 线（快速 EMA 与慢速 EMA 的差值）、信号线（MACD 线的 9 日 EMA，用于平滑波动）。在实际应用中，MACD 的主要用途包括：判断趋势方向（MACD 线在零轴上方为多头趋势，下方为空头趋势）、识别动量变化（MACD 线与价格的背离暗示趋势可能反转）、确认趋势确立（信号线穿越零轴标志趋势形成）。&lt;/p&gt;&#xA;&lt;p&gt;本文解析的代码基于 quantstrat 包实现了一套简单的 MACD 趋势跟踪策略，以 AAPL 为标的，核心逻辑为：当信号线从下方穿越零轴（上穿）时，视为多头趋势确立，触发买入信号；当信号线从上方穿越零轴（下穿）时，视为趋势转弱，触发卖出信号，通过这种方式跟踪中长期趋势并捕获收益。&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Simple MACD strategy&#xA;#&#xA;# MACD可通过多种方式应用，本策略展示其作为趋势指标的用法。&#xA;# &#xA;# 传统上，当MACD信号线穿越零时，标志着正向趋势的确立。&#xA;#&#xA;# 本策略将在信号线&amp;quot;signal&amp;quot;上穿零轴时买入，下穿零轴时卖出。&#xA;# &#xA;# Author: brian&#xA;###############################################################################&#xA;&#xA;&#xA;# 加载quantstrat包，用于量化策略开发、回测与绩效分析&#xA;require(quantstrat)&#xA;&#xA;# 清理旧策略数据，确保回测环境纯净（避免残留的订单、账户数据干扰结果）&#xA;suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, pos=.blotter))&#xA;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;))&#xA;&#xA;# 处理时区问题：若当前时区未设置，默认设为GMT，避免日期处理异常&#xA;oldtz &amp;lt;- Sys.getenv(&amp;#39;TZ&amp;#39;)&#xA;if (oldtz == &amp;#39;&amp;#39;) {&#xA;    Sys.setenv(TZ = &amp;quot;GMT&amp;quot;)&#xA;}&#xA;&#xA;&#xA;# 设定交易标的为苹果公司股票（AAPL）&#xA;stock.str &amp;lt;- &amp;#39;AAPL&amp;#39;&#xA;&#xA;# MACD指标参数设置&#xA;fastMA = 12  # 快速EMA周期（传统参数12日）&#xA;slowMA = 26  # 慢速EMA周期（传统参数26日）&#xA;signalMA = 9  # 信号线周期（传统参数9日）&#xA;maType = &amp;quot;EMA&amp;quot;  # 均线类型为指数移动平均（EMA）&#xA;&#xA;# 定义货币单位与标的属性：AAPL以美元计价，合约乘数为1（股票交易通常乘数为1）&#xA;currency(&amp;#39;USD&amp;#39;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(stock.str, currency = &amp;#39;USD&amp;#39;, multiplier = 1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 回测基础参数&#xA;startDate = &amp;#39;2006-12-31&amp;#39;  # 数据起始日期&#xA;initEq = 1000000  # 初始资金100万美元&#xA;portfolio.st = &amp;#39;macd&amp;#39;  # 投资组合名称&#xA;account.st = &amp;#39;macd&amp;#39;  # 账户名称&#xA;&#xA;# 初始化回测环境&#xA;initPortf(portfolio.st, symbols = stock.str)  # 创建包含AAPL的投资组合&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st, portfolios = portfolio.st)  # 初始化账户，关联投资组合&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio = portfolio.st)  # 初始化订单簿，记录交易订单&#xA;&#xA;# 定义策略对象，名称与组合一致，并存入环境&#xA;strat.st &amp;lt;- portfolio.st&#xA;strategy(strat.st, store = TRUE)&#xA;&#xA;# 添加MACD指标：基于收盘价计算，参数为快速EMA、慢速EMA&#xA;add.indicator(&#xA;  strat.st, &#xA;  name = &amp;quot;MACD&amp;quot;,  # 使用TTR包中的MACD函数&#xA;  arguments = list(&#xA;    x = quote(Cl(mktdata)),  # 输入为收盘价（Cl(mktdata)）&#xA;    nFast = fastMA,  # 快速EMA周期12日&#xA;    nSlow = slowMA  # 慢速EMA周期26日&#xA;  ),&#xA;  label = &amp;#39;_&amp;#39;  # 指标标签，用于后续信号引用&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 生成交易信号：基于信号线与零轴的交叉&#xA;# 信号1：信号线上穿零轴（大于零且交叉），视为买入信号&#xA;add.signal(&#xA;  strat.st,&#xA;  name = &amp;quot;sigThreshold&amp;quot;,  # 阈值型信号函数&#xA;  arguments = list(&#xA;    column = &amp;quot;signal._&amp;quot;,  # 引用MACD指标中的信号线（因指标标签为&amp;#39;_&amp;#39;，故列名为signal._）&#xA;    relationship = &amp;quot;gt&amp;quot;,  # 大于（greater than）&#xA;    threshold = 0,  # 阈值为0（零轴）&#xA;    cross = TRUE  # 必须穿越阈值（避免持续触发）&#xA;  ),&#xA;  label = &amp;quot;signal.gt.zero&amp;quot;  # 信号标签：上穿零轴&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 信号2：信号线下穿零轴（小于零且交叉），视为卖出信号&#xA;add.signal(&#xA;  strat.st,&#xA;  name = &amp;quot;sigThreshold&amp;quot;,&#xA;  arguments = list(&#xA;    column = &amp;quot;signal._&amp;quot;,&#xA;    relationship = &amp;quot;lt&amp;quot;,  # 小于（less than）&#xA;    threshold = 0,&#xA;    cross = TRUE&#xA;  ),&#xA;  label = &amp;quot;signal.lt.zero&amp;quot;  # 信号标签：下穿零轴&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义交易规则：将信号转化为具体交易动作&#xA;####&#xA;# 入场规则：当买入信号触发时，以市价单买入100股&#xA;add.rule(&#xA;  strat.st,&#xA;  name = &amp;#39;ruleSignal&amp;#39;,  # 基于信号的规则函数&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;signal.gt.zero&amp;quot;,  # 触发信号列：上穿零轴&#xA;    sigval = TRUE,  # 信号值为TRUE时执行&#xA;    orderqty = 100,  # 订单数量：100股&#xA;    ordertype = &amp;#39;market&amp;#39;,  # 市价单（确保及时入场）&#xA;    orderside = &amp;#39;long&amp;#39;,  # 做多方向&#xA;    threshold = NULL  # 无额外阈值&#xA;  ),&#xA;  type = &amp;#39;enter&amp;#39;,  # 入场规则&#xA;  label = &amp;#39;enter&amp;#39;,&#xA;  storefun = FALSE  # 不存储规则函数结果&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 可选的风险止损规则（注释未启用）：&#xA;# 1. 止损限价单：当价格回落5%时触发止损&#xA;# 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)&#xA;# 2. 追踪止损单：当价格从高点回落1单位时触发止损&#xA;# 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;),&#x9;type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;trailingexit&amp;#39;)&#xA;&#xA;# 出场规则：当卖出信号触发时，以市价单平仓所有持仓&#xA;add.rule(&#xA;  strat.st,&#xA;  name = &amp;#39;ruleSignal&amp;#39;,&#xA;  arguments = list(&#xA;    sigcol = &amp;quot;signal.lt.zero&amp;quot;,  # 触发信号列：下穿零轴&#xA;    sigval = TRUE,&#xA;    orderqty = &amp;#39;all&amp;#39;,  # 平仓全部持仓&#xA;    ordertype = &amp;#39;market&amp;#39;,  # 市价单（确保及时离场）&#xA;    orderside = &amp;#39;long&amp;#39;,  # 针对多头持仓&#xA;    threshold = NULL,&#xA;    orderset = &amp;#39;exit2&amp;#39;  # 订单集标签，用于管理出场订单&#xA;  ),&#xA;  type = &amp;#39;exit&amp;#39;,  # 出场规则&#xA;  label = &amp;#39;exit&amp;#39;&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 结束规则定义&#xA;####&#xA;&#xA;# 获取历史数据：从雅虎财经下载AAPL的历史数据（2006-12-31至2014-06-01）&#xA;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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 执行策略回测并记录时间&#xA;start_t &amp;lt;- Sys.time()  # 回测开始时间&#xA;out &amp;lt;- applyStrategy(&#xA;  strat.st, &#xA;  portfolios = portfolio.st,&#xA;  parameters = list(nFast = fastMA, nSlow = slowMA, nSig = signalMA, maType = maType),  # 传递MACD参数&#xA;  verbose = TRUE  # 输出详细过程&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#xA;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#xA;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#xA;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#xA;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#xA;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#xA;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#xA;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#xA;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#xA;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#xA;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#xA;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#xA;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#xA;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#xA;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#xA;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#xA;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#xA;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#xA;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#xA;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#xA;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#xA;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#xA;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#xA;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#xA;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#xA;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#xA;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#xA;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#xA;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#xA;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#xA;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#xA;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#xA;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#xA;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#xA;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()  # 回测结束时间&#xA;print(end_t - start_t)  # 打印回测耗时&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 0.4202731 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 更新组合与账户数据&#xA;start_t &amp;lt;- Sys.time()&#xA;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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;end_t &amp;lt;- Sys.time()&#xA;print(&amp;quot;trade blotter portfolio update:&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;print(end_t - start_t)  # 打印数据更新耗时&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Time difference of 0.032722 secs&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 可视化回测结果&#xA;chart.Posn(Portfolio = portfolio.st, Symbol = stock.str)  # 绘制AAPL的持仓变化图&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/08/10/macd/index_files/figure-html/SimpleMACD-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>MACD 策略的参数优化与有效性验证：基于 quantstrat 的实现</title>
      <link>http://gewutang.cn/2015/07/10/macd-parameters/</link>
      <pubDate>Fri, 10 Jul 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/07/10/macd-parameters/</guid>
      <description>&lt;p&gt;在量化交易中，策略参数的选择直接影响其表现 —— 同一策略在不同参数下可能呈现截然不同的收益特征。然而，盲目测试参数组合不仅效率低下，还可能因 “过度拟合” 导致策略在实盘失效。本文将解析一段基于 R 语言quantstrat包的 MACD 策略参数优化代码，展示如何系统地测试参数范围、施加合理约束，并通过统计指标验证参数有效性，为策略稳健性提供科学依据。&lt;/p&gt;&#xA;&lt;div id=&#34;代码实现与核心注释&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;代码实现与核心注释&lt;/h1&gt;&#xA;&lt;p&gt;以下是完整的 MACD 参数优化代码，包含详细注释以说明各环节的功能与逻辑：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Parameter demo for MACD&#xA;###############################################################################&#xA;&#xA;# 加载必要的包：foreach支持并行迭代，iterators用于迭代控制，quantstrat用于量化策略开发&#xA;require(foreach, quietly=TRUE)&#xA;require(iterators)&#xA;require(quantstrat)&#xA;&#xA;# 运行内置的MACD策略演示，初始化策略环境（包含策略定义、交易标的、账户等基础设置）&#xA;demo(&amp;#39;macd&amp;#39;, ask=FALSE)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## &#xA;## &#x9;demo(macd)&#xA;## &#x9;---- ~~~~&#xA;## &#xA;## &amp;gt; # Simple MACD strategy&#xA;## &amp;gt; #&#xA;## &amp;gt; # MACD may be used in many ways, this will demonstrate a trend indicator.&#xA;## &amp;gt; # &#xA;## &amp;gt; # traditionally, when the MACD signal crosses zero, this indicated a establishment of a positive trend&#xA;## &amp;gt; #&#xA;## &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&#xA;## &amp;gt; # &#xA;## &amp;gt; # Author: brian&#xA;## &amp;gt; ###############################################################################&#xA;## &amp;gt; &#xA;## &amp;gt; &#xA;## &amp;gt; require(quantstrat)&#xA;## &#xA;## &amp;gt; suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;,pos=.strategy))&#xA;## &#xA;## &amp;gt; suppressWarnings(rm(&amp;quot;account.macd&amp;quot;,&amp;quot;portfolio.macd&amp;quot;,pos=.blotter))&#xA;## &#xA;## &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;))&#xA;## &#xA;## &amp;gt; #correct for TZ issues if they crop up&#xA;## &amp;gt; oldtz&amp;lt;-Sys.getenv(&amp;#39;TZ&amp;#39;)&#xA;## &#xA;## &amp;gt; if(oldtz==&amp;#39;&amp;#39;) {&#xA;## +         Sys.setenv(TZ=&amp;quot;GMT&amp;quot;)&#xA;## + }&#xA;## &#xA;## &amp;gt; stock.str=&amp;#39;AAPL&amp;#39; # what are we trying it on&#xA;## &#xA;## &amp;gt; #MA parameters for MACD&#xA;## &amp;gt; fastMA = 12 &#xA;## &#xA;## &amp;gt; slowMA = 26 &#xA;## &#xA;## &amp;gt; signalMA = 9&#xA;## &#xA;## &amp;gt; maType=&amp;quot;EMA&amp;quot;&#xA;## &#xA;## &amp;gt; currency(&amp;#39;USD&amp;#39;)&#xA;## [1] &amp;quot;USD&amp;quot;&#xA;## &#xA;## &amp;gt; stock(stock.str,currency=&amp;#39;USD&amp;#39;,multiplier=1)&#xA;## [1] &amp;quot;AAPL&amp;quot;&#xA;## &#xA;## &amp;gt; #or use fake data&#xA;## &amp;gt; #stock.str=&amp;#39;sample_matrix&amp;#39; # what are we trying it on&#xA;## &amp;gt; #data(sample_matrix)                 # data included in package xts&#xA;## &amp;gt; #sample_matrix&amp;lt;-as.xts(sample_matrix)&#xA;## &amp;gt; &#xA;## &amp;gt; startDate=&amp;#39;2006-12-31&amp;#39;&#xA;## &#xA;## &amp;gt; initEq=1000000&#xA;## &#xA;## &amp;gt; portfolio.st=&amp;#39;macd&amp;#39;&#xA;## &#xA;## &amp;gt; account.st=&amp;#39;macd&amp;#39;&#xA;## &#xA;## &amp;gt; initPortf(portfolio.st,symbols=stock.str)&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; initAcct(account.st,portfolios=portfolio.st)&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; initOrders(portfolio=portfolio.st)&#xA;## &#xA;## &amp;gt; strat.st&amp;lt;-portfolio.st&#xA;## &#xA;## &amp;gt; # define the strategy&#xA;## &amp;gt; strategy(strat.st, store=TRUE)&#xA;## &#xA;## &amp;gt; #one indicator&#xA;## &amp;gt; add.indicator(strat.st, name = &amp;quot;MACD&amp;quot;, &#xA;## +       &#x9;&#x9;&#x9;  arguments = list(x=quote(Cl(mktdata)),&#xA;## +       &#x9;&#x9;&#x9;                   nFast=fastMA, &#xA;## +       &#x9;&#x9;&#x9;                   nSlow=slowMA),&#xA;## +       &#x9;&#x9;&#x9;  label=&amp;#39;_&amp;#39; &#xA;## + )&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; #two signals&#xA;## &amp;gt; add.signal(strat.st,name=&amp;quot;sigThreshold&amp;quot;,&#xA;## +     &#x9;&#x9;   arguments = list(column=&amp;quot;signal._&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;   &#x9;&#x9;&#x9;        relationship=&amp;quot;gt&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          threshold=0,&#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          cross=TRUE),&#xA;## +     &#x9;&#x9;   label=&amp;quot;signal.gt.zero&amp;quot;&#xA;## + )&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; add.signal(strat.st,name=&amp;quot;sigThreshold&amp;quot;,&#xA;## +     &#x9;&#x9;   arguments = list(column=&amp;quot;signal._&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;                relationship=&amp;quot;lt&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          threshold=0,&#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          cross=TRUE),&#xA;## +     &#x9;     label=&amp;quot;signal.lt.zero&amp;quot;&#xA;## + )&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; ####&#xA;## &amp;gt; # add rules&#xA;## &amp;gt; &#xA;## &amp;gt; # entry&#xA;## &amp;gt; add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, &#xA;## +     &#x9;&#x9; arguments = list(sigcol=&amp;quot;signal.gt.zero&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;              sigval=TRUE, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          orderqty=100, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          ordertype=&amp;#39;market&amp;#39;, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          orderside=&amp;#39;long&amp;#39;, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          threshold=NULL),&#xA;## +     &#x9;               type=&amp;#39;enter&amp;#39;,&#xA;## +     &#x9;&#x9;             label=&amp;#39;enter&amp;#39;,&#xA;## +     &#x9;&#x9;             storefun=FALSE&#xA;## + )&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; #alternatives for risk stops:&#xA;## &amp;gt; # simple stoplimit order, with threshold multiplier&#xA;## &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)&#xA;## &amp;gt; # alternately, use a trailing order, also with a threshold multiplier&#xA;## &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;),&#x9;type=&amp;#39;chain&amp;#39;, parent=&amp;#39;enter&amp;#39;, label=&amp;#39;trailingexit&amp;#39;)&#xA;## &amp;gt; &#xA;## &amp;gt; # exit&#xA;## &amp;gt; add.rule(strat.st,name=&amp;#39;ruleSignal&amp;#39;, &#xA;## +     &#x9;&#x9; arguments = list(sigcol=&amp;quot;signal.lt.zero&amp;quot;,&#xA;## +     &#x9;&#x9;&#x9;&#x9;              sigval=TRUE, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          orderqty=&amp;#39;all&amp;#39;, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          ordertype=&amp;#39;market&amp;#39;, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          orderside=&amp;#39;long&amp;#39;, &#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          threshold=NULL,&#xA;## +     &#x9;&#x9;&#x9;&#x9;&#x9;&#x9;          orderset=&amp;#39;exit2&amp;#39;),&#xA;## +          type=&amp;#39;exit&amp;#39;,&#xA;## +     &#x9;&#x9; label=&amp;#39;exit&amp;#39;&#xA;## + )&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; #end rules&#xA;## &amp;gt; ####&#xA;## &amp;gt; &#xA;## &amp;gt; getSymbols(stock.str,from=startDate, to=&amp;#39;2014-06-01&amp;#39;, src=&amp;#39;yahoo&amp;#39;)&#xA;## [1] &amp;quot;AAPL&amp;quot;&#xA;## &#xA;## &amp;gt; start_t&amp;lt;-Sys.time()&#xA;## &#xA;## &amp;gt; out&amp;lt;-applyStrategy(strat.st , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType),verbose=TRUE)&#xA;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#xA;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#xA;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#xA;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#xA;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#xA;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#xA;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#xA;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#xA;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#xA;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#xA;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#xA;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#xA;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#xA;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#xA;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#xA;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#xA;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#xA;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#xA;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#xA;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#xA;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#xA;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#xA;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#xA;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#xA;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#xA;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#xA;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#xA;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#xA;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#xA;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#xA;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#xA;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#xA;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#xA;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#xA;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#xA;## &#xA;## &amp;gt; end_t&amp;lt;-Sys.time()&#xA;## &#xA;## &amp;gt; print(end_t-start_t)&#xA;## Time difference of 0.437279 secs&#xA;## &#xA;## &amp;gt; start_t&amp;lt;-Sys.time()&#xA;## &#xA;## &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;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; end_t&amp;lt;-Sys.time()&#xA;## &#xA;## &amp;gt; print(&amp;quot;trade blotter portfolio update:&amp;quot;)&#xA;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&#xA;## &#xA;## &amp;gt; print(end_t-start_t)&#xA;## Time difference of 0.05017185 secs&#xA;## &#xA;## &amp;gt; chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/07/10/macd-parameters/index_files/figure-html/MACDParameter-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>MACD 策略的前进测试（Walk Forward）实现与解析</title>
      <link>http://gewutang.cn/2015/06/10/macdwfa/</link>
      <pubDate>Wed, 10 Jun 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/06/10/macdwfa/</guid>
      <description>&lt;p&gt;在量化交易中，策略的稳健性往往取决于其在不同市场周期的适应性。一次性回测容易因参数过拟合导致 “历史表现优异但未来失效” 的问题，而前进测试（Walk Forward Testing）通过滚动优化参数并验证，能有效降低过拟合风险。&lt;/p&gt;&#xA;&lt;p&gt;本文将解析一段基于quantstrat包的 MACD 策略前进测试代码，展示其实现思路与核心价值，并结合 MACD 指标的原理说明策略设计逻辑。&lt;/p&gt;&#xA;&lt;div id=&#34;代码实现与注释&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;代码实现与注释&lt;/h1&gt;&#xA;&lt;p&gt;以下是完整的前进测试代码，包含详细注释以解释各环节功能：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Walk Forward demo for MACD&#xA;###############################################################################&#xA;&#xA;# 加载必要的包：foreach用于并行迭代，iterators支持迭代器，quantstrat用于量化策略开发&#xA;require(foreach, quietly=TRUE)&#xA;require(iterators)&#xA;require(quantstrat)&#xA;&#xA;# 运行内置的MACD策略演示代码，初始化策略环境（包含策略定义、标的数据等）&#xA;# 使用source而非demo确保所有变量在当前会话中可见&#xA;source(system.file(&amp;#39;demo/macd.R&amp;#39;, package=&amp;#39;quantstrat&amp;#39;), echo = TRUE)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## &amp;gt; require(quantstrat)&#xA;## &#xA;## &amp;gt; suppressWarnings(rm(&amp;quot;order_book.macd&amp;quot;, pos = .strategy))&#xA;## &#xA;## &amp;gt; suppressWarnings(rm(&amp;quot;account.macd&amp;quot;, &amp;quot;portfolio.macd&amp;quot;, &#xA;## +     pos = .blotter))&#xA;## &#xA;## &amp;gt; suppressWarnings(rm(&amp;quot;account.st&amp;quot;, &amp;quot;portfolio.st&amp;quot;, &#xA;## +     &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;, &#xA;## +     &amp;quot;end_t&amp;quot;))&#xA;## &#xA;## &amp;gt; oldtz &amp;lt;- Sys.getenv(&amp;quot;TZ&amp;quot;)&#xA;## &#xA;## &amp;gt; if (oldtz == &amp;quot;&amp;quot;) {&#xA;## +     Sys.setenv(TZ = &amp;quot;GMT&amp;quot;)&#xA;## + }&#xA;## &#xA;## &amp;gt; stock.str = &amp;quot;AAPL&amp;quot;&#xA;## &#xA;## &amp;gt; fastMA = 12&#xA;## &#xA;## &amp;gt; slowMA = 26&#xA;## &#xA;## &amp;gt; signalMA = 9&#xA;## &#xA;## &amp;gt; maType = &amp;quot;EMA&amp;quot;&#xA;## &#xA;## &amp;gt; currency(&amp;quot;USD&amp;quot;)&#xA;## [1] &amp;quot;USD&amp;quot;&#xA;## &#xA;## &amp;gt; stock(stock.str, currency = &amp;quot;USD&amp;quot;, multiplier = 1)&#xA;## [1] &amp;quot;AAPL&amp;quot;&#xA;## &#xA;## &amp;gt; startDate = &amp;quot;2006-12-31&amp;quot;&#xA;## &#xA;## &amp;gt; initEq = 1e+06&#xA;## &#xA;## &amp;gt; portfolio.st = &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; account.st = &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; initPortf(portfolio.st, symbols = stock.str)&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; initAcct(account.st, portfolios = portfolio.st)&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; initOrders(portfolio = portfolio.st)&#xA;## &#xA;## &amp;gt; strat.st &amp;lt;- portfolio.st&#xA;## &#xA;## &amp;gt; strategy(strat.st, store = TRUE)&#xA;## &#xA;## &amp;gt; add.indicator(strat.st, name = &amp;quot;MACD&amp;quot;, arguments = list(x = quote(Cl(mktdata)), &#xA;## +     nFast = fastMA, nSlow = slowMA), label = &amp;quot;_&amp;quot;)&#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; add.signal(strat.st, name = &amp;quot;sigThreshold&amp;quot;, arguments = list(column = &amp;quot;signal._&amp;quot;, &#xA;## +     relationship = &amp;quot;gt&amp;quot;, threshold = 0, cross = TRUE), label =  .... [TRUNCATED] &#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; add.signal(strat.st, name = &amp;quot;sigThreshold&amp;quot;, arguments = list(column = &amp;quot;signal._&amp;quot;, &#xA;## +     relationship = &amp;quot;lt&amp;quot;, threshold = 0, cross = TRUE), label =  .... [TRUNCATED] &#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; add.rule(strat.st, name = &amp;quot;ruleSignal&amp;quot;, arguments = list(sigcol = &amp;quot;signal.gt.zero&amp;quot;, &#xA;## +     sigval = TRUE, orderqty = 100, ordertype = &amp;quot;market&amp;quot;, orde .... [TRUNCATED] &#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; add.rule(strat.st, name = &amp;quot;ruleSignal&amp;quot;, arguments = list(sigcol = &amp;quot;signal.lt.zero&amp;quot;, &#xA;## +     sigval = TRUE, orderqty = &amp;quot;all&amp;quot;, ordertype = &amp;quot;market&amp;quot;, or .... [TRUNCATED] &#xA;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; getSymbols(stock.str, from = startDate, to = &amp;quot;2014-06-01&amp;quot;, &#xA;## +     src = &amp;quot;yahoo&amp;quot;)&#xA;## [1] &amp;quot;AAPL&amp;quot;&#xA;## &#xA;## &amp;gt; start_t &amp;lt;- Sys.time()&#xA;## &#xA;## &amp;gt; out &amp;lt;- applyStrategy(strat.st, portfolios = portfolio.st, &#xA;## +     parameters = list(nFast = fastMA, nSlow = slowMA, nSig = signalMA, &#xA;## +         maTyp .... [TRUNCATED] &#xA;## [1] &amp;quot;2007-03-16 00:00:00 AAPL 100 @ 3.19964289665222&amp;quot;&#xA;## [1] &amp;quot;2007-08-17 00:00:00 AAPL -100 @ 4.35928583145142&amp;quot;&#xA;## [1] &amp;quot;2007-09-05 00:00:00 AAPL 100 @ 4.88428592681885&amp;quot;&#xA;## [1] &amp;quot;2008-01-16 00:00:00 AAPL -100 @ 5.70142889022827&amp;quot;&#xA;## [1] &amp;quot;2008-03-31 00:00:00 AAPL 100 @ 5.125&amp;quot;&#xA;## [1] &amp;quot;2008-06-26 00:00:00 AAPL -100 @ 6.00928592681885&amp;quot;&#xA;## [1] &amp;quot;2008-08-20 00:00:00 AAPL 100 @ 6.28000020980835&amp;quot;&#xA;## [1] &amp;quot;2008-09-09 00:00:00 AAPL -100 @ 5.41714286804199&amp;quot;&#xA;## [1] &amp;quot;2009-02-06 00:00:00 AAPL 100 @ 3.56142902374268&amp;quot;&#xA;## [1] &amp;quot;2009-03-04 00:00:00 AAPL -100 @ 3.25607109069824&amp;quot;&#xA;## [1] &amp;quot;2009-03-19 00:00:00 AAPL 100 @ 3.62928605079651&amp;quot;&#xA;## [1] &amp;quot;2009-12-15 00:00:00 AAPL -100 @ 6.93464279174805&amp;quot;&#xA;## [1] &amp;quot;2009-12-29 00:00:00 AAPL 100 @ 7.46785688400269&amp;quot;&#xA;## [1] &amp;quot;2010-02-03 00:00:00 AAPL -100 @ 7.11535692214966&amp;quot;&#xA;## [1] &amp;quot;2010-03-04 00:00:00 AAPL 100 @ 7.52535676956177&amp;quot;&#xA;## [1] &amp;quot;2010-07-16 00:00:00 AAPL -100 @ 8.92500019073486&amp;quot;&#xA;## [1] &amp;quot;2010-08-03 00:00:00 AAPL 100 @ 9.35464286804199&amp;quot;&#xA;## [1] &amp;quot;2010-08-17 00:00:00 AAPL -100 @ 8.99892902374268&amp;quot;&#xA;## [1] &amp;quot;2010-09-15 00:00:00 AAPL 100 @ 9.65071392059326&amp;quot;&#xA;## [1] &amp;quot;2011-03-22 00:00:00 AAPL -100 @ 12.1857137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 12.4357137680054&amp;quot;&#xA;## [1] &amp;quot;2011-05-23 00:00:00 AAPL -100 @ 11.9428567886353&amp;quot;&#xA;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 12.6428565979004&amp;quot;&#xA;## [1] &amp;quot;2011-11-16 00:00:00 AAPL -100 @ 13.7417860031128&amp;quot;&#xA;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 14.518928527832&amp;quot;&#xA;## [1] &amp;quot;2012-05-09 00:00:00 AAPL -100 @ 20.3278560638428&amp;quot;&#xA;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 20.9192867279053&amp;quot;&#xA;## [1] &amp;quot;2012-10-12 00:00:00 AAPL -100 @ 22.4896430969238&amp;quot;&#xA;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 16.3132133483887&amp;quot;&#xA;## [1] &amp;quot;2013-06-19 00:00:00 AAPL -100 @ 15.1071434020996&amp;quot;&#xA;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 15.7496433258057&amp;quot;&#xA;## [1] &amp;quot;2014-01-22 00:00:00 AAPL -100 @ 19.6967868804932&amp;quot;&#xA;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 19.5025005340576&amp;quot;&#xA;## [1] &amp;quot;2014-01-29 00:00:00 AAPL -100 @ 17.8839282989502&amp;quot;&#xA;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 19.2778568267822&amp;quot;&#xA;## [1] &amp;quot;2014-04-15 00:00:00 AAPL -100 @ 18.498571395874&amp;quot;&#xA;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 21.1546421051025&amp;quot;&#xA;## &#xA;## &amp;gt; end_t &amp;lt;- Sys.time()&#xA;## &#xA;## &amp;gt; print(end_t - start_t)&#xA;## Time difference of 0.4237199 secs&#xA;## &#xA;## &amp;gt; start_t &amp;lt;- Sys.time()&#xA;## &#xA;## &amp;gt; updatePortf(Portfolio = portfolio.st, Dates = paste(&amp;quot;::&amp;quot;, &#xA;## +     as.Date(Sys.time()), sep = &amp;quot;&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macd&amp;quot;&#xA;## &#xA;## &amp;gt; end_t &amp;lt;- Sys.time()&#xA;## &#xA;## &amp;gt; print(&amp;quot;trade blotter portfolio update:&amp;quot;)&#xA;## [1] &amp;quot;trade blotter portfolio update:&amp;quot;&#xA;## &#xA;## &amp;gt; print(end_t - start_t)&#xA;## Time difference of 0.02946806 secs&#xA;## &#xA;## &amp;gt; chart.Posn(Portfolio = portfolio.st, Symbol = stock.str)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/06/10/macdwfa/index_files/figure-html/WalkForward-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>三重动量趋势追踪策略及其 R 实现</title>
      <link>http://gewutang.cn/2015/05/09/rate-of-change-ema-strategy-demonstrating-ternary-indicator-ordersets-to-handle-simultaneous-stop-loss-and-take-profit-orders/</link>
      <pubDate>Sat, 09 May 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/05/09/rate-of-change-ema-strategy-demonstrating-ternary-indicator-ordersets-to-handle-simultaneous-stop-loss-and-take-profit-orders/</guid>
      <description>&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;knitr::opts_chunk$set(echo = TRUE, warning = FALSE, cache = FALSE, root.dir = here::here())&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;div id=&#34;引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;引言&lt;/h1&gt;&#xA;&lt;p&gt;该回测系统采用三重技术指标组合策略，主要逻辑架构如下：&lt;/p&gt;&#xA;&lt;div id=&#34;核心策略逻辑&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;核心策略逻辑&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;趋势判断层：通过EMA(12)与EMA(26)双均线组合识别中期趋势方向，当快线上穿慢线时判定为上升趋势。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;动量验证层：计算快线EMA(12)的ROC(9)指标，确保价格变动具备持续动能。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;趋势过滤层：引入SMA(200)作为长期趋势基准，要求价格必须位于年线上方才允许做多。&lt;/p&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;信号生成机制&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;信号生成机制&lt;/h2&gt;&#xA;&lt;p&gt;多头信号触发条件（同时满足）：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;EMA(12) &amp;gt; EMA(26)（双均线金叉）&lt;/li&gt;&#xA;&lt;li&gt;ROC(9) &amp;gt; 0（正向动量确认）&lt;/li&gt;&#xA;&lt;li&gt;收盘价 &amp;gt; SMA(200)（处于长期上升趋势）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;空头信号触发条件（同时满足）：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;EMA(12) &amp;lt; EMA(26)（双均线死叉）&lt;/li&gt;&#xA;&lt;li&gt;ROC(9) &amp;lt; 0（负向动量确认）&lt;/li&gt;&#xA;&lt;li&gt;收盘价 &amp;lt; SMA(200)（处于长期下降趋势）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;风险控制设计&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;风险控制设计&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;采用固定100股交易量简化头寸管理。&lt;/li&gt;&#xA;&lt;li&gt;通过quantstrat的ruleSignal机制实现市价单即时成交。&lt;/li&gt;&#xA;&lt;li&gt;绩效分析模块自动计算夏普比率、最大回撤等关键指标57&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;该策略通过多指标协同验证，既保留EMA对趋势的敏感度，又利用SMA过滤假突破信号，同时ROC指标确保入场时点具备足够动能。回测框架严格遵循事件驱动原则，模拟真实交易中的订单撮合流程&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;r代码实现&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;R代码实现&lt;/h1&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 加载必要的包&#xA;library(quantstrat)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(TTR)&#xA;library(PerformanceAnalytics)&#xA;library(xts)&#xA;library(quantmod)&#xA;# 设置环境选项&#xA;options(width = 240)&#xA;Sys.setenv(TZ = &amp;quot;GMT&amp;quot;)&#xA;&#xA;# 清理旧的策略对象&#xA;if (exists(&amp;quot;.strategy&amp;quot;, envir = globalenv())) {&#xA;    strategy_env &amp;lt;- get(&amp;quot;.strategy&amp;quot;, envir = globalenv())&#xA;    strategy_objects &amp;lt;- ls(strategy_env)&#xA;    for (obj in strategy_objects) {&#xA;        rm(list = obj, envir = strategy_env)&#xA;    }&#xA;}&#xA;&#xA;# 清理投资组合相关对象&#xA;if (exists(&amp;quot;.blotter&amp;quot;, envir = globalenv())) {&#xA;    blotter_env &amp;lt;- get(&amp;quot;.blotter&amp;quot;, envir = globalenv())&#xA;    blotter_objects &amp;lt;- ls(blotter_env)&#xA;    for (obj in blotter_objects) {&#xA;        rm(list = obj, envir = blotter_env)&#xA;    }&#xA;}&#xA;&#xA;&#xA;# 1. 初始化环境&#xA;currency(&amp;quot;USD&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(&amp;quot;AAPL&amp;quot;, currency=&amp;quot;USD&amp;quot;, multiplier=1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initDate &amp;lt;- &amp;quot;2010-01-01&amp;quot;&#xA;endDate &amp;lt;- &amp;quot;2025-05-25&amp;quot;&#xA;initEq &amp;lt;- 100000&#xA;&#xA;# 2. 优化后的策略函数&#xA;rocEmaStrategy &amp;lt;- function(priceData, &#xA;                          fastEMA = 12, &#xA;                          slowEMA = 26, &#xA;                          rocPeriod = 9,&#xA;                          smaFilter = 200) {&#xA;  # 计算三重技术指标&#xA;  fastMA &amp;lt;- EMA(Cl(priceData), fastEMA)&#xA;  slowMA &amp;lt;- EMA(Cl(priceData), slowEMA)&#xA;  roc &amp;lt;- ROC(fastMA, rocPeriod)&#xA;  trendFilter &amp;lt;- SMA(Cl(priceData), smaFilter)  # 新增SMA趋势过滤器&#xA;  &#xA;  # 生成交易信号&#xA;  signal &amp;lt;- ifelse(&#xA;    fastMA &amp;gt; slowMA &amp;amp; roc &amp;gt; 0 &amp;amp; Cl(priceData) &amp;gt; trendFilter, 1,&#xA;    ifelse(&#xA;      fastMA &amp;lt; slowMA &amp;amp; roc &amp;lt; 0 &amp;amp; Cl(priceData) &amp;lt; trendFilter, -1, &#xA;      0&#xA;    )&#xA;  )&#xA;  xts(na.locf(signal), order.by=index(priceData))&#xA;}&#xA;&#xA;# 3. 回测配置&#xA;strategy.st &amp;lt;- &amp;quot;enhanced_roc_ema&amp;quot;&#xA;portfolio.st &amp;lt;- &amp;quot;quant&amp;quot;&#xA;account.st &amp;lt;- &amp;quot;quant&amp;quot;&#xA;&#xA;# 清除旧策略&#xA;suppressWarnings(rm.strat(strategy.st))&#xA;&#xA;# 初始化组合&#xA;initPortf(portfolio.st, &amp;quot;AAPL&amp;quot;, initDate=initDate)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;quant&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio.st, &#xA;        initDate=initDate, initEq=initEq)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;quant&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio.st, initDate=initDate)&#xA;strategy(strategy.st, store=TRUE)&#xA;&#xA;# 4. 添加策略组件&#xA;add.indicator(strategy.st, name=&amp;quot;rocEmaStrategy&amp;quot;,&#xA;             arguments=list(priceData=quote(mktdata),&#xA;                          fastEMA=12, slowEMA=26,&#xA;                          rocPeriod=9, smaFilter=200),&#xA;             label=&amp;quot;signal&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;enhanced_roc_ema&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 交易信号规则&#xA;add.signal(strategy.st, name=&amp;quot;sigThreshold&amp;quot;,&#xA;          arguments=list(column=&amp;quot;signal&amp;quot;,&#xA;                        threshold=0.5, &#xA;                        relationship=&amp;quot;gt&amp;quot;),&#xA;          label=&amp;quot;longEntry&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;enhanced_roc_ema&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;add.signal(strategy.st, name=&amp;quot;sigThreshold&amp;quot;,&#xA;          arguments=list(column=&amp;quot;signal&amp;quot;,&#xA;                        threshold=-0.5,&#xA;                        relationship=&amp;quot;lt&amp;quot;),&#xA;          label=&amp;quot;shortEntry&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;enhanced_roc_ema&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 交易执行规则&#xA;add.rule(strategy.st, name=&amp;quot;ruleSignal&amp;quot;,&#xA;        arguments=list(sigcol=&amp;quot;longEntry&amp;quot;, sigval=TRUE,&#xA;                      orderqty=100, ordertype=&amp;quot;market&amp;quot;,&#xA;                      orderside=&amp;quot;long&amp;quot;, replace=FALSE,&#xA;                      prefer=&amp;quot;Open&amp;quot;),&#xA;        type=&amp;quot;enter&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;enhanced_roc_ema&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;add.rule(strategy.st, name=&amp;quot;ruleSignal&amp;quot;,&#xA;        arguments=list(sigcol=&amp;quot;shortEntry&amp;quot;, sigval=TRUE,&#xA;                      orderqty=-100, ordertype=&amp;quot;market&amp;quot;,&#xA;                      orderside=&amp;quot;short&amp;quot;, replace=FALSE,&#xA;                      prefer=&amp;quot;Open&amp;quot;),&#xA;        type=&amp;quot;enter&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;enhanced_roc_ema&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 5. 执行回测&#xA;getSymbols(&amp;quot;AAPL&amp;quot;, from=initDate, to=endDate, adjust=TRUE)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;applyStrategy(strategy.st, portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2010-10-19 00:00:00 AAPL 100 @ 0.0634460587102121&amp;quot;&#xA;## [1] &amp;quot;2010-10-20 00:00:00 AAPL 100 @ 0.0646171120660189&amp;quot;&#xA;## [1] &amp;quot;2010-10-21 00:00:00 AAPL 100 @ 0.065319744079503&amp;quot;&#xA;## [1] &amp;quot;2010-10-22 00:00:00 AAPL 100 @ 0.0646317477899565&amp;quot;&#xA;## [1] &amp;quot;2010-10-25 00:00:00 AAPL 100 @ 0.0646359358071077&amp;quot;&#xA;## [1] &amp;quot;2010-10-26 00:00:00 AAPL 100 @ 0.0641716968979132&amp;quot;&#xA;## [1] &amp;quot;2010-10-27 00:00:00 AAPL 100 @ 0.0643348062058937&amp;quot;&#xA;## [1] &amp;quot;2010-10-28 00:00:00 AAPL 100 @ 0.0643975371187952&amp;quot;&#xA;## [1] &amp;quot;2010-10-29 00:00:00 AAPL 100 @ 0.0636196268930247&amp;quot;&#xA;## [1] &amp;quot;2010-11-01 00:00:00 AAPL 100 @ 0.0631993007396659&amp;quot;&#xA;## [1] &amp;quot;2010-11-02 00:00:00 AAPL 100 @ 0.0641988799212357&amp;quot;&#xA;## [1] &amp;quot;2010-11-03 00:00:00 AAPL 100 @ 0.0651127164316641&amp;quot;&#xA;## [1] &amp;quot;2010-11-04 00:00:00 AAPL 100 @ 0.0659659160377667&amp;quot;&#xA;## [1] &amp;quot;2010-11-05 00:00:00 AAPL 100 @ 0.0664970738770137&amp;quot;&#xA;## [1] &amp;quot;2010-11-08 00:00:00 AAPL 100 @ 0.0663318705604577&amp;quot;&#xA;## [1] &amp;quot;2010-11-09 00:00:00 AAPL 100 @ 0.0671369693935735&amp;quot;&#xA;## [1] &amp;quot;2010-11-10 00:00:00 AAPL 100 @ 0.0662147624328656&amp;quot;&#xA;## [1] &amp;quot;2010-11-11 00:00:00 AAPL 100 @ 0.0658718140843916&amp;quot;&#xA;## [1] &amp;quot;2010-11-12 00:00:00 AAPL 100 @ 0.0660809301567832&amp;quot;&#xA;## [1] &amp;quot;2010-11-15 00:00:00 AAPL 100 @ 0.064504191955578&amp;quot;&#xA;## [1] &amp;quot;2010-11-16 00:00:00 AAPL 100 @ 0.063931209785048&amp;quot;&#xA;## [1] &amp;quot;2010-11-17 00:00:00 AAPL 100 @ 0.062986002234146&amp;quot;&#xA;## [1] &amp;quot;2010-11-18 00:00:00 AAPL 100 @ 0.0638224665237125&amp;quot;&#xA;## [1] &amp;quot;2010-12-01 00:00:00 AAPL 100 @ 0.0659282741396121&amp;quot;&#xA;## [1] &amp;quot;2010-12-02 00:00:00 AAPL 100 @ 0.0664008779150631&amp;quot;&#xA;## [1] &amp;quot;2010-12-03 00:00:00 AAPL 100 @ 0.0662921402377504&amp;quot;&#xA;## [1] &amp;quot;2010-12-06 00:00:00 AAPL 100 @ 0.0666330001616717&amp;quot;&#xA;## [1] &amp;quot;2010-12-07 00:00:00 AAPL 100 @ 0.0677120455726791&amp;quot;&#xA;## [1] &amp;quot;2010-12-08 00:00:00 AAPL 100 @ 0.0668400222254878&amp;quot;&#xA;## [1] &amp;quot;2010-12-09 00:00:00 AAPL 100 @ 0.0673628207825012&amp;quot;&#xA;## [1] &amp;quot;2010-12-10 00:00:00 AAPL 100 @ 0.0668442046586161&amp;quot;&#xA;## [1] &amp;quot;2010-12-13 00:00:00 AAPL 100 @ 0.0678312421248239&amp;quot;&#xA;## [1] &amp;quot;2010-12-14 00:00:00 AAPL 100 @ 0.0672791721199354&amp;quot;&#xA;## [1] &amp;quot;2010-12-15 00:00:00 AAPL 100 @ 0.0669173944463497&amp;quot;&#xA;## [1] &amp;quot;2010-12-16 00:00:00 AAPL 100 @ 0.0671453342598301&amp;quot;&#xA;## [1] &amp;quot;2010-12-17 00:00:00 AAPL 100 @ 0.0672582599542939&amp;quot;&#xA;## [1] &amp;quot;2010-12-20 00:00:00 AAPL 100 @ 0.06725198351259&amp;quot;&#xA;## [1] &amp;quot;2010-12-21 00:00:00 AAPL 100 @ 0.0675447482475475&amp;quot;&#xA;## [1] &amp;quot;2010-12-22 00:00:00 AAPL 100 @ 0.0678291481162483&amp;quot;&#xA;## [1] &amp;quot;2010-12-23 00:00:00 AAPL 100 @ 0.0679629859763536&amp;quot;&#xA;## [1] &amp;quot;2010-12-27 00:00:00 AAPL 100 @ 0.0675133827910967&amp;quot;&#xA;## [1] &amp;quot;2010-12-28 00:00:00 AAPL 100 @ 0.0681532783076565&amp;quot;&#xA;## [1] &amp;quot;2010-12-29 00:00:00 AAPL 100 @ 0.0682181032291335&amp;quot;&#xA;## [1] &amp;quot;2010-12-30 00:00:00 AAPL 100 @ 0.0680633587874097&amp;quot;&#xA;## [1] &amp;quot;2010-12-31 00:00:00 AAPL 100 @ 0.0675342949567382&amp;quot;&#xA;## [1] &amp;quot;2011-01-03 00:00:00 AAPL 100 @ 0.068096818252436&amp;quot;&#xA;## [1] &amp;quot;2011-01-04 00:00:00 AAPL 100 @ 0.0695188120119174&amp;quot;&#xA;## [1] &amp;quot;2011-01-05 00:00:00 AAPL 100 @ 0.0689144643849366&amp;quot;&#xA;## [1] &amp;quot;2011-01-06 00:00:00 AAPL 100 @ 0.0699955982204967&amp;quot;&#xA;## [1] &amp;quot;2011-01-07 00:00:00 AAPL 100 @ 0.0698429422033255&amp;quot;&#xA;## [1] &amp;quot;2011-01-10 00:00:00 AAPL 100 @ 0.0708550686842802&amp;quot;&#xA;## [1] &amp;quot;2011-01-11 00:00:00 AAPL 100 @ 0.072120229577485&amp;quot;&#xA;## [1] &amp;quot;2011-01-12 00:00:00 AAPL 100 @ 0.0717793696535637&amp;quot;&#xA;## [1] &amp;quot;2011-01-13 00:00:00 AAPL 100 @ 0.0721787780572582&amp;quot;&#xA;## [1] &amp;quot;2011-01-14 00:00:00 AAPL 100 @ 0.0723314340744293&amp;quot;&#xA;## [1] &amp;quot;2011-01-18 00:00:00 AAPL 100 @ 0.0689081879432328&amp;quot;&#xA;## [1] &amp;quot;2011-01-19 00:00:00 AAPL 100 @ 0.0728458565971404&amp;quot;&#xA;## [1] &amp;quot;2011-01-20 00:00:00 AAPL 100 @ 0.0703531878769312&amp;quot;&#xA;## [1] &amp;quot;2011-01-21 00:00:00 AAPL 100 @ 0.0697969354389143&amp;quot;&#xA;## [1] &amp;quot;2011-01-24 00:00:00 AAPL 100 @ 0.0683540350978144&amp;quot;&#xA;## [1] &amp;quot;2011-01-25 00:00:00 AAPL 100 @ 0.0703322812953126&amp;quot;&#xA;## [1] &amp;quot;2011-01-26 00:00:00 AAPL 100 @ 0.071718721581192&amp;quot;&#xA;## [1] &amp;quot;2011-01-27 00:00:00 AAPL 100 @ 0.071890195755429&amp;quot;&#xA;## [1] &amp;quot;2011-01-28 00:00:00 AAPL 100 @ 0.0719717559934421&amp;quot;&#xA;## [1] &amp;quot;2011-01-31 00:00:00 AAPL 100 @ 0.0702214440254014&amp;quot;&#xA;## [1] &amp;quot;2011-02-01 00:00:00 AAPL 100 @ 0.0713715907995897&amp;quot;&#xA;## [1] &amp;quot;2011-02-02 00:00:00 AAPL 100 @ 0.0720303100572382&amp;quot;&#xA;## [1] &amp;quot;2011-02-03 00:00:00 AAPL 100 @ 0.0718943781885573&amp;quot;&#xA;## [1] &amp;quot;2011-02-04 00:00:00 AAPL 100 @ 0.071860918723531&amp;quot;&#xA;## [1] &amp;quot;2011-02-07 00:00:00 AAPL 100 @ 0.0727496662192126&amp;quot;&#xA;## [1] &amp;quot;2011-02-08 00:00:00 AAPL 100 @ 0.0739604554817496&amp;quot;&#xA;## [1] &amp;quot;2011-02-09 00:00:00 AAPL 100 @ 0.0742762208069012&amp;quot;&#xA;## [1] &amp;quot;2011-02-10 00:00:00 AAPL 100 @ 0.0747362828669902&amp;quot;&#xA;## [1] &amp;quot;2011-02-11 00:00:00 AAPL 100 @ 0.0741842128621017&amp;quot;&#xA;## [1] &amp;quot;2011-02-14 00:00:00 AAPL 100 @ 0.0746108098731415&amp;quot;&#xA;## [1] &amp;quot;2011-02-15 00:00:00 AAPL 100 @ 0.0751126850964677&amp;quot;&#xA;## [1] &amp;quot;2011-02-16 00:00:00 AAPL 100 @ 0.0754493625872607&amp;quot;&#xA;## [1] &amp;quot;2011-02-17 00:00:00 AAPL 100 @ 0.0747070058350922&amp;quot;&#xA;## [1] &amp;quot;2011-02-18 00:00:00 AAPL 100 @ 0.0750123122854116&amp;quot;&#xA;## [1] &amp;quot;2011-02-22 00:00:00 AAPL 100 @ 0.0715493358315077&amp;quot;&#xA;## [1] &amp;quot;2011-02-23 00:00:00 AAPL 100 @ 0.0708425269689182&amp;quot;&#xA;## [1] &amp;quot;2011-02-24 00:00:00 AAPL 100 @ 0.0719403905369914&amp;quot;&#xA;## [1] &amp;quot;2011-02-25 00:00:00 AAPL 100 @ 0.0721996902228996&amp;quot;&#xA;## [1] &amp;quot;2011-03-08 00:00:00 AAPL 100 @ 0.0742176667431051&amp;quot;&#xA;## [1] &amp;quot;2011-03-09 00:00:00 AAPL 100 @ 0.0741716599786939&amp;quot;&#xA;## [1] &amp;quot;2011-03-10 00:00:00 AAPL 100 @ 0.0730068774805681&amp;quot;&#xA;## [1] &amp;quot;2011-03-11 00:00:00 AAPL 100 @ 0.0722143259468372&amp;quot;&#xA;## [1] &amp;quot;2011-03-14 00:00:00 AAPL 100 @ 0.0738558946535424&amp;quot;&#xA;## [1] &amp;quot;2011-03-15 00:00:00 AAPL 100 @ 0.0715388825406984&amp;quot;&#xA;## [1] &amp;quot;2011-03-16 00:00:00 AAPL 100 @ 0.071517970375057&amp;quot;&#xA;## [1] &amp;quot;2011-04-01 00:00:00 AAPL 100 @ 0.0734230267848215&amp;quot;&#xA;## [1] &amp;quot;2011-04-27 00:00:00 AAPL 100 @ 0.0736593258805356&amp;quot;&#xA;## [1] &amp;quot;2011-04-28 00:00:00 AAPL 100 @ 0.0723941705713537&amp;quot;&#xA;## [1] &amp;quot;2011-04-29 00:00:00 AAPL 100 @ 0.0725175495566268&amp;quot;&#xA;## [1] &amp;quot;2011-05-02 00:00:00 AAPL 100 @ 0.0731365329075451&amp;quot;&#xA;## [1] &amp;quot;2011-05-03 00:00:00 AAPL 100 @ 0.072770578384854&amp;quot;&#xA;## [1] &amp;quot;2011-05-04 00:00:00 AAPL 100 @ 0.0728270384400745&amp;quot;&#xA;## [1] &amp;quot;2011-05-05 00:00:00 AAPL 100 @ 0.0728563154719725&amp;quot;&#xA;## [1] &amp;quot;2011-05-06 00:00:00 AAPL 100 @ 0.0731260796167358&amp;quot;&#xA;## [1] &amp;quot;2011-05-09 00:00:00 AAPL 100 @ 0.0727433897775087&amp;quot;&#xA;## [1] &amp;quot;2011-05-10 00:00:00 AAPL 100 @ 0.0729587822916042&amp;quot;&#xA;## [1] &amp;quot;2011-05-11 00:00:00 AAPL 100 @ 0.0729859708989495&amp;quot;&#xA;## [1] &amp;quot;2011-05-12 00:00:00 AAPL 100 @ 0.0723795348474161&amp;quot;&#xA;## [1] &amp;quot;2011-05-13 00:00:00 AAPL 100 @ 0.0722833388854654&amp;quot;&#xA;## [1] &amp;quot;2011-05-16 00:00:00 AAPL 100 @ 0.070932446489165&amp;quot;&#xA;## [1] &amp;quot;2011-06-17 00:00:00 AAPL -100 @ 0.0687973618413674&amp;quot;&#xA;## [1] &amp;quot;2011-06-20 00:00:00 AAPL -100 @ 0.066365330025484&amp;quot;&#xA;## [1] &amp;quot;2011-06-21 00:00:00 AAPL -100 @ 0.0662231328831451&amp;quot;&#xA;## [1] &amp;quot;2011-06-22 00:00:00 AAPL -100 @ 0.067996439857357&amp;quot;&#xA;## [1] &amp;quot;2011-06-23 00:00:00 AAPL -100 @ 0.0666957310745732&amp;quot;&#xA;## [1] &amp;quot;2011-06-27 00:00:00 AAPL -100 @ 0.06850459710641&amp;quot;&#xA;## [1] &amp;quot;2011-07-06 00:00:00 AAPL 100 @ 0.0729713295909891&amp;quot;&#xA;## [1] &amp;quot;2011-07-07 00:00:00 AAPL 100 @ 0.0741674831295885&amp;quot;&#xA;## [1] &amp;quot;2011-07-08 00:00:00 AAPL 100 @ 0.0738893541185687&amp;quot;&#xA;## [1] &amp;quot;2011-07-11 00:00:00 AAPL 100 @ 0.0745167079197664&amp;quot;&#xA;## [1] &amp;quot;2011-07-12 00:00:00 AAPL 100 @ 0.073929084441276&amp;quot;&#xA;## [1] &amp;quot;2011-07-13 00:00:00 AAPL 100 @ 0.0749328460559741&amp;quot;&#xA;## [1] &amp;quot;2011-07-14 00:00:00 AAPL 100 @ 0.0754932809271192&amp;quot;&#xA;## [1] &amp;quot;2011-07-15 00:00:00 AAPL 100 @ 0.0755267403921455&amp;quot;&#xA;## [1] &amp;quot;2011-07-18 00:00:00 AAPL 100 @ 0.0764175763123798&amp;quot;&#xA;## [1] &amp;quot;2011-07-19 00:00:00 AAPL 100 @ 0.0790461769012699&amp;quot;&#xA;## [1] &amp;quot;2011-07-20 00:00:00 AAPL 100 @ 0.0828353775551575&amp;quot;&#xA;## [1] &amp;quot;2011-07-21 00:00:00 AAPL 100 @ 0.0809177738460081&amp;quot;&#xA;## [1] &amp;quot;2011-07-22 00:00:00 AAPL 100 @ 0.0812042621392617&amp;quot;&#xA;## [1] &amp;quot;2011-07-25 00:00:00 AAPL 100 @ 0.0816287651417259&amp;quot;&#xA;## [1] &amp;quot;2011-07-26 00:00:00 AAPL 100 @ 0.0836467472459543&amp;quot;&#xA;## [1] &amp;quot;2011-07-27 00:00:00 AAPL 100 @ 0.0837701262312274&amp;quot;&#xA;## [1] &amp;quot;2011-07-28 00:00:00 AAPL 100 @ 0.0818943524373838&amp;quot;&#xA;## [1] &amp;quot;2011-07-29 00:00:00 AAPL 100 @ 0.0810620649969227&amp;quot;&#xA;## [1] &amp;quot;2011-08-01 00:00:00 AAPL 100 @ 0.0831825083367598&amp;quot;&#xA;## [1] &amp;quot;2011-08-02 00:00:00 AAPL 100 @ 0.0831553253134374&amp;quot;&#xA;## [1] &amp;quot;2011-08-03 00:00:00 AAPL 100 @ 0.0817605089932556&amp;quot;&#xA;## [1] &amp;quot;2011-08-04 00:00:00 AAPL 100 @ 0.081432201952742&amp;quot;&#xA;## [1] &amp;quot;2011-08-05 00:00:00 AAPL 100 @ 0.0795564225748755&amp;quot;&#xA;## [1] &amp;quot;2011-08-08 00:00:00 AAPL 100 @ 0.0756354780694582&amp;quot;&#xA;## [1] &amp;quot;2011-08-31 00:00:00 AAPL 100 @ 0.08167477749016&amp;quot;&#xA;## [1] &amp;quot;2011-09-01 00:00:00 AAPL 100 @ 0.080681474750294&amp;quot;&#xA;## [1] &amp;quot;2011-09-02 00:00:00 AAPL 100 @ 0.0783644514694044&amp;quot;&#xA;## [1] &amp;quot;2011-09-06 00:00:00 AAPL 100 @ 0.0768232611577782&amp;quot;&#xA;## [1] &amp;quot;2011-09-07 00:00:00 AAPL 100 @ 0.0806271031196263&amp;quot;&#xA;## [1] &amp;quot;2011-09-08 00:00:00 AAPL 100 @ 0.0799662898534022&amp;quot;&#xA;## [1] &amp;quot;2011-09-09 00:00:00 AAPL 100 @ 0.0802862431957049&amp;quot;&#xA;## [1] &amp;quot;2011-09-12 00:00:00 AAPL 100 @ 0.0780005965393118&amp;quot;&#xA;## [1] &amp;quot;2011-09-13 00:00:00 AAPL 100 @ 0.0799119182227344&amp;quot;&#xA;## [1] &amp;quot;2011-09-14 00:00:00 AAPL 100 @ 0.0809324095699457&amp;quot;&#xA;## [1] &amp;quot;2011-09-15 00:00:00 AAPL 100 @ 0.0818546165306536&amp;quot;&#xA;## [1] &amp;quot;2011-09-16 00:00:00 AAPL 100 @ 0.0827140869944371&amp;quot;&#xA;## [1] &amp;quot;2011-09-19 00:00:00 AAPL 100 @ 0.0830193934447565&amp;quot;&#xA;## [1] &amp;quot;2011-09-20 00:00:00 AAPL 100 @ 0.0868357771219666&amp;quot;&#xA;## [1] &amp;quot;2011-09-21 00:00:00 AAPL 100 @ 0.0877538016495462&amp;quot;&#xA;## [1] &amp;quot;2011-09-22 00:00:00 AAPL 100 @ 0.0838621397600498&amp;quot;&#xA;## [1] &amp;quot;2011-09-23 00:00:00 AAPL 100 @ 0.0837053013097503&amp;quot;&#xA;## [1] &amp;quot;2011-09-26 00:00:00 AAPL 100 @ 0.0836174702140563&amp;quot;&#xA;## [1] &amp;quot;2011-09-27 00:00:00 AAPL 100 @ 0.0854723374262813&amp;quot;&#xA;## [1] &amp;quot;2011-09-28 00:00:00 AAPL 100 @ 0.0836864775686616&amp;quot;&#xA;## [1] &amp;quot;2011-09-29 00:00:00 AAPL 100 @ 0.0840482552422472&amp;quot;&#xA;## [1] &amp;quot;2011-09-30 00:00:00 AAPL 100 @ 0.0809533217355871&amp;quot;&#xA;## [1] &amp;quot;2011-10-03 00:00:00 AAPL 100 @ 0.0795417868509379&amp;quot;&#xA;## [1] &amp;quot;2011-10-17 00:00:00 AAPL 100 @ 0.0881929515439938&amp;quot;&#xA;## [1] &amp;quot;2011-10-18 00:00:00 AAPL 100 @ 0.0881971283930992&amp;quot;&#xA;## [1] &amp;quot;2011-10-19 00:00:00 AAPL 100 @ 0.0839290586901024&amp;quot;&#xA;## [1] &amp;quot;2011-10-20 00:00:00 AAPL 100 @ 0.0836467472459543&amp;quot;&#xA;## [1] &amp;quot;2011-10-21 00:00:00 AAPL 100 @ 0.0832494272668125&amp;quot;&#xA;## [1] &amp;quot;2011-10-24 00:00:00 AAPL 100 @ 0.0828479248545424&amp;quot;&#xA;## [1] &amp;quot;2011-10-25 00:00:00 AAPL 100 @ 0.0846986040496163&amp;quot;&#xA;## [1] &amp;quot;2011-10-26 00:00:00 AAPL 100 @ 0.0840147901931981&amp;quot;&#xA;## [1] &amp;quot;2011-10-27 00:00:00 AAPL 100 @ 0.0852276678802878&amp;quot;&#xA;## [1] &amp;quot;2011-10-28 00:00:00 AAPL 100 @ 0.0842740954631292&amp;quot;&#xA;## [1] &amp;quot;2011-10-31 00:00:00 AAPL 100 @ 0.0841528104864316&amp;quot;&#xA;## [1] &amp;quot;2011-11-04 00:00:00 AAPL 100 @ 0.0840712558324414&amp;quot;&#xA;## [1] &amp;quot;2011-11-08 00:00:00 AAPL 100 @ 0.084108897730596&amp;quot;&#xA;## [1] &amp;quot;2011-11-09 00:00:00 AAPL 100 @ 0.0830131225870755&amp;quot;&#xA;## [1] &amp;quot;2011-11-28 00:00:00 AAPL -100 @ 0.0778646646706309&amp;quot;&#xA;## [1] &amp;quot;2011-12-12 00:00:00 AAPL 100 @ 0.0819068941527458&amp;quot;&#xA;## [1] &amp;quot;2011-12-13 00:00:00 AAPL 100 @ 0.08218292915519&amp;quot;&#xA;## [1] &amp;quot;2011-12-14 00:00:00 AAPL 100 @ 0.0808654906398931&amp;quot;&#xA;## [1] &amp;quot;2011-12-22 00:00:00 AAPL 100 @ 0.0830193934447565&amp;quot;&#xA;## [1] &amp;quot;2011-12-23 00:00:00 AAPL 100 @ 0.0835819223244772&amp;quot;&#xA;## [1] &amp;quot;2011-12-27 00:00:00 AAPL 100 @ 0.0842950132127935&amp;quot;&#xA;## [1] &amp;quot;2011-12-28 00:00:00 AAPL 100 @ 0.0850875647465244&amp;quot;&#xA;## [1] &amp;quot;2011-12-29 00:00:00 AAPL 100 @ 0.084357744125695&amp;quot;&#xA;## [1] &amp;quot;2011-12-30 00:00:00 AAPL 100 @ 0.0843807447158891&amp;quot;&#xA;## [1] &amp;quot;2012-01-03 00:00:00 AAPL 100 @ 0.0856124517280905&amp;quot;&#xA;## [1] &amp;quot;2012-01-04 00:00:00 AAPL 100 @ 0.0857379135538934&amp;quot;&#xA;## [1] &amp;quot;2012-01-05 00:00:00 AAPL 100 @ 0.0867730462090651&amp;quot;&#xA;## [1] &amp;quot;2012-01-06 00:00:00 AAPL 100 @ 0.0877809902568915&amp;quot;&#xA;## [1] &amp;quot;2012-01-09 00:00:00 AAPL 100 @ 0.0889792322200437&amp;quot;&#xA;## [1] &amp;quot;2012-01-10 00:00:00 AAPL 100 @ 0.0890649637231393&amp;quot;&#xA;## [1] &amp;quot;2012-01-11 00:00:00 AAPL 100 @ 0.0883895147329777&amp;quot;&#xA;## [1] &amp;quot;2012-01-12 00:00:00 AAPL 100 @ 0.0883058716544347&amp;quot;&#xA;## [1] &amp;quot;2012-01-13 00:00:00 AAPL 100 @ 0.0877663545329539&amp;quot;&#xA;## [1] &amp;quot;2012-01-17 00:00:00 AAPL 100 @ 0.0887073740667048&amp;quot;&#xA;## [1] &amp;quot;2012-01-18 00:00:00 AAPL 100 @ 0.0892845386703631&amp;quot;&#xA;## [1] &amp;quot;2012-01-19 00:00:00 AAPL 100 @ 0.0899516227942682&amp;quot;&#xA;## [1] &amp;quot;2012-01-20 00:00:00 AAPL 100 @ 0.0893953703562513&amp;quot;&#xA;## [1] &amp;quot;2012-01-23 00:00:00 AAPL 100 @ 0.088387426308425&amp;quot;&#xA;## [1] &amp;quot;2012-01-24 00:00:00 AAPL 100 @ 0.0888955835574779&amp;quot;&#xA;## [1] &amp;quot;2012-01-25 00:00:00 AAPL 100 @ 0.0950310677720846&amp;quot;&#xA;## [1] &amp;quot;2012-01-26 00:00:00 AAPL 100 @ 0.0937596416052217&amp;quot;&#xA;## [1] &amp;quot;2012-01-27 00:00:00 AAPL 100 @ 0.092918989298504&amp;quot;&#xA;## [1] &amp;quot;2012-01-30 00:00:00 AAPL 100 @ 0.0932054775917576&amp;quot;&#xA;## [1] &amp;quot;2012-01-31 00:00:00 AAPL 100 @ 0.095271549300927&amp;quot;&#xA;## [1] &amp;quot;2012-02-01 00:00:00 AAPL 100 @ 0.095861266787993&amp;quot;&#xA;## [1] &amp;quot;2012-02-02 00:00:00 AAPL 100 @ 0.0953363798064269&amp;quot;&#xA;## [1] &amp;quot;2012-02-03 00:00:00 AAPL 100 @ 0.0956291501254072&amp;quot;&#xA;## [1] &amp;quot;2012-02-06 00:00:00 AAPL 100 @ 0.0958549903462891&amp;quot;&#xA;## [1] &amp;quot;2012-02-07 00:00:00 AAPL 100 @ 0.0972916254137309&amp;quot;&#xA;## [1] &amp;quot;2012-02-08 00:00:00 AAPL 100 @ 0.0983894889818041&amp;quot;&#xA;## [1] &amp;quot;2012-02-09 00:00:00 AAPL 100 @ 0.100535026920411&amp;quot;&#xA;## [1] &amp;quot;2012-02-10 00:00:00 AAPL 100 @ 0.102668023143656&amp;quot;&#xA;## [1] &amp;quot;2012-02-13 00:00:00 AAPL 100 @ 0.104460148274934&amp;quot;&#xA;## [1] &amp;quot;2012-02-14 00:00:00 AAPL 100 @ 0.105532917244237&amp;quot;&#xA;## [1] &amp;quot;2012-02-15 00:00:00 AAPL 100 @ 0.107540440473633&amp;quot;&#xA;## [1] &amp;quot;2012-02-16 00:00:00 AAPL 100 @ 0.102780943254097&amp;quot;&#xA;## [1] &amp;quot;2012-02-17 00:00:00 AAPL 100 @ 0.105208787052829&amp;quot;&#xA;## [1] &amp;quot;2012-02-21 00:00:00 AAPL 100 @ 0.105997161737455&amp;quot;&#xA;## [1] &amp;quot;2012-02-22 00:00:00 AAPL 100 @ 0.107293682503087&amp;quot;&#xA;## [1] &amp;quot;2012-02-23 00:00:00 AAPL 100 @ 0.10771191464787&amp;quot;&#xA;## [1] &amp;quot;2012-02-24 00:00:00 AAPL 100 @ 0.108671763506733&amp;quot;&#xA;## [1] &amp;quot;2012-02-27 00:00:00 AAPL 100 @ 0.109014711855207&amp;quot;&#xA;## [1] &amp;quot;2012-02-28 00:00:00 AAPL 100 @ 0.11040534574226&amp;quot;&#xA;## [1] &amp;quot;2012-02-29 00:00:00 AAPL 100 @ 0.113249333261223&amp;quot;&#xA;## [1] &amp;quot;2012-03-01 00:00:00 AAPL 100 @ 0.114631591113974&amp;quot;&#xA;## [1] &amp;quot;2012-03-02 00:00:00 AAPL 100 @ 0.113809768132368&amp;quot;&#xA;## [1] &amp;quot;2012-03-05 00:00:00 AAPL 100 @ 0.114056526102915&amp;quot;&#xA;## [1] &amp;quot;2012-03-06 00:00:00 AAPL 100 @ 0.109506139371747&amp;quot;&#xA;## [1] &amp;quot;2012-03-07 00:00:00 AAPL 100 @ 0.112253936512782&amp;quot;&#xA;## [1] &amp;quot;2012-03-08 00:00:00 AAPL 100 @ 0.111812698193781&amp;quot;&#xA;## [1] &amp;quot;2012-03-09 00:00:00 AAPL 100 @ 0.113803491690664&amp;quot;&#xA;## [1] &amp;quot;2012-03-12 00:00:00 AAPL 100 @ 0.114800976863659&amp;quot;&#xA;## [1] &amp;quot;2012-03-13 00:00:00 AAPL 100 @ 0.11659102473843&amp;quot;&#xA;## [1] &amp;quot;2012-03-14 00:00:00 AAPL 100 @ 0.120880012191091&amp;quot;&#xA;## [1] &amp;quot;2012-03-15 00:00:00 AAPL 100 @ 0.12538856342293&amp;quot;&#xA;## [1] &amp;quot;2012-03-16 00:00:00 AAPL 100 @ 0.122274811759204&amp;quot;&#xA;## [1] &amp;quot;2012-03-19 00:00:00 AAPL 100 @ 0.125129263737022&amp;quot;&#xA;## [1] &amp;quot;2012-03-20 00:00:00 AAPL 100 @ 0.125367656841311&amp;quot;&#xA;## [1] &amp;quot;2012-03-21 00:00:00 AAPL 100 @ 0.12604310024745&amp;quot;&#xA;## [1] &amp;quot;2012-03-22 00:00:00 AAPL 100 @ 0.125005879167726&amp;quot;&#xA;## [1] &amp;quot;2012-03-23 00:00:00 AAPL 100 @ 0.125572590480575&amp;quot;&#xA;## [1] &amp;quot;2012-03-26 00:00:00 AAPL 100 @ 0.125426199737062&amp;quot;&#xA;## [1] &amp;quot;2012-03-27 00:00:00 AAPL 100 @ 0.12676246757747&amp;quot;&#xA;## [1] &amp;quot;2012-03-28 00:00:00 AAPL 100 @ 0.129313684777453&amp;quot;&#xA;## [1] &amp;quot;2012-03-29 00:00:00 AAPL 100 @ 0.128142637005669&amp;quot;&#xA;## [1] &amp;quot;2012-03-30 00:00:00 AAPL 100 @ 0.127304073123504&amp;quot;&#xA;## [1] &amp;quot;2012-04-02 00:00:00 AAPL 100 @ 0.125852807916147&amp;quot;&#xA;## [1] &amp;quot;2012-04-03 00:00:00 AAPL 100 @ 0.131179005280487&amp;quot;&#xA;## [1] &amp;quot;2012-04-04 00:00:00 AAPL 100 @ 0.130562115938144&amp;quot;&#xA;## [1] &amp;quot;2012-04-05 00:00:00 AAPL 100 @ 0.13111209751848&amp;quot;&#xA;## [1] &amp;quot;2012-04-09 00:00:00 AAPL 100 @ 0.130934346902539&amp;quot;&#xA;## [1] &amp;quot;2012-04-10 00:00:00 AAPL 100 @ 0.133820158752785&amp;quot;&#xA;## [1] &amp;quot;2012-04-11 00:00:00 AAPL 100 @ 0.133040160102462&amp;quot;&#xA;## [1] &amp;quot;2012-04-12 00:00:00 AAPL 100 @ 0.130698042222802&amp;quot;&#xA;## [1] &amp;quot;2012-04-13 00:00:00 AAPL 100 @ 0.130511926740605&amp;quot;&#xA;## [1] &amp;quot;2012-04-16 00:00:00 AAPL 100 @ 0.127573837268267&amp;quot;&#xA;## [1] &amp;quot;2012-04-17 00:00:00 AAPL 100 @ 0.121066127673288&amp;quot;&#xA;## [1] &amp;quot;2012-04-18 00:00:00 AAPL 100 @ 0.128339205778675&amp;quot;&#xA;## [1] &amp;quot;2012-04-19 00:00:00 AAPL 100 @ 0.125516124841331&amp;quot;&#xA;## [1] &amp;quot;2012-06-13 00:00:00 AAPL 100 @ 0.120141826704005&amp;quot;&#xA;## [1] &amp;quot;2012-06-14 00:00:00 AAPL 100 @ 0.119455918839011&amp;quot;&#xA;## [1] &amp;quot;2012-06-15 00:00:00 AAPL 100 @ 0.119405729641471&amp;quot;&#xA;## [1] &amp;quot;2012-06-18 00:00:00 AAPL 100 @ 0.11939737594326&amp;quot;&#xA;## [1] &amp;quot;2012-06-19 00:00:00 AAPL 100 @ 0.121998782340782&amp;quot;&#xA;## [1] &amp;quot;2012-06-20 00:00:00 AAPL 100 @ 0.123004632380033&amp;quot;&#xA;## [1] &amp;quot;2012-06-21 00:00:00 AAPL 100 @ 0.122425379351822&amp;quot;&#xA;## [1] &amp;quot;2012-06-22 00:00:00 AAPL 100 @ 0.121087034254907&amp;quot;&#xA;## [1] &amp;quot;2012-06-25 00:00:00 AAPL 100 @ 0.120723168156768&amp;quot;&#xA;## [1] &amp;quot;2012-06-26 00:00:00 AAPL 100 @ 0.119474736996077&amp;quot;&#xA;## [1] &amp;quot;2012-06-27 00:00:00 AAPL 100 @ 0.120242193931038&amp;quot;&#xA;## [1] &amp;quot;2012-06-28 00:00:00 AAPL 100 @ 0.11954584394328&amp;quot;&#xA;## [1] &amp;quot;2012-06-29 00:00:00 AAPL 100 @ 0.120869547732236&amp;quot;&#xA;## [1] &amp;quot;2012-07-02 00:00:00 AAPL 100 @ 0.122276911351802&amp;quot;&#xA;## [1] &amp;quot;2012-07-03 00:00:00 AAPL 100 @ 0.124399443116192&amp;quot;&#xA;## [1] &amp;quot;2012-07-05 00:00:00 AAPL 100 @ 0.125587220620489&amp;quot;&#xA;## [1] &amp;quot;2012-07-06 00:00:00 AAPL 100 @ 0.126952759908773&amp;quot;&#xA;## [1] &amp;quot;2012-07-09 00:00:00 AAPL 100 @ 0.126578440519825&amp;quot;&#xA;## [1] &amp;quot;2012-07-10 00:00:00 AAPL 100 @ 0.129227947690334&amp;quot;&#xA;## [1] &amp;quot;2012-07-11 00:00:00 AAPL 100 @ 0.126749914694062&amp;quot;&#xA;## [1] &amp;quot;2012-07-12 00:00:00 AAPL 100 @ 0.125520312858482&amp;quot;&#xA;## [1] &amp;quot;2012-07-13 00:00:00 AAPL 100 @ 0.126087024171331&amp;quot;&#xA;## [1] &amp;quot;2012-07-16 00:00:00 AAPL 100 @ 0.126540804205694&amp;quot;&#xA;## [1] &amp;quot;2012-07-17 00:00:00 AAPL 100 @ 0.127726493285438&amp;quot;&#xA;## [1] &amp;quot;2012-07-18 00:00:00 AAPL 100 @ 0.126848204664589&amp;quot;&#xA;## [1] &amp;quot;2012-07-19 00:00:00 AAPL 100 @ 0.12782896010507&amp;quot;&#xA;## [1] &amp;quot;2012-07-20 00:00:00 AAPL 100 @ 0.128194914627761&amp;quot;&#xA;## [1] &amp;quot;2012-07-23 00:00:00 AAPL 100 @ 0.124299064721113&amp;quot;&#xA;## [1] &amp;quot;2012-07-24 00:00:00 AAPL 100 @ 0.127013402397122&amp;quot;&#xA;## [1] &amp;quot;2012-07-25 00:00:00 AAPL 100 @ 0.120129284988643&amp;quot;&#xA;## [1] &amp;quot;2012-07-26 00:00:00 AAPL 100 @ 0.121237590679479&amp;quot;&#xA;## [1] &amp;quot;2012-08-07 00:00:00 AAPL 100 @ 0.130231709305032&amp;quot;&#xA;## [1] &amp;quot;2012-08-08 00:00:00 AAPL 100 @ 0.12952489485842&amp;quot;&#xA;## [1] &amp;quot;2012-08-09 00:00:00 AAPL 100 @ 0.146772159973339&amp;quot;&#xA;## [1] &amp;quot;2012-08-10 00:00:00 AAPL 100 @ 0.146976466634898&amp;quot;&#xA;## [1] &amp;quot;2012-08-13 00:00:00 AAPL 100 @ 0.148088215086154&amp;quot;&#xA;## [1] &amp;quot;2012-08-14 00:00:00 AAPL 100 @ 0.150102660500269&amp;quot;&#xA;## [1] &amp;quot;2012-08-15 00:00:00 AAPL 100 @ 0.149967255321505&amp;quot;&#xA;## [1] &amp;quot;2012-08-16 00:00:00 AAPL 100 @ 0.149945878227322&amp;quot;&#xA;## [1] &amp;quot;2012-08-17 00:00:00 AAPL 100 @ 0.152033969951071&amp;quot;&#xA;## [1] &amp;quot;2012-08-20 00:00:00 AAPL 100 @ 0.154411876713112&amp;quot;&#xA;## [1] &amp;quot;2012-08-21 00:00:00 AAPL 100 @ 0.159355351944705&amp;quot;&#xA;## [1] &amp;quot;2012-08-22 00:00:00 AAPL 100 @ 0.155459481195116&amp;quot;&#xA;## [1] &amp;quot;2012-08-23 00:00:00 AAPL 100 @ 0.158236473566486&amp;quot;&#xA;## [1] &amp;quot;2012-08-24 00:00:00 AAPL 100 @ 0.156668625463609&amp;quot;&#xA;## [1] &amp;quot;2012-08-27 00:00:00 AAPL 100 @ 0.161533709558729&amp;quot;&#xA;## [1] &amp;quot;2012-08-28 00:00:00 AAPL 100 @ 0.160343569970999&amp;quot;&#xA;## [1] &amp;quot;2012-08-29 00:00:00 AAPL 100 @ 0.160407713940251&amp;quot;&#xA;## [1] &amp;quot;2012-08-30 00:00:00 AAPL 100 @ 0.159312597756338&amp;quot;&#xA;## [1] &amp;quot;2012-08-31 00:00:00 AAPL 100 @ 0.158507283924014&amp;quot;&#xA;## [1] &amp;quot;2012-09-04 00:00:00 AAPL 100 @ 0.158153337603173&amp;quot;&#xA;## [1] &amp;quot;2012-09-05 00:00:00 AAPL 100 @ 0.160483732663305&amp;quot;&#xA;## [1] &amp;quot;2012-09-06 00:00:00 AAPL 100 @ 0.159913604927104&amp;quot;&#xA;## [1] &amp;quot;2012-09-07 00:00:00 AAPL 100 @ 0.161072852393566&amp;quot;&#xA;## [1] &amp;quot;2012-09-10 00:00:00 AAPL 100 @ 0.16164299281647&amp;quot;&#xA;## [1] &amp;quot;2012-09-11 00:00:00 AAPL 100 @ 0.157998927743646&amp;quot;&#xA;## [1] &amp;quot;2012-09-12 00:00:00 AAPL 100 @ 0.158412260520197&amp;quot;&#xA;## [1] &amp;quot;2012-09-13 00:00:00 AAPL 100 @ 0.16091132529378&amp;quot;&#xA;## [1] &amp;quot;2012-09-14 00:00:00 AAPL 100 @ 0.16390212666709&amp;quot;&#xA;## [1] &amp;quot;2012-09-17 00:00:00 AAPL 100 @ 0.166132740809862&amp;quot;&#xA;## [1] &amp;quot;2012-09-18 00:00:00 AAPL 100 @ 0.166258643648244&amp;quot;&#xA;## [1] &amp;quot;2012-09-19 00:00:00 AAPL 100 @ 0.166348922225222&amp;quot;&#xA;## [1] &amp;quot;2012-09-20 00:00:00 AAPL 100 @ 0.166087601521373&amp;quot;&#xA;## [1] &amp;quot;2012-09-21 00:00:00 AAPL 100 @ 0.16685965081901&amp;quot;&#xA;## [1] &amp;quot;2012-09-24 00:00:00 AAPL 100 @ 0.163165701630858&amp;quot;&#xA;## [1] &amp;quot;2012-09-25 00:00:00 AAPL 100 @ 0.163498270857515&amp;quot;&#xA;## [1] &amp;quot;2012-09-26 00:00:00 AAPL 100 @ 0.158861242931557&amp;quot;&#xA;## [1] &amp;quot;2012-09-27 00:00:00 AAPL 100 @ 0.157804136109172&amp;quot;&#xA;## [1] &amp;quot;2012-09-28 00:00:00 AAPL 100 @ 0.161239149693598&amp;quot;&#xA;## [1] &amp;quot;2012-10-01 00:00:00 AAPL 100 @ 0.159436115494598&amp;quot;&#xA;## [1] &amp;quot;2012-11-23 00:00:00 AAPL 100 @ 0.154387361762617&amp;quot;&#xA;## [1] &amp;quot;2012-11-26 00:00:00 AAPL 100 @ 0.156763731709165&amp;quot;&#xA;## [1] &amp;quot;2012-11-27 00:00:00 AAPL 100 @ 0.160479344709044&amp;quot;&#xA;## [1] &amp;quot;2012-11-28 00:00:00 AAPL 100 @ 0.157136644986965&amp;quot;&#xA;## [1] &amp;quot;2012-11-29 00:00:00 AAPL 100 @ 0.160661730877112&amp;quot;&#xA;## [1] &amp;quot;2012-11-30 00:00:00 AAPL 100 @ 0.159728052092612&amp;quot;&#xA;## [1] &amp;quot;2012-12-03 00:00:00 AAPL 100 @ 0.161595395124215&amp;quot;&#xA;## [1] &amp;quot;2012-12-04 00:00:00 AAPL 100 @ 0.158369735975954&amp;quot;&#xA;## [1] &amp;quot;2012-12-05 00:00:00 AAPL 100 @ 0.154861004656115&amp;quot;&#xA;## [1] &amp;quot;2012-12-06 00:00:00 AAPL 100 @ 0.143980912332541&amp;quot;&#xA;## [1] &amp;quot;2012-12-07 00:00:00 AAPL 100 @ 0.150639083234311&amp;quot;&#xA;## [1] &amp;quot;2012-12-10 00:00:00 AAPL 100 @ 0.142908415955272&amp;quot;&#xA;## [1] &amp;quot;2013-01-15 00:00:00 AAPL -100 @ 0.135640503060255&amp;quot;&#xA;## [1] &amp;quot;2013-01-16 00:00:00 AAPL -100 @ 0.134644226249317&amp;quot;&#xA;## [1] &amp;quot;2013-01-17 00:00:00 AAPL -100 @ 0.138909701709024&amp;quot;&#xA;## [1] &amp;quot;2013-01-18 00:00:00 AAPL -100 @ 0.135700382593674&amp;quot;&#xA;## [1] &amp;quot;2013-01-22 00:00:00 AAPL -100 @ 0.137344518449722&amp;quot;&#xA;## [1] &amp;quot;2013-01-23 00:00:00 AAPL -100 @ 0.138501389872381&amp;quot;&#xA;## [1] &amp;quot;2013-01-25 00:00:00 AAPL -100 @ 0.122952961775893&amp;quot;&#xA;## [1] &amp;quot;2013-01-28 00:00:00 AAPL -100 @ 0.119180180466918&amp;quot;&#xA;## [1] &amp;quot;2013-01-29 00:00:00 AAPL -100 @ 0.124806683267604&amp;quot;&#xA;## [1] &amp;quot;2013-01-30 00:00:00 AAPL -100 @ 0.124398371430961&amp;quot;&#xA;## [1] &amp;quot;2013-01-31 00:00:00 AAPL -100 @ 0.124392934444921&amp;quot;&#xA;## [1] &amp;quot;2013-02-01 00:00:00 AAPL -100 @ 0.124972729402761&amp;quot;&#xA;## [1] &amp;quot;2013-02-04 00:00:00 AAPL -100 @ 0.123557252245704&amp;quot;&#xA;## [1] &amp;quot;2013-02-05 00:00:00 AAPL -100 @ 0.120873300078211&amp;quot;&#xA;## [1] &amp;quot;2013-02-06 00:00:00 AAPL -100 @ 0.124254102314757&amp;quot;&#xA;## [1] &amp;quot;2013-02-07 00:00:00 AAPL -100 @ 0.150519900384727&amp;quot;&#xA;## [1] &amp;quot;2013-02-12 00:00:00 AAPL 100 @ 0.155803115123383&amp;quot;&#xA;## [1] &amp;quot;2013-02-13 00:00:00 AAPL 100 @ 0.151806584359543&amp;quot;&#xA;## [1] &amp;quot;2013-02-14 00:00:00 AAPL 100 @ 0.15093254714896&amp;quot;&#xA;## [1] &amp;quot;2013-02-15 00:00:00 AAPL 100 @ 0.152339450500674&amp;quot;&#xA;## [1] &amp;quot;2013-02-19 00:00:00 AAPL 100 @ 0.149821316136009&amp;quot;&#xA;## [1] &amp;quot;2013-02-20 00:00:00 AAPL 100 @ 0.148713330074484&amp;quot;&#xA;## [1] &amp;quot;2013-02-21 00:00:00 AAPL 100 @ 0.144914992961514&amp;quot;&#xA;## [1] &amp;quot;2013-02-22 00:00:00 AAPL 100 @ 0.145970999065838&amp;quot;&#xA;## [1] &amp;quot;2013-02-25 00:00:00 AAPL 100 @ 0.147465637574825&amp;quot;&#xA;## [1] &amp;quot;2013-02-26 00:00:00 AAPL 100 @ 0.144206665182178&amp;quot;&#xA;## [1] &amp;quot;2013-02-27 00:00:00 AAPL 100 @ 0.145704548642591&amp;quot;&#xA;## [1] &amp;quot;2013-02-28 00:00:00 AAPL 100 @ 0.144281403181066&amp;quot;&#xA;## [1] &amp;quot;2013-03-01 00:00:00 AAPL 100 @ 0.142315622018769&amp;quot;&#xA;## [1] &amp;quot;2013-03-13 00:00:00 AAPL -100 @ 0.139212632879432&amp;quot;&#xA;## [1] &amp;quot;2013-03-14 00:00:00 AAPL -100 @ 0.140635778340957&amp;quot;&#xA;## [1] &amp;quot;2013-03-15 00:00:00 AAPL -100 @ 0.142292881329765&amp;quot;&#xA;## [1] &amp;quot;2013-03-20 00:00:00 AAPL 100 @ 0.148625612269892&amp;quot;&#xA;## [1] &amp;quot;2013-03-21 00:00:00 AAPL 100 @ 0.146286175818519&amp;quot;&#xA;## [1] &amp;quot;2013-03-22 00:00:00 AAPL 100 @ 0.147702831377192&amp;quot;&#xA;## [1] &amp;quot;2013-03-25 00:00:00 AAPL 100 @ 0.150987780733929&amp;quot;&#xA;## [1] &amp;quot;2013-03-26 00:00:00 AAPL 100 @ 0.151231464439149&amp;quot;&#xA;## [1] &amp;quot;2013-03-27 00:00:00 AAPL 100 @ 0.148313680468636&amp;quot;&#xA;## [1] &amp;quot;2013-03-28 00:00:00 AAPL 100 @ 0.146156204234663&amp;quot;&#xA;## [1] &amp;quot;2013-04-01 00:00:00 AAPL 100 @ 0.143582818932348&amp;quot;&#xA;## [1] &amp;quot;2013-04-05 00:00:00 AAPL -100 @ 0.137929185179701&amp;quot;&#xA;## [1] &amp;quot;2013-04-08 00:00:00 AAPL -100 @ 0.138042905977405&amp;quot;&#xA;## [1] &amp;quot;2013-04-09 00:00:00 AAPL -100 @ 0.138533544368293&amp;quot;&#xA;## [1] &amp;quot;2013-04-10 00:00:00 AAPL -100 @ 0.139098903405387&amp;quot;&#xA;## [1] &amp;quot;2013-04-11 00:00:00 AAPL -100 @ 0.140924960776868&amp;quot;&#xA;## [1] &amp;quot;2013-04-12 00:00:00 AAPL -100 @ 0.141064675891343&amp;quot;&#xA;## [1] &amp;quot;2013-04-15 00:00:00 AAPL -100 @ 0.138741490226122&amp;quot;&#xA;## [1] &amp;quot;2013-04-16 00:00:00 AAPL -100 @ 0.136977165018804&amp;quot;&#xA;## [1] &amp;quot;2013-04-17 00:00:00 AAPL -100 @ 0.136554766047611&amp;quot;&#xA;## [1] &amp;quot;2013-04-18 00:00:00 AAPL -100 @ 0.131589973013062&amp;quot;&#xA;## [1] &amp;quot;2013-04-19 00:00:00 AAPL -100 @ 0.126059803588737&amp;quot;&#xA;## [1] &amp;quot;2013-04-22 00:00:00 AAPL -100 @ 0.127577182786729&amp;quot;&#xA;## [1] &amp;quot;2013-04-23 00:00:00 AAPL -100 @ 0.131265044053421&amp;quot;&#xA;## [1] &amp;quot;2013-04-24 00:00:00 AAPL -100 @ 0.127869618850406&amp;quot;&#xA;## [1] &amp;quot;2013-04-25 00:00:00 AAPL -100 @ 0.133617477663179&amp;quot;&#xA;## [1] &amp;quot;2013-04-26 00:00:00 AAPL -100 @ 0.133156087216829&amp;quot;&#xA;## [1] &amp;quot;2013-04-29 00:00:00 AAPL -100 @ 0.136613253260346&amp;quot;&#xA;## [1] &amp;quot;2013-04-30 00:00:00 AAPL -100 @ 0.141373354064832&amp;quot;&#xA;## [1] &amp;quot;2013-05-01 00:00:00 AAPL -100 @ 0.144414619716348&amp;quot;&#xA;## [1] &amp;quot;2013-05-03 00:00:00 AAPL 100 @ 0.146640326693676&amp;quot;&#xA;## [1] &amp;quot;2013-05-06 00:00:00 AAPL 100 @ 0.148069979410735&amp;quot;&#xA;## [1] &amp;quot;2013-05-07 00:00:00 AAPL 100 @ 0.151078760842628&amp;quot;&#xA;## [1] &amp;quot;2013-05-08 00:00:00 AAPL 100 @ 0.149151971155489&amp;quot;&#xA;## [1] &amp;quot;2013-05-09 00:00:00 AAPL 100 @ 0.18311690303342&amp;quot;&#xA;## [1] &amp;quot;2013-05-10 00:00:00 AAPL 100 @ 0.182384137236034&amp;quot;&#xA;## [1] &amp;quot;2013-05-13 00:00:00 AAPL 100 @ 0.179811470599819&amp;quot;&#xA;## [1] &amp;quot;2013-05-14 00:00:00 AAPL 100 @ 0.18074337395158&amp;quot;&#xA;## [1] &amp;quot;2013-05-15 00:00:00 AAPL 100 @ 0.174893157967717&amp;quot;&#xA;## [1] &amp;quot;2013-05-16 00:00:00 AAPL 100 @ 0.168553095077562&amp;quot;&#xA;## [1] &amp;quot;2013-05-17 00:00:00 AAPL 100 @ 0.174849344728156&amp;quot;&#xA;## [1] &amp;quot;2013-05-20 00:00:00 AAPL 100 @ 0.172005876114893&amp;quot;&#xA;## [1] &amp;quot;2013-05-21 00:00:00 AAPL 100 @ 0.174490926906267&amp;quot;&#xA;## [1] &amp;quot;2013-05-22 00:00:00 AAPL 100 @ 0.176840571391976&amp;quot;&#xA;## [1] &amp;quot;2013-05-23 00:00:00 AAPL 100 @ 0.173614789726413&amp;quot;&#xA;## [1] &amp;quot;2013-05-24 00:00:00 AAPL 100 @ 0.175566191006215&amp;quot;&#xA;## [1] &amp;quot;2013-05-28 00:00:00 AAPL 100 @ 0.179170308502817&amp;quot;&#xA;## [1] &amp;quot;2013-05-29 00:00:00 AAPL 100 @ 0.175227680559195&amp;quot;&#xA;## [1] &amp;quot;2013-05-30 00:00:00 AAPL 100 @ 0.177477756267716&amp;quot;&#xA;## [1] &amp;quot;2013-05-31 00:00:00 AAPL 100 @ 0.180205725950184&amp;quot;&#xA;## [1] &amp;quot;2013-06-03 00:00:00 AAPL 100 @ 0.179500843238752&amp;quot;&#xA;## [1] &amp;quot;2013-06-04 00:00:00 AAPL 100 @ 0.18049246871512&amp;quot;&#xA;## [1] &amp;quot;2013-06-05 00:00:00 AAPL 100 @ 0.177477756267716&amp;quot;&#xA;## [1] &amp;quot;2013-06-06 00:00:00 AAPL 100 @ 0.177406081210764&amp;quot;&#xA;## [1] &amp;quot;2013-06-07 00:00:00 AAPL 100 @ 0.173833824021376&amp;quot;&#xA;## [1] &amp;quot;2013-06-10 00:00:00 AAPL 100 @ 0.177111373369024&amp;quot;&#xA;## [1] &amp;quot;2013-06-11 00:00:00 AAPL 100 @ 0.173531161737114&amp;quot;&#xA;## [1] &amp;quot;2013-06-12 00:00:00 AAPL 100 @ 0.175028564273381&amp;quot;&#xA;## [1] &amp;quot;2013-06-13 00:00:00 AAPL 100 @ 0.172240851197745&amp;quot;&#xA;## [1] &amp;quot;2013-07-15 00:00:00 AAPL 100 @ 0.169257999057556&amp;quot;&#xA;## [1] &amp;quot;2013-07-16 00:00:00 AAPL 100 @ 0.169859335770539&amp;quot;&#xA;## [1] &amp;quot;2013-07-17 00:00:00 AAPL 100 @ 0.171125761713778&amp;quot;&#xA;## [1] &amp;quot;2013-07-18 00:00:00 AAPL 100 @ 0.172591293308549&amp;quot;&#xA;## [1] &amp;quot;2013-07-19 00:00:00 AAPL 100 @ 0.172479792867577&amp;quot;&#xA;## [1] &amp;quot;2013-07-22 00:00:00 AAPL 100 @ 0.171030180792132&amp;quot;&#xA;## [1] &amp;quot;2013-07-23 00:00:00 AAPL 100 @ 0.169652254407922&amp;quot;&#xA;## [1] &amp;quot;2013-07-24 00:00:00 AAPL 100 @ 0.174801554267333&amp;quot;&#xA;## [1] &amp;quot;2013-07-25 00:00:00 AAPL 100 @ 0.175506458247327&amp;quot;&#xA;## [1] &amp;quot;2013-07-26 00:00:00 AAPL 100 @ 0.173355930047431&amp;quot;&#xA;## [1] &amp;quot;2013-07-29 00:00:00 AAPL 100 @ 0.175546272997065&amp;quot;&#xA;## [1] &amp;quot;2013-07-30 00:00:00 AAPL 100 @ 0.179194193098947&amp;quot;&#xA;## [1] &amp;quot;2013-07-31 00:00:00 AAPL 100 @ 0.181197372695114&amp;quot;&#xA;## [1] &amp;quot;2013-08-01 00:00:00 AAPL 100 @ 0.181500045613658&amp;quot;&#xA;## [1] &amp;quot;2013-08-02 00:00:00 AAPL 100 @ 0.182400067389642&amp;quot;&#xA;## [1] &amp;quot;2013-08-05 00:00:00 AAPL 100 @ 0.185060339236418&amp;quot;&#xA;## [1] &amp;quot;2013-08-06 00:00:00 AAPL 100 @ 0.186386497938545&amp;quot;&#xA;## [1] &amp;quot;2013-08-07 00:00:00 AAPL 100 @ 0.18470589863579&amp;quot;&#xA;## [1] &amp;quot;2013-08-08 00:00:00 AAPL 100 @ 0.226291535447737&amp;quot;&#xA;## [1] &amp;quot;2013-08-09 00:00:00 AAPL 100 @ 0.223744968200729&amp;quot;&#xA;## [1] &amp;quot;2013-08-12 00:00:00 AAPL 100 @ 0.222876624280699&amp;quot;&#xA;## [1] &amp;quot;2013-08-13 00:00:00 AAPL 100 @ 0.22974547506424&amp;quot;&#xA;## [1] &amp;quot;2013-08-14 00:00:00 AAPL 100 @ 0.242888005803841&amp;quot;&#xA;## [1] &amp;quot;2013-08-15 00:00:00 AAPL 100 @ 0.242175749627964&amp;quot;&#xA;## [1] &amp;quot;2013-08-16 00:00:00 AAPL 100 @ 0.243995392463416&amp;quot;&#xA;## [1] &amp;quot;2013-08-19 00:00:00 AAPL 100 @ 0.246039461910141&amp;quot;&#xA;## [1] &amp;quot;2013-08-20 00:00:00 AAPL 100 @ 0.248659214013321&amp;quot;&#xA;## [1] &amp;quot;2013-08-21 00:00:00 AAPL 100 @ 0.245673589736689&amp;quot;&#xA;## [1] &amp;quot;2013-08-22 00:00:00 AAPL 100 @ 0.246351689505858&amp;quot;&#xA;## [1] &amp;quot;2013-08-23 00:00:00 AAPL 100 @ 0.24551747593883&amp;quot;&#xA;## [1] &amp;quot;2013-08-26 00:00:00 AAPL 100 @ 0.2442881058344&amp;quot;&#xA;## [1] &amp;quot;2013-08-27 00:00:00 AAPL 100 @ 0.242946522424333&amp;quot;&#xA;## [1] &amp;quot;2013-08-28 00:00:00 AAPL 100 @ 0.237092411326871&amp;quot;&#xA;## [1] &amp;quot;2013-08-29 00:00:00 AAPL 100 @ 0.239848736949473&amp;quot;&#xA;## [1] &amp;quot;2013-08-30 00:00:00 AAPL 100 @ 0.240019466875602&amp;quot;&#xA;## [1] &amp;quot;2013-09-03 00:00:00 AAPL 100 @ 0.240556095028888&amp;quot;&#xA;## [1] &amp;quot;2013-09-04 00:00:00 AAPL 100 @ 0.243707577188893&amp;quot;&#xA;## [1] &amp;quot;2013-09-05 00:00:00 AAPL 100 @ 0.244044191052099&amp;quot;&#xA;## [1] &amp;quot;2013-09-06 00:00:00 AAPL 100 @ 0.243161204950093&amp;quot;&#xA;## [1] &amp;quot;2013-09-09 00:00:00 AAPL 100 @ 0.246361433591371&amp;quot;&#xA;## [1] &amp;quot;2013-09-10 00:00:00 AAPL 100 @ 0.24694686033334&amp;quot;&#xA;## [1] &amp;quot;2013-09-11 00:00:00 AAPL 100 @ 0.227828235051423&amp;quot;&#xA;## [1] &amp;quot;2013-09-12 00:00:00 AAPL 100 @ 0.228555133409275&amp;quot;&#xA;## [1] &amp;quot;2013-09-13 00:00:00 AAPL 100 @ 0.228964906074949&amp;quot;&#xA;## [1] &amp;quot;2013-09-30 00:00:00 AAPL 100 @ 0.232823772368073&amp;quot;&#xA;## [1] &amp;quot;2013-10-01 00:00:00 AAPL 100 @ 0.233409173056337&amp;quot;&#xA;## [1] &amp;quot;2013-10-02 00:00:00 AAPL 100 @ 0.236911911261523&amp;quot;&#xA;## [1] &amp;quot;2013-10-03 00:00:00 AAPL 100 @ 0.23929256851775&amp;quot;&#xA;## [1] &amp;quot;2013-10-04 00:00:00 AAPL 100 @ 0.236048413330545&amp;quot;&#xA;## [1] &amp;quot;2013-10-07 00:00:00 AAPL 100 @ 0.237365610473123&amp;quot;&#xA;## [1] &amp;quot;2013-10-08 00:00:00 AAPL 100 @ 0.239014497328741&amp;quot;&#xA;## [1] &amp;quot;2013-10-09 00:00:00 AAPL 100 @ 0.236428927685973&amp;quot;&#xA;## [1] &amp;quot;2013-10-10 00:00:00 AAPL 100 @ 0.239687751108858&amp;quot;&#xA;## [1] &amp;quot;2013-10-11 00:00:00 AAPL 100 @ 0.237575368848717&amp;quot;&#xA;## [1] &amp;quot;2013-10-14 00:00:00 AAPL 100 @ 0.238960852751006&amp;quot;&#xA;## [1] &amp;quot;2013-10-15 00:00:00 AAPL 100 @ 0.242707479684788&amp;quot;&#xA;## [1] &amp;quot;2013-10-16 00:00:00 AAPL 100 @ 0.244307620059132&amp;quot;&#xA;## [1] &amp;quot;2013-10-17 00:00:00 AAPL 100 @ 0.243912463521729&amp;quot;&#xA;## [1] &amp;quot;2013-10-18 00:00:00 AAPL 100 @ 0.246844417166922&amp;quot;&#xA;## [1] &amp;quot;2013-10-21 00:00:00 AAPL 100 @ 0.249664157506478&amp;quot;&#xA;## [1] &amp;quot;2013-10-22 00:00:00 AAPL 100 @ 0.256806207436271&amp;quot;&#xA;## [1] &amp;quot;2013-10-23 00:00:00 AAPL 100 @ 0.253191255925448&amp;quot;&#xA;## [1] &amp;quot;2013-10-24 00:00:00 AAPL 100 @ 0.256118337527884&amp;quot;&#xA;## [1] &amp;quot;2013-10-25 00:00:00 AAPL 100 @ 0.259201506874473&amp;quot;&#xA;## [1] &amp;quot;2013-10-28 00:00:00 AAPL 100 @ 0.258089222118437&amp;quot;&#xA;## [1] &amp;quot;2013-10-29 00:00:00 AAPL 100 @ 0.261616346591112&amp;quot;&#xA;## [1] &amp;quot;2013-10-30 00:00:00 AAPL 100 @ 0.253488867392894&amp;quot;&#xA;## [1] &amp;quot;2013-10-31 00:00:00 AAPL 100 @ 0.256118337527884&amp;quot;&#xA;## [1] &amp;quot;2013-11-01 00:00:00 AAPL 100 @ 0.255640252048795&amp;quot;&#xA;## [1] &amp;quot;2013-11-04 00:00:00 AAPL 100 @ 0.254215739697042&amp;quot;&#xA;## [1] &amp;quot;2013-11-05 00:00:00 AAPL 100 @ 0.255913451195047&amp;quot;&#xA;## [1] &amp;quot;2013-11-06 00:00:00 AAPL 100 @ 0.305328018279101&amp;quot;&#xA;## [1] &amp;quot;2013-11-07 00:00:00 AAPL 100 @ 0.302665909516973&amp;quot;&#xA;## [1] &amp;quot;2013-11-08 00:00:00 AAPL 100 @ 0.299753303509837&amp;quot;&#xA;## [1] &amp;quot;2013-11-11 00:00:00 AAPL 100 @ 0.302904740534103&amp;quot;&#xA;## [1] &amp;quot;2013-11-12 00:00:00 AAPL 100 @ 0.301553293586708&amp;quot;&#xA;## [1] &amp;quot;2013-11-13 00:00:00 AAPL 100 @ 0.301745521912206&amp;quot;&#xA;## [1] &amp;quot;2013-11-14 00:00:00 AAPL 100 @ 0.30454743874923&amp;quot;&#xA;## [1] &amp;quot;2013-11-15 00:00:00 AAPL 100 @ 0.306743551704976&amp;quot;&#xA;## [1] &amp;quot;2013-11-18 00:00:00 AAPL 100 @ 0.30581734654124&amp;quot;&#xA;## [1] &amp;quot;2013-11-19 00:00:00 AAPL 100 @ 0.302345508234516&amp;quot;&#xA;## [1] &amp;quot;2013-11-20 00:00:00 AAPL 100 @ 0.302462014963597&amp;quot;&#xA;## [1] &amp;quot;2013-11-21 00:00:00 AAPL 100 @ 0.301512508454044&amp;quot;&#xA;## [1] &amp;quot;2013-11-22 00:00:00 AAPL 100 @ 0.302630941943278&amp;quot;&#xA;## [1] &amp;quot;2013-11-25 00:00:00 AAPL 100 @ 0.303504726856413&amp;quot;&#xA;## [1] &amp;quot;2013-11-26 00:00:00 AAPL 100 @ 0.305310534492253&amp;quot;&#xA;## [1] &amp;quot;2013-11-27 00:00:00 AAPL 100 @ 0.312411471857505&amp;quot;&#xA;## [1] &amp;quot;2013-11-29 00:00:00 AAPL 100 @ 0.320083276640281&amp;quot;&#xA;## [1] &amp;quot;2013-12-02 00:00:00 AAPL 100 @ 0.325046338859356&amp;quot;&#xA;## [1] &amp;quot;2013-12-03 00:00:00 AAPL 100 @ 0.325221083398007&amp;quot;&#xA;## [1] &amp;quot;2013-12-04 00:00:00 AAPL 100 @ 0.32941523231509&amp;quot;&#xA;## [1] &amp;quot;2013-12-05 00:00:00 AAPL 100 @ 0.333580262327389&amp;quot;&#xA;## [1] &amp;quot;2013-12-06 00:00:00 AAPL 100 @ 0.329584159294772&amp;quot;&#xA;## [1] &amp;quot;2013-12-09 00:00:00 AAPL 100 @ 0.326735639766115&amp;quot;&#xA;## [1] &amp;quot;2013-12-10 00:00:00 AAPL 100 @ 0.328296798825856&amp;quot;&#xA;## [1] &amp;quot;2013-12-11 00:00:00 AAPL 100 @ 0.330289017228225&amp;quot;&#xA;## [1] &amp;quot;2013-12-12 00:00:00 AAPL 100 @ 0.327457981486416&amp;quot;&#xA;## [1] &amp;quot;2013-12-13 00:00:00 AAPL 100 @ 0.327871557042197&amp;quot;&#xA;## [1] &amp;quot;2013-12-16 00:00:00 AAPL 100 @ 0.323310435260965&amp;quot;&#xA;## [1] &amp;quot;2013-12-17 00:00:00 AAPL 100 @ 0.323770613508378&amp;quot;&#xA;## [1] &amp;quot;2013-12-18 00:00:00 AAPL 100 @ 0.320211418487299&amp;quot;&#xA;## [1] &amp;quot;2013-12-19 00:00:00 AAPL 100 @ 0.320094911758218&amp;quot;&#xA;## [1] &amp;quot;2013-12-20 00:00:00 AAPL 100 @ 0.317724054263822&amp;quot;&#xA;## [1] &amp;quot;2013-12-23 00:00:00 AAPL 100 @ 0.330871519763688&amp;quot;&#xA;## [1] &amp;quot;2013-12-24 00:00:00 AAPL 100 @ 0.331972500576016&amp;quot;&#xA;## [1] &amp;quot;2013-12-26 00:00:00 AAPL 100 @ 0.330929788683199&amp;quot;&#xA;## [1] &amp;quot;2013-12-27 00:00:00 AAPL 100 @ 0.328436606900753&amp;quot;&#xA;## [1] &amp;quot;2013-12-30 00:00:00 AAPL 100 @ 0.324731786245809&amp;quot;&#xA;## [1] &amp;quot;2013-12-31 00:00:00 AAPL 100 @ 0.322815289439857&amp;quot;&#xA;## [1] &amp;quot;2014-01-02 00:00:00 AAPL 100 @ 0.323694891911961&amp;quot;&#xA;## [1] &amp;quot;2014-01-03 00:00:00 AAPL 100 @ 0.322052193696834&amp;quot;&#xA;## [1] &amp;quot;2014-01-06 00:00:00 AAPL 100 @ 0.313075544658294&amp;quot;&#xA;## [1] &amp;quot;2014-01-07 00:00:00 AAPL 100 @ 0.31707746524988&amp;quot;&#xA;## [1] &amp;quot;2014-01-24 00:00:00 AAPL 100 @ 0.322716235387682&amp;quot;&#xA;## [1] &amp;quot;2014-01-27 00:00:00 AAPL 100 @ 0.320426948158613&amp;quot;&#xA;## [1] &amp;quot;2014-01-28 00:00:00 AAPL 100 @ 0.29636303546844&amp;quot;&#xA;## [1] &amp;quot;2014-02-11 00:00:00 AAPL 100 @ 0.370882126963025&amp;quot;&#xA;## [1] &amp;quot;2014-02-12 00:00:00 AAPL 100 @ 0.375313624577906&amp;quot;&#xA;## [1] &amp;quot;2014-02-13 00:00:00 AAPL 100 @ 0.373712948855362&amp;quot;&#xA;## [1] &amp;quot;2014-02-14 00:00:00 AAPL 100 @ 0.37917193246052&amp;quot;&#xA;## [1] &amp;quot;2014-02-18 00:00:00 AAPL 100 @ 0.381639317346451&amp;quot;&#xA;## [1] &amp;quot;2014-02-19 00:00:00 AAPL 100 @ 0.380765590295101&amp;quot;&#xA;## [1] &amp;quot;2014-02-20 00:00:00 AAPL 100 @ 0.372545665043702&amp;quot;&#xA;## [1] &amp;quot;2014-02-21 00:00:00 AAPL 100 @ 0.372405867222319&amp;quot;&#xA;## [1] &amp;quot;2014-02-24 00:00:00 AAPL 100 @ 0.365667798877586&amp;quot;&#xA;## [1] &amp;quot;2014-02-25 00:00:00 AAPL 100 @ 0.370022398358409&amp;quot;&#xA;## [1] &amp;quot;2014-02-26 00:00:00 AAPL 100 @ 0.365989315202173&amp;quot;&#xA;## [1] &amp;quot;2014-02-27 00:00:00 AAPL 100 @ 0.361466958335689&amp;quot;&#xA;## [1] &amp;quot;2014-02-28 00:00:00 AAPL 100 @ 0.369812682961738&amp;quot;&#xA;## [1] &amp;quot;2014-03-03 00:00:00 AAPL 100 @ 0.36585649793956&amp;quot;&#xA;## [1] &amp;quot;2014-03-04 00:00:00 AAPL 100 @ 0.37115474204702&amp;quot;&#xA;## [1] &amp;quot;2014-03-05 00:00:00 AAPL 100 @ 0.371098822918467&amp;quot;&#xA;## [1] &amp;quot;2014-03-06 00:00:00 AAPL 100 @ 0.372405867222319&amp;quot;&#xA;## [1] &amp;quot;2014-03-07 00:00:00 AAPL 100 @ 0.371217641734345&amp;quot;&#xA;## [1] &amp;quot;2014-03-10 00:00:00 AAPL 100 @ 0.369309448133948&amp;quot;&#xA;## [1] &amp;quot;2014-03-11 00:00:00 AAPL 100 @ 0.374265159582125&amp;quot;&#xA;## [1] &amp;quot;2014-03-12 00:00:00 AAPL 100 @ 0.373608128486218&amp;quot;&#xA;## [1] &amp;quot;2014-03-13 00:00:00 AAPL 100 @ 0.375656119907998&amp;quot;&#xA;## [1] &amp;quot;2014-03-14 00:00:00 AAPL 100 @ 0.369609985453029&amp;quot;&#xA;## [1] &amp;quot;2014-03-17 00:00:00 AAPL 100 @ 0.368848133987978&amp;quot;&#xA;## [1] &amp;quot;2014-03-18 00:00:00 AAPL 100 @ 0.367589953595526&amp;quot;&#xA;## [1] &amp;quot;2014-03-19 00:00:00 AAPL 100 @ 0.372035449657141&amp;quot;&#xA;## [1] &amp;quot;2014-03-20 00:00:00 AAPL 100 @ 0.370378854806044&amp;quot;&#xA;## [1] &amp;quot;2014-03-21 00:00:00 AAPL 100 @ 0.371804755254965&amp;quot;&#xA;## [1] &amp;quot;2014-03-24 00:00:00 AAPL 100 @ 0.376341110568182&amp;quot;&#xA;## [1] &amp;quot;2014-03-25 00:00:00 AAPL 100 @ 0.378493959688298&amp;quot;&#xA;## [1] &amp;quot;2014-03-26 00:00:00 AAPL 100 @ 0.382002791682048&amp;quot;&#xA;## [1] &amp;quot;2014-03-27 00:00:00 AAPL 100 @ 0.37745945581006&amp;quot;&#xA;## [1] &amp;quot;2014-03-28 00:00:00 AAPL 100 @ 0.376271192992895&amp;quot;&#xA;## [1] &amp;quot;2014-03-31 00:00:00 AAPL 100 @ 0.376907282412488&amp;quot;&#xA;## [1] &amp;quot;2014-04-01 00:00:00 AAPL 100 @ 0.375879759093021&amp;quot;&#xA;## [1] &amp;quot;2014-04-02 00:00:00 AAPL 100 @ 0.379109032773195&amp;quot;&#xA;## [1] &amp;quot;2014-04-03 00:00:00 AAPL 100 @ 0.378417061554239&amp;quot;&#xA;## [1] &amp;quot;2014-04-04 00:00:00 AAPL 100 @ 0.377312677429905&amp;quot;&#xA;## [1] &amp;quot;2014-04-07 00:00:00 AAPL 100 @ 0.369071773173&amp;quot;&#xA;## [1] &amp;quot;2014-04-08 00:00:00 AAPL 100 @ 0.367093699326507&amp;quot;&#xA;## [1] &amp;quot;2014-04-25 00:00:00 AAPL 100 @ 0.394591277531813&amp;quot;&#xA;## [1] &amp;quot;2014-04-28 00:00:00 AAPL 100 @ 0.400371814790748&amp;quot;&#xA;## [1] &amp;quot;2014-04-29 00:00:00 AAPL 100 @ 0.415008292062293&amp;quot;&#xA;## [1] &amp;quot;2014-04-30 00:00:00 AAPL 100 @ 0.414239422709279&amp;quot;&#xA;## [1] &amp;quot;2014-05-01 00:00:00 AAPL 100 @ 0.413792069680852&amp;quot;&#xA;## [1] &amp;quot;2014-05-02 00:00:00 AAPL 100 @ 0.4140297446418&amp;quot;&#xA;## [1] &amp;quot;2014-05-05 00:00:00 AAPL 100 @ 0.412492005935771&amp;quot;&#xA;## [1] &amp;quot;2014-05-06 00:00:00 AAPL 100 @ 0.420642013611883&amp;quot;&#xA;## [1] &amp;quot;2014-05-07 00:00:00 AAPL 100 @ 0.416063737616846&amp;quot;&#xA;## [1] &amp;quot;2014-05-08 00:00:00 AAPL 100 @ 0.486893257045593&amp;quot;&#xA;## [1] &amp;quot;2014-05-09 00:00:00 AAPL 100 @ 0.483822505075168&amp;quot;&#xA;## [1] &amp;quot;2014-05-12 00:00:00 AAPL 100 @ 0.486264236481604&amp;quot;&#xA;## [1] &amp;quot;2014-05-13 00:00:00 AAPL 100 @ 0.489997117688008&amp;quot;&#xA;## [1] &amp;quot;2014-05-14 00:00:00 AAPL 100 @ 0.490353046962866&amp;quot;&#xA;## [1] &amp;quot;2014-05-15 00:00:00 AAPL 100 @ 0.492231930945461&amp;quot;&#xA;## [1] &amp;quot;2014-05-16 00:00:00 AAPL 100 @ 0.487207767327587&amp;quot;&#xA;## [1] &amp;quot;2014-05-19 00:00:00 AAPL 100 @ 0.494839161508016&amp;quot;&#xA;## [1] &amp;quot;2014-05-20 00:00:00 AAPL 100 @ 0.500351644884877&amp;quot;&#xA;## [1] &amp;quot;2014-05-21 00:00:00 AAPL 100 @ 0.499788797461036&amp;quot;&#xA;## [1] &amp;quot;2014-05-22 00:00:00 AAPL 100 @ 0.502081517741597&amp;quot;&#xA;## [1] &amp;quot;2014-05-23 00:00:00 AAPL 100 @ 0.502619522610487&amp;quot;&#xA;## [1] &amp;quot;2014-05-27 00:00:00 AAPL 100 @ 0.509762552828095&amp;quot;&#xA;## [1] &amp;quot;2014-05-28 00:00:00 AAPL 100 @ 0.518155402260476&amp;quot;&#xA;## [1] &amp;quot;2014-05-29 00:00:00 AAPL 100 @ 0.519670090851173&amp;quot;&#xA;## [1] &amp;quot;2014-05-30 00:00:00 AAPL 100 @ 0.528054674166515&amp;quot;&#xA;## [1] &amp;quot;2014-06-02 00:00:00 AAPL 100 @ 0.524727363141038&amp;quot;&#xA;## [1] &amp;quot;2014-06-03 00:00:00 AAPL 100 @ 0.520174987048072&amp;quot;&#xA;## [1] &amp;quot;2014-06-04 00:00:00 AAPL 100 @ 0.527607729196558&amp;quot;&#xA;## [1] &amp;quot;2014-06-05 00:00:00 AAPL 100 @ 0.534858351547177&amp;quot;&#xA;## [1] &amp;quot;2014-06-06 00:00:00 AAPL 100 @ 0.537920837400563&amp;quot;&#xA;## [1] &amp;quot;2014-06-09 00:00:00 AAPL 100 @ 3.75965184420557&amp;quot;&#xA;## [1] &amp;quot;2014-06-10 00:00:00 AAPL 100 @ 3.84198320978746&amp;quot;&#xA;## [1] &amp;quot;2014-06-11 00:00:00 AAPL 100 @ 3.81764864530116&amp;quot;&#xA;## [1] &amp;quot;2014-06-12 00:00:00 AAPL 100 @ 3.81399864628432&amp;quot;&#xA;## [1] &amp;quot;2014-06-13 00:00:00 AAPL 100 @ 3.73937324675155&amp;quot;&#xA;## [1] &amp;quot;2014-06-16 00:00:00 AAPL 100 @ 3.71138899267524&amp;quot;&#xA;## [1] &amp;quot;2014-06-17 00:00:00 AAPL 100 @ 3.74383456294558&amp;quot;&#xA;## [1] &amp;quot;2014-06-18 00:00:00 AAPL 100 @ 3.74221223801804&amp;quot;&#xA;## [1] &amp;quot;2014-06-19 00:00:00 AAPL 100 @ 3.74302355519523&amp;quot;&#xA;## [1] &amp;quot;2014-06-20 00:00:00 AAPL 100 @ 3.7251782904191&amp;quot;&#xA;## [1] &amp;quot;2014-06-23 00:00:00 AAPL 100 @ 3.70368302662613&amp;quot;&#xA;## [1] &amp;quot;2014-06-24 00:00:00 AAPL 100 @ 3.68056543790561&amp;quot;&#xA;## [1] &amp;quot;2014-06-25 00:00:00 AAPL 100 @ 3.65866451552404&amp;quot;&#xA;## [1] &amp;quot;2014-06-26 00:00:00 AAPL 100 @ 3.66515381523421&amp;quot;&#xA;## [1] &amp;quot;2014-06-27 00:00:00 AAPL 100 @ 3.6834044291721&amp;quot;&#xA;## [1] &amp;quot;2014-06-30 00:00:00 AAPL 100 @ 3.73531758914611&amp;quot;&#xA;## [1] &amp;quot;2014-07-01 00:00:00 AAPL 100 @ 3.7929087316531&amp;quot;&#xA;## [1] &amp;quot;2014-07-02 00:00:00 AAPL 100 @ 3.80710399741239&amp;quot;&#xA;## [1] &amp;quot;2014-07-03 00:00:00 AAPL 100 @ 3.79899237277468&amp;quot;&#xA;## [1] &amp;quot;2014-07-07 00:00:00 AAPL 100 @ 3.81805430388976&amp;quot;&#xA;## [1] &amp;quot;2014-07-08 00:00:00 AAPL 100 @ 3.90444101765024&amp;quot;&#xA;## [1] &amp;quot;2014-07-09 00:00:00 AAPL 100 @ 3.87077878104096&amp;quot;&#xA;## [1] &amp;quot;2014-07-10 00:00:00 AAPL 100 @ 3.80264268121836&amp;quot;&#xA;## [1] &amp;quot;2014-07-11 00:00:00 AAPL 100 @ 3.86753413118587&amp;quot;&#xA;## [1] &amp;quot;2014-07-14 00:00:00 AAPL 100 @ 3.8878127286399&amp;quot;&#xA;## [1] &amp;quot;2014-07-15 00:00:00 AAPL 100 @ 3.92593659087005&amp;quot;&#xA;## [1] &amp;quot;2014-07-16 00:00:00 AAPL 100 @ 3.93283123974198&amp;quot;&#xA;## [1] &amp;quot;2014-07-17 00:00:00 AAPL 100 @ 3.85415018260377&amp;quot;&#xA;## [1] &amp;quot;2014-07-18 00:00:00 AAPL 100 @ 3.79696469868538&amp;quot;&#xA;## [1] &amp;quot;2014-07-21 00:00:00 AAPL 100 @ 3.85252785767623&amp;quot;&#xA;## [1] &amp;quot;2014-07-22 00:00:00 AAPL 100 @ 3.83995522627132&amp;quot;&#xA;## [1] &amp;quot;2014-07-23 00:00:00 AAPL 100 @ 3.86996746386377&amp;quot;&#xA;## [1] &amp;quot;2014-07-24 00:00:00 AAPL 100 @ 3.93567023100847&amp;quot;&#xA;## [1] &amp;quot;2014-07-25 00:00:00 AAPL 100 @ 3.92796426495935&amp;quot;&#xA;## [1] &amp;quot;2014-07-28 00:00:00 AAPL 100 @ 3.96730479352846&amp;quot;&#xA;## [1] &amp;quot;2014-07-29 00:00:00 AAPL 100 @ 4.02854624447913&amp;quot;&#xA;## [1] &amp;quot;2014-07-30 00:00:00 AAPL 100 @ 3.99245036576511&amp;quot;&#xA;## [1] &amp;quot;2014-07-31 00:00:00 AAPL 100 @ 3.9405372057911&amp;quot;&#xA;## [1] &amp;quot;2014-08-01 00:00:00 AAPL 100 @ 3.84887785865939&amp;quot;&#xA;## [1] &amp;quot;2014-08-04 00:00:00 AAPL 100 @ 3.90849698468252&amp;quot;&#xA;## [1] &amp;quot;2014-08-05 00:00:00 AAPL 100 @ 3.86753413118587&amp;quot;&#xA;## [1] &amp;quot;2014-08-06 00:00:00 AAPL 100 @ 3.84279421753781&amp;quot;&#xA;## [1] &amp;quot;2014-08-07 00:00:00 AAPL 100 @ 3.92785750069871&amp;quot;&#xA;## [1] &amp;quot;2014-08-08 00:00:00 AAPL 100 @ 3.90013541784939&amp;quot;&#xA;## [1] &amp;quot;2014-08-11 00:00:00 AAPL 100 @ 3.9419253102605&amp;quot;&#xA;## [1] &amp;quot;2014-08-12 00:00:00 AAPL 100 @ 3.97378528126475&amp;quot;&#xA;## [1] &amp;quot;2014-08-13 00:00:00 AAPL 100 @ 3.97833670569392&amp;quot;&#xA;## [1] &amp;quot;2014-08-14 00:00:00 AAPL 100 @ 4.02716081856247&amp;quot;&#xA;## [1] &amp;quot;2014-08-15 00:00:00 AAPL 100 @ 4.05074532893342&amp;quot;&#xA;## [1] &amp;quot;2014-08-18 00:00:00 AAPL 100 @ 4.07515722752942&amp;quot;&#xA;## [1] &amp;quot;2014-08-19 00:00:00 AAPL 100 @ 4.1132237150895&amp;quot;&#xA;## [1] &amp;quot;2014-08-20 00:00:00 AAPL 100 @ 4.15584131140222&amp;quot;&#xA;## [1] &amp;quot;2014-08-21 00:00:00 AAPL 100 @ 4.16122012405645&amp;quot;&#xA;## [1] &amp;quot;2014-08-22 00:00:00 AAPL 100 @ 4.14963479484638&amp;quot;&#xA;## [1] &amp;quot;2014-08-25 00:00:00 AAPL 100 @ 4.21169932905166&amp;quot;&#xA;## [1] &amp;quot;2014-08-26 00:00:00 AAPL 100 @ 4.19638996363747&amp;quot;&#xA;## [1] &amp;quot;2014-08-27 00:00:00 AAPL 100 @ 4.17983935804742&amp;quot;&#xA;## [1] &amp;quot;2014-08-28 00:00:00 AAPL 100 @ 4.20342386841836&amp;quot;&#xA;## [1] &amp;quot;2014-08-29 00:00:00 AAPL 100 @ 4.25597201749104&amp;quot;&#xA;## [1] &amp;quot;2014-09-02 00:00:00 AAPL 100 @ 4.26424716244779&amp;quot;&#xA;## [1] &amp;quot;2014-09-03 00:00:00 AAPL 100 @ 4.26590225457445&amp;quot;&#xA;## [1] &amp;quot;2014-09-04 00:00:00 AAPL 100 @ 4.09005274099281&amp;quot;&#xA;## [1] &amp;quot;2014-09-05 00:00:00 AAPL 100 @ 4.0879841125919&amp;quot;&#xA;## [1] &amp;quot;2014-09-08 00:00:00 AAPL 100 @ 4.10867229066033&amp;quot;&#xA;## [1] &amp;quot;2014-09-09 00:00:00 AAPL 100 @ 4.09956944180197&amp;quot;&#xA;## [1] &amp;quot;2014-09-10 00:00:00 AAPL 100 @ 4.0552967533626&amp;quot;&#xA;## [1] &amp;quot;2014-09-11 00:00:00 AAPL 100 @ 4.15460007122636&amp;quot;&#xA;## [1] &amp;quot;2014-09-18 00:00:00 AAPL 100 @ 4.2174919936567&amp;quot;&#xA;## [1] &amp;quot;2014-09-19 00:00:00 AAPL 100 @ 4.23238750712009&amp;quot;&#xA;## [1] &amp;quot;2014-09-22 00:00:00 AAPL 100 @ 4.21211318100246&amp;quot;&#xA;## [1] &amp;quot;2014-09-23 00:00:00 AAPL 100 @ 4.16246136423231&amp;quot;&#xA;## [1] &amp;quot;2014-09-24 00:00:00 AAPL 100 @ 4.22700869446586&amp;quot;&#xA;## [1] &amp;quot;2014-09-25 00:00:00 AAPL 100 @ 4.15873764370474&amp;quot;&#xA;## [1] &amp;quot;2014-09-26 00:00:00 AAPL 100 @ 4.07681231965608&amp;quot;&#xA;## [1] &amp;quot;2014-09-29 00:00:00 AAPL 100 @ 4.08177759603606&amp;quot;&#xA;## [1] &amp;quot;2014-09-30 00:00:00 AAPL 100 @ 4.17115036113986&amp;quot;&#xA;## [1] &amp;quot;2014-10-24 00:00:00 AAPL 100 @ 4.35196515110148&amp;quot;&#xA;## [1] &amp;quot;2014-10-27 00:00:00 AAPL 100 @ 4.33831087781394&amp;quot;&#xA;## [1] &amp;quot;2014-10-28 00:00:00 AAPL 100 @ 4.36106799995983&amp;quot;&#xA;## [1] &amp;quot;2014-10-29 00:00:00 AAPL 100 @ 4.4127884451309&amp;quot;&#xA;## [1] &amp;quot;2014-10-30 00:00:00 AAPL 100 @ 4.42561501451683&amp;quot;&#xA;## [1] &amp;quot;2014-10-31 00:00:00 AAPL 100 @ 4.46906031473115&amp;quot;&#xA;## [1] &amp;quot;2014-11-03 00:00:00 AAPL 100 @ 4.4777493116387&amp;quot;&#xA;## [1] &amp;quot;2014-11-04 00:00:00 AAPL 100 @ 4.52491833238059&amp;quot;&#xA;## [1] &amp;quot;2014-11-05 00:00:00 AAPL 100 @ 4.51416039139558&amp;quot;&#xA;## [1] &amp;quot;2014-11-06 00:00:00 AAPL 100 @ 4.57243769951951&amp;quot;&#xA;## [1] &amp;quot;2014-11-07 00:00:00 AAPL 100 @ 4.57875328553747&amp;quot;&#xA;## [1] &amp;quot;2014-11-10 00:00:00 AAPL 100 @ 4.59012108339057&amp;quot;&#xA;## [1] &amp;quot;2014-11-11 00:00:00 AAPL 100 @ 4.57664798312347&amp;quot;&#xA;## [1] &amp;quot;2014-11-12 00:00:00 AAPL 100 @ 4.60527836134407&amp;quot;&#xA;## [1] &amp;quot;2014-11-13 00:00:00 AAPL 100 @ 4.70716902341458&amp;quot;&#xA;## [1] &amp;quot;2014-11-14 00:00:00 AAPL 100 @ 4.76400865512817&amp;quot;&#xA;## [1] &amp;quot;2014-11-17 00:00:00 AAPL 100 @ 4.811164345451&amp;quot;&#xA;## [1] &amp;quot;2014-11-18 00:00:00 AAPL 100 @ 4.79727044168034&amp;quot;&#xA;## [1] &amp;quot;2014-11-19 00:00:00 AAPL 100 @ 4.86042565941189&amp;quot;&#xA;## [1] &amp;quot;2014-11-20 00:00:00 AAPL 100 @ 4.83811086720925&amp;quot;&#xA;## [1] &amp;quot;2014-11-21 00:00:00 AAPL 100 @ 4.94757984703247&amp;quot;&#xA;## [1] &amp;quot;2014-11-24 00:00:00 AAPL 100 @ 4.91979139704304&amp;quot;&#xA;## [1] &amp;quot;2014-11-25 00:00:00 AAPL 100 @ 5.01326117068158&amp;quot;&#xA;## [1] &amp;quot;2014-11-26 00:00:00 AAPL 100 @ 4.96568435563114&amp;quot;&#xA;## [1] &amp;quot;2014-11-28 00:00:00 AAPL 100 @ 5.02168173788951&amp;quot;&#xA;## [1] &amp;quot;2014-12-01 00:00:00 AAPL 100 @ 5.00231417633205&amp;quot;&#xA;## [1] &amp;quot;2014-12-02 00:00:00 AAPL 100 @ 4.77874480835405&amp;quot;&#xA;## [1] &amp;quot;2014-12-03 00:00:00 AAPL 100 @ 4.87347763495138&amp;quot;&#xA;## [1] &amp;quot;2014-12-04 00:00:00 AAPL 100 @ 4.87431956318255&amp;quot;&#xA;## [1] &amp;quot;2014-12-05 00:00:00 AAPL 100 @ 4.88358237984569&amp;quot;&#xA;## [1] &amp;quot;2014-12-08 00:00:00 AAPL 100 @ 4.80400683120186&amp;quot;&#xA;## [1] &amp;quot;2014-12-09 00:00:00 AAPL 100 @ 4.63938239735146&amp;quot;&#xA;## [1] &amp;quot;2014-12-10 00:00:00 AAPL 100 @ 4.8170591279654&amp;quot;&#xA;## [1] &amp;quot;2014-12-31 00:00:00 AAPL 100 @ 4.75011443013345&amp;quot;&#xA;## [1] &amp;quot;2015-01-02 00:00:00 AAPL 100 @ 4.68990644304708&amp;quot;&#xA;## [1] &amp;quot;2015-01-27 00:00:00 AAPL 100 @ 4.73327297449356&amp;quot;&#xA;## [1] &amp;quot;2015-01-29 00:00:00 AAPL 100 @ 4.89747660484041&amp;quot;&#xA;## [1] &amp;quot;2015-01-30 00:00:00 AAPL 100 @ 4.9850519171886&amp;quot;&#xA;## [1] &amp;quot;2015-02-02 00:00:00 AAPL 100 @ 4.97031576396271&amp;quot;&#xA;## [1] &amp;quot;2015-02-03 00:00:00 AAPL 100 @ 4.98926220079256&amp;quot;&#xA;## [1] &amp;quot;2015-02-04 00:00:00 AAPL 100 @ 4.98926220079256&amp;quot;&#xA;## [1] &amp;quot;2015-02-05 00:00:00 AAPL 100 @ 5.13398782881673&amp;quot;&#xA;## [1] &amp;quot;2015-02-06 00:00:00 AAPL 100 @ 5.13398782881673&amp;quot;&#xA;## [1] &amp;quot;2015-02-09 00:00:00 AAPL 100 @ 5.07110723043893&amp;quot;&#xA;## [1] &amp;quot;2015-02-10 00:00:00 AAPL 100 @ 5.1404043096508&amp;quot;&#xA;## [1] &amp;quot;2015-02-11 00:00:00 AAPL 100 @ 5.25162211413698&amp;quot;&#xA;## [1] &amp;quot;2015-02-12 00:00:00 AAPL 100 @ 5.39235553464644&amp;quot;&#xA;## [1] &amp;quot;2015-02-13 00:00:00 AAPL 100 @ 5.4445424334418&amp;quot;&#xA;## [1] &amp;quot;2015-02-17 00:00:00 AAPL 100 @ 5.4535253760672&amp;quot;&#xA;## [1] &amp;quot;2015-02-18 00:00:00 AAPL 100 @ 5.45951400448414&amp;quot;&#xA;## [1] &amp;quot;2015-02-19 00:00:00 AAPL 100 @ 5.49587362740288&amp;quot;&#xA;## [1] &amp;quot;2015-02-20 00:00:00 AAPL 100 @ 5.50186225581981&amp;quot;&#xA;## [1] &amp;quot;2015-02-23 00:00:00 AAPL 100 @ 5.56174919270068&amp;quot;&#xA;## [1] &amp;quot;2015-02-24 00:00:00 AAPL 100 @ 5.68665533733353&amp;quot;&#xA;## [1] &amp;quot;2015-02-25 00:00:00 AAPL 100 @ 5.62762410528694&amp;quot;&#xA;## [1] &amp;quot;2015-02-26 00:00:00 AAPL 100 @ 5.5091341151324&amp;quot;&#xA;## [1] &amp;quot;2015-02-27 00:00:00 AAPL 100 @ 5.56089348786639&amp;quot;&#xA;## [1] &amp;quot;2015-03-02 00:00:00 AAPL 100 @ 5.52881141005178&amp;quot;&#xA;## [1] &amp;quot;2015-03-03 00:00:00 AAPL 100 @ 5.51640662715654&amp;quot;&#xA;## [1] &amp;quot;2015-03-04 00:00:00 AAPL 100 @ 5.52239525557348&amp;quot;&#xA;## [1] &amp;quot;2015-03-05 00:00:00 AAPL 100 @ 5.50015149886278&amp;quot;&#xA;## [1] &amp;quot;2015-03-06 00:00:00 AAPL 100 @ 5.49245146077727&amp;quot;&#xA;## [1] &amp;quot;2015-03-09 00:00:00 AAPL 100 @ 5.47363019704795&amp;quot;&#xA;## [1] &amp;quot;2015-03-10 00:00:00 AAPL 100 @ 5.40732743204455&amp;quot;&#xA;## [1] &amp;quot;2015-03-11 00:00:00 AAPL 100 @ 5.3363189431641&amp;quot;&#xA;## [1] &amp;quot;2015-03-25 00:00:00 AAPL 100 @ 5.41288820804433&amp;quot;&#xA;## [1] &amp;quot;2015-03-27 00:00:00 AAPL 100 @ 5.32861923143436&amp;quot;&#xA;## [1] &amp;quot;2015-04-13 00:00:00 AAPL 100 @ 5.49116822988161&amp;quot;&#xA;## [1] &amp;quot;2015-04-14 00:00:00 AAPL 100 @ 5.43256517660794&amp;quot;&#xA;## [1] &amp;quot;2015-04-15 00:00:00 AAPL 100 @ 5.40732743204455&amp;quot;&#xA;## [1] &amp;quot;2015-04-16 00:00:00 AAPL 100 @ 5.40176632968899&amp;quot;&#xA;## [1] &amp;quot;2015-04-17 00:00:00 AAPL 100 @ 5.37053995670866&amp;quot;&#xA;## [1] &amp;quot;2015-04-20 00:00:00 AAPL 100 @ 5.37139533518718&amp;quot;&#xA;## [1] &amp;quot;2015-04-21 00:00:00 AAPL 100 @ 5.47961915182066&amp;quot;&#xA;## [1] &amp;quot;2015-04-22 00:00:00 AAPL 100 @ 5.43213732419079&amp;quot;&#xA;## [1] &amp;quot;2015-04-23 00:00:00 AAPL 100 @ 5.48817424202891&amp;quot;&#xA;## [1] &amp;quot;2015-04-24 00:00:00 AAPL 100 @ 5.58185401368143&amp;quot;&#xA;## [1] &amp;quot;2015-04-27 00:00:00 AAPL 100 @ 5.65970618310156&amp;quot;&#xA;## [1] &amp;quot;2015-04-28 00:00:00 AAPL 100 @ 5.75167519779704&amp;quot;&#xA;## [1] &amp;quot;2015-04-29 00:00:00 AAPL 100 @ 5.56773782111761&amp;quot;&#xA;## [1] &amp;quot;2015-04-30 00:00:00 AAPL 100 @ 5.5027179606541&amp;quot;&#xA;## [1] &amp;quot;2015-05-01 00:00:00 AAPL 100 @ 5.39406661795925&amp;quot;&#xA;## [1] &amp;quot;2015-05-04 00:00:00 AAPL 100 @ 5.53950543598998&amp;quot;&#xA;## [1] &amp;quot;2015-05-05 00:00:00 AAPL 100 @ 5.48175743483906&amp;quot;&#xA;## [1] &amp;quot;2015-05-06 00:00:00 AAPL 100 @ 5.41374358652285&amp;quot;&#xA;## [1] &amp;quot;2015-05-07 00:00:00 AAPL 100 @ 5.42748036895492&amp;quot;&#xA;## [1] &amp;quot;2015-05-15 00:00:00 AAPL 100 @ 5.61453033438063&amp;quot;&#xA;## [1] &amp;quot;2015-05-18 00:00:00 AAPL 100 @ 5.58451530828443&amp;quot;&#xA;## [1] &amp;quot;2015-05-19 00:00:00 AAPL 100 @ 5.68499993390695&amp;quot;&#xA;## [1] &amp;quot;2015-05-20 00:00:00 AAPL 100 @ 5.65498490781076&amp;quot;&#xA;## [1] &amp;quot;2015-05-21 00:00:00 AAPL 100 @ 5.65803021828686&amp;quot;&#xA;## [1] &amp;quot;2015-05-22 00:00:00 AAPL 100 @ 5.72458498756296&amp;quot;&#xA;## [1] &amp;quot;2015-05-26 00:00:00 AAPL 100 @ 5.7680848714692&amp;quot;&#xA;## [1] &amp;quot;2015-05-27 00:00:00 AAPL 100 @ 5.66977470903755&amp;quot;&#xA;## [1] &amp;quot;2015-05-28 00:00:00 AAPL 100 @ 5.73589471842658&amp;quot;&#xA;## [1] &amp;quot;2015-05-29 00:00:00 AAPL 100 @ 5.70848957916388&amp;quot;&#xA;## [1] &amp;quot;2015-06-01 00:00:00 AAPL 100 @ 5.66716482220406&amp;quot;&#xA;## [1] &amp;quot;2015-06-02 00:00:00 AAPL 100 @ 5.64889495061411&amp;quot;&#xA;## [1] &amp;quot;2015-06-03 00:00:00 AAPL 100 @ 5.68369499049021&amp;quot;&#xA;## [1] &amp;quot;2015-06-04 00:00:00 AAPL 100 @ 5.63671503622081&amp;quot;&#xA;## [1] &amp;quot;2015-06-05 00:00:00 AAPL 100 @ 5.63323496585764&amp;quot;&#xA;## [1] &amp;quot;2015-06-08 00:00:00 AAPL 100 @ 5.60713477001168&amp;quot;&#xA;## [1] &amp;quot;2015-06-09 00:00:00 AAPL 100 @ 5.51143515816907&amp;quot;&#xA;## [1] &amp;quot;2015-07-21 00:00:00 AAPL 100 @ 5.77895984244576&amp;quot;&#xA;## [1] &amp;quot;2015-07-22 00:00:00 AAPL 100 @ 5.30655074479603&amp;quot;&#xA;## [1] &amp;quot;2015-07-23 00:00:00 AAPL 100 @ 5.48968521621595&amp;quot;&#xA;## [1] &amp;quot;2015-07-24 00:00:00 AAPL 100 @ 5.45140543785446&amp;quot;&#xA;## [1] &amp;quot;2015-08-04 00:00:00 AAPL -100 @ 5.10775628861964&amp;quot;&#xA;## [1] &amp;quot;2015-08-05 00:00:00 AAPL -100 @ 4.91331175445832&amp;quot;&#xA;## [1] &amp;quot;2015-08-06 00:00:00 AAPL -100 @ 5.13727722653428&amp;quot;&#xA;## [1] &amp;quot;2015-08-07 00:00:00 AAPL -100 @ 5.07570257676129&amp;quot;&#xA;## [1] &amp;quot;2015-08-10 00:00:00 AAPL -100 @ 5.16208418242284&amp;quot;&#xA;## [1] &amp;quot;2015-08-12 00:00:00 AAPL -100 @ 4.98489086786013&amp;quot;&#xA;## [1] &amp;quot;2015-08-13 00:00:00 AAPL -100 @ 5.14037809602035&amp;quot;&#xA;## [1] &amp;quot;2015-08-14 00:00:00 AAPL -100 @ 5.06418491668328&amp;quot;&#xA;## [1] &amp;quot;2015-08-17 00:00:00 AAPL -100 @ 5.14037809602035&amp;quot;&#xA;## [1] &amp;quot;2015-08-18 00:00:00 AAPL -100 @ 5.15765441715266&amp;quot;&#xA;## [1] &amp;quot;2015-08-19 00:00:00 AAPL -100 @ 5.14303588758857&amp;quot;&#xA;## [1] &amp;quot;2015-08-20 00:00:00 AAPL -100 @ 5.05355341244095&amp;quot;&#xA;## [1] &amp;quot;2015-08-21 00:00:00 AAPL -100 @ 4.8918644453086&amp;quot;&#xA;## [1] &amp;quot;2015-08-24 00:00:00 AAPL -100 @ 4.2025825598099&amp;quot;&#xA;## [1] &amp;quot;2015-08-25 00:00:00 AAPL -100 @ 4.92198732230304&amp;quot;&#xA;## [1] &amp;quot;2015-08-26 00:00:00 AAPL -100 @ 4.74390785190464&amp;quot;&#xA;## [1] &amp;quot;2015-08-27 00:00:00 AAPL -100 @ 4.97160157204959&amp;quot;&#xA;## [1] &amp;quot;2015-08-28 00:00:00 AAPL -100 @ 4.96894344251194&amp;quot;&#xA;## [1] &amp;quot;2015-08-31 00:00:00 AAPL -100 @ 4.9627417035398&amp;quot;&#xA;## [1] &amp;quot;2015-09-01 00:00:00 AAPL -100 @ 4.87946096736432&amp;quot;&#xA;## [1] &amp;quot;2015-09-02 00:00:00 AAPL -100 @ 4.88300491476824&amp;quot;&#xA;## [1] &amp;quot;2015-09-03 00:00:00 AAPL -100 @ 4.98311889415818&amp;quot;&#xA;## [1] &amp;quot;2015-09-04 00:00:00 AAPL -100 @ 4.82718892604955&amp;quot;&#xA;## [1] &amp;quot;2015-09-08 00:00:00 AAPL -100 @ 4.95033822559552&amp;quot;&#xA;## [1] &amp;quot;2015-09-09 00:00:00 AAPL -100 @ 5.03937796079471&amp;quot;&#xA;## [1] &amp;quot;2015-09-10 00:00:00 AAPL -100 @ 4.88477655050077&amp;quot;&#xA;## [1] &amp;quot;2015-09-11 00:00:00 AAPL -100 @ 4.95211019929747&amp;quot;&#xA;## [1] &amp;quot;2015-09-30 00:00:00 AAPL -100 @ 4.88034678523059&amp;quot;&#xA;## [1] &amp;quot;2015-10-01 00:00:00 AAPL -100 @ 4.83161869131973&amp;quot;&#xA;## [1] &amp;quot;2015-10-02 00:00:00 AAPL -100 @ 4.78466257111083&amp;quot;&#xA;## [1] &amp;quot;2015-10-05 00:00:00 AAPL -100 @ 4.86750022936846&amp;quot;&#xA;## [1] &amp;quot;2015-10-06 00:00:00 AAPL -100 @ 4.90072397584897&amp;quot;&#xA;## [1] &amp;quot;2015-10-07 00:00:00 AAPL -100 @ 4.94989514767767&amp;quot;&#xA;## [1] &amp;quot;2015-10-08 00:00:00 AAPL -100 @ 4.88123294106628&amp;quot;&#xA;## [1] &amp;quot;2015-10-09 00:00:00 AAPL -100 @ 4.87281615047433&amp;quot;&#xA;## [1] &amp;quot;2015-10-12 00:00:00 AAPL -100 @ 4.99375073636993&amp;quot;&#xA;## [1] &amp;quot;2015-10-13 00:00:00 AAPL -100 @ 4.90914076644091&amp;quot;&#xA;## [1] &amp;quot;2015-10-14 00:00:00 AAPL -100 @ 4.92996103497714&amp;quot;&#xA;## [1] &amp;quot;2015-10-15 00:00:00 AAPL -100 @ 4.91401360962894&amp;quot;&#xA;## [1] &amp;quot;2015-10-16 00:00:00 AAPL -100 @ 4.95166712137963&amp;quot;&#xA;## [1] &amp;quot;2015-10-19 00:00:00 AAPL -100 @ 4.90825494857464&amp;quot;&#xA;## [1] &amp;quot;2015-10-20 00:00:00 AAPL -100 @ 4.93217574862751&amp;quot;&#xA;## [1] &amp;quot;2015-10-26 00:00:00 AAPL 100 @ 5.23074672700366&amp;quot;&#xA;## [1] &amp;quot;2015-10-29 00:00:00 AAPL 100 @ 5.25821147446044&amp;quot;&#xA;## [1] &amp;quot;2015-10-30 00:00:00 AAPL 100 @ 5.35965468760392&amp;quot;&#xA;## [1] &amp;quot;2015-11-02 00:00:00 AAPL 100 @ 5.3512382349814&amp;quot;&#xA;## [1] &amp;quot;2015-11-03 00:00:00 AAPL 100 @ 5.35079515706356&amp;quot;&#xA;## [1] &amp;quot;2015-11-04 00:00:00 AAPL 100 @ 5.45445308385741&amp;quot;&#xA;## [1] &amp;quot;2015-11-05 00:00:00 AAPL 100 @ 5.49137471503702&amp;quot;&#xA;## [1] &amp;quot;2015-11-06 00:00:00 AAPL 100 @ 5.45802546916774&amp;quot;&#xA;## [1] &amp;quot;2015-11-09 00:00:00 AAPL 100 @ 5.45126539861588&amp;quot;&#xA;## [1] &amp;quot;2015-11-10 00:00:00 AAPL 100 @ 5.26829479364613&amp;quot;&#xA;## [1] &amp;quot;2015-11-27 00:00:00 AAPL 100 @ 5.33093744935247&amp;quot;&#xA;## [1] &amp;quot;2015-12-01 00:00:00 AAPL 100 @ 5.35166808023507&amp;quot;&#xA;## [1] &amp;quot;2015-12-07 00:00:00 AAPL 100 @ 5.36203356759211&amp;quot;&#xA;## [1] &amp;quot;2015-12-08 00:00:00 AAPL 100 @ 5.29623591430763&amp;quot;&#xA;## [1] &amp;quot;2015-12-09 00:00:00 AAPL 100 @ 5.30164403951541&amp;quot;&#xA;## [1] &amp;quot;2015-12-15 00:00:00 AAPL -100 @ 5.04476410919672&amp;quot;&#xA;## [1] &amp;quot;2015-12-16 00:00:00 AAPL -100 @ 5.00555597506114&amp;quot;&#xA;## [1] &amp;quot;2015-12-17 00:00:00 AAPL -100 @ 5.04836918217043&amp;quot;&#xA;## [1] &amp;quot;2015-12-18 00:00:00 AAPL -100 @ 4.90821212814144&amp;quot;&#xA;## [1] &amp;quot;2015-12-21 00:00:00 AAPL -100 @ 4.83475322201954&amp;quot;&#xA;## [1] &amp;quot;2015-12-22 00:00:00 AAPL -100 @ 4.84016134722732&amp;quot;&#xA;## [1] &amp;quot;2015-12-23 00:00:00 AAPL -100 @ 4.83430245896103&amp;quot;&#xA;## [1] &amp;quot;2015-12-24 00:00:00 AAPL -100 @ 4.91226796417367&amp;quot;&#xA;## [1] &amp;quot;2015-12-28 00:00:00 AAPL -100 @ 4.8487237823503&amp;quot;&#xA;## [1] &amp;quot;2015-12-29 00:00:00 AAPL -100 @ 4.82033189863027&amp;quot;&#xA;## [1] &amp;quot;2015-12-30 00:00:00 AAPL -100 @ 4.89334004169365&amp;quot;&#xA;## [1] &amp;quot;2015-12-31 00:00:00 AAPL -100 @ 4.82258537009138&amp;quot;&#xA;## [1] &amp;quot;2016-01-04 00:00:00 AAPL -100 @ 4.62429191561533&amp;quot;&#xA;## [1] &amp;quot;2016-01-05 00:00:00 AAPL -100 @ 4.76580125881987&amp;quot;&#xA;## [1] &amp;quot;2016-01-06 00:00:00 AAPL -100 @ 4.53190508701342&amp;quot;&#xA;## [1] &amp;quot;2016-01-07 00:00:00 AAPL -100 @ 4.44717985508039&amp;quot;&#xA;## [1] &amp;quot;2016-01-08 00:00:00 AAPL -100 @ 4.44132131064557&amp;quot;&#xA;## [1] &amp;quot;2016-01-11 00:00:00 AAPL -100 @ 4.46024923312558&amp;quot;&#xA;## [1] &amp;quot;2016-01-12 00:00:00 AAPL -100 @ 4.53145466778637&amp;quot;&#xA;## [1] &amp;quot;2016-01-13 00:00:00 AAPL -100 @ 4.52108918042933&amp;quot;&#xA;## [1] &amp;quot;2016-01-14 00:00:00 AAPL -100 @ 4.41473179149667&amp;quot;&#xA;## [1] &amp;quot;2016-01-15 00:00:00 AAPL -100 @ 4.33541434093995&amp;quot;&#xA;## [1] &amp;quot;2016-01-19 00:00:00 AAPL -100 @ 4.43501200315223&amp;quot;&#xA;## [1] &amp;quot;2016-01-20 00:00:00 AAPL -100 @ 4.28584106327881&amp;quot;&#xA;## [1] &amp;quot;2016-01-21 00:00:00 AAPL -100 @ 4.37417171201701&amp;quot;&#xA;## [1] &amp;quot;2016-01-22 00:00:00 AAPL -100 @ 4.44492638361928&amp;quot;&#xA;## [1] &amp;quot;2016-01-25 00:00:00 AAPL -100 @ 4.57516905718122&amp;quot;&#xA;## [1] &amp;quot;2016-01-26 00:00:00 AAPL -100 @ 4.50351320329339&amp;quot;&#xA;## [1] &amp;quot;2016-01-27 00:00:00 AAPL -100 @ 4.32820385116105&amp;quot;&#xA;## [1] &amp;quot;2016-01-28 00:00:00 AAPL -100 @ 4.22680382437765&amp;quot;&#xA;## [1] &amp;quot;2016-01-29 00:00:00 AAPL -100 @ 4.27187050294805&amp;quot;&#xA;## [1] &amp;quot;2016-02-01 00:00:00 AAPL -100 @ 4.34758253669958&amp;quot;&#xA;## [1] &amp;quot;2016-02-02 00:00:00 AAPL -100 @ 4.30026238666807&amp;quot;&#xA;## [1] &amp;quot;2016-02-03 00:00:00 AAPL -100 @ 4.28133446418806&amp;quot;&#xA;## [1] &amp;quot;2016-02-04 00:00:00 AAPL -100 @ 4.41541156755321&amp;quot;&#xA;## [1] &amp;quot;2016-02-05 00:00:00 AAPL -100 @ 4.44581167290303&amp;quot;&#xA;## [1] &amp;quot;2016-02-08 00:00:00 AAPL -100 @ 4.28966476675085&amp;quot;&#xA;## [1] &amp;quot;2016-02-09 00:00:00 AAPL -100 @ 4.34309574479665&amp;quot;&#xA;## [1] &amp;quot;2016-02-10 00:00:00 AAPL -100 @ 4.41817511765286&amp;quot;&#xA;## [1] &amp;quot;2016-02-11 00:00:00 AAPL -100 @ 4.32006522351853&amp;quot;&#xA;## [1] &amp;quot;2016-02-12 00:00:00 AAPL -100 @ 4.3384897108246&amp;quot;&#xA;## [1] &amp;quot;2016-02-16 00:00:00 AAPL -100 @ 4.37672010906869&amp;quot;&#xA;## [1] &amp;quot;2016-02-17 00:00:00 AAPL -100 @ 4.45272089957003&amp;quot;&#xA;## [1] &amp;quot;2016-03-31 00:00:00 AAPL 100 @ 5.05381764549592&amp;quot;&#xA;## [1] &amp;quot;2016-04-01 00:00:00 AAPL 100 @ 5.01052015303935&amp;quot;&#xA;## [1] &amp;quot;2016-04-04 00:00:00 AAPL 100 @ 5.08606023471813&amp;quot;&#xA;## [1] &amp;quot;2016-04-05 00:00:00 AAPL 100 @ 5.04414486872926&amp;quot;&#xA;## [1] &amp;quot;2016-04-06 00:00:00 AAPL 100 @ 5.0773088755966&amp;quot;&#xA;## [1] &amp;quot;2016-04-07 00:00:00 AAPL 100 @ 5.06441148848985&amp;quot;&#xA;## [1] &amp;quot;2016-04-08 00:00:00 AAPL 100 @ 5.0165083134791&amp;quot;&#xA;## [1] &amp;quot;2016-04-11 00:00:00 AAPL 100 @ 5.01927186357875&amp;quot;&#xA;## [1] &amp;quot;2016-04-12 00:00:00 AAPL 100 @ 5.03631422441712&amp;quot;&#xA;## [1] &amp;quot;2016-04-13 00:00:00 AAPL 100 @ 5.10356365579693&amp;quot;&#xA;## [1] &amp;quot;2016-04-14 00:00:00 AAPL 100 @ 5.14133369663632&amp;quot;&#xA;## [1] &amp;quot;2016-04-15 00:00:00 AAPL 100 @ 5.16390350909187&amp;quot;&#xA;## [1] &amp;quot;2016-04-18 00:00:00 AAPL 100 @ 5.01558689583397&amp;quot;&#xA;## [1] &amp;quot;2016-04-28 00:00:00 AAPL -100 @ 4.4960183920266&amp;quot;&#xA;## [1] &amp;quot;2016-04-29 00:00:00 AAPL -100 @ 4.32927729146263&amp;quot;&#xA;## [1] &amp;quot;2016-05-02 00:00:00 AAPL -100 @ 4.32835622523537&amp;quot;&#xA;## [1] &amp;quot;2016-05-03 00:00:00 AAPL -100 @ 4.3389500682293&amp;quot;&#xA;## [1] &amp;quot;2016-05-04 00:00:00 AAPL -100 @ 4.38501111078552&amp;quot;&#xA;## [1] &amp;quot;2016-05-05 00:00:00 AAPL -100 @ 4.43714527238209&amp;quot;&#xA;## [1] &amp;quot;2016-05-06 00:00:00 AAPL -100 @ 4.40740708797127&amp;quot;&#xA;## [1] &amp;quot;2016-05-09 00:00:00 AAPL -100 @ 4.38994159927164&amp;quot;&#xA;## [1] &amp;quot;2016-05-10 00:00:00 AAPL -100 @ 4.4055188978306&amp;quot;&#xA;## [1] &amp;quot;2016-05-11 00:00:00 AAPL -100 @ 4.41259952082425&amp;quot;&#xA;## [1] &amp;quot;2016-05-12 00:00:00 AAPL -100 @ 4.37672462842239&amp;quot;&#xA;## [1] &amp;quot;2016-05-13 00:00:00 AAPL -100 @ 4.2483305799403&amp;quot;&#xA;## [1] &amp;quot;2016-05-16 00:00:00 AAPL -100 @ 4.36114732986344&amp;quot;&#xA;## [1] &amp;quot;2016-05-17 00:00:00 AAPL -100 @ 4.46310743664702&amp;quot;&#xA;## [1] &amp;quot;2016-05-18 00:00:00 AAPL -100 @ 4.44469803294478&amp;quot;&#xA;## [1] &amp;quot;2016-05-19 00:00:00 AAPL -100 @ 4.46735559436194&amp;quot;&#xA;## [1] &amp;quot;2016-05-20 00:00:00 AAPL -100 @ 4.46735559436194&amp;quot;&#xA;## [1] &amp;quot;2016-05-23 00:00:00 AAPL -100 @ 4.52541627074739&amp;quot;&#xA;## [1] &amp;quot;2016-05-24 00:00:00 AAPL -100 @ 4.58914115741941&amp;quot;&#xA;## [1] &amp;quot;2016-06-20 00:00:00 AAPL -100 @ 4.53155261860299&amp;quot;&#xA;## [1] &amp;quot;2016-06-21 00:00:00 AAPL -100 @ 4.48151684034926&amp;quot;&#xA;## [1] &amp;quot;2016-06-22 00:00:00 AAPL -100 @ 4.5433535368806&amp;quot;&#xA;## [1] &amp;quot;2016-06-23 00:00:00 AAPL -100 @ 4.5287205134597&amp;quot;&#xA;## [1] &amp;quot;2016-06-24 00:00:00 AAPL -100 @ 4.38569344155672&amp;quot;&#xA;## [1] &amp;quot;2016-06-27 00:00:00 AAPL -100 @ 4.38994159927164&amp;quot;&#xA;## [1] &amp;quot;2016-06-28 00:00:00 AAPL -100 @ 4.38522130398769&amp;quot;&#xA;## [1] &amp;quot;2016-06-29 00:00:00 AAPL -100 @ 4.43572921981045&amp;quot;&#xA;## [1] &amp;quot;2016-06-30 00:00:00 AAPL -100 @ 4.45791500379403&amp;quot;&#xA;## [1] &amp;quot;2016-07-01 00:00:00 AAPL -100 @ 4.50747864447873&amp;quot;&#xA;## [1] &amp;quot;2016-07-05 00:00:00 AAPL -100 @ 4.50275834919478&amp;quot;&#xA;## [1] &amp;quot;2016-07-06 00:00:00 AAPL -100 @ 4.46546740422127&amp;quot;&#xA;## [1] &amp;quot;2016-07-07 00:00:00 AAPL -100 @ 4.51739137261568&amp;quot;&#xA;## [1] &amp;quot;2016-07-08 00:00:00 AAPL -100 @ 4.55468231758918&amp;quot;&#xA;## [1] &amp;quot;2016-07-11 00:00:00 AAPL -100 @ 4.56695537343582&amp;quot;&#xA;## [1] &amp;quot;2016-07-28 00:00:00 AAPL 100 @ 4.85395379237985&amp;quot;&#xA;## [1] &amp;quot;2016-07-29 00:00:00 AAPL 100 @ 4.9181508166209&amp;quot;&#xA;## [1] &amp;quot;2016-08-01 00:00:00 AAPL 100 @ 4.92853568232687&amp;quot;&#xA;## [1] &amp;quot;2016-08-02 00:00:00 AAPL 100 @ 5.00594967741717&amp;quot;&#xA;## [1] &amp;quot;2016-08-03 00:00:00 AAPL 100 @ 4.94741686346269&amp;quot;&#xA;## [1] &amp;quot;2016-08-04 00:00:00 AAPL 100 @ 5.09354054860241&amp;quot;&#xA;## [1] &amp;quot;2016-08-05 00:00:00 AAPL 100 @ 5.12682825927067&amp;quot;&#xA;## [1] &amp;quot;2016-08-08 00:00:00 AAPL 100 @ 5.18713253636229&amp;quot;&#xA;## [1] &amp;quot;2016-08-09 00:00:00 AAPL 100 @ 5.22138568965025&amp;quot;&#xA;## [1] &amp;quot;2016-08-10 00:00:00 AAPL 100 @ 5.24454232593529&amp;quot;&#xA;## [1] &amp;quot;2016-08-11 00:00:00 AAPL 100 @ 5.23537595803558&amp;quot;&#xA;## [1] &amp;quot;2016-08-12 00:00:00 AAPL 100 @ 5.19967592905641&amp;quot;&#xA;## [1] &amp;quot;2016-08-15 00:00:00 AAPL 100 @ 5.21704359030424&amp;quot;&#xA;## [1] &amp;quot;2016-08-16 00:00:00 AAPL 100 @ 5.28892618553838&amp;quot;&#xA;## [1] &amp;quot;2016-08-17 00:00:00 AAPL 100 @ 5.26335723094243&amp;quot;&#xA;## [1] &amp;quot;2016-08-18 00:00:00 AAPL 100 @ 5.26962911132354&amp;quot;&#xA;## [1] &amp;quot;2016-08-19 00:00:00 AAPL 100 @ 5.2474368134539&amp;quot;&#xA;## [1] &amp;quot;2016-08-22 00:00:00 AAPL 100 @ 5.25177891279991&amp;quot;&#xA;## [1] &amp;quot;2016-08-23 00:00:00 AAPL 100 @ 5.23875298282999&amp;quot;&#xA;## [1] &amp;quot;2016-08-24 00:00:00 AAPL 100 @ 5.23778827634648&amp;quot;&#xA;## [1] &amp;quot;2016-08-25 00:00:00 AAPL 100 @ 5.18086102404927&amp;quot;&#xA;## [1] &amp;quot;2016-08-26 00:00:00 AAPL 100 @ 5.18182609860088&amp;quot;&#xA;## [1] &amp;quot;2016-08-29 00:00:00 AAPL 100 @ 5.1437137513108&amp;quot;&#xA;## [1] &amp;quot;2016-08-30 00:00:00 AAPL 100 @ 5.10415416026143&amp;quot;&#xA;## [1] &amp;quot;2016-08-31 00:00:00 AAPL 100 @ 5.09740011067262&amp;quot;&#xA;## [1] &amp;quot;2016-09-15 00:00:00 AAPL 100 @ 5.49299602116636&amp;quot;&#xA;## [1] &amp;quot;2016-09-16 00:00:00 AAPL 100 @ 5.55378283553378&amp;quot;&#xA;## [1] &amp;quot;2016-09-19 00:00:00 AAPL 100 @ 5.55715986032818&amp;quot;&#xA;## [1] &amp;quot;2016-09-20 00:00:00 AAPL 100 @ 5.45391896739279&amp;quot;&#xA;## [1] &amp;quot;2016-09-21 00:00:00 AAPL 100 @ 5.49251348389056&amp;quot;&#xA;## [1] &amp;quot;2016-09-22 00:00:00 AAPL 100 @ 5.51663519472721&amp;quot;&#xA;## [1] &amp;quot;2016-09-23 00:00:00 AAPL 100 @ 5.52001221952161&amp;quot;&#xA;## [1] &amp;quot;2016-09-26 00:00:00 AAPL 100 @ 5.38589556616076&amp;quot;&#xA;## [1] &amp;quot;2016-09-27 00:00:00 AAPL 100 @ 5.45150664908188&amp;quot;&#xA;## [1] &amp;quot;2016-09-28 00:00:00 AAPL 100 @ 5.48479472781825&amp;quot;&#xA;## [1] &amp;quot;2016-09-29 00:00:00 AAPL 100 @ 5.4592257732223&amp;quot;&#xA;## [1] &amp;quot;2016-09-30 00:00:00 AAPL 100 @ 5.42545515721014&amp;quot;&#xA;## [1] &amp;quot;2016-10-03 00:00:00 AAPL 100 @ 5.43751601262846&amp;quot;&#xA;## [1] &amp;quot;2016-10-04 00:00:00 AAPL 100 @ 5.45440113660049&amp;quot;&#xA;## [1] &amp;quot;2016-10-05 00:00:00 AAPL 100 @ 5.47080409136482&amp;quot;&#xA;## [1] &amp;quot;2016-10-06 00:00:00 AAPL 100 @ 5.48527689702595&amp;quot;&#xA;## [1] &amp;quot;2016-10-07 00:00:00 AAPL 100 @ 5.5147054136921&amp;quot;&#xA;## [1] &amp;quot;2016-10-10 00:00:00 AAPL 100 @ 5.54895819891197&amp;quot;&#xA;## [1] &amp;quot;2016-10-11 00:00:00 AAPL 100 @ 5.67825058371911&amp;quot;&#xA;## [1] &amp;quot;2016-10-12 00:00:00 AAPL 100 @ 5.66136545974708&amp;quot;&#xA;## [1] &amp;quot;2016-10-13 00:00:00 AAPL 100 @ 5.63434926139183&amp;quot;&#xA;## [1] &amp;quot;2016-10-14 00:00:00 AAPL 100 @ 5.68693441434303&amp;quot;&#xA;## [1] &amp;quot;2016-10-17 00:00:00 AAPL 100 @ 5.66040075326358&amp;quot;&#xA;## [1] &amp;quot;2016-10-18 00:00:00 AAPL 100 @ 5.70140758807226&amp;quot;&#xA;## [1] &amp;quot;2016-10-19 00:00:00 AAPL 100 @ 5.65654119119337&amp;quot;&#xA;## [1] &amp;quot;2016-10-20 00:00:00 AAPL 100 @ 5.63772628618624&amp;quot;&#xA;## [1] &amp;quot;2016-10-21 00:00:00 AAPL 100 @ 5.63531396787533&amp;quot;&#xA;## [1] &amp;quot;2016-10-24 00:00:00 AAPL 100 @ 5.64930460432876&amp;quot;&#xA;## [1] &amp;quot;2016-10-25 00:00:00 AAPL 100 @ 5.69031143913744&amp;quot;&#xA;## [1] &amp;quot;2016-10-26 00:00:00 AAPL 100 @ 5.5147054136921&amp;quot;&#xA;## [1] &amp;quot;2016-10-27 00:00:00 AAPL 100 @ 5.5668083974356&amp;quot;&#xA;## [1] &amp;quot;2016-10-28 00:00:00 AAPL 100 @ 5.49347855844216&amp;quot;&#xA;## [1] &amp;quot;2016-10-31 00:00:00 AAPL 100 @ 5.48286494678314&amp;quot;&#xA;## [1] &amp;quot;2016-11-29 00:00:00 AAPL 100 @ 5.45588040989116&amp;quot;&#xA;## [1] &amp;quot;2016-11-30 00:00:00 AAPL 100 @ 5.49626513926836&amp;quot;&#xA;## [1] &amp;quot;2016-12-01 00:00:00 AAPL 100 @ 5.43568823307522&amp;quot;&#xA;## [1] &amp;quot;2016-12-09 00:00:00 AAPL 100 @ 5.5312323728728&amp;quot;&#xA;## [1] &amp;quot;2016-12-12 00:00:00 AAPL 100 @ 5.57949723273594&amp;quot;&#xA;## [1] &amp;quot;2016-12-13 00:00:00 AAPL 100 @ 5.60658433585444&amp;quot;&#xA;## [1] &amp;quot;2016-12-14 00:00:00 AAPL 100 @ 5.6656841872631&amp;quot;&#xA;## [1] &amp;quot;2016-12-15 00:00:00 AAPL 100 @ 5.68242890092777&amp;quot;&#xA;## [1] &amp;quot;2016-12-16 00:00:00 AAPL 100 @ 5.73611125656367&amp;quot;&#xA;## [1] &amp;quot;2016-12-19 00:00:00 AAPL 100 @ 5.70311405558072&amp;quot;&#xA;## [1] &amp;quot;2016-12-20 00:00:00 AAPL 100 @ 5.74940850707707&amp;quot;&#xA;## [1] &amp;quot;2016-12-21 00:00:00 AAPL 100 @ 5.75236374388195&amp;quot;&#xA;## [1] &amp;quot;2016-12-22 00:00:00 AAPL 100 @ 5.73020115869921&amp;quot;&#xA;## [1] &amp;quot;2016-12-23 00:00:00 AAPL 100 @ 5.69277129038159&amp;quot;&#xA;## [1] &amp;quot;2016-12-27 00:00:00 AAPL 100 @ 5.73857351553155&amp;quot;&#xA;## [1] &amp;quot;2016-12-28 00:00:00 AAPL 100 @ 5.78782320383278&amp;quot;&#xA;## [1] &amp;quot;2016-12-29 00:00:00 AAPL 100 @ 5.73512605238027&amp;quot;&#xA;## [1] &amp;quot;2016-12-30 00:00:00 AAPL 100 @ 5.7449762154877&amp;quot;&#xA;## [1] &amp;quot;2017-01-03 00:00:00 AAPL 100 @ 5.70311405558072&amp;quot;&#xA;## [1] &amp;quot;2017-01-04 00:00:00 AAPL 100 @ 5.7055763145486&amp;quot;&#xA;## [1] &amp;quot;2017-01-05 00:00:00 AAPL 100 @ 5.70902377769987&amp;quot;&#xA;## [1] &amp;quot;2017-01-06 00:00:00 AAPL 100 @ 5.75137853969855&amp;quot;&#xA;## [1] &amp;quot;2017-01-09 00:00:00 AAPL 100 @ 5.80900058483212&amp;quot;&#xA;## [1] &amp;quot;2017-01-10 00:00:00 AAPL 100 @ 5.84938531420932&amp;quot;&#xA;## [1] &amp;quot;2017-01-11 00:00:00 AAPL 100 @ 5.84790788367953&amp;quot;&#xA;## [1] &amp;quot;2017-01-12 00:00:00 AAPL 100 @ 5.85578801416547&amp;quot;&#xA;## [1] &amp;quot;2017-01-13 00:00:00 AAPL 100 @ 5.86613040361929&amp;quot;&#xA;## [1] &amp;quot;2017-01-17 00:00:00 AAPL 100 @ 5.82820793320998&amp;quot;&#xA;## [1] &amp;quot;2017-01-18 00:00:00 AAPL 100 @ 5.90996259614777&amp;quot;&#xA;## [1] &amp;quot;2017-01-19 00:00:00 AAPL 100 @ 5.88041285831609&amp;quot;&#xA;## [1] &amp;quot;2017-01-20 00:00:00 AAPL 100 @ 5.9321248055852&amp;quot;&#xA;## [1] &amp;quot;2017-01-23 00:00:00 AAPL 100 @ 5.90996259614777&amp;quot;&#xA;## [1] &amp;quot;2017-01-24 00:00:00 AAPL 100 @ 5.88780038671033&amp;quot;&#xA;## [1] &amp;quot;2017-01-25 00:00:00 AAPL 100 @ 5.93064737505541&amp;quot;&#xA;## [1] &amp;quot;2017-01-26 00:00:00 AAPL 100 @ 5.99220948543195&amp;quot;&#xA;## [1] &amp;quot;2017-01-27 00:00:00 AAPL 100 @ 6.01535689905278&amp;quot;&#xA;## [1] &amp;quot;2017-01-30 00:00:00 AAPL 100 @ 5.95576482129772&amp;quot;&#xA;## [1] &amp;quot;2017-01-31 00:00:00 AAPL 100 @ 5.96659981284324&amp;quot;&#xA;## [1] &amp;quot;2017-02-01 00:00:00 AAPL 100 @ 6.25618784478617&amp;quot;&#xA;## [1] &amp;quot;2017-02-02 00:00:00 AAPL 100 @ 6.30297527411953&amp;quot;&#xA;## [1] &amp;quot;2017-02-03 00:00:00 AAPL 100 @ 6.3192273856925&amp;quot;&#xA;## [1] &amp;quot;2017-02-06 00:00:00 AAPL 100 @ 6.359612490815&amp;quot;&#xA;## [1] &amp;quot;2017-02-07 00:00:00 AAPL 100 @ 6.42905398018688&amp;quot;&#xA;## [1] &amp;quot;2017-02-08 00:00:00 AAPL 100 @ 6.46894685896299&amp;quot;&#xA;## [1] &amp;quot;2017-02-09 00:00:00 AAPL 100 @ 6.59764598716&amp;quot;&#xA;## [1] &amp;quot;2017-02-10 00:00:00 AAPL 100 @ 6.63823981976098&amp;quot;&#xA;## [1] &amp;quot;2017-02-13 00:00:00 AAPL 100 @ 6.66931090587274&amp;quot;&#xA;## [1] &amp;quot;2017-02-14 00:00:00 AAPL 100 @ 6.68885574466804&amp;quot;&#xA;## [1] &amp;quot;2017-02-15 00:00:00 AAPL 100 @ 6.79159174949255&amp;quot;&#xA;## [1] &amp;quot;2017-02-16 00:00:00 AAPL 100 @ 6.7991087010696&amp;quot;&#xA;## [1] &amp;quot;2017-02-17 00:00:00 AAPL 100 @ 6.77054352038184&amp;quot;&#xA;## [1] &amp;quot;2017-02-21 00:00:00 AAPL 100 @ 6.82717300655054&amp;quot;&#xA;## [1] &amp;quot;2017-02-22 00:00:00 AAPL 100 @ 6.8371958635516&amp;quot;&#xA;## [1] &amp;quot;2017-02-23 00:00:00 AAPL 100 @ 6.88480577252281&amp;quot;&#xA;## [1] &amp;quot;2017-02-24 00:00:00 AAPL 100 @ 6.81113658828785&amp;quot;&#xA;## [1] &amp;quot;2017-02-27 00:00:00 AAPL 100 @ 6.87277788530456&amp;quot;&#xA;## [1] &amp;quot;2017-02-28 00:00:00 AAPL 100 @ 6.86977110467374&amp;quot;&#xA;## [1] &amp;quot;2017-03-01 00:00:00 AAPL 100 @ 6.91036417257975&amp;quot;&#xA;## [1] &amp;quot;2017-03-02 00:00:00 AAPL 100 @ 7.01610695803508&amp;quot;&#xA;## [1] &amp;quot;2017-03-03 00:00:00 AAPL 100 @ 6.95496653622518&amp;quot;&#xA;## [1] &amp;quot;2017-03-06 00:00:00 AAPL 100 @ 6.98453423202153&amp;quot;&#xA;## [1] &amp;quot;2017-03-07 00:00:00 AAPL 100 @ 6.96899868896565&amp;quot;&#xA;## [1] &amp;quot;2017-03-08 00:00:00 AAPL 100 @ 6.96348600291082&amp;quot;&#xA;## [1] &amp;quot;2017-03-09 00:00:00 AAPL 100 @ 6.95296227070295&amp;quot;&#xA;## [1] &amp;quot;2017-03-10 00:00:00 AAPL 100 @ 6.97852067075989&amp;quot;&#xA;## [1] &amp;quot;2017-03-13 00:00:00 AAPL 100 @ 6.95847495675778&amp;quot;&#xA;## [1] &amp;quot;2017-03-14 00:00:00 AAPL 100 @ 6.9810265761839&amp;quot;&#xA;## [1] &amp;quot;2017-03-15 00:00:00 AAPL 100 @ 6.98653926223873&amp;quot;&#xA;## [1] &amp;quot;2017-03-16 00:00:00 AAPL 100 @ 7.05218985499486&amp;quot;&#xA;## [1] &amp;quot;2017-03-17 00:00:00 AAPL 100 @ 7.06622200773533&amp;quot;&#xA;## [1] &amp;quot;2017-03-20 00:00:00 AAPL 100 @ 7.03615267203719&amp;quot;&#xA;## [1] &amp;quot;2017-03-21 00:00:00 AAPL 100 @ 7.12184974349041&amp;quot;&#xA;## [1] &amp;quot;2017-03-22 00:00:00 AAPL 100 @ 7.00859000645803&amp;quot;&#xA;## [1] &amp;quot;2017-03-23 00:00:00 AAPL 100 @ 7.07925164536721&amp;quot;&#xA;## [1] &amp;quot;2017-03-24 00:00:00 AAPL 100 @ 7.09127953258545&amp;quot;&#xA;## [1] &amp;quot;2017-03-27 00:00:00 AAPL 100 @ 6.98553674713013&amp;quot;&#xA;## [1] &amp;quot;2017-03-28 00:00:00 AAPL 100 @ 7.0617118367891&amp;quot;&#xA;## [1] &amp;quot;2017-03-29 00:00:00 AAPL 100 @ 7.20052997387841&amp;quot;&#xA;## [1] &amp;quot;2017-03-30 00:00:00 AAPL 100 @ 7.22608913863032&amp;quot;&#xA;## [1] &amp;quot;2017-03-31 00:00:00 AAPL 100 @ 7.20253500409561&amp;quot;&#xA;## [1] &amp;quot;2017-04-03 00:00:00 AAPL 100 @ 7.2020341288888&amp;quot;&#xA;## [1] &amp;quot;2017-04-04 00:00:00 AAPL 100 @ 7.17898086956089&amp;quot;&#xA;## [1] &amp;quot;2017-04-05 00:00:00 AAPL 100 @ 7.22759252894573&amp;quot;&#xA;## [1] &amp;quot;2017-04-06 00:00:00 AAPL 100 @ 7.23110018478337&amp;quot;&#xA;## [1] &amp;quot;2017-04-07 00:00:00 AAPL 100 @ 7.20303587930242&amp;quot;&#xA;## [1] &amp;quot;2017-04-10 00:00:00 AAPL 100 @ 7.19652144283397&amp;quot;&#xA;## [1] &amp;quot;2017-04-11 00:00:00 AAPL 100 @ 7.16344532650501&amp;quot;&#xA;## [1] &amp;quot;2017-04-12 00:00:00 AAPL 100 @ 7.09629134343347&amp;quot;&#xA;## [1] &amp;quot;2017-04-13 00:00:00 AAPL 100 @ 7.11182688648935&amp;quot;&#xA;## [1] &amp;quot;2017-04-17 00:00:00 AAPL 100 @ 7.09027701747686&amp;quot;&#xA;## [1] &amp;quot;2017-04-18 00:00:00 AAPL 100 @ 7.08676936163923&amp;quot;&#xA;## [1] &amp;quot;2017-04-27 00:00:00 AAPL 100 @ 7.21255786109667&amp;quot;&#xA;## [1] &amp;quot;2017-04-28 00:00:00 AAPL 100 @ 7.22107732778231&amp;quot;&#xA;## [1] &amp;quot;2017-05-01 00:00:00 AAPL 100 @ 7.27169401738435&amp;quot;&#xA;## [1] &amp;quot;2017-05-02 00:00:00 AAPL 100 @ 7.39397409630918&amp;quot;&#xA;## [1] &amp;quot;2017-05-03 00:00:00 AAPL 100 @ 7.29624990233269&amp;quot;&#xA;## [1] &amp;quot;2017-05-04 00:00:00 AAPL 100 @ 7.34285729619531&amp;quot;&#xA;## [1] &amp;quot;2017-05-05 00:00:00 AAPL 100 @ 7.35488441871858&amp;quot;&#xA;## [1] &amp;quot;2017-05-08 00:00:00 AAPL 100 @ 7.46864579565275&amp;quot;&#xA;## [1] &amp;quot;2017-05-09 00:00:00 AAPL 100 @ 7.71120245267516&amp;quot;&#xA;## [1] &amp;quot;2017-05-10 00:00:00 AAPL 100 @ 7.69917533015189&amp;quot;&#xA;## [1] &amp;quot;2017-05-11 00:00:00 AAPL 100 @ 7.76776173867606&amp;quot;&#xA;## [1] &amp;quot;2017-05-12 00:00:00 AAPL 100 @ 7.88240564987243&amp;quot;&#xA;## [1] &amp;quot;2017-05-15 00:00:00 AAPL 100 @ 7.94915375821682&amp;quot;&#xA;## [1] &amp;quot;2017-05-16 00:00:00 AAPL 100 @ 7.94558746304634&amp;quot;&#xA;## [1] &amp;quot;2017-05-17 00:00:00 AAPL 100 @ 7.82635798199703&amp;quot;&#xA;## [1] &amp;quot;2017-05-18 00:00:00 AAPL 100 @ 7.70763774954955&amp;quot;&#xA;## [1] &amp;quot;2017-05-19 00:00:00 AAPL 100 @ 7.81514829292619&amp;quot;&#xA;## [1] &amp;quot;2017-05-22 00:00:00 AAPL 100 @ 7.84673881077376&amp;quot;&#xA;## [1] &amp;quot;2017-05-23 00:00:00 AAPL 100 @ 7.89259606426079&amp;quot;&#xA;## [1] &amp;quot;2017-05-24 00:00:00 AAPL 100 @ 7.83858616827155&amp;quot;&#xA;## [1] &amp;quot;2017-05-25 00:00:00 AAPL 100 @ 7.83298132373613&amp;quot;&#xA;## [1] &amp;quot;2017-05-26 00:00:00 AAPL 100 @ 7.84673881077376&amp;quot;&#xA;## [1] &amp;quot;2017-05-30 00:00:00 AAPL 100 @ 7.81718606481235&amp;quot;&#xA;## [1] &amp;quot;2017-05-31 00:00:00 AAPL 100 @ 7.84521028748944&amp;quot;&#xA;## [1] &amp;quot;2017-06-01 00:00:00 AAPL 100 @ 7.8044478524572&amp;quot;&#xA;## [1] &amp;quot;2017-06-02 00:00:00 AAPL 100 @ 7.82533870731456&amp;quot;&#xA;## [1] &amp;quot;2017-06-05 00:00:00 AAPL 100 @ 7.86406259298186&amp;quot;&#xA;## [1] &amp;quot;2017-06-06 00:00:00 AAPL 100 @ 7.84164321484019&amp;quot;&#xA;## [1] &amp;quot;2017-06-07 00:00:00 AAPL 100 @ 7.89871093487684&amp;quot;&#xA;## [1] &amp;quot;2017-06-08 00:00:00 AAPL 100 @ 7.91042987254952&amp;quot;&#xA;## [1] &amp;quot;2017-06-09 00:00:00 AAPL 100 @ 7.90737282598089&amp;quot;&#xA;## [1] &amp;quot;2017-06-12 00:00:00 AAPL 100 @ 7.42586855445189&amp;quot;&#xA;## [1] &amp;quot;2017-07-19 00:00:00 AAPL 100 @ 7.66738456311916&amp;quot;&#xA;## [1] &amp;quot;2017-07-20 00:00:00 AAPL 100 @ 7.71935668722224&amp;quot;&#xA;## [1] &amp;quot;2017-07-21 00:00:00 AAPL 100 @ 7.64241816448948&amp;quot;&#xA;## [1] &amp;quot;2017-07-24 00:00:00 AAPL 100 @ 7.67248015905273&amp;quot;&#xA;## [1] &amp;quot;2017-07-25 00:00:00 AAPL 100 @ 7.73464269754418&amp;quot;&#xA;## [1] &amp;quot;2017-07-26 00:00:00 AAPL 100 @ 7.81361976964187&amp;quot;&#xA;## [1] &amp;quot;2017-07-27 00:00:00 AAPL 100 @ 7.83400059841861&amp;quot;&#xA;## [1] &amp;quot;2017-07-28 00:00:00 AAPL 100 @ 7.6373225685559&amp;quot;&#xA;## [1] &amp;quot;2017-07-31 00:00:00 AAPL 100 @ 7.63783181715775&amp;quot;&#xA;## [1] &amp;quot;2017-08-01 00:00:00 AAPL 100 @ 7.59707015960429&amp;quot;&#xA;## [1] &amp;quot;2017-08-02 00:00:00 AAPL 100 @ 8.11576979351627&amp;quot;&#xA;## [1] &amp;quot;2017-08-03 00:00:00 AAPL 100 @ 8.00214515700237&amp;quot;&#xA;## [1] &amp;quot;2017-08-04 00:00:00 AAPL 100 @ 7.95221158226423&amp;quot;&#xA;## [1] &amp;quot;2017-08-07 00:00:00 AAPL 100 @ 8.00265440560422&amp;quot;&#xA;## [1] &amp;quot;2017-08-08 00:00:00 AAPL 100 @ 8.08112222910007&amp;quot;&#xA;## [1] &amp;quot;2017-08-09 00:00:00 AAPL 100 @ 8.1147505188338&amp;quot;&#xA;## [1] &amp;quot;2017-08-10 00:00:00 AAPL 100 @ 8.27686294981978&amp;quot;&#xA;## [1] &amp;quot;2017-08-11 00:00:00 AAPL 100 @ 8.10604651616712&amp;quot;&#xA;## [1] &amp;quot;2017-08-14 00:00:00 AAPL 100 @ 8.24684125154201&amp;quot;&#xA;## [1] &amp;quot;2017-08-15 00:00:00 AAPL 100 @ 8.31620314313258&amp;quot;&#xA;## [1] &amp;quot;2017-08-16 00:00:00 AAPL 100 @ 8.38245939626937&amp;quot;&#xA;## [1] &amp;quot;2017-08-17 00:00:00 AAPL 100 @ 8.30895639012815&amp;quot;&#xA;## [1] &amp;quot;2017-08-18 00:00:00 AAPL 100 @ 8.17126729320703&amp;quot;&#xA;## [1] &amp;quot;2017-08-21 00:00:00 AAPL 100 @ 8.15263267264751&amp;quot;&#xA;## [1] &amp;quot;2017-08-22 00:00:00 AAPL 100 @ 8.19041925689658&amp;quot;&#xA;## [1] &amp;quot;2017-08-23 00:00:00 AAPL 100 @ 8.23390056476003&amp;quot;&#xA;## [1] &amp;quot;2017-08-24 00:00:00 AAPL 100 @ 8.30429714261063&amp;quot;&#xA;## [1] &amp;quot;2017-08-25 00:00:00 AAPL 100 @ 8.2639222630378&amp;quot;&#xA;## [1] &amp;quot;2017-08-28 00:00:00 AAPL 100 @ 8.28928629347175&amp;quot;&#xA;## [1] &amp;quot;2017-08-29 00:00:00 AAPL 100 @ 8.28721613111484&amp;quot;&#xA;## [1] &amp;quot;2017-08-30 00:00:00 AAPL 100 @ 8.47873813752078&amp;quot;&#xA;## [1] &amp;quot;2017-08-31 00:00:00 AAPL 100 @ 8.47045590841947&amp;quot;&#xA;## [1] &amp;quot;2017-09-01 00:00:00 AAPL 100 @ 8.5305008846487&amp;quot;&#xA;## [1] &amp;quot;2017-09-05 00:00:00 AAPL 100 @ 8.47614984219701&amp;quot;&#xA;## [1] &amp;quot;2017-09-06 00:00:00 AAPL 100 @ 8.42231693271219&amp;quot;&#xA;## [1] &amp;quot;2017-09-07 00:00:00 AAPL 100 @ 8.39022349240382&amp;quot;&#xA;## [1] &amp;quot;2017-09-08 00:00:00 AAPL 100 @ 8.3265555345908&amp;quot;&#xA;## [1] &amp;quot;2017-09-11 00:00:00 AAPL 100 @ 8.30792091403127&amp;quot;&#xA;## [1] &amp;quot;2017-09-12 00:00:00 AAPL 100 @ 8.41714034206466&amp;quot;&#xA;## [1] &amp;quot;2017-09-13 00:00:00 AAPL 100 @ 8.27531013059289&amp;quot;&#xA;## [1] &amp;quot;2017-09-14 00:00:00 AAPL 100 @ 8.22975945020937&amp;quot;&#xA;## [1] &amp;quot;2017-10-17 00:00:00 AAPL 100 @ 8.27065167291222&amp;quot;&#xA;## [1] &amp;quot;2017-10-18 00:00:00 AAPL 100 @ 8.30377979948062&amp;quot;&#xA;## [1] &amp;quot;2017-10-19 00:00:00 AAPL 100 @ 8.11381061230157&amp;quot;&#xA;## [1] &amp;quot;2017-10-20 00:00:00 AAPL 100 @ 8.10656385929713&amp;quot;&#xA;## [1] &amp;quot;2017-10-23 00:00:00 AAPL 100 @ 8.121057365306&amp;quot;&#xA;## [1] &amp;quot;2017-10-24 00:00:00 AAPL 100 @ 8.08999940109452&amp;quot;&#xA;## [1] &amp;quot;2017-10-25 00:00:00 AAPL 100 @ 8.12209284140288&amp;quot;&#xA;## [1] &amp;quot;2017-10-26 00:00:00 AAPL 100 @ 8.13865650976866&amp;quot;&#xA;## [1] &amp;quot;2017-10-27 00:00:00 AAPL 100 @ 8.24528764247828&amp;quot;&#xA;## [1] &amp;quot;2017-10-30 00:00:00 AAPL 100 @ 8.48339659520145&amp;quot;&#xA;## [1] &amp;quot;2017-10-31 00:00:00 AAPL 100 @ 8.69096492684315&amp;quot;&#xA;## [1] &amp;quot;2017-11-01 00:00:00 AAPL 100 @ 8.7929376018721&amp;quot;&#xA;## [1] &amp;quot;2017-11-02 00:00:00 AAPL 100 @ 8.62367398744632&amp;quot;&#xA;## [1] &amp;quot;2017-11-03 00:00:00 AAPL 100 @ 9.0067180002582&amp;quot;&#xA;## [1] &amp;quot;2017-11-06 00:00:00 AAPL 100 @ 8.9223444696919&amp;quot;&#xA;## [1] &amp;quot;2017-11-07 00:00:00 AAPL 100 @ 9.00205954257753&amp;quot;&#xA;## [1] &amp;quot;2017-11-08 00:00:00 AAPL 100 @ 9.04088160292347&amp;quot;&#xA;## [1] &amp;quot;2017-11-09 00:00:00 AAPL 100 @ 9.06417468116367&amp;quot;&#xA;## [1] &amp;quot;2017-11-10 00:00:00 AAPL 100 @ 9.19593356183498&amp;quot;&#xA;## [1] &amp;quot;2017-11-13 00:00:00 AAPL 100 @ 9.1113840866725&amp;quot;&#xA;## [1] &amp;quot;2017-11-14 00:00:00 AAPL 100 @ 9.0872267503469&amp;quot;&#xA;## [1] &amp;quot;2017-11-15 00:00:00 AAPL 100 @ 8.92600555812115&amp;quot;&#xA;## [1] &amp;quot;2017-11-16 00:00:00 AAPL 100 @ 8.98954847967048&amp;quot;&#xA;## [1] &amp;quot;2017-11-17 00:00:00 AAPL 100 @ 8.9821963862354&amp;quot;&#xA;## [1] &amp;quot;2017-11-20 00:00:00 AAPL 100 @ 8.94280999969359&amp;quot;&#xA;## [1] &amp;quot;2017-11-21 00:00:00 AAPL 100 @ 8.96854272737541&amp;quot;&#xA;## [1] &amp;quot;2017-11-22 00:00:00 AAPL 100 @ 9.10403199323742&amp;quot;&#xA;## [1] &amp;quot;2017-11-24 00:00:00 AAPL 100 @ 9.19540869848893&amp;quot;&#xA;## [1] &amp;quot;2017-11-27 00:00:00 AAPL 100 @ 9.19278277912253&amp;quot;&#xA;## [1] &amp;quot;2017-11-28 00:00:00 AAPL 100 @ 9.15339639258072&amp;quot;&#xA;## [1] &amp;quot;2017-11-29 00:00:00 AAPL 100 @ 9.06569613470578&amp;quot;&#xA;## [1] &amp;quot;2017-11-30 00:00:00 AAPL 100 @ 8.95016209312867&amp;quot;&#xA;## [1] &amp;quot;2017-12-01 00:00:00 AAPL 100 @ 8.92495503011097&amp;quot;&#xA;## [1] &amp;quot;2017-12-04 00:00:00 AAPL 100 @ 9.05781837660657&amp;quot;&#xA;## [1] &amp;quot;2017-12-05 00:00:00 AAPL 100 @ 8.87821655013408&amp;quot;&#xA;## [1] &amp;quot;2017-12-06 00:00:00 AAPL 100 @ 8.796292994338&amp;quot;&#xA;## [1] &amp;quot;2017-12-18 00:00:00 AAPL 100 @ 9.18385529433122&amp;quot;&#xA;## [1] &amp;quot;2017-12-19 00:00:00 AAPL 100 @ 9.19173225111235&amp;quot;&#xA;## [1] &amp;quot;2017-12-20 00:00:00 AAPL 100 @ 9.18332962966709&amp;quot;&#xA;## [1] &amp;quot;2017-12-21 00:00:00 AAPL 100 @ 9.14656916249168&amp;quot;&#xA;## [1] &amp;quot;2017-12-22 00:00:00 AAPL 100 @ 9.1733516168656&amp;quot;&#xA;## [1] &amp;quot;2017-12-26 00:00:00 AAPL 100 @ 8.96959325538559&amp;quot;&#xA;## [1] &amp;quot;2017-12-27 00:00:00 AAPL 100 @ 8.93283278821018&amp;quot;&#xA;## [1] &amp;quot;2017-12-28 00:00:00 AAPL 100 @ 8.98009613153313&amp;quot;&#xA;## [1] &amp;quot;2017-12-29 00:00:00 AAPL 100 @ 8.95488906851543&amp;quot;&#xA;## [1] &amp;quot;2018-01-02 00:00:00 AAPL 100 @ 8.93598357092264&amp;quot;&#xA;## [1] &amp;quot;2018-01-10 00:00:00 AAPL 100 @ 9.09352911708988&amp;quot;&#xA;## [1] &amp;quot;2018-01-11 00:00:00 AAPL 100 @ 9.16862544279692&amp;quot;&#xA;## [1] &amp;quot;2018-01-12 00:00:00 AAPL 100 @ 9.25212438994923&amp;quot;&#xA;## [1] &amp;quot;2018-01-16 00:00:00 AAPL 100 @ 9.34245056719056&amp;quot;&#xA;## [1] &amp;quot;2018-01-17 00:00:00 AAPL 100 @ 9.250548998593&amp;quot;&#xA;## [1] &amp;quot;2018-01-18 00:00:00 AAPL 100 @ 9.41964794891796&amp;quot;&#xA;## [1] &amp;quot;2018-01-19 00:00:00 AAPL 100 @ 9.3797366990301&amp;quot;&#xA;## [1] &amp;quot;2018-01-22 00:00:00 AAPL 100 @ 9.31094193874796&amp;quot;&#xA;## [1] &amp;quot;2018-01-23 00:00:00 AAPL 100 @ 9.31094193874796&amp;quot;&#xA;## [1] &amp;quot;2018-01-24 00:00:00 AAPL 100 @ 9.30831601938156&amp;quot;&#xA;## [1] &amp;quot;2018-01-25 00:00:00 AAPL 100 @ 9.16442413207429&amp;quot;&#xA;## [1] &amp;quot;2018-01-26 00:00:00 AAPL 100 @ 9.03261131358887&amp;quot;&#xA;## [1] &amp;quot;2018-01-29 00:00:00 AAPL 100 @ 8.93598357092264&amp;quot;&#xA;## [1] &amp;quot;2018-02-06 00:00:00 AAPL -100 @ 8.13092573384983&amp;quot;&#xA;## [1] &amp;quot;2018-02-09 00:00:00 AAPL -100 @ 8.38474801484488&amp;quot;&#xA;## [1] &amp;quot;2018-02-21 00:00:00 AAPL 100 @ 9.22605174243937&amp;quot;&#xA;## [1] &amp;quot;2018-02-22 00:00:00 AAPL 100 @ 9.17106810573414&amp;quot;&#xA;## [1] &amp;quot;2018-02-23 00:00:00 AAPL 100 @ 9.27089262420024&amp;quot;&#xA;## [1] &amp;quot;2018-02-26 00:00:00 AAPL 100 @ 9.41395743709461&amp;quot;&#xA;## [1] &amp;quot;2018-02-27 00:00:00 AAPL 100 @ 9.5607585828616&amp;quot;&#xA;## [1] &amp;quot;2018-02-28 00:00:00 AAPL 100 @ 9.56929912137682&amp;quot;&#xA;## [1] &amp;quot;2018-03-01 00:00:00 AAPL 100 @ 9.53086384713942&amp;quot;&#xA;## [1] &amp;quot;2018-03-02 00:00:00 AAPL 100 @ 9.2244503405585&amp;quot;&#xA;## [1] &amp;quot;2018-03-05 00:00:00 AAPL 100 @ 9.35310172197678&amp;quot;&#xA;## [1] &amp;quot;2018-03-06 00:00:00 AAPL 100 @ 9.49723359309289&amp;quot;&#xA;## [1] &amp;quot;2018-03-07 00:00:00 AAPL 100 @ 9.33868829050069&amp;quot;&#xA;## [1] &amp;quot;2018-03-08 00:00:00 AAPL 100 @ 9.36751433890461&amp;quot;&#xA;## [1] &amp;quot;2018-03-09 00:00:00 AAPL 100 @ 9.49990286774376&amp;quot;&#xA;## [1] &amp;quot;2018-03-12 00:00:00 AAPL 100 @ 9.62428275808204&amp;quot;&#xA;## [1] &amp;quot;2018-03-13 00:00:00 AAPL 100 @ 9.74706206108771&amp;quot;&#xA;## [1] &amp;quot;2018-03-14 00:00:00 AAPL 100 @ 9.62588497451117&amp;quot;&#xA;## [1] &amp;quot;2018-03-15 00:00:00 AAPL 100 @ 9.52872891614768&amp;quot;&#xA;## [1] &amp;quot;2018-03-16 00:00:00 AAPL 100 @ 9.53673592555203&amp;quot;&#xA;## [1] &amp;quot;2018-03-19 00:00:00 AAPL 100 @ 9.4657382700381&amp;quot;&#xA;## [1] &amp;quot;2018-03-20 00:00:00 AAPL 100 @ 9.35470312385765&amp;quot;&#xA;## [1] &amp;quot;2018-03-21 00:00:00 AAPL 100 @ 9.34402602525417&amp;quot;&#xA;## [1] &amp;quot;2018-03-22 00:00:00 AAPL 100 @ 9.07497992014065&amp;quot;&#xA;## [1] &amp;quot;2018-04-16 00:00:00 AAPL 100 @ 9.3434924961433&amp;quot;&#xA;## [1] &amp;quot;2018-04-17 00:00:00 AAPL 100 @ 9.4214309173881&amp;quot;&#xA;## [1] &amp;quot;2018-04-18 00:00:00 AAPL 100 @ 9.49189504379115&amp;quot;&#xA;## [1] &amp;quot;2018-04-19 00:00:00 AAPL 100 @ 9.27569682984285&amp;quot;&#xA;## [1] &amp;quot;2018-04-20 00:00:00 AAPL 100 @ 9.10700958685456&amp;quot;&#xA;## [1] &amp;quot;2018-04-23 00:00:00 AAPL 100 @ 8.90575833349322&amp;quot;&#xA;## [1] &amp;quot;2018-04-30 00:00:00 AAPL -100 @ 8.6548619927284&amp;quot;&#xA;## [1] &amp;quot;2018-05-07 00:00:00 AAPL 100 @ 9.88532185379121&amp;quot;&#xA;## [1] &amp;quot;2018-05-08 00:00:00 AAPL 100 @ 9.87517991339513&amp;quot;&#xA;## [1] &amp;quot;2018-05-09 00:00:00 AAPL 100 @ 9.9584560693934&amp;quot;&#xA;## [1] &amp;quot;2018-05-10 00:00:00 AAPL 100 @ 10.0219810591621&amp;quot;&#xA;## [1] &amp;quot;2018-05-11 00:00:00 AAPL 100 @ 10.2732503814584&amp;quot;&#xA;## [1] &amp;quot;2018-05-14 00:00:00 AAPL 100 @ 10.2472264598507&amp;quot;&#xA;## [1] &amp;quot;2018-05-15 00:00:00 AAPL 100 @ 10.1263266561963&amp;quot;&#xA;## [1] &amp;quot;2018-05-16 00:00:00 AAPL 100 @ 10.0878342831145&amp;quot;&#xA;## [1] &amp;quot;2018-05-17 00:00:00 AAPL 100 @ 10.1924693425789&amp;quot;&#xA;## [1] &amp;quot;2018-05-18 00:00:00 AAPL 100 @ 10.1485551123473&amp;quot;&#xA;## [1] &amp;quot;2018-05-21 00:00:00 AAPL 100 @ 10.1924693425789&amp;quot;&#xA;## [1] &amp;quot;2018-05-22 00:00:00 AAPL 100 @ 10.2130714070368&amp;quot;&#xA;## [1] &amp;quot;2018-05-23 00:00:00 AAPL 100 @ 10.1030144904227&amp;quot;&#xA;## [1] &amp;quot;2018-05-24 00:00:00 AAPL 100 @ 10.234215326306&amp;quot;&#xA;## [1] &amp;quot;2018-05-25 00:00:00 AAPL 100 @ 10.2049386213122&amp;quot;&#xA;## [1] &amp;quot;2018-05-29 00:00:00 AAPL 100 @ 10.1707835684983&amp;quot;&#xA;## [1] &amp;quot;2018-05-30 00:00:00 AAPL 100 @ 10.1772891352706&amp;quot;&#xA;## [1] &amp;quot;2018-05-31 00:00:00 AAPL 100 @ 10.1501815040404&amp;quot;&#xA;## [1] &amp;quot;2018-06-01 00:00:00 AAPL 100 @ 10.1919274877676&amp;quot;&#xA;## [1] &amp;quot;2018-06-04 00:00:00 AAPL 100 @ 10.3898128648448&amp;quot;&#xA;## [1] &amp;quot;2018-06-05 00:00:00 AAPL 100 @ 10.4673411203382&amp;quot;&#xA;## [1] &amp;quot;2018-06-06 00:00:00 AAPL 100 @ 10.4977015349546&amp;quot;&#xA;## [1] &amp;quot;2018-06-07 00:00:00 AAPL 100 @ 10.5253510209961&amp;quot;&#xA;## [1] &amp;quot;2018-06-08 00:00:00 AAPL 100 @ 10.3643316253076&amp;quot;&#xA;## [1] &amp;quot;2018-06-11 00:00:00 AAPL 100 @ 10.3740908027253&amp;quot;&#xA;## [1] &amp;quot;2018-06-12 00:00:00 AAPL 100 @ 10.3762590492297&amp;quot;&#xA;## [1] &amp;quot;2018-06-13 00:00:00 AAPL 100 @ 10.4321007033833&amp;quot;&#xA;## [1] &amp;quot;2018-06-14 00:00:00 AAPL 100 @ 10.3849336897656&amp;quot;&#xA;## [1] &amp;quot;2018-06-15 00:00:00 AAPL 100 @ 10.302526259193&amp;quot;&#xA;## [1] &amp;quot;2018-06-18 00:00:00 AAPL 100 @ 10.1859637758066&amp;quot;&#xA;## [1] &amp;quot;2018-06-19 00:00:00 AAPL 100 @ 10.0374136588514&amp;quot;&#xA;## [1] &amp;quot;2018-07-11 00:00:00 AAPL 100 @ 10.2195769738091&amp;quot;&#xA;## [1] &amp;quot;2018-07-12 00:00:00 AAPL 100 @ 10.2754186279627&amp;quot;&#xA;## [1] &amp;quot;2018-07-13 00:00:00 AAPL 100 @ 10.3594524502284&amp;quot;&#xA;## [1] &amp;quot;2018-07-16 00:00:00 AAPL 100 @ 10.3833072980725&amp;quot;&#xA;## [1] &amp;quot;2018-07-17 00:00:00 AAPL 100 @ 10.2873460518848&amp;quot;&#xA;## [1] &amp;quot;2018-07-18 00:00:00 AAPL 100 @ 10.3974029684989&amp;quot;&#xA;## [1] &amp;quot;2018-07-19 00:00:00 AAPL 100 @ 10.2840932684986&amp;quot;&#xA;## [1] &amp;quot;2018-07-20 00:00:00 AAPL 100 @ 10.3974029684989&amp;quot;&#xA;## [1] &amp;quot;2018-07-23 00:00:00 AAPL 100 @ 10.3377658488886&amp;quot;&#xA;## [1] &amp;quot;2018-07-24 00:00:00 AAPL 100 @ 10.4337270950763&amp;quot;&#xA;## [1] &amp;quot;2018-07-25 00:00:00 AAPL 100 @ 10.4667984382676&amp;quot;&#xA;## [1] &amp;quot;2018-07-26 00:00:00 AAPL 100 @ 10.5508322605333&amp;quot;&#xA;## [1] &amp;quot;2018-07-27 00:00:00 AAPL 100 @ 10.5714343249913&amp;quot;&#xA;## [1] &amp;quot;2018-07-30 00:00:00 AAPL 100 @ 10.4039085352712&amp;quot;&#xA;## [1] &amp;quot;2018-07-31 00:00:00 AAPL 100 @ 10.3171646116899&amp;quot;&#xA;## [1] &amp;quot;2018-08-01 00:00:00 AAPL 100 @ 10.7958854784875&amp;quot;&#xA;## [1] &amp;quot;2018-08-02 00:00:00 AAPL 100 @ 10.8744974436034&amp;quot;&#xA;## [1] &amp;quot;2018-08-03 00:00:00 AAPL 100 @ 11.2241857210219&amp;quot;&#xA;## [1] &amp;quot;2018-08-06 00:00:00 AAPL 100 @ 11.2767745917894&amp;quot;&#xA;## [1] &amp;quot;2018-08-07 00:00:00 AAPL 100 @ 11.3483391353217&amp;quot;&#xA;## [1] &amp;quot;2018-08-08 00:00:00 AAPL 100 @ 11.1710549954432&amp;quot;&#xA;## [1] &amp;quot;2018-08-09 00:00:00 AAPL 100 @ 11.3597238771732&amp;quot;&#xA;## [1] &amp;quot;2018-08-10 00:00:00 AAPL 100 @ 11.4014615123911&amp;quot;&#xA;## [1] &amp;quot;2018-08-13 00:00:00 AAPL 100 @ 11.5086799493569&amp;quot;&#xA;## [1] &amp;quot;2018-08-14 00:00:00 AAPL 100 @ 11.5554165998488&amp;quot;&#xA;## [1] &amp;quot;2018-08-15 00:00:00 AAPL 100 @ 11.5037315997249&amp;quot;&#xA;## [1] &amp;quot;2018-08-16 00:00:00 AAPL 100 @ 11.6428407992988&amp;quot;&#xA;## [1] &amp;quot;2018-08-17 00:00:00 AAPL 100 @ 11.7357637243317&amp;quot;&#xA;## [1] &amp;quot;2018-08-20 00:00:00 AAPL 100 @ 11.9919888991231&amp;quot;&#xA;## [1] &amp;quot;2018-08-21 00:00:00 AAPL 100 @ 11.9205096614834&amp;quot;&#xA;## [1] &amp;quot;2018-08-22 00:00:00 AAPL 100 @ 11.7720532996086&amp;quot;&#xA;## [1] &amp;quot;2018-08-23 00:00:00 AAPL 100 @ 11.8022937733517&amp;quot;&#xA;## [1] &amp;quot;2018-08-24 00:00:00 AAPL 100 @ 11.9095130493052&amp;quot;&#xA;## [1] &amp;quot;2018-08-27 00:00:00 AAPL 100 @ 11.9397535230482&amp;quot;&#xA;## [1] &amp;quot;2018-08-28 00:00:00 AAPL 100 @ 12.042023610382&amp;quot;&#xA;## [1] &amp;quot;2018-08-29 00:00:00 AAPL 100 @ 12.1047052226841&amp;quot;&#xA;## [1] &amp;quot;2018-08-30 00:00:00 AAPL 100 @ 12.2751556479029&amp;quot;&#xA;## [1] &amp;quot;2018-08-31 00:00:00 AAPL 100 @ 12.4544028594717&amp;quot;&#xA;## [1] &amp;quot;2018-09-04 00:00:00 AAPL 100 @ 12.5588727726337&amp;quot;&#xA;## [1] &amp;quot;2018-09-05 00:00:00 AAPL 100 @ 12.5907635352418&amp;quot;&#xA;## [1] &amp;quot;2018-09-06 00:00:00 AAPL 100 @ 12.4390074346247&amp;quot;&#xA;## [1] &amp;quot;2018-09-07 00:00:00 AAPL 100 @ 12.198178523668&amp;quot;&#xA;## [1] &amp;quot;2018-09-10 00:00:00 AAPL 100 @ 12.1486925103846&amp;quot;&#xA;## [1] &amp;quot;2018-09-11 00:00:00 AAPL 100 @ 11.9870397105034&amp;quot;&#xA;## [1] &amp;quot;2018-09-12 00:00:00 AAPL 100 @ 12.3680785729359&amp;quot;&#xA;## [1] &amp;quot;2018-09-13 00:00:00 AAPL 100 @ 12.2900015357867&amp;quot;&#xA;## [1] &amp;quot;2018-09-14 00:00:00 AAPL 100 @ 12.4126153975995&amp;quot;&#xA;## [1] &amp;quot;2018-09-17 00:00:00 AAPL 100 @ 12.2146730224414&amp;quot;&#xA;## [1] &amp;quot;2018-09-18 00:00:00 AAPL 100 @ 11.974943185411&amp;quot;&#xA;## [1] &amp;quot;2018-09-19 00:00:00 AAPL 100 @ 12.0139821234795&amp;quot;&#xA;## [1] &amp;quot;2018-10-01 00:00:00 AAPL 100 @ 12.5335798095349&amp;quot;&#xA;## [1] &amp;quot;2018-10-02 00:00:00 AAPL 100 @ 12.4950912474174&amp;quot;&#xA;## [1] &amp;quot;2018-10-03 00:00:00 AAPL 100 @ 12.6490463348751&amp;quot;&#xA;## [1] &amp;quot;2018-10-04 00:00:00 AAPL 100 @ 12.68918434687&amp;quot;&#xA;## [1] &amp;quot;2018-10-05 00:00:00 AAPL 100 @ 12.5341301854859&amp;quot;&#xA;## [1] &amp;quot;2018-10-08 00:00:00 AAPL 100 @ 12.2179727611838&amp;quot;&#xA;## [1] &amp;quot;2018-10-09 00:00:00 AAPL 100 @ 12.2965993352961&amp;quot;&#xA;## [1] &amp;quot;2018-10-10 00:00:00 AAPL 100 @ 12.3966704357893&amp;quot;&#xA;## [1] &amp;quot;2018-10-11 00:00:00 AAPL 100 @ 11.7951464368791&amp;quot;&#xA;## [1] &amp;quot;2018-10-12 00:00:00 AAPL 100 @ 12.1195511105679&amp;quot;&#xA;## [1] &amp;quot;2018-11-15 00:00:00 AAPL -100 @ 10.5045144238192&amp;quot;&#xA;## [1] &amp;quot;2018-11-20 00:00:00 AAPL -100 @ 9.94580493951639&amp;quot;&#xA;## [1] &amp;quot;2018-11-21 00:00:00 AAPL -100 @ 10.0216377654314&amp;quot;&#xA;## [1] &amp;quot;2018-11-23 00:00:00 AAPL -100 @ 9.75455058603122&amp;quot;&#xA;## [1] &amp;quot;2018-11-26 00:00:00 AAPL -100 @ 9.71551917213843&amp;quot;&#xA;## [1] &amp;quot;2018-11-27 00:00:00 AAPL -100 @ 9.56329538172504&amp;quot;&#xA;## [1] &amp;quot;2018-11-28 00:00:00 AAPL -100 @ 9.85435954804428&amp;quot;&#xA;## [1] &amp;quot;2018-11-29 00:00:00 AAPL -100 @ 10.1850132668397&amp;quot;&#xA;## [1] &amp;quot;2018-11-30 00:00:00 AAPL -100 @ 10.0528628965456&amp;quot;&#xA;## [1] &amp;quot;2018-12-03 00:00:00 AAPL -100 @ 10.2853803674362&amp;quot;&#xA;## [1] &amp;quot;2018-12-04 00:00:00 AAPL -100 @ 10.0896643085678&amp;quot;&#xA;## [1] &amp;quot;2018-12-06 00:00:00 AAPL -100 @ 9.57723523317397&amp;quot;&#xA;## [1] &amp;quot;2018-12-07 00:00:00 AAPL -100 @ 9.67369961779166&amp;quot;&#xA;## [1] &amp;quot;2018-12-10 00:00:00 AAPL -100 @ 9.20030195629059&amp;quot;&#xA;## [1] &amp;quot;2018-12-11 00:00:00 AAPL -100 @ 9.571659803087&amp;quot;&#xA;## [1] &amp;quot;2018-12-12 00:00:00 AAPL -100 @ 9.50140240725897&amp;quot;&#xA;## [1] &amp;quot;2018-12-13 00:00:00 AAPL -100 @ 9.50642140040455&amp;quot;&#xA;## [1] &amp;quot;2018-12-14 00:00:00 AAPL -100 @ 9.4233395794734&amp;quot;&#xA;## [1] &amp;quot;2018-12-17 00:00:00 AAPL -100 @ 9.22539351873446&amp;quot;&#xA;## [1] &amp;quot;2018-12-18 00:00:00 AAPL -100 @ 9.22149080275568&amp;quot;&#xA;## [1] &amp;quot;2018-12-19 00:00:00 AAPL -100 @ 9.25606136208629&amp;quot;&#xA;## [1] &amp;quot;2018-12-20 00:00:00 AAPL -100 @ 8.94380834930197&amp;quot;&#xA;## [1] &amp;quot;2018-12-21 00:00:00 AAPL -100 @ 8.74642042714643&amp;quot;&#xA;## [1] &amp;quot;2018-12-24 00:00:00 AAPL -100 @ 8.26075562830463&amp;quot;&#xA;## [1] &amp;quot;2018-12-26 00:00:00 AAPL -100 @ 8.2691200496666&amp;quot;&#xA;## [1] &amp;quot;2018-12-27 00:00:00 AAPL -100 @ 8.68954559500493&amp;quot;&#xA;## [1] &amp;quot;2018-12-28 00:00:00 AAPL -100 @ 8.78210641282284&amp;quot;&#xA;## [1] &amp;quot;2018-12-31 00:00:00 AAPL -100 @ 8.83953853272673&amp;quot;&#xA;## [1] &amp;quot;2019-01-02 00:00:00 AAPL -100 @ 8.63657432966322&amp;quot;&#xA;## [1] &amp;quot;2019-01-03 00:00:00 AAPL -100 @ 8.02823900823508&amp;quot;&#xA;## [1] &amp;quot;2019-01-04 00:00:00 AAPL -100 @ 8.05890685158692&amp;quot;&#xA;## [1] &amp;quot;2019-01-07 00:00:00 AAPL -100 @ 8.29142347165647&amp;quot;&#xA;## [1] &amp;quot;2019-01-08 00:00:00 AAPL -100 @ 8.33937659467361&amp;quot;&#xA;## [1] &amp;quot;2019-01-09 00:00:00 AAPL -100 @ 8.43584012847029&amp;quot;&#xA;## [1] &amp;quot;2019-01-10 00:00:00 AAPL -100 @ 8.50330938384434&amp;quot;&#xA;## [1] &amp;quot;2019-01-11 00:00:00 AAPL -100 @ 8.52449823030943&amp;quot;&#xA;## [1] &amp;quot;2019-01-14 00:00:00 AAPL -100 @ 8.41130670460984&amp;quot;&#xA;## [1] &amp;quot;2019-01-15 00:00:00 AAPL -100 @ 8.37896614714981&amp;quot;&#xA;## [1] &amp;quot;2019-01-16 00:00:00 AAPL -100 @ 8.53564994130436&amp;quot;&#xA;## [1] &amp;quot;2019-01-17 00:00:00 AAPL -100 @ 8.59810020353282&amp;quot;&#xA;## [1] &amp;quot;2019-01-18 00:00:00 AAPL -100 @ 8.78210641282284&amp;quot;&#xA;## [1] &amp;quot;2019-01-22 00:00:00 AAPL -100 @ 8.72132886470257&amp;quot;&#xA;## [1] &amp;quot;2019-03-18 00:00:00 AAPL 100 @ 10.540144696181&amp;quot;&#xA;## [1] &amp;quot;2019-03-19 00:00:00 AAPL 100 @ 10.684802396395&amp;quot;&#xA;## [1] &amp;quot;2019-03-20 00:00:00 AAPL 100 @ 10.5645375107482&amp;quot;&#xA;## [1] &amp;quot;2019-03-21 00:00:00 AAPL 100 @ 10.7795387906573&amp;quot;&#xA;## [1] &amp;quot;2019-03-22 00:00:00 AAPL 100 @ 11.081333651966&amp;quot;&#xA;## [1] &amp;quot;2019-03-25 00:00:00 AAPL 100 @ 10.8640636152443&amp;quot;&#xA;## [1] &amp;quot;2019-03-26 00:00:00 AAPL 100 @ 10.8725734009082&amp;quot;&#xA;## [1] &amp;quot;2019-03-27 00:00:00 AAPL 100 @ 10.7074934269509&amp;quot;&#xA;## [1] &amp;quot;2019-03-28 00:00:00 AAPL 100 @ 10.7188389422289&amp;quot;&#xA;## [1] &amp;quot;2019-03-29 00:00:00 AAPL 100 @ 10.7687602481807&amp;quot;&#xA;## [1] &amp;quot;2019-04-01 00:00:00 AAPL 100 @ 10.8714385896982&amp;quot;&#xA;## [1] &amp;quot;2019-04-02 00:00:00 AAPL 100 @ 10.8402377734784&amp;quot;&#xA;## [1] &amp;quot;2019-04-03 00:00:00 AAPL 100 @ 10.9627714159379&amp;quot;&#xA;## [1] &amp;quot;2019-04-04 00:00:00 AAPL 100 @ 11.0501328357461&amp;quot;&#xA;## [1] &amp;quot;2019-04-05 00:00:00 AAPL 100 @ 11.1443022572071&amp;quot;&#xA;## [1] &amp;quot;2019-04-08 00:00:00 AAPL 100 @ 11.1426004731958&amp;quot;&#xA;## [1] &amp;quot;2019-04-09 00:00:00 AAPL 100 @ 11.3638419163488&amp;quot;&#xA;## [1] &amp;quot;2019-04-10 00:00:00 AAPL 100 @ 11.2708064404905&amp;quot;&#xA;## [1] &amp;quot;2019-04-11 00:00:00 AAPL 100 @ 11.3939079213587&amp;quot;&#xA;## [1] &amp;quot;2019-04-12 00:00:00 AAPL 100 @ 11.3003054726991&amp;quot;&#xA;## [1] &amp;quot;2019-04-15 00:00:00 AAPL 100 @ 11.2651341156552&amp;quot;&#xA;## [1] &amp;quot;2019-04-16 00:00:00 AAPL 100 @ 11.315055421607&amp;quot;&#xA;## [1] &amp;quot;2019-04-17 00:00:00 AAPL 100 @ 11.3195929352323&amp;quot;&#xA;## [1] &amp;quot;2019-04-18 00:00:00 AAPL 100 @ 11.5226808614548&amp;quot;&#xA;## [1] &amp;quot;2019-04-22 00:00:00 AAPL 100 @ 11.5062299941429&amp;quot;&#xA;## [1] &amp;quot;2019-04-23 00:00:00 AAPL 100 @ 11.5969949819738&amp;quot;&#xA;## [1] &amp;quot;2019-04-24 00:00:00 AAPL 100 @ 11.7632097671411&amp;quot;&#xA;## [1] &amp;quot;2019-04-25 00:00:00 AAPL 100 @ 11.7331437621312&amp;quot;&#xA;## [1] &amp;quot;2019-04-26 00:00:00 AAPL 100 @ 11.623657418961&amp;quot;&#xA;## [1] &amp;quot;2019-04-29 00:00:00 AAPL 100 @ 11.5952931979625&amp;quot;&#xA;## [1] &amp;quot;2019-04-30 00:00:00 AAPL 100 @ 11.5192772934321&amp;quot;&#xA;## [1] &amp;quot;2019-05-01 00:00:00 AAPL 100 @ 11.9061656833438&amp;quot;&#xA;## [1] &amp;quot;2019-05-02 00:00:00 AAPL 100 @ 11.9038960609238&amp;quot;&#xA;## [1] &amp;quot;2019-05-03 00:00:00 AAPL 100 @ 11.9634610981422&amp;quot;&#xA;## [1] &amp;quot;2019-05-06 00:00:00 AAPL 100 @ 11.5890530347185&amp;quot;&#xA;## [1] &amp;quot;2019-05-07 00:00:00 AAPL 100 @ 11.6792519153555&amp;quot;&#xA;## [1] &amp;quot;2019-05-08 00:00:00 AAPL 100 @ 11.4534720929698&amp;quot;&#xA;## [1] &amp;quot;2019-05-09 00:00:00 AAPL 100 @ 11.3683794299741&amp;quot;&#xA;## [1] &amp;quot;2019-05-10 00:00:00 AAPL 100 @ 11.3738580228924&amp;quot;&#xA;## [1] &amp;quot;2019-05-13 00:00:00 AAPL 100 @ 10.814441219945&amp;quot;&#xA;## [1] &amp;quot;2019-05-21 00:00:00 AAPL -100 @ 10.6709857989231&amp;quot;&#xA;## [1] &amp;quot;2019-05-23 00:00:00 AAPL -100 @ 10.3587261989348&amp;quot;&#xA;## [1] &amp;quot;2019-05-24 00:00:00 AAPL -100 @ 10.3817708440105&amp;quot;&#xA;## [1] &amp;quot;2019-05-28 00:00:00 AAPL -100 @ 10.3080269248522&amp;quot;&#xA;## [1] &amp;quot;2019-05-29 00:00:00 AAPL -100 @ 10.1639956953873&amp;quot;&#xA;## [1] &amp;quot;2019-05-30 00:00:00 AAPL -100 @ 10.2521427374921&amp;quot;&#xA;## [1] &amp;quot;2019-05-31 00:00:00 AAPL -100 @ 10.1530491812925&amp;quot;&#xA;## [1] &amp;quot;2019-06-03 00:00:00 AAPL -100 @ 10.1167539092532&amp;quot;&#xA;## [1] &amp;quot;2019-06-04 00:00:00 AAPL -100 @ 10.1075356995842&amp;quot;&#xA;## [1] &amp;quot;2019-06-05 00:00:00 AAPL -100 @ 10.6168299159888&amp;quot;&#xA;## [1] &amp;quot;2019-06-06 00:00:00 AAPL -100 @ 10.547695101665&amp;quot;&#xA;## [1] &amp;quot;2019-06-14 00:00:00 AAPL 100 @ 11.0356729774198&amp;quot;&#xA;## [1] &amp;quot;2019-06-17 00:00:00 AAPL 100 @ 11.1134493138728&amp;quot;&#xA;## [1] &amp;quot;2019-06-18 00:00:00 AAPL 100 @ 11.2949291904567&amp;quot;&#xA;## [1] &amp;quot;2019-06-19 00:00:00 AAPL 100 @ 11.5040619378538&amp;quot;&#xA;## [1] &amp;quot;2019-06-20 00:00:00 AAPL 100 @ 11.5438146978416&amp;quot;&#xA;## [1] &amp;quot;2019-06-21 00:00:00 AAPL 100 @ 11.453363542868&amp;quot;&#xA;## [1] &amp;quot;2019-06-24 00:00:00 AAPL 100 @ 11.4383837323817&amp;quot;&#xA;## [1] &amp;quot;2019-06-25 00:00:00 AAPL 100 @ 11.4320463231214&amp;quot;&#xA;## [1] &amp;quot;2019-06-26 00:00:00 AAPL 100 @ 11.3940227466563&amp;quot;&#xA;## [1] &amp;quot;2019-06-27 00:00:00 AAPL 100 @ 11.5392055930071&amp;quot;&#xA;## [1] &amp;quot;2019-06-28 00:00:00 AAPL 100 @ 11.4464494460679&amp;quot;&#xA;## [1] &amp;quot;2019-07-01 00:00:00 AAPL 100 @ 11.7051298506617&amp;quot;&#xA;## [1] &amp;quot;2019-07-02 00:00:00 AAPL 100 @ 11.6037321815933&amp;quot;&#xA;## [1] &amp;quot;2019-07-03 00:00:00 AAPL 100 @ 11.711467259922&amp;quot;&#xA;## [1] &amp;quot;2019-07-05 00:00:00 AAPL 100 @ 11.7155005563135&amp;quot;&#xA;## [1] &amp;quot;2019-07-08 00:00:00 AAPL 100 @ 11.569164334883&amp;quot;&#xA;## [1] &amp;quot;2019-07-09 00:00:00 AAPL 100 @ 11.4764081879437&amp;quot;&#xA;## [1] &amp;quot;2019-07-10 00:00:00 AAPL 100 @ 11.6290818186346&amp;quot;&#xA;## [1] &amp;quot;2019-07-11 00:00:00 AAPL 100 @ 11.7131955643479&amp;quot;&#xA;## [1] &amp;quot;2019-07-12 00:00:00 AAPL 100 @ 11.6636487862481&amp;quot;&#xA;## [1] &amp;quot;2019-07-15 00:00:00 AAPL 100 @ 11.7581332376131&amp;quot;&#xA;## [1] &amp;quot;2019-07-16 00:00:00 AAPL 100 @ 11.7869394835061&amp;quot;&#xA;## [1] &amp;quot;2019-07-17 00:00:00 AAPL 100 @ 11.7558291247443&amp;quot;&#xA;## [1] &amp;quot;2019-07-18 00:00:00 AAPL 100 @ 11.7529483243357&amp;quot;&#xA;## [1] &amp;quot;2019-07-19 00:00:00 AAPL 100 @ 11.8560742978299&amp;quot;&#xA;## [1] &amp;quot;2019-07-22 00:00:00 AAPL 100 @ 11.7327836005718&amp;quot;&#xA;## [1] &amp;quot;2019-07-23 00:00:00 AAPL 100 @ 12.0099004245037&amp;quot;&#xA;## [1] &amp;quot;2019-07-24 00:00:00 AAPL 100 @ 11.9643860636985&amp;quot;&#xA;## [1] &amp;quot;2019-07-25 00:00:00 AAPL 100 @ 12.0346733740051&amp;quot;&#xA;## [1] &amp;quot;2019-07-26 00:00:00 AAPL 100 @ 11.9534395496037&amp;quot;&#xA;## [1] &amp;quot;2019-07-29 00:00:00 AAPL 100 @ 12.0099004245037&amp;quot;&#xA;## [1] &amp;quot;2019-07-30 00:00:00 AAPL 100 @ 12.027183468762&amp;quot;&#xA;## [1] &amp;quot;2019-07-31 00:00:00 AAPL 100 @ 12.4684953668256&amp;quot;&#xA;## [1] &amp;quot;2019-08-01 00:00:00 AAPL 100 @ 12.3233116413779&amp;quot;&#xA;## [1] &amp;quot;2019-08-02 00:00:00 AAPL 100 @ 11.8410953664404&amp;quot;&#xA;## [1] &amp;quot;2019-08-05 00:00:00 AAPL 100 @ 11.4066975651769&amp;quot;&#xA;## [1] &amp;quot;2019-08-06 00:00:00 AAPL 100 @ 11.3099081218461&amp;quot;&#xA;## [1] &amp;quot;2019-08-19 00:00:00 AAPL 100 @ 12.3208851743917&amp;quot;&#xA;## [1] &amp;quot;2019-08-20 00:00:00 AAPL 100 @ 12.3360952709673&amp;quot;&#xA;## [1] &amp;quot;2019-08-21 00:00:00 AAPL 100 @ 12.4595264543342&amp;quot;&#xA;## [1] &amp;quot;2019-08-22 00:00:00 AAPL 100 @ 12.4712259106615&amp;quot;&#xA;## [1] &amp;quot;2019-08-23 00:00:00 AAPL 100 @ 12.2512722042193&amp;quot;&#xA;## [1] &amp;quot;2019-08-26 00:00:00 AAPL 100 @ 12.0424341863133&amp;quot;&#xA;## [1] &amp;quot;2019-08-27 00:00:00 AAPL 100 @ 12.1594305348084&amp;quot;&#xA;## [1] &amp;quot;2019-08-28 00:00:00 AAPL 100 @ 11.9394777209776&amp;quot;&#xA;## [1] &amp;quot;2019-08-29 00:00:00 AAPL 100 @ 12.1968693306225&amp;quot;&#xA;## [1] &amp;quot;2019-08-30 00:00:00 AAPL 100 @ 12.2939765141002&amp;quot;&#xA;## [1] &amp;quot;2019-09-03 00:00:00 AAPL 100 @ 12.0757776814765&amp;quot;&#xA;## [1] &amp;quot;2019-09-04 00:00:00 AAPL 100 @ 12.1904344957508&amp;quot;&#xA;## [1] &amp;quot;2019-09-05 00:00:00 AAPL 100 @ 12.401612940489&amp;quot;&#xA;## [1] &amp;quot;2019-09-06 00:00:00 AAPL 100 @ 12.5215343762188&amp;quot;&#xA;## [1] &amp;quot;2019-09-09 00:00:00 AAPL 100 @ 12.5677475411255&amp;quot;&#xA;## [1] &amp;quot;2019-09-10 00:00:00 AAPL 100 @ 12.510419580294&amp;quot;&#xA;## [1] &amp;quot;2019-09-11 00:00:00 AAPL 100 @ 12.7566972866253&amp;quot;&#xA;## [1] &amp;quot;2019-09-12 00:00:00 AAPL 100 @ 13.1503897493804&amp;quot;&#xA;## [1] &amp;quot;2019-09-13 00:00:00 AAPL 100 @ 12.8695983344697&amp;quot;&#xA;## [1] &amp;quot;2019-09-16 00:00:00 AAPL 100 @ 12.7368072289965&amp;quot;&#xA;## [1] &amp;quot;2019-09-17 00:00:00 AAPL 100 @ 12.8672588002488&amp;quot;&#xA;## [1] &amp;quot;2019-09-18 00:00:00 AAPL 100 @ 12.9316062563544&amp;quot;&#xA;## [1] &amp;quot;2019-09-19 00:00:00 AAPL 100 @ 12.9871793433673&amp;quot;&#xA;## [1] &amp;quot;2019-09-20 00:00:00 AAPL 100 @ 12.950326100567&amp;quot;&#xA;## [1] &amp;quot;2019-09-23 00:00:00 AAPL 100 @ 12.8081750729875&amp;quot;&#xA;## [1] &amp;quot;2019-09-24 00:00:00 AAPL 100 @ 12.9298513825358&amp;quot;&#xA;## [1] &amp;quot;2019-09-25 00:00:00 AAPL 100 @ 12.784776160333&amp;quot;&#xA;## [1] &amp;quot;2019-09-26 00:00:00 AAPL 100 @ 12.8695983344697&amp;quot;&#xA;## [1] &amp;quot;2019-09-27 00:00:00 AAPL 100 @ 12.9011869558145&amp;quot;&#xA;## [1] &amp;quot;2019-09-30 00:00:00 AAPL 100 @ 12.922246334248&amp;quot;&#xA;## [1] &amp;quot;2019-10-01 00:00:00 AAPL 100 @ 13.1661845063584&amp;quot;&#xA;## [1] &amp;quot;2019-10-02 00:00:00 AAPL 100 @ 13.0486026048496&amp;quot;&#xA;## [1] &amp;quot;2019-10-03 00:00:00 AAPL 100 @ 12.7777557724476&amp;quot;&#xA;## [1] &amp;quot;2019-10-04 00:00:00 AAPL 100 @ 13.1995280015217&amp;quot;&#xA;## [1] &amp;quot;2019-10-07 00:00:00 AAPL 100 @ 13.2363821369333&amp;quot;&#xA;## [1] &amp;quot;2019-10-08 00:00:00 AAPL 100 @ 13.2100581370441&amp;quot;&#xA;## [1] &amp;quot;2019-10-09 00:00:00 AAPL 100 @ 13.2808404280214&amp;quot;&#xA;## [1] &amp;quot;2019-10-10 00:00:00 AAPL 100 @ 13.3334884277997&amp;quot;&#xA;## [1] &amp;quot;2019-10-11 00:00:00 AAPL 100 @ 13.6271495124538&amp;quot;&#xA;## [1] &amp;quot;2019-10-14 00:00:00 AAPL 100 @ 13.7412207737143&amp;quot;&#xA;## [1] &amp;quot;2019-10-15 00:00:00 AAPL 100 @ 13.8283833746833&amp;quot;&#xA;## [1] &amp;quot;2019-10-16 00:00:00 AAPL 100 @ 13.6517186385244&amp;quot;&#xA;## [1] &amp;quot;2019-10-17 00:00:00 AAPL 100 @ 13.7523355696392&amp;quot;&#xA;## [1] &amp;quot;2019-10-18 00:00:00 AAPL 100 @ 13.7230864825154&amp;quot;&#xA;## [1] &amp;quot;2019-10-21 00:00:00 AAPL 100 @ 13.8944865972187&amp;quot;&#xA;## [1] &amp;quot;2019-10-22 00:00:00 AAPL 100 @ 14.1074199157754&amp;quot;&#xA;## [1] &amp;quot;2019-10-23 00:00:00 AAPL 100 @ 14.162408342386&amp;quot;&#xA;## [1] &amp;quot;2019-10-24 00:00:00 AAPL 100 @ 14.3033882639381&amp;quot;&#xA;## [1] &amp;quot;2019-10-25 00:00:00 AAPL 100 @ 14.2244162642706&amp;quot;&#xA;## [1] &amp;quot;2019-10-28 00:00:00 AAPL 100 @ 14.4736181652253&amp;quot;&#xA;## [1] &amp;quot;2019-10-29 00:00:00 AAPL 100 @ 14.5642905138313&amp;quot;&#xA;## [1] &amp;quot;2019-10-30 00:00:00 AAPL 100 @ 14.3180128075&amp;quot;&#xA;## [1] &amp;quot;2019-10-31 00:00:00 AAPL 100 @ 14.4630889223141&amp;quot;&#xA;## [1] &amp;quot;2019-11-01 00:00:00 AAPL 100 @ 14.5976340089946&amp;quot;&#xA;## [1] &amp;quot;2019-11-04 00:00:00 AAPL 100 @ 15.0533343936343&amp;quot;&#xA;## [1] &amp;quot;2019-11-05 00:00:00 AAPL 100 @ 15.0369549762539&amp;quot;&#xA;## [1] &amp;quot;2019-11-06 00:00:00 AAPL 100 @ 15.0205755588735&amp;quot;&#xA;## [1] &amp;quot;2019-11-07 00:00:00 AAPL 100 @ 15.3192381787128&amp;quot;&#xA;## [1] &amp;quot;2019-11-08 00:00:00 AAPL 100 @ 15.3162785476723&amp;quot;&#xA;## [1] &amp;quot;2019-11-11 00:00:00 AAPL 100 @ 15.2931869208728&amp;quot;&#xA;## [1] &amp;quot;2019-11-12 00:00:00 AAPL 100 @ 15.4856099167759&amp;quot;&#xA;## [1] &amp;quot;2019-11-13 00:00:00 AAPL 100 @ 15.4607439568373&amp;quot;&#xA;## [1] &amp;quot;2019-11-14 00:00:00 AAPL 100 @ 15.6158662059759&amp;quot;&#xA;## [1] &amp;quot;2019-11-15 00:00:00 AAPL 100 @ 15.611721277034&amp;quot;&#xA;## [1] &amp;quot;2019-11-18 00:00:00 AAPL 100 @ 15.7372399883413&amp;quot;&#xA;## [1] &amp;quot;2019-11-19 00:00:00 AAPL 100 @ 15.8615752086038&amp;quot;&#xA;## [1] &amp;quot;2019-11-20 00:00:00 AAPL 100 @ 15.7218473773316&amp;quot;&#xA;## [1] &amp;quot;2019-11-21 00:00:00 AAPL 100 @ 15.6123139259847&amp;quot;&#xA;## [1] &amp;quot;2019-11-22 00:00:00 AAPL 100 @ 15.5471857813847&amp;quot;&#xA;## [1] &amp;quot;2019-11-25 00:00:00 AAPL 100 @ 15.5542903413671&amp;quot;&#xA;## [1] &amp;quot;2019-11-26 00:00:00 AAPL 100 @ 15.8047369218877&amp;quot;&#xA;## [1] &amp;quot;2019-11-27 00:00:00 AAPL 100 @ 15.7242143594213&amp;quot;&#xA;## [1] &amp;quot;2019-11-29 00:00:00 AAPL 100 @ 15.7846067329853&amp;quot;&#xA;## [1] &amp;quot;2019-12-02 00:00:00 AAPL 100 @ 15.8242744618394&amp;quot;&#xA;## [1] &amp;quot;2019-12-03 00:00:00 AAPL 100 @ 15.2937795698236&amp;quot;&#xA;## [1] &amp;quot;2019-12-04 00:00:00 AAPL 100 @ 15.4571916768461&amp;quot;&#xA;## [1] &amp;quot;2019-12-05 00:00:00 AAPL 100 @ 15.6182349949222&amp;quot;&#xA;## [1] &amp;quot;2019-12-06 00:00:00 AAPL 100 @ 15.8367092486653&amp;quot;&#xA;## [1] &amp;quot;2019-12-09 00:00:00 AAPL 100 @ 15.9859104288663&amp;quot;&#xA;## [1] &amp;quot;2019-12-10 00:00:00 AAPL 100 @ 15.9030208843102&amp;quot;&#xA;## [1] &amp;quot;2019-12-11 00:00:00 AAPL 100 @ 15.9154538642795&amp;quot;&#xA;## [1] &amp;quot;2019-12-12 00:00:00 AAPL 100 @ 15.8544706486214&amp;quot;&#xA;## [1] &amp;quot;2019-12-13 00:00:00 AAPL 100 @ 16.0723522534137&amp;quot;&#xA;## [1] &amp;quot;2019-12-16 00:00:00 AAPL 100 @ 16.4003599585036&amp;quot;&#xA;## [1] &amp;quot;2019-12-17 00:00:00 AAPL 100 @ 16.5525225766017&amp;quot;&#xA;## [1] &amp;quot;2019-12-18 00:00:00 AAPL 100 @ 16.5661390476159&amp;quot;&#xA;## [1] &amp;quot;2019-12-19 00:00:00 AAPL 100 @ 16.5483776476598&amp;quot;&#xA;## [1] &amp;quot;2019-12-20 00:00:00 AAPL 100 @ 16.7100136146867&amp;quot;&#xA;## [1] &amp;quot;2019-12-23 00:00:00 AAPL 100 @ 16.6093608633179&amp;quot;&#xA;## [1] &amp;quot;2019-12-24 00:00:00 AAPL 100 @ 16.8556625148965&amp;quot;&#xA;## [1] &amp;quot;2019-12-26 00:00:00 AAPL 100 @ 16.8633597238297&amp;quot;&#xA;## [1] &amp;quot;2019-12-27 00:00:00 AAPL 100 @ 17.2363635777606&amp;quot;&#xA;## [1] &amp;quot;2019-12-30 00:00:00 AAPL 100 @ 17.1380796153381&amp;quot;&#xA;## [1] &amp;quot;2019-12-31 00:00:00 AAPL 100 @ 17.1659070131737&amp;quot;&#xA;## [1] &amp;quot;2020-01-02 00:00:00 AAPL 100 @ 17.5395035160554&amp;quot;&#xA;## [1] &amp;quot;2020-01-03 00:00:00 AAPL 100 @ 17.593382171731&amp;quot;&#xA;## [1] &amp;quot;2020-01-06 00:00:00 AAPL 100 @ 17.3944472647963&amp;quot;&#xA;## [1] &amp;quot;2020-01-07 00:00:00 AAPL 100 @ 17.7526493498116&amp;quot;&#xA;## [1] &amp;quot;2020-01-08 00:00:00 AAPL 100 @ 17.5939748206817&amp;quot;&#xA;## [1] &amp;quot;2020-01-09 00:00:00 AAPL 100 @ 18.1907813483425&amp;quot;&#xA;## [1] &amp;quot;2020-01-10 00:00:00 AAPL 100 @ 18.3897180621338&amp;quot;&#xA;## [1] &amp;quot;2020-01-13 00:00:00 AAPL 100 @ 18.4512939267426&amp;quot;&#xA;## [1] &amp;quot;2020-01-14 00:00:00 AAPL 100 @ 18.7508815850462&amp;quot;&#xA;## [1] &amp;quot;2020-01-15 00:00:00 AAPL 100 @ 18.4637269067119&amp;quot;&#xA;## [1] &amp;quot;2020-01-16 00:00:00 AAPL 100 @ 18.5667466401705&amp;quot;&#xA;## [1] &amp;quot;2020-01-17 00:00:00 AAPL 100 @ 18.7254211693004&amp;quot;&#xA;## [1] &amp;quot;2020-01-21 00:00:00 AAPL 100 @ 18.7798924739267&amp;quot;&#xA;## [1] &amp;quot;2020-01-22 00:00:00 AAPL 100 @ 18.8621893695321&amp;quot;&#xA;## [1] &amp;quot;2020-01-23 00:00:00 AAPL 100 @ 18.8231142896287&amp;quot;&#xA;## [1] &amp;quot;2020-01-24 00:00:00 AAPL 100 @ 18.9610659809053&amp;quot;&#xA;## [1] &amp;quot;2020-01-27 00:00:00 AAPL 100 @ 18.3577457353563&amp;quot;&#xA;## [1] &amp;quot;2020-01-28 00:00:00 AAPL 100 @ 18.5081322134588&amp;quot;&#xA;## [1] &amp;quot;2020-01-29 00:00:00 AAPL 100 @ 19.2097364214303&amp;quot;&#xA;## [1] &amp;quot;2020-01-30 00:00:00 AAPL 100 @ 18.9782365387673&amp;quot;&#xA;## [1] &amp;quot;2020-01-31 00:00:00 AAPL 100 @ 19.0013263587102&amp;quot;&#xA;## [1] &amp;quot;2020-02-03 00:00:00 AAPL 100 @ 18.0167124013464&amp;quot;&#xA;## [1] &amp;quot;2020-02-04 00:00:00 AAPL 100 @ 18.6685828825842&amp;quot;&#xA;## [1] &amp;quot;2020-02-05 00:00:00 AAPL 100 @ 19.1546724678533&amp;quot;&#xA;## [1] &amp;quot;2020-02-06 00:00:00 AAPL 100 @ 19.0984268300878&amp;quot;&#xA;## [1] &amp;quot;2020-02-07 00:00:00 AAPL 100 @ 19.2690783513299&amp;quot;&#xA;## [1] &amp;quot;2020-02-10 00:00:00 AAPL 100 @ 18.7795358965997&amp;quot;&#xA;## [1] &amp;quot;2020-02-11 00:00:00 AAPL 100 @ 19.3426000141011&amp;quot;&#xA;## [1] &amp;quot;2020-02-12 00:00:00 AAPL 100 @ 19.2152828580465&amp;quot;&#xA;## [1] &amp;quot;2020-02-13 00:00:00 AAPL 100 @ 19.3778659688549&amp;quot;&#xA;## [1] &amp;quot;2020-02-14 00:00:00 AAPL 100 @ 19.4107404858246&amp;quot;&#xA;## [1] &amp;quot;2020-02-18 00:00:00 AAPL 100 @ 18.8500678061074&amp;quot;&#xA;## [1] &amp;quot;2020-02-19 00:00:00 AAPL 100 @ 19.1274162168352&amp;quot;&#xA;## [1] &amp;quot;2020-02-20 00:00:00 AAPL 100 @ 19.2846199607285&amp;quot;&#xA;## [1] &amp;quot;2020-02-21 00:00:00 AAPL 100 @ 19.0449289425388&amp;quot;&#xA;## [1] &amp;quot;2020-02-24 00:00:00 AAPL 100 @ 17.7681747856487&amp;quot;&#xA;## [1] &amp;quot;2020-03-17 00:00:00 AAPL -100 @ 14.7944583836214&amp;quot;&#xA;## [1] &amp;quot;2020-03-23 00:00:00 AAPL -100 @ 13.6330660179972&amp;quot;&#xA;## [1] &amp;quot;2020-03-24 00:00:00 AAPL -100 @ 14.1279878396425&amp;quot;&#xA;## [1] &amp;quot;2020-04-02 00:00:00 AAPL -100 @ 14.3658848234608&amp;quot;&#xA;## [1] &amp;quot;2020-04-03 00:00:00 AAPL -100 @ 14.5129272369369&amp;quot;&#xA;## [1] &amp;quot;2020-04-06 00:00:00 AAPL -100 @ 14.9970894126858&amp;quot;&#xA;## [1] &amp;quot;2020-04-16 00:00:00 AAPL 100 @ 17.1776155180927&amp;quot;&#xA;## [1] &amp;quot;2020-04-17 00:00:00 AAPL 100 @ 17.0168255295894&amp;quot;&#xA;## [1] &amp;quot;2020-04-20 00:00:00 AAPL 100 @ 16.6139549092447&amp;quot;&#xA;## [1] &amp;quot;2020-04-21 00:00:00 AAPL 100 @ 16.5141329032448&amp;quot;&#xA;## [1] &amp;quot;2020-04-22 00:00:00 AAPL 100 @ 16.3545377215672&amp;quot;&#xA;## [1] &amp;quot;2020-04-23 00:00:00 AAPL 100 @ 16.489625682321&amp;quot;&#xA;## [1] &amp;quot;2020-04-24 00:00:00 AAPL 100 @ 16.5691250274865&amp;quot;&#xA;## [1] &amp;quot;2020-04-27 00:00:00 AAPL 100 @ 16.8440801762974&amp;quot;&#xA;## [1] &amp;quot;2020-04-28 00:00:00 AAPL 100 @ 17.0401361195547&amp;quot;&#xA;## [1] &amp;quot;2020-04-29 00:00:00 AAPL 100 @ 17.0192169673736&amp;quot;&#xA;## [1] &amp;quot;2020-04-30 00:00:00 AAPL 100 @ 17.3318295087226&amp;quot;&#xA;## [1] &amp;quot;2020-05-01 00:00:00 AAPL 100 @ 17.1100715377158&amp;quot;&#xA;## [1] &amp;quot;2020-05-04 00:00:00 AAPL 100 @ 17.2846100133128&amp;quot;&#xA;## [1] &amp;quot;2020-05-05 00:00:00 AAPL 100 @ 17.636673069505&amp;quot;&#xA;## [1] &amp;quot;2020-05-06 00:00:00 AAPL 100 @ 17.9594478533375&amp;quot;&#xA;## [1] &amp;quot;2020-05-07 00:00:00 AAPL 100 @ 18.1244224019302&amp;quot;&#xA;## [1] &amp;quot;2020-05-08 00:00:00 AAPL 100 @ 18.4685103730864&amp;quot;&#xA;## [1] &amp;quot;2020-05-11 00:00:00 AAPL 100 @ 18.617157067002&amp;quot;&#xA;## [1] &amp;quot;2020-05-12 00:00:00 AAPL 100 @ 19.205097901332&amp;quot;&#xA;## [1] &amp;quot;2020-05-13 00:00:00 AAPL 100 @ 18.8618803950367&amp;quot;&#xA;## [1] &amp;quot;2020-05-14 00:00:00 AAPL 100 @ 18.4002290424371&amp;quot;&#xA;## [1] &amp;quot;2020-05-15 00:00:00 AAPL 100 @ 18.1488579290241&amp;quot;&#xA;## [1] &amp;quot;2020-05-18 00:00:00 AAPL 100 @ 18.9235157843534&amp;quot;&#xA;## [1] &amp;quot;2020-05-19 00:00:00 AAPL 100 @ 19.0359066923261&amp;quot;&#xA;## [1] &amp;quot;2020-05-20 00:00:00 AAPL 100 @ 19.1356087206348&amp;quot;&#xA;## [1] &amp;quot;2020-05-21 00:00:00 AAPL 100 @ 19.2552522610328&amp;quot;&#xA;## [1] &amp;quot;2020-05-22 00:00:00 AAPL 100 @ 19.0806211166964&amp;quot;&#xA;## [1] &amp;quot;2020-05-26 00:00:00 AAPL 100 @ 19.5477124046264&amp;quot;&#xA;## [1] &amp;quot;2020-05-27 00:00:00 AAPL 100 @ 19.1029801729275&amp;quot;&#xA;## [1] &amp;quot;2020-05-28 00:00:00 AAPL 100 @ 19.1410468119194&amp;quot;&#xA;## [1] &amp;quot;2020-05-29 00:00:00 AAPL 100 @ 19.2909031999288&amp;quot;&#xA;## [1] &amp;quot;2020-06-01 00:00:00 AAPL 100 @ 19.2002646570944&amp;quot;&#xA;## [1] &amp;quot;2020-06-02 00:00:00 AAPL 100 @ 19.3815417427633&amp;quot;&#xA;## [1] &amp;quot;2020-06-03 00:00:00 AAPL 100 @ 19.6178064323705&amp;quot;&#xA;## [1] &amp;quot;2020-06-04 00:00:00 AAPL 100 @ 19.6014921585168&amp;quot;&#xA;## [1] &amp;quot;2020-06-05 00:00:00 AAPL 100 @ 19.5386489191521&amp;quot;&#xA;## [1] &amp;quot;2020-06-08 00:00:00 AAPL 100 @ 19.9555858473814&amp;quot;&#xA;## [1] &amp;quot;2020-06-09 00:00:00 AAPL 100 @ 20.0697912964948&amp;quot;&#xA;## [1] &amp;quot;2020-06-10 00:00:00 AAPL 100 @ 21.0220989992574&amp;quot;&#xA;## [1] &amp;quot;2020-06-11 00:00:00 AAPL 100 @ 21.1072994508072&amp;quot;&#xA;## [1] &amp;quot;2020-06-12 00:00:00 AAPL 100 @ 20.8299457310194&amp;quot;&#xA;## [1] &amp;quot;2020-06-15 00:00:00 AAPL 100 @ 20.1368629330502&amp;quot;&#xA;## [1] &amp;quot;2020-06-16 00:00:00 AAPL 100 @ 21.2372143267274&amp;quot;&#xA;## [1] &amp;quot;2020-06-17 00:00:00 AAPL 100 @ 21.4601852896238&amp;quot;&#xA;## [1] &amp;quot;2020-06-18 00:00:00 AAPL 100 @ 21.2341937795846&amp;quot;&#xA;## [1] &amp;quot;2020-06-19 00:00:00 AAPL 100 @ 21.4293694390113&amp;quot;&#xA;## [1] &amp;quot;2020-06-22 00:00:00 AAPL 100 @ 21.229963538348&amp;quot;&#xA;## [1] &amp;quot;2020-06-23 00:00:00 AAPL 100 @ 21.9949530611561&amp;quot;&#xA;## [1] &amp;quot;2020-06-24 00:00:00 AAPL 100 @ 22.0553787563791&amp;quot;&#xA;## [1] &amp;quot;2020-06-25 00:00:00 AAPL 100 @ 21.7955490045387&amp;quot;&#xA;## [1] &amp;quot;2020-06-26 00:00:00 AAPL 100 @ 22.019727817483&amp;quot;&#xA;## [1] &amp;quot;2020-06-29 00:00:00 AAPL 100 @ 21.3453768375093&amp;quot;&#xA;## [1] &amp;quot;2020-06-30 00:00:00 AAPL 100 @ 21.758083524502&amp;quot;&#xA;## [1] &amp;quot;2020-07-01 00:00:00 AAPL 100 @ 22.0626295447585&amp;quot;&#xA;## [1] &amp;quot;2020-07-02 00:00:00 AAPL 100 @ 22.2275923565737&amp;quot;&#xA;## [1] &amp;quot;2020-07-06 00:00:00 AAPL 100 @ 22.3575072324939&amp;quot;&#xA;## [1] &amp;quot;2020-07-07 00:00:00 AAPL 100 @ 22.6844104649356&amp;quot;&#xA;## [1] &amp;quot;2020-07-08 00:00:00 AAPL 100 @ 22.763567978154&amp;quot;&#xA;## [1] &amp;quot;2020-07-09 00:00:00 AAPL 100 @ 23.266913207981&amp;quot;&#xA;## [1] &amp;quot;2020-07-10 00:00:00 AAPL 100 @ 23.0427343950367&amp;quot;&#xA;## [1] &amp;quot;2020-07-13 00:00:00 AAPL 100 @ 23.5092208359197&amp;quot;&#xA;## [1] &amp;quot;2020-07-14 00:00:00 AAPL 100 @ 22.9230908546387&amp;quot;&#xA;## [1] &amp;quot;2020-07-15 00:00:00 AAPL 100 @ 23.926157764149&amp;quot;&#xA;## [1] &amp;quot;2020-07-16 00:00:00 AAPL 100 @ 23.3394247798669&amp;quot;&#xA;## [1] &amp;quot;2020-07-17 00:00:00 AAPL 100 @ 23.4421491993643&amp;quot;&#xA;## [1] &amp;quot;2020-07-20 00:00:00 AAPL 100 @ 23.3043786880178&amp;quot;&#xA;## [1] &amp;quot;2020-07-21 00:00:00 AAPL 100 @ 23.9702691855182&amp;quot;&#xA;## [1] &amp;quot;2020-07-22 00:00:00 AAPL 100 @ 23.3708454775263&amp;quot;&#xA;## [1] &amp;quot;2020-07-23 00:00:00 AAPL 100 @ 23.4445648994602&amp;quot;&#xA;## [1] &amp;quot;2020-07-24 00:00:00 AAPL 100 @ 21.9919325140133&amp;quot;&#xA;## [1] &amp;quot;2020-07-27 00:00:00 AAPL 100 @ 22.6499673760875&amp;quot;&#xA;## [1] &amp;quot;2020-07-28 00:00:00 AAPL 100 @ 22.8088872495712&amp;quot;&#xA;## [1] &amp;quot;2020-07-29 00:00:00 AAPL 100 @ 22.6596357086087&amp;quot;&#xA;## [1] &amp;quot;2020-07-30 00:00:00 AAPL 100 @ 22.7653806752488&amp;quot;&#xA;## [1] &amp;quot;2020-07-31 00:00:00 AAPL 100 @ 24.8675911283883&amp;quot;&#xA;## [1] &amp;quot;2020-08-03 00:00:00 AAPL 100 @ 26.1522401548772&amp;quot;&#xA;## [1] &amp;quot;2020-08-04 00:00:00 AAPL 100 @ 26.3776286619153&amp;quot;&#xA;## [1] &amp;quot;2020-08-05 00:00:00 AAPL 100 @ 26.4368465070903&amp;quot;&#xA;## [1] &amp;quot;2020-08-06 00:00:00 AAPL 100 @ 26.6851952293147&amp;quot;&#xA;## [1] &amp;quot;2020-08-07 00:00:00 AAPL 100 @ 27.5603747454441&amp;quot;&#xA;## [1] &amp;quot;2020-08-10 00:00:00 AAPL 100 @ 27.4130833804898&amp;quot;&#xA;## [1] &amp;quot;2020-08-11 00:00:00 AAPL 100 @ 27.2597071152016&amp;quot;&#xA;## [1] &amp;quot;2020-08-12 00:00:00 AAPL 100 @ 26.9012180724441&amp;quot;&#xA;## [1] &amp;quot;2020-08-13 00:00:00 AAPL 100 @ 27.8586073011023&amp;quot;&#xA;## [1] &amp;quot;2020-08-14 00:00:00 AAPL 100 @ 27.9559898528771&amp;quot;&#xA;## [1] &amp;quot;2020-08-17 00:00:00 AAPL 100 @ 28.2560482501191&amp;quot;&#xA;## [1] &amp;quot;2020-08-18 00:00:00 AAPL 100 @ 27.8397396522643&amp;quot;&#xA;## [1] &amp;quot;2020-08-19 00:00:00 AAPL 100 @ 28.2365713682806&amp;quot;&#xA;## [1] &amp;quot;2020-08-20 00:00:00 AAPL 100 @ 28.1799684217666&amp;quot;&#xA;## [1] &amp;quot;2020-08-21 00:00:00 AAPL 100 @ 29.0351049494816&amp;quot;&#xA;## [1] &amp;quot;2020-08-24 00:00:00 AAPL 100 @ 31.3321065327267&amp;quot;&#xA;## [1] &amp;quot;2020-08-25 00:00:00 AAPL 100 @ 30.3582865872324&amp;quot;&#xA;## [1] &amp;quot;2020-08-26 00:00:00 AAPL 100 @ 30.7192088471564&amp;quot;&#xA;## [1] &amp;quot;2020-08-27 00:00:00 AAPL 100 @ 30.9535350899656&amp;quot;&#xA;## [1] &amp;quot;2020-08-28 00:00:00 AAPL 100 @ 30.6784292418957&amp;quot;&#xA;## [1] &amp;quot;2020-08-31 00:00:00 AAPL 100 @ 124.240187398637&amp;quot;&#xA;## [1] &amp;quot;2020-09-01 00:00:00 AAPL 100 @ 129.284577205237&amp;quot;&#xA;## [1] &amp;quot;2020-09-02 00:00:00 AAPL 100 @ 133.988138296423&amp;quot;&#xA;## [1] &amp;quot;2020-09-03 00:00:00 AAPL 100 @ 123.587728573806&amp;quot;&#xA;## [1] &amp;quot;2020-09-04 00:00:00 AAPL 100 @ 116.926783578459&amp;quot;&#xA;## [1] &amp;quot;2020-09-08 00:00:00 AAPL 100 @ 110.966991469956&amp;quot;&#xA;## [1] &amp;quot;2020-09-09 00:00:00 AAPL 100 @ 114.190346689771&amp;quot;&#xA;## [1] &amp;quot;2020-09-10 00:00:00 AAPL 100 @ 117.209192792864&amp;quot;&#xA;## [1] &amp;quot;2020-09-11 00:00:00 AAPL 100 @ 111.570763662443&amp;quot;&#xA;## [1] &amp;quot;2020-09-14 00:00:00 AAPL 100 @ 111.716838418814&amp;quot;&#xA;## [1] &amp;quot;2020-09-15 00:00:00 AAPL 100 @ 115.2323357217&amp;quot;&#xA;## [1] &amp;quot;2020-09-16 00:00:00 AAPL 100 @ 112.213489618607&amp;quot;&#xA;## [1] &amp;quot;2020-09-17 00:00:00 AAPL 100 @ 106.847729404254&amp;quot;&#xA;## [1] &amp;quot;2020-09-18 00:00:00 AAPL 100 @ 107.509928527421&amp;quot;&#xA;## [1] &amp;quot;2020-09-21 00:00:00 AAPL 100 @ 101.803332167983&amp;quot;&#xA;## [1] &amp;quot;2020-09-22 00:00:00 AAPL 100 @ 109.730241049313&amp;quot;&#xA;## [1] &amp;quot;2020-09-23 00:00:00 AAPL 100 @ 108.697992315721&amp;quot;&#xA;## [1] &amp;quot;2020-09-24 00:00:00 AAPL 100 @ 102.416837229136&amp;quot;&#xA;## [1] &amp;quot;2020-09-25 00:00:00 AAPL 100 @ 105.591498386937&amp;quot;&#xA;## [1] &amp;quot;2020-09-28 00:00:00 AAPL 100 @ 111.999247633219&amp;quot;&#xA;## [1] &amp;quot;2020-09-29 00:00:00 AAPL 100 @ 111.55129049544&amp;quot;&#xA;## [1] &amp;quot;2020-09-30 00:00:00 AAPL 100 @ 110.811183844919&amp;quot;&#xA;## [1] &amp;quot;2020-10-01 00:00:00 AAPL 100 @ 114.560396300196&amp;quot;&#xA;## [1] &amp;quot;2020-10-02 00:00:00 AAPL 100 @ 109.934742736364&amp;quot;&#xA;## [1] &amp;quot;2020-10-05 00:00:00 AAPL 100 @ 110.92804513595&amp;quot;&#xA;## [1] &amp;quot;2020-10-06 00:00:00 AAPL 100 @ 112.671179625052&amp;quot;&#xA;## [1] &amp;quot;2020-10-07 00:00:00 AAPL 100 @ 111.619457724457&amp;quot;&#xA;## [1] &amp;quot;2020-10-08 00:00:00 AAPL 100 @ 113.206784588522&amp;quot;&#xA;## [1] &amp;quot;2020-10-09 00:00:00 AAPL 100 @ 112.26217625095&amp;quot;&#xA;## [1] &amp;quot;2020-10-12 00:00:00 AAPL 100 @ 116.917043280123&amp;quot;&#xA;## [1] &amp;quot;2020-10-13 00:00:00 AAPL 100 @ 121.990653981734&amp;quot;&#xA;## [1] &amp;quot;2020-10-14 00:00:00 AAPL 100 @ 117.832438152354&amp;quot;&#xA;## [1] &amp;quot;2020-10-15 00:00:00 AAPL 100 @ 115.612125630462&amp;quot;&#xA;## [1] &amp;quot;2020-10-16 00:00:00 AAPL 100 @ 118.105107068423&amp;quot;&#xA;## [1] &amp;quot;2020-10-19 00:00:00 AAPL 100 @ 116.819662585765&amp;quot;&#xA;## [1] &amp;quot;2020-10-20 00:00:00 AAPL 100 @ 113.158090526508&amp;quot;&#xA;## [1] &amp;quot;2020-10-21 00:00:00 AAPL 100 @ 113.615787962624&amp;quot;&#xA;## [1] &amp;quot;2020-10-22 00:00:00 AAPL 100 @ 114.375367780148&amp;quot;&#xA;## [1] &amp;quot;2020-10-23 00:00:00 AAPL 100 @ 113.343119046556&amp;quot;&#xA;## [1] &amp;quot;2020-10-26 00:00:00 AAPL 100 @ 111.025425830307&amp;quot;&#xA;## [1] &amp;quot;2020-10-27 00:00:00 AAPL 100 @ 112.466677938001&amp;quot;&#xA;## [1] &amp;quot;2020-11-12 00:00:00 AAPL 100 @ 116.689535865269&amp;quot;&#xA;## [1] &amp;quot;2020-11-13 00:00:00 AAPL 100 @ 116.513945231727&amp;quot;&#xA;## [1] &amp;quot;2020-11-16 00:00:00 AAPL 100 @ 116.006680093722&amp;quot;&#xA;## [1] &amp;quot;2020-11-17 00:00:00 AAPL 100 @ 116.621251032363&amp;quot;&#xA;## [1] &amp;quot;2020-11-18 00:00:00 AAPL 100 @ 115.704276896923&amp;quot;&#xA;## [1] &amp;quot;2020-11-19 00:00:00 AAPL 100 @ 114.709260826022&amp;quot;&#xA;## [1] &amp;quot;2020-11-20 00:00:00 AAPL 100 @ 115.733540762098&amp;quot;&#xA;## [1] &amp;quot;2020-11-23 00:00:00 AAPL 100 @ 114.309308931141&amp;quot;&#xA;## [1] &amp;quot;2020-11-24 00:00:00 AAPL 100 @ 111.119421104698&amp;quot;&#xA;## [1] &amp;quot;2020-11-25 00:00:00 AAPL 100 @ 112.719243569197&amp;quot;&#xA;## [1] &amp;quot;2020-11-27 00:00:00 AAPL 100 @ 113.714252197609&amp;quot;&#xA;## [1] &amp;quot;2020-12-02 00:00:00 AAPL 100 @ 119.030734389177&amp;quot;&#xA;## [1] &amp;quot;2020-12-03 00:00:00 AAPL 100 @ 120.493987187865&amp;quot;&#xA;## [1] &amp;quot;2020-12-04 00:00:00 AAPL 100 @ 119.596527257534&amp;quot;&#xA;## [1] &amp;quot;2020-12-07 00:00:00 AAPL 100 @ 119.313630823355&amp;quot;&#xA;## [1] &amp;quot;2020-12-08 00:00:00 AAPL 100 @ 121.323169727778&amp;quot;&#xA;## [1] &amp;quot;2020-12-09 00:00:00 AAPL 100 @ 121.479246156211&amp;quot;&#xA;## [1] &amp;quot;2020-12-10 00:00:00 AAPL 100 @ 117.547974827869&amp;quot;&#xA;## [1] &amp;quot;2020-12-11 00:00:00 AAPL 100 @ 119.430693726546&amp;quot;&#xA;## [1] &amp;quot;2020-12-14 00:00:00 AAPL 100 @ 119.596527257534&amp;quot;&#xA;## [1] &amp;quot;2020-12-15 00:00:00 AAPL 100 @ 121.293898420114&amp;quot;&#xA;## [1] &amp;quot;2020-12-16 00:00:00 AAPL 100 @ 124.288696292883&amp;quot;&#xA;## [1] &amp;quot;2020-12-17 00:00:00 AAPL 100 @ 125.742184546527&amp;quot;&#xA;## [1] &amp;quot;2020-12-18 00:00:00 AAPL 100 @ 125.800727161855&amp;quot;&#xA;## [1] &amp;quot;2020-12-21 00:00:00 AAPL 100 @ 121.957239986552&amp;quot;&#xA;## [1] &amp;quot;2020-12-22 00:00:00 AAPL 100 @ 128.385801152211&amp;quot;&#xA;## [1] &amp;quot;2020-12-23 00:00:00 AAPL 100 @ 128.922330155392&amp;quot;&#xA;## [1] &amp;quot;2020-12-24 00:00:00 AAPL 100 @ 128.102912160522&amp;quot;&#xA;## [1] &amp;quot;2020-12-28 00:00:00 AAPL 100 @ 130.707500355988&amp;quot;&#xA;## [1] &amp;quot;2020-12-29 00:00:00 AAPL 100 @ 134.668035549505&amp;quot;&#xA;## [1] &amp;quot;2020-12-30 00:00:00 AAPL 100 @ 132.258544750202&amp;quot;&#xA;## [1] &amp;quot;2020-12-31 00:00:00 AAPL 100 @ 130.795291951514&amp;quot;&#xA;## [1] &amp;quot;2021-01-04 00:00:00 AAPL 100 @ 130.249013288268&amp;quot;&#xA;## [1] &amp;quot;2021-01-05 00:00:00 AAPL 100 @ 125.73243488646&amp;quot;&#xA;## [1] &amp;quot;2021-01-06 00:00:00 AAPL 100 @ 124.591099489682&amp;quot;&#xA;## [1] &amp;quot;2021-01-07 00:00:00 AAPL 100 @ 125.215420088389&amp;quot;&#xA;## [1] &amp;quot;2021-01-08 00:00:00 AAPL 100 @ 129.185704941972&amp;quot;&#xA;## [1] &amp;quot;2021-01-11 00:00:00 AAPL 100 @ 126.025088423193&amp;quot;&#xA;## [1] &amp;quot;2021-01-12 00:00:00 AAPL 100 @ 125.351989754201&amp;quot;&#xA;## [1] &amp;quot;2021-01-13 00:00:00 AAPL 100 @ 125.605614880715&amp;quot;&#xA;## [1] &amp;quot;2021-01-22 00:00:00 AAPL 100 @ 132.94139307926&amp;quot;&#xA;## [1] &amp;quot;2021-01-25 00:00:00 AAPL 100 @ 139.565059083571&amp;quot;&#xA;## [1] &amp;quot;2021-01-26 00:00:00 AAPL 100 @ 140.082073881643&amp;quot;&#xA;## [1] &amp;quot;2021-01-27 00:00:00 AAPL 100 @ 139.916225465678&amp;quot;&#xA;## [1] &amp;quot;2021-01-28 00:00:00 AAPL 100 @ 136.102024483016&amp;quot;&#xA;## [1] &amp;quot;2021-01-29 00:00:00 AAPL 100 @ 132.502420216649&amp;quot;&#xA;## [1] &amp;quot;2021-02-01 00:00:00 AAPL 100 @ 130.473374549606&amp;quot;&#xA;## [1] &amp;quot;2021-02-02 00:00:00 AAPL 100 @ 132.404864076079&amp;quot;&#xA;## [1] &amp;quot;2021-02-03 00:00:00 AAPL 100 @ 132.434127941255&amp;quot;&#xA;## [1] &amp;quot;2021-02-04 00:00:00 AAPL 100 @ 132.96090728437&amp;quot;&#xA;## [1] &amp;quot;2021-02-05 00:00:00 AAPL 100 @ 134.1854056372&amp;quot;&#xA;## [1] &amp;quot;2021-02-08 00:00:00 AAPL 100 @ 132.895811823051&amp;quot;&#xA;## [1] &amp;quot;2021-02-09 00:00:00 AAPL 100 @ 133.472214403366&amp;quot;&#xA;## [1] &amp;quot;2021-02-12 00:00:00 AAPL 100 @ 131.254526867466&amp;quot;&#xA;## [1] &amp;quot;2021-02-16 00:00:00 AAPL 100 @ 132.368260203676&amp;quot;&#xA;## [1] &amp;quot;2021-02-17 00:00:00 AAPL 100 @ 128.225946175853&amp;quot;&#xA;## [1] &amp;quot;2021-04-09 00:00:00 AAPL 100 @ 126.809357751926&amp;quot;&#xA;## [1] &amp;quot;2021-04-12 00:00:00 AAPL 100 @ 129.466689029062&amp;quot;&#xA;## [1] &amp;quot;2021-04-13 00:00:00 AAPL 100 @ 129.388530473003&amp;quot;&#xA;## [1] &amp;quot;2021-04-14 00:00:00 AAPL 100 @ 131.830929447781&amp;quot;&#xA;## [1] &amp;quot;2021-04-15 00:00:00 AAPL 100 @ 130.736739477391&amp;quot;&#xA;## [1] &amp;quot;2021-04-16 00:00:00 AAPL 100 @ 131.205675906527&amp;quot;&#xA;## [1] &amp;quot;2021-04-19 00:00:00 AAPL 100 @ 130.433869482453&amp;quot;&#xA;## [1] &amp;quot;2021-04-20 00:00:00 AAPL 100 @ 131.90908800384&amp;quot;&#xA;## [1] &amp;quot;2021-04-21 00:00:00 AAPL 100 @ 129.310371916944&amp;quot;&#xA;## [1] &amp;quot;2021-04-22 00:00:00 AAPL 100 @ 129.974697282617&amp;quot;&#xA;## [1] &amp;quot;2021-04-23 00:00:00 AAPL 100 @ 129.114982980405&amp;quot;&#xA;## [1] &amp;quot;2021-04-26 00:00:00 AAPL 100 @ 131.723463296602&amp;quot;&#xA;## [1] &amp;quot;2021-04-27 00:00:00 AAPL 100 @ 131.89930886732&amp;quot;&#xA;## [1] &amp;quot;2021-04-28 00:00:00 AAPL 100 @ 131.215440135826&amp;quot;&#xA;## [1] &amp;quot;2021-04-29 00:00:00 AAPL 100 @ 133.325676427768&amp;quot;&#xA;## [1] &amp;quot;2021-04-30 00:00:00 AAPL 100 @ 128.743733565929&amp;quot;&#xA;## [1] &amp;quot;2021-05-03 00:00:00 AAPL 100 @ 128.997737692706&amp;quot;&#xA;## [1] &amp;quot;2021-05-04 00:00:00 AAPL 100 @ 128.167330985614&amp;quot;&#xA;## [1] &amp;quot;2021-05-05 00:00:00 AAPL 100 @ 126.223176035091&amp;quot;&#xA;## [1] &amp;quot;2021-06-16 00:00:00 AAPL 100 @ 127.582558585817&amp;quot;&#xA;## [1] &amp;quot;2021-06-17 00:00:00 AAPL 100 @ 127.024753501789&amp;quot;&#xA;## [1] &amp;quot;2021-06-18 00:00:00 AAPL 100 @ 127.915300405852&amp;quot;&#xA;## [1] &amp;quot;2021-06-21 00:00:00 AAPL 100 @ 127.514063018411&amp;quot;&#xA;## [1] &amp;quot;2021-06-22 00:00:00 AAPL 100 @ 129.304937641156&amp;quot;&#xA;## [1] &amp;quot;2021-06-23 00:00:00 AAPL 100 @ 130.909872258377&amp;quot;&#xA;## [1] &amp;quot;2021-06-24 00:00:00 AAPL 100 @ 131.575326033364&amp;quot;&#xA;## [1] &amp;quot;2021-06-25 00:00:00 AAPL 100 @ 130.606502747278&amp;quot;&#xA;## [1] &amp;quot;2021-06-28 00:00:00 AAPL 100 @ 130.557568809107&amp;quot;&#xA;## [1] &amp;quot;2021-06-29 00:00:00 AAPL 100 @ 131.917848668017&amp;quot;&#xA;## [1] &amp;quot;2021-06-30 00:00:00 AAPL 100 @ 133.25855196515&amp;quot;&#xA;## [1] &amp;quot;2021-07-01 00:00:00 AAPL 100 @ 133.679365914367&amp;quot;&#xA;## [1] &amp;quot;2021-07-02 00:00:00 AAPL 100 @ 134.951558711553&amp;quot;&#xA;## [1] &amp;quot;2021-07-06 00:00:00 AAPL 100 @ 137.075175154333&amp;quot;&#xA;## [1] &amp;quot;2021-07-07 00:00:00 AAPL 100 @ 140.470969461757&amp;quot;&#xA;## [1] &amp;quot;2021-07-08 00:00:00 AAPL 100 @ 138.552884518819&amp;quot;&#xA;## [1] &amp;quot;2021-07-09 00:00:00 AAPL 100 @ 139.697866995811&amp;quot;&#xA;## [1] &amp;quot;2021-07-12 00:00:00 AAPL 100 @ 143.083895421159&amp;quot;&#xA;## [1] &amp;quot;2021-07-13 00:00:00 AAPL 100 @ 140.950498163762&amp;quot;&#xA;## [1] &amp;quot;2021-07-14 00:00:00 AAPL 100 @ 144.933484796692&amp;quot;&#xA;## [1] &amp;quot;2021-07-15 00:00:00 AAPL 100 @ 146.04910989729&amp;quot;&#xA;## [1] &amp;quot;2021-07-16 00:00:00 AAPL 100 @ 145.285788245962&amp;quot;&#xA;## [1] &amp;quot;2021-07-19 00:00:00 AAPL 100 @ 140.676486029057&amp;quot;&#xA;## [1] &amp;quot;2021-07-20 00:00:00 AAPL 100 @ 140.392693079734&amp;quot;&#xA;## [1] &amp;quot;2021-07-21 00:00:00 AAPL 100 @ 142.418426713631&amp;quot;&#xA;## [1] &amp;quot;2021-07-22 00:00:00 AAPL 100 @ 142.819664101071&amp;quot;&#xA;## [1] &amp;quot;2021-07-23 00:00:00 AAPL 100 @ 144.395241341898&amp;quot;&#xA;## [1] &amp;quot;2021-07-26 00:00:00 AAPL 100 @ 145.099848240438&amp;quot;&#xA;## [1] &amp;quot;2021-07-27 00:00:00 AAPL 100 @ 145.931665459172&amp;quot;&#xA;## [1] &amp;quot;2021-07-28 00:00:00 AAPL 100 @ 141.71381981509&amp;quot;&#xA;## [1] &amp;quot;2021-07-29 00:00:00 AAPL 100 @ 141.596390309514&amp;quot;&#xA;## [1] &amp;quot;2021-07-30 00:00:00 AAPL 100 @ 141.293020798415&amp;quot;&#xA;## [1] &amp;quot;2021-08-02 00:00:00 AAPL 100 @ 143.230682303129&amp;quot;&#xA;## [1] &amp;quot;2021-08-03 00:00:00 AAPL 100 @ 142.692438848336&amp;quot;&#xA;## [1] &amp;quot;2021-08-04 00:00:00 AAPL 100 @ 144.121229207193&amp;quot;&#xA;## [1] &amp;quot;2021-08-05 00:00:00 AAPL 100 @ 143.837421325328&amp;quot;&#xA;## [1] &amp;quot;2021-08-06 00:00:00 AAPL 100 @ 143.435479252414&amp;quot;&#xA;## [1] &amp;quot;2021-08-09 00:00:00 AAPL 100 @ 143.288457495109&amp;quot;&#xA;## [1] &amp;quot;2021-08-10 00:00:00 AAPL 100 @ 143.523683333848&amp;quot;&#xA;## [1] &amp;quot;2021-08-11 00:00:00 AAPL 100 @ 143.141450692718&amp;quot;&#xA;## [1] &amp;quot;2021-08-12 00:00:00 AAPL 100 @ 143.278662026587&amp;quot;&#xA;## [1] &amp;quot;2021-08-13 00:00:00 AAPL 100 @ 146.003297766934&amp;quot;&#xA;## [1] &amp;quot;2021-08-16 00:00:00 AAPL 100 @ 145.58185334189&amp;quot;&#xA;## [1] &amp;quot;2021-08-17 00:00:00 AAPL 100 @ 147.238199771759&amp;quot;&#xA;## [1] &amp;quot;2021-08-18 00:00:00 AAPL 100 @ 146.816770301629&amp;quot;&#xA;## [1] &amp;quot;2021-08-19 00:00:00 AAPL 100 @ 142.141759571718&amp;quot;&#xA;## [1] &amp;quot;2021-08-20 00:00:00 AAPL 100 @ 144.503768562891&amp;quot;&#xA;## [1] &amp;quot;2021-08-23 00:00:00 AAPL 100 @ 145.356437926586&amp;quot;&#xA;## [1] &amp;quot;2021-08-24 00:00:00 AAPL 100 @ 146.473734489499&amp;quot;&#xA;## [1] &amp;quot;2021-08-25 00:00:00 AAPL 100 @ 146.826565770151&amp;quot;&#xA;## [1] &amp;quot;2021-08-26 00:00:00 AAPL 100 @ 145.3956497105&amp;quot;&#xA;## [1] &amp;quot;2021-08-27 00:00:00 AAPL 100 @ 144.542965391891&amp;quot;&#xA;## [1] &amp;quot;2021-08-30 00:00:00 AAPL 100 @ 146.032699127412&amp;quot;&#xA;## [1] &amp;quot;2021-08-31 00:00:00 AAPL 100 @ 149.619814654889&amp;quot;&#xA;## [1] &amp;quot;2021-09-01 00:00:00 AAPL 100 @ 149.786427349237&amp;quot;&#xA;## [1] &amp;quot;2021-09-02 00:00:00 AAPL 100 @ 150.805709407279&amp;quot;&#xA;## [1] &amp;quot;2021-09-03 00:00:00 AAPL 100 @ 150.697899433888&amp;quot;&#xA;## [1] &amp;quot;2021-09-07 00:00:00 AAPL 100 @ 151.883809141192&amp;quot;&#xA;## [1] &amp;quot;2021-09-08 00:00:00 AAPL 100 @ 153.8537750678&amp;quot;&#xA;## [1] &amp;quot;2021-09-09 00:00:00 AAPL 100 @ 152.39345764767&amp;quot;&#xA;## [1] &amp;quot;2021-09-10 00:00:00 AAPL 100 @ 151.91321050167&amp;quot;&#xA;## [1] &amp;quot;2021-09-13 00:00:00 AAPL 100 @ 147.630242836325&amp;quot;&#xA;## [1] &amp;quot;2021-09-14 00:00:00 AAPL 100 @ 147.355820168586&amp;quot;&#xA;## [1] &amp;quot;2021-09-15 00:00:00 AAPL 100 @ 145.601459233847&amp;quot;&#xA;## [1] &amp;quot;2021-09-16 00:00:00 AAPL 100 @ 145.483853791934&amp;quot;&#xA;## [1] &amp;quot;2021-09-17 00:00:00 AAPL 100 @ 145.856290964543&amp;quot;&#xA;## [1] &amp;quot;2021-10-21 00:00:00 AAPL 100 @ 145.846480541108&amp;quot;&#xA;## [1] &amp;quot;2021-10-22 00:00:00 AAPL 100 @ 146.708960328238&amp;quot;&#xA;## [1] &amp;quot;2021-10-25 00:00:00 AAPL 100 @ 145.71906467576&amp;quot;&#xA;## [1] &amp;quot;2021-10-26 00:00:00 AAPL 100 @ 146.356129047586&amp;quot;&#xA;## [1] &amp;quot;2021-10-27 00:00:00 AAPL 100 @ 146.385530408064&amp;quot;&#xA;## [1] &amp;quot;2021-10-28 00:00:00 AAPL 100 @ 146.836376193586&amp;quot;&#xA;## [1] &amp;quot;2021-10-29 00:00:00 AAPL 100 @ 144.288148616109&amp;quot;&#xA;## [1] &amp;quot;2021-11-01 00:00:00 AAPL 100 @ 146.022903658891&amp;quot;&#xA;## [1] &amp;quot;2021-11-02 00:00:00 AAPL 100 @ 145.699473738717&amp;quot;&#xA;## [1] &amp;quot;2021-11-03 00:00:00 AAPL 100 @ 147.395016997585&amp;quot;&#xA;## [1] &amp;quot;2021-11-04 00:00:00 AAPL 100 @ 148.561320812933&amp;quot;&#xA;## [1] &amp;quot;2021-11-05 00:00:00 AAPL 100 @ 149.08240854293&amp;quot;&#xA;## [1] &amp;quot;2021-11-08 00:00:00 AAPL 100 @ 148.611285233697&amp;quot;&#xA;## [1] &amp;quot;2021-11-09 00:00:00 AAPL 100 @ 147.423644730825&amp;quot;&#xA;## [1] &amp;quot;2021-11-10 00:00:00 AAPL 100 @ 147.24697910614&amp;quot;&#xA;## [1] &amp;quot;2021-11-11 00:00:00 AAPL 100 @ 146.206574933911&amp;quot;&#xA;## [1] &amp;quot;2021-11-12 00:00:00 AAPL 100 @ 145.686357871057&amp;quot;&#xA;## [1] &amp;quot;2021-11-15 00:00:00 AAPL 100 @ 147.590500590829&amp;quot;&#xA;## [1] &amp;quot;2021-11-16 00:00:00 AAPL 100 @ 147.168456058478&amp;quot;&#xA;## [1] &amp;quot;2021-11-17 00:00:00 AAPL 100 @ 148.208860230706&amp;quot;&#xA;## [1] &amp;quot;2021-11-18 00:00:00 AAPL 100 @ 150.868774179579&amp;quot;&#xA;## [1] &amp;quot;2021-11-19 00:00:00 AAPL 100 @ 154.735933183945&amp;quot;&#xA;## [1] &amp;quot;2021-11-22 00:00:00 AAPL 100 @ 158.691439977394&amp;quot;&#xA;## [1] &amp;quot;2021-11-23 00:00:00 AAPL 100 @ 158.141793620499&amp;quot;&#xA;## [1] &amp;quot;2021-11-24 00:00:00 AAPL 100 @ 157.778637629709&amp;quot;&#xA;## [1] &amp;quot;2021-11-26 00:00:00 AAPL 100 @ 156.620456374357&amp;quot;&#xA;## [1] &amp;quot;2021-11-29 00:00:00 AAPL 100 @ 156.424141266832&amp;quot;&#xA;## [1] &amp;quot;2021-11-30 00:00:00 AAPL 100 @ 157.032691142029&amp;quot;&#xA;## [1] &amp;quot;2021-12-01 00:00:00 AAPL 100 @ 164.38423363061&amp;quot;&#xA;## [1] &amp;quot;2021-12-02 00:00:00 AAPL 100 @ 155.805796603695&amp;quot;&#xA;## [1] &amp;quot;2021-12-03 00:00:00 AAPL 100 @ 160.988197935477&amp;quot;&#xA;## [1] &amp;quot;2021-12-06 00:00:00 AAPL 100 @ 161.253196372504&amp;quot;&#xA;## [1] &amp;quot;2021-12-07 00:00:00 AAPL 100 @ 165.954664630373&amp;quot;&#xA;## [1] &amp;quot;2021-12-08 00:00:00 AAPL 100 @ 168.948290299255&amp;quot;&#xA;## [1] &amp;quot;2021-12-09 00:00:00 AAPL 100 @ 171.67690255437&amp;quot;&#xA;## [1] &amp;quot;2021-12-10 00:00:00 AAPL 100 @ 171.971360238918&amp;quot;&#xA;## [1] &amp;quot;2021-12-13 00:00:00 AAPL 100 @ 177.772106233838&amp;quot;&#xA;## [1] &amp;quot;2021-12-14 00:00:00 AAPL 100 @ 172.010614274379&amp;quot;&#xA;## [1] &amp;quot;2021-12-15 00:00:00 AAPL 100 @ 171.873202685156&amp;quot;&#xA;## [1] &amp;quot;2021-12-16 00:00:00 AAPL 100 @ 175.966121067828&amp;quot;&#xA;## [1] &amp;quot;2021-12-17 00:00:00 AAPL 100 @ 166.788943930396&amp;quot;&#xA;## [1] &amp;quot;2021-12-20 00:00:00 AAPL 100 @ 165.169449130492&amp;quot;&#xA;## [1] &amp;quot;2021-12-21 00:00:00 AAPL 100 @ 168.38881920094&amp;quot;&#xA;## [1] &amp;quot;2021-12-22 00:00:00 AAPL 100 @ 169.84145814084&amp;quot;&#xA;## [1] &amp;quot;2021-12-23 00:00:00 AAPL 100 @ 172.599529643476&amp;quot;&#xA;## [1] &amp;quot;2021-12-27 00:00:00 AAPL 100 @ 173.816599440389&amp;quot;&#xA;## [1] &amp;quot;2021-12-28 00:00:00 AAPL 100 @ 176.829859615372&amp;quot;&#xA;## [1] &amp;quot;2021-12-29 00:00:00 AAPL 100 @ 176.015199844709&amp;quot;&#xA;## [1] &amp;quot;2021-12-30 00:00:00 AAPL 100 @ 176.152611433933&amp;quot;&#xA;## [1] &amp;quot;2021-12-31 00:00:00 AAPL 100 @ 174.798115071056&amp;quot;&#xA;## [1] &amp;quot;2022-01-03 00:00:00 AAPL 100 @ 174.542926398709&amp;quot;&#xA;## [1] &amp;quot;2022-01-04 00:00:00 AAPL 100 @ 179.254204421258&amp;quot;&#xA;## [1] &amp;quot;2022-01-05 00:00:00 AAPL 100 @ 176.290023023157&amp;quot;&#xA;## [1] &amp;quot;2022-01-06 00:00:00 AAPL 100 @ 169.507746420831&amp;quot;&#xA;## [1] &amp;quot;2022-01-07 00:00:00 AAPL 100 @ 169.694236786936&amp;quot;&#xA;## [1] &amp;quot;2022-01-10 00:00:00 AAPL 100 @ 165.954664630373&amp;quot;&#xA;## [1] &amp;quot;2022-01-11 00:00:00 AAPL 100 @ 169.134780665361&amp;quot;&#xA;## [1] &amp;quot;2022-02-04 00:00:00 AAPL 100 @ 168.721279238041&amp;quot;&#xA;## [1] &amp;quot;2022-02-07 00:00:00 AAPL 100 @ 169.880951050281&amp;quot;&#xA;## [1] &amp;quot;2022-02-08 00:00:00 AAPL 100 @ 168.7704205429&amp;quot;&#xA;## [1] &amp;quot;2022-02-09 00:00:00 AAPL 100 @ 173.015977352985&amp;quot;&#xA;## [1] &amp;quot;2022-02-10 00:00:00 AAPL 100 @ 171.13889047642&amp;quot;&#xA;## [1] &amp;quot;2022-02-11 00:00:00 AAPL 100 @ 169.360086209575&amp;quot;&#xA;## [1] &amp;quot;2022-02-14 00:00:00 AAPL 100 @ 164.485559686421&amp;quot;&#xA;## [1] &amp;quot;2022-02-15 00:00:00 AAPL 100 @ 168.023523694824&amp;quot;&#xA;## [1] &amp;quot;2022-02-16 00:00:00 AAPL 100 @ 168.888362673728&amp;quot;&#xA;## [1] &amp;quot;2022-02-17 00:00:00 AAPL 100 @ 168.082487262328&amp;quot;&#xA;## [1] &amp;quot;2022-02-18 00:00:00 AAPL 100 @ 166.893348662156&amp;quot;&#xA;## [1] &amp;quot;2022-02-22 00:00:00 AAPL 100 @ 162.136749274009&amp;quot;&#xA;## [1] &amp;quot;2022-03-15 00:00:00 AAPL -100 @ 148.299400590661&amp;quot;&#xA;## [1] &amp;quot;2022-03-25 00:00:00 AAPL 100 @ 170.883376685299&amp;quot;&#xA;## [1] &amp;quot;2022-03-28 00:00:00 AAPL 100 @ 169.202840032352&amp;quot;&#xA;## [1] &amp;quot;2022-03-29 00:00:00 AAPL 100 @ 173.644947066054&amp;quot;&#xA;## [1] &amp;quot;2022-03-30 00:00:00 AAPL 100 @ 175.472892637759&amp;quot;&#xA;## [1] &amp;quot;2022-03-31 00:00:00 AAPL 100 @ 174.775122098722&amp;quot;&#xA;## [1] &amp;quot;2022-04-01 00:00:00 AAPL 100 @ 171.030785604057&amp;quot;&#xA;## [1] &amp;quot;2022-04-04 00:00:00 AAPL 100 @ 171.561487703228&amp;quot;&#xA;## [1] &amp;quot;2022-04-05 00:00:00 AAPL 100 @ 174.44098521899&amp;quot;&#xA;## [1] &amp;quot;2022-04-06 00:00:00 AAPL 100 @ 169.389567993326&amp;quot;&#xA;## [1] &amp;quot;2022-04-07 00:00:00 AAPL 100 @ 168.210251655799&amp;quot;&#xA;## [1] &amp;quot;2022-04-08 00:00:00 AAPL 100 @ 168.81956184776&amp;quot;&#xA;## [1] &amp;quot;2022-04-11 00:00:00 AAPL 100 @ 165.802477675884&amp;quot;&#xA;## [1] &amp;quot;2022-04-12 00:00:00 AAPL 100 @ 165.124366657955&amp;quot;&#xA;## [1] &amp;quot;2022-04-27 00:00:00 AAPL -100 @ 153.223068418674&amp;quot;&#xA;## [1] &amp;quot;2022-04-28 00:00:00 AAPL -100 @ 156.505503640136&amp;quot;&#xA;## [1] &amp;quot;2022-05-02 00:00:00 AAPL -100 @ 154.009284308966&amp;quot;&#xA;## [1] &amp;quot;2022-05-03 00:00:00 AAPL -100 @ 155.424454916507&amp;quot;&#xA;## [1] &amp;quot;2022-05-06 00:00:00 AAPL -100 @ 153.546606930267&amp;quot;&#xA;## [1] &amp;quot;2022-05-09 00:00:00 AAPL -100 @ 152.483658257259&amp;quot;&#xA;## [1] &amp;quot;2022-05-10 00:00:00 AAPL -100 @ 153.064353609541&amp;quot;&#xA;## [1] &amp;quot;2022-05-11 00:00:00 AAPL -100 @ 151.076245071999&amp;quot;&#xA;## [1] &amp;quot;2022-05-12 00:00:00 AAPL -100 @ 140.515675272942&amp;quot;&#xA;## [1] &amp;quot;2022-05-13 00:00:00 AAPL -100 @ 142.306929783079&amp;quot;&#xA;## [1] &amp;quot;2022-05-16 00:00:00 AAPL -100 @ 143.251778053925&amp;quot;&#xA;## [1] &amp;quot;2022-05-17 00:00:00 AAPL -100 @ 146.509510968256&amp;quot;&#xA;## [1] &amp;quot;2022-05-18 00:00:00 AAPL -100 @ 144.531254142796&amp;quot;&#xA;## [1] &amp;quot;2022-05-19 00:00:00 AAPL -100 @ 137.671308784027&amp;quot;&#xA;## [1] &amp;quot;2022-05-20 00:00:00 AAPL -100 @ 136.893774422193&amp;quot;&#xA;## [1] &amp;quot;2022-05-23 00:00:00 AAPL -100 @ 135.614298333322&amp;quot;&#xA;## [1] &amp;quot;2022-05-24 00:00:00 AAPL -100 @ 138.58661693648&amp;quot;&#xA;## [1] &amp;quot;2022-05-25 00:00:00 AAPL -100 @ 136.244192174601&amp;quot;&#xA;## [1] &amp;quot;2022-05-26 00:00:00 AAPL -100 @ 135.220620314217&amp;quot;&#xA;## [1] &amp;quot;2022-05-27 00:00:00 AAPL -100 @ 143.094300839142&amp;quot;&#xA;## [1] &amp;quot;2022-05-31 00:00:00 AAPL -100 @ 146.716201689891&amp;quot;&#xA;## [1] &amp;quot;2022-06-01 00:00:00 AAPL -100 @ 147.533082828641&amp;quot;&#xA;## [1] &amp;quot;2022-06-02 00:00:00 AAPL -100 @ 145.495775802101&amp;quot;&#xA;## [1] &amp;quot;2022-06-03 00:00:00 AAPL -100 @ 144.580452631794&amp;quot;&#xA;## [1] &amp;quot;2022-06-13 00:00:00 AAPL -100 @ 130.771986612635&amp;quot;&#xA;## [1] &amp;quot;2022-06-14 00:00:00 AAPL -100 @ 131.027890841122&amp;quot;&#xA;## [1] &amp;quot;2022-06-15 00:00:00 AAPL -100 @ 132.169563103667&amp;quot;&#xA;## [1] &amp;quot;2022-06-16 00:00:00 AAPL -100 @ 129.994467268655&amp;quot;&#xA;## [1] &amp;quot;2022-06-17 00:00:00 AAPL -100 @ 128.016210443194&amp;quot;&#xA;## [1] &amp;quot;2022-06-21 00:00:00 AAPL -100 @ 131.313305152295&amp;quot;&#xA;## [1] &amp;quot;2022-06-22 00:00:00 AAPL -100 @ 132.661668136475&amp;quot;&#xA;## [1] &amp;quot;2022-06-23 00:00:00 AAPL -100 @ 134.6596283861&amp;quot;&#xA;## [1] &amp;quot;2022-06-24 00:00:00 AAPL -100 @ 137.690982172485&amp;quot;&#xA;## [1] &amp;quot;2022-06-27 00:00:00 AAPL -100 @ 140.446773359779&amp;quot;&#xA;## [1] &amp;quot;2022-06-28 00:00:00 AAPL -100 @ 139.885781431662&amp;quot;&#xA;## [1] &amp;quot;2022-06-29 00:00:00 AAPL -100 @ 135.28952222738&amp;quot;&#xA;## [1] &amp;quot;2022-06-30 00:00:00 AAPL -100 @ 135.082831505745&amp;quot;&#xA;## [1] &amp;quot;2022-07-01 00:00:00 AAPL -100 @ 133.891930718494&amp;quot;&#xA;## [1] &amp;quot;2022-08-01 00:00:00 AAPL 100 @ 158.467657258345&amp;quot;&#xA;## [1] &amp;quot;2022-08-02 00:00:00 AAPL 100 @ 157.572037512203&amp;quot;&#xA;## [1] &amp;quot;2022-08-03 00:00:00 AAPL 100 @ 158.300343349333&amp;quot;&#xA;## [1] &amp;quot;2022-08-04 00:00:00 AAPL 100 @ 163.388707586423&amp;quot;&#xA;## [1] &amp;quot;2022-08-05 00:00:00 AAPL 100 @ 160.856059661762&amp;quot;&#xA;## [1] &amp;quot;2022-08-08 00:00:00 AAPL 100 @ 163.970472150122&amp;quot;&#xA;## [1] &amp;quot;2022-08-09 00:00:00 AAPL 100 @ 161.654374778797&amp;quot;&#xA;## [1] &amp;quot;2022-08-10 00:00:00 AAPL 100 @ 165.261575861743&amp;quot;&#xA;## [1] &amp;quot;2022-08-11 00:00:00 AAPL 100 @ 167.60725438436&amp;quot;&#xA;## [1] &amp;quot;2022-08-12 00:00:00 AAPL 100 @ 167.370725483736&amp;quot;&#xA;## [1] &amp;quot;2022-08-15 00:00:00 AAPL 100 @ 169.046203697585&amp;quot;&#xA;## [1] &amp;quot;2022-08-16 00:00:00 AAPL 100 @ 170.288025542005&amp;quot;&#xA;## [1] &amp;quot;2022-08-17 00:00:00 AAPL 100 @ 170.27817518405&amp;quot;&#xA;## [1] &amp;quot;2022-08-18 00:00:00 AAPL 100 @ 171.244036618598&amp;quot;&#xA;## [1] &amp;quot;2022-08-19 00:00:00 AAPL 100 @ 170.534419839298&amp;quot;&#xA;## [1] &amp;quot;2022-08-22 00:00:00 AAPL 100 @ 167.242595636755&amp;quot;&#xA;## [1] &amp;quot;2022-08-23 00:00:00 AAPL 100 @ 164.670238571468&amp;quot;&#xA;## [1] &amp;quot;2022-08-24 00:00:00 AAPL 100 @ 164.906782510807&amp;quot;&#xA;## [1] &amp;quot;2022-08-25 00:00:00 AAPL 100 @ 166.345716785317&amp;quot;&#xA;## [1] &amp;quot;2022-08-26 00:00:00 AAPL 100 @ 168.109908375615&amp;quot;&#xA;## [1] &amp;quot;2022-08-29 00:00:00 AAPL 100 @ 158.825758019548&amp;quot;&#xA;## [1] &amp;quot;2022-08-30 00:00:00 AAPL 100 @ 159.791634492811&amp;quot;&#xA;## [1] &amp;quot;2022-09-06 00:00:00 AAPL -100 @ 154.213263992807&amp;quot;&#xA;## [1] &amp;quot;2022-09-07 00:00:00 AAPL -100 @ 152.587067646159&amp;quot;&#xA;## [1] &amp;quot;2022-09-08 00:00:00 AAPL -100 @ 152.409655931977&amp;quot;&#xA;## [1] &amp;quot;2022-09-09 00:00:00 AAPL -100 @ 153.227686803635&amp;quot;&#xA;## [1] &amp;quot;2022-09-12 00:00:00 AAPL -100 @ 157.288260010634&amp;quot;&#xA;## [1] &amp;quot;2022-09-14 00:00:00 AAPL -100 @ 152.557486494867&amp;quot;&#xA;## [1] &amp;quot;2022-09-15 00:00:00 AAPL -100 @ 152.419506289931&amp;quot;&#xA;## [1] &amp;quot;2022-09-16 00:00:00 AAPL -100 @ 149.029133391701&amp;quot;&#xA;## [1] &amp;quot;2022-09-19 00:00:00 AAPL -100 @ 147.156527709046&amp;quot;&#xA;## [1] &amp;quot;2022-09-20 00:00:00 AAPL -100 @ 151.187534803467&amp;quot;&#xA;## [1] &amp;quot;2022-09-21 00:00:00 AAPL -100 @ 155.070711334998&amp;quot;&#xA;## [1] &amp;quot;2022-09-22 00:00:00 AAPL -100 @ 150.182256898386&amp;quot;&#xA;## [1] &amp;quot;2022-09-23 00:00:00 AAPL -100 @ 149.009417637077&amp;quot;&#xA;## [1] &amp;quot;2022-09-26 00:00:00 AAPL -100 @ 147.501485740741&amp;quot;&#xA;## [1] &amp;quot;2022-09-27 00:00:00 AAPL -100 @ 150.537065288036&amp;quot;&#xA;## [1] &amp;quot;2022-09-28 00:00:00 AAPL -100 @ 145.510615607774&amp;quot;&#xA;## [1] &amp;quot;2022-09-29 00:00:00 AAPL -100 @ 143.992833353484&amp;quot;&#xA;## [1] &amp;quot;2022-09-30 00:00:00 AAPL -100 @ 139.242344083093&amp;quot;&#xA;## [1] &amp;quot;2022-10-03 00:00:00 AAPL -100 @ 136.216629932467&amp;quot;&#xA;## [1] &amp;quot;2022-10-04 00:00:00 AAPL -100 @ 142.938258542487&amp;quot;&#xA;## [1] &amp;quot;2022-10-05 00:00:00 AAPL -100 @ 141.992112862563&amp;quot;&#xA;## [1] &amp;quot;2022-10-06 00:00:00 AAPL -100 @ 143.707007546944&amp;quot;&#xA;## [1] &amp;quot;2022-10-07 00:00:00 AAPL -100 @ 140.484165927513&amp;quot;&#xA;## [1] &amp;quot;2022-10-10 00:00:00 AAPL -100 @ 138.394747098857&amp;quot;&#xA;## [1] &amp;quot;2022-10-11 00:00:00 AAPL -100 @ 137.882242749648&amp;quot;&#xA;## [1] &amp;quot;2022-10-12 00:00:00 AAPL -100 @ 137.12335914186&amp;quot;&#xA;## [1] &amp;quot;2022-10-13 00:00:00 AAPL -100 @ 133.043070180237&amp;quot;&#xA;## [1] &amp;quot;2022-10-14 00:00:00 AAPL -100 @ 142.228641763187&amp;quot;&#xA;## [1] &amp;quot;2022-10-17 00:00:00 AAPL -100 @ 139.035381295047&amp;quot;&#xA;## [1] &amp;quot;2022-10-18 00:00:00 AAPL -100 @ 143.391630666541&amp;quot;&#xA;## [1] &amp;quot;2022-10-19 00:00:00 AAPL -100 @ 139.646434339945&amp;quot;&#xA;## [1] &amp;quot;2022-10-20 00:00:00 AAPL -100 @ 140.957253806189&amp;quot;&#xA;## [1] &amp;quot;2022-10-21 00:00:00 AAPL -100 @ 140.809408204585&amp;quot;&#xA;## [1] &amp;quot;2022-10-24 00:00:00 AAPL -100 @ 145.06710888039&amp;quot;&#xA;## [1] &amp;quot;2022-11-08 00:00:00 AAPL -100 @ 138.614456965421&amp;quot;&#xA;## [1] &amp;quot;2022-11-09 00:00:00 AAPL -100 @ 136.728878206643&amp;quot;&#xA;## [1] &amp;quot;2022-11-10 00:00:00 AAPL -100 @ 139.433844830183&amp;quot;&#xA;## [1] &amp;quot;2022-11-11 00:00:00 AAPL -100 @ 143.955278133753&amp;quot;&#xA;## [1] &amp;quot;2022-11-14 00:00:00 AAPL -100 @ 147.064990276888&amp;quot;&#xA;## [1] &amp;quot;2022-11-15 00:00:00 AAPL -100 @ 150.273429657188&amp;quot;&#xA;## [1] &amp;quot;2022-11-16 00:00:00 AAPL -100 @ 147.222947830889&amp;quot;&#xA;## [1] &amp;quot;2022-12-07 00:00:00 AAPL -100 @ 140.371693328616&amp;quot;&#xA;## [1] &amp;quot;2022-12-08 00:00:00 AAPL -100 @ 140.539517580869&amp;quot;&#xA;## [1] &amp;quot;2022-12-09 00:00:00 AAPL -100 @ 140.519769120703&amp;quot;&#xA;## [1] &amp;quot;2022-12-12 00:00:00 AAPL -100 @ 140.875166085375&amp;quot;&#xA;## [1] &amp;quot;2022-12-13 00:00:00 AAPL -100 @ 147.588211493813&amp;quot;&#xA;## [1] &amp;quot;2022-12-14 00:00:00 AAPL -100 @ 143.491287233663&amp;quot;&#xA;## [1] &amp;quot;2022-12-15 00:00:00 AAPL -100 @ 139.3055024346&amp;quot;&#xA;## [1] &amp;quot;2022-12-16 00:00:00 AAPL -100 @ 134.942026685031&amp;quot;&#xA;## [1] &amp;quot;2022-12-19 00:00:00 AAPL -100 @ 133.382229732507&amp;quot;&#xA;## [1] &amp;quot;2022-12-20 00:00:00 AAPL -100 @ 129.709799452116&amp;quot;&#xA;## [1] &amp;quot;2022-12-21 00:00:00 AAPL -100 @ 131.279463102892&amp;quot;&#xA;## [1] &amp;quot;2022-12-22 00:00:00 AAPL -100 @ 132.631953946493&amp;quot;&#xA;## [1] &amp;quot;2022-12-23 00:00:00 AAPL -100 @ 129.245808552026&amp;quot;&#xA;## [1] &amp;quot;2022-12-27 00:00:00 AAPL -100 @ 129.699932753864&amp;quot;&#xA;## [1] &amp;quot;2022-12-28 00:00:00 AAPL -100 @ 128.011793405757&amp;quot;&#xA;## [1] &amp;quot;2022-12-29 00:00:00 AAPL -100 @ 126.353276747898&amp;quot;&#xA;## [1] &amp;quot;2022-12-30 00:00:00 AAPL -100 @ 126.767911561235&amp;quot;&#xA;## [1] &amp;quot;2023-01-03 00:00:00 AAPL -100 @ 128.613993399683&amp;quot;&#xA;## [1] &amp;quot;2023-01-04 00:00:00 AAPL -100 @ 125.267344925547&amp;quot;&#xA;## [1] &amp;quot;2023-01-05 00:00:00 AAPL -100 @ 125.504273724718&amp;quot;&#xA;## [1] &amp;quot;2023-01-06 00:00:00 AAPL -100 @ 124.398600974032&amp;quot;&#xA;## [1] &amp;quot;2023-01-09 00:00:00 AAPL -100 @ 128.801566112101&amp;quot;&#xA;## [1] &amp;quot;2023-01-10 00:00:00 AAPL -100 @ 128.594244939517&amp;quot;&#xA;## [1] &amp;quot;2023-01-11 00:00:00 AAPL -100 @ 129.571590358281&amp;quot;&#xA;## [1] &amp;quot;2023-01-12 00:00:00 AAPL -100 @ 132.167963046403&amp;quot;&#xA;## [1] &amp;quot;2023-01-13 00:00:00 AAPL -100 @ 130.34161460446&amp;quot;&#xA;## [1] &amp;quot;2023-01-17 00:00:00 AAPL -100 @ 133.105811544835&amp;quot;&#xA;## [1] &amp;quot;2023-02-03 00:00:00 AAPL 100 @ 146.137008476707&amp;quot;&#xA;## [1] &amp;quot;2023-02-06 00:00:00 AAPL 100 @ 150.618959923608&amp;quot;&#xA;## [1] &amp;quot;2023-02-07 00:00:00 AAPL 100 @ 148.713632704664&amp;quot;&#xA;## [1] &amp;quot;2023-02-08 00:00:00 AAPL 100 @ 151.912205386712&amp;quot;&#xA;## [1] &amp;quot;2023-02-09 00:00:00 AAPL 100 @ 151.813478149546&amp;quot;&#xA;## [1] &amp;quot;2023-02-10 00:00:00 AAPL 100 @ 147.774009834412&amp;quot;&#xA;## [1] &amp;quot;2023-02-13 00:00:00 AAPL 100 @ 149.24719210162&amp;quot;&#xA;## [1] &amp;quot;2023-02-14 00:00:00 AAPL 100 @ 150.403992002312&amp;quot;&#xA;## [1] &amp;quot;2023-02-15 00:00:00 AAPL 100 @ 151.382829650587&amp;quot;&#xA;## [1] &amp;quot;2023-02-16 00:00:00 AAPL 100 @ 151.778311380398&amp;quot;&#xA;## [1] &amp;quot;2023-02-17 00:00:00 AAPL 100 @ 150.631408329281&amp;quot;&#xA;## [1] &amp;quot;2023-02-21 00:00:00 AAPL 100 @ 148.505652543229&amp;quot;&#xA;## [1] &amp;quot;2023-02-22 00:00:00 AAPL 100 @ 147.190653915948&amp;quot;&#xA;## [1] &amp;quot;2023-02-23 00:00:00 AAPL 100 @ 148.396892804531&amp;quot;&#xA;## [1] &amp;quot;2023-02-24 00:00:00 AAPL 100 @ 145.450513183454&amp;quot;&#xA;## [1] &amp;quot;2023-02-28 00:00:00 AAPL 100 @ 145.391192432648&amp;quot;&#xA;## [1] &amp;quot;2023-03-10 00:00:00 AAPL 100 @ 148.515549392804&amp;quot;&#xA;## [1] &amp;quot;2023-03-13 00:00:00 AAPL 100 @ 146.142613753954&amp;quot;&#xA;## [1] &amp;quot;2023-03-14 00:00:00 AAPL 100 @ 149.573471317712&amp;quot;&#xA;## [1] &amp;quot;2023-03-15 00:00:00 AAPL 100 @ 149.484490191504&amp;quot;&#xA;## [1] &amp;quot;2023-03-16 00:00:00 AAPL 100 @ 150.44354922729&amp;quot;&#xA;## [1] &amp;quot;2023-03-17 00:00:00 AAPL 100 @ 154.31932750875&amp;quot;&#xA;## [1] &amp;quot;2023-03-20 00:00:00 AAPL 100 @ 153.320726334648&amp;quot;&#xA;## [1] &amp;quot;2023-03-21 00:00:00 AAPL 100 @ 155.545345009823&amp;quot;&#xA;## [1] &amp;quot;2023-03-22 00:00:00 AAPL 100 @ 157.503005219711&amp;quot;&#xA;## [1] &amp;quot;2023-03-23 00:00:00 AAPL 100 @ 157.03830588952&amp;quot;&#xA;## [1] &amp;quot;2023-03-24 00:00:00 AAPL 100 @ 157.067966264922&amp;quot;&#xA;## [1] &amp;quot;2023-03-27 00:00:00 AAPL 100 @ 158.135785039406&amp;quot;&#xA;## [1] &amp;quot;2023-03-28 00:00:00 AAPL 100 @ 156.188006592431&amp;quot;&#xA;## [1] &amp;quot;2023-03-29 00:00:00 AAPL 100 @ 157.572207733431&amp;quot;&#xA;## [1] &amp;quot;2023-03-30 00:00:00 AAPL 100 @ 159.707845282397&amp;quot;&#xA;## [1] &amp;quot;2023-03-31 00:00:00 AAPL 100 @ 160.607583567378&amp;quot;&#xA;## [1] &amp;quot;2023-04-03 00:00:00 AAPL 100 @ 162.416941900253&amp;quot;&#xA;## [1] &amp;quot;2023-04-04 00:00:00 AAPL 100 @ 164.720659938722&amp;quot;&#xA;## [1] &amp;quot;2023-04-05 00:00:00 AAPL 100 @ 162.881641230444&amp;quot;&#xA;## [1] &amp;quot;2023-04-06 00:00:00 AAPL 100 @ 160.597686717803&amp;quot;&#xA;## [1] &amp;quot;2023-04-10 00:00:00 AAPL 100 @ 159.5990855437&amp;quot;&#xA;## [1] &amp;quot;2023-04-11 00:00:00 AAPL 100 @ 160.518602441169&amp;quot;&#xA;## [1] &amp;quot;2023-04-12 00:00:00 AAPL 100 @ 159.401344678795&amp;quot;&#xA;## [1] &amp;quot;2023-04-13 00:00:00 AAPL 100 @ 159.806723258181&amp;quot;&#xA;## [1] &amp;quot;2023-04-14 00:00:00 AAPL 100 @ 162.733324266769&amp;quot;&#xA;## [1] &amp;quot;2023-04-17 00:00:00 AAPL 100 @ 163.227683972364&amp;quot;&#xA;## [1] &amp;quot;2023-04-18 00:00:00 AAPL 100 @ 164.226300233128&amp;quot;&#xA;## [1] &amp;quot;2023-04-19 00:00:00 AAPL 100 @ 163.929681392439&amp;quot;&#xA;## [1] &amp;quot;2023-04-20 00:00:00 AAPL 100 @ 164.216403383553&amp;quot;&#xA;## [1] &amp;quot;2023-04-21 00:00:00 AAPL 100 @ 163.188141834047&amp;quot;&#xA;## [1] &amp;quot;2023-04-24 00:00:00 AAPL 100 @ 163.138702846155&amp;quot;&#xA;## [1] &amp;quot;2023-04-25 00:00:00 AAPL 100 @ 163.326561948147&amp;quot;&#xA;## [1] &amp;quot;2023-04-26 00:00:00 AAPL 100 @ 161.220584774583&amp;quot;&#xA;## [1] &amp;quot;2023-04-27 00:00:00 AAPL 100 @ 163.326561948147&amp;quot;&#xA;## [1] &amp;quot;2023-04-28 00:00:00 AAPL 100 @ 166.589339022402&amp;quot;&#xA;## [1] &amp;quot;2023-05-01 00:00:00 AAPL 100 @ 167.370420719111&amp;quot;&#xA;## [1] &amp;quot;2023-05-02 00:00:00 AAPL 100 @ 168.171281028308&amp;quot;&#xA;## [1] &amp;quot;2023-05-03 00:00:00 AAPL 100 @ 167.587940196505&amp;quot;&#xA;## [1] &amp;quot;2023-05-04 00:00:00 AAPL 100 @ 163.029943107458&amp;quot;&#xA;## [1] &amp;quot;2023-05-05 00:00:00 AAPL 100 @ 169.0512407008&amp;quot;&#xA;## [1] &amp;quot;2023-05-08 00:00:00 AAPL 100 @ 170.534319817583&amp;quot;&#xA;## [1] &amp;quot;2023-05-09 00:00:00 AAPL 100 @ 171.097897123558&amp;quot;&#xA;## [1] &amp;quot;2023-05-10 00:00:00 AAPL 100 @ 171.068236748155&amp;quot;&#xA;## [1] &amp;quot;2023-05-11 00:00:00 AAPL 100 @ 171.888875669841&amp;quot;&#xA;## [1] &amp;quot;2023-05-12 00:00:00 AAPL 100 @ 171.898902431122&amp;quot;&#xA;## [1] &amp;quot;2023-05-15 00:00:00 AAPL 100 @ 171.443470865177&amp;quot;&#xA;## [1] &amp;quot;2023-05-16 00:00:00 AAPL 100 @ 170.285070872409&amp;quot;&#xA;## [1] &amp;quot;2023-05-17 00:00:00 AAPL 100 @ 170.00784771726&amp;quot;&#xA;## [1] &amp;quot;2023-05-18 00:00:00 AAPL 100 @ 171.285053317226&amp;quot;&#xA;## [1] &amp;quot;2023-05-19 00:00:00 AAPL 100 @ 174.641447688332&amp;quot;&#xA;## [1] &amp;quot;2023-05-22 00:00:00 AAPL 100 @ 172.255334360245&amp;quot;&#xA;## [1] &amp;quot;2023-05-23 00:00:00 AAPL 100 @ 171.413769463377&amp;quot;&#xA;## [1] &amp;quot;2023-05-24 00:00:00 AAPL 100 @ 169.393983495836&amp;quot;&#xA;## [1] &amp;quot;2023-05-25 00:00:00 AAPL 100 @ 170.700905605131&amp;quot;&#xA;## [1] &amp;quot;2023-05-26 00:00:00 AAPL 100 @ 171.601888413127&amp;quot;&#xA;## [1] &amp;quot;2023-05-30 00:00:00 AAPL 100 @ 175.205804537581&amp;quot;&#xA;## [1] &amp;quot;2023-05-31 00:00:00 AAPL 100 @ 175.572131898128&amp;quot;&#xA;## [1] &amp;quot;2023-06-01 00:00:00 AAPL 100 @ 175.938459258674&amp;quot;&#xA;## [1] &amp;quot;2023-06-02 00:00:00 AAPL 100 @ 179.235450826181&amp;quot;&#xA;## [1] &amp;quot;2023-06-05 00:00:00 AAPL 100 @ 180.819596090624&amp;quot;&#xA;## [1] &amp;quot;2023-06-06 00:00:00 AAPL 100 @ 178.185961009188&amp;quot;&#xA;## [1] &amp;quot;2023-06-07 00:00:00 AAPL 100 @ 176.671129087296&amp;quot;&#xA;## [1] &amp;quot;2023-06-08 00:00:00 AAPL 100 @ 176.136473639847&amp;quot;&#xA;## [1] &amp;quot;2023-06-09 00:00:00 AAPL 100 @ 179.700792931079&amp;quot;&#xA;## [1] &amp;quot;2023-06-12 00:00:00 AAPL 100 @ 179.473077148106&amp;quot;&#xA;## [1] &amp;quot;2023-06-13 00:00:00 AAPL 100 @ 180.987909069997&amp;quot;&#xA;## [1] &amp;quot;2023-06-14 00:00:00 AAPL 100 @ 181.552250811717&amp;quot;&#xA;## [1] &amp;quot;2023-06-15 00:00:00 AAPL 100 @ 182.136413631341&amp;quot;&#xA;## [1] &amp;quot;2023-06-16 00:00:00 AAPL 100 @ 184.878943781023&amp;quot;&#xA;## [1] &amp;quot;2023-06-20 00:00:00 AAPL 100 @ 182.581949765863&amp;quot;&#xA;## [1] &amp;quot;2023-06-21 00:00:00 AAPL 100 @ 183.067082733608&amp;quot;&#xA;## [1] &amp;quot;2023-06-22 00:00:00 AAPL 100 @ 181.918593279792&amp;quot;&#xA;## [1] &amp;quot;2023-06-23 00:00:00 AAPL 100 @ 183.710648356832&amp;quot;&#xA;## [1] &amp;quot;2023-06-26 00:00:00 AAPL 100 @ 184.977958525374&amp;quot;&#xA;## [1] &amp;quot;2023-06-27 00:00:00 AAPL 100 @ 184.047274315579&amp;quot;&#xA;## [1] &amp;quot;2023-06-28 00:00:00 AAPL 100 @ 186.067045175591&amp;quot;&#xA;## [1] &amp;quot;2023-06-29 00:00:00 AAPL 100 @ 187.205654305511&amp;quot;&#xA;## [1] &amp;quot;2023-06-30 00:00:00 AAPL 100 @ 189.730379211173&amp;quot;&#xA;## [1] &amp;quot;2023-07-03 00:00:00 AAPL 100 @ 191.859060246959&amp;quot;&#xA;## [1] &amp;quot;2023-07-05 00:00:00 AAPL 100 @ 189.670976407574&amp;quot;&#xA;## [1] &amp;quot;2023-07-06 00:00:00 AAPL 100 @ 187.958114996981&amp;quot;&#xA;## [1] &amp;quot;2023-07-07 00:00:00 AAPL 100 @ 189.512558859624&amp;quot;&#xA;## [1] &amp;quot;2023-07-10 00:00:00 AAPL 100 @ 187.383862716309&amp;quot;&#xA;## [1] &amp;quot;2023-07-11 00:00:00 AAPL 100 @ 187.284863079486&amp;quot;&#xA;## [1] &amp;quot;2023-07-12 00:00:00 AAPL 100 @ 187.799697449031&amp;quot;&#xA;## [1] &amp;quot;2023-07-13 00:00:00 AAPL 100 @ 188.611576051628&amp;quot;&#xA;## [1] &amp;quot;2023-07-14 00:00:00 AAPL 100 @ 188.344248327903&amp;quot;&#xA;## [1] &amp;quot;2023-07-17 00:00:00 AAPL 100 @ 189.997691827369&amp;quot;&#xA;## [1] &amp;quot;2023-07-18 00:00:00 AAPL 100 @ 191.433330082814&amp;quot;&#xA;## [1] &amp;quot;2023-07-19 00:00:00 AAPL 100 @ 191.185808329465&amp;quot;&#xA;## [1] &amp;quot;2023-07-20 00:00:00 AAPL 100 @ 193.156071817301&amp;quot;&#xA;## [1] &amp;quot;2023-07-21 00:00:00 AAPL 100 @ 192.175895342859&amp;quot;&#xA;## [1] &amp;quot;2023-07-24 00:00:00 AAPL 100 @ 191.492732886413&amp;quot;&#xA;## [1] &amp;quot;2023-07-25 00:00:00 AAPL 100 @ 191.413524112438&amp;quot;&#xA;## [1] &amp;quot;2023-07-26 00:00:00 AAPL 100 @ 191.750150071185&amp;quot;&#xA;## [1] &amp;quot;2023-07-27 00:00:00 AAPL 100 @ 194.076860595673&amp;quot;&#xA;## [1] &amp;quot;2023-07-28 00:00:00 AAPL 100 @ 192.740237084579&amp;quot;&#xA;## [1] &amp;quot;2023-07-31 00:00:00 AAPL 100 @ 194.116457428896&amp;quot;&#xA;## [1] &amp;quot;2023-08-01 00:00:00 AAPL 100 @ 194.294680947222&amp;quot;&#xA;## [1] &amp;quot;2023-08-02 00:00:00 AAPL 100 @ 193.106564445126&amp;quot;&#xA;## [1] &amp;quot;2023-08-03 00:00:00 AAPL 100 @ 189.670976407574&amp;quot;&#xA;## [1] &amp;quot;2023-08-04 00:00:00 AAPL 100 @ 183.680946955032&amp;quot;&#xA;## [1] &amp;quot;2023-09-05 00:00:00 AAPL 100 @ 186.665307658179&amp;quot;&#xA;## [1] &amp;quot;2023-09-06 00:00:00 AAPL 100 @ 186.784273696062&amp;quot;&#xA;## [1] &amp;quot;2023-09-07 00:00:00 AAPL 100 @ 173.677647335836&amp;quot;&#xA;## [1] &amp;quot;2023-10-16 00:00:00 AAPL 100 @ 175.234190261802&amp;quot;&#xA;## [1] &amp;quot;2023-10-17 00:00:00 AAPL 100 @ 175.135041811614&amp;quot;&#xA;## [1] &amp;quot;2023-10-18 00:00:00 AAPL 100 @ 174.07422600866&amp;quot;&#xA;## [1] &amp;quot;2023-10-19 00:00:00 AAPL 100 @ 174.530272572495&amp;quot;&#xA;## [1] &amp;quot;2023-10-27 00:00:00 AAPL -100 @ 165.478581829274&amp;quot;&#xA;## [1] &amp;quot;2023-10-30 00:00:00 AAPL -100 @ 167.570487053641&amp;quot;&#xA;## [1] &amp;quot;2023-10-31 00:00:00 AAPL -100 @ 167.897658785747&amp;quot;&#xA;## [1] &amp;quot;2023-11-08 00:00:00 AAPL 100 @ 180.786170657902&amp;quot;&#xA;## [1] &amp;quot;2023-11-09 00:00:00 AAPL 100 @ 181.39093989702&amp;quot;&#xA;## [1] &amp;quot;2023-11-10 00:00:00 AAPL 100 @ 182.632565509509&amp;quot;&#xA;## [1] &amp;quot;2023-11-13 00:00:00 AAPL 100 @ 184.469122332097&amp;quot;&#xA;## [1] &amp;quot;2023-11-14 00:00:00 AAPL 100 @ 186.335444699268&amp;quot;&#xA;## [1] &amp;quot;2023-11-15 00:00:00 AAPL 100 @ 186.484363309344&amp;quot;&#xA;## [1] &amp;quot;2023-11-16 00:00:00 AAPL 100 @ 188.191860366104&amp;quot;&#xA;## [1] &amp;quot;2023-11-17 00:00:00 AAPL 100 @ 188.866909591965&amp;quot;&#xA;## [1] &amp;quot;2023-11-20 00:00:00 AAPL 100 @ 188.509526134785&amp;quot;&#xA;## [1] &amp;quot;2023-11-21 00:00:00 AAPL 100 @ 190.018480192637&amp;quot;&#xA;## [1] &amp;quot;2023-11-22 00:00:00 AAPL 100 @ 190.097900421772&amp;quot;&#xA;## [1] &amp;quot;2023-11-24 00:00:00 AAPL 100 @ 189.482397432939&amp;quot;&#xA;## [1] &amp;quot;2023-11-27 00:00:00 AAPL 100 @ 188.539306827229&amp;quot;&#xA;## [1] &amp;quot;2023-11-28 00:00:00 AAPL 100 @ 188.400325213207&amp;quot;&#xA;## [1] &amp;quot;2023-11-29 00:00:00 AAPL 100 @ 189.512178125382&amp;quot;&#xA;## [1] &amp;quot;2023-11-30 00:00:00 AAPL 100 @ 188.459886598093&amp;quot;&#xA;## [1] &amp;quot;2023-12-01 00:00:00 AAPL 100 @ 188.9463298211&amp;quot;&#xA;## [1] &amp;quot;2023-12-04 00:00:00 AAPL 100 @ 188.598868212115&amp;quot;&#xA;## [1] &amp;quot;2023-12-05 00:00:00 AAPL 100 @ 188.827207051327&amp;quot;&#xA;## [1] &amp;quot;2023-12-06 00:00:00 AAPL 100 @ 193.036373160481&amp;quot;&#xA;## [1] &amp;quot;2023-12-07 00:00:00 AAPL 100 @ 192.222342320598&amp;quot;&#xA;## [1] &amp;quot;2023-12-08 00:00:00 AAPL 100 @ 192.78819062488&amp;quot;&#xA;## [1] &amp;quot;2023-12-11 00:00:00 AAPL 100 @ 191.706118405148&amp;quot;&#xA;## [1] &amp;quot;2023-12-12 00:00:00 AAPL 100 @ 191.676337712705&amp;quot;&#xA;## [1] &amp;quot;2023-12-13 00:00:00 AAPL 100 @ 193.671719845703&amp;quot;&#xA;## [1] &amp;quot;2023-12-14 00:00:00 AAPL 100 @ 196.580427039828&amp;quot;&#xA;## [1] &amp;quot;2023-12-15 00:00:00 AAPL 100 @ 196.093983816822&amp;quot;&#xA;## [1] &amp;quot;2023-12-18 00:00:00 AAPL 100 @ 194.664449988105&amp;quot;&#xA;## [1] &amp;quot;2023-12-19 00:00:00 AAPL 100 @ 194.733948369046&amp;quot;&#xA;## [1] &amp;quot;2023-12-20 00:00:00 AAPL 100 @ 195.468558979793&amp;quot;&#xA;## [1] &amp;quot;2023-12-21 00:00:00 AAPL 100 @ 194.67438698416&amp;quot;&#xA;## [1] &amp;quot;2023-12-22 00:00:00 AAPL 100 @ 193.761061923033&amp;quot;&#xA;## [1] &amp;quot;2023-12-26 00:00:00 AAPL 100 @ 192.202483476349&amp;quot;&#xA;## [1] &amp;quot;2023-12-27 00:00:00 AAPL 100 @ 191.090630564174&amp;quot;&#xA;## [1] &amp;quot;2023-12-28 00:00:00 AAPL 100 @ 192.728629239994&amp;quot;&#xA;## [1] &amp;quot;2023-12-29 00:00:00 AAPL 100 @ 192.490368552588&amp;quot;&#xA;## [1] &amp;quot;2024-01-24 00:00:00 AAPL 100 @ 193.999322610439&amp;quot;&#xA;## [1] &amp;quot;2024-01-25 00:00:00 AAPL 100 @ 193.800779611531&amp;quot;&#xA;## [1] &amp;quot;2024-01-26 00:00:00 AAPL 100 @ 192.857689005821&amp;quot;&#xA;## [1] &amp;quot;2024-01-29 00:00:00 AAPL 100 @ 190.614109189362&amp;quot;&#xA;## [1] &amp;quot;2024-01-30 00:00:00 AAPL 100 @ 189.551895813879&amp;quot;&#xA;## [1] &amp;quot;2024-01-31 00:00:00 AAPL 100 @ 185.680239169796&amp;quot;&#xA;## [1] &amp;quot;2024-02-01 00:00:00 AAPL 100 @ 182.652424353758&amp;quot;&#xA;## [1] &amp;quot;2024-02-20 00:00:00 AAPL -100 @ 180.698693117713&amp;quot;&#xA;## [1] &amp;quot;2024-02-21 00:00:00 AAPL -100 @ 180.847801755782&amp;quot;&#xA;## [1] &amp;quot;2024-02-22 00:00:00 AAPL -100 @ 182.378550336516&amp;quot;&#xA;## [1] &amp;quot;2024-02-26 00:00:00 AAPL -100 @ 181.146003864733&amp;quot;&#xA;## [1] &amp;quot;2024-02-27 00:00:00 AAPL -100 @ 180.012847984473&amp;quot;&#xA;## [1] &amp;quot;2024-02-28 00:00:00 AAPL -100 @ 181.414372112317&amp;quot;&#xA;## [1] &amp;quot;2024-02-29 00:00:00 AAPL -100 @ 180.181825640534&amp;quot;&#xA;## [1] &amp;quot;2024-03-01 00:00:00 AAPL -100 @ 178.472149727555&amp;quot;&#xA;## [1] &amp;quot;2024-03-04 00:00:00 AAPL -100 @ 175.092551104769&amp;quot;&#xA;## [1] &amp;quot;2024-03-05 00:00:00 AAPL -100 @ 169.734908321419&amp;quot;&#xA;## [1] &amp;quot;2024-03-06 00:00:00 AAPL -100 @ 170.033110430369&amp;quot;&#xA;## [1] &amp;quot;2024-03-07 00:00:00 AAPL -100 @ 168.134572676148&amp;quot;&#xA;## [1] &amp;quot;2024-03-08 00:00:00 AAPL -100 @ 167.985479205268&amp;quot;&#xA;## [1] &amp;quot;2024-03-11 00:00:00 AAPL -100 @ 171.901829490413&amp;quot;&#xA;## [1] &amp;quot;2024-03-12 00:00:00 AAPL -100 @ 172.110560349646&amp;quot;&#xA;## [1] &amp;quot;2024-03-13 00:00:00 AAPL -100 @ 171.732851834352&amp;quot;&#xA;## [1] &amp;quot;2024-03-14 00:00:00 AAPL -100 @ 171.872010796237&amp;quot;&#xA;## [1] &amp;quot;2024-03-15 00:00:00 AAPL -100 @ 170.142450698077&amp;quot;&#xA;## [1] &amp;quot;2024-03-18 00:00:00 AAPL -100 @ 174.516046239238&amp;quot;&#xA;## [1] &amp;quot;2024-03-19 00:00:00 AAPL -100 @ 173.293419109261&amp;quot;&#xA;## [1] &amp;quot;2024-03-20 00:00:00 AAPL -100 @ 174.665139710119&amp;quot;&#xA;## [1] &amp;quot;2024-03-21 00:00:00 AAPL -100 @ 175.987157431619&amp;quot;&#xA;## [1] &amp;quot;2024-03-22 00:00:00 AAPL -100 @ 170.728905239793&amp;quot;&#xA;## [1] &amp;quot;2024-03-25 00:00:00 AAPL -100 @ 169.546061647366&amp;quot;&#xA;## [1] &amp;quot;2024-03-26 00:00:00 AAPL -100 @ 168.979476123642&amp;quot;&#xA;## [1] &amp;quot;2024-03-27 00:00:00 AAPL -100 @ 169.387018500301&amp;quot;&#xA;## [1] &amp;quot;2024-03-28 00:00:00 AAPL -100 @ 170.718970730797&amp;quot;&#xA;## [1] &amp;quot;2024-04-01 00:00:00 AAPL -100 @ 170.162334883258&amp;quot;&#xA;## [1] &amp;quot;2024-04-02 00:00:00 AAPL -100 @ 168.0650007788&amp;quot;&#xA;## [1] &amp;quot;2024-04-03 00:00:00 AAPL -100 @ 167.776733178846&amp;quot;&#xA;## [1] &amp;quot;2024-04-04 00:00:00 AAPL -100 @ 169.267728556407&amp;quot;&#xA;## [1] &amp;quot;2024-04-05 00:00:00 AAPL -100 @ 168.571933746983&amp;quot;&#xA;## [1] &amp;quot;2024-04-08 00:00:00 AAPL -100 @ 168.015297899444&amp;quot;&#xA;## [1] &amp;quot;2024-04-09 00:00:00 AAPL -100 @ 167.687277096318&amp;quot;&#xA;## [1] &amp;quot;2024-04-10 00:00:00 AAPL -100 @ 167.786682855031&amp;quot;&#xA;## [1] &amp;quot;2024-04-11 00:00:00 AAPL -100 @ 167.329437599015&amp;quot;&#xA;## [1] &amp;quot;2024-04-12 00:00:00 AAPL -100 @ 173.213897535729&amp;quot;&#xA;## [1] &amp;quot;2024-04-15 00:00:00 AAPL -100 @ 174.307300212816&amp;quot;&#xA;## [1] &amp;quot;2024-04-16 00:00:00 AAPL -100 @ 170.718970730797&amp;quot;&#xA;## [1] &amp;quot;2024-04-17 00:00:00 AAPL -100 @ 168.591817932164&amp;quot;&#xA;## [1] &amp;quot;2024-04-18 00:00:00 AAPL -100 @ 167.021300981069&amp;quot;&#xA;## [1] &amp;quot;2024-04-19 00:00:00 AAPL -100 @ 165.212234476567&amp;quot;&#xA;## [1] &amp;quot;2024-04-22 00:00:00 AAPL -100 @ 164.526374176138&amp;quot;&#xA;## [1] &amp;quot;2024-04-23 00:00:00 AAPL -100 @ 164.357396520077&amp;quot;&#xA;## [1] &amp;quot;2024-04-24 00:00:00 AAPL -100 @ 165.540240112504&amp;quot;&#xA;## [1] &amp;quot;2024-04-25 00:00:00 AAPL -100 @ 168.512296358631&amp;quot;&#xA;## [1] &amp;quot;2024-04-26 00:00:00 AAPL -100 @ 168.860201346938&amp;quot;&#xA;## [1] &amp;quot;2024-04-29 00:00:00 AAPL -100 @ 172.329240885063&amp;quot;&#xA;## [1] &amp;quot;2024-04-30 00:00:00 AAPL -100 @ 172.289487681892&amp;quot;&#xA;## [1] &amp;quot;2024-05-01 00:00:00 AAPL -100 @ 168.561999237988&amp;quot;&#xA;## [1] &amp;quot;2024-05-02 00:00:00 AAPL -100 @ 171.474402928574&amp;quot;&#xA;## [1] &amp;quot;2024-05-06 00:00:00 AAPL 100 @ 181.255344132441&amp;quot;&#xA;## [1] &amp;quot;2024-05-07 00:00:00 AAPL 100 @ 182.348731642339&amp;quot;&#xA;## [1] &amp;quot;2024-05-08 00:00:00 AAPL 100 @ 181.752342591628&amp;quot;&#xA;## [1] &amp;quot;2024-05-09 00:00:00 AAPL 100 @ 181.464074991674&amp;quot;&#xA;## [1] &amp;quot;2024-05-10 00:00:00 AAPL 100 @ 184.039305304875&amp;quot;&#xA;## [1] &amp;quot;2024-05-13 00:00:00 AAPL 100 @ 184.576800171001&amp;quot;&#xA;## [1] &amp;quot;2024-05-14 00:00:00 AAPL 100 @ 186.637156657121&amp;quot;&#xA;## [1] &amp;quot;2024-05-15 00:00:00 AAPL 100 @ 187.035303814948&amp;quot;&#xA;## [1] &amp;quot;2024-05-16 00:00:00 AAPL 100 @ 189.583384873997&amp;quot;&#xA;## [1] &amp;quot;2024-05-17 00:00:00 AAPL 100 @ 188.627846882973&amp;quot;&#xA;## [1] &amp;quot;2024-05-20 00:00:00 AAPL 100 @ 188.448692052772&amp;quot;&#xA;## [1] &amp;quot;2024-05-21 00:00:00 AAPL 100 @ 190.200493983927&amp;quot;&#xA;## [1] &amp;quot;2024-05-22 00:00:00 AAPL 100 @ 191.375009114815&amp;quot;&#xA;## [1] &amp;quot;2024-05-23 00:00:00 AAPL 100 @ 190.091005413995&amp;quot;&#xA;## [1] &amp;quot;2024-05-24 00:00:00 AAPL 100 @ 187.941071512774&amp;quot;&#xA;## [1] &amp;quot;2024-05-28 00:00:00 AAPL 100 @ 190.618537108825&amp;quot;&#xA;## [1] &amp;quot;2024-05-29 00:00:00 AAPL 100 @ 188.72738746937&amp;quot;&#xA;## [1] &amp;quot;2024-05-30 00:00:00 AAPL 100 @ 189.87202827413&amp;quot;&#xA;## [1] &amp;quot;2024-05-31 00:00:00 AAPL 100 @ 190.548870848556&amp;quot;&#xA;## [1] &amp;quot;2024-06-03 00:00:00 AAPL 100 @ 192.002066208281&amp;quot;&#xA;## [1] &amp;quot;2024-06-04 00:00:00 AAPL 100 @ 193.733972172366&amp;quot;&#xA;## [1] &amp;quot;2024-06-05 00:00:00 AAPL 100 @ 194.490428990596&amp;quot;&#xA;## [1] &amp;quot;2024-06-06 00:00:00 AAPL 100 @ 194.77908757849&amp;quot;&#xA;## [1] &amp;quot;2024-06-07 00:00:00 AAPL 100 @ 193.743920155901&amp;quot;&#xA;## [1] &amp;quot;2024-06-10 00:00:00 AAPL 100 @ 195.983446659984&amp;quot;&#xA;## [1] &amp;quot;2024-06-11 00:00:00 AAPL 100 @ 192.748575042975&amp;quot;&#xA;## [1] &amp;quot;2024-06-12 00:00:00 AAPL 100 @ 206.404711207338&amp;quot;&#xA;## [1] &amp;quot;2024-06-13 00:00:00 AAPL 100 @ 213.740415017279&amp;quot;&#xA;## [1] &amp;quot;2024-06-14 00:00:00 AAPL 100 @ 212.854558474285&amp;quot;&#xA;## [1] &amp;quot;2024-06-17 00:00:00 AAPL 100 @ 212.376781884893&amp;quot;&#xA;## [1] &amp;quot;2024-06-18 00:00:00 AAPL 100 @ 216.577139476461&amp;quot;&#xA;## [1] &amp;quot;2024-06-20 00:00:00 AAPL 100 @ 212.93417271809&amp;quot;&#xA;## [1] &amp;quot;2024-06-21 00:00:00 AAPL 100 @ 209.410657700947&amp;quot;&#xA;## [1] &amp;quot;2024-06-24 00:00:00 AAPL 100 @ 206.753088071967&amp;quot;&#xA;## [1] &amp;quot;2024-06-25 00:00:00 AAPL 100 @ 208.176424293325&amp;quot;&#xA;## [1] &amp;quot;2024-06-26 00:00:00 AAPL 100 @ 210.515491383805&amp;quot;&#xA;## [1] &amp;quot;2024-06-27 00:00:00 AAPL 100 @ 213.69064472408&amp;quot;&#xA;## [1] &amp;quot;2024-06-28 00:00:00 AAPL 100 @ 214.765619268571&amp;quot;&#xA;## [1] &amp;quot;2024-07-01 00:00:00 AAPL 100 @ 211.102741355369&amp;quot;&#xA;## [1] &amp;quot;2024-07-02 00:00:00 AAPL 100 @ 215.143840083806&amp;quot;&#xA;## [1] &amp;quot;2024-07-03 00:00:00 AAPL 100 @ 218.975924843674&amp;quot;&#xA;## [1] &amp;quot;2024-07-05 00:00:00 AAPL 100 @ 220.618238204898&amp;quot;&#xA;## [1] &amp;quot;2024-07-08 00:00:00 AAPL 100 @ 226.032918049256&amp;quot;&#xA;## [1] &amp;quot;2024-07-09 00:00:00 AAPL 100 @ 226.869004299051&amp;quot;&#xA;## [1] &amp;quot;2024-07-10 00:00:00 AAPL 100 @ 228.232637431437&amp;quot;&#xA;## [1] &amp;quot;2024-07-11 00:00:00 AAPL 100 @ 230.312905072389&amp;quot;&#xA;## [1] &amp;quot;2024-07-12 00:00:00 AAPL 100 @ 227.854401428441&amp;quot;&#xA;## [1] &amp;quot;2024-07-15 00:00:00 AAPL 100 @ 235.379208052118&amp;quot;&#xA;## [1] &amp;quot;2024-07-16 00:00:00 AAPL 100 @ 233.906101537561&amp;quot;&#xA;## [1] &amp;quot;2024-07-17 00:00:00 AAPL 100 @ 228.381933123271&amp;quot;&#xA;## [1] &amp;quot;2024-07-18 00:00:00 AAPL 100 @ 229.208071389531&amp;quot;&#xA;## [1] &amp;quot;2024-07-19 00:00:00 AAPL 100 @ 223.773495578102&amp;quot;&#xA;## [1] &amp;quot;2024-07-22 00:00:00 AAPL 100 @ 225.95328861769&amp;quot;&#xA;## [1] &amp;quot;2024-07-23 00:00:00 AAPL 100 @ 223.325578127077&amp;quot;&#xA;## [1] &amp;quot;2024-07-24 00:00:00 AAPL 100 @ 222.957305295378&amp;quot;&#xA;## [1] &amp;quot;2024-07-25 00:00:00 AAPL 100 @ 217.910898282719&amp;quot;&#xA;## [1] &amp;quot;2024-07-26 00:00:00 AAPL 100 @ 217.681973159319&amp;quot;&#xA;## [1] &amp;quot;2024-08-19 00:00:00 AAPL 100 @ 224.92934604934&amp;quot;&#xA;## [1] &amp;quot;2024-08-20 00:00:00 AAPL 100 @ 224.97917394971&amp;quot;&#xA;## [1] &amp;quot;2024-08-21 00:00:00 AAPL 100 @ 225.72654683924&amp;quot;&#xA;## [1] &amp;quot;2024-08-22 00:00:00 AAPL 100 @ 226.992087317665&amp;quot;&#xA;## [1] &amp;quot;2024-08-23 00:00:00 AAPL 100 @ 224.869558651032&amp;quot;&#xA;## [1] &amp;quot;2024-08-26 00:00:00 AAPL 100 @ 225.965696432471&amp;quot;&#xA;## [1] &amp;quot;2024-08-27 00:00:00 AAPL 100 @ 225.208364045004&amp;quot;&#xA;## [1] &amp;quot;2024-08-28 00:00:00 AAPL 100 @ 227.121636817559&amp;quot;&#xA;## [1] &amp;quot;2024-08-29 00:00:00 AAPL 100 @ 229.294008589903&amp;quot;&#xA;## [1] &amp;quot;2024-08-30 00:00:00 AAPL 100 @ 229.383689687365&amp;quot;&#xA;## [1] &amp;quot;2024-09-03 00:00:00 AAPL 100 @ 227.749434910473&amp;quot;&#xA;## [1] &amp;quot;2024-09-04 00:00:00 AAPL 100 @ 220.883569906873&amp;quot;&#xA;## [1] &amp;quot;2024-09-05 00:00:00 AAPL 100 @ 220.853676207719&amp;quot;&#xA;## [1] &amp;quot;2024-09-06 00:00:00 AAPL 100 @ 223.165541772554&amp;quot;&#xA;## [1] &amp;quot;2024-09-09 00:00:00 AAPL 100 @ 220.046515919881&amp;quot;&#xA;## [1] &amp;quot;2024-09-24 00:00:00 AAPL 100 @ 227.849075505873&amp;quot;&#xA;## [1] &amp;quot;2024-09-25 00:00:00 AAPL 100 @ 224.142104757378&amp;quot;&#xA;## [1] &amp;quot;2024-09-26 00:00:00 AAPL 100 @ 226.503813427924&amp;quot;&#xA;## [1] &amp;quot;2024-09-27 00:00:00 AAPL 100 @ 227.659753813012&amp;quot;&#xA;## [1] &amp;quot;2024-09-30 00:00:00 AAPL 100 @ 229.234205986255&amp;quot;&#xA;## [1] &amp;quot;2024-10-01 00:00:00 AAPL 100 @ 228.716038397359&amp;quot;&#xA;## [1] &amp;quot;2024-10-02 00:00:00 AAPL 100 @ 225.098748746326&amp;quot;&#xA;## [1] &amp;quot;2024-10-03 00:00:00 AAPL 100 @ 224.351375856796&amp;quot;&#xA;## [1] &amp;quot;2024-10-04 00:00:00 AAPL 100 @ 227.101702616343&amp;quot;&#xA;## [1] &amp;quot;2024-10-07 00:00:00 AAPL 100 @ 223.713618265944&amp;quot;&#xA;## [1] &amp;quot;2024-10-08 00:00:00 AAPL 100 @ 223.514321869804&amp;quot;&#xA;## [1] &amp;quot;2024-10-09 00:00:00 AAPL 100 @ 224.441056954258&amp;quot;&#xA;## [1] &amp;quot;2024-10-10 00:00:00 AAPL 100 @ 226.982127819727&amp;quot;&#xA;## [1] &amp;quot;2024-10-11 00:00:00 AAPL 100 @ 228.496807800003&amp;quot;&#xA;## [1] &amp;quot;2024-10-14 00:00:00 AAPL 100 @ 227.898903406243&amp;quot;&#xA;## [1] &amp;quot;2024-10-15 00:00:00 AAPL 100 @ 232.791708238981&amp;quot;&#xA;## [1] &amp;quot;2024-10-16 00:00:00 AAPL 100 @ 230.788754368963&amp;quot;&#xA;## [1] &amp;quot;2024-10-17 00:00:00 AAPL 100 @ 232.612330838716&amp;quot;&#xA;## [1] &amp;quot;2024-10-18 00:00:00 AAPL 100 @ 235.352698100326&amp;quot;&#xA;## [1] &amp;quot;2024-10-21 00:00:00 AAPL 100 @ 233.628762225972&amp;quot;&#xA;## [1] &amp;quot;2024-10-22 00:00:00 AAPL 100 @ 233.070726234644&amp;quot;&#xA;## [1] &amp;quot;2024-10-23 00:00:00 AAPL 100 @ 233.260063132847&amp;quot;&#xA;## [1] &amp;quot;2024-10-24 00:00:00 AAPL 100 @ 229.174418587947&amp;quot;&#xA;## [1] &amp;quot;2024-10-25 00:00:00 AAPL 100 @ 228.935268994715&amp;quot;&#xA;## [1] &amp;quot;2024-10-28 00:00:00 AAPL 100 @ 232.502730745379&amp;quot;&#xA;## [1] &amp;quot;2024-10-29 00:00:00 AAPL 100 @ 232.283500148023&amp;quot;&#xA;## [1] &amp;quot;2024-10-30 00:00:00 AAPL 100 @ 231.795211052941&amp;quot;&#xA;## [1] &amp;quot;2024-10-31 00:00:00 AAPL 100 @ 228.536660997095&amp;quot;&#xA;## [1] &amp;quot;2024-11-01 00:00:00 AAPL 100 @ 220.195984415651&amp;quot;&#xA;## [1] &amp;quot;2024-11-25 00:00:00 AAPL 100 @ 230.903007253043&amp;quot;&#xA;## [1] &amp;quot;2024-11-26 00:00:00 AAPL 100 @ 232.76850230006&amp;quot;&#xA;## [1] &amp;quot;2024-11-27 00:00:00 AAPL 100 @ 233.905758325728&amp;quot;&#xA;## [1] &amp;quot;2024-11-29 00:00:00 AAPL 100 @ 234.244936475718&amp;quot;&#xA;## [1] &amp;quot;2024-12-02 00:00:00 AAPL 100 @ 236.699023279564&amp;quot;&#xA;## [1] &amp;quot;2024-12-03 00:00:00 AAPL 100 @ 239.232904171115&amp;quot;&#xA;## [1] &amp;quot;2024-12-04 00:00:00 AAPL 100 @ 242.285537965167&amp;quot;&#xA;## [1] &amp;quot;2024-12-05 00:00:00 AAPL 100 @ 243.402853079944&amp;quot;&#xA;## [1] &amp;quot;2024-12-06 00:00:00 AAPL 100 @ 242.32545023109&amp;quot;&#xA;## [1] &amp;quot;2024-12-09 00:00:00 AAPL 100 @ 241.248047382235&amp;quot;&#xA;## [1] &amp;quot;2024-12-10 00:00:00 AAPL 100 @ 246.295868254446&amp;quot;&#xA;## [1] &amp;quot;2024-12-11 00:00:00 AAPL 100 @ 247.363300647855&amp;quot;&#xA;## [1] &amp;quot;2024-12-12 00:00:00 AAPL 100 @ 246.295868254446&amp;quot;&#xA;## [1] &amp;quot;2024-12-13 00:00:00 AAPL 100 @ 247.223638161266&amp;quot;&#xA;## [1] &amp;quot;2024-12-16 00:00:00 AAPL 100 @ 247.393227236262&amp;quot;&#xA;## [1] &amp;quot;2024-12-17 00:00:00 AAPL 100 @ 249.478194079641&amp;quot;&#xA;## [1] &amp;quot;2024-12-18 00:00:00 AAPL 100 @ 251.553190467575&amp;quot;&#xA;## [1] &amp;quot;2024-12-19 00:00:00 AAPL 100 @ 246.904400922168&amp;quot;&#xA;## [1] &amp;quot;2024-12-20 00:00:00 AAPL 100 @ 247.44309473556&amp;quot;&#xA;## [1] &amp;quot;2024-12-23 00:00:00 AAPL 100 @ 254.156910213455&amp;quot;&#xA;## [1] &amp;quot;2024-12-24 00:00:00 AAPL 100 @ 254.875178779358&amp;quot;&#xA;## [1] &amp;quot;2024-12-26 00:00:00 AAPL 100 @ 257.568678290458&amp;quot;&#xA;## [1] &amp;quot;2024-12-27 00:00:00 AAPL 100 @ 257.209528785437&amp;quot;&#xA;## [1] &amp;quot;2024-12-30 00:00:00 AAPL 100 @ 251.623014099834&amp;quot;&#xA;## [1] &amp;quot;2024-12-31 00:00:00 AAPL 100 @ 251.832515440751&amp;quot;&#xA;## [1] &amp;quot;2025-01-02 00:00:00 AAPL 100 @ 248.330952376458&amp;quot;&#xA;## [1] &amp;quot;2025-01-03 00:00:00 AAPL 100 @ 242.774364279261&amp;quot;&#xA;## [1] &amp;quot;2025-01-06 00:00:00 AAPL 100 @ 243.722075096973&amp;quot;&#xA;## [1] &amp;quot;2025-01-07 00:00:00 AAPL 100 @ 242.395273863349&amp;quot;&#xA;## [1] &amp;quot;2025-02-18 00:00:00 AAPL 100 @ 243.830248775831&amp;quot;&#xA;## [1] &amp;quot;2025-02-19 00:00:00 AAPL 100 @ 244.33959061953&amp;quot;&#xA;## [1] &amp;quot;2025-02-20 00:00:00 AAPL 100 @ 244.619222705214&amp;quot;&#xA;## [1] &amp;quot;2025-02-21 00:00:00 AAPL 100 @ 245.627894497229&amp;quot;&#xA;## [1] &amp;quot;2025-02-24 00:00:00 AAPL 100 @ 244.609226048635&amp;quot;&#xA;## [1] &amp;quot;2025-02-25 00:00:00 AAPL 100 @ 247.675212812187&amp;quot;&#xA;## [1] &amp;quot;2025-02-26 00:00:00 AAPL 100 @ 244.010020967373&amp;quot;&#xA;## [1] &amp;quot;2025-02-27 00:00:00 AAPL 100 @ 239.096466154756&amp;quot;&#xA;## [1] &amp;quot;2025-02-28 00:00:00 AAPL 100 @ 236.639681129044&amp;quot;&#xA;## [1] &amp;quot;2025-03-03 00:00:00 AAPL 100 @ 241.473338883066&amp;quot;&#xA;## [1] &amp;quot;2025-03-04 00:00:00 AAPL 100 @ 237.398695566304&amp;quot;&#xA;## [1] &amp;quot;2025-03-05 00:00:00 AAPL 100 @ 235.111686075558&amp;quot;&#xA;## [1] &amp;quot;2025-03-06 00:00:00 AAPL 100 @ 234.132973775665&amp;quot;&#xA;## [1] &amp;quot;2025-03-12 00:00:00 AAPL -100 @ 219.851698376234&amp;quot;&#xA;## [1] &amp;quot;2025-03-13 00:00:00 AAPL -100 @ 215.667183269948&amp;quot;&#xA;## [1] &amp;quot;2025-03-14 00:00:00 AAPL -100 @ 210.973341558768&amp;quot;&#xA;## [1] &amp;quot;2025-03-17 00:00:00 AAPL -100 @ 213.030641291499&amp;quot;&#xA;## [1] &amp;quot;2025-03-18 00:00:00 AAPL -100 @ 213.879534205128&amp;quot;&#xA;## [1] &amp;quot;2025-03-19 00:00:00 AAPL -100 @ 213.939453189374&amp;quot;&#xA;## [1] &amp;quot;2025-03-20 00:00:00 AAPL -100 @ 213.709758670163&amp;quot;&#xA;## [1] &amp;quot;2025-03-21 00:00:00 AAPL -100 @ 211.282933136575&amp;quot;&#xA;## [1] &amp;quot;2025-03-24 00:00:00 AAPL -100 @ 220.710572707635&amp;quot;&#xA;## [1] &amp;quot;2025-03-25 00:00:00 AAPL -100 @ 220.480878188424&amp;quot;&#xA;## [1] &amp;quot;2025-03-26 00:00:00 AAPL -100 @ 223.217280061014&amp;quot;&#xA;## [1] &amp;quot;2025-03-27 00:00:00 AAPL -100 @ 221.100061344037&amp;quot;&#xA;## [1] &amp;quot;2025-03-28 00:00:00 AAPL -100 @ 221.379693429721&amp;quot;&#xA;## [1] &amp;quot;2025-03-31 00:00:00 AAPL -100 @ 216.725792628436&amp;quot;&#xA;## [1] &amp;quot;2025-04-01 00:00:00 AAPL -100 @ 219.522128724077&amp;quot;&#xA;## [1] &amp;quot;2025-04-02 00:00:00 AAPL -100 @ 221.030160942019&amp;quot;&#xA;## [1] &amp;quot;2025-04-04 00:00:00 AAPL -100 @ 193.636076052363&amp;quot;&#xA;## [1] &amp;quot;2025-04-07 00:00:00 AAPL -100 @ 176.967931268044&amp;quot;&#xA;## [1] &amp;quot;2025-04-08 00:00:00 AAPL -100 @ 186.455489823349&amp;quot;&#xA;## [1] &amp;quot;2025-04-09 00:00:00 AAPL -100 @ 171.724806803269&amp;quot;&#xA;## [1] &amp;quot;2025-04-10 00:00:00 AAPL -100 @ 188.822396372692&amp;quot;&#xA;## [1] &amp;quot;2025-04-11 00:00:00 AAPL -100 @ 185.856284742087&amp;quot;&#xA;## [1] &amp;quot;2025-04-14 00:00:00 AAPL -100 @ 211.163095168083&amp;quot;&#xA;## [1] &amp;quot;2025-04-15 00:00:00 AAPL -100 @ 201.595639554182&amp;quot;&#xA;## [1] &amp;quot;2025-04-16 00:00:00 AAPL -100 @ 198.100223244332&amp;quot;&#xA;## [1] &amp;quot;2025-04-17 00:00:00 AAPL -100 @ 196.941738752897&amp;quot;&#xA;## [1] &amp;quot;2025-04-21 00:00:00 AAPL -100 @ 193.01689289675&amp;quot;&#xA;## [1] &amp;quot;2025-04-22 00:00:00 AAPL -100 @ 195.863151320059&amp;quot;&#xA;## [1] &amp;quot;2025-04-23 00:00:00 AAPL -100 @ 205.730217093994&amp;quot;&#xA;## [1] &amp;quot;2025-04-24 00:00:00 AAPL -100 @ 204.621670169033&amp;quot;&#xA;## [1] &amp;quot;2025-05-12 00:00:00 AAPL -100 @ 210.970001220703&amp;quot;&#xA;## [1] &amp;quot;2025-05-13 00:00:00 AAPL -100 @ 210.429992675781&amp;quot;&#xA;## [1] &amp;quot;2025-05-14 00:00:00 AAPL -100 @ 212.429992675781&amp;quot;&#xA;## [1] &amp;quot;2025-05-15 00:00:00 AAPL -100 @ 210.949996948242&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updatePortf(portfolio.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;quant&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updateAcct(account.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;quant&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updateEndEq(account.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;quant&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 6. 绩效分析&#xA;equity &amp;lt;- getAccount(account.st)$summary$End.Eq&#xA;returns &amp;lt;- Return.calculate(equity)&#xA;charts.PerformanceSummary(returns, main=&amp;quot;策略资金曲线&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/05/09/rate-of-change-ema-strategy-demonstrating-ternary-indicator-ordersets-to-handle-simultaneous-stop-loss-and-take-profit-orders/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于 quantstrat 的配对交易策略实现与解析</title>
      <link>http://gewutang.cn/2015/04/10/pair-trade/</link>
      <pubDate>Fri, 10 Apr 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/04/10/pair-trade/</guid>
      <description>&lt;p&gt;配对交易是一种基于统计套利的量化策略，其核心逻辑是通过识别价格走势高度相关的两只股票，当它们的价格偏离历史正常关系时建立对冲头寸，等待关系回归时获利。&lt;/p&gt;&#xA;&lt;p&gt;本文将解析一段基于 R 语言 quantstrat 包的配对交易策略代码，展示其实现思路与核心意义。&lt;/p&gt;&#xA;&lt;div id=&#34;策略核心逻辑与代码框架&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;策略核心逻辑与代码框架&lt;/h1&gt;&#xA;&lt;p&gt;这段代码以 SPY 和 DIA 两只 ETF 为交易标的，通过构建价格比率的均值回归模型实现配对交易。策略的核心步骤包括：数据同步与准备、策略环境初始化、指标与信号构建、自定义订单规模控制、交易规则设置，以及回测与绩效分析。&lt;/p&gt;&#xA;&lt;p&gt;以下是完整代码实现：&lt;/p&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Kindly contributed to quantstrat by Garrett See&#xA;# code borrowed heavily from existing quantstrat demos&#xA;&#xA;# 这是一个简单的配对交易示例，旨在展示如何扩展现有 quantstrat 功能。&#xA;# 它使用 addPosLimits 指定头寸水平和限制，并演示如何将自定义订单规模函数传递给 osFUN&#xA;&#xA;# 注意：先构建价差并将其视为单一工具，比处理股票组合更简单&#xA;&#xA;## 给定两只股票，计算其名义价值比率。如果比率低于 2 标准差区间，&#xA;# 当它回升穿过区间时，买入股票 1 并卖出股票 2。如果比率高于 2 标准差区间，&#xA;# 当它回落穿过区间时，卖出股票 1 并买入股票 2。如果比率穿过其移动平均线，则平仓所有头寸。&#xA;&#xA;# 股票 A 的买入（卖出）数量 = MaxPos / lvls&#xA;# 股票 B 的卖出（买入）数量 = MaxPos * 比率 / lvls  &#xA;&#xA;require(quantstrat)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;suppressWarnings(rm(&amp;quot;order_book.pair1&amp;quot;,pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.pairs&amp;quot;, &amp;quot;portfolio.pair1&amp;quot;, pos=.blotter))&#xA;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;, &#xA;                    &amp;quot;symb1&amp;quot;, &amp;quot;symb2&amp;quot;, &amp;quot;portfolio1.st&amp;quot;, &amp;quot;account.st&amp;quot;, &#xA;                    &amp;quot;pairStrat&amp;quot;, &amp;quot;out1&amp;quot;))&#xA;&#xA;initDate &amp;lt;- &amp;#39;2013-12-31&amp;#39;    &#xA;startDate &amp;lt;- &amp;#39;2014-01-01&amp;#39;&#xA;endDate &amp;lt;- &amp;#39;2015-05-01&amp;#39;&#xA;initEq &amp;lt;- 100000&#xA;SD &amp;lt;- 2  # 标准差倍数&#xA;N &amp;lt;- 20  # 移动平均线周期&#xA;&#xA;MaxPos &amp;lt;- 1500  # 股票 A 的最大头寸；&#xA;# 股票 B 的最大头寸将是 max * 比率，即对股票 B 没有硬性头寸限制&#xA;lvls &amp;lt;- 3  # 分几步建仓；每笔订单数量 = MaxPos/lvls&#xA;&#xA;symb1 &amp;lt;- &amp;#39;SPY&amp;#39;  # 可更改这些标的以尝试其他配对&#xA;symb2 &amp;lt;- &amp;#39;DIA&amp;#39;  # 如果更改，需确保头寸限制仍然合理&#xA;&#xA;portfolio1.st &amp;lt;- &amp;#39;pair1&amp;#39;&#xA;account.st &amp;lt;- &amp;#39;pairs&amp;#39;&#xA;&#xA;# 获取历史数据&#xA;getSymbols(c(symb1, symb2), from=startDate, to=endDate, adjust=TRUE) &lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot; &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 以下函数用于确保所有标的的时间戳相同，删除其中一只股票数据缺失的行&#xA;alignSymbols &amp;lt;- function(symbols, env=.GlobalEnv) {&#xA;  # 这是 qmao::alignSymbols() 的简化版本&#xA;  if (length(symbols) &amp;lt; 2) &#xA;    stop(&amp;quot;必须提供至少 2 个标的&amp;quot;)&#xA;  if (any(!is.character(symbols))) &#xA;    stop(&amp;quot;标的必须是字符向量。&amp;quot;)&#xA;  ff &amp;lt;- get(symbols[1],env=env)&#xA;  for (sym in symbols[-1]) {&#xA;    tmp.sym &amp;lt;- get(sym,env=env)&#xA;    ff &amp;lt;- merge(ff, tmp.sym, all=FALSE)  # 只保留双方都有数据的行&#xA;  }&#xA;  for (sym in symbols) {&#xA;    assign(sym,ff[,grep(sym, colnames(ff))], env=env)  # 更新标的数据&#xA;  }&#xA;  symbols&#xA;}&#xA;alignSymbols(c(symb1, symb2))  # 对齐两只股票的时间戳&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot; &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 定义金融工具&#xA;currency(&amp;quot;USD&amp;quot;)  # 基础货币为美元&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(symb1, currency=&amp;quot;USD&amp;quot;, multiplier=1)  # 定义股票 1 的属性&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;SPY&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stock(symb2, currency=&amp;quot;USD&amp;quot;, multiplier=1)  # 定义股票 2 的属性&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;DIA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 初始化投资组合、账户和订单&#xA;initPortf(name=portfolio1.st, c(symb1,symb2))  # 创建包含两只股票的组合&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pair1&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(account.st, portfolios=portfolio1.st, initEq=initEq)  # 初始化账户，初始资金 10 万美元&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio=portfolio1.st)  # 初始化订单簿&#xA;&#xA;# osFUN 需要知道哪个标的是第一腿、哪个是第二腿，以及 MaxPos 和 lvls 的值。&#xA;# 因此，在投资组合中创建一个插槽来存储这些信息。&#xA;pair &amp;lt;- c(1, 2, MaxPos, lvls)&#xA;names(pair) &amp;lt;- c(symb1, symb2, &amp;quot;MaxPos&amp;quot;, &amp;quot;lvls&amp;quot;)&#xA;.blotter[[paste(&amp;#39;portfolio&amp;#39;, portfolio1.st, sep=&amp;#39;.&amp;#39;)]]$pair &amp;lt;- pair  # 存储到组合属性中&#xA;&#xA;# 按标的创建初始头寸限制和水平&#xA;# 如果 lvls=3，则允许多空各 3 次建仓。&#xA;addPosLimit(portfolio=portfolio1.st, timestamp=startDate, symbol=symb1, &#xA;            maxpos=MaxPos, longlevels=lvls, minpos=-MaxPos, shortlevels=lvls)&#xA;addPosLimit(portfolio=portfolio1.st, timestamp=startDate, symbol=symb2, &#xA;            maxpos=MaxPos, longlevels=lvls, minpos=-MaxPos, shortlevels=lvls)&#xA;&#xA;# 创建策略对象&#xA;pairStrat &amp;lt;- strategy(&amp;#39;pairStrat&amp;#39;)&#xA;&#xA;# 指标函数：计算两只股票的价格比率&#xA;calcRatio &amp;lt;- function(x) { &#xA;  # 返回两只股票的名义收盘价比率&#xA;  x1 &amp;lt;- get(x[1])&#xA;  x2 &amp;lt;- get(x[2])&#xA;  mult1 &amp;lt;- getInstrument(x[1])$multiplier  # 合约乘数&#xA;  mult2 &amp;lt;- getInstrument(x[2])$multiplier&#xA;  rat &amp;lt;- (mult1 * Cl(x1)) / (mult2 * Cl(x2))  # 收盘价比率&#xA;  colnames(rat) &amp;lt;- &amp;#39;Ratio&amp;#39;&#xA;  rat&#xA;}&#xA;# 用于确定入场/出场的指标：比率及其布林带&#xA;Ratio &amp;lt;- calcRatio(c(symb1[1], symb2[1]))  &#xA;&#xA;# 将对冲比率存储在投资组合中，以便订单规模函数使用。&#xA;# 在本示例中，对冲比率与 Ratio 指标相同。&#xA;.blotter[[paste(&amp;#39;portfolio&amp;#39;,portfolio1.st,sep=&amp;#39;.&amp;#39;)]]$HedgeRatio &amp;lt;- Ratio&#xA;# 创建获取最新对冲比率的函数&#xA;getHedgeRatio &amp;lt;- function(portfolio, timestamp) {&#xA;  portf &amp;lt;- getPortfolio(portfolio)&#xA;  timestamp &amp;lt;- format(timestamp,&amp;quot;%Y-%m-%d %H:%M:%S&amp;quot;)  # 格式化时间戳&#xA;  # 上述行确保在使用日内数据且时间戳为午夜时，不会获取次日的最后一个值&#xA;  toDate &amp;lt;- paste(&amp;quot;::&amp;quot;, timestamp, sep=&amp;quot;&amp;quot;)&#xA;  Ratio &amp;lt;- last(portf$HedgeRatio[toDate])  # 获取该时间点的最新比率&#xA;  as.numeric(Ratio)&#xA;}&#xA;&#xA;# 添加指标：比率的布林带&#xA;pairStrat &amp;lt;- add.indicator(strategy=pairStrat, name = &amp;quot;calcRatio&amp;quot;, &#xA;                           arguments=list(x=c(symb1,symb2)))&#xA;pairStrat &amp;lt;- add.indicator(strategy=pairStrat, name = &amp;quot;BBands&amp;quot;, &#xA;                           arguments=list(HLC=quote(Ratio), sd=SD, n=N, &#xA;                                          maType=&amp;#39;SMA&amp;#39;))  # 基于 20 日 SMA 计算布林带&#xA;&#xA;# 生成信号：当比率从下方穿过下轨时买入，从上方穿过上轨时卖出，穿过中轨时平仓&#xA;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#xA;                        arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;up&amp;quot;), &#xA;                                        relationship=&amp;quot;lt&amp;quot;),  # 比率 &amp;lt; 上轨（从上方穿过）&#xA;                        label=&amp;quot;cross.up&amp;quot;)&#xA;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#xA;                        arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;dn&amp;quot;), &#xA;                                        relationship=&amp;quot;gt&amp;quot;),  # 比率 &amp;gt; 下轨（从下方穿过）&#xA;                        label=&amp;quot;cross.dn&amp;quot;)&#xA;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#xA;                        arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;mavg&amp;quot;), &#xA;                                  relationship=&amp;quot;lt&amp;quot;),  # 比率 &amp;lt; 中轨（从上方穿过）&#xA;                        label=&amp;quot;cross.mid.fa&amp;quot;)&#xA;pairStrat &amp;lt;- add.signal(strategy=pairStrat, name=&amp;quot;sigCrossover&amp;quot;, &#xA;                        arguments=list(columns=c(&amp;quot;Ratio&amp;quot;,&amp;quot;mavg&amp;quot;), &#xA;                                       relationship=&amp;quot;gt&amp;quot;),  # 比率 &amp;gt; 中轨（从下方穿过）&#xA;                        label=&amp;quot;cross.mid.fb&amp;quot;)&#xA;&#xA;# 自定义订单规模函数&#xA;#######################_ORDER SIZING FUNCTION_##################################&#xA;osSpreadMaxPos &amp;lt;- function (data, timestamp, orderqty, ordertype, orderside, &#xA;                            portfolio, symbol, ruletype, ..., orderprice) {&#xA;  portf &amp;lt;- getPortfolio(portfolio)&#xA;  # 检查 pair 插槽是否包含该函数所需的内容&#xA;  if (!any(portf$pair == 1) &amp;amp;&amp;amp; !(any(portf$pair == 2))) &#xA;    stop(&amp;#39;pair 必须包含值 1 和 2&amp;#39;)&#xA;  if (!any(names(portf$pair) == &amp;quot;MaxPos&amp;quot;) || !any(names(portf$pair) == &amp;quot;lvls&amp;quot;)) &#xA;    stop(&amp;#39;pair 必须包含 MaxPos 和 lvls&amp;#39;)  &#xA;  &#xA;  if (portf$pair[symbol] == 1) legside &amp;lt;- &amp;quot;long&amp;quot;  # 第一腿&#xA;  if (portf$pair[symbol] == 2) legside &amp;lt;- &amp;quot;short&amp;quot;  # 第二腿（与第一腿方向相反）&#xA;  MaxPos &amp;lt;- portf$pair[&amp;quot;MaxPos&amp;quot;]&#xA;  lvls &amp;lt;- portf$pair[&amp;quot;lvls&amp;quot;]&#xA;  ratio &amp;lt;- getHedgeRatio(portfolio, timestamp)  # 获取当前对冲比率&#xA;  pos &amp;lt;- getPosQty(portfolio, symbol, timestamp)  # 当前头寸&#xA;  PosLimit &amp;lt;- getPosLimit(portfolio, symbol, timestamp)  # 头寸限制&#xA;  qty &amp;lt;- orderqty&#xA;  if (legside == &amp;quot;short&amp;quot;) {# 标的是第二腿&#xA;    ## 注释掉下一行可使每只股票的订单规模相等。&#xA;    addPosLimit(portfolio=portfolio, timestamp=timestamp, symbol=symbol, &#xA;                maxpos=round(MaxPos*ratio,0), longlevels=lvls, &#xA;                minpos=round(-MaxPos*ratio,0), shortlevels=lvls)  # 调整第二腿的头寸限制&#xA;    ## &#xA;    qty &amp;lt;- -orderqty  # 反转股票 B 的订单数量（多空相反）&#xA;  }&#xA;  &#xA;  if (qty &amp;gt; 0) orderside = &amp;#39;long&amp;#39;  # 确定订单方向&#xA;  if (qty &amp;lt; 0) orderside = &amp;#39;short&amp;#39;&#xA; &#xA;  # 调用 osMaxPos 计算符合头寸限制的订单数量&#xA;  orderqty &amp;lt;- osMaxPos(data=data,timestamp=timestamp, orderqty=qty,&#xA;                       ordertype=ordertype, orderside=orderside,&#xA;                       portfolio=portfolio, symbol=symbol, ruletype=ruletype, &#xA;                       ...)&#xA;          &#xA;  # 在规则信号函数中添加订单&#xA;  if (!is.null(orderqty) &amp;amp; !orderqty == 0 &amp;amp; !is.null(orderprice)) {&#xA;    addOrder(portfolio=portfolio, symbol=symbol, &#xA;             timestamp=timestamp, qty=orderqty, price=as.numeric(orderprice), &#xA;             ordertype=ordertype, side=orderside, replace=FALSE,&#xA;             status=&amp;quot;open&amp;quot;, ...=...)&#xA;  }&#xA;  return(0)  # 使 ruleSignal 函数不会尝试下单&#xA;}&#xA;################################################################################&#xA;&#xA;# 为多空方向创建入场和出场规则。两个标的将获得相同的买卖信号，&#xA;# 但 osMaxPos 会为第二个标的反转这些信号。&#xA;# orderqty 大于头寸限制允许的值。osMaxPos 会将订单数量调整为最大允许值的 1/3。&#xA;# （1/3 是因为我们在 PosLimit 中使用了 3 个水平）&#xA;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                      arguments=list(sigcol=&amp;quot;cross.dn&amp;quot;, sigval=TRUE, &#xA;                                     orderqty=1e6, ordertype=&amp;#39;market&amp;#39;, &#xA;                                     orderside=NULL, osFUN=&amp;#39;osSpreadMaxPos&amp;#39;), &#xA;                      type=&amp;#39;enter&amp;#39;)  # cross.dn 信号触发入场&#xA;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                      arguments=list(sigcol=&amp;quot;cross.up&amp;quot;, sigval=TRUE, &#xA;                                     orderqty=-1e6, ordertype=&amp;#39;market&amp;#39;, &#xA;                                     orderside=NULL, osFUN=&amp;#39;osSpreadMaxPos&amp;#39;), &#xA;                      type=&amp;#39;enter&amp;#39;)  # cross.up 信号触发入场&#xA;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                      arguments=list(sigcol=&amp;quot;cross.mid.fb&amp;quot;, sigval=TRUE, &#xA;                                     orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, &#xA;                                     orderside=NULL), &#xA;                      type=&amp;#39;exit&amp;#39;)  # cross.mid.fb 信号触发出场&#xA;pairStrat &amp;lt;- add.rule(strategy=pairStrat, name=&amp;#39;ruleSignal&amp;#39;, &#xA;                      arguments=list(sigcol=&amp;quot;cross.mid.fa&amp;quot;, sigval=TRUE, &#xA;                                     orderqty=&amp;#39;all&amp;#39;, ordertype=&amp;#39;market&amp;#39;, &#xA;                                     orderside=NULL), &#xA;                      type=&amp;#39;exit&amp;#39;)  # cross.mid.fa 信号触发出场&#xA;&#xA;&#xA;# 执行策略回测&#xA;out1&amp;lt;-applyStrategy(strategy=pairStrat, portfolios=portfolio1.st)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2014-02-04 00:00:00 DIA 500 @ 150.182621296714&amp;quot;&#xA;## [1] &amp;quot;2014-03-07 00:00:00 DIA 568 @ 160.616223229789&amp;quot;&#xA;## [1] &amp;quot;2014-03-17 00:00:00 DIA 573 @ 158.690860184405&amp;quot;&#xA;## [1] &amp;quot;2014-03-25 00:00:00 DIA -1641 @ 159.963991320726&amp;quot;&#xA;## [1] &amp;quot;2014-03-31 00:00:00 DIA -574 @ 160.796174254035&amp;quot;&#xA;## [1] &amp;quot;2014-04-22 00:00:00 DIA 574 @ 161.4743809578&amp;quot;&#xA;## [1] &amp;quot;2014-05-22 00:00:00 DIA 571 @ 162.261848260392&amp;quot;&#xA;## [1] &amp;quot;2014-06-02 00:00:00 DIA 572 @ 164.246166224292&amp;quot;&#xA;## [1] &amp;quot;2014-06-27 00:00:00 DIA 576 @ 165.51814455118&amp;quot;&#xA;## [1] &amp;quot;2014-07-02 00:00:00 DIA 27 @ 166.728744615667&amp;quot;&#xA;## [1] &amp;quot;2014-07-15 00:00:00 DIA -1746 @ 167.69330191996&amp;quot;&#xA;## [1] &amp;quot;2014-07-21 00:00:00 DIA -583 @ 167.676368799237&amp;quot;&#xA;## [1] &amp;quot;2014-07-24 00:00:00 DIA 583 @ 167.932553399241&amp;quot;&#xA;## [1] &amp;quot;2014-09-17 00:00:00 DIA -580 @ 169.355777408222&amp;quot;&#xA;## [1] &amp;quot;2014-09-24 00:00:00 DIA -584 @ 169.969729387294&amp;quot;&#xA;## [1] &amp;quot;2014-10-15 00:00:00 DIA -582 @ 159.57109738308&amp;quot;&#xA;## [1] &amp;quot;2014-10-21 00:00:00 DIA 1746 @ 164.043208292008&amp;quot;&#xA;## [1] &amp;quot;2014-10-27 00:00:00 DIA 576 @ 166.101170700567&amp;quot;&#xA;## [1] &amp;quot;2014-11-05 00:00:00 DIA -576 @ 172.700494456876&amp;quot;&#xA;## [1] &amp;quot;2014-12-03 00:00:00 DIA -586 @ 177.512433609148&amp;quot;&#xA;## [1] &amp;quot;2014-12-10 00:00:00 DIA -579 @ 173.872106214788&amp;quot;&#xA;## [1] &amp;quot;2014-12-18 00:00:00 DIA 1165 @ 176.351884195197&amp;quot;&#xA;## [1] &amp;quot;2015-01-15 00:00:00 DIA -579 @ 172.030741759151&amp;quot;&#xA;## [1] &amp;quot;2015-01-21 00:00:00 DIA 579 @ 174.456324855292&amp;quot;&#xA;## [1] &amp;quot;2015-01-29 00:00:00 DIA 578 @ 173.013702717688&amp;quot;&#xA;## [1] &amp;quot;2015-02-05 00:00:00 DIA -578 @ 177.640012837652&amp;quot;&#xA;## [1] &amp;quot;2015-04-21 00:00:00 DIA 583 @ 179.259994506836&amp;quot;&#xA;## [1] &amp;quot;2015-04-28 00:00:00 DIA 583 @ 180.839996337891&amp;quot;&#xA;## [1] &amp;quot;2014-02-04 00:00:00 SPY -500 @ 171.264049909226&amp;quot;&#xA;## [1] &amp;quot;2014-03-07 00:00:00 SPY -500 @ 183.831285747939&amp;quot;&#xA;## [1] &amp;quot;2014-03-17 00:00:00 SPY -500 @ 181.946695046646&amp;quot;&#xA;## [1] &amp;quot;2014-03-25 00:00:00 SPY 1500 @ 182.730103228311&amp;quot;&#xA;## [1] &amp;quot;2014-03-31 00:00:00 SPY 500 @ 183.416649931591&amp;quot;&#xA;## [1] &amp;quot;2014-04-22 00:00:00 SPY -500 @ 184.279745767482&amp;quot;&#xA;## [1] &amp;quot;2014-05-22 00:00:00 SPY -500 @ 185.94707775133&amp;quot;&#xA;## [1] &amp;quot;2014-06-02 00:00:00 SPY -500 @ 189.193474635518&amp;quot;&#xA;## [1] &amp;quot;2014-06-27 00:00:00 SPY -500 @ 192.977678507185&amp;quot;&#xA;## [1] &amp;quot;2014-07-15 00:00:00 SPY 1500 @ 194.367200918673&amp;quot;&#xA;## [1] &amp;quot;2014-07-21 00:00:00 SPY 500 @ 194.475604869373&amp;quot;&#xA;## [1] &amp;quot;2014-07-24 00:00:00 SPY -500 @ 195.766587803966&amp;quot;&#xA;## [1] &amp;quot;2014-09-17 00:00:00 SPY 500 @ 197.83611230376&amp;quot;&#xA;## [1] &amp;quot;2014-09-24 00:00:00 SPY 500 @ 197.582667853886&amp;quot;&#xA;## [1] &amp;quot;2014-10-15 00:00:00 SPY 500 @ 184.582760931563&amp;quot;&#xA;## [1] &amp;quot;2014-10-21 00:00:00 SPY -1500 @ 192.147074898032&amp;quot;&#xA;## [1] &amp;quot;2014-10-27 00:00:00 SPY -500 @ 194.216362617501&amp;quot;&#xA;## [1] &amp;quot;2014-11-05 00:00:00 SPY 500 @ 200.335121161983&amp;quot;&#xA;## [1] &amp;quot;2014-12-03 00:00:00 SPY 500 @ 205.830132301385&amp;quot;&#xA;## [1] &amp;quot;2014-12-10 00:00:00 SPY 500 @ 201.147003486903&amp;quot;&#xA;## [1] &amp;quot;2014-12-18 00:00:00 SPY -1000 @ 204.731130073504&amp;quot;&#xA;## [1] &amp;quot;2015-01-15 00:00:00 SPY 500 @ 198.135576473045&amp;quot;&#xA;## [1] &amp;quot;2015-01-21 00:00:00 SPY -500 @ 202.177531751319&amp;quot;&#xA;## [1] &amp;quot;2015-01-29 00:00:00 SPY -500 @ 201.092379263502&amp;quot;&#xA;## [1] &amp;quot;2015-02-05 00:00:00 SPY 500 @ 205.204015568481&amp;quot;&#xA;## [1] &amp;quot;2015-04-21 00:00:00 SPY -500 @ 209.600006103516&amp;quot;&#xA;## [1] &amp;quot;2015-04-28 00:00:00 SPY -500 @ 211.440002441406&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 更新组合和账户数据&#xA;updatePortf(Portfolio=portfolio1.st,&#xA;            Dates=paste(&amp;quot;::&amp;quot;, as.Date(Sys.time()), sep=&amp;#39;&amp;#39;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-07-13 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2014-01-02/2015-04-30. Using all data&#xA;## instead.&#xA;## Warning in .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :&#xA;## Could not parse ::2025-07-13 as ISO8601 string, or one/bothends of the range&#xA;## were outside the available prices: 2014-01-02/2015-04-30. Using all data&#xA;## instead.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pair1&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updateAcct(account.st, Dates=paste(startDate, endDate, sep=&amp;quot;::&amp;quot;)) &lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;updateEndEq(account.st, Dates=paste(startDate, endDate, sep=&amp;quot;::&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;pairs&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;getEndEq(account.st, Sys.time())  # 查看最终权益&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 103576.8&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 绘制持仓图表&#xA;# dev.new()&#xA;chart.Posn(Portfolio=portfolio1.st, Symbol=symb1)  # 股票 1 的持仓图&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/04/10/pair-trade/index_files/figure-html/pairTrade-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于 RSI 指标的双向交易策略：实现与回测分析</title>
      <link>http://gewutang.cn/2015/03/15/relative-strength-index-rsi-strategy/</link>
      <pubDate>Sun, 15 Mar 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/03/15/relative-strength-index-rsi-strategy/</guid>
      <description>&lt;p&gt;在量化交易领域，相对强弱指数（RSI）作为一种经典的技术指标，常被用于识别市场的超买与超卖状态，为交易决策提供参考。&lt;/p&gt;&#xA;&lt;p&gt;本文所解析的 R 语言代码，便围绕 RSI 指标构建了一套完整的双向交易策略，并通过系统化的回测流程，验证其在多个行业 ETF 上的表现。&lt;/p&gt;&#xA;&lt;p&gt;该策略的核心设计思路，是利用 RSI 指标的阈值穿越信号触发交易动作。&lt;/p&gt;&#xA;&lt;p&gt;首先，代码通过 TTR 包中的 RSI 函数计算价格数据的相对强弱指数，默认采用 2 日周期（相较于常用的 14 日周期，更敏感地捕捉短期市场波动）。在此基础上，策略设定了两个关键阈值：当 RSI 上穿 70 时，判定市场进入超买状态，生成做空信号；当 RSI 下穿 30 时，判定市场进入超卖状态，生成做多信号。为避免信号频繁触发，代码通过 “cross=TRUE” 参数确保，只有当 RSI 真正穿越阈值时才产生有效信号。&lt;/p&gt;&#xA;&lt;p&gt;基于上述信号，策略进一步定义了四套交易规则：RSI 上穿 70 时卖出 1000 股（做空入场），RSI 下穿 30 时平掉全部空头仓位（做空离场）；RSI 下穿 30 时买入 1000 股（做多入场），RSI 上穿 70 时平掉全部多头仓位（做多离场）。&lt;/p&gt;&#xA;&lt;p&gt;这一设计使得策略既能在市场超买时捕捉下跌机会，也能在超卖时把握反弹可能，实现双向交易的灵活性。&lt;/p&gt;&#xA;&lt;p&gt;为确保回测的严谨性，代码构建了完整的回测系统。在环境初始化阶段，通过清理旧策略数据、创建新的策略对象与交易环境，避免残留信息干扰结果。数据准备环节则选取了 9 个 SPDR 行业 ETF 作为交易标的，从雅虎财经获取 1998 年以来的历史价格数据，并完成货币单位与合约属性的设置。&lt;/p&gt;&#xA;&lt;p&gt;风险控制方面，策略为每个品种设定了 ±300 股的持仓限制，既允许做空操作，又通过仓位管控降低过度交易的风险。&#xA;在策略执行阶段，代码通过 applyStrategy 函数运行回测，记录执行时间以评估效率，并通过 updatePortf 函数实时更新组合数据。&lt;/p&gt;&#xA;&lt;p&gt;回测完成后，系统提供了多维度的绩效评估与可视化工具：为每个交易品种生成持仓图表，直观展示交易时机与仓位变化；叠加 RSI 指标曲线，便于观察信号与价格走势的关联；计算各品种收益及组合总收益，并借助 PerformanceAnalytics 包生成综合绩效图表，包括累积收益曲线、回撤分析与月度收益分布等，帮助交易者全面了解策略表现。&lt;/p&gt;</description>
    </item>
    <item>
      <title>技术指标在量化投资中的应用：以RSI指标为例</title>
      <link>http://gewutang.cn/2015/02/09/rsi-cross-strategy-with-signal-analysis/</link>
      <pubDate>Mon, 09 Feb 2015 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2015/02/09/rsi-cross-strategy-with-signal-analysis/</guid>
      <description>&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# ===================== 工作流程说明 =====================&#xA;# 本代码演示如何使用Quantstrat进行信号有效性分析，通过扫描RSI参数评估超卖信号（RSI&amp;lt;30）的未来收益表现&#xA;&#xA;# ===================== 包加载与初始化 =====================&#xA;# 加载所需R包&#xA;require(iterators)       # 迭代器工具（用于参数扫描循环）&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: iterators&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;require(quantstrat)      # 量化策略框架核心包&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;zoo&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &amp;#39;quantmod&amp;#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#xA;## &#xA;##     legend&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;require(gamlss.util)     # 统计分布可视化工具（用于箱线图）&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.util&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.dist&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;gamlss.dist&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:TTR&amp;#39;:&#xA;## &#xA;##     DPO&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: splines&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.data&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &amp;#39;gamlss.data&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:datasets&amp;#39;:&#xA;## &#xA;##     sleep&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: nlme&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: parallel&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  **********   GAMLSS Version 5.4-22  **********&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## For more on GAMLSS look at https://www.gamlss.com/&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Type gamlssNews() to see new features/changes/bug fixes.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 清理策略环境残留数据（防止旧数据干扰）&#xA;suppressWarnings(rm(&amp;quot;order_book.RSI&amp;quot;, pos = .strategy))    # 删除策略订单记录&#xA;suppressWarnings(rm(&amp;quot;account.RSI&amp;quot;, &amp;quot;portfolio.RSI&amp;quot;, pos = .blotter)) # 删除账户组合记录&#xA;suppressWarnings(rm(&amp;quot;account.st&amp;quot;, &amp;quot;portfolio.st&amp;quot;, &amp;quot;stock.str&amp;quot;, &amp;quot;stratRSI&amp;quot;, &amp;quot;startDate&amp;quot;, &amp;quot;initEq&amp;quot;, &amp;#39;start_t&amp;#39;, &amp;#39;end_t&amp;#39;)) # 清除临时变量&#xA;&#xA;# ===================== 参数设置 =====================&#xA;n = 2  # RSI指标的默认计算周期（日）&#xA;&#xA;# ===================== 数据准备阶段 =====================&#xA;# 设置货币类型和交易标的&#xA;currency(&amp;quot;USD&amp;quot;)  # 设置基础货币为美元&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;currency(&amp;quot;EUR&amp;quot;)  # 其他货币类型（示例用）&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;EUR&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;symbols = c(&amp;quot;SPY&amp;quot;)  # 交易标的为标普500 ETF&#xA;&#xA;# 初始化金融工具并下载历史数据&#xA;for(symbol in symbols){&#xA;  stock(symbol, currency = &amp;quot;USD&amp;quot;, multiplier = 1)  # 定义标的属性（货币/乘数）&#xA;  getSymbols(symbol, src = &amp;#39;yahoo&amp;#39;)  # 从雅虎财经下载OHLC数据&#xA;}&#xA;&#xA;# ===================== 账户/组合/策略初始化 =====================&#xA;stratRSI &amp;lt;- strategy(&amp;quot;RSI&amp;quot;)  # 创建策略对象&#xA;&#xA;# 回测参数设置&#xA;startDate = &amp;#39;1997-12-31&amp;#39;  # 回测起始日期&#xA;initEq = 100000           # 初始资金10万美元&#xA;port.st &amp;lt;- &amp;#39;RSI&amp;#39;          # 组合命名（便于参数调整后重复运行）&#xA;&#xA;# 初始化组合、账户和订单系统&#xA;initPortf(port.st, symbols = symbols)  # 创建投资组合对象&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initAcct(port.st, portfolios = port.st, initEq = initEq)  # 创建账户对象&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;RSI&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initOrders(portfolio = port.st)        # 初始化订单簿&#xA;&#xA;# 设置头寸限制规则（风险控制）&#xA;for(symbol in symbols){&#xA;  # 参数说明：组合名称, 标的, 生效日, 最大持仓量, 最大交易次数&#xA;  addPosLimit(port.st, symbol, startDate, 300, 3) &#xA;}&#xA;&#xA;# ===================== 技术指标配置 =====================&#xA;# 添加RSI指标到策略中&#xA;stratRSI &amp;lt;- add.indicator(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;RSI&amp;quot;,  # 使用TTR包的RSI函数&#xA;  arguments = list(&#xA;    price = quote(getPrice(mktdata)),  # 使用收盘价计算&#xA;    n = n                              # RSI计算周期&#xA;  ), &#xA;  label = &amp;quot;RSI&amp;quot;  # 指标标签（用于后续信号引用）&#xA;)&#xA;&#xA;# ===================== 交易信号定义 =====================&#xA;# 信号1：RSI上穿70（超买信号）&#xA;stratRSI &amp;lt;- add.signal(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;sigThreshold&amp;quot;,    # 阈值型信号生成器&#xA;  arguments = list(&#xA;    threshold = 70,         # 阈值水平&#xA;    column = &amp;quot;RSI&amp;quot;,         # 作用于RSI指标列&#xA;    relationship = &amp;quot;gt&amp;quot;,    # 大于关系（greater than）&#xA;    cross = TRUE            # 需要穿越阈值（避免持续触发）&#xA;  ),&#xA;  label = &amp;quot;RSI.gt.70&amp;quot;      # 信号标签&#xA;)&#xA;&#xA;# 信号2：RSI下穿30（超卖信号）&#xA;stratRSI &amp;lt;- add.signal(&#xA;  strategy = stratRSI, &#xA;  name = &amp;quot;sigThreshold&amp;quot;,&#xA;  arguments = list(&#xA;    threshold = 30,         # 阈值水平  &#xA;    column = &amp;quot;RSI&amp;quot;,&#xA;    relationship = &amp;quot;lt&amp;quot;,    # 小于关系（less than）&#xA;    cross = TRUE&#xA;  ),&#xA;  label = &amp;quot;RSI.lt.30&amp;quot;      # 信号标签&#xA;)&#xA;&#xA;# ===================== 信号分析模块 =====================&#xA;signal.label = &amp;#39;RSI.lt.30&amp;#39;  # 选择分析的信号（此处分析超卖信号）&#xA;&#xA;.n = seq(2, 10, 1)  # 创建RSI参数的扫描范围（2到10日，步长1）&#xA;&#xA;# 将参数扫描配置添加到策略对象&#xA;strategy.st &amp;lt;- add.distribution(&#xA;  stratRSI,&#xA;  paramset.label = &amp;#39;RSI&amp;#39;,        # 参数集名称&#xA;  component.type = &amp;#39;indicator&amp;#39;,  # 组件类型（指标）&#xA;  component.label = &amp;#39;RSI&amp;#39;,       # 对应指标标签&#xA;  variable = list(n = .n),       # 要扫描的参数变量（n值）&#xA;  label = &amp;#39;nRSI&amp;#39;                 # 参数集标签&#xA;)&#xA;&#xA;# ===================== 执行参数研究 =====================&#xA;results &amp;lt;- apply.paramset.signal.analysis(&#xA;  strategy.st,                # 策略对象&#xA;  paramset.label = &amp;#39;RSI&amp;#39;,     # 参数集标签&#xA;  port.st,                    # 组合名称&#xA;  sigcol = signal.label,      # 分析的目标信号列&#xA;  sigval = 1,                 # 信号触发值（1=激活状态）&#xA;  on = NULL,                  # 分析频率（NULL表示事件驱动）&#xA;  forward.days = 10,          # 向前分析天数（信号触发后10日）&#xA;  cum.sum = TRUE,             # 计算累计收益&#xA;  include.day.of.signal = F,  # 排除信号触发当日&#xA;  obj.fun = signal.obj.slope, # 目标函数（收益曲线斜率）&#xA;  decreasing = T,             # 结果降序排列&#xA;  mktdata = NULL,             # 使用默认市场数据&#xA;  verbose = TRUE              # 显示详细运行信息&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set:  2 &#xA;## Applying Parameter Set:  3 &#xA;## Applying Parameter Set:  4 &#xA;## Applying Parameter Set:  5 &#xA;## Applying Parameter Set:  6 &#xA;## Applying Parameter Set:  7 &#xA;## Applying Parameter Set:  8 &#xA;## Applying Parameter Set:  9 &#xA;## Applying Parameter Set:  10&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# ===================== 可视化分析结果 =====================&#xA;# 绘制不同参数收益柱状图&#xA;signal.plot(&#xA;  results$sigret.by.asset$SPY,  # 选择SPY的信号收益数据&#xA;  rows = 2,                     # 图形分2行显示&#xA;  columns = 5                   # 每行5列（共显示10个参数组合）&#xA;)&#xA;&#xA;# 绘制参数集2的收益分布箱线图&#xA;distributional.boxplot(&#xA;  results$sigret.by.asset$SPY$paramset.2  # 指定参数集数据&#xA;)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 41038.77 &#xA;## GAMLSS-RS iteration 2: Global Deviance = 41038.77&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## gamlss package currently doesnt &#xA;##                             support encapsulation of their &#xA;##                             plotting function. Pending Patch.&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/02/09/rsi-cross-strategy-with-signal-analysis/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>技术指标在量化投资中的应用：以SMA指标为例</title>
      <link>http://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>http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/</guid>
      <description>&lt;h2 id=&#34;引言&#34;&gt;引言&lt;/h2&gt;&#xA;&lt;p&gt;在量化投资领域，技术指标是构建自动化交易策略的核心工具。通过对历史价格、成交量等数据的数学建模，技术指标能够将市场行为转化为可量化的信号，为程序化交易提供决策依据。简单移动平均线（Simple Moving Average,SMA）作为最经典的技术指标之一，凭借其简洁的逻辑和广泛适用性，在量化策略开发中占据重要地位。&lt;/p&gt;&#xA;&lt;h3 id=&#34;技术指标的核心作用&#34;&gt;技术指标的核心作用&lt;/h3&gt;&#xA;&lt;p&gt;技术指标通过三种典型方式驱动量化交易：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;趋势识别：MACD、布林带等指标可捕捉价格方向性变动；&lt;/li&gt;&#xA;&lt;li&gt;超买超卖判断：RSI、KDJ等振荡器可识别市场极端状态；&lt;/li&gt;&#xA;&lt;li&gt;波动率测量：ATR、标准差指标可量化市场风险强度。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;这些指标通过参数优化、多周期组合、跨指标协同等方式构建数学模型，在沪深300股指期货、加密货币等不同市场展现出稳定收益特征。研究表明，技术指标驱动的策略在趋势性市场中夏普比率可达1.5以上。&lt;/p&gt;&#xA;&lt;h3 id=&#34;sma指标的量化应用&#34;&gt;SMA指标的量化应用&lt;/h3&gt;&#xA;&lt;p&gt;SMA的计算公式为：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;SMA_{t} = \frac{1}{n}\sum_{i=1}^{n} P_{t-i+1} \&#xA;=\frac{P_t+P_{t-1}+\cdots+P_{t-n+1}}{n}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其在量化策略中的典型应用包括：&lt;/p&gt;&#xA;&lt;h4 id=&#34;趋势跟踪策略&#34;&gt;趋势跟踪策略&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;双均线系统：结合5日SMA与20日SMA，当短周期线上穿长周期线时生成买入信号（黄金交叉），反之为卖出信号（死亡交叉）。回测显示该策略在A股市场年化收益可达12%-18%[2]。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;价格偏离策略：当现价突破100日SMA时视为牛市启动，跌破则触发止损。该策略在美股ETF轮动中成功降低28%的最大回撤[3]。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;风险控制模块&#34;&gt;风险控制模块&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;动态止盈：当持仓盈利超过200日SMA的3倍标准差时自动减仓；&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;波动过滤：仅在价格位于50日SMA上方时允许开多单，规避下行趋势风险。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;参数优化实践&#34;&gt;参数优化实践&lt;/h4&gt;&#xA;&lt;p&gt;通过遗传算法对SMA周期进行动态调整，发现：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;股票市场最优参数集中在20-60日&lt;/li&gt;&#xA;&lt;li&gt;加密货币市场更适用10-30日短周期&lt;/li&gt;&#xA;&lt;li&gt;参数组合的夏普比率比单一参数提升35%&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;策略实现&#34;&gt;策略实现&lt;/h2&gt;&#xA;&lt;p&gt;我们用R语言实现一个简单的SMA策略。完整代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 基于简单移动平均线的信号评估策略&#xA;###############################################################)############&#xA;&#xA;# 加载包：&#xA;require(devtools)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: devtools&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: usethis&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(iterators)      # 迭代器工具&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: iterators&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(quantstrat)     # 量化策略回测框架&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantstrat&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: quantmod&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: blotter&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: FinancialInstrument&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;PerformanceAnalytics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:graphics&#39;:&#xA;## &#xA;##     legend&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: foreach&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(gamlss.util)    # 统计工具包（用于数据分布分析）&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.util&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.dist&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;gamlss.dist&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:TTR&#39;:&#xA;## &#xA;##     DPO&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: splines&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: gamlss.data&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;gamlss.data&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:datasets&#39;:&#xA;## &#xA;##     sleep&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: nlme&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: parallel&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  **********   GAMLSS Version 5.4-22  **********&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## For more on GAMLSS look at https://www.gamlss.com/&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Type gamlssNews() to see new features/changes/bug fixes.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(reshape2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: reshape2&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(rCharts)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: rCharts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Attaching package: &#39;rCharts&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:base&#39;:&#xA;## &#xA;##     %||%&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(beanplot)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: beanplot&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;###########################################################################&#xA;# 配置时区设置&#xA;ttz&amp;lt;-Sys.getenv(&#39;TZ&#39;)   # 保存当前时区&#xA;Sys.setenv(TZ=&#39;UTC&#39;)    # 设置回测时区为UTC（避免时区问题影响）&#xA;&#xA;# 清理残留数据&#xA;suppressWarnings(rm(&amp;quot;order_book.macross&amp;quot;,pos=.strategy))&#xA;suppressWarnings(rm(&amp;quot;account.macross&amp;quot;,&amp;quot;portfolio.macross&amp;quot;,pos=.blotter))&#xA;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;,&#39;start_t&#39;,&#39;end_t&#39;))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;###########################################################################&#xA;# 数据准备&#xA;startDate=&amp;quot;2000-01-01&amp;quot;                         # 回测起始日期&#xA;stock.str=c(&#39;XLY&#39;,&#39;XLF&#39;,&#39;XLP&#39;,&#39;XLI&#39;,&#39;RTH&#39;,&#39;XLV&#39;,&#39;XLK&#39;,&#39;XLE&#39;,&#39;IYT&#39;) # 股票代码列表&#xA;currency(&#39;USD&#39;)                                # 设置基准货币为美元&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;USD&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;stock(stock.str, currency=&#39;USD&#39;, multiplier=1) # 定义交易品种属性&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 下载雅虎财经数据&#xA;getSymbols(stock.str, from=startDate, src = &#39;yahoo&#39;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&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;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;for (symbol in stock.str) {&#xA;    # 检查对象是否存在&#xA;  if (exists(symbol)) {&#xA;    # 生成文件名&#xA;    file_name &amp;lt;- paste0(symbol, &amp;quot;.rds&amp;quot;)&#xA;    # 保存为RDS文件&#xA;    saveRDS(get(symbol), file = file_name)&#xA;    # 打印保存信息&#xA;    message(&amp;quot;已保存: &amp;quot;, symbol, &amp;quot; -&amp;gt; &amp;quot;, file_name)&#xA;  } else {&#xA;    warning(&amp;quot;对象 &amp;quot;, symbol, &amp;quot; 不存在&amp;quot;)&#xA;  }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLY -&amp;gt; XLY.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLF -&amp;gt; XLF.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLP -&amp;gt; XLP.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLI -&amp;gt; XLI.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: RTH -&amp;gt; RTH.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLV -&amp;gt; XLV.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLK -&amp;gt; XLK.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: XLE -&amp;gt; XLE.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 已保存: IYT -&amp;gt; IYT.rds&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 调整所有股票数据为复权价格&#xA;for(i in stock.str)&#xA;  assign(i, adjustOHLC(get(i), use.Adjusted=TRUE))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;###########################################################################&#xA;# 初始化账户、组合、策略&#xA;initEq=1000000                          # 初始资金100万美元&#xA;portfolio.st=&#39;macross&#39;                  # 组合名称&#xA;account.st=&#39;macross&#39;                    # 账户名称&#xA;# 初始化组合、账户、订单簿&#xA;initPortf(portfolio.st,&#xA;          symbols=stock.str)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;initAcct(account.st,&#xA;         portfolios=portfolio.st, &#xA;         initEq=initEq)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;macross&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;initOrders(portfolio=portfolio.st)&#xA;# 创建策略对象&#xA;strategy.st&amp;lt;- strategy(portfolio.st)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 添加技术指标&#xA;# 添加50日SMA指标&#xA;strategy.st &amp;lt;- add.indicator(strategy = strategy.st, &#xA;                             name = &amp;quot;SMA&amp;quot;, &#xA;                             arguments = list(x=quote(Cl(mktdata)), &#xA;                                              n=50),&#xA;                             label= &amp;quot;ma50&amp;quot; )&#xA;# 添加200日SMA指标&#xA;strategy.st &amp;lt;- add.indicator(strategy = strategy.st, &#xA;                             name = &amp;quot;SMA&amp;quot;, &#xA;                             arguments = list(x=quote(Cl(mktdata)), &#xA;                                              n=200),&#xA;                             label= &amp;quot;ma200&amp;quot;)&#xA;&#xA;# 添加信号规则&#xA;# 当50日均线上穿200日均线时生成信号&#xA;strategy.st &amp;lt;- add.signal(strategy = strategy.st,&#xA;                          name=&amp;quot;sigCrossover&amp;quot;,&#xA;                          arguments = list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;),&#xA;                                           relationship=&amp;quot;gte&amp;quot;),&#xA;                          label=&amp;quot;ma50.gt.ma200&amp;quot;)&#xA;# 当50日均线下穿200日均线时生成信号&#xA;strategy.st &amp;lt;- add.signal(strategy = strategy.st,&#xA;                          name=&amp;quot;sigCrossover&amp;quot;,&#xA;                          arguments =list(columns=c(&amp;quot;ma50&amp;quot;,&amp;quot;ma200&amp;quot;),&#xA;                                          relationship=&amp;quot;lt&amp;quot;), &#xA;                          label=&amp;quot;ma50.lt.ma200&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;###########################################################################&#xA;# 参数优化设置&#xA;# 需要分析的信号列标签&#xA;signal.label = &#39;ma50.gt.ma200&#39;&#xA;&#xA;# # 定义参数范围&#xA;.FastSMA = seq(1,5,1)    # 快速SMA参数范围：1-5日&#xA;.SlowSMA = seq(5,20,5)   # 慢速SMA参数范围：5-20日（步长5）&#xA;&#xA;# 添加快速SMA参数分布&#xA;strategy.st&amp;lt;-add.distribution(strategy.st,&#xA;                              paramset.label = &#39;SMA&#39;,&#xA;                              component.type = &#39;indicator&#39;,&#xA;                              component.label = &#39;ma50&#39;,&#xA;                              variable = list(n = .FastSMA),&#xA;                              label = &#39;nFAST&#39;)&#xA;# 添加慢速SMA参数分布&#xA;strategy.st&amp;lt;-add.distribution(strategy.st,&#xA;                              paramset.label = &#39;SMA&#39;,&#xA;                              component.type = &#39;indicator&#39;,&#xA;                              component.label = &#39;ma200&#39;,&#xA;                              variable = list(n = .SlowSMA),&#xA;                              label = &#39;nSLOW&#39;)&#xA;&#xA;# 添加参数约束：快速SMA周期必须小于慢速SMA&#xA;strategy.st&amp;lt;-add.distribution.constraint(strategy.st,&#xA;                                         paramset.label = &#39;SMA&#39;,&#xA;                                         distribution.label.1 = &#39;nFAST&#39;,&#xA;                                         distribution.label.2 = &#39;nSLOW&#39;,&#xA;                                         operator = &#39;&amp;lt;&#39;,&#xA;                                         label = &#39;SMA&#39;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# # 执行信号分析（日线级别）&#xA;results = apply.paramset.signal.analysis(&#xA;  strategy.st, &#xA;  paramset.label = &#39;SMA&#39;, &#xA;  portfolio.st, &#xA;  sigcol = signal.label,    # 分析的信号列&#xA;  sigval = 1,               # 信号触发阈值&#xA;  on = NULL,                # 分析频率（NULL表示原始数据频率）&#xA;  forward.days = 50,        # 信号后观察50天&#xA;  cum.sum = TRUE,           # 计算累积收益&#xA;  include.day.of.signal = F,# 排除信号当天&#xA;  obj.fun = signal.obj.slope, # 使用斜率作为目标函数&#xA;  decreasing = T            # 按降序排序结果&#xA;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set:  1, 5 &#xA;## Applying Parameter Set:  2, 5 &#xA;## Applying Parameter Set:  3, 5 &#xA;## Applying Parameter Set:  4, 5 &#xA;## Applying Parameter Set:  1, 10 &#xA;## Applying Parameter Set:  2, 10 &#xA;## Applying Parameter Set:  3, 10 &#xA;## Applying Parameter Set:  4, 10 &#xA;## Applying Parameter Set:  5, 10 &#xA;## Applying Parameter Set:  1, 15 &#xA;## Applying Parameter Set:  2, 15 &#xA;## Applying Parameter Set:  3, 15 &#xA;## Applying Parameter Set:  4, 15 &#xA;## Applying Parameter Set:  5, 15 &#xA;## Applying Parameter Set:  1, 20 &#xA;## Applying Parameter Set:  2, 20 &#xA;## Applying Parameter Set:  3, 20 &#xA;## Applying Parameter Set:  4, 20 &#xA;## Applying Parameter Set:  5, 20&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;#------------------------------- 日线级别分析结果 ------------------------------#&#xA;# 绘制IYT标的参数组合(5,20)的收益分布箱线图&#xA;# signal: 信号分析结果数据集&#xA;# x.val: x轴刻度位置，seq(1,50,5)表示从1到50步长5&#xA;# val: 每个盒须图对应的时间窗口长度&#xA;# ylim/xlim: 坐标轴范围&#xA;# mai: 图形边距参数&#xA;&#xA;  distributional.boxplot(&#xA;    signal=results$sigret.by.asset$IYT$paramset.5.20,&#xA;    x.val=seq(1, 50, 5),  # 显示1-50天，每5天一个刻度&#xA;    val=10,               # 每个盒须图代表10天的收益窗口&#xA;    ylim=c(-20, 20),      # Y轴收益范围限制在±20%&#xA;    xlim=c(0, 50),        # X轴范围0-50天&#xA;    mai=c(1,1,0.3,0.5),   # 图形边距设置（下左上右）&#xA;    h=0                   # 水平参考线位置（0轴）&#xA;    )                 &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 39613.69 &#xA;## GAMLSS-RS iteration 2: Global Deviance = 39613.69&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 绘制XLE标的信号的参数组合面板图（5行4列布局）&#xA;&#xA;&#xA;  signal.plot(&#xA;    results$sigret.by.asset$XLE, &#xA;    rows=5,      # 图形行数&#xA;    columns = 4  # 图形列数&#xA;  )&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-9-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 绘制XLE标的的豆状分布图（展示参数组合的密度分布）&#xA;&#xA;&#xA;  beanplot.signals(&#xA;    results$sigret.by.asset$XLE,&#xA;    rows=5,     # 图形行数&#xA;    columns = 4# 图形列数 &#xA;  )&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-10-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 绘制IYT标的参数组合(5,20)的信号路径图&#xA;# signal.path.plot(results$sigret.by.asset$IYT$paramset.5.20)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;#----------------------------- 周线级别前瞻分析 -----------------------------#&#xA;# 执行周线级别信号分析（向前看10天）&#xA;results.w = apply.paramset.signal.analysis(&#xA;  strategy.st,           # 策略对象&#xA;  paramset.label=&#39;SMA&#39;,  # 参数集标签  &#xA;  portfolio.st,          # 组合名称&#xA;  sigcol = signal.label, # 信号列名称&#xA;  sigval = 1,            # 信号触发值&#xA;  on=&#39;weeks&#39;,            # 按周汇总结果&#xA;  forward.days=10,       # 信号后观察10天&#xA;  cum.sum=TRUE,          # 计算累积收益&#xA;  include.day.of.signal=F, # 排除信号当天&#xA;  obj.fun=signal.obj.slope, # 使用斜率评估&#xA;  decreasing=T           # 降序排列结果&#xA;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Applying Parameter Set:  1, 5 &#xA;## Applying Parameter Set:  2, 5 &#xA;## Applying Parameter Set:  3, 5 &#xA;## Applying Parameter Set:  4, 5 &#xA;## Applying Parameter Set:  1, 10 &#xA;## Applying Parameter Set:  2, 10 &#xA;## Applying Parameter Set:  3, 10 &#xA;## Applying Parameter Set:  4, 10 &#xA;## Applying Parameter Set:  5, 10 &#xA;## Applying Parameter Set:  1, 15 &#xA;## Applying Parameter Set:  2, 15 &#xA;## Applying Parameter Set:  3, 15 &#xA;## Applying Parameter Set:  4, 15 &#xA;## Applying Parameter Set:  5, 15 &#xA;## Applying Parameter Set:  1, 20 &#xA;## Applying Parameter Set:  2, 20 &#xA;## Applying Parameter Set:  3, 20 &#xA;## Applying Parameter Set:  4, 20 &#xA;## Applying Parameter Set:  5, 20&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 绘制周线分析箱线图（时间窗口调整为10天）&#xA;distributional.boxplot(signal=results.w$sigret.by.asset$IYT$paramset.5.20,&#xA;                       x.val=seq(1, 10, 2), # 1-10天步长2&#xA;                       val=10,              &#xA;                       ylim=c(-20, 20),&#xA;                       xlim=c(0, 10),&#xA;                       mai=c(1,1,0.3,0.5),&#xA;                       h=0)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## GAMLSS-RS iteration 1: Global Deviance = 8067.487 &#xA;## GAMLSS-RS iteration 2: Global Deviance = 8067.487&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 绘制周线信号分析可视化面板&#xA;signal.plot(results.w$sigret.by.asset$XLE, rows=5, columns = 4)&#xA;&#xA;# 绘制周线豆状分布图&#xA;beanplot.signals(results.w$sigret.by.asset$XLE, rows=5, columns = 4)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-2.png&#34; width=&#34;672&#34; /&gt;&lt;img src=&#34;http://gewutang.cn/2015/01/08/a-simple-moving-average-strategy-for-evaluating-signal/index_files/figure-html/unnamed-chunk-12-3.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>分级基金笔记：复杂分级产品</title>
      <link>http://gewutang.cn/2014/09/14/notes-on-grade-fund-part-one/</link>
      <pubDate>Sun, 14 Sep 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/09/14/notes-on-grade-fund-part-one/</guid>
      <description>&lt;h3 id=&#34;固定收益剩余分成型&#34;&gt;固定收益+剩余分成型&lt;/h3&gt;&#xA;&lt;h4 id=&#34;代表产品&#34;&gt;代表产品&lt;/h4&gt;&#xA;&lt;p&gt;长盛同庆和国泰估值。&lt;/p&gt;&#xA;&lt;h4 id=&#34;产品概况&#34;&gt;产品概况&lt;/h4&gt;&#xA;&lt;p&gt;产品有固定的到期时间，按原定契约到期后一般直接转为LOF基金。长盛同庆目前即将召开持有人大会，商议到期后转为新的分级基金事宜，其目的是为了解决转为LOF可能会有风险错配的问题，如果新议案被通过，投资人依然有机会通过将两类份额都折算成母基金进行申赎。两类份额合并募集，募集后按照一定的比例拆分为稳健类份额和激进类份额。两类份额分别上市交易，不可单独申购赎回，也不含配对转换机制。当母基金净值位于阈值之内时，稳健类份额可以获取固定收益，激进类份额具有高杠杆；当母基金净值超过阈值时，稳健类份额还可以按照约定的分成比例获取一定的超额收益，激进类份额杠杆将有一定降低。&lt;/p&gt;&#xA;&lt;h4 id=&#34;产品特点&#34;&gt;产品特点&lt;/h4&gt;&#xA;&lt;h5 id=&#34;1到期一般可以直接转为lof或分别折算成母基金申赎折价收敛的确定性高&#34;&gt;1.到期一般可以直接转为LOF或分别折算成母基金申赎，折价收敛的确定性高。&lt;/h5&gt;&#xA;&lt;p&gt;产品的两类份额在到期日前只能上市交易而无法赎回，流动性受到限制，因此其在二级市场常常折价，这点与普通封闭式基金类似。但此类产品明确到期后一般可直接转为LOF基金而无需再召开份额持有人大会来讨论转型方案，转型的确定性相对较强。即使近期长盛同庆召开份额持有人大会讨论转型方式，如果提案获得通过，投资者仍然有机会将两类份额折算为母基金进行申赎兑现本金和收益，因此随着到期日的临近，其折价向0值收敛的确定性较强。&lt;/p&gt;&#xA;&lt;h5 id=&#34;2封闭运作可使投资策略及节奏得到更好的贯彻&#34;&gt;2.封闭运作可使投资策略及节奏得到更好的贯彻。&lt;/h5&gt;&#xA;&lt;p&gt;封闭式运作使得投资经理无需考虑投资者的申购赎回引发的资金变化，其投资策略及节奏能得到更好的贯彻，或将为组合带来更高的长期投资回报。&lt;/p&gt;&#xA;&lt;h5 id=&#34;3稳健份额在获取约定收益的同时仍不失分享剩余收益的机会&#34;&gt;3.稳健份额在获取约定收益的同时仍不失分享剩余收益的机会。&lt;/h5&gt;&#xA;&lt;p&gt;对于稳健类份额来说，约定收益采用固定利率模式加上剩余收益分成，当市场利率下降时，其投资价值上升，而在股市大幅上涨时，也能获取一定的超额收益。&lt;/p&gt;&#xA;&lt;h5 id=&#34;4产品不含到点折算条款有可能导致极端高杠杆的出现&#34;&gt;4.产品不含到点折算条款，有可能导致极端高杠杆的出现。&lt;/h5&gt;&#xA;&lt;p&gt;当激进类份额净值大幅下降后，其杠杆将迅速提升，对于希望通过高杠杆搏取反弹收益的投资者来说这时的激进类份额无疑极具诱惑。&lt;/p&gt;&#xA;&lt;h5 id=&#34;5流动性受限二级市场多维持折价状态&#34;&gt;5.流动性受限，二级市场多维持折价状态。&lt;/h5&gt;&#xA;&lt;p&gt;子基金在封闭运作期内无法按净值赎回，只能通过二级市场卖出，由于需要对流动性作出补偿，因此导致其二级市场通常会产生折价。&lt;/p&gt;&#xA;&lt;h5 id=&#34;6转型后赎回份额时存在不确定性&#34;&gt;6.转型后赎回份额时存在不确定性。&lt;/h5&gt;&#xA;&lt;p&gt;封闭期结束后尽管可折算为LOF基金，但契约规定转型后的一定时间后才接受赎回申请，因此投资者需要承担这段时间净值波动的风险。考虑到转型后的LOF仍为股票型基金，对于稳健类份额投资者来说，一方面所持有的折算后份额与其风险偏好不符，另一方面一旦市场出现下跌，很有可能导致其长期累积收益的显著回落。当然基金在到期时会尽量通过降低仓位、缩短封闭时间、再转型为分级基金等方式来解决上述问题。&lt;/p&gt;&#xA;&lt;h5 id=&#34;7稳健类份额可能会出现亏损&#34;&gt;7.稳健类份额可能会出现亏损。&lt;/h5&gt;&#xA;&lt;p&gt;此类产品都无向下到点折算条款，一旦母基金出现大幅亏损，且不足以支付稳健类份额约定收益甚至本金时，由于没有折算条款的保护，稳健类份额投资者可能面临无法实现约定收益或本金亏损的风险。但是这两只产品都是采用主动型管理模式，相对于指数型基金，其在单边下跌和震荡的市场中表现会优于指数型产品。&lt;/p&gt;&#xA;&lt;h5 id=&#34;8主动型管理模式相对不太透明难以让投资者迅速做出买卖决策&#34;&gt;8.主动型管理模式相对不太透明，难以让投资者迅速做出买卖决策。&lt;/h5&gt;&#xA;&lt;p&gt;分级基金由于具有二级市场，因此是不错的右侧交易工具，而主动型产品由于操作及仓位水平的透明性不及复制指数型产品，在市场上涨时会因为无法对其可能的净值涨幅形成稳定预期而降低对投资者的吸引力。&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://i3.sinaimg.cn/cj/2012/0410/U5695P31DT20120410155143.png&#34; alt=&#34;长盛同庆收益分配规则&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;图1: 长盛同庆收益分配规则&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://i3.sinaimg.cn/cj/2012/0410/U5695P31DT20120410155642.png&#34; alt=&#34;国泰估值收益分配规则&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;图2：国泰估值收益分配规则&lt;/p&gt;</description>
    </item>
    <item>
      <title>房价上涨的逻辑</title>
      <link>http://gewutang.cn/2014/07/04/the-history-of-chinas-bloom-of-real-eastate/</link>
      <pubDate>Fri, 04 Jul 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/07/04/the-history-of-chinas-bloom-of-real-eastate/</guid>
      <description>&lt;p&gt;一直想梳理一下中国房价上涨的逻辑，暂时思考了一下几个方面，记在这里：&lt;/p&gt;&#xA;&lt;h2 id=&#34;重回正规的城市化&#34;&gt;重回正规的城市化&lt;/h2&gt;&#xA;&lt;p&gt;改革开放后，邓小平带领中国将重心从阶级斗争转到经济建设上来，中国也得以继续自己的工业化进程。工业化进程中伴生的是城市的扩张以及劳动力从农村向城市的转移，概括来说就是城镇化。城镇化过程中需要大量的地产建设，包括工业地产、商业地产和居住地产。无论中国还是西方，无论资本主义还是社会主义，各国在工业化过程中都经历了城镇化，在城镇化过程中经历了房地产行业的勃兴和扩张，这是人类社会的发展规律，也是房价上涨的基本逻辑支撑。当然，中国有自己的特色所在。&lt;/p&gt;&#xA;&lt;h2 id=&#34;垄断的土地供应制度&#34;&gt;垄断的土地供应制度&lt;/h2&gt;&#xA;&lt;p&gt;建房是需要土地的，而且土地成本占据了建房成本的很大一部分。但这些土地是哪些人来提供呢？当然，是土地所有者。在西方，土地是私产，政府和个人都能拥有土地，但个人的土地更多一些，因此，土地主要是由个人提供的。中国，由于历史原因，土地归国家或者集体所有，其中集体的土地不能自由买卖。也就说，能提供土地的只有国家。而政府是国有土地的行权人。这就导致中国的土地供应事实上是垄断型的。这为后面的所有事情打下了伏笔。&lt;/p&gt;&#xA;&lt;h2 id=&#34;住房货币化改革&#34;&gt;住房货币化改革&lt;/h2&gt;&#xA;&lt;p&gt;1998年7月3日颁布的23号文是中国住房制度改革的起点。从那时开始中国开始城市开始告别住房分配制度，进入住房市场化时代。也拉开了住房价格有市场供给决定的大幕。&lt;/p&gt;&#xA;&lt;h2 id=&#34;财税体制改革&#34;&gt;财税体制改革&lt;/h2&gt;&#xA;&lt;p&gt;1994年的财税改革，其中一个重要的影响是把地方很多财政来源划归了中央，从而导致地方财政来源减少。与土地的垄断供应制度相结合，则地方政府很容易走上靠卖地赚钱的思路。&lt;/p&gt;&#xA;&lt;h2 id=&#34;过度宽松的货币政策&#34;&gt;过度宽松的货币政策&lt;/h2&gt;&#xA;&lt;p&gt;中国过度宽松的货币政策导致过多的货币追逐商品和服务。从而，带动物价的整体上涨。由于通货膨胀并不是一个均匀的过程，而是像投石入水一样，一点点扩散开来，并集中到少数几个领域。通胀的产生，催生了财富保值需求。房地产则以自身的属性称为一个绝佳的投资保值商品。于是，境内外各种资金纷纷追逐中国的房地产。此时，中国的房地产在需求端由城镇化过程中的自住房需求叠加可自我增强型的投资保值需求组成；供给端则由垄断性供应加市场化购地、建房构成。由于住房的投资性需求不满足普通商品的需求曲线形态，它是随价格的上涨而上涨的，从而，其严重导致了房价的高企。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R时间序列笔记：zoo包</title>
      <link>http://gewutang.cn/2014/06/04/notes-on-zoo/</link>
      <pubDate>Wed, 04 Jun 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/06/04/notes-on-zoo/</guid>
      <description>&lt;p&gt;R软件是处理时间序列的利器。之所以这么说主要是基于两点：一是R软件中的时间序列模型异常丰富，已经将从计量经济学的良心arma到garch族模型到VAR模型以及时间序列结构变异点诊断等一网打尽；二是R软件中的时间序列对象的类型也足够丰富，可以让我们随心所欲地处理规则时间序列和不规则时间序列，以及让不规则时间序列拥有规则的时间属性。以上种种方便都让我们在处理经济和金融时间序列时更加得心应手。&lt;/p&gt;&#xA;&lt;p&gt;目前，R中已有的时间序列相关包如下。&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;时序类型&lt;/td&gt;&lt;td&gt;对象类型&lt;/td&gt;&lt;td&gt;索引类型&lt;/td&gt;&lt;td&gt;R包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;规则时间序列&lt;/td&gt;&lt;td&gt;ts&lt;/td&gt;&lt;td&gt;Date&lt;/td&gt;&lt;td&gt;stat包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;不规则时间序列&lt;/td&gt;&lt;td&gt;timeSeries&lt;/td&gt;&lt;td&gt;timeDate&lt;/td&gt;&lt;td&gt;timeSeries(fSeries)&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;不规则时间序列&lt;/td&gt;&lt;td&gt;its&lt;/td&gt;&lt;td&gt;POSIXct&lt;/td&gt;&lt;td&gt;its包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;不规则时间序列&lt;/td&gt;&lt;td&gt;irts&lt;/td&gt;&lt;td&gt;POSIXct&lt;/td&gt;&lt;td&gt;tseries包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;各种&lt;/td&gt;&lt;td&gt;zoo&lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt;zoo包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA;&lt;td&gt;各种&lt;/td&gt;&lt;td&gt;xts&lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt;xts包&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;第一个是stat包。RCore们在stat包中定义了R语言中最古典的时间序列对象类型，即ts类型。想必用R处理过时间序列的同学们都不陌生，这货的用法很简单，下面是从帮助文档中摘的例子：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(graphics)&#xA;## Generate a timeseries as a sample&#xA;ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##      Qtr1 Qtr2 Qtr3 Qtr4&#xA;## 1959         1    2    3&#xA;## 1960    4    5    6    7&#xA;## 1961    8    9   10&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;## Using July 1954 as start date:&#xA;gnp &amp;lt;- ts(cumsum(1 + round(rnorm(100), 2)),&#xA;          start = c(1954, 7), frequency = 12)&#xA;plot(gnp) # using &#39;plot.ts&#39; for time-series plot&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2014/06/04/notes-on-zoo/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;acf(gnp)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2014/06/04/notes-on-zoo/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;pacf(gnp)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2014/06/04/notes-on-zoo/index_files/figure-html/unnamed-chunk-1-3.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;ts类型的对象的缺点显而易见：只适宜处理规则时间序列、没有独立的索引属性等等。基于种种缺点，能人纷们纷出手开发了另外几个R包，包括专门处理不规则时间序列的its包，能同时处理规则时间序列和不规则时间序列的tseries包，能从网络获取金融数据并具有独立时间属性的tseries包，还有本文标题中提到的zoo包。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R数据库交互系列笔记：DBI包</title>
      <link>http://gewutang.cn/2014/05/19/notes-on-dbi/</link>
      <pubDate>Mon, 19 May 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/05/19/notes-on-dbi/</guid>
      <description>&lt;p&gt;DBI是DataBase Interface的简称，即数据库界面。这篇笔记的主要内容是讲一讲如何用R中的DBI包来与常见数据库进行交互。&lt;/p&gt;&#xA;&lt;h3 id=&#34;1-简介&#34;&gt;1 简介&lt;/h3&gt;&#xA;&lt;p&gt;DBI定义了一个与RDBMS类型数据库进行通信的泛型接口，对于任意一个RDBMS，DBI都会有一个兼容的数据库驱动来实现对接。目前，其可用的驱动包括Oracle、MySQL和SQLite等。&lt;/p&gt;&#xA;&lt;p&gt;理论上说，DBI的泛型接口允许我们同样的代码来跟不同的RDBMS进行通信。但实际应用的时候，还要看具体的RDBMS与SQL标准的是否完全相似，以及数据库开发者开发过程中是否避免使用RDBMS特有的一些功能。&lt;/p&gt;&#xA;&lt;p&gt;下面以SQLite数据库为例说明一下。例子中用的是Affymetrix hgu95av2的微阵列芯片数据，用之前先加载相关的包。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(tools)&#xA;library(&amp;quot;RBioinf&amp;quot;)&#xA;library(&amp;quot;DBI&amp;quot;)&#xA;library(&amp;quot;RSQLite&amp;quot;)&#xA;dbPath &amp;lt;- &amp;quot;hgu95av2-sqlite.db&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h3 id=&#34;2-dbi连接sqlite数据库&#34;&gt;2 DBI连接SQLite数据库&lt;/h3&gt;&#xA;&lt;p&gt;DBI连接RDBMS需要两步。第一步，用dbDriver函数创建一个适合的数据库驱动；第二步，用dbConnect函数创建数据库连接。具体到SQLite，只需要指定包含数据库的文件夹路径即可。而对于MySQL或者Oracle，则需要指定主机地址、用户名和密码等信息。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;drv &amp;lt;- dbDriver(&amp;quot;SQLite&amp;quot;)&#xA;db &amp;lt;- dbConnect(drv, dbPath)&#xA;db&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;dbDisconnect函数可以断开连接。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;dbDisconnect(db)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h3 id=&#34;3-查询数据库结构&#34;&gt;3 查询数据库结构&lt;/h3&gt;&#xA;&lt;p&gt;可以用dbListTables和dbListFields函数来列出数据库中的表名以及各表对应的字段（列名）。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;db &amp;lt;- dbConnect(drv, dbPath)&#xA;dbListTables(db)&#xA;dbListFields(db, &amp;quot;go_probe&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h3 id=&#34;4-查询语句&#34;&gt;4 查询语句&lt;/h3&gt;&#xA;&lt;p&gt;dbSendQuery函数可以执行任意SQL语句。返回的结果是DBI结果的子类，这个子类的结果可以用fetch函数来查询。为了节约内存，用不到的数据库查询对象可以用dbClearResult函数进行清除。&lt;/p&gt;&#xA;&lt;p&gt;fetch函数可以将查询结果转为数据框。dbColumnInfo、dbGetRowsAffected和dbGetRowCount函数可以返回更多关于查询的信息。下面分别演示。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;sql &amp;lt;- &amp;quot;select affy_id, evi from go_probe&amp;quot;&#xA;rs &amp;lt;- dbSendQuery(db, sql)&#xA;rs&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;DBIResult实例包含了查询的相关信息。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;dbColumnInfo(rs)&#xA;dbGetStatement(rs)&#xA;dbGetRowsAffected(rs)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;具体的结果可用fetch函数查看。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;chunk &amp;lt;- fetch(rs, n = 5)&#xA;chunk&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;dbGetRowCount函数范围所有查询结果的行数。&lt;/p&gt;</description>
    </item>
    <item>
      <title>融资融券业务的会计处理</title>
      <link>http://gewutang.cn/2014/05/18/securities-margin-trading/</link>
      <pubDate>Sun, 18 May 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/05/18/securities-margin-trading/</guid>
      <description>&lt;p&gt;融资融券(securities margin trading)又称“证券信用交易”，是指投资者向具有上海证券交易所或深圳证券交易所会员资格的证券公司提供担保物，借入资金买入本所上市证券或借入本所上市证券并卖出的行为。从财务处理角度来看，这个行业的财务有其独有的特点，也就是说，在会计处理过程中，应有一套会计处理的思想。&lt;/p&gt;&#xA;&lt;h4 id=&#34;1融资融券业务的会计处理思路&#34;&gt;1.融资融券业务的会计处理思路&lt;/h4&gt;&#xA;&lt;p&gt;对于融资业务的会计处理，证券公司将资金借给客户，形成一项应收债权，与银行贷款业务并无本质区别。因此，证券公司和客户根据银行贷款的业务做相似的会计处理即可。而对于融券业务的会计处理，证券公司由于仅是暂时性借出证券，在约定的期限之后，客户将归还证券公司相同数量和种类的证券，证券公司并没有真正转移借出证券的风险和报酬，因此，不应该终止确认该证券，但是为了反映该证券已被借出的事实，可以将证券的“自营账户”金额转移到“融券账户”，“融券账户”则按照以前相同的会计计量方法核算。对客户而言，因借入证券应确认“交易性金融资产”，同时因为承担金融负债的目的，主要是为了近期内回购，因此，在出售该证券期间可以相对应地确认“交易性金融负债”，用以反映企业因证券价格浮动而带来负债金额的变动。&lt;/p&gt;&#xA;&lt;h4 id=&#34;2案例说明&#34;&gt;2.案例说明&lt;/h4&gt;&#xA;&lt;p&gt;假设2014年10月1日沪市大盘位于3000点，甲企业预期大盘有可能进一步走低，因此，向乙证券公司融券，做空交易。甲企业融券A股票100000股，此时A股票市价10元/股，约定3个月后于2014年12月31日按年利率12％归还给A股票，同时按照股票市值的60％缴纳保证金600000元。假定2010年12月31日A股票市价8元，不考虑相关交易的手续费。&lt;/p&gt;&#xA;&lt;p&gt;10月1日甲企业融券缴纳保证金、实际融券以及出售股票时的会计分录为：&lt;/p&gt;&#xA;&lt;p&gt;借：其他应收款——融券保证金 　　　               600000&#xA;贷：银行存款——自有资金 　　 　　　　        600000&lt;/p&gt;&#xA;&lt;p&gt;借：交易性金融资产——A股票融券成本              1000000&#xA;贷：交易性金融负债——A股票融券本金          1000000&lt;/p&gt;&#xA;&lt;p&gt;借：其他货币资金——融券存款 　　                1000000&#xA;贷：交易性金融资产——A股票融券成本          1000000&lt;/p&gt;&#xA;&lt;p&gt;12月31日，A股票市价跌至8元，公允价值变动＝(10-8)*100000股＝200000元，甲企业会计分录为：&lt;/p&gt;&#xA;&lt;p&gt;借：交易性金融负债——A股票公允价值变动           200000&#xA;贷：公允价值变动损益——A股票                 200000&lt;/p&gt;&#xA;&lt;p&gt;12月31日，甲企业购入100000股A企业股票归还乙证券公司，此时应冲销交易性金融负债科目余额，会计分录如下：&lt;/p&gt;&#xA;&lt;p&gt;借：交易性金融资产——A股票融券成本               800000&#xA;贷：其他货币资金——融券存款 　　　　　       800000&lt;/p&gt;&#xA;&lt;p&gt;借：交易性金融负债——A股票融券本金              1000000&#xA;贷：交易性金融负债——A股票公允价值变动       200000&#xA;交易性金融资产——A股票融券成本           800000&lt;/p&gt;&#xA;&lt;p&gt;通过“交易性金融负债”及其对应的“公允价值变动”会计科目，确认和计量融券业务因证券价格浮动而带来负债金额的变动及其收益，最终甲企业通过融券做空交易获得200000元利得，扣除融券利息费用30000元，净利润170000元。&lt;/p&gt;&#xA;&lt;p&gt;需要注意的是，乙证券公司是逐日盯市，假如12月20日A股票的价格与甲企业预期相反，上升到15.7元，那么3万元融券利息和57万元的负债浮亏将抵消60万元保证金，如果甲企业没有追加保证金，则其证券账户中资金将会被强制平仓，购买A股票以归还乙证券公司的股票，相关会计处理如下：&lt;/p&gt;&#xA;&lt;p&gt;借：公允价值变动损益——A股票（融券）                570000&#xA;贷：交易性金融负债——A股票融券公允价值变动      570000&lt;/p&gt;&#xA;&lt;p&gt;借：交易性金融负债——A股票融券本金 　　　          1000000&#xA;交易性金融负债——A股票融券公允价值变动          570000&#xA;应付利息——A股票（融券） 　　　　　　            30000&#xA;贷：其他应收款——融券保证金 　　　　　　　　    600000&#xA;其他货币资金——融券存款 　　　　　　　     1000000&lt;/p&gt;</description>
    </item>
    <item>
      <title>Rattle:R中的数据挖掘GUI第4章试读</title>
      <link>http://gewutang.cn/2014/04/16/rattle-chap-4/</link>
      <pubDate>Wed, 16 Apr 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/04/16/rattle-chap-4/</guid>
      <description>&lt;h2 id=&#34;第四章-导入数据&#34;&gt;第四章 导入数据&lt;/h2&gt;&#xA;&lt;p&gt;数据具有不同的来源和格式。借助R软件的高扩展性能，Rattle提供了与各种格式数据源的接口。能与使用R软件实属幸运，因为R系统属开源系统，因而，可以非常方便地与其它应用进行协作通信。R语言支持多种数据格式的直接导入。&lt;/p&gt;&#xA;&lt;p&gt;不同软件之间最常用的数据通信格式是逗号分隔文件（CSV）。这些文件的扩展名是.csv。这是一种面向行列的简单文本格式，其中各列以逗号隔开。这类文件可以用来在电子表格、数据库、气象观测站以及其它应用之间进行数据导入和导出。如果对列分隔符稍作改变，比如替换为制表符，就得到通常以txt为扩展名的文本文件。&lt;/p&gt;&#xA;&lt;p&gt;这些简单的数据格式（csv和txt文件）未包括元数据信息。这意味着文件中对文件中所包含的数据的结构进行描述的信息。当其它软件读入此类数据时，需要对此进行假设。&lt;/p&gt;&#xA;&lt;p&gt;其它类型的数据源提供了描述数据的信息，因而，我们的软件读入这类数据时不需要猜测其读入的内容。属性关联文件格式（4.2节）通常以.arff为扩展名，它在csv格式的基础上增加了元数据。&lt;/p&gt;&#xA;&lt;p&gt;从数据库中直接提取的数据通常会提供元数据信息。开放数据库互联（ODBC）标准提供了一个从多种数据库中获取数据的开放式接口，而R软件对此提供了相应的支持。这允许我们与多种数据源包括Microsoft excel,microsoft access ,SQL server Oracle MySQL,Postgre以及SQLite进行直接通信。4.3节会介绍 RODBC。&lt;/p&gt;&#xA;&lt;p&gt;没有必要在 Rattle 中提供在R中载入数据的所有功能。然而，我们可以用底层的R命令字载入数据，并在Rattle中进行检索，这部分内容详见4.4节。&lt;/p&gt;&#xA;&lt;p&gt;R包本身也是提供了丰富的数据集。虽然许多数据集可能跟我们选定的主题无关，但我们可以借助他们来体验R中的数据挖掘过程。通过 rattle 界面的library 按钮或者 data 菜单下的 soource 可以查看到包含在当前库中的数据集列表。这部分内容详见&#xA;4.6节。&lt;/p&gt;&#xA;&lt;p&gt;通过相关机制将数据载入rattle之后，我们需要定义数据集中各个变量的角色，同时，需要设定数据集中各条观测值在数据挖掘过程中的使用方式。这些信息会记录在rattle界面上，而rattle本身也提供了诸多有用的默认设置。&lt;/p&gt;&#xA;&lt;p&gt;选定数据集，并执行 Data 菜单之后，将看到针对数据的文本性描述。&#xA;图4.1显示的是载入 weather.csv 文件之后的Rattle应用界面，weather.csv 是 rattle 包自带的一个样例数据集。打开R软件，载入 rattle包 ，启动 rattle 应用，再点击 Execute 按钮请求加载 weather 数据集即可得到上述结果：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(rattle)&#xA;rattle()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;图4.1 载入 weather.csv 数据集&lt;/p&gt;&#xA;&lt;p&gt;本章将概述各种源数据格式并讨论如何导入它们以进行数据挖掘。此外，还将讨论 Rattle 提供的关于数据挖掘过程中如何使用数据的设定。&lt;/p&gt;&#xA;&lt;h3 id=&#34;41-csv-数据&#34;&gt;4.1 CSV 数据&lt;/h3&gt;&#xA;&lt;p&gt;最简单、最常用的一种数据交换格式是逗号分隔文件（CSV）格式。这种格式的文件已日渐成为在不同软件应用中交换数据的标准格式。其以 CSV 为扩展名，可以在电子表格、数据库包括 LibreOffice ，Calc ,Gnumeric,Microsoft Excel，SAS/Enterprise Miner,Teradata,Netezza 和其它软件应用中导入和导出。基于此，CSV 格式非常适宜用来向 rattle 中导入数据。这种数据文件的缺点是没有包含显式的元数据信息（元数据，又叫数据的数据，包括数据文件中的数据类型是数值型还是分类型等信息）。由于缺失元数据信息，R 软件有时候会搞错数据中某特定列的数据类型。不过，问题不大，在使用R载入数据时，我们可以帮助其识别数据类型信息。&lt;/p&gt;</description>
    </item>
    <item>
      <title>可转债计算细节</title>
      <link>http://gewutang.cn/2014/03/24/notes-on-cb/</link>
      <pubDate>Mon, 24 Mar 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/03/24/notes-on-cb/</guid>
      <description>&lt;p&gt;转债的交易记录以及交割过程比股票要稍显复杂，但是，弄清楚这个过程也很有必要。以2014年3月19号的交易为例。&lt;/p&gt;&#xA;&lt;h2 id=&#34;转债转股是如何完成和计算的&#34;&gt;转债转股是如何完成和计算的&lt;/h2&gt;&#xA;&lt;p&gt;假设我们的投资组合在3月19号早上以125.5的价格买入100张中鼎转债，手续费12.5元。收盘之前执行转股。结算后，投资组合里拥有的中鼎股票是1440股，价格为当时市场价格；与此同时，保证金账户增加现金6.41元。那么，问题来了，这里的1440股和6.41元是怎么来的？&lt;/p&gt;&#xA;&lt;p&gt;如通常所知，可转债的面值一般是100元，中鼎转债也一样。同时，中鼎转债的转股条款中约定的转股价格是6.94元，因此，根据下列公式：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\text{每张转债转股后可得股数=转债面值（100元）/转股价格}&#xA;$$&#xA;可知：&lt;/p&gt;&#xA;&lt;p&gt;每张中鼎可转债可得到的转股股数是：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;100/6.94&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;那么，100张中鼎转债对应的转股股数就是：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;(100/6.94)*100=1440.992&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;在结算的时候，计算结果的整数部分即1440会以中鼎股份股票的形式交割给我们，这就是实际得到的股票数目。不足一股的部分，即0.922乘以转股价格之后以现金形式返给我们，我们得到现金是6.41元。&lt;/p&gt;&#xA;&lt;h2 id=&#34;如何计算转股之后的盈亏平衡价格&#34;&gt;如何计算转股之后的盈亏平衡价格&lt;/h2&gt;&#xA;&lt;p&gt;如果已开通融券业务，在买入转债之后，可以以自己中意的价格 &lt;code&gt;\(p*\)&lt;/code&gt; 卖出可转债,此时交易的利润已经锁定。&lt;/p&gt;&#xA;&lt;p&gt;如果没有开通融券业务，则买入转债之后，转股收益取决于第二天的正股价格。在给定转股价的情况下，转债价格和正股价格是线性关系，对应到上面的例子就是：&lt;/p&gt;&#xA;&lt;p&gt;转债价格=100/6.94*正股价格&lt;/p&gt;&#xA;&lt;p&gt;或者：&lt;/p&gt;&#xA;&lt;p&gt;转债价格=14.41*正股价格&lt;/p&gt;&#xA;&lt;p&gt;于是有，125.5的买入价格对应正股盈亏平衡价格为125.5÷14.41=8.71。也即，如果第二天的股票价格高于8.71，我们以125.5的价格买入的这笔中鼎转债在转股之后就是盈利的。&lt;/p&gt;</description>
    </item>
    <item>
      <title>善用向量化提升R语言的计算性能</title>
      <link>http://gewutang.cn/2014/03/08/use-vectorisation-to-improve-computational-performance/</link>
      <pubDate>Sat, 08 Mar 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/03/08/use-vectorisation-to-improve-computational-performance/</guid>
      <description>&lt;p&gt;用R 语言用的久了，渐渐发现 R 语言的世界里有很多黑魔法。其中一个黑魔法是向量化运算。向量化运算可以说是R语言区别于其它语言的特质之一，善用向量化运算不仅能有效降低代码的冗余度，还能显著提升代码的运算效率。遗憾的是，很多接触过其它编程语言的人未能掌握这一特性使得他们经常诟病R语言运算效率低下。这里给看官们举个例子来体会下。&lt;/p&gt;&#xA;&lt;p&gt;想必大家都遇到过替换向量元素的情况。比如，我们有下面这样一个数据框，维度是1200000×2：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;n = 1200000&#xA;df = data.frame(values = rnorm(n), &#xA;                ID = rep(LETTERS[1:3], each = n/3),&#xA;                stringsAsFactors = FALSE)&#xA;head(df)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##       values ID&#xA;## 1  1.8442348  A&#xA;## 2 -0.5089413  A&#xA;## 3 -0.8743655  A&#xA;## 4  0.7824285  A&#xA;## 5 -1.2115204  A&#xA;## 6 -1.4914611  A&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tail(df)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##               values ID&#xA;## 1199995  1.753660616  C&#xA;## 1199996 -0.007581797  C&#xA;## 1199997 -0.635219385  C&#xA;## 1199998 -1.437210958  C&#xA;## 1199999 -0.101465502  C&#xA;## 1200000  0.152135010  C&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;假设要把&lt;em&gt;df$ID&lt;/em&gt;中的A替换为“A公司”，B和C类似。怎么做呢？一个常用的解决方案是利用if&amp;hellip;for写循环。代码是这样的：&lt;/p&gt;</description>
    </item>
    <item>
      <title>说说风险均摊投资组合的构建方法</title>
      <link>http://gewutang.cn/2014/01/14/construct-pairty-risk-portfolio/</link>
      <pubDate>Tue, 14 Jan 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/01/14/construct-pairty-risk-portfolio/</guid>
      <description>&lt;h3 id=&#34;引子&#34;&gt;引子&lt;/h3&gt;&#xA;&lt;p&gt;构建股票投资组合时，在筛选出备选股票之后，要解决的另外一个问题是确定备选股票在投资组合中的权重。我们可以用一个向量来表示：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;w=\left(w_1,w_2,&amp;hellip;,w_n \right)&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其中， &lt;code&gt;\(\sum_{i=1}^nw_{i}=1,i=1,2,...,n\)&lt;/code&gt; 。&lt;/p&gt;&#xA;&lt;p&gt;显然，权重的可选集是个无限集合。如果想选取其中一个作为最优权重，那得先确定一个判断最优与否的方法。耳熟能详的方法自然是收益-风险法，即如果一个权重能够使投资组合的收益最大、风险最小，那么，它就是好的。如果用标准差来刻画风险的话，就有下面的评价函数:&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;U(R,\sigma)&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其中， &lt;code&gt;\(\frac{\partial U\left(\cdot\right)}{\partial R}\gt 0\)&lt;/code&gt; ， &lt;code&gt;\(\frac{\partial U\left(\cdot\right)}{\partial\sigma}\lt 0\)&lt;/code&gt;。&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;\(U\left(\cdot\right)\)&lt;/code&gt; 最简单的形式如下：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;U_p = aR_{p} + b \sigma _{p}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其中， &lt;code&gt;\(a\gt 0\)&lt;/code&gt; ， &lt;code&gt;\(b\le 0\)&lt;/code&gt; ； &lt;code&gt;\(R_{p}\)&lt;/code&gt; 是组合的收益率， &lt;code&gt;\(\sigma_{p}\)&lt;/code&gt; 是组合的标准差。显然：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;R_{p} = w&amp;rsquo;R&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\sigma_p = w&amp;rsquo;Cw&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其中， &lt;code&gt;\(R=(R_1,R_2,...,R_n)\)&lt;/code&gt; 是组合中各证券的收益率， &lt;code&gt;\(C\)&lt;/code&gt; 是组合中各证券的收益率的协方差矩阵。&lt;/p&gt;&#xA;&lt;p&gt;把 &lt;code&gt;\(R_{p}\)&lt;/code&gt; 和 &lt;code&gt;\(\sigma_p\)&lt;/code&gt; 带入 &lt;code&gt;\(U_p\)&lt;/code&gt; ，解出令 &lt;code&gt;\(U_p\)&lt;/code&gt; 最小的 &lt;code&gt;\(w\)&lt;/code&gt; 即可。&lt;/p&gt;&#xA;&lt;p&gt;当然，也有其它理念。比如下面要说的风险均摊。风险均摊是指选出来这样一个权重，使得其确定的投资组合中各个证券的风险占证券投资总的风险的比例是相同的。有些拗口，直接看下面的公式。&lt;/p&gt;</description>
    </item>
    <item>
      <title>与佛教有关的一点事儿</title>
      <link>http://gewutang.cn/2014/01/01/something-about-buddhism/</link>
      <pubDate>Wed, 01 Jan 2014 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2014/01/01/something-about-buddhism/</guid>
      <description>&lt;p&gt;大概去年这个时候，小姨说她很多同事都相信佛教，很多是虔诚的佛教信徒。虽然没有出家，但严守佛门清规戒律，是通常所说的“居士”。&lt;/p&gt;&#xA;&lt;p&gt;我记起来我在地铁中也见过不少坐在座位上、手持念珠、口中念念有词的妇人。咋见时，我还以为她们被什么邪教仁波切洗脑、以至走火入魔的不幸人儿呢。每次我都会尽量离他们得远一些，免得他们向我传教。等后来见得多了才知道那些无非是笃信佛家的普通人。&lt;/p&gt;&#xA;&lt;p&gt;近年来，身边的朋友也出现不少是近佛之人。比如，李舰、魏太云等。最早还以为他们会看破红尘出家，后来发现自己想多了。毕竟近佛学与遁入空门还有好几条街区的距离。&lt;/p&gt;&#xA;&lt;p&gt;昨日是旧年最后一天，魏太云纠集了一众人去凤凰岭龙泉寺作义工，在朋友圈子里引起小小轰动。说起龙泉寺，它可算是京郊有名的寺院了。然而，该寺非得名于得道高僧，而是得名于高僧之学历和俗世背景。我觉得比较有名的两位分别是龙泉寺信息技术组负责法师贤信和柳智宇。前者毕业于北工大计算机专业、据说是精通 Linux 和 MangoDB 的技术男；后者是毕业于北京大学，曾拿到国际奥林匹克数学竞赛金牌的学界精英。这寺庙的师傅也与众不同，大都是理科出身，最独特的是他们遁入空门后，并未全然不理会世俗诸事，相反，会经常组织活动，共同专研各种 IT 学问。用传统佛教徒的标准来看，真的是“向外用心”了！&lt;/p&gt;&#xA;&lt;p&gt;“佛教是古老的，但佛教徒都是现代的。”学诚方丈如是说。&lt;/p&gt;&#xA;&lt;p&gt;这么看来，佛教也在默默地发生变。，他们对世俗诸事不再统统排斥，而是彼此融解，行世俗事、持菩萨心。可能就因为这样才吸引越来越多的年轻人走向佛门吧。&lt;/p&gt;&#xA;&lt;p&gt;“外儒内佛”，像苏东坡那样，也是我的梦想。来年春天，山花烂漫的时候，争取去龙泉寺拜谒！&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的金融数据接口</title>
      <link>http://gewutang.cn/2013/11/05/financial-data-accessible-from-r/</link>
      <pubDate>Tue, 05 Nov 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/11/05/financial-data-accessible-from-r/</guid>
      <description>&lt;p&gt;借助R软件，我们可以从网站上获取很多经济和金融数据。我整理了一下目前R可以使用的获取金融数据的包，供大家参考。&lt;/p&gt;&#xA;&lt;table&gt;&#xA;  &lt;thead&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;th&gt;数据源&lt;/th&gt;&#xA;          &lt;th&gt;金融工具类型&lt;/th&gt;&#xA;          &lt;th&gt;R包&lt;/th&gt;&#xA;          &lt;th&gt;免费&lt;/th&gt;&#xA;          &lt;th&gt;是否发布在CRAN上&lt;/th&gt;&#xA;          &lt;th&gt;链接&lt;/th&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/thead&gt;&#xA;  &lt;tbody&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;yahoo&lt;/td&gt;&#xA;          &lt;td&gt;所有金融工具，日数据&lt;/td&gt;&#xA;          &lt;td&gt;quantmod&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://www.quantmod.com&#34;&gt;http://www.quantmod.com&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;google&lt;/td&gt;&#xA;          &lt;td&gt;所有金融工具，财务数据，日数据&lt;/td&gt;&#xA;          &lt;td&gt;quantmod&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://www.quantmod.com&#34;&gt;http://www.quantmod.com&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Oanda&lt;/td&gt;&#xA;          &lt;td&gt;外汇、贵金属数据&lt;/td&gt;&#xA;          &lt;td&gt;quantmod&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://www.quantmod.com&#34;&gt;http://www.quantmod.com&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Fred&lt;/td&gt;&#xA;          &lt;td&gt;经济数据&lt;/td&gt;&#xA;          &lt;td&gt;quamod&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://www.quantmod.com&#34;&gt;http://www.quantmod.com&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Quandl&lt;/td&gt;&#xA;          &lt;td&gt;期货数据，日数据&lt;/td&gt;&#xA;          &lt;td&gt;RQuandl&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;http://quandl/help/packages/r&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;TrueFX&lt;/td&gt;&#xA;          &lt;td&gt;外汇高频数据&lt;/td&gt;&#xA;          &lt;td&gt;TFX&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;是&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://rpubs.com/gsee/TFX&#34;&gt;http://rpubs.com/gsee/TFX&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Bloomberg&lt;/td&gt;&#xA;          &lt;td&gt;所有品种、基金、所有频度&lt;/td&gt;&#xA;          &lt;td&gt;Rbbg&lt;/td&gt;&#xA;          &lt;td&gt;否&lt;/td&gt;&#xA;          &lt;td&gt;否&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;http://finddata.org/rbloomberg/&#34;&gt;http://finddata.org/rbloomberg/&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Interactive Broker&lt;/td&gt;&#xA;          &lt;td&gt;所有品种、所有频度&lt;/td&gt;&#xA;          &lt;td&gt;IBrokers&lt;/td&gt;&#xA;          &lt;td&gt;否&lt;/td&gt;&#xA;          &lt;td&gt;yes&lt;/td&gt;&#xA;          &lt;td&gt;&lt;a href=&#34;https://www.interactivebrokers.com&#34;&gt;www.interactivebrokers.com&lt;/a&gt;&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;</description>
    </item>
    <item>
      <title>安装RMySQL</title>
      <link>http://gewutang.cn/2013/09/28/notes-on-rmysql/</link>
      <pubDate>Sat, 28 Sep 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/09/28/notes-on-rmysql/</guid>
      <description>&lt;p&gt;处理金融市场数据的时候会用到MySQL数据库，R提供了一个RMySQL`包作为自身跟MySQL的接口。这个包本身很容易用，唯一不足的一点是在Windows下面安装RMySQL有点麻烦。安装RMySQL时,除了按部就班的安装MySQL、R、RTools、RMySQL等之外还要设置各种path变量以及把某些文件夹在各个路径里来回移动。&lt;/p&gt;&#xA;&lt;p&gt;这篇文章就说一下自己在安装RMySQL过程中总结出来的较优的安装步骤。&lt;/p&gt;&#xA;&lt;h4 id=&#34;下载并安装-mysql&#34;&gt;下载并安装 MySQL&lt;/h4&gt;&#xA;&lt;p&gt;下载并安装 &lt;a href=&#34;http:http://dev.mysql.com/downloads/mysql/&#34;&gt;MySQL&lt;/a&gt; ，这一点大多数人都知道如何操作，只提醒一点看清楚自己的电脑是 32位还是64位.&lt;/p&gt;&#xA;&lt;h4 id=&#34;把mysql添加到path变量&#34;&gt;把MySQL添加到Path变量.&lt;/h4&gt;&#xA;&lt;p&gt;具体做法是找到我的电脑(指图标)，右键，系统属性，打开&amp;quot;高级&amp;quot;选项卡，下面可以瞅见&amp;quot;环境变量&amp;quot;，点进去，上面部分为&amp;quot;用户变量&amp;quot;，下面部分是&amp;quot;系统变量&amp;quot;，在变量列，找到一个叫Path的东西，选中，点编辑，把MySQL的安装路径复制进去即可。&lt;/p&gt;&#xA;&lt;h4 id=&#34;关联mysql和r软件&#34;&gt;关联MySQL和R软件&lt;/h4&gt;&#xA;&lt;p&gt;具体是，打开 C:\Program Files\MySQL\MySQL Server 5.5\lib ，新建 OPT 文件夹，并复制 MYSQLLIB.LIB 到该文件夹内。同时，复制 libmysql.dll 到到\R\R-2.14.0\bin\ (64 bit) 或 \R\R-2.14.0\bin\i386\ (32 bit) 和 C:\Windows\System32 中;接下来,找到 R\3.0.2\etc 文件夹，并新建 Renviron.site 文件,用记事本打开,向其中添加一行:MYSQL_HOME =”C:/Program Files/MySQL/MySQL Server 5.5/” .&lt;/p&gt;&#xA;&lt;h4 id=&#34;下载并安装rtools&#34;&gt;下载并安装&lt;a href=&#34;http://cran.r-project.org/bin/windows/Rtools/&#34;&gt;RTools&lt;/a&gt;&lt;/h4&gt;&#xA;&lt;p&gt;安装过程很简单。要提醒的是要把\Rtools\2.14\bin,\Rtools\3.0.x\MinGW\bin,\Rtools\3.0.x\MinGW64\bin 添加到 path 变量。&lt;/p&gt;&#xA;&lt;h4 id=&#34;下载rmysql源代码&#34;&gt;下载&lt;a href=&#34;http://biostat.mc.vanderbilt.edu/wiki/main/RMySQL/RMySQL_0.8-0.tar.gz&#34;&gt;RMySQL&lt;/a&gt;源代码&lt;/h4&gt;&#xA;&lt;p&gt;略过。&lt;/p&gt;&#xA;&lt;h4 id=&#34;运行代码安装rmysql&#34;&gt;运行代码安装RMySQL&lt;/h4&gt;&#xA;&lt;p&gt;假设源码存放地址为 d://RMySQL_0.8-0.tar.gz ,运行如下命令:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;install.packages(&amp;quot;RMySQL_0.8-0.tar.gz&amp;quot;,repo=NULL,type=&amp;quot;source&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;测试安装是否成功:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(RMySQL) &#xA;drv = dbDriver(&amp;quot;MySQL&amp;quot;) &#xA;con = dbConnect(drv,host=&amp;quot;localhost&amp;quot;,dbname=&amp;quot;test&amp;quot;,user=&amp;quot;root&amp;quot;,pass=&amp;quot;root&amp;quot;) &#xA;album = dbGetQuery(con,statement=&amp;quot;select * from t_master&amp;quot;) &#xA;album&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>R软件中的集合计算操作</title>
      <link>http://gewutang.cn/2013/09/27/notes-on-sets-caculations-in-r/</link>
      <pubDate>Fri, 27 Sep 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/09/27/notes-on-sets-caculations-in-r/</guid>
      <description>&lt;p&gt;在处理数据尤其是在编写函数或编译R包的时候会涉及到集合的运算操作。比如，判断某个元素是否属于某个集合，并用这个判断结果作为 if 结构中的条件语句。&lt;/p&gt;&#xA;&lt;p&gt;在量化投资的编程中更是经常用到集合的运算操作。比如，判断某支股票是否在今日的投资组合里，又或者在已有当前投资组合的情况下如何生成交易单以得到理想投资组合的股票代码，这涉及到差集操作。因此，了解一下R中的集合计算操作是很有必要的。&lt;/p&gt;&#xA;&lt;p&gt;根据小学时候的知识，我们知道集合计算无非包括这么几类：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;两个集合的交集，即两个集合中共有的那些元素&lt;/li&gt;&#xA;&lt;li&gt;两个集合的并集，即两个集合粗暴合并，并剔除重复值后剩余的那些元素&lt;/li&gt;&#xA;&lt;li&gt;两个集合的差集，集合1减去集合1跟集合2的交集，即得到集合1、集合2的差集，换句话说，集合1中包含而集合2未包含的那些元素&lt;/li&gt;&#xA;&lt;li&gt;元素判断，即开篇说的那个问题，某个元素是否属于某个集合&lt;/li&gt;&#xA;&lt;li&gt;集合等价判断，即两个集合是否是同一个集合&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;它们对应的R函数就是下面这些：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;union(x, y)&lt;/li&gt;&#xA;&lt;li&gt;intersect(x, y)&lt;/li&gt;&#xA;&lt;li&gt;setdiff(x, y)&lt;/li&gt;&#xA;&lt;li&gt;is.element(el, set)&lt;/li&gt;&#xA;&lt;li&gt;setequal(x, y)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;本着解释知识不举例子就是耍流氓的精神，我们看看下面的例子:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 随意生成两个向量x和y&#xA;x &amp;lt;- c(sort(sample(1:20, 9)), NA)&#xA;y &amp;lt;- c(sort(sample(3:23, 7)), NA)&#xA;# 查看 x，y 都长什么样子&#xA;x&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  [1]  1  4  6  7  9 11 15 17 19 NA&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;y&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 12 13 14 15 19 20 21 NA&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 计算并集&#xA;union(x, y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  [1]  1  4  6  7  9 11 15 17 19 NA 12 13 14 20 21&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 计算交集&#xA;intersect(x, y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 15 19 NA&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 计算差集，注意：计算差集时，集合的先后顺序是影响结果滴！&#xA;setdiff(x, y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1]  1  4  6  7  9 11 17&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;setdiff(y, x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 12 13 14 20 21&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# x，y是否相等，当然不相等啦....&#xA;setequal(x, y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] FALSE&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 看看并集、交集、差集的关系&#xA;## True for all possible x &amp;amp; y :&#xA;setequal( union(x, y),&#xA;          c(setdiff(x, y), intersect(x, y), setdiff(y, x)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] TRUE&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 元素判断&#xA;is.element(5, x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] FALSE&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;is.element(100, x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] FALSE&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;is.element(x, y) # length 10&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;is.element(y, x) # length  8&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>WDI包：从世界银行获取世界发展指标数据</title>
      <link>http://gewutang.cn/2013/09/09/notes-on-wdi/</link>
      <pubDate>Mon, 09 Sep 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/09/09/notes-on-wdi/</guid>
      <description>&lt;p&gt;在网络上闲逛时偶然发现了WDI包，这个包可以帮助我们搜索、提取及格式化世界银行（World Bank）公布的世界发展指标（World Development Indicators）数据。&lt;/p&gt;&#xA;&lt;p&gt;WDI包包括一个数据对象WDI_data及三个函数 WDIsearch、WDIcache和WDI。其中，WDI是主函数，WDIsearch和WDIcache是辅助函数。&lt;/p&gt;&#xA;&lt;p&gt;WDI_data作为一个本地对象，保存了世界银行截止2012年6月18日所有可获得的指标信息。&lt;/p&gt;&#xA;&lt;p&gt;WDIcache函数作用是更新可获得的WDI的指标列表。从世界银行网站上下载一个WDI指标的列表。返回一个数据框，该数据框可以作为WDIsarch函数的对象。从世界银行网站下载所有的时序信息会非常耗时间。用来对wDI_data对象中的信息进行更新。通过 cache 函数输入到WDIsearch函数中。&lt;/p&gt;&#xA;&lt;p&gt;WDIseach函数的作用是搜索所有WDI序列的名字和信息描述。搜索WDI序列的代码、名字、描述及数据源。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt; WDIsearch(string = &amp;quot;gdp&amp;quot;, field = &amp;quot;name&amp;quot;, short = TRUE,&#xA;    cache = NULL)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;string 搜索关键字&#xA;field 搜索范围&amp;rsquo;indicator&amp;rsquo;, &amp;rsquo;name&amp;rsquo;, &amp;lsquo;description&amp;rsquo;, &amp;lsquo;sourceDatabase&amp;rsquo;, &amp;lsquo;sourceOrganization&amp;rsquo;等。&#xA;short 逻辑变量。TRUE，只返回代码和名字。FALSE返回序列的所有信息。&#xA;cache WDIcache函数生成的数据列表。缺失的话，WDIsearch会在本地的数据列表中进行查询。&lt;/p&gt;&#xA;&lt;p&gt;返回的是满足特定搜索条件的序列的代码、名字、数据源及序列描述。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;WDIsearch(string=&#39;gdp&#39;, field=&#39;name&#39;, cache=NULL)&#xA;WDIsearch(string=&#39;AG.AGR.TRAC.NO&#39;, field=&#39;indicator&#39;, cache=NULL)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;使用世界银行的API获取需要的数据，解析生成的JSON文件并将结果整理为包含国家和年份信息的长格式数据。&lt;/p&gt;</description>
    </item>
    <item>
      <title>add.indicator函数笔记</title>
      <link>http://gewutang.cn/2013/09/01/notes-on-add.indicator/</link>
      <pubDate>Sun, 01 Sep 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/09/01/notes-on-add.indicator/</guid>
      <description>&lt;p&gt;add.indicator函数是quantstrat包中的主函数之一。它的主要功能是生成信号指示器。这种解释方法有点绕口。举个例子说起来更容易明白。假如说，我们想测试这样一个策略：即当某支股票的价格低于5日算术平均时，买入该股票；高于5日算术平均时卖出该股票。那么，在这个测试过程中，我们要用到 SMA(price,n=5) 这个序列（SMA函数的作用是计算算术平均），既然要用到这个序列，那么得首先生成这个序列， add.indicator函数就是干这个的，是用来生成序列的。&lt;/p&gt;&#xA;&lt;p&gt;看看帮助文档中的 add.indicator 函数的参数：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;add.indicator(strategy, name, arguments,&#xA;    parameters = NULL, label = NULL, ..., enabled = TRUE,&#xA;    indexnum = NULL, store = FALSE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;strategy参数是策略名。在quantstrat 包的框架中，所有指示器（indicator）都从属于策略（strategy），因此，使用add.indicator函数时要率先指定策略名；name参数的作用是指定生成指示器所用的R函数，比如，这里我们用到的函数是SMA ，那么，此时的name参数就是SMA；arguments参数也是个从属参数，它的作用是指定name 参数对应的那个函数的参数。好绕口！说人话的话是这样的：SMA函数除了要输入时间序列参数x 外，还得指定做算术平均时候的时间跨度n，而这个n就是在aruments中指定的。label参数的作用是设定指示器的标签，这样在生成指示器序列时，指示器的序列名的后缀会是该标签的内容。该参数默认是 name+ind。其它参数暂时不怎么重要，就不闲扯了，多说无益，上代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;##定义一个策略，名字名为 str_sma &#xA;strategy(&amp;quot;str_sma&amp;quot;, store=TRUE)&#xA;##获取策略的标的，名字为 SPY&#xA;getSymbols(&amp;quot;SPY&amp;quot;, src=&#39;yahoo&#39;)&#xA;##添加指示器&#xA;add.indicator(&#39;str.sma&#39;, &#39;SMA&#39;, arguments=list(x=quote(Ad(SPY)), n=20),label=&amp;quot;sma.20&amp;quot;)&#xA;##查看策略的结构&#xA;str(getStrategy(&#39;str.sma&#39;)$indicators)&#xA;## 执行指示器&#xA;out &amp;lt;- applyIndicators(&#39;str.sma&#39;, SPY)&#xA;## 查看指示器结果&#xA;tail(out)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>ETF的跨市场套利交易</title>
      <link>http://gewutang.cn/2013/08/30/etf/</link>
      <pubDate>Fri, 30 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/30/etf/</guid>
      <description>&lt;p&gt;ETF的跨市场套利交易包括了三个步骤：上证50ETF二级市场买卖、一级市场申购赎回和一篮子股票二级市场的买卖三个交易程序。&lt;/p&gt;&#xA;&lt;p&gt;在套利之前，首先要清楚除可在二级市场买卖之外，上证50ETF也可在一级市场上进行申购赎回。上证50ETF基金的申购、赎回时是用一篮子股票(50只成份股)与上证50ETF基金份额进行互换交易。&lt;/p&gt;&#xA;&lt;p&gt;具体过程如下：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;申购：用一篮子股票从基金公司处换取上证50ETF基金份额。&lt;/li&gt;&#xA;&lt;li&gt;赎回：用上证50ETF基金份额从基金公司处换取一篮子股票，一篮子股票各种股票比例与上证50指数一致。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;上证50ETF基金申购、赎回简称为&amp;quot;50申赎&amp;quot;，代码为&amp;quot;510051&amp;quot;。该代码显示值代表50ETF基金最新基金份额参考净值(即IOPV值)，是根据上证50指数50只成份股市场价格计算出来的，并根据成份股价格变化每15秒更新一次。最小申购赎回单位为100万份基金份额，申购、赎回均以份额申请，其申请提交后不得撤销。申购赎回费为申购或赎回份额的0.5%。&lt;/p&gt;&#xA;&lt;p&gt;跨市套利交易:&amp;ldquo;50申赎&amp;quot;提供了短时间内的上证50ETF基金参考净值，也反映了一篮子股票当时的平均市场价格水平。大额资金投资者可以根据&amp;quot;50申赎&amp;quot;与&amp;quot;50ETF&amp;quot;两者之间的市场价格差进行短线套利交易，价格差大小用折/溢价率表示:&lt;/p&gt;&#xA;&lt;p&gt;折/溢价率＝(50ETF二级市场价格P-申购赎回参考净值IOPV)/(申购赎回参考净值IOPV)×100%&lt;/p&gt;&#xA;&lt;p&gt;折价套利：当ETF二级市场交易价格低于其份额净值，即发生折价交易时,大额资金投资者可以通过在二级市场大量低价买进ETF,然后在一级市场(即向基金公司)按净值赎回(高价卖出)份额,换回一篮子股票,再于二级市场上卖掉股票而实现套利交易。&lt;/p&gt;&#xA;&lt;p&gt;溢价套利:当二级市场ETF交易价格高于其份额净值,即发生溢价交易时,大额资金投资者可以在二级市场买进一篮子股票,再用这些股票于一级市场(即向基金公司)申购ETF(低价买入ETF),再于二级市场上高价卖掉ETF而实现套利交易。&lt;/p&gt;&#xA;&lt;p&gt;能够套利成功并获益的前提是折溢价率高于套利交易费用，一级交易券商的套利成本最低，仅0.3545%,只考虑免除佣金、申购赎回费用之后的其他必要交易费用，此时能够套利的折溢价空间只需30个基点以上，即0.3分的价格折价差；其次是普通交易券商，为0.8545%，在前者基础上增加了0.5%的申购赎回费；而一般投资者套利成本最高，为1.35%，如果能够享受佣金优惠，则可稍低。&lt;/p&gt;&#xA;&lt;p&gt;假如上证50ETF二级市场价格即50ETF显示价格为0.865元，申购赎回基金参考净值IOPV为0.879元(即上证50ETF基金净值估计值)，则投资者进行套利的过程如下：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;二级市场买入50ETF至少100万份,则买入100万份ETF基金所需资金为0.865×1000000×(1+0.25%)＝867162.5元,其中缴纳交易佣金费率为0.25%。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;将买入的100万ETF基金在一级市场上向基金公司进行赎回，得到一篮子股票，其中要向券商缴纳0.5%的申购赎回费，计5000元。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;将得到的一篮子股票于二级市场同步卖出,卖出金额估计为0.879×1000000×(1-0.3%-0.1%-0.1%)＝874605元,其中缴纳0.3%的佣金、0.1%的印花税、0.1%的过户费。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;套利差价收益为:874605-5000-867162.5＝2442.5元。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;在该例折价套利投资过程中，这一系列操作程序可以借助ETF套利软件在短期内迅速完成,可以减少价格变动带来的市场风险，上述收益计算也是在市场价格没有发生变化的假设前提条件下进行的；如果投资者能够获得佣金、申购赎回费用的优惠，则套利收益大大增加。&lt;/p&gt;&#xA;&lt;p&gt;如果瞬时折溢价率偏低，但是投资者认为当日后市上证50指数会上涨，这可以把上述一系列操作过程完成时间延长，等上证50指数(上证50ETF、50成份股)上涨之后，再将手中当日先前已经申购赎回成功的一篮子股票或ETF基金及时卖出，则可获得更高的可靠收益。&lt;/p&gt;</description>
    </item>
    <item>
      <title>使用R和DEoptim求解大规模投资组合</title>
      <link>http://gewutang.cn/2013/08/20/large-scale-portfolio-optimization-with-r-and-deoptim/</link>
      <pubDate>Tue, 20 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/20/large-scale-portfolio-optimization-with-r-and-deoptim/</guid>
      <description>&lt;h3 id=&#34;1楔子&#34;&gt;1.楔子&lt;/h3&gt;&#xA;&lt;p&gt;第四届上海 R 语言会议时，在演讲中提到过一个 R 包：DEoptim 。这是一个用来做优化的包，它实现了几个差分进化算法（ Differential Evolution algorithms ）。差分进化算法是个什么东西呢？从“进化算法”这四个字可以看出它似乎跟生物学有点沾边，事实上，它算是遗传算法（ Genetic algorithms ）的一种。&lt;/p&gt;&#xA;&lt;p&gt;诸位知道，遗传算法总的来说是借鉴了生物学中生物进化（其实，说优化可能更准确）过程，这种思路还是比较巧妙的。差分进化算法也类似，差分进化算法的主要过程是这样的：生成随机种子的群体、对各群体进行杂交、对群体进行变异、筛选优秀的群体作为后代。不断重复这一过程直到群体后代的优秀程度没法得到显著的提高为止。如果把目标函数（ objective function ）看作衡量群体优秀程度的标准、把目标函数的输入参数看作群体，显然，可以用差分遗传算法来对目标函数进行优化。&lt;/p&gt;&#xA;&lt;p&gt;作为一种优化算法，差分进化算法可以应用到很多领域，金融领域的优化问题很多，求解投资组合就是其中一个应用。所以，这篇文章想看一看差分进化算法在求解投资组合方面的表现。选择投资组合的准则有很多，这里我们根据组合的 CVaR 对组合进行优化。具体来说，在各资产对组合贡献的 CVaR 的权重不超过 5% 的条件下，使得组合的 CVaR 的值最小。&lt;/p&gt;&#xA;&lt;p&gt;具体的技术细节可参考 Portfolio Optimization with CVaR budgets 。&lt;/p&gt;&#xA;&lt;p&gt;这里的组合优化对应的是一个非凸优化问题。用传统的局部最优方法，比如， optim 包和 nlminb 包中的 L-BFGS-B 方法和 Nelder-Mead 方法，在求解此类优化问题时，常常收敛到次优解，而非最优解。DEoptim 算法属于随机全局优化算法，相比较而言，它的优化效果会更好。&lt;/p&gt;&#xA;&lt;p&gt;为了是文章更接近现实，下面使用的组合中的证券数目设定为 100 只股票。&lt;/p&gt;&#xA;&lt;h3 id=&#34;2系统环境说明&#34;&gt;2.系统环境说明&lt;/h3&gt;&#xA;&lt;p&gt;文中所有的结果和代码都基于 R 3.0.1 。获取数据用的是 quantmod 包中的 getSymbols 函数。目标函数中的风险度量函数来自 PerformanceAnalytics 包。DEoptim 函数的初始群落则由 PortfolioAnalytics 包中的 random_portfolios 函数生成。&lt;/p&gt;&#xA;&lt;p&gt;DEoptim 函数会反复计算目标函数的值，直到把群落逼近全局最小值。对提高 DEoptim 函数的性能感兴趣的同学，必须集中活力解决目标函数的进化速度。对于纯 R 代码而言，最好的途径是并行。当然，最根本的道路还是用 c或者Fortran 对该算法进行重写。&lt;/p&gt;</description>
    </item>
    <item>
      <title>热天怀旧</title>
      <link>http://gewutang.cn/2013/08/17/about-yibo/</link>
      <pubDate>Sat, 17 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/17/about-yibo/</guid>
      <description>&lt;p&gt;待在家里梳理了一下跟逸波的相识历史，发现还是颇有意思。&lt;/p&gt;&#xA;&lt;p&gt;我跟逸波相识于校内网。当时我在校内网看他写了一篇文章，那还是2011年4月的事情。看完之后很想邀请他到主站来当作者。于是便冒昧地写了留言，没想到很快就得到了逸波的回复。在和他聊天的过程中，我还给他的文章提了挺多意见，后来发现我当时真是无知无畏、关公门前耍大刀了。后来，那篇文章在COS主站发表并迅速蹿红。后来被人更改名字之后又在校内网重新流传，点击量数十万计。&lt;/p&gt;&#xA;&lt;p&gt;2012年4月份，求学路上的珊珊要写一篇关于“微博”影响力的毕业论文，期间需要分析微博上美国大使馆的语料。看她一页页复制、黏贴太过辛苦，我就想着用 R 写一段脚本，自动抓取美国大使馆发布的所有微博内容。无意中，我又搜索到了逸波在 163 网站 上面的博文。博文内容刚好跟我想要做的类似，我顿时心花怒放，毕竟不用重复造轮子了。于是，我和他开始定期的切磋抓取微博的各种方法。过了这么长时间，目前，逸波的 rWeibo 包依然是获取微博信息的最佳途径之一。&lt;/p&gt;&#xA;&lt;p&gt;再之后的接触就是跟翻译相关了。我接触的第一个翻译项目是《R in a nutshell》。着手翻译之后，我的进度稍微有点低于预期，但为了尽快完成就冒昧的邀请智恒和逸波帮忙审校一遍。经过这件事儿我才发现逸波是翻译的好手，翻译得又快又好。后来，出版社的朋友想让找找人帮忙翻译《The R Book》时，我脑海中立即就想到了他。等我邮件问他是否愿意负责整个翻译项目，他很爽快的就答应了。后来他以极快的速度突进了这个翻译项目。我也把自己当作伯乐一般开心。&lt;/p&gt;</description>
    </item>
    <item>
      <title>使用R语言构造投资组合的有效前沿</title>
      <link>http://gewutang.cn/2013/08/09/use-r-to-do-portfolio-optimization/</link>
      <pubDate>Fri, 09 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/09/use-r-to-do-portfolio-optimization/</guid>
      <description>&lt;p&gt;构造投资组合是金融投资分析中历久弥新的问题。多年以来，学界、业界提出诸多对投资组合进行优化的方法。比如，最经典的基于收益率均值和收益率波动性进行组合优化，由于马克维滋提出用收益率方差表示收益率的波动性，所以，这种方法又称为的 M-V 方法，即 &lt;code&gt;Mean-Variance&lt;/code&gt; 方法的缩写；后来，又衍生出基于夏普比率（&lt;code&gt;Sharp Ratio&lt;/code&gt;）的投资组合优化方法；近年来，随着&lt;code&gt;VaR&lt;/code&gt; (&lt;code&gt;Value at Risk&lt;/code&gt;) 和 &lt;code&gt;CVaR&lt;/code&gt; (&lt;code&gt;Conditional Vaule at Risk&lt;/code&gt;) 概念的兴起，基于 &lt;code&gt;VaR&lt;/code&gt; 和 &lt;code&gt;CVaR&lt;/code&gt; 对投资组合进行优化的思路也开始勃兴；除此之外，对冲基金届还有一种非常有生命力的投资组合优化方法，即桥水公司（&lt;code&gt;Bridge-Water&lt;/code&gt;）公司提出的风险均摊方法（ &lt;code&gt;Risk Pairy&lt;/code&gt; ），这种方法的核心思路在于，估计组合中各个资产的风险度及其占组合风险的比率，然后，按照该比例对组合头寸进行分配。&lt;/p&gt;&#xA;&lt;p&gt;几种方法中，在学界和业界最收关注的还是 &lt;code&gt;M-V&lt;/code&gt; 方法。而在 &lt;code&gt;M-V&lt;/code&gt; 方法中最基本的一个知识点，就是构造投资组合的有效前沿。&lt;/p&gt;&#xA;&lt;p&gt;理论这里不再赘述，简单说一下其在 &lt;code&gt;R&lt;/code&gt; 语言中的实现。构造有效前沿的步骤大致可按照获取数据、将数据加工处理为收益率矩阵、以收益率矩阵为输入计算得到有效前沿这三个步骤来完成。下面分布来说一说。&lt;/p&gt;&#xA;&lt;p&gt;第一步，获取数据。最简单的方法是使用 &lt;code&gt;quantmod&lt;/code&gt; 中的 &lt;code&gt;getSymbols&lt;/code&gt; 函数。因为要要做的事是构建资产组合，因此，得同时获取多只股票的交易数据，这里取 QQQ/SPY/YHOO 三只股票为例。需要运行的代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 载入 quatnmod 包&#xA;require(quantmod) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：quantmod&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 下载 QQQ/SPY/TSLA 交易数据&#xA;getSymbols(c(&#39;QQQ&#39;,&#39;SPY&#39;,&#39;TSLA&#39;)) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;QQQ&amp;quot;  &amp;quot;SPY&amp;quot;  &amp;quot;TSLA&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;第二步，将交易数据处理为收益率数据。这一步可以用 dailyReturn 函数来完成。&lt;/p&gt;</description>
    </item>
    <item>
      <title>孤独的投资者卡洛·坎奈尔</title>
      <link>http://gewutang.cn/2013/08/05/lonely-investor-carlo-cannell/</link>
      <pubDate>Mon, 05 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/05/lonely-investor-carlo-cannell/</guid>
      <description>&lt;p&gt;卡洛·坎奈尔(CarloCannell)是一位名副其实的价值投资者，因拥有一套独特的价值选股方法而闻名。1992年，他在旧金山创建专注于股票多空策略的坎奈尔资产管理公司(CannellCapitalLLC)，成立以来旗舰基金汤加基金(TongaFund)的年均收益率维持在20%左右的水平。&lt;/p&gt;&#xA;&lt;p&gt;诱人的回报率自然吸引了众多投资者，不过坎奈尔竭力将资产管理规模控制在8亿美元的水平。作为小盘股捡股者的他不可能在运营大量资金的情况下仍然维持着超高回报率。过去几年，他先后退还了投资者2.5亿美元资金。&lt;/p&gt;&#xA;&lt;h3 id=&#34;分析师比例指标&#34;&gt;“分析师比例”指标&lt;/h3&gt;&#xA;&lt;p&gt;坎奈尔认为孤独者才能成为更好的基金经理，这一点恰好体现在他关注的对象往往是其他投资者从不关注的领域。巴菲特曾说，理性的人总是比较孤独的，但对于投资者来说，却是最重要的素质。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔与其他价值投资者的另一个共同特质，是认为市场并非有效，因此他宁愿在那些“人迹罕至”的市场角落里“深耕”。&lt;/p&gt;&#xA;&lt;p&gt;“我认为无效率的市场比有效率的市场能够提供更大的潜在回报。由于有大量的公司信息分布不合理，这导致了市场失效。因此，我们要用大量时间去发掘很大程度上被投资市场忽视的投资价值。”坎奈尔这样说道。&lt;/p&gt;&#xA;&lt;p&gt;目前，美国有超过13000家公司必须向美国证监会提交各种形式的报告以及做出信息披露，其中大多数公司都缺乏研究机构的持续跟踪。坎奈尔把“缺乏持续跟踪”定义为只有三个或者更少的卖方分析师定期发布公司的预期收益报告，因此这些公司缺乏常规的信息流动。&lt;/p&gt;&#xA;&lt;p&gt;根据坎奈尔的经验，缺乏信息流动会导致市场估值跟公司的实际内在价值间差异更大。基于这样的理念，坎奈尔创造了一个所谓的“分析师比例”指标。这项指标跟踪上市公司的分析师人数。&lt;/p&gt;&#xA;&lt;p&gt;“我们认为一个公司的研究者越少越好。在我们投资组合中大约有65%的企业几乎不被任何分析师关注。”坎奈尔解释道。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔并不认为根据这项指标筛选个股是最科学的，但的确是筛选最具价值产品的起点。&lt;/p&gt;&#xA;&lt;p&gt;接着，坎奈尔会根据一些传统指标进一步进行筛选。这些指标包括：较低的市净率、较低的账面价值溢价或者折价、市盈率小于股本回报率以及持续产生现金流大于资本支出等。&lt;/p&gt;&#xA;&lt;p&gt;位于美国俄亥俄州利普西克的恩尼斯(Ennis)公司是坎奈尔喜欢的典型小盘股。这家公司是一家有着悠久历史的印刷企业。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔称：“我们认为这家企业有着非常好的管理机制。他们的竞争力非常强，非常自律，高管也十分诚实。此外，只有两个非常小的机构研究员在研究这家公司。这个公司从不开季度电话会议，也很少对产品进行宣传，正是我们喜欢的公司。”&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔从2001年到2006年，一直持有这家公司。在这期间，公司的股价从每股7.5美元一路上涨至26美元。&lt;/p&gt;&#xA;&lt;h3 id=&#34;古怪的投资理念和方法&#34;&gt;“古怪”的投资理念和方法&lt;/h3&gt;&#xA;&lt;p&gt;除了传统的价值投资分析外，坎奈尔还拥有一些“古怪”的投资理念和方法。&lt;/p&gt;&#xA;&lt;p&gt;“投资者其实和放射科医生、机械工程师或者驾驶员一样，通过某种外在的模式来对事物的本质进行判断。而不同水平的人找出外在形态之间关联性的能力不一样。我认为通过这种能力来甄选股票就是我们的优势所在。”坎奈尔这样说道。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔用一个例子说明这种能力的具体表现：“我们在对潜在的卖空对象进行研究的过程中发现，如果一家公司是由非‘六大’会计师事务所进行财务审计，且这家公司的库存比例正不断下降，这时候公司现金流和净收入之间的差距会否扩大与分析师会否研究这家公司有很大关联性。如果分析师对一家公司的研究力度加大，那么这个公司就可能成为我们潜在的做空对象。”&lt;/p&gt;&#xA;&lt;p&gt;2010年初，坎奈尔就指出，他已经开始做空德国的太阳能公司。主要原因是，这个行业得到的政府补贴越来越少，其债务规模也达到了极高水平。&lt;/p&gt;&#xA;&lt;p&gt;此外，坎奈尔还有更为“独到”的研究指标。他指出，预付指标是他非常关注的数据之一，而这个指标经常被投资者忽略。&lt;/p&gt;&#xA;&lt;p&gt;“公司经常说他们的预付资产一般用于保险、广告或进行商战，但这项资金其实经常用于进行行贿。因此这个数据起伏很大，有时会给公司财务带来很大麻烦。如果这个数据伴随的是毛利率下降，那么投资者就需要提高警惕了。因为在下一季的财报中，会计很有可能会加大销售成本，来掩饰他们在预付资金方面的漏洞。”&lt;/p&gt;&#xA;&lt;p&gt;在对公司进行财务分析之后，坎奈尔接下来需要做的，是在“信息分布不合理”的情况下，对买入公司的未来发展进行一个系统评估。&lt;/p&gt;&#xA;&lt;p&gt;“对于这一点，我们的做法是聘用一个曾在CNBC(美国全国广播公司财经频道)工作过的记者，他每天的工作就是逛街，包括塔吉特百货(Targets)和百思买(BestBuys)等等，然后发现市场的细微变化。”坎奈尔这样说道，“此外我们也订阅大量的行业杂志，比如石油化工行业、殡葬行业等等。”&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔近期向SEC提交的13F档案显示，他在截至2013年3月31日的第一季度中，前三大重仓股分别是价值媒体公司(ValuevisionMediaInc)、信息交换解决方案供应商内部链接控股公司(IntraLinksHoldings)，以及多频道电信公司(MultibandCorp)。&lt;/p&gt;&#xA;&lt;p&gt;由于坎奈尔非常看重公司的增长潜能，他也不可避免会通过对管理层施压，来改善股价表现。这个时候的他更像是一名积极的“股东行动主义”投资者。&lt;/p&gt;&#xA;&lt;h3 id=&#34;投资顺应自然法则&#34;&gt;投资顺应自然法则&lt;/h3&gt;&#xA;&lt;p&gt;在投资方法之外，坎奈尔的古怪念头也随处可见。他被其他价值投资人熟知是因为在2009年的价值投资大会时，曾经将灭绝的物种与投资相比较。&lt;/p&gt;&#xA;&lt;p&gt;他指出，史特拉海牛(Hydrodamalisgigas)是一个已经灭绝了的生物。18世纪中叶之前，人类还未穿过白令海峡，这些海牛生活在北太平洋的海岸线上。自从有1500人徒步来到这里后，史特拉海牛被发现，并被人类猎杀，它们在短短27年之后就灭绝了。&lt;/p&gt;&#xA;&lt;p&gt;这个道理可以引申到投资中去。“物种就好比是上市公司。一些公司在大环境变化的过程中，总会经历比别人更长的一段适应期，如果不能适应，那么就会灭绝。比如说，餐饮公司受经济危机的冲击要比一些非周期的行业要大。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔认为，巴菲特在投资过程中总会利用类似大自然的天然法则，从而规避很多不必要的风险。因此，利用自然规律获益远比想尽一切办法找出下一个可口可乐公司要容易得多。&lt;/p&gt;&#xA;&lt;h3 id=&#34;坎奈尔小档案&#34;&gt;坎奈尔小档案&lt;/h3&gt;&#xA;&lt;p&gt;坎奈尔1963年出生在加利福尼亚州，父亲彼得·坎奈尔(PeterB.Cannell)和祖父都是投资者，并各自成立了自己的资产管理公司。在这样的家庭背景影响下，坎奈尔走上投资之路并不意外，但他并非一开始就热爱投资。&lt;/p&gt;&#xA;&lt;p&gt;坎奈尔的大学生涯在普林斯顿大学度过，他一开始选择了古典文学专业，但很快发现自己并不喜欢，于是转向了社会学专业。1986年毕业之后，他只身前往南太平洋的岛国——斐济，成为一名自由职业的记者，此后又先后为两家日本公司服务。&lt;/p&gt;&#xA;&lt;p&gt;1988年，他开始自己创业，开了一家开发设计桌面音乐乐谱的公司，缺乏管理经验的他很快意识到自己需要到商学院深造，于是选择了牛津大学的坦普顿学院(TempletonCollege)，这段求学经历真正促使他走向了投资行业。&lt;/p&gt;&#xA;&lt;p&gt;“我在牛津那段时间投入到投资中，而且是以一种非常学术和理论化的视角来进行投资。”坎奈尔这样说道，“我做了很多关于市场有效论的研究，非常了解这个假说什么时候会失效。在这些研究下，我的投资哲学得以形成，并沿用至今。”&lt;/p&gt;&#xA;&lt;p&gt;1992年，在获得MBA学位后不久，坎奈尔创建了坎奈尔资产管理公司。也正是这一年，他与妻子珍妮弗·布拉德利(JenniferT.Bradley)喜结良缘，婚后他们育有三个子女。&lt;/p&gt;</description>
    </item>
    <item>
      <title>Lee Hachadoorian 加入 Supstat</title>
      <link>http://gewutang.cn/2013/08/02/lee-hachadoorian-joins-supstat/</link>
      <pubDate>Fri, 02 Aug 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/08/02/lee-hachadoorian-joins-supstat/</guid>
      <description>&lt;p&gt;上午收到 vivian 邮件发来的喜讯，她在邮件中说纽约城市大学研究生中心的 Lee Hachadoorian 已经答应加盟 Supstat 了。他是纽约城市大学研究生中心的 Ph.D，同时也担任该校的访问助理教授（Visiting Assistant Professor）。他的研究领域广泛，包括：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;GIS&lt;/li&gt;&#xA;&lt;li&gt;空间分析（Spatial Analysis）&lt;/li&gt;&#xA;&lt;li&gt;城市经济地理学（Urban Economic Geography）&lt;/li&gt;&#xA;&lt;li&gt;人口迁徙（Residential Mobility）&lt;/li&gt;&#xA;&lt;li&gt;区域研究（Regionalism）&lt;/li&gt;&#xA;&lt;li&gt;自由和开源软件（Free and Open Source Software）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;最擅长的空间查询（spatial query）和普查范围研究（cencus tracts study）。以后可以帮助 Supstat 做与地理位置有关的项目或 GIS 项目了。&lt;/p&gt;</description>
    </item>
    <item>
      <title>时空数据可视化</title>
      <link>http://gewutang.cn/2013/07/28/the-visualization-of-spatual-temporal-data/</link>
      <pubDate>Sun, 28 Jul 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/07/28/the-visualization-of-spatual-temporal-data/</guid>
      <description>&lt;p&gt;这几天北京有点热，穿着汗衫都觉得浑身冒烟儿，做啥都没有激情，只能赖在家里或者躲进图书馆和博物馆。没什么紧要的事情都懒得往外面走，但一月一次的“COS数据分析沙龙”是个例外。没有紧要的事儿，都得抽出时间去参加，偶尔还得客串一下主持人。&lt;/p&gt;&#xA;&lt;p&gt;这次沙龙的主题是“时空数据的可视化”，分享嘉宾是来自中科院地理所的王江浩（@沐洲），整个沙龙内容真的令人震撼。&lt;/p&gt;&#xA;&lt;p&gt;我第一次对江浩有印象是在微博上。二〇一三年三月，上海爆发H9N7疫情，之后不久江苏、浙江等省份相继出现疫情。一时间全国上下议论纷纷，专家、民众都担心这次疫情会酿成第二次SARS（非典型性肺炎）。很多人都开始关注H9N7的疫情趋势，就在这个时候，江浩的微博开始贴出H9N7疫情的可视化图，且被大量转发。&lt;/p&gt;&#xA;&lt;p&gt;再不久，雅安发生地震，江浩根据网上抓取的地震相关数据，对雅安周边的地区震级做了相当经典的可视化，从地图上标注了雅安周边地区各个县、乡的地震成度。让人对当地情况一目了然。我也是第一次感觉到了时空数据可视化的重要性。&lt;/p&gt;&#xA;&lt;p&gt;细想之下，何止地震和疫情呢？交通情况也是时空数据可视化中的重点。此外，还有经济学中的时空经济学，也可以研究经济体在时空中的发展变化。我感觉，时空数据可视化对于国家宏观管理意义非凡。&lt;/p&gt;</description>
    </item>
    <item>
      <title>修改chartSeries绘图结果的x轴标签</title>
      <link>http://gewutang.cn/2013/06/26/change-xlabels-in-chartseries/</link>
      <pubDate>Wed, 26 Jun 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/06/26/change-xlabels-in-chartseries/</guid>
      <description>&lt;p&gt;用quantmod包中的chartSeries绘图时，x轴标签会默认为中文汉字，看起来不太美观。想要修改制图结果吧，chartSeries函数中没有提供可设置x.labels的参数。原来chartSeries函数会自动识别系统语言并根据系统语言生成x.labels。这样的话，只要我们把自己的系统语言更改为英语，就应该显示为更为整洁的英文了。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;require(quantmod)&#xA;getSymbols(&amp;quot;AAPL&amp;quot;)&#xA;chartSeries(AAPL,theme=&amp;quot;white&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;Sys.setlocale(&amp;quot;LC_TIME&amp;quot;,&amp;quot;english&amp;quot;)&#xA;chartSeries(AAPL,theme=&amp;quot;white&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;不过，这个好像不怎么重要吧？&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的金融分析包：quantmod学习笔记补充版</title>
      <link>http://gewutang.cn/2013/06/12/notes-on-quantmod/</link>
      <pubDate>Wed, 12 Jun 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/06/12/notes-on-quantmod/</guid>
      <description>&lt;p&gt;很久之前在&lt;a href=&#34;http://cos.name&#34;&gt;统计之都&lt;/a&gt;&lt;a href=&#34;http://cos.name/cn&#34;&gt;论坛&lt;/a&gt; 发了一个&lt;code&gt;quantmod&lt;/code&gt;包的学习笔记。听说后来流传很广泛。因为当时我也是初学&lt;code&gt;quantmod&lt;/code&gt;，所以，该版本笔记中有很多错漏之处，同时笔记的叙述结构也不太合理。一直我都想修订一版出来作为补充。修订的版本现在在&lt;a href=&#34;https://github.com/dengyishuo/Notes/tree/master/quantmod&#34;&gt;这里&lt;/a&gt;可以看到。&lt;/p&gt;&#xA;&lt;p&gt;供大家参考，如果有什么建议和意见，请不吝指正。&lt;/p&gt;</description>
    </item>
    <item>
      <title>安装mgarch包终极解决方案</title>
      <link>http://gewutang.cn/2013/06/10/recipe-about-mgarch/</link>
      <pubDate>Mon, 10 Jun 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/06/10/recipe-about-mgarch/</guid>
      <description>&lt;p&gt;&lt;code&gt;mgarch&lt;/code&gt;包是一个可以拟合和诊断&lt;code&gt;BEKK&lt;/code&gt;模型的&lt;code&gt;R&lt;/code&gt;包。包中的主函数很少，只有&lt;code&gt;mvBEKK.sim&lt;/code&gt;，&lt;code&gt;mvBEKK.diag&lt;/code&gt;，&lt;code&gt;mvBEKK.est&lt;/code&gt;三个。这三个函数各司其职，分别用来模拟&lt;code&gt;BEKK&lt;/code&gt;数据、诊断&lt;code&gt;BEKK&lt;/code&gt;模型、拟合&lt;code&gt;BEKK&lt;/code&gt;模型。照常理说，这样的包很好啦。就只有一样不好，作者自从2009年更新一次之后，再也不提供更新的&lt;code&gt;.zip&lt;/code&gt;包了。很多需要拟合&lt;code&gt;BEKK&lt;/code&gt;模型的同学急得团团转就是不知道怎么安装和使用这个包。这里提供三个方案：&lt;/p&gt;&#xA;&lt;h3 id=&#34;方案一&#34;&gt;方案一：&lt;/h3&gt;&#xA;&lt;p&gt;下载&lt;a href=&#34;http://sourceforge.net/projects/mgarch/?source=directory&#34;&gt;mgarch&lt;/a&gt;的源代码包&lt;code&gt;mgarch_0.00-1.tar.gz&lt;/code&gt;。解压缩到&lt;code&gt;R&lt;/code&gt;的当前工作文件夹中。如果不知道当前工作文件夹路径。敲入&lt;code&gt;get.wd()&lt;/code&gt;函数就可以看到了。解压缩之后，按照&lt;code&gt;mgarch-&amp;gt;&amp;gt;R&lt;/code&gt;的顺序打开文件夹，你会在R文件夹中看到好几个&lt;code&gt;.r&lt;/code&gt;格式的文件。用&lt;code&gt;source()&lt;/code&gt;函数载入进来就可以用了。&lt;/p&gt;&#xA;&lt;h3 id=&#34;方案二&#34;&gt;方案二：&lt;/h3&gt;&#xA;&lt;p&gt;拿着mgarch包，按照&lt;a href=&#34;http://cos.name/2011/05/write-r-packages-like-a-ninja/&#34;&gt;开发R程序包之忍者篇&lt;/a&gt;自己编译&lt;a href=&#34;http://sourceforge.net/projects/mgarch/?source=directory&#34;&gt;mgarch&lt;/a&gt;包。&lt;/p&gt;&#xA;&lt;h3 id=&#34;方案三&#34;&gt;方案三:&lt;/h3&gt;&#xA;&lt;p&gt;这个方案跟方案二类似，也得先安装&lt;code&gt;Rtools&lt;/code&gt;，目的是可以编译源代码。同时，由于&lt;a href=&#34;http://sourceforge.net/projects/mgarch/?source=directory&#34;&gt;mgarch&lt;/a&gt;依赖于&lt;code&gt;zoo&lt;/code&gt;、&lt;code&gt;tseries&lt;/code&gt;、&lt;code&gt;mvnorm&lt;/code&gt;三个支撑包，所以，也得捎带手安装一下，并保证它们被升级了到合适的版本。接着，将&lt;code&gt;mgarch_0.00-1.tar.gz&lt;/code&gt;放到当前工作文件夹中（不知道当前工作文件夹是哪个？看方案一）。最后，敲入下面的代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;install.packages(&amp;quot;mgarch_0.00-1.tar.gz&amp;quot;,repos=NULL,type=&amp;quot;source&amp;quot;)  ##安装源代码文件&#xA;library(mgarch)   ##查看是否可以使用&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>安装mgarchBEKK包</title>
      <link>http://gewutang.cn/2013/06/08/how-to-install-mgarchbekk/</link>
      <pubDate>Sat, 08 Jun 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/06/08/how-to-install-mgarchbekk/</guid>
      <description>&lt;p&gt;&lt;code&gt;R&lt;/code&gt;中另外一个拟合&lt;code&gt;BEKK&lt;/code&gt;模型的包是&lt;code&gt;mgarchBEKK&lt;/code&gt;。这个包跟前面提到的&lt;code&gt;mgarch&lt;/code&gt;包类似。主函数也基本一样，分别是&lt;code&gt;mvBEKK.sim&lt;/code&gt;、&lt;code&gt;mvBEKK.est&lt;/code&gt;、&lt;code&gt;mvBEKK.diag&lt;/code&gt;等。这个包作者已经很久没有更新了，好像已经放弃更新。但是有不少人还想用它。我们现在能在&lt;code&gt;quantmod&lt;/code&gt;包的主页找到该包的源代码，但不巧的是源代码包中缺失了&lt;code&gt;namespace&lt;/code&gt;。不过，没关系，我们可以为其添加一个&lt;code&gt;namespace&lt;/code&gt;即可。添加完之后，压缩为&lt;code&gt;.tar&lt;/code&gt;格式的压缩包（已添加完&lt;code&gt;namespace&lt;/code&gt;的&lt;code&gt;mgarchBEKK&lt;/code&gt;可以到&lt;a href=&#34;https://github.com/dengyishuo/dengyishuo.github.com/blob/master/media/mgarchBEKK.tar&#34;&gt;这里&lt;/a&gt;下载），再按照前面提到的安装方式进行安装即可。&lt;/p&gt;</description>
    </item>
    <item>
      <title>有用但不易被发现的R函数：file.choose</title>
      <link>http://gewutang.cn/2013/06/03/useful-functions-in-r/</link>
      <pubDate>Mon, 03 Jun 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/06/03/useful-functions-in-r/</guid>
      <description>&lt;p&gt;用R进行数据分析的时候，常常需要从外部载入数据或者函数。比如，我们有一个名为&lt;code&gt;example.txt&lt;/code&gt;的数据位于E盘中的R文件夹中，我们可以用下面的命令把数据读取到R里：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;dat = read.table(&amp;quot;E://R/example.txt&amp;quot;,header=TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;在同样的文件夹中，有一个现成的函数脚本文件命名为&lt;code&gt;fun.r&lt;/code&gt;，现在需要将其载入到R中使用。可以用下面的命令:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;soure(&amp;quot;E://R/fun.r&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;上面这两种做法有一个共同点就是得记住文件的绝对路径。绝对路径短的话，没有任何问题。当绝对路径很长，长到记不清楚的时候就麻烦了。&lt;/p&gt;&#xA;&lt;p&gt;一个偷懒的做法是使用&lt;code&gt;setwd()&lt;/code&gt;设定工作文件夹。比如，我们可以把R的工作路径设定为&lt;code&gt;E://R/&lt;/code&gt;：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;setwd(&amp;quot;E://R/&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;然后，载入数据或者函数就可以用简化的命令了：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;source(&amp;quot;fun.r&amp;quot;)&#xA;dat &amp;lt;- read.table(&amp;quot;example.txt&amp;quot;,header=TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;如果连&lt;code&gt;E://R/&lt;/code&gt;都记不清楚呢？那就只能搞一个浏览窗口了。R中恰好有这个函数：&lt;code&gt;file.choose()&lt;/code&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;source(file.choose())&#xA;dat &amp;lt;- read.table(file.choose(),header=TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;运行之后，R会弹出一个&lt;code&gt;Select File&lt;/code&gt;的小窗口，挥动鼠标就可以对各个盘符和文件夹进行选择了。&lt;/p&gt;</description>
    </item>
    <item>
      <title>自动下载财务套表</title>
      <link>http://gewutang.cn/2013/05/27/download-financial-sheets/</link>
      <pubDate>Mon, 27 May 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/05/27/download-financial-sheets/</guid>
      <description>&lt;p&gt;在国内获取上市公司的财务数据是一件叫人头疼的事儿。对于没有Wind终端的个人而言，只有一条路：那就是从网络上抓取。当然，这还是有前提的，那就是网上得有数据可抓。很长一段时间，我都受此困扰，跋山涉水之后，终于从sina财经找到了一个可以下载上市公司历年财务套表的接口，用R写一段自动下载的代码，就可以轻松获取上市公司财务数据了（耶！好开心，妈妈再也不用担心我的的数据来源了！）。&lt;/p&gt;&#xA;&lt;p&gt;代码真相在这里：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# symbol：股票代码&#xA;# type：报表类型。BS为资产负债表；PS为利润表；CF为现金流量表&#xA;# file:存储下载文件的地址。比如，file=&amp;quot;D://&amp;quot;&#xA;&#xA;getsheets &amp;lt;- function(symbol,type,file){&#xA;    pre=&amp;quot;http://money.finance.sina.com.cn/corp/go.php/vDOWN_&amp;quot;;&#xA;    mid=&amp;quot;/displaytype/4/stockid/&amp;quot;;&#xA;    end=&amp;quot;/ctrl/all.phtml&amp;quot;;&#xA;    &#xA;    if(type==&amp;quot;BS&amp;quot;){&#xA;        url=paste(pre,&amp;quot;BalanceSheet&amp;quot;,mid,symbol,end,sep=&amp;quot;&amp;quot;);&#xA;        destfile=paste(file,&amp;quot;BalanceSheet_&amp;quot;,symbol,&amp;quot;.xls&amp;quot;,sep=&amp;quot;&amp;quot;);&#xA;    }&#xA;    else if(type==&amp;quot;PS&amp;quot;){&#xA;          url=paste(pre,&amp;quot;ProfitStatement&amp;quot;,mid,symbol,end,sep=&amp;quot;&amp;quot;);&#xA;          destfile=paste(file,&amp;quot;ProfitStatement_&amp;quot;,symbol,&amp;quot;.xls&amp;quot;,sep=&amp;quot;&amp;quot;);&#xA;    }&#xA;    else if(type==&amp;quot;CF&amp;quot;){&#xA;          url=paste(pre,&amp;quot;CashFlow&amp;quot;,mid,symbol,end,sep=&amp;quot;&amp;quot;);&#xA;          destfile=paste(file,&amp;quot;CashFlow_&amp;quot;,symbol,&amp;quot;.xls&amp;quot;,sep=&amp;quot;&amp;quot;);&#xA;    }&#xA;    download.file(url, destfile);&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;运行时如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 下载000065的资产负债表，并存放在D盘&#xA;getsheets(&amp;quot;000065&amp;quot;,&amp;quot;BS&amp;quot;,&amp;quot;D://&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;D盘里面有一个叫BalanceSheet_000065.xls的文件，打开就可以看到历年的资产负债表数据了。&lt;/p&gt;</description>
    </item>
    <item>
      <title>音乐及语言</title>
      <link>http://gewutang.cn/2013/04/20/language/</link>
      <pubDate>Sat, 20 Apr 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/04/20/language/</guid>
      <description>&lt;p&gt;昨天去中山公园音乐厅听了一场音乐会，现在想起来意犹未尽。去的时候，地铁1号线上的人不多，让我喜出望外，兴许是天气在搞鬼。从地铁天安门西站出来的时候，夜色冥冥，还淅淅沥沥地下着小雨。长安街上人影寥寥，路面有点湿润，有些「天街小雨润如酥」的气氛。&lt;/p&gt;&#xA;&lt;p&gt;对我来说，趁着这样的天气到紫禁城去听音乐会是最好的。安静、肃穆，能见识到最好的老北京城。从拱门拐进长安西街的时候，无意中回头看了看对面的天安门广场，当然是灯火辉煌的，两边的松树还被装饰上彩灯，明明晃晃。只是人少了点儿。如果能再添几个叫卖的小摊贩和一些舞龙舞狮的生意，那可能就是清朝的京城了。&lt;/p&gt;&#xA;&lt;p&gt;听音乐会的时候，有一会儿在走神，因为我一直想琢磨钢琴这东西到底是怎么制造出来的，到底是什么人能想到搞出来这样一台机器来娱人身心？真TM是个天才。有机会的话要好好研究一下钢琴的原理，争取自己搞一台简易点的出来。可是，想到自己连乐理都没有搞的十分清楚，这种念头是不是太痴心妄想了？&lt;/p&gt;&#xA;&lt;p&gt;我对音乐不是很精通，对音符、旋律这些东西不是很敏感。说白了，对我来说，音乐是一门陌生的语言。我虽然能学会一点，可是因为没有系统地掌握过这门语言的单词和句法，所以，好多时候并不能精确地理解音乐家用这门语言书写的那些篇章。&lt;/p&gt;&#xA;&lt;p&gt;当音乐当作是语言，我觉得是个很有意思的看法。因为音乐有自己的词汇，也有自己的语法规则，也能够构成自己的文章。好像不只是音乐，会计、金融都可以被视为语言。学习知识很多时候就是学习语言，要了解词汇、语法规则，最重要的是要多听多用。以后要不要创建一门学科叫&lt;code&gt;大语言学&lt;/code&gt;？&lt;/p&gt;</description>
    </item>
    <item>
      <title>从东方财富网获取股票开户数据</title>
      <link>http://gewutang.cn/2013/04/18/get-acount-data/</link>
      <pubDate>Thu, 18 Apr 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/04/18/get-acount-data/</guid>
      <description>&lt;p&gt;股票账户的开户数据对于观察和预测股票市场的冷热程度非常重要。但对于普通的投资人而言，由于没有专业的数据终端，他们是没有太多渠道可以获得股票开户数据的。好在一些网站上经常会公布股票开户数据，这样给普通投资者一个很好的渠道去获取数据。&lt;/p&gt;&#xA;&lt;p&gt;目前，东方财富网的&lt;a href=&#34;https://data.eastmoney.com/cjsj/gpkhsj.html&#34;&gt;网页&lt;/a&gt;上就提供了逐月的股票账户开户数据，可以供普通投资者参考。&lt;/p&gt;&#xA;&lt;p&gt;以前有人用R软件中的XML包去抓取上述网页。现在R软件中则提供了httr包和rvest包替代XML去抓取网页。不过，鉴于网站可能有严格的反爬虫机制，这种办法未必可以成功了。我还听说有人用RSelenium包获取网页上的表格数据，这个方案可能更复杂一些，但更可行。&lt;/p&gt;</description>
    </item>
    <item>
      <title>几本关于R的书籍</title>
      <link>http://gewutang.cn/2013/04/18/some-books-on-r/</link>
      <pubDate>Thu, 18 Apr 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/04/18/some-books-on-r/</guid>
      <description>&lt;p&gt;最近，出版社的朋友推荐了四本在亚马逊上卖的火热的R书籍。大致看了下内容，都算是上乘之作。这里简单介绍一下：&lt;/p&gt;&#xA;&lt;p&gt;第一本叫《Introductory Time Series with R》。这是Springer出版社的UseR!系列作品，质量绝对是有保证的，内容集中于时间序列分析方面。作者是Paul S.P. Cowpertwait和Andrew V. Metcalfe 。&lt;/p&gt;&#xA;&lt;p&gt;第二本是《Time Series Analysis: With Applications in R》是Springer统计学系列的一本书，也是专注于时间序列分析，这本书的厚度是第一本书的两倍。&lt;/p&gt;&#xA;&lt;p&gt;第三本叫《Financial Risk Forecasting: The Theory and Practice of Forecasting Market Risk with Implementation in R and Matlab 》是Wiley出版社Finance Series系列的一本书，比较薄，内容主要集中在金融风险的预测上，更适合搞投资的人来阅读。&lt;/p&gt;&#xA;&lt;p&gt;第四本是《Software for Data Analysis: Programming with R》是 Springer旗下Statistics and Computing系列的一般书，作者是R领域的大牛人叫John Chambers，我想他的名字是如雷贯耳的，无需多言，只需膜拜。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的量化投资包：quantstrat</title>
      <link>http://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>http://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=&#34;quantstrat是什么东西&#34;&gt;quantstrat是什么东西？&lt;/h2&gt;&#xA;&lt;p&gt;quantstrat是Quantitative Strategy（量化策略）的缩写，它是由Peter Carl、 Brian G. Peterson、Josh Ulrich、Garrett See、Yu Chen等联合开发的用于量化投资的R包。&lt;/p&gt;&#xA;&lt;p&gt;这个包为用户提供了一个测试和模拟基于信号的量化策略模型的泛型框架。它不仅可以&#xA;帮助用户构建交易系统，还可以对构建的交易系统进行仿真测试；能够支持对多资产类别和多币种组合进行回测（backtesting）等。&lt;/p&gt;&#xA;&lt;p&gt;这个包在设计的过程中充分贯彻了“交易导向型”（Transaction-oriented）原则。&lt;/p&gt;&#xA;&lt;p&gt;目前，这个包由Brian G.Peterson在维护，还在开发中，不过已经被很多业内人士拿来实战了。&lt;/p&gt;&#xA;&lt;p&gt;因为quantstrat包是一个建立在xts, FinancialInstrument,blotter等包上的高级抽象包，所以，在应用quantstrat包时，&#xA;除了除了安装quantstrat包之外，还得记得装上这三个支撑包。看起来安装有点麻烦，正式因为安装麻烦，所以，它才能使得用户用几行简单的代码就能来构建和测试策略。&lt;/p&gt;&#xA;&lt;h2 id=&#34;quantstrat涉及的量化策略基础知识&#34;&gt;quantstrat涉及的量化策略基础知识&lt;/h2&gt;&#xA;&lt;h3 id=&#34;基于信号的策略的泛型建模&#34;&gt;基于信号的策略的泛型建模&lt;/h3&gt;&#xA;&lt;p&gt;基于信号的策略模型需要先生成交易信号。交易指标是从市场数据中获知的量化指标（包括移动平均、RSI、波动带、通道、动量等）。交易指标应该以矢量化（快速回测）和数据流（实施执行）的形式应用于市场数据，并且假定交易指标是路径独立的（即不受账户、组合类型、现有头寸和交易的限制）。&lt;/p&gt;&#xA;&lt;p&gt;交易指标和市场数据的相互作用会生成交易信号（交叉、阈值、倍数），交易信号标示交易者应当采取交易行动的时间，虽然，有时候，你无法行动。跟交易指标类似，交易信号也应该以矢量化和流形式应用于市场数据，并且需要假定交易信号是路径独立的。&lt;/p&gt;&#xA;&lt;p&gt;交易规则基于交易指标、交易信号、账户头寸和账户/组合特性生成交易指令。注意关于头寸大小、填补模拟、指令生成和管理的规则与交易指标和交易信号的生成过程是独立的。与交易指标和交易信号不同，交易规则在是执行时是路径依赖式（事实上，也支持非路径依赖式的交易规则，然而，非路径依赖式的交易规则在现实中很少见）的，与已往市场数据和执行规则时的头寸大小相关。交易规则可以生成新的交易指令，也可以对现有交易指令进行修改（比如风险控制、填补、再平衡、介入、退出等）。&lt;/p&gt;&#xA;&lt;h3 id=&#34;quantstrat如何构建策略&#34;&gt;Quantstrat如何构建策略？&lt;/h3&gt;&#xA;&lt;p&gt;quantstrat通过FinancialInstrument包指定金融工具（包括所用币种）、通过&lt;code&gt;blotter&lt;/code&gt;包跟踪交易信息和市值及组合和账户的变动。&lt;/p&gt;&#xA;&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;&#xA;&lt;p&gt;用来指定交易指标、交易信号和加以规则的函数不限于上述那些。add.indicator、add.signal、和add.rule的参数为任意R函数。因为支持链是基于xts对象构建的，因此，如果自定义的函数返回结果是xts对象的话，其能够被很好地整合到quantstrat包中。&lt;/p&gt;&#xA;&lt;p&gt;策略模型以层形式创建，并且是延迟执行的。这意味着策略可以不加调整地应用于多种不同的组合。执行之前，策略不知道自身将应用到何种金融工具上，也不知道将被传递什么参数。例如，移动平均的时间参数和阈值参数很可能影响策略的性能。参数的默认值可以在策略对象中设定，也可以在执行时通过applyStrategy的参数（参数是一个命名列表，跟参数列表的用法类似）来设定。&lt;/p&gt;&#xA;&lt;p&gt;quantstrat对交易指令进行建模，交易指令可能转化为实际交易，也可能不能转化为实际交易。这提供了一个额外功能，可以用来评估策略是如何工作的、为何不工作以及如何改进。例如，策略的性能经常受到“休息现价订单”的改变、替换和删除的影响。订单允许量化策略师检查决策作出时的市场条件。同时，订单历史使得针对许多策略的颇为重要的计算过程变得简单，比如填补比率的计算。&lt;/p&gt;&#xA;&lt;h3 id=&#34;参数匹配规则&#34;&gt;参数匹配规则&lt;/h3&gt;&#xA;&lt;p&gt;Quantstrat中很多地方会应用到通过策略技术参数传递（参数列表或者…）给交易指标、信号和交易规则函数的变元：&lt;/p&gt;&#xA;&lt;p&gt;这些变元按照下面的顺序进行匹配，后面的赋值可以覆盖前值。具体的顺序如下:&#xA;赋值给每个交易指标、交易信号和交易规则的arguments=list(…)参数；&#xA;调用&lt;code&gt;applyStrategy&lt;/code&gt;是应用的&lt;code&gt;parameters=list{…}&lt;/code&gt;参数；&#xA;调用&lt;code&gt;applyStrategy&lt;/code&gt;时，在…中传递的任意附加参数。&lt;/p&gt;</description>
    </item>
    <item>
      <title>不放肝胆，如何成行？</title>
      <link>http://gewutang.cn/2013/04/11/taking-a-change-on-a-new-road/</link>
      <pubDate>Thu, 11 Apr 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/04/11/taking-a-change-on-a-new-road/</guid>
      <description>&lt;p&gt;二月底的时候，一直在跟林荟断断续续的商量统计之都的事情，制定了一些计划，却总觉得推行地稍慢。&lt;/p&gt;&#xA;&lt;p&gt;我们这些人似乎非得推一推才能往前走。当时突然兀自决定在R会议之前最好增加一次R基础培训，于是，自己先把消息放出去了，心里想的是，只有这样才能逼着大家往前走。&lt;/p&gt;&#xA;&lt;p&gt;三月初，跟太云、堰平和思喆碰头商量了R基础培训的事情。席间大家对培训的事情又爱又恨。爱的是这个东西似乎很有前景，恨的是不知道需求到底有多少。世事多难，也不知道这条道儿有多少曲折。然而，通知既然已经发出，恐怕就只能走下去了。&lt;/p&gt;&#xA;&lt;p&gt;俗话说，万事开头难。真是一语成谶。培训通知发出后，二十几天过去了，报名者寥寥，大家都有些心灰意冷。好在堰平心有不甘，紧急踩了一脚油门儿，几天之中十几人报名。于是，联系培训场地，购买培训书籍，两天之中安排妥当。眼下，等着周末堰平在新街口为大家开班儿。期间种种得失，培训结束再行总结。&lt;/p&gt;&#xA;&lt;p&gt;不管好坏，先做起来总是好的。行动起来胜过患得患失。&lt;/p&gt;</description>
    </item>
    <item>
      <title>初来乍到</title>
      <link>http://gewutang.cn/2013/04/10/new-comer/</link>
      <pubDate>Wed, 10 Apr 2013 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2013/04/10/new-comer/</guid>
      <description>&lt;p&gt;折腾了好些日子，博客终于搬家了。由于我对github上搞博客还比较陌生，感觉自己初来乍到，就像刘姥姥到了大观园一样，有点提心吊胆、惶恐不安。加上最近我也在搬家，就迟迟没有动手写东西了。&lt;/p&gt;&#xA;&lt;p&gt;但终归这里还是自己的地界儿，还得靠自己打理。思来想去还是得码几个字儿，好歹算是宣布自己来了。初来乍到，我是不是应该说一句&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;“Hello，世界！”&lt;/p&gt;&lt;/blockquote&gt;</description>
    </item>
    <item>
      <title>科斯对现代经济学的批评</title>
      <link>http://gewutang.cn/2012/12/13/coase-on-economics/</link>
      <pubDate>Thu, 13 Dec 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/12/13/coase-on-economics/</guid>
      <description>&lt;p&gt;当下的经济学，无论是印刷在教课书上的，抑或是讲台上口授的，都与商业关系疏远，与企业家精神更是差以千里。经济学与商业生活的背离已经到了触目惊心甚至糟糕的地步。&lt;/p&gt;&#xA;&lt;p&gt;往昔并非如此。回想现代经济学诞生之初，亚当斯密将其设想为一门研究“国家财富性质与原因”的学问。他的极具开创性的著作——《国富论》——被商人们——尽管亚当斯密因为他们贪婪、短视和其他缺点而赤裸裸地蔑视他们——广为阅读。该著作激发并引了导政客们针对贸易和其它经济政策的争论。当时的学术社区非常之小，以至于经济学家们不得不通过著作向公众们大声疾呼。甚至在近于20世纪的端口，阿尔弗雷德马歇尔依然努力将经济学定义为一门“研究财富和人类行为”的学问。当时的经济学保持住了与产业界的联系。&lt;/p&gt;&#xA;&lt;p&gt;20世纪，经济学将自己巩固成为一门职业。经济学家们可以专为别人著述。同时，经济学领域经历了一次典范转移，其逐步将自己打造为一个探讨经济化行为的理论分支，并逐步放弃了把真实经济运行作为研究对象的传统。眼下，产出理论在经济学中已被边缘化，取而代之的流行范式是研究稀缺资源配置的均衡性。经济学家用以分析商业公司的工具过于抽象和投机，这些工具对于企业家和经理们试图将经典产品低价销售给消费者的常态努力少有帮助。&lt;/p&gt;&#xA;&lt;p&gt;经济学与真实经济运行的脱离极大地伤害了商业和学术。鉴于经济学家难以提供务实的观点，企业家和经理们将不得不依靠自身的商业头脑、个人判断和经验来进行决策。经济危机期间，当商业领袖丧失自&#xA;信，他们经常想要向政治力量求助。政府正日益被看作是经济问题、创新和就业的拯救者。&lt;/p&gt;&#xA;&lt;p&gt;因为经济学正日益沦为政府用以管理经济的便捷手段，当公众试图寻找“经济如何运转”的真知时，已无法再向经济学这一工具求助。鉴于经济学不再植根于对真实经济运行的系统性研究，其也难担此任。在人类的大部分历史中，家庭和部落都主要依靠于周遭已存的经济环境；他们与其他家庭或者部落以及外部世界的联系十分贫乏且断断续续。商业社会的崛起彻底扭转了这一情形。如今，一个现代市场经济及其劳动力的精细分工依赖于一个不断扩大的贸易网。这需要一个错综复杂的社会机构来协调市场机制和公司的各种边界。在现代经济日益机构密集之时，将经济学理论简化为价格理论无异于自找泥潭；当经济学沦为硬科学，无视社会、历史、文化和政治对经济运行的影响时，无异于自取灭亡。&lt;/p&gt;&#xA;&lt;p&gt;重建贫瘠的经济学领域与经济联系已迫在眉睫。市场经济正在中国、印度、非洲和其他地方纷纷兴起，这意味着一个充满着企业家精神的时代的到来。对于经济学家而言，这是一个千载难逢的机会，经济学家得以借此机会研究市场经济是如何在不同的文化、机构和组织形态中建立的。然而，只有当经济学家调整自身，重新将人作为人，将经济体系作为经济体系来研究时，才能有所收获。&lt;/p&gt;</description>
    </item>
    <item>
      <title>里昂证券最重要的图表：中国外储增长表</title>
      <link>http://gewutang.cn/2012/12/01/the-most-importtant-plot-about-china/</link>
      <pubDate>Sat, 01 Dec 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/12/01/the-most-importtant-plot-about-china/</guid>
      <description>&lt;p&gt;几天前，华尔街日报采访了法国里昂证券的研究员Russel Napier。Russel Napier说，目前全球经济已进入早期复苏阶段的希望可能过于乐观了。他说，中国外汇储备增长将出现令人担忧的停滞，而中国外汇储备增长一直是全球经济增长的动力。&lt;/p&gt;&#xA;&lt;p&gt;从下图可以看到，中国外汇储备增长率已经在过去五年里急剧下降，并且现在已经接近零增长。Napier说下面这张图表是“世界上最重要的图表”。他说：“在过去20年中，中国外储增长决定了金融市场所有的关键发展动向。印钱措施将美国国债收益率曲线人为压低。它是全球经济增长的基石，而现在这一切结束了。”&lt;/p&gt;&#xA;&lt;p&gt;与之相佐证的是eFinancial News的一则报道：&lt;/p&gt;&#xA;&lt;p&gt;上一次中国外储增长低于10%出现在1990年代末，接着就是科技股市场泡沫破裂，经济衰退。自2001年增长恢复后，经济在那十年间也蓬勃发展。但是在2007年金融危机爆发前，中国外储增长再度下降。&lt;/p&gt;&#xA;&lt;p&gt;在这里有必要厘清一下Russel Napier和eFinancial News背后的逻辑。&lt;/p&gt;&#xA;&lt;p&gt;在长久的一段时间里，世界经济的增长一直靠中美之间的金融配合驱动的。具体的逻辑是这样的：起点是美国开动印钞机，导致美国消费、投资增加；进一步导致以引入外资和出口为导向的国家（以中国为主）的外汇储备的增加（通过出口赢得外汇）；这些国家利用外储购买美国国债，使资金重新回流到美国，美国信贷增加，消费、投资增加。如此循环不已。&lt;/p&gt;&#xA;&lt;p&gt;随着中国经济的发展，中国改变现行发展模式的动力越来越大。这意味着中国将不再像过去那样参与上面的信贷循环。这无疑将在短期内给世界经济带来沉重的打击。世界经济要恢复增长的路径无非两条：第一条，让中国重回过去的角色，这似乎不太可行。那么，就只有寻找一个替代中国的国家；第二条，中国开始扮演接近美国的角色，这样一来，中国需要进行大量的改革措施来适应这一角色。任重道远。&lt;/p&gt;&#xA;&lt;p&gt;无论如何世界经济将经历一个艰难的再平衡过程，这个过程已经进行了3年，根据历史经验，还需要2-4年的时间才能走完。对投资者而言，这可能是个好机会。每当经济运行到底部，总是有一堆被低估的证券可供选择。&lt;/p&gt;&#xA;&lt;p&gt;获取数据和绘制图形的R代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(XML)&#xA;getFX=function(pg){&#xA;url=paste(&amp;quot;http://data.caixin.com/macro/macro_indicator_more.html?id=F0010&amp;amp;cpage=&amp;quot;,pg,&amp;quot;&amp;amp;pageSize=30&amp;amp;url=macro_indicator_more.html#top&amp;quot;,sep=&amp;quot;&amp;quot;);&#xA;url=htmlParse(url,encoding=&amp;quot;utf-8&amp;quot;);&#xA;table=readHTMLTable(url)[[2]]&#xA;}&#xA;&#xA;table=getFX(1)&#xA;for(i in 2:7){&#xA;temp=getFX(i)&#xA;table=rbind(table,temp)&#xA;}&#xA;write.csv(table,&amp;quot;FX.csv&amp;quot;)&#xA;FX=read.csv(&amp;quot;FX.csv&amp;quot;)[,4]&#xA;growth=diff(rev(FX))/rev(FX)*100&#xA;growth.ts=ts(growth,start=c(1995,5),frequency=12)&#xA;plot(growth.ts,type=&amp;quot;h&amp;quot;,col=&amp;quot;yellow2&amp;quot;)&#xA;abline(h=0.5,col=&amp;quot;red&amp;quot;,lty=2)&#xA;text(1998,1,&amp;quot;1998&amp;quot;)&#xA;text(2008,1,&amp;quot;2008&amp;quot;)&#xA;text(2012,1,&amp;quot;2012&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>从历史数据看今日股市</title>
      <link>http://gewutang.cn/2012/11/27/historical-data-and-todays-stock-market/</link>
      <pubDate>Tue, 27 Nov 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/11/27/historical-data-and-todays-stock-market/</guid>
      <description>&lt;p&gt;在股市发展史上，人性的贪婪和恐惧总是交替出现的。如果我们假设股市存在一个潜在的合理增长线，那么，贪婪和恐惧则周期性地驱动着股市围绕潜在合理增长线上下波动。在这个假设下，找到潜在的合理增长线、最大偏离幅度及偏离持续期对于我们进行投资就显得很有意义。&lt;/p&gt;&#xA;&lt;p&gt;观察一下1999年-2012年中国股市数据的直方图和经验累积分布函数图。我将大于&lt;code&gt;quantile(ssec,p=0.9)&lt;/code&gt;的数据定义为异常值（下图中的黑点部分）。剔除异常值数据之后，拟合出股市的潜在合理增长线（蓝色线）&#xA;。&#xA;图形显示，中国股市已经步入低估时代（恐惧驱动的时代）一段时间了，然而，还没有见底。未来的底部可能偏离潜在合理增长线在1.5倍标准差附近。&lt;/p&gt;&#xA;&lt;p&gt;相关R代码为：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)&#xA;getSymbols(&amp;quot;^SSEC&amp;quot;,from=&amp;quot;1991-01-01&amp;quot;)&#xA;ssec=coredata(Cl(SSEC))&#xA;&#xA;layout(m=matrix(c(1,2,&#xA;                  3,3),nc=2,byrow=T))&#xA;&#xA;hist(ssec,prob=T)&#xA;lines(density(ssec))&#xA;&#xA;plot(ecdf(ssec))&#xA;abline(h=0.9,lty=2)&#xA;abline(v=quantile(ssec,0.9),lty=2)&#xA;&#xA;plot(ssec,pch=20)&#xA;points((1:length(ssec))[ssec&amp;lt; =quantile(ssec,0.9)],ssec[ssec&amp;lt;=quantile(ssec,0.9)],col=&amp;quot;red&amp;quot;,pch=20)&#xA;l=lm(ssec[ssec&amp;lt;=quantile(ssec,0.9)]~(1:length(ssec))[ssec&amp;lt;=quantile(ssec,0.9)])&#xA;abline(l,col=&amp;quot;blue&amp;quot;)&#xA;&#xA;h=fitted(l)+2*sd(residuals(l))&#xA;lines((1:length(ssec))[ssec&amp;lt;=quantile(ssec,0.9)],h,col=&amp;quot;green&amp;quot;)&#xA;&#xA;h=fitted(l)-2*sd(residuals(l))&#xA;lines((1:length(ssec))[ssec&amp;lt;=quantile(ssec,0.9)],h,col=&amp;quot;green&amp;quot;)&#xA;dev.off()&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>乱码，又见乱码</title>
      <link>http://gewutang.cn/2012/09/13/junk-code-again/</link>
      <pubDate>Thu, 13 Sep 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/09/13/junk-code-again/</guid>
      <description>&lt;p&gt;在Windows系统下，用readHTMLTable函数读取编码为unicod的网页时会出现乱码。这个时候的乱码是因为R是用本地码（通常是GBK）来解释了网页中的unicode，因此出现了符号不兼容。此时，只要用iconv(x,&amp;ldquo;utf-8&amp;rdquo;,&amp;ldquo;gbk&amp;rdquo;)就可以了。然而，有时候会出现另外一种情况：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(XML);&#xA;url=&amp;quot;http://data.caixin.com/macro/macro_indicator_more.html?id=F0001&amp;amp;cpage=2&amp;amp;pageSize=30&amp;amp;url=macro_indicator_more.html#top&amp;quot;;&#xA;tables=readHTMLTable(url)&#xA;head(tables)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;这种情况下icnov好像就无能为力了。不过，事情还是有解决方案的，在readHTMLTable之前加上一句代码就可以了。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;url= htmlParse(url,encoding=&amp;quot;UTF-8&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;看看效果：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(XML)&#xA;url &amp;lt;- &amp;quot;http://data.caixin.com/macro/macro_indicator_more.html?id=F0001&amp;amp;cpage=2&amp;amp;pageSize=30&amp;amp;url=macro_indicator_more.html#top&amp;quot;&#xA;url&amp;lt;- htmlParse(url,encoding=&amp;quot;UTF-8&amp;quot;)&#xA;tables &amp;lt;- readHTMLTable(url)&#xA;head(tables)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>获取上市公司主要财务指标</title>
      <link>http://gewutang.cn/2012/09/07/get-financial-data-from-web/</link>
      <pubDate>Fri, 07 Sep 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/09/07/get-financial-data-from-web/</guid>
      <description>&lt;p&gt;写了一段获取上市公司主要财务指标的代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载XML包&#xA;library(XML)&#xA;####id:股票代码&#xA;####year:年度&#xA;get.finance &amp;lt;- function(id,year,...){&#xA;  web &amp;lt;- paste(&amp;quot;http://money.finance.sina.com.cn/corp/go.php/vFD_FinancialGuideLine/stockid/&amp;quot;,id,&amp;quot;/ctrl/&amp;quot;,year,&amp;quot;/displaytype/4.phtml&amp;quot;,sep=&amp;quot;&amp;quot;)&#xA;  tables &amp;lt;- readHTMLTable(web)$BalanceSheetNewTable0&#xA;  colnames(tables)&amp;lt;-tables[1,]&#xA;  tables &amp;lt;- tables[-1,]&#xA;  return(tables)&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;简单演示一下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(XML)&#xA;res &amp;lt;- get.finance(&amp;quot;000002&amp;quot;,&amp;quot;2012&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;基于这些财务数据和价格数据，就可以着手建立股票排名系统了。&lt;/p&gt;</description>
    </item>
    <item>
      <title>美食记：椰奶鸡蛋羹</title>
      <link>http://gewutang.cn/2012/08/30/my-cookbook/</link>
      <pubDate>Thu, 30 Aug 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/08/30/my-cookbook/</guid>
      <description>&lt;p&gt;这段时间对美食很感兴趣，时不时地大动烟火之心到处踅摸各种美食料理。前几天在微博上看到一个简易美食叫“椰奶鸡蛋羹”，自己试着做了下，味道还不错，聊记之。&lt;/p&gt;&#xA;&lt;p&gt;要用到的材料：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;鸡蛋2个&lt;/li&gt;&#xA;&lt;li&gt;椰树椰汁1瓶&lt;/li&gt;&#xA;&lt;li&gt;白砂糖若干&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;做法：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;把两颗鸡蛋洗净外壳，打入碗中打散（我一般会打100下，叫“百打蛋”），加一勺白砂糖。&lt;/li&gt;&#xA;&lt;li&gt;倒入半罐椰树椰汁，放入蒸锅内大火蒸上15分钟即可。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;味道又甜又香，十分美味。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的斯皮尔曼等级相关检验与异方差</title>
      <link>http://gewutang.cn/2012/08/16/spearman-test-in-r/</link>
      <pubDate>Thu, 16 Aug 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/08/16/spearman-test-in-r/</guid>
      <description>&lt;p&gt;有一种异方差表现为模型的残差与自变量相关，也就是说，模型残差随着自变量增大而变大或者反之。在这种情况下只要度量模型的残差与自变量的相关性就可以确定是否有异方差了。如果自变量是连续变量，则普通的相关性检验就可以奏效。倘若自变量是年龄、职业、气候等定型变量时就得考虑别的方法了。&lt;/p&gt;&#xA;&lt;p&gt;此时，斯皮尔曼等级相关检验是个不错的选择。斯皮尔曼等级相关检验是一种非参数检验方法，它的大致原理是这样的：&lt;/p&gt;&#xA;&lt;p&gt;如果变量和变量相关，那么，将两者按照大小排序，则两者的顺序应该是相仿的，若两者相关性弱，则两者的顺序没有明显的一致性。&lt;/p&gt;&#xA;&lt;p&gt;举个简单的例子：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt; x &amp;lt;-c(25,40,54,58,68)&#xA; y &amp;lt;-c(1.6,2.9,10.7,14.8,5.7) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;上下对应，下面按照的大小进行排序，则能看到下面的结果：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;rank(x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4 5&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;rank(y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 1 2 4 5 3&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;rank(x)-rank(y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1]  0  0 -1 -1  2&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;构造统计量，容易知道等于1时，两者正相关；等于-1时两者负相关。设定原假设统计量为零，该统计量近似服从均值为0，方差为的正态分布，由此可对其进行假设检验。&lt;/p&gt;&#xA;&lt;p&gt;R中&lt;code&gt;pspearman&lt;/code&gt;包中的&lt;code&gt;spearman.test&lt;/code&gt;函数可以完成斯皮尔曼等级相关检验：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 安装包，如果未安装&#xA;# install.packages(&amp;quot;pspearman&amp;quot;)&#xA;library(pspearman)&#xA;x=c(25,40,53,58,68)&#xA;y=c(1.6,2.9,10.7,14.8,5.7)&#xA;spearman.test(x,y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## &#x9;Spearman&#39;s rank correlation rho&#xA;## &#xA;## data:  x and y&#xA;## S = 6, p-value = 0.2333&#xA;## alternative hypothesis: true rho is not equal to 0&#xA;## sample estimates:&#xA;## rho &#xA;## 0.7&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;也可用&lt;code&gt;cor.test&lt;/code&gt;函数来检验：&lt;/p&gt;</description>
    </item>
    <item>
      <title>资本市场的先知先觉</title>
      <link>http://gewutang.cn/2012/05/15/capital-market/</link>
      <pubDate>Tue, 15 May 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/05/15/capital-market/</guid>
      <description>&lt;p&gt;广州药业(600332)和白云山A(000522)近期的股价走势淋漓尽致地阐释了资本市场的先知先觉。事情肇始于2011年的价值1080亿元的“王老吉”商标合同争议案，回顾一下重要历史有助于理解资本市场的特性。&lt;/p&gt;&#xA;&lt;p&gt;2011年4月，广药向贸仲提出仲裁请求，并提供相应资料；5月王老吉商标案立案，确定当年9月底开庭；后因鸿道集团一直未应诉，开庭时间推迟至2011年12月29日，但当日仲裁并未出结果。&lt;/p&gt;&#xA;&lt;p&gt;2012年5月10日，广药集团在其官网发布紧急招聘公告，称其全资子公司广州王老吉大健康产业有限公司，紧急招聘3000名“快消人才”。&lt;/p&gt;&#xA;&lt;p&gt;2012年5月11日，广药集团收到中国国际经济贸易仲裁委员会日期为2012年5月9日的裁决书，贸仲裁决：广药集团与加多宝母公司鸿道(集团)有限公司签订的《“王老吉”商标许可补充协议》和《关于“王老吉”商标使用许可合同的补充协议》无效；鸿道(集团)有限公司停止使用“王老吉”商标。该裁决为终局裁决，自作出之日起生效。&lt;/p&gt;&#xA;&lt;p&gt;2012年5月13日晚，广药集团发布公告告知上述裁决书。然而，广州药业(600332)和白云山A(000522)的股价在5月3日就开始有所企升。&lt;/p&gt;&#xA;&lt;p&gt;对大多数资本市场的利好消息而言，当你知道的时候，那个好消息的利好价值已经被先知先觉者压榨殆尽了，再去跟就真成傻瓜。&lt;/p&gt;</description>
    </item>
    <item>
      <title>棘手的R乱码和给力的iconv函数</title>
      <link>http://gewutang.cn/2012/05/09/iconv-and-r/</link>
      <pubDate>Wed, 09 May 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/05/09/iconv-and-r/</guid>
      <description>&lt;p&gt;从网上抓数据（网页是&lt;code&gt;utf-8&lt;/code&gt;编码）时，发现读入R的数据出现了乱码。代码和返回结果分别如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;library(XML)&#xA;id=&amp;quot;000002&amp;quot;&#xA;year=&amp;quot;2011&amp;quot;&#xA;web=paste(&amp;quot;http://money.finance.sina.com.cn/corp/go.php/vFD_FinancialGuideLine/stockid/&amp;quot;,id,&amp;quot;/ctrl/&amp;quot;,year,&amp;quot;/displaytype/4.phtml&amp;quot;,sep=&amp;quot;&amp;quot;)&#xA;web&#xA;[1] &amp;quot;http://money.finance.sina.com.cn/corp/go.php/vFD_FinancialGuideLine/stockid/000002/ctrl/2011/displaytype/4.phtml&amp;quot;&#xA;tables head(tables)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;涓囩A(000002) 璐一姟镌囨爣 NA&#xA;1 鎶ュ憡镞ユ湡 2011-12-31&#xA;2 姣忚偂镌囨爣&#xA;3 鎽婅杽姣忚偂鏀剁泭(鍏\x83) 1.055&#xA;4 锷犳潈姣忚偂鏀剁泭(鍏\x83) 0.88&#xA;5 姣忚偂鏀剁泭_璋冩暣鍚\x8e(鍏\x83) 0.88&#xA;6 镓ｉ櫎闱炵粡甯告ф崯鐩婂悗镄勬疮镶℃敹鐩\x8a(鍏\x83) 0.87&#xA;NA NA NA&#xA;1 2011-09-30 2011-06-30 2011-03-31&#xA;2&#xA;3 0.3735 0.2958 0.1082&#xA;4 0.326 0.27 0.11&#xA;5 0.326 0.27 0.11&#xA;6 0.321 0.27&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;去COS咨询了一番，都说运行完我提供的代码之后没有出现乱码。当时的我有些凌乱。心中苦闷得很，难道R代码的运行结果还跟运行者相关么？如果R语言真能发展到如此智能的地步，我定然乐不可支。当然，这是题外话了，按住不说。&lt;/p&gt;&#xA;&lt;p&gt;于是，我泡在论坛上翻了无数旧帖，最终发现了李舰和陈丽云讨论的结果。他们说R中出现乱码后可以用&lt;code&gt;Encoding&lt;/code&gt;来解决。在这个原则指导下我各种&lt;code&gt;Encoding&lt;/code&gt;，却回回都是无功而返。思考再三，我觉得可能是电脑的问题。&lt;/p&gt;&#xA;&lt;p&gt;只是电脑的问题那么多，根源在哪儿呢？我贴出了自己的&lt;code&gt;sessionInfo&lt;/code&gt;，想找出问题的根源。幸好有人跟帖也贴出了sessionInfo。对比之下才发现问题出在电脑系统的内部编码上。他们几人所用的都是Linux平台，内部编码设定都是utf。而我苦苦耗在的Windows平台上，系统的内部编码是gbk。&lt;/p&gt;&#xA;&lt;p&gt;好了，找到了病根。下一步只要修改一下Windows系统的内部编码就行了。我正欢欣鼓舞呢，颜林林却说，Windows系统没法修改系统编码。好吧!我就像一个病人在熟读各种医书终于发现病根所在之时，却被告知没有药材。绝望之情状如井喷。&lt;/p&gt;&#xA;&lt;p&gt;至此，我几近投奔Linux了。结果，无意中见到一个人用iconv函数解决类似的问题。看起来效果不错，我就尝试了一下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;rownames(tables)&amp;lt;-iconv(tables[,1],&amp;quot;UTF-8&amp;quot;,&amp;quot;gbk&amp;quot;)&#xA;head(tables[,-1])&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;虽然不知道什么原理，但问题毕竟是解决了。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R语言在线资源列表</title>
      <link>http://gewutang.cn/2012/02/19/web-resource-about-r/</link>
      <pubDate>Sun, 19 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/19/web-resource-about-r/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://alittleknowledge.wordpress.com/2009/09/11/r-for-pedestrians/&#34;&gt;http://alittleknowledge.wordpress.com/2009/09/11/r-for-pedestrians/&lt;/a&gt;&#xA;&lt;a href=&#34;http://astro.u-strasbg.fr/~fmurtagh/mda-sw/&#34;&gt;http://astro.u-strasbg.fr/~fmurtagh/mda-sw/&lt;/a&gt;&#xA;&lt;a href=&#34;http://bayes.bgsu.edu/bcwr/&#34;&gt;http://bayes.bgsu.edu/bcwr/&lt;/a&gt;&#xA;&lt;a href=&#34;http://biocep-distrib.r-forge.r-project.org/doc.html&#34;&gt;http://biocep-distrib.r-forge.r-project.org/doc.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://biostatistics.iop.kcl.ac.uk/publications/everitt/&#34;&gt;http://biostatistics.iop.kcl.ac.uk/publications/everitt/&lt;/a&gt;&#xA;&lt;a href=&#34;http://blog.opendatagroup.com/courses/&#34;&gt;http://blog.opendatagroup.com/courses/&lt;/a&gt;&#xA;&lt;a href=&#34;http://bm2.genes.nig.ac.jp/RGM2/index.php?clear=all&#34;&gt;http://bm2.genes.nig.ac.jp/RGM2/index.php?clear=all&lt;/a&gt;&#xA;&lt;a href=&#34;http://cm.bell-labs.com/cm/ms/departments/sia/project/trellis/&#34;&gt;http://cm.bell-labs.com/cm/ms/departments/sia/project/trellis/&lt;/a&gt;&#xA;&lt;a href=&#34;http://code.google.com/p/batchfiles/&#34;&gt;http://code.google.com/p/batchfiles/&lt;/a&gt;&#xA;&lt;a href=&#34;http://commons.wikimedia.org/wiki/Category:Created_with_R&#34;&gt;http://commons.wikimedia.org/wiki/Category:Created_with_R&lt;/a&gt;&#xA;&lt;a href=&#34;http://csde.washington.edu/statnet/&#34;&gt;http://csde.washington.edu/statnet/&lt;/a&gt;&#xA;&lt;a href=&#34;http://dataspora.com/blog/predictive-analytics-using-r/&#34;&gt;http://dataspora.com/blog/predictive-analytics-using-r/&lt;/a&gt;&#xA;&lt;a href=&#34;http://ecology.msu.montana.edu/labdsv/R/&#34;&gt;http://ecology.msu.montana.edu/labdsv/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://ess.r-project.org/&#34;&gt;http://ess.r-project.org/&lt;/a&gt;&#xA;&lt;a href=&#34;http://factominer.free.fr/&#34;&gt;http://factominer.free.fr/&lt;/a&gt;&#xA;&lt;a href=&#34;http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/R_BioCondManual.htm&#34;&gt;http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/R_BioCondManual.htm&lt;/a&gt;l&#xA;&lt;a href=&#34;http://faculty.washington.edu/tlumley/Rcourse/&#34;&gt;http://faculty.washington.edu/tlumley/Rcourse/&lt;/a&gt;&#xA;&lt;a href=&#34;http://gking.harvard.edu/readme/&#34;&gt;http://gking.harvard.edu/readme/&lt;/a&gt;&#xA;&lt;a href=&#34;http://grass.itc.it/statsgrass/grass_geostats.html&#34;&gt;http://grass.itc.it/statsgrass/grass_geostats.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://greg.weebly.com/1/post/2008/10/programming-in-r-makes-serious-statistics-serious-fun.html&#34;&gt;http://greg.weebly.com/1/post/2008/10/programming-in-r-makes-serious-statistics-serious-fun.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://igraph.sourceforge.net/documentation.html&#34;&gt;http://igraph.sourceforge.net/documentation.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.inside-r.org/&#34;&gt;http://www.inside-r.org/&lt;/a&gt;&#xA;&lt;a href=&#34;http://jblevins.org/computing/r/&#34;&gt;http://jblevins.org/computing/r/&lt;/a&gt;&#xA;&lt;a href=&#34;http://jgr.markushelbig.org/JGR.html&#34;&gt;http://jgr.markushelbig.org/JGR.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://learnr.wordpress.com/&#34;&gt;http://learnr.wordpress.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://lethain.com/&#34;&gt;http://lethain.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://math.acadiau.ca/ACMMaC/Rmpi/examples.html&#34;&gt;http://math.acadiau.ca/ACMMaC/Rmpi/examples.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://mathesaurus.sourceforge.net/&#34;&gt;http://mathesaurus.sourceforge.net/&lt;/a&gt;&#xA;&lt;a href=&#34;http://mathstat.helsinki.fi/openbugs/&#34;&gt;http://mathstat.helsinki.fi/openbugs/&lt;/a&gt;&#xA;&lt;a href=&#34;http://mccammon.ucsd.edu/~bgrant/bio3d/index.html&#34;&gt;http://mccammon.ucsd.edu/~bgrant/bio3d/index.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://people.biology.ufl.edu/bolker/emdbook/&#34;&gt;http://people.biology.ufl.edu/bolker/emdbook/&lt;/a&gt;&#xA;&lt;a href=&#34;http://personality-project.org/r/&#34;&gt;http://personality-project.org/r/&lt;/a&gt;&#xA;&lt;a href=&#34;http://pj.freefaculty.org/R/Rtips.html&#34;&gt;http://pj.freefaculty.org/R/Rtips.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://pj.freefaculty.org/R/statsRus.html&#34;&gt;http://pj.freefaculty.org/R/statsRus.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://planatscher.net/svmtut/svmtut.html&#34;&gt;http://planatscher.net/svmtut/svmtut.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://processtrends.com/Learn_R_Toolkit.htm&#34;&gt;http://processtrends.com/Learn_R_Toolkit.htm&lt;/a&gt;&#xA;&lt;a href=&#34;http://rocr.bioinf.mpi-sb.mpg.de/&#34;&gt;http://rocr.bioinf.mpi-sb.mpg.de/&lt;/a&gt;&#xA;&lt;a href=&#34;http://rosuda.org/JRI/&#34;&gt;http://rosuda.org/JRI/&lt;/a&gt;&#xA;&lt;a href=&#34;http://rpy.sourceforge.net/&#34;&gt;http://rpy.sourceforge.net/&lt;/a&gt;&#xA;&lt;a href=&#34;http://ryouready.wordpress.com/2009/03/16/r-monitor-function-progress-with-a-progress-bar/&#34;&gt;http://ryouready.wordpress.com/2009/03/16/r-monitor-function-progress-with-a-progress-bar/&lt;/a&gt;&#xA;&lt;a href=&#34;http://socserv.mcmaster.ca/jfox/&#34;&gt;http://socserv.mcmaster.ca/jfox/&lt;/a&gt;&#xA;&lt;a href=&#34;http://socserv.socsci.mcmaster.ca/jfox/Courses/R-course/index.html&#34;&gt;http://socserv.socsci.mcmaster.ca/jfox/Courses/R-course/index.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://stats.math.uni-augsburg.de/Mondrian/&#34;&gt;http://stats.math.uni-augsburg.de/Mondrian/&lt;/a&gt;&#xA;&lt;a href=&#34;http://tables2graphs.com/doku.php&#34;&gt;http://tables2graphs.com/doku.php&lt;/a&gt;&#xA;&lt;a href=&#34;http://tolstoy.newcastle.edu.au/R/&#34;&gt;http://tolstoy.newcastle.edu.au/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://users.fmg.uva.nl/rgrasman/rpages/2005/09/error-bars-in-plots.html&#34;&gt;http://users.fmg.uva.nl/rgrasman/rpages/2005/09/error-bars-in-plots.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://wiener.math.csi.cuny.edu/Statistics/R/simpleR/&#34;&gt;http://wiener.math.csi.cuny.edu/Statistics/R/simpleR/&lt;/a&gt;&#xA;&lt;a href=&#34;http://wiki.math.yorku.ca/index.php/R:_Getting_started&#34;&gt;http://wiki.math.yorku.ca/index.php/R:_Getting_started&lt;/a&gt;&#xA;&lt;a href=&#34;http://wiki.services.openoffice.org/wiki/R_and_Calc&#34;&gt;http://wiki.services.openoffice.org/wiki/R_and_Calc&lt;/a&gt;&#xA;&lt;a href=&#34;http://wiki.stdout.org/rcookbook/&#34;&gt;http://wiki.stdout.org/rcookbook/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.agr.kuleuven.ac.be/vakken/statisticsbyR/index.htm&#34;&gt;http://www.agr.kuleuven.ac.be/vakken/statisticsbyR/index.htm&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.ai.rug.nl/~hedderik/R/US2004/&#34;&gt;http://www.ai.rug.nl/~hedderik/R/US2004/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.ats.ucla.edu/stat/R/&#34;&gt;http://www.ats.ucla.edu/stat/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.bioconductor.org/&#34;&gt;http://www.bioconductor.org/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.burns-stat.com/&#34;&gt;http://www.burns-stat.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.cerebralmastication.com/r-resources/&#34;&gt;http://www.cerebralmastication.com/r-resources/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.ci.tuwien.ac.at/gR/&#34;&gt;http://www.ci.tuwien.ac.at/gR/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.cl.cam.ac.uk/~sjm217/projects/graphics/&#34;&gt;http://www.cl.cam.ac.uk/~sjm217/projects/graphics/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.cyclismo.org/tutorial/R/&#34;&gt;http://www.cyclismo.org/tutorial/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.daimi.au.dk/~besen/TBiB2007/lecture-notes/rpy.html&#34;&gt;http://www.daimi.au.dk/~besen/TBiB2007/lecture-notes/rpy.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.decisionsciencenews.com/?p=261&#34;&gt;http://www.decisionsciencenews.com/?p=261&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.decisionsciencenews.com/?p=264&#34;&gt;http://www.decisionsciencenews.com/?p=264&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.drewconway.com/zia/?p=1221&#34;&gt;http://www.drewconway.com/zia/?p=1221&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.econ.uiuc.edu/~econ472/e-Tutorial.html&#34;&gt;http://www.econ.uiuc.edu/~econ472/e-Tutorial.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.faculty.ucr.edu/~tgirke/Documents/R_BioCond/R_Programming.html&#34;&gt;http://www.faculty.ucr.edu/~tgirke/Documents/R_BioCond/R_Programming.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.harding.edu/fmccown/R/&#34;&gt;http://www.harding.edu/fmccown/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.ibm.com/developerworks/linux/library/l-r1/&#34;&gt;http://www.ibm.com/developerworks/linux/library/l-r1/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.johndcook.com/R_language_for_programmers.html&#34;&gt;http://www.johndcook.com/R_language_for_programmers.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.liaad.up.pt/~ltorgo/DataMiningWithR/&#34;&gt;http://www.liaad.up.pt/~ltorgo/DataMiningWithR/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.math.montana.edu/Rweb/Rnotes/R.html&#34;&gt;http://www.math.montana.edu/Rweb/Rnotes/R.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.math.umass.edu/~lavine/Book/book.html&#34;&gt;http://www.math.umass.edu/~lavine/Book/book.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.math.yorku.ca/SCS/Gallery/&#34;&gt;http://www.math.yorku.ca/SCS/Gallery/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.maths.bath.ac.uk/~jjf23/book/&#34;&gt;http://www.maths.bath.ac.uk/~jjf23/book/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.mayin.org/ajayshah/KB/R/&#34;&gt;http://www.mayin.org/ajayshah/KB/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.nettakeaway.com/tp/?s=R&#34;&gt;http://www.nettakeaway.com/tp/?s=R&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.nickfieller.staff.shef.ac.uk/sheff-only/statmod.html&#34;&gt;http://www.nickfieller.staff.shef.ac.uk/sheff-only/statmod.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?_r=1&#34;&gt;http://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?_r=1&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.omegahat.org&#34;&gt;http://www.omegahat.org&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.personality-project.org/r/&#34;&gt;http://www.personality-project.org/r/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.personality-project.org/r/r.anova.html&#34;&gt;http://www.personality-project.org/r/r.anova.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.phaget4.org/R/image_matrix.html&#34;&gt;http://www.phaget4.org/R/image_matrix.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.phaget4.org/R/R_notes.html&#34;&gt;http://www.phaget4.org/R/R_notes.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.psych.upenn.edu/~baron/rpsych/rpsych.html&#34;&gt;http://www.psych.upenn.edu/~baron/rpsych/rpsych.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.r-bloggers.com/&#34;&gt;http://www.r-bloggers.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.rdatamining.com/&#34;&gt;http://www.r-cookbook.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.rdatamining.com/&#34;&gt;http://www.rdatamining.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.revolution-computing.com/&#34;&gt;http://www.revolution-computing.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.rosuda.org/iplots/&#34;&gt;http://www.rosuda.org/iplots/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.rparallel.org/examples.php&#34;&gt;http://www.rparallel.org/examples.php&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.santafe.edu/~aaronc/powerlaws/&#34;&gt;http://www.santafe.edu/~aaronc/powerlaws/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.splusbook.com/Rintro/RCourseMaterial.html&#34;&gt;http://www.sfu.ca/~sblay/R/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.splusbook.com/Rintro/RCourseMaterial.html&#34;&gt;http://www.splusbook.com/Rintro/RCourseMaterial.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html&#34;&gt;http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.columbia.edu/~gelman/bugsR/&#34;&gt;http://www.stat.columbia.edu/~gelman/bugsR/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.pitt.edu/stoffer/tsa2/&#34;&gt;http://www.stat.pitt.edu/stoffer/tsa2/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.tamu.edu/~ljin/Finance/stat689-R.htm&#34;&gt;http://www.stat.tamu.edu/~ljin/Finance/stat689-R.htm&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.ucl.ac.be/ISdidactique/Rhelp/&#34;&gt;http://www.stat.ucl.ac.be/ISdidactique/Rhelp/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.ufl.edu/~aa/cda/cda.html&#34;&gt;http://www.stat.ufl.edu/~aa/cda/cda.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.umn.edu/alr/R.html&#34;&gt;http://www.stat.umn.edu/alr/R.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.umn.edu/geyer/5102/examp/reg.html#predplot&#34;&gt;http://www.stat.umn.edu/geyer/5102/examp/reg.html#predplot&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stat.wisc.edu/~deepayan/SIBS2005/demos/trellis-graphics.html&#34;&gt;http://www.stat.wisc.edu/~deepayan/SIBS2005/demos/trellis-graphics.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.statistik.lmu.de/~leisch/Sweave/&#34;&gt;http://www.statistik.lmu.de/~leisch/Sweave/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.statmethods.net/index.html&#34;&gt;http://www.statmethods.net/index.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.stats.ox.ac.uk/pub/MASS4/&#34;&gt;http://www.stats.ox.ac.uk/pub/MASS4/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.statslab.cam.ac.uk/~pat/&#34;&gt;http://www.statslab.cam.ac.uk/~pat/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.student.uit.no/~felsted/rtips/rtips_html/rtips.html&#34;&gt;http://www.student.uit.no/~felsted/rtips/rtips_html/rtips.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.theresearchkitchen.com/blog/archives/category/project-euler&#34;&gt;http://www.theresearchkitchen.com/blog/archives/category/project-euler&lt;/a&gt;&#xA;&lt;a href=&#34;http://www.togaware.com/&#34;&gt;http://www.togaware.com/&lt;/a&gt;&#xA;&lt;a href=&#34;http://www1.maths.lth.se/help/R/RCC/&#34;&gt;http://www1.maths.lth.se/help/R/RCC/&lt;/a&gt;&#xA;[http://www2.warwick.ac.uk/fac/sci/moac/degrees/modules/ch923/r_introduction/histograms_etc/ http://wwwmaths.anu.edu.au/~johnm/](&lt;a href=&#34;http://www2.warwick.ac.uk/fac/sci/moac/degrees/modules/ch923/r_introduction/histograms_etc/&#34;&gt;http://www2.warwick.ac.uk/fac/sci/moac/degrees/modules/ch923/r_introduction/histograms_etc/&lt;/a&gt; &lt;a href=&#34;http://wwwmaths.anu.edu.au/~johnm/&#34;&gt;http://wwwmaths.anu.edu.au/~johnm/&lt;/a&gt;)&#xA;&lt;a href=&#34;http://www-stat.stanford.edu/~jhf/R-MART.html&#34;&gt;http://www-stat.stanford.edu/~jhf/R-MART.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://xweb.geos.ed.ac.uk/~hcp/r_notes/r_notes.html&#34;&gt;http://xweb.geos.ed.ac.uk/~hcp/r_notes/r_notes.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://zoonek.free.fr/blosxom/R/2006-08-27_The_Grammar_of_Graphics.html&#34;&gt;http://zoonek.free.fr/blosxom/R/2006-08-27_The_Grammar_of_Graphics.html&lt;/a&gt;&#xA;&lt;a href=&#34;http://zoonek2.free.fr/UNIX/48_R/all.html&#34;&gt;http://zoonek2.free.fr/UNIX/48_R/all.html&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>读柯文《在中国发现历史》</title>
      <link>http://gewutang.cn/2012/02/17/find-history-on-china/</link>
      <pubDate>Fri, 17 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/17/find-history-on-china/</guid>
      <description>&lt;p&gt;保守派惧怕任何改革，温和派则深恐改革会加强他们所厌恶的康有为一派人物的政治影响。整个官僚体制中，许多人也许并不反对改革本身，但唯恐某项改革会危及他们的仕途。&lt;/p&gt;&#xA;&lt;p&gt;总而言之，戊戌维新的志士如果说善意有余，则实现这种善意所必备的实际政治头脑和经验却极端不足。&lt;/p&gt;&#xA;&lt;p&gt;这一点在目前的情形下依然值得借鉴。其既说明了改革所面对的各种利益之纠纷，也说明了优秀改革派所必须具备之条件：改革的善意、实际的政治头脑以及充足的改革经验。&lt;/p&gt;</description>
    </item>
    <item>
      <title>伊塔洛·卡尔维诺的《孤独》真是荒诞的黑色幽默</title>
      <link>http://gewutang.cn/2012/02/09/italo-calvino-lonely/</link>
      <pubDate>Thu, 09 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/09/italo-calvino-lonely/</guid>
      <description>&lt;p&gt;在一些瞬间感觉十分无聊，也没有心思看书。在这心慌意乱的时候，姗姗发来了一篇小说——卡尔维诺的《孤独》——给我消遣。&lt;/p&gt;&#xA;&lt;p&gt;《孤独》的故事很简单：&lt;/p&gt;&#xA;&lt;p&gt;主人公在某个天黑风高的夜晚独自晃荡，忽然发现一伙窃贼在行凶，就打算入伙分一杯羹。显然，这一刻他脑袋中的邪恶小人战胜了脑袋中正义小人。后来，他被窃贼打发到外面去放风，在遇到巡逻的警察之后便举报了那一伙窃贼。窃贼被发现之后，奋力逃走，主人公跟着逃走，不料奔跑速度太慢，竟混入了警察和群众的队伍，后来又跑太快又加入了窃贼的队伍……最终终于落单，他又开始孤独的晃荡，而先前的一切像从来没有发生过。&lt;/p&gt;&#xA;&lt;p&gt;真是一出荒诞的黑色幽默。或许无法融入任何群体是天下最深的孤独。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R是一种生活，数据是一种态度</title>
      <link>http://gewutang.cn/2012/02/08/r-is-a-kind-of-life-and-data-is-an-attitude/</link>
      <pubDate>Wed, 08 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/08/r-is-a-kind-of-life-and-data-is-an-attitude/</guid>
      <description>&lt;p&gt;有多少人听说过R软件？有多少人用过R软件？你现在最常用的软件是什么？SAS？SPSS？EViews?如果你是一个经常和数据分析打交道的人，或者从事金融建模，数学建模的人员，还不用R，你就有点out了。如果现在不学R，五年后你没有任何优势可言；如果5年后，你还不学R，那你差不多就可以被淘汰了。&lt;/p&gt;&#xA;&lt;p&gt;接下来，我先介绍一下R软件，然后，会详细的讲解为什么你不用R就会out。&lt;/p&gt;&#xA;&lt;p&gt;R是有Ross Ihaka跟Robert一起开发的一个面向对象的编程语言。什么是面向对象呢？就像现在，我在这里演讲，直接看见你们，你们就是我面向的对象，当然，这是开玩笑。面向对象的意思就是，R里面的一切东西都是视为对象（Object）。无论是数据框、列表还是函数或者环境，这里先不讲，免得把大家吓跑了。&lt;/p&gt;&#xA;&lt;p&gt;R语言的前身是S语言，这个语言一度是数据分析领域里面的标准语言。当然，现在也是，但是正在逐步被R语言取代。S语言是由贝尔实验室开发的，贝尔实验室开发了很多经典的东西，S是其中之一。S语言的缺点是它不是免费的，而R最大的优点在于R是免费的。&lt;/p&gt;&#xA;&lt;p&gt;现在很多人用SPSS，SAS,MatLab，但是用的都是盗版软件。从法律上来讲这是非法的，但是非法的东西在中国很流行，很多教授也用这些非法软件，并且还帮着同学去获取盗版软件，其实，这不太可取。当然，在感情上我予以充分理解。&lt;/p&gt;&#xA;&lt;p&gt;上次去上海参加第4届R会议，来了一个嘉宾是新西兰人，新西兰是R的故乡，中间熊熹提到她复印了一本ggplot2的书，被外国人看到，外国人就问她征求过版权问题吗？在外国人看来这是很严肃的事情。当然，这是题外话。&lt;/p&gt;&#xA;&lt;p&gt;免费是R很大的优点，但不是唯一的。R的最大优点是出色的作图功能、丰富的统计学方法以及超快的更新速度。下面讲一下R的各种功能。&lt;/p&gt;&#xA;&lt;p&gt;首先，R可以作为计算器使用，例如（屏幕演示）。其次，出色的绘图功能，例如（屏幕演示，直方图，小提琴图），还可以绘制一些高级的图形，向日葵图，玫瑰花瓣图，脸谱图。当然，这是最基本的功能。虽然很基本，SPSS这些软件是不可能做到这么完美的。R的画图分四个层级，最基本的就是刚才演示的。高级一点的是grid，然后是，Lattice，Lattice的图形是这样的（屏幕演示），很适合做多元数据展示。grid是这样的（屏幕演示），grid图形引入了图层概念，并且可以切换图形的视角。举个例子：（A4纸演示），接下来是ggpolt2：ggplot2是一个更具有创新性的绘图设备，它创建了自己的语法，相当于R里面有创建了一门新的绘图语言。ggplot2的功能是这样的（屏幕演示）。最后，比较终极的绘图BOSS是ggobi，这是一个动态的绘图软件，交互性很强，不过可能很快要被谢益辉的cecran给取代了。后者是前者的加强版。&lt;/p&gt;&#xA;&lt;p&gt;然后，就是R建模。R的建模能力超强。建立线性模型，只需要用lm函数就可以了，广义线性模型glm就可以了，广义可加模型gam就可以了。&lt;/p&gt;&#xA;&lt;p&gt;然后是，时间序列分析。这个很有用的。我觉得，对于本科生也好，研究生也好，想基于一元或者多元回归做一个比较上层次的论文是困难的。但是，用时间序列分析是可以的，可以用arima，说到这里很多人就笑了，arima这个我知道，终于听到一个自己知道的模型了，其次是garch模型。我知道很多人做garch模型用EViews，但是这个软件太老了，极其不智能，稍微高级的方法都用不了。加一个外生变量要折腾半天。而且最重要的是你用的是盗版软件，话说过来，即使是正版的Eviews也不智能。R里面做garch模型就很简单，用rgarch包就可以了，可以构建各种高级的garch模型，tgarch，igarch，gjrgarch之类的，还有高级的Dccgarch，可能你又听不懂了，还有更高阶的BEKK模型，BEKK没什么神秘的，其实就是多元GARCH模型，就是把GARCH模型从一维推广到了二维了。还有更高阶的，因为参数估计起来太麻烦，所以应用比较少。&lt;/p&gt;&#xA;&lt;p&gt;Rgarch包还能计算VaR。也就是Value at Risk。风险价值。这个东西很高级，没有听过吧？其实，我认为上了大三的人就应该很熟悉VaR。但是很多人不知道，因为中国的教育不太成功，我们叫不成功，不叫失败，免得打击教育部。高校的老师，怎么说呢，比较善于推己及人。他们当年读大三的时候，就不懂得VaR，他们复习半年，考上研究生的时候也不怎么懂。所以他们认为别的本科生也不懂，或者说没有能力懂。他们对中国的学生太没有信心了。其实，VaR的概念并不是很难。（演示）&lt;/p&gt;&#xA;&lt;p&gt;这就是VaR，其实就是分位数。分位数其实就是对密度函数求变上限的积分，求变上限的积分都会吧？不过，也不用手动求，用R就可以了。&lt;/p&gt;&#xA;&lt;p&gt;理论上讲，只要知道了密度函数，求积分不在话下。所以求密度函数其实是VaR的核心。根据求VaR密度函数的方法的不同，可以将它分成三种重用的方法，历史模拟法，蒙特卡洛模拟法，方差-协方差法。历史模拟法就是用历史数据的密度函数代替未来的密度函数，蒙特卡洛模拟法就是假设一个分布，模拟一批随机数。说到随机数，想起来本科期间一件事情，当时精算专业要生成随机数，一批学生不知道怎么做，在Excel里面瞎折腾。我就想不明白，为什么不用R呢，产生正态分布的随机数rnorm(5)就可以了，产生5万个，rnorm(50000)，在Excel里面，折腾死你。&lt;/p&gt;&#xA;&lt;p&gt;蒙特卡洛模拟完随机数，就根据这一批样本来计算VaR。方差-协方差呢，跟这个不同，它求的是条件密度函数，理论上讲应该更准确。历史模拟法对历史区间的选择很敏感；蒙特卡洛对分布的假设很敏感，正态分布假设和t分布假设结果相差很大；方差-协方差对厚尾与否很敏感。&lt;/p&gt;&#xA;&lt;p&gt;如果是单个资产的话，像上面这样求分位数就搞定了。资产组合的时候，要知道联合分布，才能求分位数。联合分布怎么求，最新的方法是Copula。简单而言就是个复合函数：假设你有联合分布函数F(x1,x2,x3,…)，有边际分布，f(x1),f(x2),f(x3)…要找到要个函数H（·）使得F(x1,x2,x3,…)=H（f(x1),f(x2),f(x2)…）。&lt;/p&gt;&#xA;&lt;p&gt;这个函数就是Copula。这是工作原理，操作起来有一系列复杂的技术。当然，求解这个过程计算机会帮忙的，不需要手算。君子善假于物也。&lt;/p&gt;&#xA;&lt;p&gt;本科生的时候做过一个项目是关于VaR的。当时学校的一个老师提出过质疑，因为VaR的最初定义是是关于头寸的。而R里面是基于收益率的。两者在测量风险方面没什么差异。但是，他非得坚持算头寸。答辩的时候，我跟他讲这是很容易换算的，他问怎么容易。我就解释给他，当时年少无知，连续向他发问，我估计他很久没有看最新的VaR文献了，因此，我质疑的问题，他一个都答不上来，后面听答辩的同学，开始欢呼，掌声雷动。我很了解同龄人，大家都喜欢看见权威被打倒。这是潜在的暴力倾向，我很不喜欢。值得注意的是，刘姝威老师也在下面拍手。&lt;/p&gt;&#xA;&lt;p&gt;现在想起来当时太冲动了，事后我试图向那个老师道歉。当然，不是因为我用错了方法，只是因为我用错了态度。我觉得一个人还是应该尊重另一个人的。&lt;/p&gt;&#xA;&lt;p&gt;刘姝威老师后来说，本科生能做出来这个VaR是很不容易的。我当时心里很舒服，得到了赞赏。后来想想，这个赞赏的代价很大。VaR明明很容易计算，她却因为这个儿称赞我，那不是在贬低其他所有的不会算VaR的同学吗？这是我后来想明白的。&lt;/p&gt;&#xA;&lt;p&gt;R还能做极值理论。今年上海交大的一个研究生问我用R作极值理论的东西，我就帮他说了一下，他说帮了他大忙。说要感谢我，我说不用，在论文后面，鸣谢一下就可以了。其实，我心里一直在怀疑，研究生要毕业了，极值理论都还不懂，研究生到底读的是神马？更进一步，这样的人为什么能踏过研究生的门槛，问什么能考上研究生呢？中国的研究生录取机制有问题。&lt;/p&gt;&#xA;&lt;p&gt;再之外，R在数据挖掘领域是顶呱呱的。里面有各种方法，比如随机森林，支持向量机，Lasso等。什么是Lasso，也许你说，那是lasso，我跟你说，你说错了，这个单词的重音在第二个。关于这方面，刘思喆是专家，你们可以到论坛上或者微博上咨询他。&lt;/p&gt;&#xA;&lt;p&gt;R能做的东西很多。那有没有R不能做到的事情？让我想一想，好像还真有。比如，R好像不能下电影，看电影，听音乐。不过，R里面却是提供了很多高级的工具，它提供了一个hook，可以启动电脑中的其他程序，比如迅雷，可以自动下载网页附件，自动解压缩，读入数据等等。很多R粉丝甚至用R关机，用R发微博，发校内状态。R能给人很多惊喜，比如用R下五子棋，扫雷，玩儿递归汉诺塔。做贺卡，马赛克图，这里面是什么知道吗？是AV女优，不信你看看，是不是发现了很多熟人？&lt;/p&gt;&#xA;&lt;p&gt;话说回来，很长一段时间里。SPSS,SAS,Matlab还会继续存在，不会骤然消失。我甚至认为这些软件不会消失。因为他还是有自己的受众的。你不能要求所有人的都学习R编程。&lt;/p&gt;&#xA;&lt;p&gt;这些软件也有些优点，我最喜欢Matlab，他跟R有点像。好吧R跟matlab有点像，免得伤害Matlab user的感情。曾经有一个人发了一段代码，让用R实现。我不知道那人是什么动机。是想看看R能不能做到，还是想看看COS有没有人会。很不好意思，我两者都会一点，于是我就用R重现了那个过程，代码极其简单。后来，那人说，太牛了，这都能做到。其实，我想说，这不算什么。真的。牛人都在做更重要的事情。&lt;/p&gt;&#xA;&lt;p&gt;我希望将来随着时日推移、随着SPSS，SAS，Matlab这一批老人家的辞世，R能快速的成长起来，老人家不愿意学编程，这一点我是很理解的，但是，他们阻碍了R的崛起，R将来势必是一种潮流。&lt;/p&gt;&#xA;&lt;p&gt;目前，市场上流行一个观点。即数据越便宜，数据分析技术越昂贵。我早就意识到了这个问题，目前中国获取数据很难，大家都把数据当资源来买。国外就是不一样，国外开放很多数据，因为国外认为，数据里面的信息才是资源。我把数据源放开，你有本事就从里面寻找信息吧。所以，国外分析数据的人就很贵。&lt;/p&gt;&#xA;&lt;p&gt;将来，中国的数据提供商肯定会转型，会开始搞咨询，搞分析，而不是单纯的卖数据。卖数据没有前途。他们不卖数据了，数据分析师就会开始值钱了。这一天，我相信很快就到来了。&lt;/p&gt;&#xA;&lt;p&gt;R这么好，如何学习R呢?我推荐一些材料给大家。一个是Rforbeginner，R导论。看完之后，看一下Rin a Nutshell，接着看The R book或者Statistics with R。这些资源基本都是开放的。&lt;/p&gt;&#xA;&lt;p&gt;你们也可以跟着学校的老师学，如果有用R的老师的话，实在不行就靠自己自学，来论坛问也是可以的。&lt;/p&gt;&#xA;&lt;p&gt;总之，R是一片广阔的海域，你认为自己有理想，就放马过来吧。加入时代的浪潮，Come on!最后一点希望，希望在做的各位，将来毕业时都能够写出一篇漂亮的论文，不要抄袭！&lt;/p&gt;&#xA;&lt;p&gt;祝各位身体健康，天天快乐，谢谢！&lt;/p&gt;</description>
    </item>
    <item>
      <title>quantmod:R中的金融分析包（学习笔记）</title>
      <link>http://gewutang.cn/2012/02/06/notes-on-quantmod-2/</link>
      <pubDate>Mon, 06 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/06/notes-on-quantmod-2/</guid>
      <description>&lt;p&gt;很久之前随手写了一份quantmod学习笔记，本来只是随手涂鸦，后来竟然出乎意料地广泛流传。因为那个册子格式混乱，结构也没有筹划，因此一直想着重新写一份。目前还没写完，但已经大致可以看了，先发出来，共需要的人来看，也便于大家提些意见。&lt;/p&gt;&#xA;&lt;p&gt;下载：&lt;a href=&#34;https://github.com/dengyishuo/Notes/tree/master/quantmod&#34;&gt;notes on quantmod&lt;/a&gt;。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R与金融时间序列分析常见问题集</title>
      <link>http://gewutang.cn/2012/02/03/r-and-questions-in-financial-timeseries-analysing/</link>
      <pubDate>Fri, 03 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/03/r-and-questions-in-financial-timeseries-analysing/</guid>
      <description>&lt;p&gt;大概四、五个月前，心血来潮打算总结一下COS论坛上关于金融时间序列分析上的常见问题，期许能够形一个集子以方便有需求者查询使用。因先前从未写过书籍，一时难以把握集子的结构便犹豫了很久。后来，无意中发现了《A Discussion of Time Series Objects for R in Finance》这本册子，读罢便决定以该册子的结构为蓝本来总结金融时间序列分析的常见问题集。问题集结构如下:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;基础篇&#xA;&lt;ul&gt;&#xA;&lt;li&gt;时间序列对象创建&lt;/li&gt;&#xA;&lt;li&gt;时间戳操作&lt;/li&gt;&#xA;&lt;li&gt;取子集操作&lt;/li&gt;&#xA;&lt;li&gt;缺失值处理&lt;/li&gt;&#xA;&lt;li&gt;操作效率度量等&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;技术篇&#xA;&lt;ul&gt;&#xA;&lt;li&gt;R与ARIMA模型&lt;/li&gt;&#xA;&lt;li&gt;GARCH族模型&lt;/li&gt;&#xA;&lt;li&gt;VaR（Value at Risk）模型&lt;/li&gt;&#xA;&lt;li&gt;极值理论模型等&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;目前，基础篇大致完成，技术篇尚未完善，而已完成部分中定有不少错漏之处，希望发现错误的童鞋们能够及时批评指正。此外，本问题集由王晓伟、熊熹、刘剑锋、杨羽提供初稿，由我最终润色完成。感兴趣的人可以在点击&lt;a href=&#34;https://github.com/dengyishuo/dengyishuo.github.com/blob/master/RFinance/FAQ.pdf&#34;&gt;FAQ&lt;/a&gt;下载。&lt;/p&gt;</description>
    </item>
    <item>
      <title>weibo数据开发思路</title>
      <link>http://gewutang.cn/2012/02/01/thoughts-on-weibo/</link>
      <pubDate>Wed, 01 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/01/thoughts-on-weibo/</guid>
      <description>&lt;p&gt;weibo开放了API提供了众多的接口。只是这些接口对于非开发人员而言过于复杂，不利于weibo数据的开发和挖掘。&lt;/p&gt;&#xA;&lt;p&gt;李舰结合weibo的API以及TwitteR包编写了一个叫Rweibo的包，然而Rweibo包的使用方法也相当繁琐。根据自己的需要，我整理了一下weibo数据的开发思路，需要建立的是这样一套东西：&lt;/p&gt;&#xA;&lt;p&gt;应当具有的功能：&lt;/p&gt;&#xA;&lt;p&gt;1.获取用户A在时间t至时间t+k之间发布的所有微博正文文本内容。&lt;/p&gt;&#xA;&lt;p&gt;对应的函数为：weibotext(A,from=t,to=t+k)。&lt;/p&gt;&#xA;&lt;p&gt;该函数有三个参数：分别表示用户ID、起始时间t和截止时间t+k。&lt;/p&gt;&#xA;&lt;p&gt;2.获取用户A发布的某条微博W的被转发次数和被评论次数。&lt;/p&gt;&#xA;&lt;p&gt;3.获取用户A当前的粉丝数量。&lt;/p&gt;&#xA;&lt;p&gt;4.获取用户A当前关注的用户数量。&lt;/p&gt;</description>
    </item>
    <item>
      <title>货币的省际贬值</title>
      <link>http://gewutang.cn/2012/02/01/devaluation-caused-by-different-places/</link>
      <pubDate>Wed, 01 Feb 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/02/01/devaluation-caused-by-different-places/</guid>
      <description>&lt;p&gt;回老家之后发现老家的烧饼是0.5元一个，而北京的烧饼是1元一个。也即，同样的1元钱在河南和在北京的购买力是不一样的，且相差了1倍。倘若将河南和北京各视为一个国家的话，根据购买力平价理论，则两地的汇率应当是1:2。然而，由于河南和北京同属一个国家，从而汇率问题，准确的说货币的购买力不等问题被忽略了。&lt;/p&gt;&#xA;&lt;p&gt;在上述问题被忽略的基础上，富有想象力的人们产生了一种美好的愿望，即在北京挣钱、回老家去花。然而，由于两地之间路途遥远，这是不现实的。但是，这并无碍于人们实现这个美好愿望的心理，你可以将挣来的钱，寄给家里的父母花。&lt;/p&gt;&#xA;&lt;p&gt;对于个体而言，上面的做法是个不错的解决方案，从某种意义上讲，较好的实现了货币的购买力。然而，站在更好的一个角度来看，这是一种比较恶劣的做法。试想，北京的钱被源源不断地被输往河南，而河南的烧饼供给量并不会随之增加，久而久之会出现什么效果？河南的烧饼价格会上涨，如果将烧饼看做河南生产的唯一消费品的话，河南产生了通货膨胀！没错，我相信这是通胀在省际之间转移的重要途径之一。这也就解释了为什么眼下太康县的物价水平跟北京有的一拼。长远来看，这种做法严重损伤了固守在河南工作的人们的利益。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R语言书籍的学习路线图</title>
      <link>http://gewutang.cn/2012/01/19/how-to-learn-r-by-reading-books/</link>
      <pubDate>Thu, 19 Jan 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/01/19/how-to-learn-r-by-reading-books/</guid>
      <description>&lt;p&gt;现在对R感兴趣的人越来越多，很多人都想快速地掌握R。然而，由于目前大部分高校都没有开设R课程，这就导致很多人不知道如何着手学习R。&lt;/p&gt;&#xA;&lt;p&gt;对于初学R，最常见的方式是看看百度上的简介。遇到不会的地方，就跑到论坛上吼一嗓子，然后欣欣然（得到解答）或者悲伤（未得到解答）地离去。一直到遇到下一个问题再回来。当然，这不是最好的学习方式。最好的方式是看书。&lt;/p&gt;&#xA;&lt;p&gt;目前，市面上介绍R语言的书籍已经很多，中英文都有。那么，众多书籍中，一个菜鸟应该从哪一本入门呢？入门之后如何才能把自己练就成某个方面的高手呢？相信这是很多人心中的疑问。有这种疑问的人有福了，因为笔者将根据自己的经历总结一下R书籍的学习路线图以使大家少走些弯路。&lt;/p&gt;&#xA;&lt;p&gt;本文分为6个部分，分别介绍初级入门、高级入门、绘图与可视化、计量经济学、时间序列分析和金融等。&lt;/p&gt;&#xA;&lt;h4 id=&#34;1初级入门&#34;&gt;1.初级入门&lt;/h4&gt;&#xA;&lt;p&gt;&lt;a href=&#34;http://cran.r-project.org/doc/manuals/R-intro.pdf&#34;&gt;《An Introduction to R》&lt;/a&gt;，这是R官网公布的入门小册子，其有中文版，由丁国徽翻译的，译名是&lt;a href=&#34;http://cran.r-project.org/doc/contrib/Ding-R-intro_cn.pdf&#34;&gt;《R导论》&lt;/a&gt;。&lt;a href=&#34;http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf&#34;&gt;《R4Beginners》&lt;/a&gt;，这本小册子有中文版应该叫&lt;a href=&#34;http://www.biosino.org/R/R-doc/files/R4beg_cn_2.0.pdf&#34;&gt;《R入门》&lt;/a&gt;。除此之外，还可以去读&lt;a href=&#34;http://www.bjt.name/&#34;&gt;刘思喆&lt;/a&gt;的&lt;a href=&#34;http://cos.name/cn/topic/7673&#34;&gt;《153分钟学会R》&lt;/a&gt;，对R有一个感性的认识，这个册子收集了R初学者提问频率最高的153个问题。为什么叫153分钟呢？因为最初作者写了153个问题，阅读一个问题花费1分钟时间，全局下来也就是153分钟了。有了这些基础之后，要去读一些经典书籍比较全面的入门书籍，比如《统计建模与R软件》，国外还有《R Cookbook》和《R in action》（中文翻译为《R实战》）等。&lt;/p&gt;&#xA;&lt;p&gt;最后隆重推荐《R in a Nutshell》，直译是“果壳里面的R”。当然，这是开玩笑的，”in a Nutshell“是英文俚语，意思是“简单的说”。很多技术类书籍都有Nutshell版本，这类书籍一般翻译为”**核心技术手册“，目前，我们已经译好这本书的中文版，预计今年会面世。这本书很不错，深度和广度都超越同类书籍，大家可以从现在开始期待，并广而告知一下！&lt;/p&gt;&#xA;&lt;h4 id=&#34;2高级入门&#34;&gt;2.高级入门&lt;/h4&gt;&#xA;&lt;p&gt;读了上述书籍之后，你就可以去高级入门阶段了。这时候要读的书有两本很经典的。《Statistics with R》和《The R book》以及《Modern Statistics with S》。之所以说这三本书高级，是因为这三本书已经不再限于R了，而是结合了数据分析的各种常见方法来写就的，比较系统的介绍了R在线性回归、方差分析、多元统计、R绘图、时间序列分析、数据挖掘等各方面的内容。看完之后你会发现，哇，原来R能做的事情这么多，而且做起来是那么简洁。&lt;/p&gt;&#xA;&lt;p&gt;读到这里已经差不多了，剩下的估计就是你要专门攻读的某个方面内容了。下面大致说一说。&lt;/p&gt;&#xA;&lt;h4 id=&#34;3绘图与可视化&#34;&gt;3.绘图与可视化&lt;/h4&gt;&#xA;&lt;p&gt;亚里斯多德说，“较其他感觉而言，人类更喜欢观看”。因此，绘图和可视化得到很多人的关注和重视。那么，如何学习R画图和数据可视化呢？再简单些，如何画直方图？如何往直方图上添加密度曲线呢？我想读完下面这几本书你就大致会明白了。&lt;/p&gt;&#xA;&lt;p&gt;首先，画图入门可以读《R Graphics》，个人认为这本是比较经典的，全面介绍了R中绘图系统。该书对应的有一个网站，google之就可以了。更深入的可以读《Lattice：Multivariate Data Visualization with R》，Lattice是R中针对多元数据可视化而开发的一套绘图系统。上面这些都是比较普通的。对应的有比较文艺和优雅的ggplot2系统，看《ggplot2：Elegant Graphics for Data Analysis》，中文版是《ggplot2:数据分析与图形艺术》。这本书理论性较强，实战比较弱。与之互补的要推荐《The R Graphics cookbook》，中文对应的是《R图形可视化手册》，译者是邓一硕、魏太云和肖楠，这本书详细列举了所有常见数据分析图形的绘制方法，适合做工具书。再有就是交互式图形的书了，著名的交互系统是ggobi，这个我已经喜欢多年，关于ggobi的书有《Interactive and Dynamic Graphics for Data Analysis With R and GGobi》，不过，也只是适宜入门，更多更全面的还是去ggobi的主页吧，上面有各种资料以及包的更新信息。&lt;/p&gt;&#xA;&lt;p&gt;此外，R还推出了shiny和rChart以及rechart等包，可以将图形绘制到网页上，这个可以说是未来的主流，不得不关注。&lt;/p&gt;&#xA;&lt;p&gt;特别推荐一下，中文版绘图书籍中谢益辉的《现代统计图形》和我们翻译的《R图形可视化手册》。&lt;/p&gt;&#xA;&lt;h4 id=&#34;4计量经济学&#34;&gt;4.计量经济学&lt;/h4&gt;&#xA;&lt;p&gt;关于计量经济学，首先推荐一本很薄的小册子:《Econometrics In R》，做入门用。然后，是《Applied Econometrics with R》，该书对应的R包是AER包，可以安装之后配合使用，效果甚佳。计量经济学中很大一部分是关于时间序列分析的，这一块内容在下面的地方说。&lt;/p&gt;&#xA;&lt;h4 id=&#34;5时间序列分析&#34;&gt;5.时间序列分析&lt;/h4&gt;&#xA;&lt;p&gt;时间序列书籍的书籍分两类，一种是比较普适的书籍，典型的代表是：《Time Series Analysis and Its Applications ：with R examples》。该书介绍了各种时间序列分析的经典方法及实现各种经典方法的R代码，该书有中文版。如果不想买的话，建议去作者主页直接下载，英文版读起来很简单。时间序列分析中有一大块儿是关于金融时间序列分析的。这方面比较流行的书有两本《Analysis of financial time series》，这本书的最初是用的S-plus代码，不过新版已经以R代码为主了。这本书适合有时间序列分析基础和金融基础的人来看，因为书中关于时间序列分析的理论以及各种金融知识讲解的不是特别清楚，将极值理论计算VaR的部分就比较难看懂。另外一个比较有意思的是Rmetrics推出的《TimeSeries FAQ》，这本书是金融时间序列入门的东西，讲的很基础，但是很难懂。对应的中文版有《金融时间序列分析常见问题集》。经济领域的时间序列有一种特殊的情况叫协整，很多人很关注这方面的理论，关心这个的可以看《Analysis of Integrated and Cointegrated Time Series with R》。最后，比较高级的一本书是关于小波分析的，看《Wavelet Methods in Statistics with R》。附加一点，关于时间序列聚类的书籍目前比较少见，是一个处女地，有志之士可以开垦。&lt;/p&gt;</description>
    </item>
    <item>
      <title>规则这件事情</title>
      <link>http://gewutang.cn/2012/01/16/my-comments-on-rules/</link>
      <pubDate>Mon, 16 Jan 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/01/16/my-comments-on-rules/</guid>
      <description>&lt;p&gt;昨天部门进行体育比赛。比赛之前，主办方就担心比赛过程出现不公平，因此，竭尽全力想制定出一个合理的规则，避免有人钻空子。&lt;/p&gt;&#xA;&lt;p&gt;以往的比赛经验表明，常常有一些身强体壮，虎背熊腰的人被多个队邀请当外援。这本身没什么问题，但是，他当多个对的外援就意味着，获奖之后，他可以拿多份奖品，这是群众们不能够忍受的。因此，本次主办方规定一个人只能参加一个队，同时为了将队员名单敲死，主办方要求每个队赛前报出参赛名单，比赛时不准再换，也即是若某队的队员缺席，只能认命！&lt;/p&gt;&#xA;&lt;p&gt;比赛开始，某个队全是女人，另外一个队则有男人助阵。毫无疑问，全是女人的队败下阵来。 接下来，A队和B队比赛，第一场，A队输。于是，A队提出抗议，说自己的队员少了一名，因为该队员临时被部长叫去工作去了。要求B队自己撤下一名队员。B 队不干，B队称，你们可以换上队里的其它队员，A队称没有男队员，只有女队员，所以不愿意换。B队称，人家全是女人的队不也比了，难道跟都是女队的人比赛 的队要全部撤下男队员吗？A队不管，A对便派出几个泼辣的女人上去生拉硬拽B队把绳上的一名队员扯下来，第二场，A对获胜。该场结果引发现场观众强烈不 满，B队抗议，旁观的C队，劝B队不要在意，B队愤怒之下悉数离场。比赛结果A队胜！&lt;/p&gt;&#xA;&lt;p&gt;接下来，A队与C队比赛，要求C队撤下队员，C队看到A队故技重施，自然不爽，提出抗议，C队此时多希望B队能帮自己说话，遗憾的是，B队已经在A队的无赖和C队的冷漠之下黯然离场了，愤愤不平的C队最终罢赛，C队离场！A队大获全胜！&lt;/p&gt;&#xA;&lt;p&gt;我相信这样的事情在中国司空见惯，所有的比赛中都会有践踏规则的无赖，也会有冷漠旁观的 看客。看客在事不关己时冷漠旁观，待到无赖欺负到自己门口时又希望别人伸出援手，最终死在无赖的手中。事后，部门的同事为此愤愤不平很久，同事希望A队的 人下次能够道德觉醒。&lt;/p&gt;&#xA;&lt;p&gt;其实，寄希望于无赖的到的觉醒是没有前途的。唯一能解决问题的就是制定出一个很严格的规则使的规则不要具备任何弹性，比如不允许非参赛人员靠近场地以避免泼妇强制拉下队员的情况等。&lt;/p&gt;&#xA;&lt;p&gt;在国民素质尚未达到足够高度的今天，我们只能想方设法制定一个足够严格的规则，使得无赖没有空子可钻。&lt;/p&gt;</description>
    </item>
    <item>
      <title>为对抗新兴软件R，SAS或对国内高校免费</title>
      <link>http://gewutang.cn/2012/01/14/sas-anti-r/</link>
      <pubDate>Sat, 14 Jan 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/01/14/sas-anti-r/</guid>
      <description>&lt;p&gt;近段时间来，SAS的用户在不断流失，而导致用户流失的原因则在于新兴软件R对其带来的冲击。R软件作为新兴软件，其主要特点是免费、更新快、功能灵活。其中免费对SAS等商业软件的冲击最大。所以，我认为SAS可能免费应该是对R软件免费的响应。&lt;/p&gt;&#xA;&lt;p&gt;一直以来SAS不断丧失高校师生用户。长此以往，等这些学生踏入业界，SAS在业界也难以立足了。SAS对此一直没有太多关注，我猜测是SAS对R的威胁不屑一顾吧。不知道什么原因促使SAS高层突然做出这个决定。&lt;/p&gt;&#xA;&lt;p&gt;这个决定是很英明的。目前，还有相当大部分的人在用SAS软件，SAS一旦免费，由于用户的惯性，势必阻碍用户向R迁移。只要SAS留住了高校用户，等这些学生踏入业界，他们依然会用SAS，虽说SAS对业界还没打算免费，但是学生踏入业界之后，对SAS是否免费可能就不再敏感了，因此，SAS就锁定了一批用户。这真是一个绝妙的策略。那SAS对高校免费会不会丧失什么利润呢？应该不会，因为高校师生本来用的都是破解的SAS，而不是付费的。&lt;/p&gt;&#xA;&lt;p&gt;能让SAS免费也算是R的功德吧！&lt;/p&gt;</description>
    </item>
    <item>
      <title>黄牛党是价格管制体制下的需求调节器</title>
      <link>http://gewutang.cn/2012/01/11/ticket-scalper/</link>
      <pubDate>Wed, 11 Jan 2012 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2012/01/11/ticket-scalper/</guid>
      <description>&lt;p&gt;最近因为回家抢票的事情，我认真思考了一下黄牛党的问题，觉得黄牛党的存在是十分必要的。&lt;/p&gt;&#xA;&lt;p&gt;一直以来，国人把车票难买归根于黄牛党的存在，其中误会深矣。从经济学角度讲，决定车票是否难买取决于两个方面：一是需求方面，即有多少人需要而能购买车票；二是供给方面，即铁道部有多少车票。&lt;/p&gt;&#xA;&lt;p&gt;倘若车票市场是自由市场，则两者总能够达到一个平衡。然而，车票市场不是自由市场，原因是政府对车票设定了价格上限，这一举措是有政治考量的，这是题外话。然而，这一举措，必然导致过高的车票需求，实际上，使得市场没有达到自己的均衡。市场不是白痴，它总会想办法调解这个局面，于是，一个崭新的职业诞生了，那就是黄牛党。“黄牛党”三个字出现以来，一直遭人鄙视。然而，其在经济学上却是有意义的。按照自由市场原理，市场上存在超额需求，必然要导致价格上涨，以在更高的价格水平达到均衡。然而，限价导致车票不能明着涨，那好，黄牛党可以让车票价格暗地里涨，而暗地里涨上去的价格，事实上，有助于构建一个新的均衡点。&lt;/p&gt;&#xA;&lt;p&gt;那些靠自己排队去购买更多车票的人其实并没有什么过错。当然，如果法令规定不许低价买票，高价卖票那黄牛党就有过错了，因为他们违反法令了。现代社会下，所有人都应该遵守法令，除非该法令因为不合理而被废止。&lt;/p&gt;&#xA;&lt;p&gt;我是想说，这样的法令规定也许就是不合理的。当然了，也有一种黄牛党是应该打击的，那就是利用自己的职权的黄牛党。打击这些人的原因，不是因为他们作为黄牛党而倒票，乃在于他们滥用职权。滥用职权谋取私利现代社会最应该命令禁止，并应予以严厉打击的。&lt;/p&gt;&#xA;&lt;p&gt;打击黄牛党并不能很好的解决售票问题。解决售票问题最根本的途径似乎乃在于增加铁路供给，多建设南北铁路大动脉。不过，这又是具有风险的，因为中国人口的南北移动带有明显的季节性，倘若为了缓解季节性的车票压力，而铺设过多的南北铁路，必然导致南北铁路在其余时间入不敷出，最终导致亏损，无论是政府还是市场决策，都是不划算的。&lt;/p&gt;&#xA;&lt;p&gt;因此，多建设南北动脉不能解决根本问题。也就是供给方面似乎无有可为了。那就从需求方面看看吧。车票的需求短期内是绝对刚性的，因为中国人不可能在春节期间都不回家过年。在长期内是可能的，因为到时候中国的文化会发生改变，或许不再过春节了。还有一种可能，就是届时中国的社会发展已经达到了区域平衡，即河南、四川的人不需要跑到北京来工作了，因为当地工业已经发展到了足以吸纳当地劳动力的地步，没有工作地点和家乡的分离，也就不再有过节时候的大量需求了，届时车票也就不成问题了。看来看去，所有问题的解，还在于发展经济，平衡发展经济。&lt;/p&gt;&#xA;&lt;p&gt;如果你能看到这里，恭喜你，你要觉得乏味了。因为你发现，靠，这是一个长期问题啊！长期的解决方案有个什么用啊。是的，长期方案用途很小，大部分人都喜欢短期方案，喜欢立竿见影的策略。那么,来说一说短期吧。&lt;/p&gt;&#xA;&lt;p&gt;短期内，需大于供的局面难以彻底解决。最优化的结果是是的所有的供给有效地分配给需求，不存在重复分配，也不存在迟缓分配。要做到这一点，铁道部已经推出了电话订票和网络订票，我知道他们本意很好，可是，他们真的做的很差。他们应该做一个简单的招标，设奖金500万，召集计算机高手设计一个高规格的订票网站。网站的大致样子可以是这样的：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;进去主页，会让用户选择订票入口。一般推荐选择地理位置最近的入口，比如人在北京进北京入口。同时，网站应该像同花顺或者三国杀那样，显示那个入口的订票压力较大。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;用户选择入口之后，网站跳转。比如北京入口，则跳转到只负责北京地区的服务器上。如果选择河北入口，则跳转到河北的服务器上。这里服务器是并联的。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;调转到北京服务器之后，选择目的地，比如郑州。网站跳转到北京-郑州服务器上。之所以这么设计，是为了防止下面的状况，即北京去南宁的票没有售完，但是因为订北京到郑州的人太多，堵死了北京服务器，从而去南宁的人有票也订不到。这种方案将北京去南宁和北京去郑州的服务器并联。两者互不影响。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;订票开始。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;这貌似也是一个不太短期的方案，那来个更短期的吧。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;在订票电话前面加拨0317不拥堵！&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>R数据分析实例</title>
      <link>http://gewutang.cn/2011/12/23/r-data-analysis-examples/</link>
      <pubDate>Fri, 23 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/23/r-data-analysis-examples/</guid>
      <description>&lt;h2 id=&#34;回归模型regression-models&#34;&gt;回归模型（Regression Models）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;一元回归&lt;/li&gt;&#xA;&lt;li&gt;多元回归&lt;/li&gt;&#xA;&lt;li&gt;非线性回归&lt;/li&gt;&#xA;&lt;li&gt;稳健回归&lt;/li&gt;&#xA;&lt;li&gt;岭回归&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;二分变量及分类模型models-for-binary-and-categorical-outcomes&#34;&gt;二分变量及分类模型（Models for Binary and Categorical Outcomes）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Logistic回归（Logistic Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;确切Logistic回归（Exact Logistic Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;多分类Logistic回归（Multinomial Logistic Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;顺序Logistic回归（Ordinal Logistic Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Probit回归（Probit Regression)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;计数模型count-models&#34;&gt;计数模型（Count Models）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;泊松回归（Poisson Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;负二项式回归（Negative Binomial Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;零膨胀泊松回归（Zero-inflated Poisson Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;零膨胀负二项式回归（Zero-inflated Negative Binomial Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;零截断泊松回归（Zero-truncated Poisson）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;零截断负二项式回归（Zero-truncated Negative Binomial)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;双侧截断回归censored-and-truncated-regression&#34;&gt;双侧截断回归（Censored and Truncated Regression）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Tobit回归（Tobit Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;截断回归（Truncated Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;区间回归（Interval Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;多因素分析multivariate-analysis&#34;&gt;多因素分析（Multivariate Analysis）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;单边多元方差分析（One-way MANOVA）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;判别函数分析（Discriminant Function Analysis）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;典型相关分析（Canonical Correlation Analysis）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;多元回归分析（Multivariate Multiple Regression）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;效能样本量分析power-analysis--sample-size&#34;&gt;效能/样本量分析（Power Analysis / Sample Size）&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;单样本t检验（Single-sample t-test）&lt;/p&gt;</description>
    </item>
    <item>
      <title>从股票代码到公司名</title>
      <link>http://gewutang.cn/2011/12/12/from-symbol-to-company-name/</link>
      <pubDate>Mon, 12 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/12/from-symbol-to-company-name/</guid>
      <description>&lt;p&gt;Ruser也有Ruser的恶习，经常弄的什么事情都想用R来完成：用R玩儿扫雷、用R玩汉诺塔、用R玩儿黑白棋、用R获取股票数据、用R获取财务数据。&lt;/p&gt;&#xA;&lt;p&gt;现在想的是，如果已经知道一个股票代码，比如上交所的000061，如何知道其对应的公司名称呢？如果能找到一个数据库，其包含公司名称、公司股票代码、公司简称就可以完成这样的事情了。&lt;/p&gt;&#xA;&lt;p&gt;最典型的金融数据库有什么呢？google?yahoo? yahoo是可以的。代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)&#xA;paste(getQuote(&amp;quot;000061.ss&amp;quot;, what=yahooQF(&amp;quot;Name&amp;quot;))[,2])&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;返回结果如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;[1] &amp;quot;SSE CORPORATE BON&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>套利与排队</title>
      <link>http://gewutang.cn/2011/12/11/queuing-and-arbitrage/</link>
      <pubDate>Sun, 11 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/11/queuing-and-arbitrage/</guid>
      <description>&lt;p&gt;某次跟同事吃饭遇到排队，便聊到排队这件事情中个体应当如何选择自己的最佳行为。聊到最后愕然发现，“排队现象”与金融是很相像的。两者中最重要的一个概念都是“套利”。&lt;/p&gt;&#xA;&lt;p&gt;为了简便起见，我们虚构一个故事：假如A到一个餐厅，该餐厅有三个窗口，每个窗口都有若干人排队，问A应该去哪个队？&lt;/p&gt;&#xA;&lt;p&gt;正常情况下，A可以任选一队，因为任何一队比其他队都没有任何优势。如果有优势的话，那先前必然已经出现一些人自动转向那个优势队来抵消优势队的优势，这个过程就是典型的“套利”过程。在金融中，“套利”过程最重要的结果是导致了“平价”，在排队中，“平价”表现为最终所有的队伍长度是一样的。&lt;/p&gt;&#xA;&lt;p&gt;严格的分析过程等我有时间，我再写在这里。&lt;/p&gt;&#xA;&lt;p&gt;**备注：**本来今天想写一写函数型数据与货币政策制定之间的关系的，因为弄丢了一个材料，所以只能改天再写。该文章名是《函数型数据分析手段或在政策制定中大有可为》。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R数据分析实例：稳健回归</title>
      <link>http://gewutang.cn/2011/12/09/r-data-analysis-examples-robust-regression/</link>
      <pubDate>Fri, 09 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/09/r-data-analysis-examples-robust-regression/</guid>
      <description>&lt;p&gt;**按语：**当数据含有离群点（Outliar）或者强影响点（influential observation）时，稳健回归（Robust Regression）会比普通最小二乘法(OLS)的表现要更优异。稳健回归也可以用来检测数据中的强影响点。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;提示:&lt;/strong&gt; 本文旨在介绍与稳健回归相关的R命令，因此，并未全面覆盖稳健回归的相关知识，也不涉及数据清洗、数据检测、模型假设和模型诊断等内容。&lt;/p&gt;&#xA;&lt;p&gt;文档内容基于R 2.14.0，最后更新于2011年12月19日。&lt;/p&gt;&#xA;&lt;h4 id=&#34;简介&#34;&gt;简介&lt;/h4&gt;&#xA;&lt;p&gt;介绍几个线性回归（linear regression）中的术语：&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;残差（Residual）&lt;/strong&gt;:基于回归方程的预测值与观测值的差。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;离群点（Outlier）&lt;/strong&gt;:线性回归（linear regression）中的离群点是指对应残差较大的观测值。也就是说，当某个观测值与基于回归方程的预测值相差较大时，该观测值即可视为离群点。 离群点的出现一般是因为样本自身较为特殊或者数据录入错误导致的，当然也可能是其他问题。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;杠杆率（Leverage）&lt;/strong&gt;: 当某个观测值所对应的预测值为极端值时，该观测值称为高杠杆率点。杠杆率衡量的是独立变量对自身均值的偏异程度。高杠杆率的观测值对于回归方程的参数有重大影响。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;影响力点：（Influence）&lt;/strong&gt;: 若某观测值的剔除与否，对回归方程的系数估计有显著相应，则该观测值是具有影响力的，称为影响力点。影响力是高杠杆率和离群情况引起的。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Cook距离（Cook&amp;rsquo;s distance&lt;/strong&gt;）: 综合了杠杆率信息和残差信息的统计量。&lt;/p&gt;&#xA;&lt;p&gt;使用最小二乘回归时，有时候会遇到离群点和高杠杆率点。此时，若认定离群点或者高杠杆率点的出现并非因为数据录入错误或者该该观测值来自另外一个总体的话，使用最小二乘回归会变得很棘手，因为数据分析者因为没有充分的理由剔除离群点和高杠杆率。此时稳健回归是个极佳的替代方案。稳健回归在剔除离群点或者高杠杆率点和保留离群点或高杠杆率点并像最小二乘法那样平等使用各点之间找到了一个折中。其在估计回归参数时，根据观测值的稳健情况对观测值进行赋权。简而言之，稳健回归是加权最小二乘回归，或称文艺最小二乘回归。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;MASS&lt;/strong&gt; 包中的 &lt;strong&gt;rlm&lt;/strong&gt;命令提供了不同形式的稳健回归拟合方式。接下来，以基于Huber方法和bisquare方法下的M估计为例来进行演示。这是两种最为基本的M估计方法。在M估计中，要做的事情是在满足约束 $ \Sigma_{i=1}^{n}w_{i}(y_i-x_{i}^{&amp;rsquo;}b) x_{i}^{&amp;rsquo;} = 0 $时，求出使得 &lt;code&gt;\(\Sigma w_{i}^2e_i^2\)&lt;/code&gt; 最小的参数。&lt;/p&gt;&#xA;&lt;p&gt;由于权重的估计依赖于残差，而残差的估计又反过来依赖于权重，因此需用迭代重复加权最小二乘（ &lt;strong&gt;I&lt;/strong&gt;teratively &lt;strong&gt;R&lt;/strong&gt;eweighted &lt;strong&gt;L&lt;/strong&gt;east &lt;strong&gt;S&lt;/strong&gt;quares ，IRLS）来估计参数。&lt;/p&gt;&#xA;&lt;p&gt;举例，第j次迭代得到的系数矩阵为:$ B_j = \left[X^{&amp;rsquo;}W_{j-1}X-1X]-1X^{&amp;rsquo;}W_{j-1}Y\right]$  ，这里下脚标表示求解过程中的迭代次数，而不是通常的行标或者列标，持续这一过程，直到结果收敛为止。Huber方法下，残差较小的观测值被赋予的权重为1，残差较大的观测值的权重随着残差的增大而递减。而bisquare方法下，所有的非0残差所对应观测值的权重都是递减的。&lt;/p&gt;&#xA;&lt;p&gt;Huber权重函数通过阈值参数 $ k $ 控制对异常值的敏感度：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;w(e) =&#xA;\begin{cases}&#xA;1 &amp;amp; \text{若 } |e| \leq k \&#xA;\frac{k}{|e|} &amp;amp; \text{若 } |e| &amp;gt; k&#xA;\end{cases}&#xA;$$&lt;/p&gt;</description>
    </item>
    <item>
      <title>尊严是人的固定成本</title>
      <link>http://gewutang.cn/2011/12/07/dignity-is-likes-fixed-costs/</link>
      <pubDate>Wed, 07 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/07/dignity-is-likes-fixed-costs/</guid>
      <description>&lt;p&gt;昨天吃午饭的时候，几个人谈到“西单磕头王”都“羡慕”不已。据说这个人在北京拥有四辆车和数套房产。这些财富在我们看来短时间内是可望而不可及的，而他竟然通过“磕头乞讨”就得到了。&lt;/p&gt;&#xA;&lt;p&gt;其中一人说，这些人比我们好多了。我们辛辛苦苦工作，脑力劳动加体力劳动到头来也没人家挣的多。另一个人则持有相反观点，他认为这个人付出挺多的，最重要的是他为此付出了自己的尊严。&lt;/p&gt;&#xA;&lt;p&gt;我在思考一个问题：大部分人不去乞讨的原因是什么？&lt;/p&gt;&#xA;&lt;p&gt;从经济学角度来讲，是因为道德教育导致了我们对自己的尊严估值甚高，高到让我们觉得无论什么东西都不足以与其等值交换。结果大部分人都守着一个无法变现的尊严。而“西单磕头王”所做的事情概括起来有两部分来构成：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;重估自己的尊严。在这过程中，他重估自己的尊严，并且适当的调低了自己对尊严的估值，这样使得世界上有些东西可以与尊严等值交换。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;把重估的尊严进行变现。也就是说，他卖出了自己的尊严来换取金钱，当然还有随之而来的负面评价。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;有了这个基础，我们便可以研究尊严在经济学中到底处于什么地位了。&lt;/p&gt;&#xA;&lt;p&gt;我认为对于任何一个乞讨的人而言，他第一次放下尊严是一个重大的举动。因为从此之后，尊严就成为了他乞讨事业中的“固定成本”。也就说，一旦一个人放下尊严，那么第二次、第三次放下尊严并不会使他付出更大的代价了（从道德角度来讲，一旦一个人决定不要脸，那么已经不可能是他更觉得羞耻了）。&lt;/p&gt;&#xA;&lt;p&gt;这就可以很好的解释为什么有些人愿意乞讨为生了。我们可以从他乞讨的成本和收益来进行分析。&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;乞讨的成本。&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;乞讨的成本分为两部分，即固定成本和变动成本。固定成本就是一个人的尊严，这构成乞讨成本的最大一部分。变动成本则包括乞讨过程中的吃、穿、道具、交通费等东西。&lt;/p&gt;&#xA;&lt;ol start=&#34;2&#34;&gt;&#xA;&lt;li&gt;乞讨的收益。&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;从乞讨过程中得到的金钱和物品。&lt;/p&gt;&#xA;&lt;p&gt;由于尊严是固定成本的最大部分，变动成本在成本中所占比例甚少，再加上一旦开始乞讨，尊严作为固定成本则随即沉没无法收回。而乞讨的收益则是日益增加，由此推出乞讨的平均成本是日益递减的。那么，作为一个理性人，乞讨者没有理由自动终结乞讨过程。&lt;/p&gt;</description>
    </item>
    <item>
      <title>成熟不是一件急迫的事情</title>
      <link>http://gewutang.cn/2011/12/07/cause-maturity-is-not-an-urgent-matter/</link>
      <pubDate>Wed, 07 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/07/cause-maturity-is-not-an-urgent-matter/</guid>
      <description>&lt;p&gt;昨天跟姗姗聊天时说起“学生气”这种东西，然后一直都有一段话萦绕耳旁，晚上就敲下来了。&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;我觉得我应该珍惜你的“学生气”，因为“学生气”这种东西跟“幼稚”这种东西是一样的，总有一天会消失，而且永远不会再回来。我迟早会看到一个成熟稳重的你，因此，我 完全不用心急。我应该珍惜现在的你以及现在的感觉，并让它自然保持成长。终有一天我们会明白，成熟不是一件急迫的事情，它像月季开花一样自然，而“幼稚”则像第一朵 花蕾，值得人万分珍惜。愿老天眷顾脚下这座城市，因为我们生活在这里，我爱你们，晚安！&lt;/p&gt;&lt;/blockquote&gt;</description>
    </item>
    <item>
      <title>烟火众生</title>
      <link>http://gewutang.cn/2011/12/05/the-girls-we-loved-before/</link>
      <pubDate>Mon, 05 Dec 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/12/05/the-girls-we-loved-before/</guid>
      <description>&lt;p&gt;每个时代，都有一个梦想让人们为之风靡。罗曼·罗兰&lt;/p&gt;&#xA;&lt;p&gt;北京今天大雾，有点风雨如晦的感觉。这导致我直接比平时起晚了5-10分钟，而这关键的5-10分钟导致我差点迟到。打开电脑，弹出的新闻说，北京今天的PM2.5指数爆表了，我回头看看外面的天色，顿时觉得心中不快。心中不快的时候，最适合做两件事情：睡觉和读小说。&lt;/p&gt;&#xA;&lt;p&gt;所以在忙碌的间隙，我搜索了一下现下最流行的小说：九把刀、《那些年，我们一起追的女孩》。书名很一般，我猜测这是一个青春系的煽情小说。我打算读一读，因为我想知道到底是什么东西导致了这本拥有普通名字的小说能够在年轻人之间如此风靡。&lt;/p&gt;&#xA;&lt;p&gt;我的动机很纯洁，不过当我看到三分之一处，发现自己的动机已经被丢到九霄云外了。我发现自己竟然被小说的情节吸引了，小说的情节让我想起了自己的高中时代，那些风风火火的日子，也一样有些风花雪月，各种浪漫幻想，当然最重要的是我想起来那些一同走过高中岁月的人儿。我想着，等到以后，有谁来跟我一起回忆呢？蓦然就伤感了！我一直觉得回忆这件事将占据老年人的大部分时间！不过，等我老了我肯定不会花费大量时间来回忆的，因为到时候我肯定在周游列国。&lt;/p&gt;&#xA;&lt;p&gt;当我看到李小华改名换姓、跟柯景腾假装不相识的时候，心里惆怅难平（这种惆怅的情绪，让我蓦然觉得自己还年轻！我还年轻呵！年轻多好！），只得停下来一会儿。我很想出去兜兜风，缓解一下内心的压抑和惆怅，奈何“帝都今日有大雾”！还是作罢，继续其它吧。&lt;/p&gt;&#xA;&lt;p&gt;中午吃完饭，没有休息，迅速的续读。后面的部分情节很在预料中，是典型的80后小说。照例，看完之后，我也很羡慕主角柯景腾。羡慕他能结识一个如此美丽（我一直对面孔姣好的女孩子有更多好感）而且读书好，并且跟他过往密切的女孩子。唯一冲减这种羡慕情绪的事情之后一件，那就是开始时一样喜欢柯景腾的李小华后来跑到哪里去了？改名换姓之后就凭空蒸发了么？她竟然没有出现在结局中。而据说在电影中，这个角色则从头就没有出现。九把刀是想怎样？？？？写丢人物难道不是作家的大忌么？？？&lt;/p&gt;&#xA;&lt;p&gt;真的有点入迷了。晚上回来又看了一下九把刀在北京大学的演讲，热血+励志。然而，打动我的并不在这里。打动我的地方在于九把刀讲话的方式十分的口语化，一点都没有架子，甚至动不动都爆粗口，不停地说“干”，不像国内的一些名人，总是端着，放不下身段。&lt;/p&gt;&#xA;&lt;p&gt;我知道很多人喜欢装成不食人间烟火的样子，好像这样才能证明他们有才华，有知识。然而，历史长河中，哪个人不是烟火众生之一呢？做一个烟火众生有什么不好，我一直想，假如我在做好烟火众生之一之时，还能够得到老天的眷顾，拥有一些才华，能好好读些书，赚到些财富，得到几个真心朋友那就很幸运了。&lt;/p&gt;&#xA;&lt;p&gt;关电脑之前反思了一下长久以来懒得写博客的原因，乃在于WordPress的众多界面做的太难看了，我搜索了很久都没有找到自己中意的模板（当然我懒得自己去写模板），我都不知道当初是什么理由支撑我用WordPress系统做一个网站，我想唯一可以算作原因的应该是WordPress支持LaTex公式吧。&lt;/p&gt;&#xA;&lt;p&gt;借用九把刀的风格，没有好模板，那我就只能好好地等着，又或许哪天我热血再现就会自己写一个漂亮的模板过来！&lt;/p&gt;</description>
    </item>
    <item>
      <title>一价定律</title>
      <link>http://gewutang.cn/2011/11/29/the-law-of-one-price/</link>
      <pubDate>Tue, 29 Nov 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/11/29/the-law-of-one-price/</guid>
      <description>&lt;p&gt;一价定律即绝对购买力平价理论，它是由的货币学派的代表人物弗里德曼(1953)提出的。&lt;/p&gt;&#xA;&lt;p&gt;一价定律认为在没有运输费用和官方贸易壁垒的自由竞争市场上，一件同质商品在不同国家（地点）出售，如果以同一种货币计价，其价格应是相等的。即，通过汇率折算之后的标价是应当是一致的。如果情况不是这样，或者更专业的说，如果在各国间存在价格差异，则会发生商品套购。直到价差被消除，贸易停止，这时达到商品市场的均衡状态。举个例子：&lt;/p&gt;&#xA;&lt;p&gt;一颗苹果在中国卖2RMB，那么，根据即期汇率6.48来换算的话，它在美国的售价应该是 &lt;code&gt;\(2/6.48=0.3086\)&lt;/code&gt; 。如果苹果在美国的售价是0.4美元的话，你就可以拿着钱来中国买苹果，再回美国去卖，从而谋利。&lt;/p&gt;&#xA;&lt;p&gt;当然了，结果会出现为数众多的你来进行套购，最终苹果在美国的售价将回归至0.3068美元。&lt;/p&gt;&#xA;&lt;p&gt;当然，这个理论是在严格的假设下得出来的，其中比较重要的几个假设是：&lt;/p&gt;&#xA;&lt;p&gt;1.对比国家都实行了同等程度的货币自由兑换，货币、商品、劳务和资本流通是完全自由的；&#xA;2.自由贸易，关税为0;&#xA;3.信息是完全的；&#xA;4.易成本为零。&lt;/p&gt;&#xA;&lt;p&gt;因此，一价定律理论上非常完美，但现实中并非如此。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的3维HCPC图</title>
      <link>http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/</link>
      <pubDate>Mon, 28 Nov 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/</guid>
      <description>&lt;p&gt;第四届R会议上，余苓提到了一个叫FactoMineR的包，这个包提供了一个可以绘制立体谱系图的函数plot.HCPC()。我记得之前有人在COS论坛上问过这个问题，我就特别留意了一下这个函数。&lt;/p&gt;&#xA;&lt;p&gt;它的用法如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 安装包,如果未安装&#xA;# install.packages(&amp;quot;FactoMineR&amp;quot;)&#xA;library(FactoMineR)  #加载包&#xA;data(iris)           #加载数据&#xA;res.hcpc &amp;lt;- HCPC(iris[1:4], nb.clust=3)   #基于PC的聚类&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;img src=&#34;http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;672&#34; /&gt;&lt;img src=&#34;http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/index_files/figure-html/unnamed-chunk-1-3.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;plot.HCPC(res.hcpc, choice=&amp;quot;3D.map&amp;quot;, angle=60)   #3D谱系图&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/11/28/3d-graph-of-hcpc-in-r/index_files/figure-html/unnamed-chunk-1-4.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;但是，这个图不够好，因为这个图缺少该有的交互性。等我有时间了，我会用rgl包重新写一个具有交互功能的立体谱系图。&lt;/p&gt;</description>
    </item>
    <item>
      <title>什么培养了你的品味</title>
      <link>http://gewutang.cn/2011/11/22/who-determines-your-taste-on-arts/</link>
      <pubDate>Tue, 22 Nov 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/11/22/who-determines-your-taste-on-arts/</guid>
      <description>&lt;p&gt;如果你拿着梵高的《向日葵》，问一个受过高中以上教育的人对画作的感受。90%或者更多的人会说这是一件惊世之作。现代人对这种高质量的画作好像具有一种先天的品位。&lt;/p&gt;&#xA;&lt;p&gt;我们把时光退回到梵高生活的年代，你看到的是另外一个景象。在梵高生活的年代，纵然是最有鉴别力的评论也没有鉴定出梵高画作的价值。&lt;/p&gt;&#xA;&lt;p&gt;现在高中生都能意识到的东西，当时最伟大的评论家却都忽略了？或者说，当时连最伟大的评论家们都忽略的东西现在却轻而易举的背高中生感受到了？这是人类的品位在漫长的岁月中有了长足的进化么？&lt;/p&gt;&#xA;&lt;p&gt;我想不是的。从梵高的年代到此时，这中间的岁月纵然漫长，可是用来改善人类的品位，显然还是太短暂了。那现代人为什么知道梵高画作的价值呢？答案是教育。&lt;/p&gt;&#xA;&lt;p&gt;一幅画作在某个人眼里是否是好的，本来应该取决于这个人的直觉。&lt;/p&gt;&#xA;&lt;p&gt;在现代社会，可不是这样。现代社会里，更多时候取决于你受到的教育。当你受到的教育告诉你，梵高的《向日葵》是一副价值连城的绝世珍品的时候，你舍弃了自己的直觉，你认为那是一幅很好的画作，纵然你本身对他并不认同。那我们可以做一个实验，来验证这个问题么？答案是很难。&lt;/p&gt;&#xA;&lt;p&gt;因为你首先要排除实验中的群体接收到任何关于梵高的信息的可能性，其次，你不能跟他讲19世纪以来的绘画史，因为梵高的出现本身已经改变了19世纪以来的绘画史。也许，你会问，梵高改变了绘画史？难道这不说明他的伟大么？我想说的是，他起初改变的并不是绘画史，他只是改变了一群人。改变了一个伟大的评论家们。而这些评论家具有左右人们品位高低的权势。&lt;/p&gt;&#xA;&lt;p&gt;他们对梵高的画作进行估值，最终奠定了梵高的绘画地位。在整个链条中，最重要的不是梵高，而是这些评论家所拥有的凌驾于一般公众之上的权势。当然，你还可以说，梵高能改变有权势的一群人，难道不能说明梵高伟大么？其实，更残忍的说法是，梵高的绘画品位，只是刚好被当时的有权势评论家认可而已。&lt;/p&gt;&#xA;&lt;p&gt;如果将来有一天，某个孩子，见到了梵高的《向日葵》，并且说那是一幅垃圾。千万不要苛责孩子没有品位。如果将来有一天，你见到一幅也是出自梵高之手，但是，因为你没听说过，因而将它认定为了垃圾的一幅画作，你也无须自卑。这不涉及品位的问题，这只是教育。教育而已。&lt;/p&gt;</description>
    </item>
    <item>
      <title>apply函数族入门</title>
      <link>http://gewutang.cn/2011/11/09/apply-family/</link>
      <pubDate>Wed, 09 Nov 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/11/09/apply-family/</guid>
      <description>&lt;p&gt;在任何一个R语言问答网站或者论坛，你都能看见这样的问题：&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Q:如何用循环做【&amp;hellip;各种奇怪的事情&amp;hellip;】&#xA;A:不用用循环哦，亲！apply函数可以解决这个问题哦，亲！&lt;/p&gt;&#xA;&lt;p&gt;上面答案中提到的这个神奇的apply函数到底是什么？下面通过一些简单的操作示范一下。&lt;/p&gt;&#xA;&lt;p&gt;打开R，敲入??apply函数，选定base包部分你会看到下面的东西：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;base::apply Apply Functions Over Array Margins&#xA;base::by Apply a Function to a Data Frame Split by Factors&#xA;base::eapply Apply a Function Over Values in an Environment&#xA;base::lapply Apply a Function over a List or Vector&#xA;base::mapply Apply a Function to Multiple List or Vector Arguments&#xA;base::rapply Recursively Apply a Function to a List&#xA;base::tapply Apply a Function Over a Ragged Array&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;下面一一示范。&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;apply&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;先看看帮助文档中对其的描述：&lt;/p&gt;</description>
    </item>
    <item>
      <title>用R检验股票的协整关系</title>
      <link>http://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</link>
      <pubDate>Thu, 27 Oct 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</guid>
      <description>&lt;h2 id=&#34;需要用到的r包&#34;&gt;需要用到的R包&lt;/h2&gt;&#xA;&lt;p&gt;xts和zoo是两个强大的处理时序对象的R包。xts包本身又相当于是一个加强版的zoo包。zoo包处理的对象是zoo，zoo对象与数据框（dataframe)类似，由行列构成。不同的是，zoo对象比数据框多了一个时间戳。&lt;/p&gt;&#xA;&lt;p&gt;假设我们有一个zoo对象t，index(t)返回时间戳，也就是一个表示日期的向量。start(t)和end（t）可以查看zoo对象的起始值和末端值。&lt;/p&gt;&#xA;&lt;h2 id=&#34;数据预处理&#34;&gt;数据预处理&lt;/h2&gt;&#xA;&lt;p&gt;分四个步骤：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;读入数据（data frame格式）&lt;/li&gt;&#xA;&lt;li&gt;将日期转化为Date对象&lt;/li&gt;&#xA;&lt;li&gt;将data frame数据转换为zoo对象&lt;/li&gt;&#xA;&lt;li&gt;合并数据&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;相应的r代码&#34;&gt;相应的R代码：&lt;/h2&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)   # 金融数据获取&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(urca)       # 协整检验&#xA;library(ggplot2)    # 可视化&#xA;&#xA;# 获取2010-2011年股票数据&#xA;getSymbols(c(&amp;quot;AAPL&amp;quot;,&amp;quot;MSFT&amp;quot;))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;MSFT&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 提取收盘价并转换为对数序列&#xA;aapl &amp;lt;- log(Ad(AAPL$AAPL.Adjusted))&#xA;msft &amp;lt;- log(Ad(MSFT$MSFT.Adjusted))&#xA;&#xA;# 创建合并数据集&#xA;prices &amp;lt;- merge(aapl, msft)&#xA;colnames(prices) &amp;lt;- c(&amp;quot;log_AAPL&amp;quot;, &amp;quot;log_MSFT&amp;quot;)&#xA;&#xA;# 绘制对数价格趋势&#xA;ggplot(prices, aes(x=Index)) +&#xA;  geom_line(aes(y=log_AAPL, color=&amp;quot;苹果&amp;quot;)) +&#xA;  geom_line(aes(y=log_MSFT, color=&amp;quot;微软&amp;quot;)) +&#xA;  labs(title=&amp;quot;苹果与微软对数收盘价序列&amp;quot;, y=&amp;quot;log(Price)&amp;quot;, x=&amp;quot;日期&amp;quot;) +&#xA;  scale_color_manual(values=c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;)) +&#xA;  theme_minimal(base_family = &amp;quot;PingFang SC&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/10/27/use-r-to-test-cointegration/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h2 id=&#34;平稳性检验&#34;&gt;平稳性检验&lt;/h2&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 原始序列检验&#xA;# 苹果价格ADF检验&#xA;summary(ur.df(prices$log_AAPL, type=&amp;quot;drift&amp;quot;, lags=5)) #P-value大于0.05则序列不平稳&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ############################################### &#xA;## # Augmented Dickey-Fuller Test Unit Root Test # &#xA;## ############################################### &#xA;## &#xA;## Test regression drift &#xA;## &#xA;## &#xA;## Call:&#xA;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#xA;## &#xA;## Residuals:&#xA;##       Min        1Q    Median        3Q       Max &#xA;## -0.197346 -0.008942  0.000164  0.010330  0.141513 &#xA;## &#xA;## Coefficients:&#xA;##               Estimate Std. Error t value Pr(&amp;gt;|t|)  &#xA;## (Intercept)  0.0016271  0.0008139   1.999   0.0457 *&#xA;## z.lag.1     -0.0002047  0.0002258  -0.907   0.3646  &#xA;## z.diff.lag1 -0.0321993  0.0147383  -2.185   0.0290 *&#xA;## z.diff.lag2 -0.0155993  0.0147150  -1.060   0.2892  &#xA;## z.diff.lag3 -0.0090648  0.0147162  -0.616   0.5379  &#xA;## z.diff.lag4  0.0250380  0.0147149   1.702   0.0889 .&#xA;## z.diff.lag5  0.0145020  0.0147113   0.986   0.3243  &#xA;## ---&#xA;## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1&#xA;## &#xA;## Residual standard error: 0.01992 on 4600 degrees of freedom&#xA;## Multiple R-squared:  0.00234,&#x9;Adjusted R-squared:  0.001039 &#xA;## F-statistic: 1.798 on 6 and 4600 DF,  p-value: 0.09538&#xA;## &#xA;## &#xA;## Value of test-statistic is: -0.9067 5.4705 &#xA;## &#xA;## Critical values for test statistics: &#xA;##       1pct  5pct 10pct&#xA;## tau2 -3.43 -2.86 -2.57&#xA;## phi1  6.43  4.59  3.78&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 微软价格ADF检验&#xA;summary(ur.df(prices$log_MSFT, type=&amp;quot;drift&amp;quot;, lags=5))  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ############################################### &#xA;## # Augmented Dickey-Fuller Test Unit Root Test # &#xA;## ############################################### &#xA;## &#xA;## Test regression drift &#xA;## &#xA;## &#xA;## Call:&#xA;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#xA;## &#xA;## Residuals:&#xA;##       Min        1Q    Median        3Q       Max &#xA;## -0.148299 -0.008044  0.000121  0.008836  0.160776 &#xA;## &#xA;## Coefficients:&#xA;##               Estimate Std. Error t value Pr(&amp;gt;|t|)    &#xA;## (Intercept)  0.0001374  0.0010067   0.137  0.89142    &#xA;## z.lag.1      0.0001571  0.0002344   0.670  0.50269    &#xA;## z.diff.lag1 -0.1088675  0.0147498  -7.381 1.86e-13 ***&#xA;## z.diff.lag2 -0.0421217  0.0148530  -2.836  0.00459 ** &#xA;## z.diff.lag3  0.0054135  0.0148661   0.364  0.71576    &#xA;## z.diff.lag4 -0.0431566  0.0148530  -2.906  0.00368 ** &#xA;## z.diff.lag5 -0.0185213  0.0147761  -1.253  0.21010    &#xA;## ---&#xA;## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1&#xA;## &#xA;## Residual standard error: 0.01756 on 4600 degrees of freedom&#xA;## Multiple R-squared:  0.01478,&#x9;Adjusted R-squared:  0.0135 &#xA;## F-statistic:  11.5 on 6 and 4600 DF,  p-value: 8.16e-13&#xA;## &#xA;## &#xA;## Value of test-statistic is: 0.6703 4.8403 &#xA;## &#xA;## Critical values for test statistics: &#xA;##       1pct  5pct 10pct&#xA;## tau2 -3.43 -2.86 -2.57&#xA;## phi1  6.43  4.59  3.78&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 一阶差分平稳性验证&#xA;# 差分处理&#xA;diff_aapl &amp;lt;- diff(prices$log_AAPL)[-1]&#xA;diff_msft &amp;lt;- diff(prices$log_MSFT)[-1]&#xA;&#xA;# 差分序列ADF检验&#xA;summary(ur.df(diff_aapl, type=&amp;quot;none&amp;quot;))  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ############################################### &#xA;## # Augmented Dickey-Fuller Test Unit Root Test # &#xA;## ############################################### &#xA;## &#xA;## Test regression none &#xA;## &#xA;## &#xA;## Call:&#xA;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#xA;## &#xA;## Residuals:&#xA;##       Min        1Q    Median        3Q       Max &#xA;## -0.197910 -0.007903  0.001180  0.011336  0.140719 &#xA;## &#xA;## Coefficients:&#xA;##            Estimate Std. Error t value Pr(&amp;gt;|t|)    &#xA;## z.lag.1    -1.04076    0.02112 -49.286   &amp;lt;2e-16 ***&#xA;## z.diff.lag  0.01349    0.01473   0.916     0.36    &#xA;## ---&#xA;## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1&#xA;## &#xA;## Residual standard error: 0.01999 on 4608 degrees of freedom&#xA;## Multiple R-squared:  0.5134,&#x9;Adjusted R-squared:  0.5131 &#xA;## F-statistic:  2430 on 2 and 4608 DF,  p-value: &amp;lt; 2.2e-16&#xA;## &#xA;## &#xA;## Value of test-statistic is: -49.2864 &#xA;## &#xA;## Critical values for test statistics: &#xA;##       1pct  5pct 10pct&#xA;## tau1 -2.58 -1.95 -1.62&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;summary(ur.df(diff_msft, type=&amp;quot;none&amp;quot;)) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ############################################### &#xA;## # Augmented Dickey-Fuller Test Unit Root Test # &#xA;## ############################################### &#xA;## &#xA;## Test regression none &#xA;## &#xA;## &#xA;## Call:&#xA;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#xA;## &#xA;## Residuals:&#xA;##       Min        1Q    Median        3Q       Max &#xA;## -0.149197 -0.007368  0.000777  0.009507  0.165463 &#xA;## &#xA;## Coefficients:&#xA;##            Estimate Std. Error t value Pr(&amp;gt;|t|)    &#xA;## z.lag.1    -1.14697    0.02190 -52.367  &amp;lt; 2e-16 ***&#xA;## z.diff.lag  0.03991    0.01475   2.705  0.00685 ** &#xA;## ---&#xA;## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1&#xA;## &#xA;## Residual standard error: 0.01758 on 4608 degrees of freedom&#xA;## Multiple R-squared:  0.5521,&#x9;Adjusted R-squared:  0.5519 &#xA;## F-statistic:  2840 on 2 and 4608 DF,  p-value: &amp;lt; 2.2e-16&#xA;## &#xA;## &#xA;## Value of test-statistic is: -52.3667 &#xA;## &#xA;## Critical values for test statistics: &#xA;##       1pct  5pct 10pct&#xA;## tau1 -2.58 -1.95 -1.62&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h2 id=&#34;协整检验&#34;&gt;协整检验&lt;/h2&gt;&#xA;&lt;p&gt;我们用Engle-Granger两步法进行协整检验。&lt;/p&gt;</description>
    </item>
    <item>
      <title>R与月跨度函数，理论与应用的差距</title>
      <link>http://gewutang.cn/2011/09/15/r-and-time-span-function/</link>
      <pubDate>Thu, 15 Sep 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/09/15/r-and-time-span-function/</guid>
      <description>&lt;p&gt;R在处理数据方面的灵活性一直都被人们赞誉。当然，理论界的人的多些，应用界的人相对较少。&lt;/p&gt;&#xA;&lt;p&gt;混迹应用界的人一般会对R有更多的细节要求。比如在处理日期的时候，如果我想获取两个日期，2010年3月25日和2010年6月27日的月跨度，R里面就没有现存的函数。这一点让我意外，因为这是数据处理实务中经常遇到的问题。为什么R包的作者没有想到呢？不要抱怨，抱怨没用，这就是理论和应用的差距。&lt;/p&gt;&#xA;&lt;p&gt;应用者的一切以方便为导向，理论者则不同，他们负责提供石头，建筑有时候要靠你自己去完成。&lt;/p&gt;&#xA;&lt;p&gt;花了一会儿时间，找到了三个石头，length,months,seq。完成了建筑，在代码里面把2010-03-**全部处理为2010-03-01，把2010-06-**全部处理成2010-06-01。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;d1=&amp;quot;2010-03-25&amp;quot;&#xA;d2=&amp;quot;2010-06-24&amp;quot;&#xA;&#xA;length(seq(from=as.Date(paste(substr(d1,1,7),&amp;quot;01&amp;quot;,sep=&amp;quot;-&amp;quot;)),&#xA;                to=as.Date(paste(substr(d2,1,7),&amp;quot;01&amp;quot;,sep=&amp;quot;-&amp;quot;)),&#xA;                by=&amp;quot;month&amp;quot;))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 4&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;当然，也可以封装一下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;month.span=function(d1,d2){&#xA; date1=paste(substr(d1,1,7),&amp;quot;01&amp;quot;,sep=&amp;quot;-&amp;quot;)&#xA; date2=paste(substr(d2,1,7),&amp;quot;01&amp;quot;,sep=&amp;quot;-&amp;quot;)&#xA; L=length(seq(from=as.Date(date1),to=as.Date(date2),by=&amp;quot;month&amp;quot;))&#xA; L&#xA;}&#xA;&#xA;month.span(d1,d2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 4&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>非线性模型极大似然估计方法</title>
      <link>http://gewutang.cn/2011/09/13/max-likelyho/</link>
      <pubDate>Tue, 13 Sep 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/09/13/max-likelyho/</guid>
      <description>&lt;h2 id=&#34;一模型设定与概率框架&#34;&gt;一、模型设定与概率框架&lt;/h2&gt;&#xA;&lt;p&gt;设非线性生产函数形式为：&#xA;$$&#xA;y_i = aL_i^bK_i^c + \epsilon_i \quad (i=1,&amp;hellip;,n)&#xA;$$&#xA;其中误差项 &lt;code&gt;\(\epsilon_i \sim N(0,\sigma^2)\)&lt;/code&gt;，则观测值 &lt;code&gt;\(y_i\)&lt;/code&gt; 服从均值为 &lt;code&gt;\(aL_i^bK_i^c\)&lt;/code&gt;、方差为 &lt;code&gt;\(\sigma^2\)&lt;/code&gt; 的正态分布。&lt;/p&gt;&#xA;&lt;h2 id=&#34;二似然函数构建&#34;&gt;二、似然函数构建&lt;/h2&gt;&#xA;&lt;h3 id=&#34;1-联合概率密度函数&#34;&gt;1. 联合概率密度函数&lt;/h3&gt;&#xA;&lt;p&gt;$$&#xA;L(a,b,c,\sigma) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left[ -\frac{(y_i-aL_i^bK_i^c)^2}{2\sigma^2} \right]&#xA;$$&lt;/p&gt;&#xA;&lt;h3 id=&#34;2-负对数似然函数目标函数&#34;&gt;2. 负对数似然函数（目标函数）&lt;/h3&gt;&#xA;&lt;p&gt;$$&#xA;\ell(a,b,c,\sigma) = -\ln L = \frac{n}{2}\ln(2\pi) + n\ln\sigma + \frac{1}{2\sigma^2}\sum_{i=1}^n (y_i - aL_i^bK_i^c)^2&#xA;$$&lt;/p&gt;&#xA;&lt;h2 id=&#34;三参数估计流程&#34;&gt;三、参数估计流程&lt;/h2&gt;&#xA;&lt;h3 id=&#34;1-数据处理与准备&#34;&gt;1. 数据处理与准备&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 导入数据&#xA;data &amp;lt;- data.frame(&#xA;  y = c(76,47,97,107,123,139,159,152,191,201,207,200),&#xA;  L = c(0.02,0.02,0.06,0.06,0.11,0.11,0.22,0.22,0.56,0.56,1.10,1.10),&#xA;  K = c(1.1,0.9,2.3,2.1,3.4,3.6,4.5,4.3,5.8,6.0,7.2,7.0)&#xA;)&#xA;&#xA;# 定义负对数似然函数&#xA;mloglik &amp;lt;- function(theta) {&#xA;  a &amp;lt;- theta[1]&#xA;  b &amp;lt;- theta[2]&#xA;  c &amp;lt;- theta[3]&#xA;  sigma &amp;lt;- theta[4]&#xA;  &#xA;  y_pred &amp;lt;- a * data$L^b * data$K^c&#xA;  residuals &amp;lt;- data$y - y_pred&#xA;  n &amp;lt;- length(data$y)&#xA;  &#xA;  # 避免\sigma 趋近于零&#xA;  if(sigma &amp;lt;= 0) return(1e10)&#xA;  &#xA;  (n/2)*log(2*pi) + n*log(sigma) + sum(residuals^2)/(2*sigma^2)&#xA;}&#xA;&#xA;# 采用线性化方法获取初始值&#xA;log_model &amp;lt;- lm(log(y) ~ log(L) + log(K), data = data)&#xA;theta_init &amp;lt;- c(&#xA;  exp(coef(log_model)[1]),&#xA;  coef(log_model)[2],&#xA;  coef(log_model)[3],&#xA;  sd(resid(log_model))&#xA;)&#xA;&#xA;# 优化求解&#xA;fit &amp;lt;- nlm(mloglik, theta_init, hessian = TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in nlm(mloglik, theta_init, hessian = TRUE): NA/NaN replaced by maximum&#xA;## positive value&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in nlm(mloglik, theta_init, hessian = TRUE): Inf replaced by maximum&#xA;## positive value&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 提取估计结果&#xA;params &amp;lt;- fit$estimate&#xA;names(params) &amp;lt;- c(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;sigma&amp;quot;)&#xA;params&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##           a           b           c       sigma &#xA;## 55.45620820 -0.02273718  0.67704166  7.67493891&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>NASDAQ有泡沫吗</title>
      <link>http://gewutang.cn/2011/09/06/demo-about-lppl/</link>
      <pubDate>Tue, 06 Sep 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/09/06/demo-about-lppl/</guid>
      <description>&lt;h2 id=&#34;lppl模型是什么&#34;&gt;LPPL模型是什么&lt;/h2&gt;&#xA;&lt;p&gt;LPPL（对数周期性幂律）模型是由Didier Sornette等学者提出的金融市场泡沫检测模型，用于识别资产价格在崩盘前的非线性加速增长模式。该模型结合了幂律增长和对数周期性振荡，被广泛应用于金融泡沫预警和临界点预测。&lt;/p&gt;&#xA;&lt;p&gt;模型的核心是资产价格演化方程：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\ln p(t) = A + B(t_c - t)^\beta \left[1 + C\cos\left(\omega \ln(t_c - t) + \phi\right)\right]&#xA;$$&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;\(t_c\)&lt;/code&gt; 是临界时间（预测的崩盘时间）&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\beta\)&lt;/code&gt; 是幂律指数&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\omega\)&lt;/code&gt; 是振荡频率（典型值5-15）&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(C\)&lt;/code&gt; 是振荡幅度&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\phi\)&lt;/code&gt; 是相位参数&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;其中： &lt;code&gt;\(0 \lt \beta \lt 1\)&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;LPPL模型具有如下特征：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;价格加速增长&#xA;&lt;ul&gt;&#xA;&lt;li&gt;幂律项 &lt;code&gt;\((t_c - t)^\beta\)&lt;/code&gt; 描述价格超指数增长。&lt;/li&gt;&#xA;&lt;li&gt;典型泡沫阶段 &lt;code&gt;\(\beta \in (0,1)\)&lt;/code&gt;，反映增长速率随时间递增。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;对数周期振荡&#xA;&lt;ul&gt;&#xA;&lt;li&gt;其中 &lt;code&gt;\(cos\left[\omega\ln\left(t_{c}-t\right)\right]\)&lt;/code&gt; 反映投资者博弈产生的离散尺度不变性。&lt;/li&gt;&#xA;&lt;li&gt;震荡幅度随 &lt;code&gt;\(t\to t_{c}\)&lt;/code&gt; 增大，体现市场分歧加剧。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;临界时间特性&#xA;&lt;ul&gt;&#xA;&lt;li&gt;当 &lt;code&gt;\(t\to t_{c}\)&lt;/code&gt; 时模型预测系统失稳。&lt;/li&gt;&#xA;&lt;li&gt;实际崩盘可能发生在 &lt;code&gt;\(t_{c}\)&lt;/code&gt; 前6个月内。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;拟合模型&#34;&gt;拟合模型&lt;/h2&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载依赖包&#xA;library(quantmod)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(DEoptim)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：parallel&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## DEoptim package&#xA;## Differential Evolution algorithm in R&#xA;## Authors: D. Ardia, K. Mullen, B. Peterson and J. Ulrich&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(ggplot2)&#xA;&#xA;# 获取纳斯达克指数数据（2010-01-01至2011-08-30）&#xA;getSymbols(&amp;quot;^IXIC&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = &amp;quot;2010-01-01&amp;quot;, to = &amp;quot;2011-08-30&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;IXIC&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;nasdaq &amp;lt;- data.frame(&#xA;  t = 1:nrow(IXIC), &#xA;  ln_p = log(Ad(IXIC))&#xA;)&#xA;&#xA;colnames(nasdaq)=c(&amp;quot;t&amp;quot;,&amp;quot;ln_p&amp;quot;)&#xA;# 数据降噪（7日移动平均）&#xA;nasdaq$ln_p_smooth &amp;lt;- stats::filter(nasdaq$ln_p, rep(1/7, 7), sides = 2)&#xA;nasdaq &amp;lt;- na.omit(nasdaq)  # 剔除NA值&#xA;&#xA;&#xA;# 定义LPPL模型函数&#xA;lppl_model &amp;lt;- function(params, t) {&#xA;  A &amp;lt;- params[&amp;quot;A&amp;quot;]; B &amp;lt;- params[&amp;quot;B&amp;quot;]; tc &amp;lt;- params[&amp;quot;tc&amp;quot;]&#xA;  beta &amp;lt;- params[&amp;quot;beta&amp;quot;]; C &amp;lt;- params[&amp;quot;C&amp;quot;]; omega &amp;lt;- params[&amp;quot;omega&amp;quot;]; phi &amp;lt;- params[&amp;quot;phi&amp;quot;]&#xA;  term &amp;lt;- pmax(tc - t, 1e-6)  # 防止出现负数&#xA;  A + B * term^beta + C * term^beta * cos(omega * log(term) + phi)&#xA;}&#xA;&#xA;# 定义损失函数（均方误差）&#xA;loss &amp;lt;- function(params) {&#xA;  pred &amp;lt;- lppl_model(params, nasdaq$t)&#xA;  if (any(is.na(pred))) return(Inf)&#xA;  mean((pred - nasdaq$ln_p_smooth)^2, na.rm = TRUE)&#xA;}&#xA;&#xA;# 参数约束范围&#xA;lower &amp;lt;- c(&#xA;  A = min(nasdaq$ln_p_smooth), &#xA;  B = -2, &#xA;  tc = max(nasdaq$t) + 1, &#xA;  beta = 0.01, &#xA;  C = 0, &#xA;  omega = 6, &#xA;  phi = 0&#xA;)&#xA;&#xA;upper &amp;lt;- c(&#xA;  A = max(nasdaq$ln_p_smooth), &#xA;  B = 0, &#xA;  tc = max(nasdaq$t) * 1.2, &#xA;  beta = 0.99, &#xA;  C = 0.5, &#xA;  omega = 13, &#xA;  phi = 2 * pi&#xA;)&#xA;&#xA;&#xA;# 全局优化（差分进化算法）&#xA;set.seed(123)&#xA;de_result &amp;lt;- DEoptim(&#xA;  fn = loss, &#xA;  lower = lower, &#xA;  upper = upper, &#xA;  control = list(&#xA;    itermax = 500, &#xA;    NP = 100, &#xA;    trace = FALSE&#xA;  )&#xA;)&#xA;&#xA;# 局部优化（L-BFGS-B）&#xA;final_params &amp;lt;- optim(&#xA;  de_result$optim$bestmem, &#xA;  fn = loss, &#xA;  method = &amp;quot;L-BFGS-B&amp;quot;,&#xA;  lower = lower, &#xA;  upper = upper&#xA;)$par&#xA;&#xA;# 输出参数估计结果&#xA;cat(&amp;quot;LPPL模型参数估计结果：\n&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## LPPL模型参数估计结果：&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;print(round(final_params, 4))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##        A        B       tc     beta        C    omega      phi &#xA;##   7.9574  -0.0009 459.8781   0.9167   0.0000   7.7609   5.3808&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h2 id=&#34;结果解读&#34;&gt;结果解读&lt;/h2&gt;&#xA;&lt;p&gt;从模型结果可以看出：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R中的魔方矩阵</title>
      <link>http://gewutang.cn/2011/08/29/magic/</link>
      <pubDate>Mon, 29 Aug 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/08/29/magic/</guid>
      <description>&lt;p&gt;某个朋友去证券公司笔试，题目中竟然有一道要求用C语言完成魔方矩阵的输出。如果之前没有做过相关练习，骤然间写出代码可是不容易的事情。&lt;/p&gt;&#xA;&lt;p&gt;今天下午查了一下R中的魔方矩阵，发现里面已经有一个magic包可以完成魔方矩阵的输出。R真有点无所不包的气势。&lt;/p&gt;&#xA;&lt;h2 id=&#34;魔方矩阵原理应用与可视化实现&#34;&gt;魔方矩阵：原理、应用与可视化实现&lt;/h2&gt;&#xA;&lt;h3 id=&#34;一数学原理&#34;&gt;一、数学原理&lt;/h3&gt;&#xA;&lt;p&gt;魔方矩阵（Magic Square）又称幻方，是$n \times n$的方阵，满足以下特性：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;等和定律&lt;/strong&gt;：每行、每列及两条主对角线的元素和相等，称为魔方常数$M =\frac{n(n^2+1)}{2}$；&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;元素排列&lt;/strong&gt;：包含$1$到$n^2$的连续自然数；&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;构造规律&lt;/strong&gt;：奇数阶常用Siamese方法（楼梯法），偶数阶采用LUX或边界填充策略。&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;以3阶魔方为例：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;&lt;code&gt;\begin{bmatrix} 8 &amp;amp; 1 &amp;amp; 6 \\ 3 &amp;amp; 5 &amp;amp; 7 \\ 4 &amp;amp; 9 &amp;amp; 2 \\ \end{bmatrix}&lt;/code&gt;&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其行、列、对角线之和均为$15$。&lt;/p&gt;&#xA;&lt;h3 id=&#34;二核心价值&#34;&gt;二、核心价值&lt;/h3&gt;&#xA;&lt;h4 id=&#34;1-科学研究维度&#34;&gt;1. 科学研究维度&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;数论验证&lt;/strong&gt;：魔方常数与矩阵阶数的数学关系为代数理论提供验证案例&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;组合优化&lt;/strong&gt;：启发旅行商问题(TSP)、排班系统等组合优化算法设计&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;2-文化应用场景&#34;&gt;2. 文化应用场景&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;密码原型&lt;/strong&gt;：古代战争曾用魔方矩阵加密军事情报&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;艺术设计&lt;/strong&gt;：印度泰姬陵地砖、敦煌壁画均包含幻方图案&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;三r语言实现&#34;&gt;三、R语言实现&lt;/h3&gt;&#xA;&lt;h4 id=&#34;1-构造魔方矩阵&#34;&gt;1. 构造魔方矩阵&lt;/h4&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 安装必要包&#xA;if (!require(&amp;quot;magic&amp;quot;)) install.packages(&amp;quot;magic&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：magic&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：abind&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(magic)&#xA;&#xA;# 生成5阶魔方矩阵&#xA;magic_matrix &amp;lt;- magic(5)&#xA;print(magic_matrix)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##      [,1] [,2] [,3] [,4] [,5]&#xA;## [1,]    9    2   25   18   11&#xA;## [2,]    3   21   19   12   10&#xA;## [3,]   22   20   13    6    4&#xA;## [4,]   16   14    7    5   23&#xA;## [5,]   15    8    1   24   17&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h4 id=&#34;2-可视化&#34;&gt;2. 可视化&lt;/h4&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(ggplot2)&#xA;library(reshape2)&#xA;&#xA;# 数据重塑&#xA;melted_matrix &amp;lt;- melt(magic_matrix)&#xA;colnames(melted_matrix) &amp;lt;- c(&amp;quot;Row&amp;quot;, &amp;quot;Column&amp;quot;, &amp;quot;Value&amp;quot;)&#xA;&#xA;# 绘制热力图&#xA;ggplot(melted_matrix, aes(x = Column, y = Row, fill = Value)) +&#xA;  geom_tile(color = &amp;quot;white&amp;quot;) +&#xA;  scale_fill_gradient(low = &amp;quot;#FFD700&amp;quot;, high = &amp;quot;#FF4500&amp;quot;) +&#xA;  geom_text(aes(label = Value), color = &amp;quot;black&amp;quot;, size = 4) +&#xA;  theme_minimal(base_family=&amp;quot;PingFang SC&amp;quot;) +&#xA;  labs(title = &amp;quot;5阶魔方矩阵可视化&amp;quot;,&#xA;       subtitle = &amp;quot;行、列、对角线之和均为65&amp;quot;) +&#xA;  coord_fixed()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/29/magic/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;还可以写一段更简单的代码：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R与矩阵运算总结</title>
      <link>http://gewutang.cn/2011/08/27/matrix-operations/</link>
      <pubDate>Sat, 27 Aug 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/08/27/matrix-operations/</guid>
      <description>&lt;h1 id=&#34;1-矩阵基本操作&#34;&gt;1 矩阵基本操作&lt;/h1&gt;&#xA;&lt;h2 id=&#34;11创建向量&#34;&gt;1.1创建向量&lt;/h2&gt;&#xA;&lt;p&gt;R里面有多种方法来创建向量（Vector），最简单的是用函数c()。例如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;X=c(1,2,3,4)&#xA;&#xA;X&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;当然，还有别的方法。例如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;X=1:4&#xA;&#xA;X&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;还有seq()函数。例如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;X=seq(1,4,length=4)&#xA;&#xA;X&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;注意一点，R中的向量默认为列向量，如果要得到行向量需要对其进行转置。&lt;/p&gt;&#xA;&lt;h2 id=&#34;12创建矩阵&#34;&gt;1.2创建矩阵&lt;/h2&gt;&#xA;&lt;p&gt;R中创建矩阵的方法也有很多。大致分为直接创建和由其它格式转换两种方法。&lt;/p&gt;&#xA;&lt;h3 id=&#34;121直接创建矩阵&#34;&gt;1.2.1直接创建矩阵&lt;/h3&gt;&#xA;&lt;p&gt;最简单的直接创建矩阵的方法是用matrix()函数，matrix()函数的使用方法如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;args(matrix)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) &#xA;## NULL&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;其中，data参数输入的为矩阵的元素，不能为空；nrow参数输入的是矩阵的行数，默认为1；ncol参数输入的是矩阵的列数，默认为1；byrow参数控制矩阵元素的排列方式，TRUE表示按行排列，FALSE表示按列排列，默认为FALSE；dimnames参数输入矩阵的行名和列名，可以不输入，系统默认为NULL。例如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;matrix(1:6,nrow=2,ncol=3,byrow=FALSE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##      [,1] [,2] [,3]&#xA;## [1,]    1    3    5&#xA;## [2,]    2    4    6&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;改变矩阵的行数和列数：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R语言绘制维恩图</title>
      <link>http://gewutang.cn/2011/08/27/venn/</link>
      <pubDate>Sat, 27 Aug 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/08/27/venn/</guid>
      <description>&lt;p&gt;很久之前整理的关于R绘制维恩图的资料，虽然，用R绘制维恩图并不是很常见，还是记下一笔，以备需要者参考。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# Gplots包&#xA;library(gplots)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;gplots&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:stats&#39;:&#xA;## &#xA;##     lowess&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;A&amp;lt;- 1:20&#xA;B&amp;lt;- 1:20&#xA;C&amp;lt;- 2:20&#xA;D&amp;lt;- 3:21&#xA;&#xA;input&amp;lt;-list(A,B,C,D)&#xA;input&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [[1]]&#xA;##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20&#xA;## &#xA;## [[2]]&#xA;##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20&#xA;## &#xA;## [[3]]&#xA;##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20&#xA;## &#xA;## [[4]]&#xA;##  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;venn(input)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;A&amp;lt;- as.logical(rbinom(100, 1, 0.2))&#xA;B&amp;lt;- as.logical(rbinom(100, 1, 0.7))&#xA;C&amp;lt;- as.logical(rbinom(100, 1, 0.2))&#xA;D&amp;lt;- as.logical(rbinom(100, 1, 0.1))&#xA;&#xA;input&amp;lt;-data.frame(A,B,C,D)&#xA;venn(input)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tmp &amp;lt;- venn(input, simplify=TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in drawVennDiagram(data = counts, small = small, showSetLogicLabel = showSetLogicLabel, : Not shown: 0101 contains 2&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-3.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tmp&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##      num A B C D&#xA;## 0000   0 0 0 0 0&#xA;## 0001   1 0 0 0 1&#xA;## 0010   4 0 0 1 0&#xA;## 0011   0 0 0 1 1&#xA;## 0100  36 0 1 0 0&#xA;## 0101   2 0 1 0 1&#xA;## 0110   6 0 1 1 0&#xA;## 0111   1 0 1 1 1&#xA;## 1000   5 1 0 0 0&#xA;## 1001   0 1 0 0 1&#xA;## 1010   0 1 0 1 0&#xA;## 1011   1 1 0 1 1&#xA;## 1100  15 1 1 0 0&#xA;## 1101   1 1 1 0 1&#xA;## 1110   6 1 1 1 0&#xA;## 1111   1 1 1 1 1&#xA;## attr(,&amp;quot;intersections&amp;quot;)&#xA;## attr(,&amp;quot;intersections&amp;quot;)$A&#xA;## [1] &amp;quot;18&amp;quot; &amp;quot;29&amp;quot; &amp;quot;32&amp;quot; &amp;quot;54&amp;quot; &amp;quot;58&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$B&#xA;##  [1] &amp;quot;12&amp;quot;  &amp;quot;14&amp;quot;  &amp;quot;19&amp;quot;  &amp;quot;22&amp;quot;  &amp;quot;28&amp;quot;  &amp;quot;30&amp;quot;  &amp;quot;34&amp;quot;  &amp;quot;36&amp;quot;  &amp;quot;40&amp;quot;  &amp;quot;43&amp;quot;  &amp;quot;45&amp;quot;  &amp;quot;46&amp;quot; &#xA;## [13] &amp;quot;51&amp;quot;  &amp;quot;52&amp;quot;  &amp;quot;53&amp;quot;  &amp;quot;57&amp;quot;  &amp;quot;60&amp;quot;  &amp;quot;66&amp;quot;  &amp;quot;67&amp;quot;  &amp;quot;68&amp;quot;  &amp;quot;69&amp;quot;  &amp;quot;70&amp;quot;  &amp;quot;73&amp;quot;  &amp;quot;75&amp;quot; &#xA;## [25] &amp;quot;76&amp;quot;  &amp;quot;77&amp;quot;  &amp;quot;78&amp;quot;  &amp;quot;80&amp;quot;  &amp;quot;81&amp;quot;  &amp;quot;87&amp;quot;  &amp;quot;88&amp;quot;  &amp;quot;89&amp;quot;  &amp;quot;92&amp;quot;  &amp;quot;94&amp;quot;  &amp;quot;98&amp;quot;  &amp;quot;100&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$C&#xA;## [1] &amp;quot;6&amp;quot;  &amp;quot;10&amp;quot; &amp;quot;33&amp;quot; &amp;quot;84&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$D&#xA;## [1] &amp;quot;79&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`A:B`&#xA;##  [1] &amp;quot;2&amp;quot;  &amp;quot;5&amp;quot;  &amp;quot;9&amp;quot;  &amp;quot;13&amp;quot; &amp;quot;17&amp;quot; &amp;quot;21&amp;quot; &amp;quot;25&amp;quot; &amp;quot;26&amp;quot; &amp;quot;27&amp;quot; &amp;quot;38&amp;quot; &amp;quot;39&amp;quot; &amp;quot;44&amp;quot; &amp;quot;49&amp;quot; &amp;quot;50&amp;quot; &amp;quot;56&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`B:C`&#xA;## [1] &amp;quot;20&amp;quot; &amp;quot;71&amp;quot; &amp;quot;72&amp;quot; &amp;quot;82&amp;quot; &amp;quot;83&amp;quot; &amp;quot;97&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`B:D`&#xA;## [1] &amp;quot;62&amp;quot; &amp;quot;63&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`A:B:C`&#xA;## [1] &amp;quot;3&amp;quot;  &amp;quot;11&amp;quot; &amp;quot;47&amp;quot; &amp;quot;74&amp;quot; &amp;quot;85&amp;quot; &amp;quot;95&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`A:B:D`&#xA;## [1] &amp;quot;96&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`A:C:D`&#xA;## [1] &amp;quot;7&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`B:C:D`&#xA;## [1] &amp;quot;23&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;intersections&amp;quot;)$`A:B:C:D`&#xA;## [1] &amp;quot;37&amp;quot;&#xA;## &#xA;## attr(,&amp;quot;class&amp;quot;)&#xA;## [1] &amp;quot;venn&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;venn(input, showSetLogicLabel=TRUE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-4.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# asbio包&#xA;library(asbio)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：tcltk&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;Venn(A=.3,B=.2,AandB=.06)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-5.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# limma包&#xA;# install.packages(&amp;quot;BiocManager&amp;quot;)&#xA;# BiocManager::install(&amp;quot;limma&amp;quot;)&#xA;library(limma)&#xA;Y &amp;lt;- matrix(rnorm(100*6),100,6)&#xA;Y[1:10,3:4] &amp;lt;- Y[1:10,3:4]+3&#xA;Y[1:20,5:6] &amp;lt;- Y[1:20,5:6]+3&#xA;design &amp;lt;- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))&#xA;fit &amp;lt;- eBayes(lmFit(Y,design))&#xA;results &amp;lt;- decideTests(fit)&#xA;a &amp;lt;- vennCounts(results)&#xA;print(a)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##   x1 x2 x3 Counts&#xA;## 1  0  0  0     88&#xA;## 2  0  0  1      8&#xA;## 3  0  1  0      0&#xA;## 4  0  1  1      4&#xA;## 5  1  0  0      0&#xA;## 6  1  0  1      0&#xA;## 7  1  1  0      0&#xA;## 8  1  1  1      0&#xA;## attr(,&amp;quot;class&amp;quot;)&#xA;## [1] &amp;quot;VennCounts&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;vennDiagram(a)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-6.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;vennDiagram(results,include=c(&amp;quot;up&amp;quot;,&amp;quot;down&amp;quot;),counts.col=c(&amp;quot;red&amp;quot;,&amp;quot;green&amp;quot;))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-7.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# venneuler包&#xA;&#xA;library(venneuler)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：rJava&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;vd &amp;lt;- venneuler(c(A=0.3, B=0.3, C=1.1, &amp;quot;A&amp;amp;B&amp;quot;=0.1, &amp;quot;A&amp;amp;C&amp;quot;=0.2, &amp;quot;B&amp;amp;C&amp;quot;=0.1 ,&amp;quot;A&amp;amp;B&amp;amp;C&amp;quot;=0.1))&#xA;&#xA;plot(vd)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-8.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;m &amp;lt;- data.frame(elements=c(&amp;quot;1&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;3&amp;quot;),sets=c(&amp;quot;A&amp;quot;,&amp;quot;A&amp;quot;,&amp;quot;B&amp;quot;,&amp;quot;C&amp;quot;,&amp;quot;C&amp;quot;))&#xA;  &#xA;v &amp;lt;- venneuler(m)&#xA;&#xA;plot(v)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-9.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;m &amp;lt;- as.matrix(data.frame(A=c(1.5, 0.2, 0.4, 0, 0),B=c(0, 0.2, 0, 1, 0), C=c(0, 0, 0.3, 0, 1)))&#xA;&#xA;v &amp;lt;- venneuler(m &amp;gt; 0)&#xA;&#xA;plot(v)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-10.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;v &amp;lt;- venneuler(m)&#xA;&#xA;plot(v)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-11.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# Vennerable包&#xA;&#xA;# BiocManager::install(&amp;quot;graph&amp;quot;)&#xA;# BiocManager::install(&amp;quot;RBGL&amp;quot;)&#xA;# BiocManager::install(&amp;quot;reshape&amp;quot;)&#xA;# install.packages(&amp;quot;Vennerable&amp;quot;, repos=&amp;quot;http://R-Forge.R-project.org&amp;quot;)&#xA;&#xA;library(Vennerable)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：graph&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：BiocGenerics&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：generics&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;generics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,&#xA;##     setequal, union&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;BiocGenerics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:rJava&#39;:&#xA;## &#xA;##     anyDuplicated, duplicated, unique&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:limma&#39;:&#xA;## &#xA;##     plotMA&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:stats&#39;:&#xA;## &#xA;##     IQR, mad, sd, var, xtabs&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     anyDuplicated, aperm, append, as.data.frame, basename, cbind,&#xA;##     colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,&#xA;##     get, grep, grepl, is.unsorted, lapply, Map, mapply, match, mget,&#xA;##     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,&#xA;##     rbind, Reduce, rownames, sapply, saveRDS, table, tapply, unique,&#xA;##     unsplit, which.max, which.min&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：RBGL&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：grid&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：lattice&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：RColorBrewer&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：reshape&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：gtools&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xtable&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;Vennerable&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:asbio&#39;:&#xA;## &#xA;##     Venn&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;Venn(n=3)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## A Venn object on 3 sets named&#xA;## 1,2,3 &#xA;## 000 100 010 110 001 101 011 111 &#xA;##   1   1   1   1   1   1   1   1&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;plot(Venn(n=3))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-12.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;data(StemCell)&#xA;w &amp;lt;- Venn(Sets=StemCell[1:2])&#xA;Weights(w)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##  00  10  01  11 &#xA;##   0 219 875 404&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;plot(w)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-13.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;Weights(w) &amp;lt;- 1:4&#xA;plot(w)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/27/venn/index_files/figure-html/unnamed-chunk-1-14.png&#34; width=&#34;672&#34; /&gt;</description>
    </item>
    <item>
      <title>杂</title>
      <link>http://gewutang.cn/2011/08/08/young-people-are-undergoing-changes/</link>
      <pubDate>Mon, 08 Aug 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/08/08/young-people-are-undergoing-changes/</guid>
      <description>&lt;p&gt;我发现中国的青年人有所改变。念初中高中的时候，我们这一批人大都很厌恶鲁迅。当然，这不只是鲁迅的责任，而是归咎于教育部。教育部以鲁迅的名义压给我们很多背诵的负担。&lt;/p&gt;&#xA;&lt;p&gt;可是，现在的年轻人重新认识了鲁迅。一批人开始重新流传鲁迅的语录和文摘。青年人纷纷投奔到鲁迅的门下，把鲁迅当做奖章奖励给自己心中的舆论领袖，如韩寒等。大家开始感叹鲁迅对国人本性认知之深刻，这真的是从前从未想象过的。&lt;/p&gt;&#xA;&lt;p&gt;可能因为现在的年轻人见识越来越广泛，反思能力也日益变强，已经不再是那种被填鸭式塞入意识形态的人了。&lt;/p&gt;&#xA;&lt;p&gt;有这样的青年人是一个国家的幸运，这个国家一定会越来越好的。&lt;/p&gt;</description>
    </item>
    <item>
      <title>draw.circle:R中的绘圆利器</title>
      <link>http://gewutang.cn/2011/08/03/a-powerful-tool-to-draw-circle-with-r/</link>
      <pubDate>Wed, 03 Aug 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/08/03/a-powerful-tool-to-draw-circle-with-r/</guid>
      <description>&lt;p&gt;R用户经常要在图形中绘圆，如果你是其中的一员的话，你就不得不了解一下plotrix包中的draw.circle()函数。draw.circle()函数的优点有：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;任意指定位置&lt;/li&gt;&#xA;&lt;li&gt;随意设定圆内的颜色&lt;/li&gt;&#xA;&lt;li&gt;随意设定圆的线型&lt;/li&gt;&#xA;&lt;li&gt;随意设定圆线的颜色&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(plotrix)&#xA;plot(1:5,seq(1,10,length=5),type=&amp;quot;n&amp;quot;,xlab=&amp;quot;&amp;quot;,ylab=&amp;quot;&amp;quot;,main=&amp;quot;Test draw.circle()&amp;quot;)&#xA;draw.circle(2,4,c(1,0.66,0.33),border=&amp;quot;purple&amp;quot;,&#xA;col=c(&amp;quot;#ff00ff&amp;quot;,&amp;quot;#ff77ff&amp;quot;,&amp;quot;#ffccff&amp;quot;),lty=1,lwd=1)&#xA;draw.circle(2.5,8,0.6,border=&amp;quot;red&amp;quot;,lty=3,lwd=3)&#xA;draw.circle(4,3,0.7,border=&amp;quot;green&amp;quot;,lty=1,lwd=1)&#xA;draw.circle(3.5,7,0.8,border=&amp;quot;blue&amp;quot;,lty=2,lwd=2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/08/03/a-powerful-tool-to-draw-circle-with-r/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;</description>
    </item>
    <item>
      <title>大旗如果扛不动，就先插在地上</title>
      <link>http://gewutang.cn/2011/07/18/about-translate-books/</link>
      <pubDate>Mon, 18 Jul 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/07/18/about-translate-books/</guid>
      <description>&lt;p&gt;COS一直怀都有译书和著书的愿望.这种愿望的背后，我想有一种知识分子的传统品格在里面。传统知识分子都信奉人的一生要做到“三立”，而其中的一“立”就是指“著书立说”。除此之外，当然还有一个更现实的原因，那就是解决COS长远发展的资金问题。&lt;/p&gt;&#xA;&lt;p&gt;目前，COS是一个非商业网站。我想以后也会一直是。可是，这并不意味着COS的发展不需要资金支撑。如何解决这个问题一直令人头疼。&lt;/p&gt;&#xA;&lt;p&gt;我们想过写书，可是考虑到写书的进度太慢，而且对自己的写作水平也不自信，害怕写出来质量不高的东西，那就贻害读者了。因此，迟迟没有动手。这也说明，COS的人大都还是“宁为玉碎，不为瓦全”的。COS能有这种“宁缺勿滥”的坚持一直让我骄傲。&lt;/p&gt;&#xA;&lt;p&gt;那干脆译书吧。于是，我们就着手翻译了一点东西。慢慢发现，翻译书籍也并不简单，尤其翻译书籍时字斟句酌的过程让人十分骄傲。太云和怡轩调侃说，翻译部分词句时很有“给自己耳光的冲动”。这种为难，我懂，实在是一种夹杂着无奈窘迫。但是，细想起来，也可爱至极。&lt;/p&gt;&#xA;&lt;p&gt;最近，重杰兄提出说成立一个翻译小组，张家平兄也有此提议。我觉得确实有这个必要，但是又觉得组织起来困难重重。COS资金并不富裕，也没有实体，如果成立了翻译小组，那翻译工作也只能靠小组的成员自愿完成。这样一来，总觉得对这些成员不住。好在，大家都很有热情，期待不久之后可以好好考虑此事。&lt;/p&gt;&#xA;&lt;p&gt;不久之前，跟益辉说了这个想法。益辉问，你可否扛起这个大旗。我没有答话。只是心里想，也许扛不起。可是扛不起，又有什么关系？扛不起就先把旗插在地上好了，至少，要先把大旗立起来，立起来就能听到旗声猎猎。&lt;/p&gt;</description>
    </item>
    <item>
      <title>新书：Financial Risk Forecasting</title>
      <link>http://gewutang.cn/2011/06/27/financial-risk-forcasting/</link>
      <pubDate>Mon, 27 Jun 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/06/27/financial-risk-forcasting/</guid>
      <description>&lt;p&gt;Jon Danielsson撰写的一本金融风险预测的书籍。书的全称是：Financial Risk Forecasting: The Theory and Practice of Forecasting Market Risk with Implementation in R and Matlab (The Wiley Finance Series)。&lt;/p&gt;&#xA;&lt;p&gt;我目前还没有细看这本书，但是，依然推荐一下，推荐的理由在于作者为这本书附上了全部的R和matlab代码。详情见&lt;a href=&#34;http://www.financialriskforecasting.com/&#34;&gt;这里&lt;/a&gt;。&lt;/p&gt;</description>
    </item>
    <item>
      <title>用四个参数拟合大象</title>
      <link>http://gewutang.cn/2011/06/23/use-four-parameters-to-fit-an-elephant/</link>
      <pubDate>Thu, 23 Jun 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/06/23/use-four-parameters-to-fit-an-elephant/</guid>
      <description>&lt;p&gt;冯·诺依曼说过一句话，&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;With four parameters I can fit an elephant, and with five I can make him wiggle his trunk.&lt;/p&gt;&lt;/blockquote&gt;&#xA;&lt;p&gt;冯·诺依曼说这句话的时候，当然是一种隐喻。他的大致意思是说，当一个模型能够完美拟合一个数据集的时候，不要沾沾自喜或者得意到脱掉下巴。因为之所以出现这种情况很可能是因为模型的参数太多，而不是模型的形式很好。如果给模型足够多的参数，我们实际上可以完美的拟合任何数据集。&lt;/p&gt;&#xA;&lt;p&gt;虽然用四个参数来拟合大象并非是冯·诺依曼的本意，可是打他说出来这句话起，就有很多人前赴后继地研究如何用四个参数来真的拟合出来一头大象。后来，Jurgen Mayer, Khaled Khairy, and Jonathon Howard在论文“Drawing an elephant with four complex parameters”当真实现了这句话。&lt;/p&gt;&#xA;&lt;p&gt;绘制大象的R代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;p1=50-30i&#xA;p2=18+8i&#xA;p3=12-10i&#xA;p4=-14-60i&#xA;p5=40+20i&#xA;&#xA;fourier=function(tt,CC){&#xA;f=numeric(length(tt));&#xA;A=Re(CC);&#xA;B=Im(CC);&#xA;k=1:length(CC);&#xA;for(i in 1:length(tt)){&#xA;co=cos(tt[i]*k)&#xA;si=sin(tt[i]*k)&#xA;f[i]=sum(A*co+B*si)&#xA;f&#xA;}&#xA;f&#xA;}&#xA;&#xA;elephant=function(tt,p1,p2,p3,p4,p5){&#xA;n=6&#xA;Cx=complex(n);&#xA;Cy=complex(n);&#xA;Cx[1]=Re(p1)*1i&#xA;Cx[2]=Re(p2)*1i&#xA;Cx[3]=Re(p3)&#xA;Cx[5]=Re(p4)&#xA;Cy[1]=Im(p4)+Im(p1)*1i&#xA;Cy[2]=Im(p2)*1i&#xA;Cy[3]=Im(p3)*1i&#xA;&#xA;x=fourier(tt,Cx)&#xA;y=fourier(tt,Cy)&#xA;res=cbind(y,-x)&#xA;res&#xA;}&#xA;&#xA;tt=seq(from=0,to=2*pi,length=100)&#xA;el=elephant(tt,p1,p2,p3,p4,p5)&#xA;&#xA;plot(el,type=&amp;quot;l&amp;quot;,col=&amp;quot;red&amp;quot;,ylab=&amp;quot;&amp;quot;,xlab=&amp;quot;&amp;quot;,lwd=2)&#xA;points(Im(p5),Im(p5),pch=20,col=&amp;quot;red&amp;quot;)  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/06/23/use-four-parameters-to-fit-an-elephant/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;</description>
    </item>
    <item>
      <title>fAssets包学习笔记</title>
      <link>http://gewutang.cn/2011/06/21/notes-on-fassets/</link>
      <pubDate>Tue, 21 Jun 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/06/21/notes-on-fassets/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://yishuo.org/wp-content/uploads/2011/06/fAssets%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.pdf&#34;&gt;fAssets学习笔记&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;这是fAssets包的学习笔记。说笔记其实也并不准确。因为这个东西只是我在使用fAssets包的过程中顺手记下的一些散言碎语。本来是没有公布的必要，只是鉴于目前用R做量化分析的人十分缺少成型的书籍和资料，而国内众多对R做量化分析感兴趣的人又没有太多耐心去一点点啃食包中自带的帮助文档。因此，将这个东西公布出来，希望对R做量化分析感兴趣的人能够快速的对fAssets包有一个理解。&lt;/p&gt;&#xA;&lt;p&gt;笔记中肯定有很多理解不够深入甚至错误之处，如果哪位读者发现之后肯指点给我，我将不胜感激。我定当迅速更正，免得误导别人。&lt;/p&gt;</description>
    </item>
    <item>
      <title>COS能带领统计学创造历史么</title>
      <link>http://gewutang.cn/2011/06/13/will-we-creat-history/</link>
      <pubDate>Mon, 13 Jun 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/06/13/will-we-creat-history/</guid>
      <description>&lt;h2 id=&#34;1统计学是什么&#34;&gt;1.统计学是什么？&lt;/h2&gt;&#xA;&lt;p&gt;统计学是什么？是数学？是软件？是调查问卷？街头采访？是国家统计局？还是想会计学类似的终日与报表打交道的学科？这几个疑问应该包揽了大部分人对统计学的认识。&lt;/p&gt;&#xA;&lt;p&gt;我一直觉得国内能搞清楚统计学是什么的人没有几个。即使是统计学的泰斗们他们对统计学的界定也难免失之偏颇。我敢说现在大部分教授统计学的老师倾向于把统计学界定为调查数据，发布数据的一种学科。这样的老师特别注重于数据的获取，比如是电话调查获取还是街头采访获取，还是别的什么。比如说抽样的方式，各种复杂的抽样理论等。获取数据之后通过简单的加工，就可以形成一些指数作为谈资供大众消遣和某些部门参考。持这种观点的人，在教授统计学的时候，很注重抽样理论。受这种教学方式的学生，比较适合从事市场调查类行业。&lt;/p&gt;&#xA;&lt;p&gt;还有一种老师将统计学看做一门工具，就是用来分析数据然后得出一些结论。这些人里面经济学和金融学老师比较多。不过，要说明的是，国内的大部分经济学和金融学老师对统计学认识的非常肤浅。他们通常教的最多的就是回归，一元回归，多元回归，逐步回归，大部分都到此为止了。如果有一个经济学或者金融学老师懂得lasso或者稳健回归的话，那么，他在国内的教坛上绝对是一个异种。&lt;/p&gt;&#xA;&lt;p&gt;还有一种老师，倾向于数理。这种老师希望把统计的数理基础搞的很扎实。我觉得这是很不多的方向。如果一个学统计的人数理基础很扎实的话，稍微在经济或者金融领域开点窍的话，做出点成绩都是理所当然的。但是，在国内存在一种情况。那就是大部分数理基础很好的学生，对统计学的应用都做得不是很好，最典型的就是很多理论超级扎实的同学对统计软件并不是十分精通。更多的人精通一些软件，但是对经济或者金融领域的应用又通很少的窍。这真是让人甚为纠结。而且，数理化还有一个明显的缺点，就是过分注重数学，以至于往往会忽略统计理论的直观性阐述。比如，二项分布与正态分布近似这个例子，如果用高尔顿板演示肯定比推导那一大推公式要好很多。&lt;/p&gt;&#xA;&lt;p&gt;不管怎样，这些在我心里都不是理想的统计学。&lt;/p&gt;&#xA;&lt;h2 id=&#34;2理想的统计学&#34;&gt;2.理想的统计学&lt;/h2&gt;&#xA;&lt;p&gt;我觉得理想的统计学必须是数理、软件和应用并行的一种东西。数理是基础，软件是手段，应用是核心&lt;/p&gt;&#xA;&lt;p&gt;。任何一个理论之所以产生最终都是为了解决现实问题。比如，回归的产生，应该没有哪一个人会认为回归这个东西是统计学家或者数学家为了乐趣而搞出来的吧？比如逐步回归的产生，她的目的就是为了解决变量选择问题的。比如遗传算法，其实就是为了缩小搜索空间而产生的吧。确定了这一点，统计学就很清晰了。当学习统计学理论的时候，要先搞清楚这个理论是解决什么问题的。如果是你你怎么解决这个问题？为什么要用这个方法解决这个问题？这个方法如何用软件实现？如何解读软件的输出结果？如果结果有什么现实意义？结果是否需要根据现实情况进行斧正？这几个问题如果搞清楚了，那么统计学的理论就真的扎实了。剩下的事情就是应用统计了。&lt;/p&gt;&#xA;&lt;p&gt;我现在常看到自己的许多金融和经济的研究生统计学天天揪着garch和arch不放，如果发现一个arch效用就高兴的不得了，如果能做一个egarch模型那就算是佼佼者了。这种情况让人很无语，也很无奈。他们做出来一个arch模型，常常不分析结果就把结论发表了，而且还得发表在不错的期刊上。中国的期刊让人情何以堪？！！&lt;/p&gt;&#xA;&lt;p&gt;我一直梦想着能让国内个搞金融的，搞经济的，搞BI的人多了解一下统计学。告诉他们统计学不是统计局那样的报表。统计学有很多鲜活的有用的模型和理论。告诉他们金融、经济、BI领域的很多结论依靠的都是统计学手段。而他们一方面肤浅的应用者统计学理论，一方面蔑视着学习统计的人们。这让我觉得好像资本主义社会。金融和经济即是资本家，他们占有和应用统计学的成果，还反过头来看不起统计学。&lt;/p&gt;&#xA;&lt;p&gt;统计学在国内被应用的如此广泛，然后却不被任何人尊重。一个简单的统计学理论要被冠以很多五花八门的名头，否则得话就不被人们认可。这真是统计学的悲剧。但是，我觉得，现在是时候，让国内的统计学奋发了。&lt;/p&gt;&#xA;&lt;h2 id=&#34;3无cos不统计&#34;&gt;3.无COS，不统计&lt;/h2&gt;&#xA;&lt;p&gt;要让国内的统计学奋发，需得有一群懂统计，爱统计，普及统计的人存在。能达到这三个条件的人从前就有，但是力量薄弱。直到COS诞生，一切变得与众不同了。&lt;/p&gt;&#xA;&lt;p&gt;COS有一批懂统计的人组成，不仅懂统计理论，而且精于统计软件的应用，什么统计软件大佬SAS，骨干SPSS啊，新秀R等等，以及计量经济学的工匠Eviews，还有被搞金融的人弄得神乎其神的matlab啊，COS统统都有。COS通通都懂，而且乐意教给大家。从此之后，大家再也不用去那些经济学论坛去询问计量和统计学（软件）问题了。&lt;/p&gt;&#xA;&lt;p&gt;COS的目标就是写一批通俗易懂的数据分析文章，宣传统计学思想和统计软件的用法。同时再争取能出版一些数据处理方面的书籍。要达成这个目标就需要更多的统计学高手和学者来加入到COS之中来。&lt;/p&gt;&#xA;&lt;p&gt;希望此事能早日达成。&lt;/p&gt;</description>
    </item>
    <item>
      <title>setSymbolLookup</title>
      <link>http://gewutang.cn/2011/06/02/setsymbollookup/</link>
      <pubDate>Thu, 02 Jun 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/06/02/setsymbollookup/</guid>
      <description>&lt;p&gt;用quantmod获取OHLC数据时，如果不知道所有获取的股票的简称，在使用数据时可能会遇到一些麻烦，比如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)&#xA;getSymbols(&amp;quot;000065.SS&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;获取数据后，敲入:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;000065.SS&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;结果是无法返回所获取的数据。不过，不用着急，这个问题可以用setSymbolLookup来解决：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;setSymbolLookup(myname=list(name=&amp;quot;000065.SS&amp;quot;, src=&amp;quot;yahoo&amp;quot;))  &#xA;getSymbols(&amp;quot;myname&amp;quot;)  &#xA;MYNAME   #这里是大写字母&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>R与量化投资分析入门书单（更新....）</title>
      <link>http://gewutang.cn/2011/05/31/books-on-quantative-investment/</link>
      <pubDate>Tue, 31 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/31/books-on-quantative-investment/</guid>
      <description>&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;David Ruppert：Statistics and Data Analysis for Financial Engineering&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Stefano M. Iacus：Option Pricing and Estimation of Financial Models with R&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Jon Danielsson：Financial Risk Forecasting: The Theory and Practice of Forecasting Market Risk with Implementation in R and Matlab&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Ngai Hang Chan：Time Series: Applications to Finance with R and S-Plus(R)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>获取交易明细数据</title>
      <link>http://gewutang.cn/2011/05/16/use-r-to-get-trade-details/</link>
      <pubDate>Mon, 16 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/16/use-r-to-get-trade-details/</guid>
      <description>&lt;p&gt;或许是因为大学期间经常不吃早饭的原因，最终我的侥幸心理被无情的现实打破，不幸于上周罹患反流性食道炎。因为身体十分不适，所以也懒得写东西。&lt;/p&gt;&#xA;&lt;p&gt;数天前，我在COS上发布了一个获取股市交易明细的R代码。几经修缮，臻于完整。将来我会将它们放入iquant包中。&lt;/p&gt;&#xA;&lt;p&gt;两个示例如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;source(&amp;quot;get.nowdata.r&amp;quot;);  #载入代码&#xA;nowdata=get.nowdata(&amp;quot;000061&amp;quot;);  #获取000061的交易明细数据&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;返回结果如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$symbol&#xA;[1] &amp;quot;000061&amp;quot;&#xA;&#xA;$Company&#xA;[1] &amp;quot;农产品&amp;quot;&#xA;&#xA;$Date&#xA;[1] &amp;quot;2011年05月16日 15:00&amp;quot;&#xA;&#xA;$Details&#xA;Time Volum Price Turnover Orders NIO&#xA;1    09:25:02   133 17.57    23.44   平盘   0&#xA;2    09:30:02   251 17.60    44.21   流入   2&#xA;3    09:30:32    68 17.60    11.97   平盘   0&#xA;4    09:30:52     2 17.65     0.35   流入   2&#xA;5    09:31:02     8 17.78     1.42   流入   2&#xA;6    09:31:12     4 17.65     0.71   流出   1&#xA;7    09:31:22    11 17.80     1.96   流入   2&#xA;8    09:31:32     6 17.78     1.07   流入   2&#xA;9    09:31:42    10 17.66     1.91   平盘   0&#xA;10   09:31:47     6 17.73     1.06   流入   2&#xA;11   09:32:22    10 17.73     1.77   流入   2&#xA;12   09:32:37     3 17.73     0.53   流出   1&#xA;13   09:32:47     8 17.73     1.42   流出   1&#xA;14   09:32:52     4 17.73     0.71   流出   1&#xA;15   09:32:57    74 17.67    13.10   流出   1&#xA;16   09:33:12    10 17.73     1.77   流入   2&#xA;17   09:33:32     4 17.73     0.71   流入   2&#xA;18   09:33:37     6 17.73     1.06   流出   1&#xA;19   09:33:52    14 17.75     2.48   流入   2&#xA;20   09:34:02    12 17.73     2.12   流入   2&#xA;21   09:34:22     2 17.69     0.35   流出   1&#xA;22   09:34:27     3 17.69     0.53   流出   1&#xA;23   09:34:42     2 17.69     0.35   流出   1&#xA;24   09:34:47     1 17.73     0.18   流入   2&#xA;25   09:34:57     9 17.69     1.59   流出   1&#xA;26   09:35:07     1 17.69     0.18   流出   1&#xA;27   09:35:17    82 17.75    14.55   流入   2&#xA;28   09:35:22    13 17.75     2.31   流出   1&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;获得历史数据：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R与单位根检验</title>
      <link>http://gewutang.cn/2011/05/08/use-r-to-do-unitroots-test/</link>
      <pubDate>Sun, 08 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/08/use-r-to-do-unitroots-test/</guid>
      <description>&lt;p&gt;R里面的fUnitRoots包可以轻松实现时间序列的单位根检验。两个主函数分别是adfTest和unitrootTest。对于以应用统计为主要目的的人而言，看懂单位根检验的结果可能比探讨单位根检验的原理更重要。&lt;/p&gt;&#xA;&lt;p&gt;为了清晰地表明有单位根的序列的检验结果和无单位根的序列的检验结果之间的差别，在这里生成两个可对比的单位根序列来举例：&lt;/p&gt;&#xA;&lt;p&gt;首先，载入fUnitRoots包：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(fUnitRoots);&#xA;&#xA;# 生成没有单位根的序列x和由单位根的序列y&#xA;&#xA;x &amp;lt;-  rnorm(1000) &#xA;y &amp;lt;- cumsum(c(0, x)) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h1 id=&#34;对序列x进行adf检验&#34;&gt;对序列x进行adf检验：&lt;/h1&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;adfTest(x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in adfTest(x): p-value smaller than printed p-value&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Title:&#xA;##  Augmented Dickey-Fuller Test&#xA;## &#xA;## Test Results:&#xA;##   PARAMETER:&#xA;##     Lag Order: 1&#xA;##   STATISTIC:&#xA;##     Dickey-Fuller: -21.1357&#xA;##   P VALUE:&#xA;##     0.01 &#xA;## &#xA;## Description:&#xA;##  Mon May  5 17:44:36 2025 by user:&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;注意上述结果中的P值。&lt;/p&gt;&#xA;&lt;p&gt;对序列y进行adf检验：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;adfTest(y) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## Title:&#xA;##  Augmented Dickey-Fuller Test&#xA;## &#xA;## Test Results:&#xA;##   PARAMETER:&#xA;##     Lag Order: 1&#xA;##   STATISTIC:&#xA;##     Dickey-Fuller: 0.8508&#xA;##   P VALUE:&#xA;##     0.8875 &#xA;## &#xA;## Description:&#xA;##  Mon May  5 17:44:36 2025 by user:&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;注意上述结果的p值，跟x序列的结果有什么区别？&lt;/p&gt;</description>
    </item>
    <item>
      <title>在时间序列的线图上添加垂线</title>
      <link>http://gewutang.cn/2011/05/08/add-vertical-lines-to-timeseries-plot/</link>
      <pubDate>Sun, 08 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/08/add-vertical-lines-to-timeseries-plot/</guid>
      <description>&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(xts)&#xA;v &amp;lt;- vector()&#xA;v[1] &amp;lt;- 10&#xA;for(i in 2:100){&#xA;    v[i] &amp;lt;- v[i-1]+rnorm(1, 0, 0.01)&#xA;}&#xA;X &amp;lt;- xts(v, seq(as.Date(&amp;quot;2009-01-01&amp;quot;), by=&amp;quot;day&amp;quot;, length.out=100))&#xA;plot.xts(X)&#xA;# abline(v=X[50,])              #不行&#xA;# abline(v=50)                  #不行&#xA;# abline(v=index(X)[50])             #不行&#xA;abline(v =.index(X)[50],col=&amp;quot;red&amp;quot;)    #不行&#xA;abline(v = (.index(X)[30]), col = &amp;quot;blue&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>用R计算协偏度和协峰度</title>
      <link>http://gewutang.cn/2011/05/08/use-r-to-caculate-coskewness-and-cokurtosis/</link>
      <pubDate>Sun, 08 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/08/use-r-to-caculate-coskewness-and-cokurtosis/</guid>
      <description>&lt;p&gt;在传统的金融理论中，均值方差分析假设正态分布，但现实中的数据往往偏离这一假设。当资产收益率不服从正态分布时，仅用协方差不足以解释风险，这时候协高阶矩，比如协偏度和协峰度）就变得重要了。协偏度和协峰度是用于衡量资产收益率分布的高阶协动特性。&lt;/p&gt;&#xA;&lt;p&gt;协偏度衡量个股收益对市场极端波动的敏感度。低协偏度的股票可能有更高的预期回报，因为投资者偏好正偏而厌恶负偏，导致协偏度溢价效应。这有助于资产定价和组合优化。协峰度反映尾部风险和市场极端波动的协同性。正峰度表示更多极端值，可能意味着更大的市场风险。峰度还可以帮助评估异常值风险和集中度，这对金融风险预警很重要。&lt;/p&gt;&#xA;&lt;p&gt;在金融领域，协偏度和协峰度能更全面评估风险，尤其在市场波动剧烈时更是如此。&lt;/p&gt;&#xA;&lt;p&gt;PerformanceAnalytics包中提供了CoSkewness()和CoKurtosis()函数来计算两个资产间的协偏和协峰度。&lt;/p&gt;&#xA;&lt;p&gt;例如：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(PerformanceAnalytics)  #载入包&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;PerformanceAnalytics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:graphics&#39;:&#xA;## &#xA;##     legend&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;data(managers)                 #加载managers数据集&#xA;CoSkewness(managers[, &amp;quot;HAM2&amp;quot;, drop=FALSE], managers[, &amp;quot;SP500 TR&amp;quot;, drop=FALSE]) #计算协偏度&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] -2.101289e-06&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;CoKurtosis(managers[, &amp;quot;HAM2&amp;quot;, drop=FALSE], managers[, &amp;quot;SP500 TR&amp;quot;, drop=FALSE]) #计算协峰度&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] 2.579066e-06&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>用R进行Jarque-Bera检验</title>
      <link>http://gewutang.cn/2011/05/06/use-r-to-do-jb-test/</link>
      <pubDate>Fri, 06 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/06/use-r-to-do-jb-test/</guid>
      <description>&lt;p&gt;线性回归模型的检验是建立在假设随机误差 &lt;code&gt;\(u_t\)&lt;/code&gt; 服从正态分布的基础之上的。但问题是我们没有办法直接观察到真实的 &lt;code&gt;\(u_t\)&lt;/code&gt; ，那么，如何验证它是否服从正态分布呢？一般来说，会用残差 &lt;code&gt;\(e_t\)&lt;/code&gt; 近似表示 &lt;code&gt;\(u_t\)&lt;/code&gt; ，再通过 &lt;code&gt;\(e_t\)&lt;/code&gt; 的正态性来推断 &lt;code&gt;\(u_t\)&lt;/code&gt; 的正态性。&lt;/p&gt;&#xA;&lt;p&gt;一种比较常用的正态性检验方法叫Jarque-Bera(雅克-贝拉)检验，简称JB检验。其思路大概是这样的：&lt;/p&gt;&#xA;&lt;p&gt;首先，计算偏度系数 &lt;code&gt;\(s_{k}\)&lt;/code&gt; 和 峰度系数 &lt;code&gt;\(k\)&lt;/code&gt; ：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;s_{k}=\frac{\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{3}}{(\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{2})^{\frac{3}{2}} }&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\text{k}=\frac{\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{4}}{(\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{2})^{2} }-3&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;Jarqe和Bera建立了如下检验统计量：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\text{JB}=\frac{n}{6}(sk^{2}+\frac{k^{2}}{4})&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;在正态性分布的假定下，JB统计量渐近服从自由度为2的 &lt;code&gt;\(\chi^2\)&lt;/code&gt; 分布。&lt;/p&gt;&#xA;&lt;p&gt;显然，如果变量服从正态分布，则S为0，K为0，因而JB统计量的值为零。如果变量不是服从正态分布的变量，则JB统计量将为一个逐渐增大值。而针对每一个JB统计量，都可以算出其对应的P值，根据P值和事先确定的置信水平便可以确定是否拒绝变量为正态分布的假设。&lt;/p&gt;&#xA;&lt;p&gt;在R软件中，Jarque-Bera检验可以用tseries包中的jarque.bera.test来实现。我们这里虚构两个数据，第一个例子中的数据服从正态分布，第二例子中的数据是1到200的序列。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(tseries)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;set.seed(123)&#xA;x=rnorm(20)&#xA;jb &amp;lt;- jarque.bera.test(x)&#xA;jb&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## &#x9;Jarque Bera Test&#xA;## &#xA;## data:  x&#xA;## X-squared = 0.081917, df = 2, p-value = 0.9599&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;返回结果中p-value等于0.9598692，大于0.05，因此在95%置信水平下，无法拒绝x服从正态分布的假设。&lt;/p&gt;</description>
    </item>
    <item>
      <title>符号偏误检验</title>
      <link>http://gewutang.cn/2011/05/02/use-r-to-do-sign-bias-test/</link>
      <pubDate>Mon, 02 May 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/05/02/use-r-to-do-sign-bias-test/</guid>
      <description>&lt;p&gt;Engle和Ng（1993）提出了四种检验条件方差对称性的方法，分别是：&lt;/p&gt;&#xA;&lt;p&gt;1.符号偏误检验（Sign Bias Test）&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\epsilon_t=a_1+b_1S_{t-1}^{-}+Z_{1t}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;2.负符号偏误检验(Negative Sign Bias Test)&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\epsilon_t=a_2+b_2S_{t-1}^{-}\epsilon_{t-1}+Z_{2t}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;3.正符号偏误检验（Positive Sign Bias Test）&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\epsilon_t=a_3+b_3S_{t-1}^{+}\epsilon_{t-1}+Z_{3t}&#xA;$$&#xA;其中： &lt;code&gt;\(S_{t-1}^{-}\)&lt;/code&gt; 为虚拟变量，当 &lt;code&gt;\(\epsilon_{t-1}&amp;lt;0\)&lt;/code&gt; 时， &lt;code&gt;\(S_{t-1}^{-}=1\)&lt;/code&gt; ，否则为0。 &lt;code&gt;\(S_{t-1}^{+}=1-S_{t-1}^{-}\)&lt;/code&gt; ，Z为残差。&lt;/p&gt;&#xA;&lt;p&gt;4.联合检验（Joint Test）&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\epsilon_t=c_0+c_1S_{t-1}^{-}+c_2S_{t-1}^{-}\epsilon_{t-1}+c_3S_{t-1}^{+}\epsilon_{t-1}+Z_t&#xA;$$&#xA;联合检验的统计量服从自由度为3的卡方分布。&lt;/p&gt;&#xA;&lt;p&gt;在实践中，可以直接用R软件进行符号偏误检验：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(rgarch)&#xA;data(dmbp)             #载入数据&#xA;spec = ugarchspec()    #设定模型为默认&#xA;fit = ugarchfit(data = dmbp[,1], spec = spec)   # 拟合模型&#xA;signbias(fit)                                   # 提取符号偏误检验结果&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;检验结果如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;                      t-value      prob sig&#xA;Sign Bias          1.50254937 0.1331156    &#xA;Negative Sign Bias 0.02483478 0.9801893    &#xA;Positive Sign Bias 0.79126111 0.4288869    &#xA;Joint Effect       3.07511135 0.3801888  &#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>万历十五年</title>
      <link>http://gewutang.cn/2011/04/27/%E4%B8%87%E5%8E%86%E5%8D%81%E4%BA%94%E5%B9%B4/</link>
      <pubDate>Wed, 27 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/27/%E4%B8%87%E5%8E%86%E5%8D%81%E4%BA%94%E5%B9%B4/</guid>
      <description>&lt;p&gt;前几天，顺手翻了翻黄仁宇写的《万历十五年》，读了几页，兴趣索然，感觉没什么意思。心里想这本久负盛名的书原来读起来不过如此。&lt;/p&gt;&#xA;&lt;p&gt;今天闲着无聊，又拿出来翻了几页。怪了，竟然渐渐为其行文风格所吸引，尤其敬佩他对张居正的描写，不落窠臼。他将张居正作为一个人来描写，而不是一个脸谱化的历史人物，这种风格让人佩服。看来这本书并非浪得虚名。当即决定订购一册，收藏起来，以备日后增值。&lt;/p&gt;&#xA;&lt;p&gt;看来以后判断什么事情，不能够以一时的意见来做定夺，应该反复考量几次才行。&lt;/p&gt;</description>
    </item>
    <item>
      <title>用R计算Treynor系数</title>
      <link>http://gewutang.cn/2011/04/20/use-r-to-caculate-treynor-index/</link>
      <pubDate>Wed, 20 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/20/use-r-to-caculate-treynor-index/</guid>
      <description>&lt;p&gt;Treynor指数是Treynor提出的用以衡量基金绩效的指数。Treynor认为优秀的基金管理者应能够通过投资组合消除所有的非系统性风险，因此，可以用单位系统性风险系数所获得的超额收益率来衡量投资基金的业绩。他采用基金收益率和无风险收益率的差来衡量基金的超额收益率。采用基金投资收益率的 &lt;code&gt;\(\beta\)&lt;/code&gt; 系数作为衡量系统风险的指标。很显然，Treynor指数越大，表示单位系统风险系数对应的超额收益越大，也就代表基金的效益越好。在运作基金或者投资组合时这个指数十分有用。&lt;/p&gt;&#xA;&lt;p&gt;Treynor指数的计算公式如下:&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;T=\frac{r_i-r_f}{\beta_i}&#xA;$$&#xA;其中：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;\(T\)&lt;/code&gt; ：Treynor指数&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(r_f\)&lt;/code&gt; ：无风险收益率&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(r_i\)&lt;/code&gt; ：基金的收益率&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\beta_i\)&lt;/code&gt; ：基金的 &lt;code&gt;\(\beta\)&lt;/code&gt; 系数&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;计算Treynor指数的R代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;library(PerformanceAnalytics)&#xA;TreynorRatio()&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>用R拟合缺项的ARIMA模型</title>
      <link>http://gewutang.cn/2011/04/17/use-r-to-fit-sparse-arma-model/</link>
      <pubDate>Sun, 17 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/17/use-r-to-fit-sparse-arma-model/</guid>
      <description>&lt;p&gt;标准的ARMA（p,q）模型是这样的：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;X_t=a_1X_{t-1}+\ldots+a_pX_{t-p}+\epsilon_t+b_1\epsilon_{t-1}+\ldots+b_q\epsilon_{t-q}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;比如，ARMA(3,2)的形式如下：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;X_t=a_1X_{t-1}+a_2X_{t-2}+a_3X_{t-3}+\epsilon_t+b_1\epsilon_{t-1}+b_2\epsilon_{t-2}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;拟合ARMA（3,2）模型的R代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;arima(lh, order = c(3,0,2))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;运行结果如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Call:&#xA;arima(x = lh, order = c(3, 0, 2))&#xA;&#xA;Coefficients:&#xA;         ar1     ar2      ar3     ma1      ma2  intercept&#xA;      0.0402  0.4567  -0.4164  0.6602  -0.1109     2.3911&#xA;s.e.  0.3952  0.2032   0.2310  0.4403   0.3977     0.1010&#xA;&#xA;sigma^2 estimated as 0.1711:  log likelihood = -26.2,  aic = 66.4&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;代码清晰易懂。order=c(3,0,2)中的3便是模型中的p，2是模型中的q。中间的0是序列X在建立ARMA模型前的差分次数。&lt;/p&gt;&#xA;&lt;p&gt;有时候，我们不需要拟合完整的ARMA模型。比如，要拟合的模型如下：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;X_t=a_1X_{t-1}+a_3X_{t-3}+\epsilon_t+b_1\epsilon_{t-1}+b_2\epsilon_{t-2}&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;虽然模型的最高阶依然是（3,2）可是AR项里面缺少了第二项。其实，这个问题解决起来很简单的。分3步：&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;观察完整的ARMA模型有几个参数，上例中共有6个参数。&lt;/li&gt;&#xA;&lt;li&gt;观察缺失项的参数在原模型参数中的位置，上例中缺失项是6个参数中的第2个参数。&lt;/li&gt;&#xA;&lt;li&gt;在arima()函数中，加入fixed=c(NA,0,NA,NA,NA,NA)，transform.pars = FALSE。&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;上例中的R代码如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;arima(lh, order = c(3,0,2),fixed=c(NA,0,NA,NA,NA,NA),transform.pars = FALSE)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;返回结果如下：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R获取股票数据</title>
      <link>http://gewutang.cn/2011/04/14/use-r-to-get-trading-data/</link>
      <pubDate>Thu, 14 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/14/use-r-to-get-trading-data/</guid>
      <description>&lt;p&gt;R中有多个包都提供了从线上数据库获取股票数据的方法。如果非得在其中找出一个最好的，quantmod当之无愧。譬如下载沪市大盘数据，代码可以是：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)   &#xA;SSE &amp;lt;- getSymbols(&amp;quot;000001.SS&amp;quot;,auto.assign=FALSE)   &#xA;head(SSE) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;或者：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)  &#xA;setSymbolLookup(SSE=list(name=&amp;quot;000001.SS&amp;quot;, src=&amp;quot;yahoo&amp;quot;))  &#xA;getSymbols(&amp;quot;SSE&amp;quot;)  &#xA;head(SSE)  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;以上是Jeff Ryan（quantmod作者）推荐过的方法。不过，后来JoshuaUlrich在他的邮件中给出了一个更简洁的方法：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)  &#xA;getSymbols(&amp;quot;^SSEC&amp;quot;)  &#xA;head(SSEC)  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;虽然quantmod的包出自Jeff Ryan之手，显而易见，在这一点上Joshua Ulrich是青出于蓝而胜于蓝。&lt;/p&gt;</description>
    </item>
    <item>
      <title>quantmod：R中的金融分析包</title>
      <link>http://gewutang.cn/2011/04/10/notes-about-quantmod/</link>
      <pubDate>Sun, 10 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/10/notes-about-quantmod/</guid>
      <description>&lt;p&gt;大概两年前，写了一份quantmod的读书笔记。当时只是草草完成，不足之处很多。于是，一直以来都有修缮之心，思忖良久，前段时间终于开始着手，目前大致确定了内容。先将完成的部分记在&lt;a href=&#34;https://github.com/dengyishuo/Notes/tree/master/quantmod&#34;&gt;这里&lt;/a&gt;。&lt;/p&gt;</description>
    </item>
    <item>
      <title>用R拟合Vasicek模型</title>
      <link>http://gewutang.cn/2011/04/10/use-r-to-fit-vasicek-model/</link>
      <pubDate>Sun, 10 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/10/use-r-to-fit-vasicek-model/</guid>
      <description>&lt;p&gt;Vasicek模型几乎可以说是最常见的利率模型。在金融领域有着很大的用处，可以用R来做Vasicek模型的人显然不够多。&lt;/p&gt;&#xA;&lt;p&gt;这里给出一个拟合Vasicek模型的代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;#Vasicek negloglike函数:&#xA;VASICEKnegloglike&amp;lt;-function(param,data,times){ n.obs&amp;lt;-length(data)&#xA;  dum&amp;lt;-sort.list(times)&#xA;  data&amp;lt;-data[dum]&#xA;  times&amp;lt;-times[dum]&#xA;  delta&amp;lt;-diff(times,1)&#xA;  mv&amp;lt;-data[1:(n.obs-1)]*exp(-param[2]*delta)+param[1]*(1-exp(-param[2]*delta))&#xA;  variance&amp;lt;-param[3]^2*(1-exp(-2*param[2]*delta))/(2*param[2])&#xA;  VASICEKnegloglike&amp;lt;--sum(log(dnorm(data[2:n.obs],mv,sqrt(variance))))/n.obs&#xA;}&#xA;&#xA;# VASICEKyield函数&#xA;VASICEKyield&amp;lt;-function(r,tau,Pparam,riskpremium=0)&#xA;{ b&amp;lt;-Pparam[1]+riskpremium&#xA;  a&amp;lt;-Pparam[2]&#xA;  sig&amp;lt;-Pparam[3]&#xA;  Btau&amp;lt;-(1-exp(-a*tau))/a&#xA;  Atau&amp;lt;-((Btau-tau)*(a^2*b-0.5*sig^2)/a^2 - sig^2*Btau^2/(4*a))&#xA;  VASICEKyield&amp;lt;-r*Btau/tau-Atau/tau&#xA;}&#xA;&#xA;# 数据实例&#xA;maturities&amp;lt;-c(0.25,0.5,1,2,5,10)&#xA;spliceddata&amp;lt;-matrix(scan(&amp;quot;http://www.math.ku.dk/~rolf/teaching/mfe04/USrates.52to04.dat&amp;quot;),byrow=T,ncol=7)&#xA;obs&amp;lt;-spliceddata[,2]&#xA;N&amp;lt;-length(obs)-1&#xA;dt&amp;lt;-1/12&#xA;data&amp;lt;-obs[2:(N+1)]&#xA;lagdata&amp;lt;-obs[1:N]&#xA;dates&amp;lt;-spliceddata[,1]&#xA;&#xA;#Closed form estimators&#xA;bhat&amp;lt;-(sum(data*lagdata) - sum(data)*sum(lagdata)/N)/(sum(lagdata*lagdata) - sum(lagdata)*sum(lagdata)/N)&#xA;kappahat&amp;lt;--log(bhat)/dt&#xA;ahat&amp;lt;-sum(data)/N-bhat*sum(lagdata)/N&#xA;thetahat&amp;lt;-ahat/(1-bhat)&#xA;s2hat&amp;lt;-sum((data-lagdata*bhat-ahat)^2)/N&#xA;sigmahat&amp;lt;-sqrt(2*kappahat*s2hat/(1-bhat^2))&#xA;#plot the data (or more accurately, the short rate)&#xA;plot(dates[2:(N+1)],data,type=&#39;l&#39;, xlab=&amp;quot;date&amp;quot;, ylab=&amp;quot;US 3M rate&amp;quot;, main=&amp;quot;US short rate 1952-2004&amp;quot;)&#xA;abline(h=thetahat, lty=3)&#xA;text(dates[10], thetahat, &amp;quot;thetahat&amp;quot;)&#xA;&#xA;tstVAS&amp;lt;-optim(par=c(thetahat,kappahat,sigmahat),&#xA;              fn=VASICEKnegloglike,method = &amp;quot;BFGS&amp;quot;, data=obs,times=dates)&#xA;#analyze average yield curves&#xA;meanyield&amp;lt;-2:7&#xA;for(i in 1:6) meanyield[i]&amp;lt;-mean(spliceddata[,i+1])&#xA;plot(maturities,meanyield,type=&#39;l&#39;,ylim=c(0.05,0.068),xlim=c(0,14),&#xA;     ylab=&amp;quot;mean/average/typical yield&amp;quot;,&#xA;&#x9; main=&amp;quot;Typical yield curves w/ US  1952-2004 data&amp;quot;)&#xA;text(maturities[6],meanyield[6], &amp;quot;Average observed yield curve&amp;quot;)&#xA;points(maturities,VASICEKyield(meanyield[1],maturities,tstVAS$par,riskpremium=0),type=&#39;l&#39;,lty=2)&#xA;text(maturities[6],VASICEKyield(meanyield[1],maturities[6],tstVAS$par,riskpremium=0)+0.001,&#xA;                       &amp;quot;Typical Vasicek model curve, I (r0 = avr. short rate, risk premium=0) &amp;quot;)&#xA;points(maturities,VASICEKyield(thetahat,maturities,tstVAS$par,riskpremium=0),type=&#39;l&#39;,lty=2)&#xA;text(maturities[6],VASICEKyield(thetahat,maturities[6],tstVAS$par,riskpremium=0)-0.0005,&amp;quot;Typical Vasicek model curve, II (r0 = thetahat, risk premium=0) &amp;quot;)&#xA;&#xA;rpfit&amp;lt;-function(rp){&#xA;rpfit&amp;lt;-sum(abs(meanyield-VASICEKyield(meanyield[1],maturities,tstVAS$par,riskpremium=rp)))&#xA;}&#xA;&#xA;rp&amp;lt;-optim(par=c(0),fn=rpfit,method = &amp;quot;BFGS&amp;quot;)$par&#xA;&#xA;theory&amp;lt;-VASICEKyield(meanyield[1],maturities,tstVAS$par,riskpremium=rp)&#xA;points(maturities,theory,type=&#39;l&#39;,lty=2)&#xA;&#xA;text(maturities[6],theory[6],&amp;quot;Typical Vasicek model curve, III (r0=avr. short rate, risk premium=&#39;what fits best&#39;) &amp;quot;)&#xA;&#xA;plot(maturities,spliceddata[(N+1),2:7],type=&#39;l&#39;,ylab=&amp;quot;yield&amp;quot;,ylim=c(0.01,0.06),xlim=c(0,13), main=&amp;quot;On the last day in the sample&amp;quot;)&#xA;&#xA;text(maturities[6],spliceddata[(N+1),7], &amp;quot;Observed yield curve&amp;quot;)&#xA;&#xA;points(maturities,VASICEKyield(spliceddata[(N+1),3],maturities,tstVAS$par,riskpremium=rp),type=&#39;l&#39;,lty=2)&#xA;&#xA;text(maturities[6],VASICEKyield(spliceddata[(N+1),3],maturities[6],tstVAS$par,riskpremium=rp),&amp;quot;Vasicek model &#39;prediction&#39; (w/ all the estimates) &amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>从Tsay的书看R的未来</title>
      <link>http://gewutang.cn/2011/04/09/predict-r-based-on-the-change-of-tsays-book/</link>
      <pubDate>Sat, 09 Apr 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/04/09/predict-r-based-on-the-change-of-tsays-book/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://faculty.chicagobooth.edu/ruey.tsay/&#34;&gt;Tsay&lt;/a&gt;写的最著名的书叫&lt;a href=&#34;http://faculty.chicagobooth.edu/ruey.tsay/teaching/&#34;&gt;《Analysis of Financial Time Series》&lt;/a&gt;，这本书目前已经到了第三版。可能很少有人注意到，第三版跟第二版比在书籍介绍里有一个微妙的变化：&lt;/p&gt;&#xA;&lt;p&gt;第二版的部分&lt;a href=&#34;http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts2/&#34;&gt;书籍介绍&lt;/a&gt;如下：&#xA;Software packages used in the book:&#xA;SCA (Scientific Computing Associates),&#xA;S-Plus with FinMetrics (Insightful)&#xA;RATS (Regression Analysis of Time Series),&#xA;&lt;strong&gt;and&lt;/strong&gt;&#xA;** R with RMetrics (newer version is recommended.)**&#xA;Instructions and some demonstrations for using R and S-Plus&#xA;can be found in my teaching web: R-demo&lt;/p&gt;&#xA;&lt;p&gt;第三版的部分&lt;a href=&#34;http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/&#34;&gt;书籍介绍&lt;/a&gt;如下：&#xA;&lt;strong&gt;The main software package used in the book is R:&lt;/strong&gt;&#xA;Other packages used are&#xA;SCA (Scientific Computing Associates),&#xA;S-Plus with FinMetrics (Insightful)&#xA;RATS (Regression Analysis of Time Series).&lt;/p&gt;</description>
    </item>
    <item>
      <title>应该建立怎样的网站</title>
      <link>http://gewutang.cn/2011/03/14/what-type-of-website-should-we-have/</link>
      <pubDate>Mon, 14 Mar 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/03/14/what-type-of-website-should-we-have/</guid>
      <description>&lt;p&gt;最近，我思考了一下这个问题：什么样的网站管理模式是最好的？思考的结果是自我管理式的网站是最好的。&lt;/p&gt;&#xA;&lt;p&gt;当然，这个“自我”并非是指网站的创建团队，而应当是网站的用户。把网站处理成一个平台，弱化管理团队的功能，使管理团队除了技术维护网站之外，退化到普通用户的地位。&lt;/p&gt;&#xA;&lt;p&gt;让网站用户自己发表自己的话题、让用户彼此评价。就像是一个SNS。学术网站也可以采用SNS的方式，虽然现在还很少学术网站敢破旧立新采用这个模式。采用SNS自我管理模式的好处之一是能够激起用户的存在感和参与感。此外，在用户自我管理的过程中，精华的话题和帖子会在岁月中留存下来。采用SNS自我管理模式的网站从来不用担心话题和文章的来源问题。&lt;/p&gt;&#xA;&lt;p&gt;周日COS管理人员的网络会议上，益辉说他并不担心&lt;code&gt;COS&lt;/code&gt;的文章来源问题。我的感觉与他恰恰相反。我最担心的就是&lt;code&gt;COS&lt;/code&gt;的文章来源问题。现在&lt;code&gt;COS&lt;/code&gt;主站的文章更新有些缓慢。为了解决这个问题，我认为cos的文章来源不应当限于主站编辑部的作者们，而是应当发动所有的用户。&lt;code&gt;COS&lt;/code&gt;主站的作者们也应当退化到普通用户的地位，要敢于与用户去竞争，如果作者们足够优秀，那么，他们的文章在众多的文章中会被筛选留存的。采用这样的方式应该不会增加过多的管理困难，然而，却可以长久的解决网站内容的更新问题。&lt;/p&gt;&#xA;&lt;p&gt;下午看到一则消息：&lt;/p&gt;&#xA;&lt;p&gt;刚开完“两会”的陈天桥亲自到酷6，向管理层布道：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;互联网处在以技术推动，并通过产品呈现差异化需求的时代，必需对用户需求的把握推出差异化产品。&lt;/li&gt;&#xA;&lt;li&gt;用户自发的传播、评论将是互联网传播的主导力量；&lt;/li&gt;&#xA;&lt;li&gt;形式越来越“轻薄化”，传播速度越来越快；&lt;/li&gt;&#xA;&lt;li&gt;互联网传播形式必需是开放的。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;陈天桥说的第2条正是指明了这个网络时代的生存核心。只有让用户成为网站的主体，网站才能实现自我管理，继而才能走向自我成长！&lt;/p&gt;&#xA;&lt;p&gt;然而，采用SNS自我管理模式有一个缺点，即可能走向“民粹主义”。法国大革命的历史经验告诉世人：“民粹主义”在政治哲学上是能够站的得住脚的，只是在政治实践上难免造成混乱。因此，要对民粹作品进行筛选，以保持的精英地位。对于&lt;code&gt;COS&lt;/code&gt;的发展而言，&lt;code&gt;COS&lt;/code&gt;应当有一批专家式的人物，来把握数据分析理论和数据分析方式的正确性，以及保证这些数据分析理论和数据分析方法的更新。&lt;/p&gt;&#xA;&lt;p&gt;这样看来，想法已经走入了一个怪圈。一方面需要让用户自我管理，一方面又要避免“民粹主义”。这是一个常见的悖论。高手难免寂寞，时尚又往往容易流于浅薄。难道没有一种模式既能保持精英主义，又能博得大众欢迎么？肯定是有的！尚需探索。大卫·李嘉图和约翰·梅纳德·凯恩斯最可贵的地方在于既保持了在经济理论的前沿地位，同时又利用经济理论占据了足够的财富。&lt;/p&gt;</description>
    </item>
    <item>
      <title>男人通常在想什么</title>
      <link>http://gewutang.cn/2011/03/07/what-every-man-thinks-about-apart-from-sex/</link>
      <pubDate>Mon, 07 Mar 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/03/07/what-every-man-thinks-about-apart-from-sex/</guid>
      <description>&lt;p&gt;英国最近出了一本书叫《What Every Man Thinks About Apart From Sex》。推出之后异常爆火，在Amazon一开卖就被抢购一空。购买者主要是女性以及想了解自己的部分男性。&lt;/p&gt;&#xA;&lt;p&gt;这本书设计的很有特色：除却封面，里面200多页打开后差不多全都是空白。这说明除了性, 男人想的&amp;hellip;就沒有啦!&lt;/p&gt;&#xA;&lt;p&gt;我看到之后爆笑不止，但作者的思维很赞。只是可怜那些买书者，买来的书只能当笔记本用了！&lt;/p&gt;</description>
    </item>
    <item>
      <title>CPI数据经过调整吗</title>
      <link>http://gewutang.cn/2011/02/20/is-cpi-true/</link>
      <pubDate>Sun, 20 Feb 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/02/20/is-cpi-true/</guid>
      <description>&lt;p&gt;不久前，2011年1月份的CPI数据出炉。这个引人瞩目的数据低于人们对于1月份CPI破5的预期，尴尬地站在4.9的位置上。坊间纷纷传言说统计局人为调低食品类权重而达到了此效果。孰真孰假？不妨来算一下。&lt;/p&gt;&#xA;&lt;p&gt;国家统计局公布的居民价格指数的如数据1：（统计局所公布的2010年1月至11月的数据）：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Index   y   x1   x2   x3   x4   x5   x6   x7   x8&#xA;1  101.5 103.7 101.5 99.6  98.9 102.3  99.5  98.8 102.5&#xA;2  102.7 106.2 101.6 98.7  99.2 102.4 100.1 100.8 103.0&#xA;3  102.4 105.2 101.7 98.9  99.3 102.5 100.0 100.3 103.3&#xA;4  102.8 105.9 101.7 98.7  99.5 102.8 100.0 100.4 104.5&#xA;5  103.1 106.1 101.7 98.8  99.7 103.2 100.1 100.6 105.0&#xA;6  102.9 105.7 101.7 99.0 100.0 103.2  99.7 100.9 105.0&#xA;7  103.3 106.8 101.6 99.2 100.2 103.3  99.3 101.1 104.8&#xA;8  103.5 107.5 101.5 98.8 100.4 103.3  99.4 101.2 104.4&#xA;9  103.6 108.0 101.4 98.5 100.4 103.4  99.3 101.2 104.3&#xA;10 104.4 110.1 101.5 98.7 100.5 103.7  99.5 100.9 104.9&#xA;11 105.1 111.7 101.6 99.3 100.7 104.0  99.3 100.6 105.8&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;各符号的含义如下：&lt;/p&gt;</description>
    </item>
    <item>
      <title>2010年中国与美国经济对比</title>
      <link>http://gewutang.cn/2011/02/12/china-vs-usa/</link>
      <pubDate>Sat, 12 Feb 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/02/12/china-vs-usa/</guid>
      <description>&lt;p&gt;中国和美国现在的实力越来越接近，也许在未来会发生很多微妙的事情。对于这个实际而言，有两个实力接近的国家历来不是什么好事。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(WDI)&#xA;library(tidyverse)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──&#xA;## ✔ dplyr     1.1.4     ✔ readr     2.1.5&#xA;## ✔ forcats   1.0.0     ✔ stringr   1.5.1&#xA;## ✔ ggplot2   3.5.2     ✔ tibble    3.2.1&#xA;## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1&#xA;## ✔ purrr     1.0.4     &#xA;## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──&#xA;## ✖ dplyr::filter() masks stats::filter()&#xA;## ✖ dplyr::lag()    masks stats::lag()&#xA;## ℹ Use the conflicted package (&amp;lt;http://conflicted.r-lib.org/&amp;gt;) to force all conflicts to become errors&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(plotly)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;plotly&#39;&#xA;## &#xA;## The following object is masked from &#39;package:ggplot2&#39;:&#xA;## &#xA;##     last_plot&#xA;## &#xA;## The following object is masked from &#39;package:stats&#39;:&#xA;## &#xA;##     filter&#xA;## &#xA;## The following object is masked from &#39;package:graphics&#39;:&#xA;## &#xA;##     layout&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(dplyr)&#xA;library(ggplot2)&#xA;&#xA;# 获取GDP数据（当前美元价）&#xA;gdp_data &amp;lt;- WDI(&#xA;  country = c(&amp;quot;CN&amp;quot;, &amp;quot;US&amp;quot;),&#xA;  indicator = c(&amp;quot;NY.GDP.MKTP.CD&amp;quot;, &amp;quot;NY.GDP.PCAP.CD&amp;quot;),  # GDP总量与人均GDP&#xA;  start = 1978,&#xA;  end = 2010&#xA;) %&amp;gt;%&#xA;  rename(gdp = NY.GDP.MKTP.CD, gdp_per_cap = NY.GDP.PCAP.CD) %&amp;gt;%&#xA;  mutate(country = case_when(&#xA;    iso2c == &amp;quot;CN&amp;quot; ~ &amp;quot;中国&amp;quot;,&#xA;    iso2c == &amp;quot;US&amp;quot; ~ &amp;quot;美国&amp;quot;&#xA;  ))&#xA;  &#xA;  &#xA;  p1 &amp;lt;- ggplot(gdp_data, aes(x=year, y=gdp/1e12, color=country)) +&#xA;  geom_line(linewidth=1.2) +&#xA;  scale_y_log10(&#xA;    &amp;quot;GDP（万亿美元）&amp;quot;,&#xA;    breaks = c(0.1, 1, 10),&#xA;    labels = c(&amp;quot;0.1&amp;quot;, &amp;quot;1&amp;quot;, &amp;quot;10&amp;quot;)&#xA;  ) +&#xA;  scale_color_manual(values=c(&amp;quot;#D62728&amp;quot;, &amp;quot;#2CA02C&amp;quot;)) +&#xA;  labs(title=&amp;quot;1978-2010中美GDP增长轨迹对比&amp;quot;,&#xA;       subtitle=&amp;quot;数据来源：世界银行WDI数据库（经对数转换处理）&amp;quot;) +&#xA;  theme_minimal(base_family=&amp;quot;PingFang SC&amp;quot;)&#xA;  p1&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/02/12/china-vs-usa/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;  # 保存为pdf文件&#xA;  # ggsave(&amp;quot;Sino-US_gdp.pdf&amp;quot;, device=cairo_pdf, width=14, height=9, dpi=300)&#xA;  # 转化为交互图形&#xA;  # ggplotly(p1)&#xA;  &#xA;  gdp_data &amp;lt;- WDI(&#xA;  country = c(&amp;quot;CN&amp;quot;, &amp;quot;US&amp;quot;),&#xA;  indicator = c(&amp;quot;NY.GDP.MKTP.CD&amp;quot;, &amp;quot;NY.GDP.PCAP.CD&amp;quot;),  &#xA;  start = 1978,&#xA;  end = 2010&#xA;) %&amp;gt;%&#xA;  rename(gdp = NY.GDP.MKTP.CD, gdp_per_cap = NY.GDP.PCAP.CD) %&amp;gt;%&#xA;  mutate(global_share = gdp / sum(gdp))  %&amp;gt;%&#xA;  group_by(year) &#xA;  p2 &amp;lt;- ggplot(gdp_data, aes(x=year, y=global_share, fill=country)) +&#xA;  geom_area(alpha=0.8) +&#xA;  scale_y_continuous(labels=scales::percent) +&#xA;  labs(y=&amp;quot;占全球GDP比重&amp;quot;, title=&amp;quot;全球经济格局演变&amp;quot;)&#xA;  p2&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/02/12/china-vs-usa/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;  # 保存为pdf文件&#xA;  # ggsave(&amp;quot;Sino-US_Comparison.pdf&amp;quot;, device=cairo_pdf, width=14, height=9, dpi=300)&#xA;  # 转化为交互图表&#xA;  # ggplotly(p2)  &#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>2010年各地房价排行榜揭晓</title>
      <link>http://gewutang.cn/2011/02/10/house-price/</link>
      <pubDate>Thu, 10 Feb 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/02/10/house-price/</guid>
      <description>&lt;p&gt;今日看到一篇新闻，该新闻称《2010年中国城市房价排行榜》近日揭晓，杭州房价跃居榜首。该文列出了2010年7月中国城市房价排行榜数据如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;cities &amp;lt;- readRDS(&amp;quot;cities.rds&amp;quot;)&#xA;print(cities)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;##        city   lon      lat hours_eprice&#xA;## 1      杭州 30.16   120.10        25840&#xA;## 2      北京 39.55   116.24        22310&#xA;## 3      温州 28.01   120.39        18854&#xA;## 4      三亚 18.14   109.31        18319&#xA;## 5      深圳 22.33   114.07        16978&#xA;## 6      宁波 29.52   121.33        13438&#xA;## 7      广州 23.08   113.14        12560&#xA;## 8      南京 32.03   118.46        12016&#xA;## 9      舟山 30.01   122.06        10500&#xA;## 10     绍兴 30.00   120.34        10105&#xA;## 11     珠海 22.17   113.34         9747&#xA;## 12     大连 38.55   121.36         9678&#xA;## 13     厦门 24.27   118.06         9660&#xA;## 14     苏州 31.19   120.37         9103&#xA;## 15     台州 28.41   121.27         8967&#xA;## 16     青岛 36.03   120.18         8962&#xA;## 17     天津 39.02   117.12         8958&#xA;## 18     南通 32.01   120.51         8950&#xA;## 19     福州 26.05   119.18         8666&#xA;## 20     湖州 30.52   120.06         8220&#xA;## 21   哈尔滨 45.44   126.36         7939&#xA;## 22     无锡 31.34   120.18         7843&#xA;## 23     佛山 23.02   113.06         7822&#xA;## 24     济南 36.40   117.00         7760&#xA;## 25     泉州 24.56   118.36         7680&#xA;## 26     嘉兴 30.46   120.45         7587&#xA;## 27     海口 20.02   110.20         7288&#xA;## 28     东莞 23.02   113.45         7023&#xA;## 29     成都 30.40   104.04         6630&#xA;## 30     合肥 31.52   117.17         6255&#xA;## 31     武汉 30.35   114.17         6196&#xA;## 32     南宁 22.48   108.19         6137&#xA;## 33     唐山 39.36   118.11         6098&#xA;## 34     昆明 25.04   102.42         6006&#xA;## 35     金华 29.07   119.39         6000&#xA;## 36     中山 22.31   113.22         5988&#xA;## 37     烟台 37.32   121.24         5835&#xA;## 38     重庆 29.35   106.33         5720&#xA;## 39     日照 35.23   119.32         5700&#xA;## 40     郑州 34.46 11340.00         5689&#xA;## 41     惠州 23.05   114.22         5649&#xA;## 42     宜昌 30.42   111.17         5637&#xA;## 43     太原 37.54   112.33         5635&#xA;## 44     扬州 32.23   119.26         5630&#xA;## 45     南昌 28.40   115.55         5573&#xA;## 46     长春 43.54   125.19         5445&#xA;## 47     兰州 36.04   103.51         5440&#xA;## 48     西安 34.17   108.57         5398&#xA;## 49     威海 37.31   122.07         5363&#xA;## 50     长沙 28.12   112.59         5339&#xA;## 51     常州 31.47   119.58         5302&#xA;## 52     江门 22.35   113.04         5205&#xA;## 53     鞍山 41.07   123.00         5150&#xA;## 54   秦皇岛 39.55   119.35         5114&#xA;## 55     漳州 24.31   117.39         5110&#xA;## 56     泰安 36.11   117.08         5100&#xA;## 57     镇江 32.11   119.27         5052&#xA;## 58     承德 40.59   117.57         5016&#xA;## 59     沈阳 41.48   123.25         4980&#xA;## 60 呼和浩特 40.48   111.41         4920&#xA;## 61   石家庄 38.02   114.30         4874&#xA;## 62     桂林 25.17   110.17         4865&#xA;## 63     泰州 32.30   119.54         4830&#xA;## 64     柳州 23.19   109.24         4822&#xA;## 65     大庆 46.36   125.01         4814&#xA;## 66     湛江 21.11   110.24         4772&#xA;## 67     九江 29.43   115.58         4771&#xA;## 68     银川 38.27   106.16         4690&#xA;## 69     徐州 34.15   117.11         4632&#xA;## 70     贵阳 26.35   106.42         4534&#xA;## 71 乌鲁木齐 43.45    87.36         4410&#xA;## 72     东营 37.27   118.30         4370&#xA;## 73     汕头 23.22   116.41         4330&#xA;## 74     邯郸 36.36   114.28         4300&#xA;## 75     包头 40.39   109.49         4225&#xA;## 76     洛阳 34.41   112.27         4207&#xA;## 77     延安 36.35   109.28         4135&#xA;## 78     吉林 43.52   126.33         4008&#xA;## 79     济宁 35.23   116.33         3950&#xA;## 80     北海 21.28   109.07         3890&#xA;## 81     开封 34.47   114.21         3737&#xA;## 82   葫芦岛 40.45   120.51         3719&#xA;## 83     潍坊 36.43   119.06         3689&#xA;## 84 齐齐哈尔 47.20   123.57         3671&#xA;## 85     清远 23.42   113.01         3625&#xA;## 86     营口 40.39   122.13         3580&#xA;## 87     锦州 41.07   121.09         3550&#xA;## 88     西宁 36.38   101.48         3440&#xA;## 89     襄樊 32.02   112.08         3432&#xA;## 90     拉萨 29.39    91.08         3400&#xA;## 91     淄博 36.48   118.03         3300&#xA;## 92     丹东 40.08   124.22         3273&#xA;## 93     喀什 39.30    75.59         3250&#xA;## 94   张家口 40.48   114.53         3210&#xA;## 95 克拉玛依 45.36    84.51         3200&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;用R将其可视化：&lt;/p&gt;</description>
    </item>
    <item>
      <title>R与时间序列的平稳性</title>
      <link>http://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/</link>
      <pubDate>Sun, 09 Jan 2011 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/</guid>
      <description>&lt;p&gt;“协整”算是计量经济学里面的明星概念，不了解计量经济学的人很容易被它笼罩的计量经济学光环给吓到。但事实上，协整并不神秘，是平易近人。&lt;/p&gt;&#xA;&lt;p&gt;要了解“协整”，首先得提到一个名词——平稳。&lt;/p&gt;&#xA;&lt;h2 id=&#34;什么是平稳时间序列&#34;&gt;什么是平稳时间序列&lt;/h2&gt;&#xA;&lt;p&gt;金融领域有很多类型的时间序列，学者们为了研究的方便通常会对这些时间序列进行分类。比如，按照时间序列数据产生时间间隔的长短分为高频时间序列和非高频时间序列。按照时间序列的平稳性与否，便可以将时间序列分为平稳的时间序列和非平稳的时间序列。&lt;/p&gt;&#xA;&lt;p&gt;判断某个时间序列平稳与否主要看一下三点：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;时间序列（随机过程）的均值是否是常数?或者说时间序列（随机过程）的均值是不是跟时间无关？如果不是常数则该时间序列是非平稳的；如果是常数，则该时间序列可能是平稳的，但不肯定，须进入第二步。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;时间序列（随机过程）的方差是否是常数？或者说时间序列（随机过程）的方差是不是跟时间无关？如果不是常数则该时间序列是非平稳的；如果是常数，则该时间序列可能是平稳的，但不肯定，须进入第三步。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;任意两个时期的时间序列之间的协方差是否仅仅依赖于两个时间序列的时间间隔。&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;如果一个时间序列同时满足上述三个条件，那么，该时间序列就是平稳的。&lt;/p&gt;&#xA;&lt;p&gt;由于均值、方差和协方差可以被统一的成为统计特性，因此也可以说如果时间序列的统计特性不随着时间的推移而发生变化，则说明时间序列是平稳的。&lt;/p&gt;&#xA;&lt;p&gt;平稳时间序列还可以进一步细分。根据平稳时间序列进行差分的次数，可以将平稳时间序列分为不同阶的平稳时间序列。如果一个时间序列本身是平稳的，那么该时间序列可被记作I(0),括号里面的0表示该时间序列没有进行差分。如果一个时间序列一阶差分之后平稳，那么可被记作I(1),以此类推。&lt;/p&gt;&#xA;&lt;h2 id=&#34;平稳性的检验&#34;&gt;平稳性的检验&lt;/h2&gt;&#xA;&lt;p&gt;在应用过程中，时间序列的平稳性的判断要简单的多。常用的判断时间序列平稳性的方法有两个：图示法和单位根检验法。&lt;/p&gt;&#xA;&lt;p&gt;图示法，顾名思义，就是画出时间序列的时序图，来目测时间序列是否平稳。如果画出的时间序列不存在明显的趋势，那么时间序列可能是平稳的。这个方法比较随意和主观，因此，只能作为辅助判断的手段。&lt;/p&gt;&#xA;&lt;p&gt;单位根检验法是一个理论基础扎实的判断手段，单位根检验的方法非常多，一般常用的有DF检验(Dickey-Fuller Test)，ADF检验(Augmented Dickey-Fuller Test)和PP检验(Phillips-Perron Test)。这三个检验的理论基础在这里不讲,因为除却数理统计的专业人员，大部分人都不必深究这些理论，现实中只要懂得这些检验用软件的实现方法就可以了。&lt;/p&gt;&#xA;&lt;p&gt;下面以R为例：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;#生成两个时间序列：&#xA;&#xA;x=rnorm(500);  #没有单位根&#xA;&#xA;y=cumsum(x);   #有单位根&#xA;&#xA;# 绘制时序图&#xA;&#xA;plot.ts(x);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;plot.ts(y);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# DF.test检验&#xA;&#xA;library(tseries) #载入tseries包&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;adf.test(x)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning in adf.test(x): p-value smaller than printed p-value&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## &#x9;Augmented Dickey-Fuller Test&#xA;## &#xA;## data:  x&#xA;## Dickey-Fuller = -7.863, Lag order = 7, p-value = 0.01&#xA;## alternative hypothesis: stationary&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;P值等于0.01，拒绝x是原假设（原假设为非平稳），即可以认为x是平稳的。&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于极值理论的 VaR 计算及实证分析</title>
      <link>http://gewutang.cn/2010/12/29/var-caculation-and-evt/</link>
      <pubDate>Wed, 29 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/29/var-caculation-and-evt/</guid>
      <description>&lt;div id=&#34;一引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;一、引言&lt;/h1&gt;&#xA;&lt;p&gt;风险价值（VaR）作为衡量金融资产极端损失的核心指标，传统方法（如 GARCH 模型）虽能捕捉波动聚类特性，但对尾部风险的刻画仍依赖于整体分布假设。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;极值理论（Extreme Value Theory, EVT）&lt;/strong&gt; 则直接聚焦于收益率序列的尾部行为，通过对极端值建模突破整体分布限制，更精准地估计极端市场条件下的潜在损失。&lt;/p&gt;&#xA;&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;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;二极值理论核心模型&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;二、极值理论核心模型&lt;/h1&gt;&#xA;&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;&#xA;&lt;ol style=&#34;list-style-type: decimal&#34;&gt;&#xA;&lt;li&gt;广义极值分布（GEV）&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;&lt;strong&gt;分块最大值法&lt;/strong&gt;将时间序列划分为若干块（如按日、周），取每块内的极端值（最大值或最小值），其渐进分布可统一为 GEV。&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;GEV 分布函数&lt;/strong&gt;：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;G(x; \mu, \sigma, \xi) = \exp\left\{ -\left[1 + \xi \left( \frac{x - \mu}{\sigma} \right) \right]^{-1/\xi} \right\} \tag{1}&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\mu\)&lt;/span&gt; 为位置参数（中心趋势），决定了分布的中心位置，反映极端值的平均水平；&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\sigma &amp;gt; 0\)&lt;/span&gt; 为尺度参数（离散程度）；&lt;/li&gt;&#xA;&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\xi\)&lt;/span&gt; 为形状参数（决定尾部特性）：&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;若 &lt;span class=&#34;math inline&#34;&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; ，则 GEV 为 Frechet 分布（厚尾，如幂律尾）。Frechet 分布具有显著的厚尾特征，概率密度函数呈渐近幂律形式，尾部衰减速度较慢，能更准确捕捉金融市场中极端事件发生概率较高的尾部风险，在 VaR 计算中可更可靠地拟合金融资产收益率极端值。&lt;/p&gt;</description>
    </item>
    <item>
      <title>索罗模型模拟</title>
      <link>http://gewutang.cn/2010/12/25/solow-model/</link>
      <pubDate>Sat, 25 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/25/solow-model/</guid>
      <description>&lt;h2 id=&#34;索罗模型&#34;&gt;索罗模型&lt;/h2&gt;&#xA;&lt;p&gt;索罗模型是宏观经济学中的基础模型，用于解释经济体长期经济增长的原因，索罗模型的主要关注集中在资本积累、人口增长和技术进步。&lt;/p&gt;&#xA;&lt;h2 id=&#34;索罗模型的模拟&#34;&gt;索罗模型的模拟&lt;/h2&gt;&#xA;&lt;p&gt;基于R语言写一段模拟索罗模型的应用。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(shiny)&#xA;library(ggplot2)&#xA;library(deSolve)&#xA;&#xA;# 索罗模型微分方程&#xA;solow_model &amp;lt;- function(time, state, parameters) {&#xA;  with(as.list(c(state, parameters)), {&#xA;    dk &amp;lt;- s * (k^alpha) * (A * L)^(1-alpha) - (delta + n + g) * k&#xA;    list(dk)&#xA;  })&#xA;}&#xA;&#xA;ui &amp;lt;- fluidPage(&#xA;  titlePanel(&amp;quot;索罗经济增长模型模拟器&amp;quot;),&#xA;  sidebarLayout(&#xA;    sidebarPanel(&#xA;      sliderInput(&amp;quot;s&amp;quot;, &amp;quot;储蓄率 (s)&amp;quot;, 0.1, 0.5, 0.25, 0.01),&#xA;      sliderInput(&amp;quot;delta&amp;quot;, &amp;quot;折旧率 (δ)&amp;quot;, 0.01, 0.15, 0.05, 0.01),&#xA;      sliderInput(&amp;quot;n&amp;quot;, &amp;quot;人口增长率 (n)&amp;quot;, 0.0, 0.05, 0.02, 0.001),&#xA;      sliderInput(&amp;quot;g&amp;quot;, &amp;quot;技术进步率 (g)&amp;quot;, 0.0, 0.05, 0.01, 0.001),&#xA;      sliderInput(&amp;quot;alpha&amp;quot;, &amp;quot;资本产出弹性 (α)&amp;quot;, 0.2, 0.8, 0.3, 0.05),&#xA;      numericInput(&amp;quot;k0&amp;quot;, &amp;quot;初始人均资本&amp;quot;, 0.1, min = 0.01, max = 10),&#xA;      actionButton(&amp;quot;run&amp;quot;, &amp;quot;运行模拟&amp;quot;)&#xA;    ),&#xA;    &#xA;    mainPanel(&#xA;      tabsetPanel(&#xA;        tabPanel(&amp;quot;资本动态&amp;quot;, plotOutput(&amp;quot;capital_plot&amp;quot;)),&#xA;        tabPanel(&amp;quot;稳态分析&amp;quot;, &#xA;                 plotOutput(&amp;quot;phase_diagram&amp;quot;),&#xA;                 htmlOutput(&amp;quot;steady_state&amp;quot;))&#xA;      ),&#xA;      h4(&amp;quot;模型说明:&amp;quot;),&#xA;      uiOutput(&amp;quot;model_desc&amp;quot;)&#xA;    )&#xA;  )&#xA;)&#xA;&#xA;server &amp;lt;- function(input, output) {&#xA;  observeEvent(input$run, {&#xA;    parameters &amp;lt;- c(s = input$s,&#xA;                    delta = input$delta,&#xA;                    n = input$n,&#xA;                    g = input$g,&#xA;                    alpha = input$alpha,&#xA;                    A = 1,  # 初始技术水平&#xA;                    L = 1)  # 初始劳动力&#xA;    &#xA;    state &amp;lt;- c(k = input$k0)&#xA;    times &amp;lt;- seq(0, 100, by = 1)&#xA;    &#xA;    # 解微分方程&#xA;    out &amp;lt;- ode(y = state, times = times, func = solow_model, parms = parameters)&#xA;    &#xA;    # 处理结果&#xA;    df &amp;lt;- data.frame(&#xA;      Time = out[,1],&#xA;      Capital = out[,2],&#xA;      Output = (out[,2]^input$alpha) * (parameters[&amp;quot;A&amp;quot;] * exp((parameters[&amp;quot;n&amp;quot;]+parameters[&amp;quot;g&amp;quot;])*out[,1]))^(1-input$alpha),&#xA;      Consumption = (1 - input$s) * (out[,2]^input$alpha) * (parameters[&amp;quot;A&amp;quot;] * exp((parameters[&amp;quot;n&amp;quot;]+parameters[&amp;quot;g&amp;quot;])*out[,1]))^(1-input$alpha)&#xA;    )&#xA;    &#xA;    # 资本动态图&#xA;    output$capital_plot &amp;lt;- renderPlot({&#xA;      ggplot(df, aes(Time, Capital)) +&#xA;        geom_line(color = &amp;quot;darkblue&amp;quot;, size = 1.2) +&#xA;        geom_hline(yintercept = (input$s/(input$delta + input$n + input$g))^(1/(1-input$alpha)), &#xA;                   linetype = &amp;quot;dashed&amp;quot;, color = &amp;quot;red&amp;quot;) +&#xA;        labs(title = &amp;quot;人均资本动态路径&amp;quot;,&#xA;             subtitle = &amp;quot;红色虚线表示理论稳态值&amp;quot;,&#xA;             y = &amp;quot;人均资本 (k)&amp;quot;) +&#xA;        theme_minimal()&#xA;    })&#xA;    &#xA;    # 相位图&#xA;    output$phase_diagram &amp;lt;- renderPlot({&#xA;      ggplot(df, aes(Capital, Output)) +&#xA;        geom_path(color = &amp;quot;darkgreen&amp;quot;, size = 1.2) +&#xA;        geom_point(aes(x = tail(Capital,1), y = tail(Output,1)), &#xA;                   color = &amp;quot;red&amp;quot;, size = 3) +&#xA;        labs(title = &amp;quot;资本-产出相位图&amp;quot;,&#xA;             x = &amp;quot;人均资本 (k)&amp;quot;,&#xA;             y = &amp;quot;人均产出 (y)&amp;quot;) +&#xA;        theme_minimal()&#xA;    })&#xA;    &#xA;    # 稳态计算&#xA;    output$steady_state &amp;lt;- renderUI({&#xA;      k_star &amp;lt;- (input$s/(input$delta + input$n + input$g))^(1/(1-input$alpha))&#xA;      y_star &amp;lt;- k_star^input$alpha&#xA;      c_star &amp;lt;- (1 - input$s) * y_star&#xA;      &#xA;      HTML(paste(&#xA;        &amp;quot;&amp;lt;h4&amp;gt;稳态值计算:&amp;lt;/h4&amp;gt;&amp;quot;,&#xA;        paste0(&amp;quot;人均资本 k* = &amp;quot;, round(k_star, 3)),&#xA;        paste0(&amp;quot;人均产出 y* = &amp;quot;, round(y_star, 3)),&#xA;        paste0(&amp;quot;人均消费 c* = &amp;quot;, round(c_star, 3)),&#xA;        sep = &amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&#xA;      ))&#xA;    })&#xA;  })&#xA;  &#xA;  output$model_desc &amp;lt;- renderUI({&#xA;    HTML(&amp;quot;&amp;lt;p&amp;gt;标准索罗模型设定：&amp;lt;/p&amp;gt;&#xA;         &amp;lt;ul&amp;gt;&#xA;           &amp;lt;li&amp;gt;生产函数：Y = K&amp;lt;sup&amp;gt;α&amp;lt;/sup&amp;gt;(AL)&amp;lt;sup&amp;gt;1-α&amp;lt;/sup&amp;gt;&amp;lt;/li&amp;gt;&#xA;           &amp;lt;li&amp;gt;资本积累：dk/dt = sY - (δ + n + g)K&amp;lt;/li&amp;gt;&#xA;           &amp;lt;li&amp;gt;稳态条件：sy = (δ + n + g)k&amp;lt;/li&amp;gt;&#xA;         &amp;lt;/ul&amp;gt;&amp;quot;)&#xA;  })&#xA;}&#xA;&#xA;shinyApp(ui = ui, server = server)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;div style=&#34;width: 100% ; height: 400px ; text-align: center; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;&#34; class=&#34;muted well&#34;&gt;Shiny applications not supported in static R Markdown documents&lt;/div&gt;&#xA;&lt;h2 id=&#34;一些说明&#34;&gt;一些说明&lt;/h2&gt;&#xA;&lt;h3 id=&#34;可以互动调整以下参数&#34;&gt;可以互动调整以下参数：&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;储蓄率 (s)&lt;/li&gt;&#xA;&lt;li&gt;资本折旧率 (δ)&lt;/li&gt;&#xA;&lt;li&gt;人口增长率 (n)&lt;/li&gt;&#xA;&lt;li&gt;技术进步率 (g)&lt;/li&gt;&#xA;&lt;li&gt;资本产出弹性 (α)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;可视化展示&#34;&gt;可视化展示&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;资本积累动态路径&lt;/li&gt;&#xA;&lt;li&gt;资本-产出相位图&lt;/li&gt;&#xA;&lt;li&gt;理论稳态值标记&lt;/li&gt;&#xA;&lt;li&gt;实时稳态值计算&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;技术实现&#34;&gt;技术实现&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;使用deSolve包进行微分方程求解&lt;/li&gt;&#xA;&lt;li&gt;ggplot2生成动态图表&lt;/li&gt;&#xA;&lt;li&gt;响应式编程实现实时更新&lt;/li&gt;&#xA;&lt;li&gt;HTML输出格式化显示结果&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;使用方法&#34;&gt;使用方法：&lt;/h3&gt;&#xA;&lt;p&gt;确保已安装所需包：&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于GRACH模型计算VaR</title>
      <link>http://gewutang.cn/2010/12/23/grach-var/</link>
      <pubDate>Thu, 23 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/23/grach-var/</guid>
      <description>&lt;div id=&#34;一引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;一、引言&lt;/h1&gt;&#xA;&lt;p&gt;风险价值（VaR）是衡量金融资产潜在损失的核心指标，但传统方法假设收益率服从独立同分布的正态分布，难以捕捉金融时间序列的 “波动聚类”（volatility clustering）和 “尖峰厚尾” 特性。&lt;/p&gt;&#xA;&lt;p&gt;ARCH（自回归条件异方差）模型及扩展的 GARCH（广义自回归条件异方差）模型通过刻画波动率的动态变化，能更精准地拟合收益率分布，从而提升 VaR 估计的可靠性。&lt;/p&gt;&#xA;&lt;p&gt;本文以特斯拉（TSLA）股票收益率为例，详细介绍 GARCH 模型的构建、参数估计及 VaR 计算流程，并通过 R 语言quantmod和rgarch包实现实证分析。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;二garch-模型理论&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;二、GARCH 模型理论&lt;/h1&gt;&#xA;&lt;div id=&#34;arch-模型基础&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;ARCH 模型基础&lt;/h2&gt;&#xA;&lt;p&gt;ARCH 模型假设资产收益率的条件方差依赖于过去的平方残差，其核心是波动率的动态性。&lt;/p&gt;&#xA;&lt;p&gt;均值方程：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;r_t = \mu + \epsilon_t \tag{1}&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中，&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(r_t\)&lt;/span&gt; 为 t 时刻的收益率，&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\mu\)&lt;/span&gt; 为常数均值，&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\epsilon_t\)&lt;/span&gt; 为随机扰动项，且 &lt;span class=&#34;math inline&#34;&gt;\(\epsilon_t = \sigma_t \cdot z_t\)&lt;/span&gt; （ &lt;span class=&#34;math inline&#34;&gt;\(z_t\)&lt;/span&gt; )为独立同分布的随机变量，通常假设 &lt;span class=&#34;math inline&#34;&gt;\(z_t \sim N(0,1)\)&lt;/span&gt; ）。&lt;/p&gt;&#xA;&lt;p&gt;条件方差方程（ARCH (q)）：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\sigma_t^2 = \omega + \alpha_1 \epsilon_{t-1}^2 + \alpha_2 \epsilon_{t-2}^2 + ... + \alpha_q \epsilon_{t-q}^2 \tag{2}&#xA;\]&lt;/span&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>基于混合正态分布的 VaR 计算</title>
      <link>http://gewutang.cn/2010/12/21/var-mixture-of-norms/</link>
      <pubDate>Tue, 21 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/21/var-mixture-of-norms/</guid>
      <description>&lt;div id=&#34;一引言&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;一、引言&lt;/h1&gt;&#xA;&lt;p&gt;风险价值（Value at Risk, 简称 VaR ）是金融风险管理中衡量市场风险的核心指标，其定义为：在一定置信水平下，某一金融资产或组合在未来特定时间段内可能遭受的最大潜在损失。传统 VaR 计算多假设资产收益率服从正态分布，但实际金融时间序列往往呈现 “尖峰厚尾” 特性（峰度高于正态分布，尾部概率更大），导致正态分布假设下的 VaR 低估尾部风险。&lt;/p&gt;&#xA;&lt;p&gt;混合正态模型（Mixture of Normals Model）通过将收益率分布拟合为多个正态分布的加权组合，能够有效捕捉尖峰厚尾特征，从而更精准地估计 VaR。&lt;/p&gt;&#xA;&lt;p&gt;本文详细介绍混合正态模型的构建、参数估计及 VaR 计算方法，并通过 R 语言进行实现。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;二混合正态模型理论模型定义&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;二、混合正态模型理论模型定义&lt;/h1&gt;&#xA;&lt;p&gt;假设金融资产收益率X服从两成分混合正态分布，其概率密度函数（PDF）为：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;f(x; \Theta) = \alpha \cdot \phi(x; \mu_1, \sigma_1^2) + (1-\alpha) \cdot \phi(x; \mu_2, \sigma_2^2) \tag{1}&#xA;\]&lt;/span&gt;&lt;/p&gt;&#xA;&lt;p&gt;其中：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\Theta = (\alpha, \mu_1, \sigma_1^2, \mu_2, \sigma_2^2)\)&lt;/span&gt; 为待估参数；&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\alpha \in (0,1)\)&lt;/span&gt; 是第一成分的权重；&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\phi(x; \mu, \sigma^2) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)\)&lt;/span&gt; 为正态分布密度函数；&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\mu_1\)&lt;/span&gt; , &lt;span class=&#34;math inline&#34;&gt;\(\mu_2\)&lt;/span&gt; 分别为两成分的均值；&lt;/p&gt;</description>
    </item>
    <item>
      <title>指数加权移动平均法计算VaR</title>
      <link>http://gewutang.cn/2010/12/18/caculate-var-via-ewma/</link>
      <pubDate>Sat, 18 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/18/caculate-var-via-ewma/</guid>
      <description>&lt;h2 id=&#34;指数加权移动平均法&#34;&gt;指数加权移动平均法&lt;/h2&gt;&#xA;&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;&#xA;&lt;h3 id=&#34;单资产情形&#34;&gt;单资产情形&lt;/h3&gt;&#xA;&lt;h4 id=&#34;1-数据准备与收益率计算&#34;&gt;1. 数据准备与收益率计算&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;获取资产历史收盘价序列${P_t}$，计算对数收益率：&#xA;$$&#xA;r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)&#xA;$$&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;2-动态方差估计&#34;&gt;2. 动态方差估计&lt;/h4&gt;&#xA;&lt;p&gt;EWMA通过指数衰减因子 &lt;code&gt;\(\lambda\)&lt;/code&gt; 赋予近期数据更高权重，捕捉波动率的时变特性：&lt;/p&gt;&#xA;&lt;p&gt;$$&#xA;\sigma_t^2 = \lambda \sigma_{t-1}^2 + (1-\lambda) r_{t-1}^2&#xA;$$&lt;/p&gt;&#xA;&lt;p&gt;其中：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\lambda \in (0,1)\)&lt;/code&gt;：衰减因子（典型值0.94）&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(r_{t-1}\)&lt;/code&gt;：前一日收益率&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;\(\sigma_{t-1}^2\)&lt;/code&gt;：前一日波动率&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;3-var计算正态假设&#34;&gt;3. VaR计算（正态假设）&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;单日VaR公式：&#xA;$$&#xA;\text{VaR}_{\alpha} = -\mu + \sigma_t \Phi^{-1}(1-\alpha)&#xA;$$&#xA;其中$\mu$为收益率均值（短期内可忽略），$\Phi^{-1}$为标准正态分布分位数（如95%置信水平对应1.645）&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h4 id=&#34;4-多期var调整&#34;&gt;4. 多期VaR调整&lt;/h4&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;持有期$T$天的VaR扩展：&#xA;$$&#xA;\text{VaR}&lt;em&gt;T = \text{VaR}&lt;/em&gt;{1d} \times \sqrt{T}&#xA;$$&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h5 id=&#34;代码实现&#34;&gt;代码实现&lt;/h5&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载必要包&#xA;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)  # 金融数据获取&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：quantmod&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;if (!require(&amp;quot;ggplot2&amp;quot;)) install.packages(&amp;quot;ggplot2&amp;quot;)    # 可视化&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：ggplot2&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)&#xA;library(ggplot2)&#xA;&#xA;# 获取标普500指数数据（示例）&#xA;getSymbols(&amp;quot;^GSPC&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = &amp;quot;2020-01-01&amp;quot;, to = Sys.Date())&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;GSPC&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 计算对数收益率&#xA;returns &amp;lt;- na.omit(ROC(Cl(GSPC)))       # 使用收盘价计算&#xA;colnames(returns) &amp;lt;- &amp;quot;Daily_Return&amp;quot;&#xA;&#xA;# 参数设置&#xA;lambda &amp;lt;- 0.94                          # 衰减因子&#xA;portfolio_value &amp;lt;- 1e6                  # 组合价值100万美元&#xA;confidence_level &amp;lt;- 0.95                # 置信水平&#xA;z_score &amp;lt;- qnorm(1 - confidence_level)  # 分位数（正态分布）&#xA;&#xA;# 初始化EWMA方差序列&#xA;n &amp;lt;- length(returns)&#xA;ewma_var &amp;lt;- numeric(n)&#xA;ewma_var[1] &amp;lt;- var(returns)             # 初始化为样本方差&#xA;&#xA;# 递归计算EWMA方差&#xA;for (i in 2:n) {&#xA;  ewma_var[i] &amp;lt;- lambda * ewma_var[i-1] + (1 - lambda) * returns[i-1]^2&#xA;}&#xA;&#xA;# 转换为年化波动率&#xA;ewma_vol &amp;lt;- sqrt(ewma_var * 252)&#xA;&#xA;# 计算VaR序列&#xA;VaR &amp;lt;- portfolio_value * z_score * ewma_vol&#xA;&#xA;# 获取最新VaR值&#xA;latest_VaR &amp;lt;- tail(VaR, 1)&#xA;cat(&amp;quot;最新EWMA VaR（95%置信度）:&amp;quot;, round(latest_VaR, 2), &amp;quot;美元\n&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 最新EWMA VaR（95%置信度）: -581585.7 美元&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 可视化结果&#xA;results &amp;lt;- data.frame(&#xA;  Date = index(GSPC)[-1], &#xA;  Volatility = ewma_vol,&#xA;  VaR = VaR&#xA;)&#xA;&#xA;ggplot(results, aes(x = Date)) +&#xA;  geom_line(aes(y = Volatility, color = &amp;quot;波动率&amp;quot;)) +&#xA;  geom_line(aes(y = VaR / 1e4, color = &amp;quot;VaR（万美元）&amp;quot;)) +&#xA;  scale_y_continuous(&#xA;    name = &amp;quot;年化波动率&amp;quot;,&#xA;    sec.axis = sec_axis(~ . * 1e4, name = &amp;quot;VaR（美元）&amp;quot;)&#xA;  ) +&#xA;  labs(title = &amp;quot;EWMA波动率与VaR动态变化&amp;quot;,&#xA;       color = &amp;quot;指标类型&amp;quot;) +&#xA;  theme_minimal()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/12/18/caculate-var-via-ewma/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;多元资产组合ewma-var建模原理与r实现&#34;&gt;多元资产组合EWMA-VaR建模原理与R实现&lt;/h3&gt;&#xA;&lt;h4 id=&#34;协方差矩阵动态更新&#34;&gt;协方差矩阵动态更新&lt;/h4&gt;&#xA;&lt;p&gt;多资产EWMA模型通过递归更新协方差矩阵捕捉资产间相关性的时变特征:&lt;/p&gt;</description>
    </item>
    <item>
      <title>VaR和方差协方差方法及R语言</title>
      <link>http://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/</link>
      <pubDate>Fri, 17 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/</guid>
      <description>&lt;h2 id=&#34;方差-协方差法&#34;&gt;方差-协方差法&lt;/h2&gt;&#xA;&lt;p&gt;方差-协方差法是一种基于资产收益率正态性假设的VaR计算方法。其核心思想是通过历史数据估计资产收益率的均值、方差和协方差矩阵，构建投资组合收益率的概率分布，继而计算VaR值。&lt;/p&gt;&#xA;&lt;p&gt;具体流程可分为以下三步：&lt;/p&gt;&#xA;&lt;p&gt;首先，计算资产收益率的协方差矩阵以衡量风险关联；其次，根据投资组合权重计算组合收益率的期望值和标准差；最后，在给定置信水平下，利用正态分布分位数确定VaR值。例如，在95%置信水平下，单日VaR可表示为组合市值的绝对值乘以其标准差再乘1.645（对应正态分布左尾5%分位数）。该方法计算效率高，但对非线性风险和非正态分布敏感。多日VaR可通过时间平方根法则调整，即单日VaR乘 &lt;code&gt;\(\sqrt{T}\)&lt;/code&gt; 得到。&lt;/p&gt;&#xA;&lt;h2 id=&#34;r代码实现&#34;&gt;R代码实现&lt;/h2&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载必要包&#xA;library(quantmod)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(PerformanceAnalytics)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;PerformanceAnalytics&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:graphics&#39;:&#xA;## &#xA;##     legend&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(ggplot2)&#xA;library(dplyr)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ######################### Warning from &#39;xts&#39; package ##########################&#xA;## #                                                                             #&#xA;## # The dplyr lag() function breaks how base R&#39;s lag() function is supposed to  #&#xA;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #&#xA;## # source() into this session won&#39;t work correctly.                            #&#xA;## #                                                                             #&#xA;## # Use stats::lag() to make sure you&#39;re not using dplyr::lag(), or you can add #&#xA;## # conflictRules(&#39;dplyr&#39;, exclude = &#39;lag&#39;) to your .Rprofile to stop           #&#xA;## # dplyr from breaking base R&#39;s lag() function.                                #&#xA;## #                                                                             #&#xA;## # Code in packages is not affected. It&#39;s protected by R&#39;s namespace mechanism #&#xA;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #&#xA;## #                                                                             #&#xA;## ###############################################################################&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;dplyr&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:xts&#39;:&#xA;## &#xA;##     first, last&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:stats&#39;:&#xA;## &#xA;##     filter, lag&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     intersect, setdiff, setequal, union&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 1. 下载股票数据（示例使用苹果、微软、亚马逊）&#xA;symbols &amp;lt;- c(&amp;quot;AAPL&amp;quot;, &amp;quot;MSFT&amp;quot;, &amp;quot;AMZN&amp;quot;)&#xA;getSymbols(symbols, src = &amp;quot;yahoo&amp;quot;, from = &amp;quot;2022-01-01&amp;quot;, to = Sys.Date())&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;MSFT&amp;quot; &amp;quot;AMZN&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 2. 提取收盘价并合并&#xA;prices &amp;lt;- merge(Cl(AAPL), Cl(MSFT), Cl(AMZN)) %&amp;gt;% na.omit()&#xA;colnames(prices) &amp;lt;- symbols&#xA;&#xA;# 3. 计算对数收益率&#xA;returns &amp;lt;- na.omit(Return.calculate(prices, method = &amp;quot;log&amp;quot;))&#xA;&#xA;# 4. 设定投资组合参数&#xA;weights &amp;lt;- c(0.3, 0.4, 0.3)  # 组合权重&#xA;conf_level &amp;lt;- 0.95            # 置信水平&#xA;portfolio_value &amp;lt;- 1e6        # 组合价值（美元）&#xA;&#xA;# 5. 计算协方差矩阵与组合标准差&#xA;cov_matrix &amp;lt;- cov(returns)&#xA;port_sd &amp;lt;- sqrt(t(weights) %*% cov_matrix %*% weights)&#xA;&#xA;# 6. 计算单日VaR&#xA;var_1d &amp;lt;- abs(portfolio_value * qnorm(1 - conf_level) * port_sd)&#xA;&#xA;# 7. 计算5日VaR（时间平方根法则）&#xA;var_5d &amp;lt;- var_1d * sqrt(5)&#xA;&#xA;# 8. 可视化VaR分布&#xA;ggplot(data = data.frame(Returns = as.numeric(returns %*% weights)), &#xA;       aes(x = Returns)) +&#xA;  geom_histogram(aes(y = ..density..), bins = 50, fill = &amp;quot;steelblue&amp;quot;, alpha = 0.7) +&#xA;  geom_vline(xintercept = -var_1d/portfolio_value, color = &amp;quot;red&amp;quot;, &#xA;             linetype = &amp;quot;dashed&amp;quot;, size = 1) +&#xA;  stat_function(fun = dnorm, args = list(mean = mean(returns %*% weights),&#xA;               sd = sd(returns %*% weights)), color = &amp;quot;darkgreen&amp;quot;) +&#xA;  labs(title = &amp;quot;Portfolio Returns Distribution with VaR&amp;quot;,&#xA;       x = &amp;quot;Daily Returns&amp;quot;, y = &amp;quot;Density&amp;quot;) +&#xA;  annotate(&amp;quot;text&amp;quot;, x = -var_1d/portfolio_value, y = 15, &#xA;           label = paste0(&amp;quot;95% 1d VaR: $&amp;quot;, round(var_1d)), color = &amp;quot;red&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.&#xA;## ℹ Please use `linewidth` instead.&#xA;## This warning is displayed once every 8 hours.&#xA;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#xA;## generated.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.&#xA;## ℹ Please use `after_stat(density)` instead.&#xA;## This warning is displayed once every 8 hours.&#xA;## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was&#xA;## generated.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/12/17/var-caculation-via-variance-covariance-method/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 输出结果&#xA;cat(&amp;quot;单日VaR（95%置信度）:&amp;quot;, round(var_1d), &amp;quot;美元\n&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 单日VaR（95%置信度）: 28894 美元&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;cat(&amp;quot;5日VaR（95%置信度）:&amp;quot;, round(var_5d), &amp;quot;美元&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 5日VaR（95%置信度）: 64609 美元&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>VaR和蒙特卡洛模拟法</title>
      <link>http://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/</link>
      <pubDate>Thu, 16 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/</guid>
      <description>&lt;h2 id=&#34;蒙特卡洛模拟法&#34;&gt;蒙特卡洛模拟法&lt;/h2&gt;&#xA;&lt;p&gt;蒙特卡洛模拟法是一种基于随机抽样和统计模拟的数值计算方法，其通过生成大量随机样本，利用概率统计理论估计复杂问题的近似解。该方法由数学家冯·诺伊曼和乌拉姆于20世纪40年代在核武器研制项目中首次提出。蒙特卡洛模拟法也可以用来计算投资组合的VaR。&lt;/p&gt;&#xA;&lt;p&gt;在应用蒙特卡洛模拟法计算VaR时，首先根据投资组合的历史特征，总结投资组合收益率对应的可能分布并提取该分布对应的核心参数；再以这些核心参数为基础，结合特定分布模拟出一系列符合要求的伪随机数作为假想的未来数据；最后，以这些“假想的未来数据”作为研究对象计算出VaR值。&lt;/p&gt;&#xA;&lt;h2 id=&#34;蒙特卡洛模拟法计算单资产var&#34;&gt;蒙特卡洛模拟法计算单资产VaR&lt;/h2&gt;&#xA;&lt;p&gt;基于R语言应用蒙特卡洛模拟法计算VaR的代码如下:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载必要包&#xA;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：quantmod&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;zoo&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     as.Date, as.Date.numeric&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;if (!require(&amp;quot;MASS&amp;quot;)) install.packages(&amp;quot;MASS&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：MASS&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(quantmod)&#xA;library(MASS)&#xA;library(dplyr)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## ######################### Warning from &#39;xts&#39; package ##########################&#xA;## #                                                                             #&#xA;## # The dplyr lag() function breaks how base R&#39;s lag() function is supposed to  #&#xA;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #&#xA;## # source() into this session won&#39;t work correctly.                            #&#xA;## #                                                                             #&#xA;## # Use stats::lag() to make sure you&#39;re not using dplyr::lag(), or you can add #&#xA;## # conflictRules(&#39;dplyr&#39;, exclude = &#39;lag&#39;) to your .Rprofile to stop           #&#xA;## # dplyr from breaking base R&#39;s lag() function.                                #&#xA;## #                                                                             #&#xA;## # Code in packages is not affected. It&#39;s protected by R&#39;s namespace mechanism #&#xA;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #&#xA;## #                                                                             #&#xA;## ###############################################################################&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## &#xA;## 载入程序包：&#39;dplyr&#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following object is masked from &#39;package:MASS&#39;:&#xA;## &#xA;##     select&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:xts&#39;:&#xA;## &#xA;##     first, last&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:stats&#39;:&#xA;## &#xA;##     filter, lag&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &#39;package:base&#39;:&#xA;## &#xA;##     intersect, setdiff, setequal, union&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 获取苹果公司股票数据（过去2年）&#xA;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()))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 计算对数收益率&#xA;returns &amp;lt;- na.omit(ROC(Cl(AAPL)))  &#xA;colnames(returns) &amp;lt;- &amp;quot;AAPL_Return&amp;quot;&#xA;&#xA;# 参数设置&#xA;set.seed(123)&#xA;n_sim &amp;lt;- 10000  # 模拟次数&#xA;confidence_level &amp;lt;- 0.95&#xA;portfolio_value &amp;lt;- 1e6  # 组合价值100万美元&#xA;&#xA;# 估计收益率参数&#xA;mu &amp;lt;- mean(returns)&#xA;sigma &amp;lt;- sd(returns)&#xA;&#xA;# 蒙特卡洛模拟（正态分布）&#xA;sim_normal &amp;lt;- rnorm(n_sim, mean = mu, sd = sigma)&#xA;portfolio_loss_normal &amp;lt;- portfolio_value * (1 - exp(sim_normal))  # 对数收益转换为价格变动&#xA;&#xA;# 蒙特卡洛模拟（t分布）&#xA;&#xA;# 计算VaR&#xA;var_normal &amp;lt;- quantile(portfolio_loss_normal, probs = confidence_level)&#xA;&#xA;# 格式化输出&#xA;cat(&amp;quot;蒙特卡洛模拟VaR（正态分布假设）:&amp;quot;, round(var_normal, 2), &amp;quot;USD\n&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 蒙特卡洛模拟VaR（正态分布假设）: 27011.97 USD&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 可视化比较&#xA;hist(portfolio_loss_normal, breaks = 50, main = &amp;quot;正态分布模拟损失&amp;quot;, &#xA;     xlab = &amp;quot;损失金额(USD)&amp;quot;, col = &amp;quot;lightblue&amp;quot;)&#xA;abline(v = var_normal, col = &amp;quot;red&amp;quot;, lwd = 2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/12/16/var-calculation-via-montecarlo-simulation/index_files/figure-html/single-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;也可以将模拟的分布确定为以下分布：&lt;/p&gt;</description>
    </item>
    <item>
      <title>VaR和历史模拟法及R语言</title>
      <link>http://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/</link>
      <pubDate>Wed, 15 Dec 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/</guid>
      <description>&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(showtext)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: sysfonts&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Loading required package: showtextdb&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;SimHei.ttf&amp;quot;)&#xA;showtext_auto()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;div id=&#34;引言&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;引言&lt;/h2&gt;&#xA;&lt;p&gt;现代金融领域的计量工作分为两大块，即资产定价和风险度量。而资产定价又是以度量风险并为风险定价为基础，由此可见，风险度量工作实际上是现代金融计量领域的核心。金融领域度量风险的方法有很多，其中应用最多的是方差、最大回撤和 VaR 等。&lt;/p&gt;&#xA;&lt;p&gt;在业界，VaR 不仅单纯作为风险管理的工具，在进行投资组合优化时，也会将 VaR 作为重要的过滤指标之一。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;var-的定义&#34; class=&#34;section level2&#34;&gt;&#xA;&lt;h2&gt;VaR 的定义&lt;/h2&gt;&#xA;&lt;p&gt;VaR 是 Value at Risk 的简称，中文译作“在险价值”。它是指在市场正常波动情况下，投资者所持有的投资组合在未来一定时期 &lt;span class=&#34;math inline&#34;&gt;\(T\)&lt;/span&gt; 内，在一定置信水平 &lt;span class=&#34;math inline&#34;&gt;\(\alpha\)&lt;/span&gt; 下所可能产生的最大可能损失。&lt;/p&gt;&#xA;&lt;p&gt;假设投资者持有的投资组合的初始价值为 &lt;span class=&#34;math inline&#34;&gt;\(P_{0}\)&lt;/span&gt; ，&lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; 为未来一定时期 &lt;span class=&#34;math inline&#34;&gt;\(T\)&lt;/span&gt; 对应的收益率。那么，该组合的期末价值为：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;P_{T}=P_{0}(1+r_{T})&#xA;\]&lt;/span&gt;&#xA;&lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; 可视作随机变量，若假设在某一置信水平下，该投资组合的最低价值为 &lt;span class=&#34;math inline&#34;&gt;\(P^*\)&lt;/span&gt; ，则：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;P_{T}^*=P_{0}(1+r_{T}^*)&#xA;\]&lt;/span&gt;&#xA;显然，根据VaR的定义可知，其等于投资组合的最低价值和初始价值之差：&lt;/p&gt;&#xA;&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[&#xA;VaR_{\alpha}=-P_{0}r_{\alpha}^*&#xA;\]&lt;/span&gt;&#xA;可见，计算 VaR 值等价于计算一定置信水平下的投资组合的最低价值 &lt;span class=&#34;math inline&#34;&gt;\(P^*\)&lt;/span&gt; 或最低收益率 &lt;span class=&#34;math inline&#34;&gt;\(r^*\)&lt;/span&gt; 。而计算最低价值或最低收益率则需依赖于价格序列或收益率序列的分布或概率密度函数。换句话说，计算金融资产序列的分布函数或概率密度函数正是计算 VaR 值的核心之处。&lt;/p&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;var-的算法&#34; class=&#34;section level1&#34;&gt;&#xA;&lt;h1&gt;VaR 的算法&lt;/h1&gt;&#xA;&lt;p&gt;清楚了 VaR 的定义，计算 VaR 的方法也就呼之欲出了。目前，常用的 VaR 算法可以分为参数法即方差-协方差法、蒙特卡洛模拟法以及非参数方法即历史模拟法。在 VaR 的应用过程中，人们也对 VaR 的上述算法进行了扩展。比如，在方差-协方差方法下人们提出了指数级权平均模型计算方差或者结合 Garch 类模型计算方差进而计算 VaR 的方法。&lt;/p&gt;</description>
    </item>
    <item>
      <title>scatterplot3d:一个特立独行的R包</title>
      <link>http://gewutang.cn/2010/06/13/scatterplot3d/</link>
      <pubDate>Sun, 13 Jun 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/06/13/scatterplot3d/</guid>
      <description>&lt;p&gt;scatterplot3d是一个力图在3D空间展示多维数据的R包，在这个包里只含有一个函数scatterplot3d()，这一点上可谓是特立独行。scatterplot3d()的用法如下：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;    scatterplot3d(x, y=NULL, z=NULL, color=par(&amp;quot;col&amp;quot;), pch=NULL,&#xA;        main=NULL, sub=NULL, xlim=NULL, ylim=NULL, zlim=NULL,&#xA;        xlab=NULL, ylab=NULL, zlab=NULL, scale.y=1, angle=40,&#xA;        axis=TRUE, tick.marks=TRUE, label.tick.marks=TRUE,&#xA;        x.ticklabs=NULL, y.ticklabs=NULL, z.ticklabs=NULL,&#xA;        y.margin.add=0, grid=TRUE, box=TRUE, lab=par(&amp;quot;lab&amp;quot;),&#xA;        lab.z=mean(lab[1:2]), type=&amp;quot;p&amp;quot;, highlight.3d=FALSE,&#xA;        mar=c(5,3,4,3)+0.1, col.axis=par(&amp;quot;col.axis&amp;quot;),&#xA;        col.grid=&amp;quot;grey&amp;quot;, col.lab=par(&amp;quot;col.lab&amp;quot;),&#xA;        cex.symbols=par(&amp;quot;cex&amp;quot;), cex.axis=0.8 * par(&amp;quot;cex.axis&amp;quot;),&#xA;        cex.lab=par(&amp;quot;cex.lab&amp;quot;), font.axis=par(&amp;quot;font.axis&amp;quot;),&#xA;        font.lab=par(&amp;quot;font.lab&amp;quot;), lty.axis=par(&amp;quot;lty&amp;quot;),&#xA;        lty.grid=par(&amp;quot;lty&amp;quot;), lty.hide=NULL, lty.hplot=par(&amp;quot;lty&amp;quot;),&#xA;        log=&amp;quot;&amp;quot;, ...)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;这个函数的参数多到让人眼花缭乱。下面是几个例子:&lt;/p&gt;&#xA;&lt;p&gt;先用scatterplot3d()画一个螺旋线：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(scatterplot3d)&#xA;z &amp;lt;- seq(-10, 10, 0.01)&#xA;x &amp;lt;- cos(z)&#xA;y &amp;lt;- sin(z)&#xA;scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis=&amp;quot;blue&amp;quot;,col.grid=&amp;quot;lightblue&amp;quot;,main=&amp;quot;scatterplot3d - 1&amp;quot;, pch=20)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;再画半椭圆:&lt;/p&gt;</description>
    </item>
    <item>
      <title>支持向量机</title>
      <link>http://gewutang.cn/2010/05/05/support-vector-machine/</link>
      <pubDate>Wed, 05 May 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/05/05/support-vector-machine/</guid>
      <description>&lt;p&gt;支持向量机(Support Vector Machine,SVM)是Corinna  Cortes和Vapnik等于1995年首先提出的一种机器学习方法，它在解决小样本、非线性及高维模式识别中表现出许多特有的优势，并能够推广应用到函数拟合等 其他机器学习问题中。&lt;/p&gt;&#xA;&lt;p&gt;通俗的介绍一下这个过程:&lt;/p&gt;&#xA;&lt;p&gt;我们有一些数据点，这些数据点是n维实空间中的点。我们希望能够把这些点通过一个n-1维的超平面分开。通常这个被称为线性分类器。有很多分类器都符合这个要求。但是我们还希望找到分类最佳的平面，即使得属 于两个不同类的数据点间隔最大的那个面，该面亦称为最大间隔超平面。如果我们能够找到这个面，那么这个分类器就称为最大间隔分类器。&lt;/p&gt;&#xA;&lt;p&gt;理解了上面这段话就可以理解支持向量机了。支持向量机是将向量映射到一个更高维的空间里，在这个空间里建立有一个最大间隔超平面。在分开数据的超平面的两边建有两个互相平行的超平面。建立方向合适的分隔超平面使两个与之平行的超平面间的距离最大化。其假定为，平行超平面间的距离或差距越大，分类器的总误差越小。而所谓支持向量是指那些在间隔区边缘的训练样本点。这里的“机（machine,机器）”实际上是一个算法。在机器学习领域，常把一些算法看做是一个机器。&lt;/p&gt;&#xA;&lt;p&gt;学习支持向量机知识的一个极好指南是C.J.CBurges的《模式识别支持向量机指南》。&lt;/p&gt;&#xA;&lt;p&gt;目前，R中已经实现了支持向量机算法(相关的包有e1071包、kernlab包、klar包和svmpath包。以数据iris和cats为例加以说明：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(e1071)&#xA;library(MASS)&#xA;data(cats)&#xA;m= svm(Sex~., data = cats)&#xA;plot(m, cats)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/05/05/support-vector-machine/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;data(iris)&#xA;m2=svm(Species~., data = iris)&#xA;plot(m2, iris, Petal.Width ~ Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length = 4))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/05/05/support-vector-machine/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;plot(m, cats, svSymbol = 1, dataSymbol = 2, symbolPalette = rainbow(4),color.palette =terrain.colors)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/05/05/support-vector-machine/index_files/figure-html/unnamed-chunk-2-2.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;支持向量机还可以用到股票价格的预测上，具体案例回头再写。&lt;/p&gt;</description>
    </item>
    <item>
      <title>BEKK模型</title>
      <link>http://gewutang.cn/2010/04/15/about-bekk-model/</link>
      <pubDate>Thu, 15 Apr 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/04/15/about-bekk-model/</guid>
      <description>&lt;p&gt;BEKK模型是目前比较流行的多变量波动性模型，可以看作GARCH模型在多维空间的实现。BEKK模型在应用过程中也有诸多缺点，其中最大的一个不足之处在于模型的待估计参数会随着模型维数的增加指数型增长。然而，这一点肯定会随着计算机的发展而被逐渐克服的。&lt;/p&gt;&#xA;&lt;p&gt;R软件中提供了mgarch和mgarchBEKK两个包来估计BEKK模型参数。&lt;/p&gt;&#xA;&lt;h2 id=&#34;mgarch包及其示例&#34;&gt;mgarch包及其示例&lt;/h2&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 安装包（如果未安装）&#xA;# devtools::install_github(&amp;quot;https://github.com/vst/mgarch&amp;quot;)&#xA;&#xA;library(mgarch)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：tseries&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by &#39;quantmod&#39;:&#xA;##   method            from&#xA;##   as.zoo.data.frame zoo&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：mvtnorm&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;先看mgarch包，该包有三个主要函数：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;mvBEKK.sim: 模拟一个MGARCH-BEKK过程&lt;/li&gt;&#xA;&lt;li&gt;mvBEKK.est: 估计MGARCH-BEKK模型的参数&lt;/li&gt;&#xA;&lt;li&gt;mvBEKK.diag:对拟合的MGARCH-BEKK模型进行诊断&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;可以通过以下命令查询相关函数的用法：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;?mvBEKK.sim&#xA;?mvBEKK.est&#xA;?mvBEKK.diag&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;运行以下命令可以模拟一个MGARCH-BEKK过程：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;set.seed(123)&#xA;sim = mvBEKK.sim(series.count = 3, T = 1000)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;pre&gt;&lt;code&gt;## Class attributes are accessible through following names:&#xA;## length series.count order params true.params eigenvalues uncond.cov.matrix white.noise eps cor sd&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;合并模拟的时间序列：&lt;/p&gt;</description>
    </item>
    <item>
      <title>四龟追逐问题</title>
      <link>http://gewutang.cn/2010/03/29/four-turtles/</link>
      <pubDate>Mon, 29 Mar 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/03/29/four-turtles/</guid>
      <description>&lt;p&gt;四龟行走是一个很著名的论题，题目的大意是这样的：&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;在一个房间的四个角上，分别有一只乌龟。在某一个时刻t同时出发以匀速V走向顺时针方向的下一只乌龟，如果他们的方向始终对准目标，那么它们的运动轨迹是怎样的？&lt;/p&gt;&lt;/blockquote&gt;&#xA;&lt;p&gt;这道题目的解答方法至少有三个：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;借助微分方程来解答，这是最正经的解法，但是也最没有意思，是&amp;quot;书虫&amp;quot;才干的出来的事情。&lt;/li&gt;&#xA;&lt;li&gt;常理判断。由于四龟地位完全对称，而相邻两龟运动方向始终垂直，因此四龟轨迹对称且都是螺旋线。这是极其聪明的人才能干出的事情。&lt;/li&gt;&#xA;&lt;li&gt;统计模拟方法。这个方法兼具艺术性和趣味性，而且不会远离群众，是有趣的人会干出的事情。&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;关于前两个这里一笔带过，第三个方法在薛毅很久之前的一本书《统计建模与R软件》是有提及过。怡轩说，这个统计模拟的例子于他有着特殊的意义。巧合的是，四龟行走也恰恰是我学习统计学的启蒙案例。也就是在见到这个例子的刹那，我才初次领悟了编程的景致之所在，也开始了对编程的敬仰之旅。&lt;/p&gt;&#xA;&lt;p&gt;前段时间，在看自然图形时，突然发现自然界很多植物如“紫荆花”等等都有着类似四龟行走轨迹的花型。因此萌发妄想将它写成一个动画。我尽量避免使用复杂的数学语言来描述以上过程，因为我觉得数学语言尽管清晰有时候只会耽误大众对事情本质的理解。&lt;/p&gt;&#xA;&lt;p&gt;下面是一段模拟四龟行走问题的R代码：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 加载必要的包&#xA;library(ggplot2)&#xA;library(gganimate)&#xA;&#xA;# 模拟参数设置&#xA;V &amp;lt;- 0.02       # 移动速度&#xA;dt &amp;lt;- 0.01      # 时间步长&#xA;total_time &amp;lt;- 500 # 总模拟时间&#xA;threshold &amp;lt;- 0.01 # 停止阈值&#xA;&#xA;# 初始化四只乌龟的起始位置（正方形顶点）&#xA;turtles &amp;lt;- matrix(c(&#xA;  0.0, 1.0,   # 左上角乌龟1&#xA;  1.0, 1.0,   # 右上角乌龟2&#xA;  1.0, 0.0,   # 右下角乌龟3&#xA;  0.0, 0.0    # 左下角乌龟4&#xA;), ncol = 2, byrow = TRUE)&#xA;&#xA;# 轨迹记录矩阵&#xA;history &amp;lt;- data.frame(&#xA;  turtle = integer(),&#xA;  time = numeric(),&#xA;  x = numeric(),&#xA;  y = numeric()&#xA;)&#xA;&#xA;# 主循环模拟&#xA;current_time &amp;lt;- 0&#xA;while(current_time &amp;lt; total_time) {&#xA;  # 计算各乌龟的目标方向&#xA;  new_positions &amp;lt;- matrix(nrow = 4, ncol = 2)&#xA;  &#xA;  for(i in 1:4) {&#xA;    current_pos &amp;lt;- turtles[i, ]&#xA;    target_pos &amp;lt;- turtles[(i %% 4) + 1, ]&#xA;    &#xA;    # 计算方向向量并标准化&#xA;    direction &amp;lt;- target_pos - current_pos&#xA;    distance &amp;lt;- sqrt(sum(direction^2))&#xA;    &#xA;    if(distance &amp;lt; threshold) break  # 提前终止条件&#xA;    &#xA;    unit_direction &amp;lt;- direction / distance&#xA;    displacement &amp;lt;- V * dt * unit_direction&#xA;    &#xA;    new_positions[i, ] &amp;lt;- current_pos + displacement&#xA;  }&#xA;  &#xA;  # 记录当前时刻所有位置&#xA;  for(i in 1:4) {&#xA;    history &amp;lt;- rbind(history, data.frame(&#xA;      turtle = i,&#xA;      time = current_time,&#xA;      x = turtles[i, 1],&#xA;      y = turtles[i, 2]&#xA;    ))&#xA;  }&#xA;  &#xA;  # 同步更新所有乌龟位置&#xA;  turtles &amp;lt;- new_positions&#xA;  current_time &amp;lt;- current_time + dt&#xA;}&#xA;&#xA;# 可视化静态轨迹&#xA;ggplot(history, aes(x, y, color = factor(turtle))) +&#xA;  geom_path(aes(group = turtle), linewidth = 0.8) +&#xA;  geom_point(data = subset(history, time == min(time)), &#xA;             size = 4, shape = 19) +  # 起始点&#xA;  geom_point(data = subset(history, time == max(time)), &#xA;             size = 4, shape = 4) +    # 终止点&#xA;  scale_color_manual(values = c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;, &amp;quot;#45B7D1&amp;quot;, &amp;quot;#96CEB4&amp;quot;)) +&#xA;  coord_fixed() +&#xA;  theme_minimal() +&#xA;  labs(title = &amp;quot;四龟追逐运动轨迹&amp;quot;, color = &amp;quot;乌龟编号&amp;quot;)&#xA;&#xA;# 生成动态可视化（需要安装gganimate）&#xA;anim &amp;lt;- ggplot(history, aes(x, y, color = factor(turtle))) +&#xA;  geom_path(aes(group = turtle), alpha = 0.5) +&#xA;  geom_point(size = 3) +&#xA;  transition_reveal(time) +&#xA;  scale_color_manual(values = c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;, &amp;quot;#45B7D1&amp;quot;, &amp;quot;#96CEB4&amp;quot;)) +&#xA;  coord_fixed() +&#xA;  labs(title = &#39;时间: {frame_along}&#39;, x = &#39;X坐标&#39;, y = &#39;Y坐标&#39;, color = &#39;乌龟编号&#39;)&#xA;&#xA;animate(anim, fps = 20, duration = total_time, width = 600, height = 600)&#xA;&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>布袋图与异常值检测</title>
      <link>http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/</link>
      <pubDate>Wed, 24 Mar 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/</guid>
      <description>&lt;p&gt;这两天在R软件中发现了一种趣味横生的检测异常值的可视化方法叫bagplot，我翻译为“布袋图”。它首先定义一个二维数据的中心,然后，将与中心较为疏离的点定义为野值。&lt;/p&gt;&#xA;&lt;p&gt;以经典的car数据为例演示下布袋图的用法：&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(aplpack);&#xA;library(rpart);&#xA;cardata&amp;lt;-car.test.frame[,6:7];&#xA;par(mfrow=c(1,1))&#xA;bagplot(cardata,verbose=F,factor=3,show.baghull=T,&#xA;    dkmethod=2,show.loophull=T,precision=1)&#xA;title(&amp;quot;car data Chambers/Hastie 1992&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;可以看到图形右上角有一些偏离中心较远的点被染成了血红色，那就是布袋图眼中的野值。&lt;/p&gt;&#xA;&lt;p&gt;如果随机生成一个正态数据，对应的布袋图会是什么样子？&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;seed=222;&#xA;set.seed(seed);&#xA;n=200;&#xA;data=rnorm(n,mean=0);&#xA;datan&amp;lt;-cbind(data,c(rnorm(n-2,mean=20),10,30));&#xA;datan[,2]&amp;lt;-datan[,2]*300;&#xA;bagplot(datan,factor=3,create.plot=T,approx.limit=300,&#xA;    show.outlier=T,show.looppoints=T,&#xA;    show.bagpoints=T,dkmethod=2,&#xA;    show.whiskers=T,show.loophull=T,&#xA;    show.baghull=T,verbose=F)&#xA;title(paste(&amp;quot;seed: &amp;quot;,seed,&amp;quot;/ n: &amp;quot;,n));&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;我们增加数据规模看看会是什么效果。&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;seed &amp;lt;- 194&#xA;n &amp;lt;- 3000&#xA;set.seed(seed)&#xA;data &amp;lt;- rnorm(n)&#xA;datan&amp;lt;-cbind(data,c(rnorm(n-2,mean=76,sd=20),105,50));&#xA;datan[,2]&amp;lt;-datan[,2]*300&#xA;bagplot(datan,factor=3,create.plot=T,approx.limit=300,&#xA;    show.outlier=T,show.looppoints=T,&#xA;    show.bagpoints=T,dkmethod=2,&#xA;    show.whiskers=T,show.loophull=T,&#xA;    show.baghull=T,verbose=F)&#xA;title(paste(&amp;quot;seed: &amp;quot;,seed,&amp;quot;/ n: &amp;quot;,n))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;p&gt;像一颗海胆。&lt;/p&gt;&#xA;&lt;h1 id=&#34;其他异常值检测方法&#34;&gt;其他异常值检测方法&lt;/h1&gt;&#xA;&lt;p&gt;除了布袋图之外，还有很多异常值的识别方法，这些方法大致可以分为可视化方法、模型方法和经验方法三类。&lt;/p&gt;&#xA;&lt;h2 id=&#34;可视化方法&#34;&gt;可视化方法&lt;/h2&gt;&#xA;&lt;p&gt;可视化方法识别异常值是最直观的一种方法了。常用的图形主要包括箱线图、QQ图、直方图、密度图、小提琴图、布袋图、散点图、热力图以及局部离群因子（LOF）得分图。&lt;/p&gt;&#xA;&lt;h3 id=&#34;箱线图&#34;&gt;箱线图&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 箱线图&#xA;boxplot(data, main = &amp;quot;Boxplot for Outlier Detection&amp;quot;, col = &amp;quot;lightblue&amp;quot;)&#xA;# 添加异常值标记&#xA;outliers &amp;lt;- boxplot.stats(data)$out&#xA;points(rep(1, length(outliers)), outliers, col = &amp;quot;red&amp;quot;, pch = 19)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;qq图&#34;&gt;QQ图&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;qqnorm(data, main = &amp;quot;Q-Q Plot for Normality Check&amp;quot;)&#xA;qqline(data, col = &amp;quot;red&amp;quot;)&#xA;# 标出异常值&#xA;qq_outliers &amp;lt;- data[abs(scale(data)) &amp;gt; 3]&#xA;points(which(data %in% qq_outliers), qq_outliers, col = &amp;quot;red&amp;quot;, pch = 19)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;直方图&#34;&gt;直方图&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;hist(data, breaks = 30, col = &amp;quot;skyblue&amp;quot;, main = &amp;quot;Histogram with Outliers&amp;quot;)&#xA;abline(v = outliers, col = &amp;quot;red&amp;quot;, lwd = 2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;密度图&#34;&gt;密度图&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 密度图&#xA;plot(density(data), main = &amp;quot;Density Plot with Outliers&amp;quot;)&#xA;rug(data, col = &amp;quot;blue&amp;quot;)&#xA;rug(outliers, col = &amp;quot;red&amp;quot;, lwd = 2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-7-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;小提琴图&#34;&gt;小提琴图&lt;/h3&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;# 使用ggplot2&#xA;library(ggplot2)&#xA;ggplot(data.frame(value = data), aes(x = &amp;quot;&amp;quot;, y = value)) +&#xA;  geom_violin(fill = &amp;quot;lightgreen&amp;quot;) +&#xA;  geom_boxplot(width = 0.1, fill = &amp;quot;orange&amp;quot;) +&#xA;  ggtitle(&amp;quot;Violin Plot with Boxplot&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/24/bagplot-and-extreme-value-detection/index_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;h3 id=&#34;布袋图&#34;&gt;布袋图&lt;/h3&gt;&#xA;&lt;p&gt;见本文开篇内容，不赘述。&lt;/p&gt;</description>
    </item>
    <item>
      <title>脸谱图</title>
      <link>http://gewutang.cn/2010/03/20/chernoff/</link>
      <pubDate>Sat, 20 Mar 2010 00:00:00 +0000</pubDate>
      <guid>http://gewutang.cn/2010/03/20/chernoff/</guid>
      <description>&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmltools-fill/fill.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-binding/plotly.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/typedarray/typedarray.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/jquery/jquery.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/crosstalk/css/crosstalk.min.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/crosstalk/js/crosstalk.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-htmlwidgets-css/plotly-htmlwidgets.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-main/plotly-latest.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmltools-fill/fill.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/d3/d3.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/sankey/sankey.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/sankeyNetwork-binding/sankeyNetwork.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmltools-fill/fill.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-binding/plotly.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/typedarray/typedarray.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/jquery/jquery.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/crosstalk/css/crosstalk.min.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/crosstalk/js/crosstalk.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;link href=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-htmlwidgets-css/plotly-htmlwidgets.css&#34; rel=&#34;stylesheet&#34; /&gt;&#xA;&lt;script src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/plotly-main/plotly-latest.min.js&#34;&gt;&lt;/script&gt;&#xA;&lt;h1 id=&#34;脸谱图&#34;&gt;脸谱图&lt;/h1&gt;&#xA;&lt;p&gt;出于一种个体的审美原因，我一直都很欣赏Chernoff创造的脸谱图。它让我感觉到了统计学家的浪漫，让我摆脱了对统计学家严谨、保守的刻板印象。&lt;/p&gt;&#xA;&lt;p&gt;脸谱图工作原理极其简单，它用人类的脸部特征来刻画多维变量，与雷达图、平行坐标图等在本质上并无太大不同。只不过，它带着人类的五官出现，是一种显得可爱又好玩的针对多元数据的可视化方法，可能十分适合用来激发人们对多元数据可视化的兴趣。&lt;/p&gt;&#xA;&lt;p&gt;一般的Chernoff脸谱图是这样的，以R中自带的鸢尾花数据集（iris）为例:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(aplpack)&#xA;faces(iris[1:20,1:4],face.type=0)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;img src=&#34;http://gewutang.cn/2010/03/20/chernoff/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&#xA;&lt;pre&gt;&lt;code&gt;## effect of variables:&#xA;##  modified item       Var           &#xA;##  &amp;quot;height of face   &amp;quot; &amp;quot;Sepal.Length&amp;quot;&#xA;##  &amp;quot;width of face    &amp;quot; &amp;quot;Sepal.Width&amp;quot; &#xA;##  &amp;quot;structure of face&amp;quot; &amp;quot;Petal.Length&amp;quot;&#xA;##  &amp;quot;height of mouth  &amp;quot; &amp;quot;Petal.Width&amp;quot; &#xA;##  &amp;quot;width of mouth   &amp;quot; &amp;quot;Sepal.Length&amp;quot;&#xA;##  &amp;quot;smiling          &amp;quot; &amp;quot;Sepal.Width&amp;quot; &#xA;##  &amp;quot;height of eyes   &amp;quot; &amp;quot;Petal.Length&amp;quot;&#xA;##  &amp;quot;width of eyes    &amp;quot; &amp;quot;Petal.Width&amp;quot; &#xA;##  &amp;quot;height of hair   &amp;quot; &amp;quot;Sepal.Length&amp;quot;&#xA;##  &amp;quot;width of hair   &amp;quot;  &amp;quot;Sepal.Width&amp;quot; &#xA;##  &amp;quot;style of hair   &amp;quot;  &amp;quot;Petal.Length&amp;quot;&#xA;##  &amp;quot;height of nose  &amp;quot;  &amp;quot;Petal.Width&amp;quot; &#xA;##  &amp;quot;width of nose   &amp;quot;  &amp;quot;Sepal.Length&amp;quot;&#xA;##  &amp;quot;width of ear    &amp;quot;  &amp;quot;Sepal.Width&amp;quot; &#xA;##  &amp;quot;height of ear   &amp;quot;  &amp;quot;Petal.Length&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;上图可能看起来比较呆板。我们当然可以画一个更好看的：&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
