378 lines
13 KiB
HTML
378 lines
13 KiB
HTML
|
<h1>Chap 1: Algorithmic Trading Fundamentals</h1>
|
|||
|
|
|||
|
<h2>1. 我们为何交易?</h2>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>交易是人类天性</li>
|
|||
|
<li>交易的历史
|
|||
|
<ul>
|
|||
|
<li>在古罗马广场上,有钱人交易货币、债券</li>
|
|||
|
<li>在14世纪威尼斯,交易国债</li>
|
|||
|
<li>1531年,比利时出现了最早的股票交易市场</li>
|
|||
|
<li>在1602(航海时代),荷兰东印度公司公募</li>
|
|||
|
<li>17世纪,法国为探索路易斯安娜公募</li>
|
|||
|
</ul></li>
|
|||
|
<li>上述案例共同点:
|
|||
|
<ul>
|
|||
|
<li>有钱人想变得更有钱;</li>
|
|||
|
<li>最终都失败</li>
|
|||
|
</ul></li>
|
|||
|
<li>交易听起来有利可图,但并非一直如此</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h2>2. 现代交易的基本概念</h2>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>市场基本因素:供需(从古至今)</li>
|
|||
|
<li>现代交易:高效的市场价格发现(技术因素)
|
|||
|
<ul>
|
|||
|
<li>竞争点:</li>
|
|||
|
<li>精细程度</li>
|
|||
|
<li>洞察力</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>2.1 market sector</h3>
|
|||
|
|
|||
|
<p>vs asset classes: 基于标的的不同</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>商品(金属,农产品)</li>
|
|||
|
<li>能源(油,气)</li>
|
|||
|
<li>股票</li>
|
|||
|
<li>债权</li>
|
|||
|
<li>外汇</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>2.2 Asset classes</h3>
|
|||
|
|
|||
|
<p>vs market sectors: 基于金融产品的不同</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>债券,外汇,股票</li>
|
|||
|
<li>基于上述标的的衍生品
|
|||
|
<ul>
|
|||
|
<li>future contract (期货合约)</li>
|
|||
|
<li>必须交割</li>
|
|||
|
<li>option contract (期权合约)</li>
|
|||
|
<li>可不交割,但要付premium(权利金)
|
|||
|
<ul>
|
|||
|
<li>call option</li>
|
|||
|
<li>put option</li>
|
|||
|
</ul></li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h2>3. 现代交易系统</h2>
|
|||
|
|
|||
|
<p><img src="../img/1_2.jpg" alt="" title="" /></p>
|
|||
|
|
|||
|
<h3>3.1 Exchange market data protocol</h3>
|
|||
|
|
|||
|
<p>沟通交易中心和参与者的通讯协议。可基于通讯协议来设计,编写交易软件</p>
|
|||
|
|
|||
|
<h3>3.2 Market data feed handlers</h3>
|
|||
|
|
|||
|
<p>让参与者与指定的exchange market data protocol交互的<strong>程序</strong>. 可以:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>subscribe</li>
|
|||
|
<li>receive</li>
|
|||
|
<li>decode</li>
|
|||
|
<li>检查错误,断网</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>3.3 Exchange order book</h3>
|
|||
|
|
|||
|
<p>Exchange order book记录、维护所有的买卖信息,包括:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>价格</li>
|
|||
|
<li>数量</li>
|
|||
|
<li>order types</li>
|
|||
|
<li>ID</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>Buy order (bids) 根据价格从到到低排列 (价高者先被匹配), 相同价格的由匹配算法决定。</p>
|
|||
|
|
|||
|
<p>匹配算法包括:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>FIFO</li>
|
|||
|
<li>Pro-rata (按比例)</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>3.4 Limit order book</h3>
|
|||
|
|
|||
|
<p>类似Exchange order book,但由参与者创建,因此只有部分信息。</p>
|
|||
|
|
|||
|
<p>目标:收集并安排买卖来获取市场信息。</p>
|
|||
|
|
|||
|
<h3>3.5 Order types</h3>
|
|||
|
|
|||
|
<p>大多数交易中心支持多种下单类型:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><strong>IOC (Immediate Or Cancel)</strong>: 从不加入book,要么被全部执行,要不被取消
|
|||
|
<ul>
|
|||
|
<li>优点:避免由于 order management 带来的麻烦</li>
|
|||
|
</ul></li>
|
|||
|
<li><strong>GTD (Good Till Day)</strong>: 被记入book,如果匹配成功,则交易可执行部分,剩余则加入book,直到能被完全匹配</li>
|
|||
|
<li><strong>Stop orders</strong>: 等到stop price (a specific price),orders 才会被加入 book,并成为GTD
|
|||
|
<ul>
|
|||
|
<li>优点:可被用来止损,或者获利</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>3.6 Exchange order entry protocols</h3>
|
|||
|
|
|||
|
<p>发出交易请求时用的通讯协议。交易中心同样用它来回复交易者</p>
|
|||
|
|
|||
|
<h3>3.7 Order entry gateway</h3>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>交易者的 client app</li>
|
|||
|
<li>通过 order entry protocols 来与交易中心的匹配引擎进行交流(下单,更改价格...)</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>Drop-copy gateways:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>第二套gateway来确定交易的真实性</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>3.8 Positions (仓位) and PnL (profit and loss) management</h3>
|
|||
|
|
|||
|
<h4>3.8.1 Position</h4>
|
|||
|
|
|||
|
<p>下单后交易者就得到了 <strong>position (仓位\头寸)</strong> in the instruments that they got executed, for 执行时的 数量 & 价格</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><strong>Long position (多头头寸)</strong> = buy side execution
|
|||
|
<ul>
|
|||
|
<li>多头赚钱:market price > position price (i.e. buy low)</li>
|
|||
|
</ul></li>
|
|||
|
<li><strong>Short position (空头头寸)</strong> = sell sid execution
|
|||
|
<ul>
|
|||
|
<li>空头赚钱:market price < position price (sell high)</li>
|
|||
|
</ul></li>
|
|||
|
<li><p><strong>Being flat</strong> = no position</p></li>
|
|||
|
<li><p><strong>Open position (未平仓头寸)</strong> = 指尚未对冲或交割的头寸,即持仓者承诺要买入或卖出某些未履约的商品,或买入或卖出没有相反方向相配的商品。 </p></li>
|
|||
|
<li><strong>Close position (平仓)</strong> = 期货交易者买入或卖出与其所持期货合约的品种、数量及交割月份相同但交易方向相反的期货合约,了结期货交易的行为。 </li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h4>3.8.2 VWAP (Volume Weighted Average Price)</h4>
|
|||
|
|
|||
|
<p>在不同价位、数量不同的多头或空头,组合为 <strong>Volume Weighted Average Price (成交量加权平均价</strong>)</p>
|
|||
|
|
|||
|
<h4>3.8.3 Profit and Loss (PoL) of position</h4>
|
|||
|
|
|||
|
<p>通过比较当前市场价格和 open position (未平仓头寸/持仓头寸,即尚未交割的头寸) 的价格,可以得到 unrealized PoL:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>Long position 有 unrealized profit (loss) 当市场价格上涨(下降)</li>
|
|||
|
<li>Profit/Loss is realized when open position is closed (平仓)
|
|||
|
<ul>
|
|||
|
<li>平仓: sell to close a long position, or buy to close a short position.</li>
|
|||
|
</ul></li>
|
|||
|
<li>After close position, PnL become <em>realized PnL</em>.</li>
|
|||
|
<li>total PnL = realized PnLs + unrealized PnLs for open position.</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h2>4. 从直觉到程序交易</h2>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>大多数情况下,交易策略来自于直觉。e.g.
|
|||
|
<ul>
|
|||
|
<li>trend-following (趋势跟踪),跟着大盘走(如果你觉得大盘还会涨、跌)</li>
|
|||
|
<li>mean reversion strategies, 逆着大盘走(如果你觉得大盘已经到顶、底)</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>No trading idea is right all the time.</p>
|
|||
|
|
|||
|
<h3>4.1 为何要自动交易?</h3>
|
|||
|
|
|||
|
<p>手动交易的进化:对人大叫手动挂单 -> 打电话(broker)来挂单 -> 在程序里手动输入来挂单</p>
|
|||
|
|
|||
|
<p>缺点:</p>
|
|||
|
|
|||
|
<ol>
|
|||
|
<li>慢</li>
|
|||
|
<li>容易错过信息</li>
|
|||
|
<li>容易犯错</li>
|
|||
|
<li>容易分心</li>
|
|||
|
</ol>
|
|||
|
|
|||
|
<h3>4.2 算法交易的进化: rule-based to AI</h3>
|
|||
|
|
|||
|
<p>以趋势跟踪来做案例,分析算法交易的进化:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>早期的经典趋势交易:交易员通过图表观测趋势是否开始/持续。e.g. 股票价格连续一周每天上身5%, 那就应该买入;若股票价格两小时内下跌10%,那就要卖出</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h2>5. 算法交易的部件</h2>
|
|||
|
|
|||
|
<p>实际应用中,算法交易系统可以分为两部分:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>Core infrastructure (核心基础设施), 负责:
|
|||
|
<ul>
|
|||
|
<li>deals with exchange-facing market data protocol integration 与交所的数据协议进行整合</li>
|
|||
|
<li>market data handler: 作为handler读取市场数据</li>
|
|||
|
<li>internal market data format normalization 在内部对市场数据进行标准化</li>
|
|||
|
<li>historical data recording 记录历史数据</li>
|
|||
|
<li>instrument definition recording/dissemination 记录并传播金融工具的定义</li>
|
|||
|
<li>exchange order entry protocols 交所下单的通讯协议协议</li>
|
|||
|
<li>exchange order entry gateway 负责下单</li>
|
|||
|
<li>core side risk systems</li>
|
|||
|
<li>broker-facing app, 与中介沟通</li>
|
|||
|
<li>back office reconciliation app, 与后台调和</li>
|
|||
|
<li>addressing compliance requirement 解决合规问题</li>
|
|||
|
<li>...</li>
|
|||
|
</ul></li>
|
|||
|
<li>Algorithmic trading strategy (算法交易系统),负责:
|
|||
|
<ul>
|
|||
|
<li>normalizing market data 标准化市场数据</li>
|
|||
|
<li>building order book 建立 order book</li>
|
|||
|
<li>generating signals from incoming market data and order flow information 通过接收到的市场数据和下单情况产生交易信号</li>
|
|||
|
<li>aggregation of different signals 整合不同的交易信号</li>
|
|||
|
<li>efficient execution logic built on top of statistical predictive abilities (alpha) 基于统计判断能力(alpha)的高效执行</li>
|
|||
|
<li>position and PnL management inside the strategies 仓位管理</li>
|
|||
|
<li>risk management inside strategies 风险控制</li>
|
|||
|
<li>backtesting 回测</li>
|
|||
|
<li>historical signal trading research platforms 研究</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p><img src="../img/1_5_1.jpg" alt="" title="" /></p>
|
|||
|
|
|||
|
<h3>5.1 Market Data subscription 订阅市场数据</h3>
|
|||
|
|
|||
|
<p>负责与 feed handler components 沟通来传播标准化数据。数据在内部网络或本机上通过 Inter-Process Communication (IPC) 来进行传播。</p>
|
|||
|
|
|||
|
<h3>5.2 Limit order books</h3>
|
|||
|
|
|||
|
<p>当交易策略得到标准化后的数据,它能为目标金融工具构建出一个limit order book。</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>简单版:记录多空两方的参与者数量</li>
|
|||
|
<li>复杂版:构建出参与者的先后顺序</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>5.3 Signals(交易信号)</h3>
|
|||
|
|
|||
|
<p>limit order book构建完成后,每次数据更新都能激发交易信号</p>
|
|||
|
|
|||
|
<p>signals 又可被称为 indicators, predictors, calculators, features, alpha, etc.</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>交易信号是精心定义的</li>
|
|||
|
<li>从收到的市场数据信息,limit order book 和交易信息中获取</li>
|
|||
|
<li>让参与者得到对其他参与者的相对优势 (edge or advantage vias-a-vis other market participants)</li>
|
|||
|
<li>交易员花费大量时间构建、更新、添加交易信号</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>5.4 Signal aggregators (信号整合)</h3>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>很多程序交易系统会整合数个交易信号来得到一个更强(better edge)的信号。</li>
|
|||
|
<li>different aggregtion approach:
|
|||
|
<ul>
|
|||
|
<li>linear/non-linear combination 线性、非线性组合</li>
|
|||
|
<li>regression 递归</li>
|
|||
|
<li>...</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>5.5 Execution logic 执行逻辑</h3>
|
|||
|
|
|||
|
<p>好的交易策略要达到 <strong>快速</strong>&<strong>复杂/富有经验</strong>两个目标,二者往往互相冲突。因此,execution logic 需要在二者之间取得平衡,同时尽量隐藏自己的意图。</p>
|
|||
|
|
|||
|
<p>市场是双向透明的,其他参与者可以得到所有挂单的信息,并且推测影响。同时<strong>slippage(滑点)</strong> 和 <strong>fee(交易费)</strong>也不能忽视。</p>
|
|||
|
|
|||
|
<h4>slippage</h4>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>slippage = 预期的交易价格和真实交易价格间的差异</li>
|
|||
|
<li>两个主要原因:
|
|||
|
<ul>
|
|||
|
<li>latency (延迟),当交易请求抵达交易所慢了一步</li>
|
|||
|
<li>large volume (大当量),当挂单数量巨大而被分批次(在不同价位)被成交,那整个交易的 VWAP 就会与预期不同</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>当交易算法的仓位数量变大时,滑点会更加显著</p>
|
|||
|
|
|||
|
<h4>fee</h4>
|
|||
|
|
|||
|
<p>exchange fees & broker feed 往往与交易量正相关</p>
|
|||
|
|
|||
|
<p>有时一个优异的交易策略会因为滑点和交易费而赚不到钱</p>
|
|||
|
|
|||
|
<h3>5.6 Position and PnL management (盈亏管理)</h3>
|
|||
|
|
|||
|
<p>不同的交易策略会导致不同复杂程度的盈亏管理。</p>
|
|||
|
|
|||
|
<p>e.g.:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>pairs trading (配对交易): 基于两个相关性较高的股票或者其他证券,如果在未来时期保持着良好的相关性,一旦两者之间出现了背离的走势,且这种背离在未来是会得到纠正的,那么就可能产生套利的机会。
|
|||
|
<ul>
|
|||
|
<li>需要跟踪多个仓位并对多个金融产品进行盈亏管理。复杂度上升</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>5.7 风险管理</h3>
|
|||
|
|
|||
|
<p>风控是算法交易的基石(cornerstone):</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>违反交易所的规定会导致法律性惩罚。</li>
|
|||
|
<li>高频交易的风险在于bug
|
|||
|
<ul>
|
|||
|
<li>需要大量测试,压力测试,鲁棒测试, etc.</li>
|
|||
|
</ul></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h3>5.8 Backtesting 回测</h3>
|
|||
|
|
|||
|
<p>研究交易算法需要回测。回测能够模拟交易系统的行为并且得到 expected PnL, expected risk exposure, other matrices.</p>
|
|||
|
|
|||
|
<p>高精度的回测系统非常复杂。</p>
|
|||
|
|
|||
|
<p>回测系统可以提供:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>Profit and loss (P and L): 排除交易费用所产生的盈亏</li>
|
|||
|
<li>Net profit and loss (net P and L): 包括交易费用的盈亏</li>
|
|||
|
<li>Exposure: The capital invested 投入的本金</li>
|
|||
|
<li>Number of trades 交易过程中产生的交易数量</li>
|
|||
|
<li>Annualized return: 年化收益</li>
|
|||
|
<li>Sharpe ratio (夏普率): 一项投资(例如证券或投资组合)在对其调整风险后,相对于无风险资产的表现。 计算方法为一项投资(例如证券或投资组合)在对其调整风险后,相对于无风险资产的表现</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h2>Why Python?</h2>
|
|||
|
|
|||
|
<p>此处会实现一个简单的基于趋势跟踪的交易策略(买低卖高)。算法的大致步骤是:</p>
|
|||
|
|
|||
|
<ol>
|
|||
|
<li>得到GOOGLE从2014-01-01到2018-01-01的股票数据,包括当日高位、低位、开盘价、收盘价、交易量</li>
|
|||
|
<li>生成交易信号:
|
|||
|
<ol>
|
|||
|
<li>使用GOOGLE股票的修正后收盘价作为基础,计算每日价格变动</li>
|
|||
|
<li>假如价格变动为正(价格上涨),那就卖出,反之买入(基于自身所拥有仓位的数量上)</li>
|
|||
|
</ol></li>
|
|||
|
<li>生成交易数量的图表,向上箭头表示买入,向下箭头表示卖出</li>
|
|||
|
<li>回测:
|
|||
|
<ol>
|
|||
|
<li>假设拥有$1000作为启动资金,计算3年来 cash (initial amount of money) 和 holding (invest this money)
|
|||
|
<ol>
|
|||
|
<li>若股票价格上涨,value of holding 上涨</li>
|
|||
|
<li>卖出后,这只股票value of the holding 被转移至 cash amount中。</li>
|
|||
|
<li><strong>总资产价格 (sum total of the assets)</strong> = cash + holding </li>
|
|||
|
</ol></li>
|
|||
|
</ol></li>
|
|||
|
</ol>
|