Finished chap1
commit
34cbbddc20
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "Learn-Algorithmic-Trading---Fundamentals-of-Algorithmic-Trading"]
|
||||||
|
path = Learn-Algorithmic-Trading---Fundamentals-of-Algorithmic-Trading
|
||||||
|
url = git@github.com:PacktPublishing/Learn-Algorithmic-Trading---Fundamentals-of-Algorithmic-Trading.git
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"python.pythonPath": "/home/jason/miniconda3/envs/quants/bin/python"
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 08161f2bd7ae94ad26e7e2b6ec9b088eff50583b
|
|
@ -0,0 +1,377 @@
|
||||||
|
<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>
|
|
@ -0,0 +1,290 @@
|
||||||
|
# Chap 1: Algorithmic Trading Fundamentals
|
||||||
|
|
||||||
|
## 1. 我们为何交易?
|
||||||
|
|
||||||
|
* 交易是人类天性
|
||||||
|
* 交易的历史
|
||||||
|
* 在古罗马广场上,有钱人交易货币、债券
|
||||||
|
* 在14世纪威尼斯,交易国债
|
||||||
|
* 1531年,比利时出现了最早的股票交易市场
|
||||||
|
* 在1602(航海时代),荷兰东印度公司公募
|
||||||
|
* 17世纪,法国为探索路易斯安娜公募
|
||||||
|
* 上述案例共同点:
|
||||||
|
* 有钱人想变得更有钱;
|
||||||
|
* 最终都失败
|
||||||
|
* 交易听起来有利可图,但并非一直如此
|
||||||
|
|
||||||
|
## 2. 现代交易的基本概念
|
||||||
|
|
||||||
|
* 市场基本因素:供需(从古至今)
|
||||||
|
* 现代交易:高效的市场价格发现(技术因素)
|
||||||
|
* 竞争点:
|
||||||
|
* 精细程度
|
||||||
|
* 洞察力
|
||||||
|
|
||||||
|
### 2.1 market sector
|
||||||
|
|
||||||
|
vs asset classes: 基于标的的不同
|
||||||
|
|
||||||
|
* 商品(金属,农产品)
|
||||||
|
* 能源(油,气)
|
||||||
|
* 股票
|
||||||
|
* 债权
|
||||||
|
* 外汇
|
||||||
|
|
||||||
|
### 2.2 Asset classes
|
||||||
|
|
||||||
|
vs market sectors: 基于金融产品的不同
|
||||||
|
|
||||||
|
* 债券,外汇,股票
|
||||||
|
* 基于上述标的的衍生品
|
||||||
|
* future contract (期货合约)
|
||||||
|
* 必须交割
|
||||||
|
* option contract (期权合约)
|
||||||
|
* 可不交割,但要付premium(权利金)
|
||||||
|
* call option
|
||||||
|
* put option
|
||||||
|
|
||||||
|
## 3. 现代交易系统
|
||||||
|
|
||||||
|
![](../img/1_2.jpg)
|
||||||
|
|
||||||
|
### 3.1 Exchange market data protocol
|
||||||
|
|
||||||
|
沟通交易中心和参与者的通讯协议。可基于通讯协议来设计,编写交易软件
|
||||||
|
|
||||||
|
### 3.2 Market data feed handlers
|
||||||
|
|
||||||
|
让参与者与指定的exchange market data protocol交互的**程序**. 可以:
|
||||||
|
|
||||||
|
* subscribe
|
||||||
|
* receive
|
||||||
|
* decode
|
||||||
|
* 检查错误,断网
|
||||||
|
|
||||||
|
### 3.3 Exchange order book
|
||||||
|
|
||||||
|
Exchange order book记录、维护所有的买卖信息,包括:
|
||||||
|
|
||||||
|
* 价格
|
||||||
|
* 数量
|
||||||
|
* order types
|
||||||
|
* ID
|
||||||
|
|
||||||
|
Buy order (bids) 根据价格从到到低排列 (价高者先被匹配), 相同价格的由匹配算法决定。
|
||||||
|
|
||||||
|
匹配算法包括:
|
||||||
|
|
||||||
|
* FIFO
|
||||||
|
* Pro-rata (按比例)
|
||||||
|
|
||||||
|
### 3.4 Limit order book
|
||||||
|
|
||||||
|
类似Exchange order book,但由参与者创建,因此只有部分信息。
|
||||||
|
|
||||||
|
目标:收集并安排买卖来获取市场信息。
|
||||||
|
|
||||||
|
### 3.5 Order types
|
||||||
|
|
||||||
|
大多数交易中心支持多种下单类型:
|
||||||
|
|
||||||
|
* **IOC (Immediate Or Cancel)**: 从不加入book,要么被全部执行,要不被取消
|
||||||
|
* 优点:避免由于 order management 带来的麻烦
|
||||||
|
* **GTD (Good Till Day)**: 被记入book,如果匹配成功,则交易可执行部分,剩余则加入book,直到能被完全匹配
|
||||||
|
* **Stop orders**: 等到stop price (a specific price),orders 才会被加入 book,并成为GTD
|
||||||
|
* 优点:可被用来止损,或者获利
|
||||||
|
|
||||||
|
### 3.6 Exchange order entry protocols
|
||||||
|
|
||||||
|
发出交易请求时用的通讯协议。交易中心同样用它来回复交易者
|
||||||
|
|
||||||
|
### 3.7 Order entry gateway
|
||||||
|
|
||||||
|
* 交易者的 client app
|
||||||
|
* 通过 order entry protocols 来与交易中心的匹配引擎进行交流(下单,更改价格...)
|
||||||
|
|
||||||
|
Drop-copy gateways:
|
||||||
|
|
||||||
|
* 第二套gateway来确定交易的真实性
|
||||||
|
|
||||||
|
### 3.8 Positions (仓位) and PnL (profit and loss) management
|
||||||
|
|
||||||
|
#### 3.8.1 Position
|
||||||
|
|
||||||
|
下单后交易者就得到了 **position (仓位\头寸)** in the instruments that they got executed, for 执行时的 数量 & 价格
|
||||||
|
|
||||||
|
* **Long position (多头头寸)** = buy side execution
|
||||||
|
* 多头赚钱:market price > position price (i.e. buy low)
|
||||||
|
* **Short position (空头头寸)** = sell sid execution
|
||||||
|
* 空头赚钱:market price < position price (sell high)
|
||||||
|
* **Being flat** = no position
|
||||||
|
|
||||||
|
* **Open position (未平仓头寸)** = 指尚未对冲或交割的头寸,即持仓者承诺要买入或卖出某些未履约的商品,或买入或卖出没有相反方向相配的商品。
|
||||||
|
* **Close position (平仓)** = 期货交易者买入或卖出与其所持期货合约的品种、数量及交割月份相同但交易方向相反的期货合约,了结期货交易的行为。
|
||||||
|
|
||||||
|
#### 3.8.2 VWAP (Volume Weighted Average Price)
|
||||||
|
|
||||||
|
在不同价位、数量不同的多头或空头,组合为 **Volume Weighted Average Price (成交量加权平均价**)
|
||||||
|
|
||||||
|
#### 3.8.3 Profit and Loss (PoL) of position
|
||||||
|
|
||||||
|
通过比较当前市场价格和 open position (未平仓头寸/持仓头寸,即尚未交割的头寸) 的价格,可以得到 unrealized PoL:
|
||||||
|
|
||||||
|
* Long position 有 unrealized profit (loss) 当市场价格上涨(下降)
|
||||||
|
* Profit/Loss is realized when open position is closed (平仓)
|
||||||
|
* 平仓: sell to close a long position, or buy to close a short position.
|
||||||
|
* After close position, PnL become *realized PnL*.
|
||||||
|
* total PnL = realized PnLs + unrealized PnLs for open position.
|
||||||
|
|
||||||
|
## 4. 从直觉到程序交易
|
||||||
|
|
||||||
|
* 大多数情况下,交易策略来自于直觉。e.g.
|
||||||
|
* trend-following (趋势跟踪),跟着大盘走(如果你觉得大盘还会涨、跌)
|
||||||
|
* mean reversion strategies, 逆着大盘走(如果你觉得大盘已经到顶、底)
|
||||||
|
|
||||||
|
No trading idea is right all the time.
|
||||||
|
|
||||||
|
### 4.1 为何要自动交易?
|
||||||
|
|
||||||
|
手动交易的进化:对人大叫手动挂单 -> 打电话(broker)来挂单 -> 在程序里手动输入来挂单
|
||||||
|
|
||||||
|
缺点:
|
||||||
|
|
||||||
|
1. 慢
|
||||||
|
2. 容易错过信息
|
||||||
|
3. 容易犯错
|
||||||
|
4. 容易分心
|
||||||
|
|
||||||
|
### 4.2 算法交易的进化: rule-based to AI
|
||||||
|
|
||||||
|
以趋势跟踪来做案例,分析算法交易的进化:
|
||||||
|
|
||||||
|
* 早期的经典趋势交易:交易员通过图表观测趋势是否开始/持续。e.g. 股票价格连续一周每天上身5%, 那就应该买入;若股票价格两小时内下跌10%,那就要卖出
|
||||||
|
|
||||||
|
## 5. 算法交易的部件
|
||||||
|
|
||||||
|
实际应用中,算法交易系统可以分为两部分:
|
||||||
|
|
||||||
|
* Core infrastructure (核心基础设施), 负责:
|
||||||
|
* deals with exchange-facing market data protocol integration 与交所的数据协议进行整合
|
||||||
|
* market data handler: 作为handler读取市场数据
|
||||||
|
* internal market data format normalization 在内部对市场数据进行标准化
|
||||||
|
* historical data recording 记录历史数据
|
||||||
|
* instrument definition recording/dissemination 记录并传播金融工具的定义
|
||||||
|
* exchange order entry protocols 交所下单的通讯协议协议
|
||||||
|
* exchange order entry gateway 负责下单
|
||||||
|
* core side risk systems
|
||||||
|
* broker-facing app, 与中介沟通
|
||||||
|
* back office reconciliation app, 与后台调和
|
||||||
|
* addressing compliance requirement 解决合规问题
|
||||||
|
* ...
|
||||||
|
* Algorithmic trading strategy (算法交易系统),负责:
|
||||||
|
* normalizing market data 标准化市场数据
|
||||||
|
* building order book 建立 order book
|
||||||
|
* generating signals from incoming market data and order flow information 通过接收到的市场数据和下单情况产生交易信号
|
||||||
|
* aggregation of different signals 整合不同的交易信号
|
||||||
|
* efficient execution logic built on top of statistical predictive abilities (alpha) 基于统计判断能力(alpha)的高效执行
|
||||||
|
* position and PnL management inside the strategies 仓位管理
|
||||||
|
* risk management inside strategies 风险控制
|
||||||
|
* backtesting 回测
|
||||||
|
* historical signal trading research platforms 研究
|
||||||
|
|
||||||
|
![](../img/1_5_1.jpg)
|
||||||
|
|
||||||
|
### 5.1 Market Data subscription 订阅市场数据
|
||||||
|
|
||||||
|
负责与 feed handler components 沟通来传播标准化数据。数据在内部网络或本机上通过 Inter-Process Communication (IPC) 来进行传播。
|
||||||
|
|
||||||
|
### 5.2 Limit order books
|
||||||
|
|
||||||
|
当交易策略得到标准化后的数据,它能为目标金融工具构建出一个limit order book。
|
||||||
|
|
||||||
|
* 简单版:记录多空两方的参与者数量
|
||||||
|
* 复杂版:构建出参与者的先后顺序
|
||||||
|
|
||||||
|
### 5.3 Signals(交易信号)
|
||||||
|
|
||||||
|
limit order book构建完成后,每次数据更新都能激发交易信号
|
||||||
|
|
||||||
|
signals 又可被称为 indicators, predictors, calculators, features, alpha, etc.
|
||||||
|
|
||||||
|
* 交易信号是精心定义的
|
||||||
|
* 从收到的市场数据信息,limit order book 和交易信息中获取
|
||||||
|
* 让参与者得到对其他参与者的相对优势 (edge or advantage vias-a-vis other market participants)
|
||||||
|
* 交易员花费大量时间构建、更新、添加交易信号
|
||||||
|
|
||||||
|
### 5.4 Signal aggregators (信号整合)
|
||||||
|
|
||||||
|
* 很多程序交易系统会整合数个交易信号来得到一个更强(better edge)的信号。
|
||||||
|
* different aggregtion approach:
|
||||||
|
* linear/non-linear combination 线性、非线性组合
|
||||||
|
* regression 递归
|
||||||
|
* ...
|
||||||
|
|
||||||
|
### 5.5 Execution logic 执行逻辑
|
||||||
|
|
||||||
|
好的交易策略要达到 **快速**&**复杂/富有经验**两个目标,二者往往互相冲突。因此,execution logic 需要在二者之间取得平衡,同时尽量隐藏自己的意图。
|
||||||
|
|
||||||
|
市场是双向透明的,其他参与者可以得到所有挂单的信息,并且推测影响。同时**slippage(滑点)** 和 **fee(交易费)**也不能忽视。
|
||||||
|
|
||||||
|
#### slippage
|
||||||
|
|
||||||
|
* slippage = 预期的交易价格和真实交易价格间的差异
|
||||||
|
* 两个主要原因:
|
||||||
|
* latency (延迟),当交易请求抵达交易所慢了一步
|
||||||
|
* large volume (大当量),当挂单数量巨大而被分批次(在不同价位)被成交,那整个交易的 VWAP 就会与预期不同
|
||||||
|
|
||||||
|
当交易算法的仓位数量变大时,滑点会更加显著
|
||||||
|
|
||||||
|
#### fee
|
||||||
|
|
||||||
|
exchange fees & broker feed 往往与交易量正相关
|
||||||
|
|
||||||
|
有时一个优异的交易策略会因为滑点和交易费而赚不到钱
|
||||||
|
|
||||||
|
### 5.6 Position and PnL management (盈亏管理)
|
||||||
|
|
||||||
|
不同的交易策略会导致不同复杂程度的盈亏管理。
|
||||||
|
|
||||||
|
e.g.:
|
||||||
|
|
||||||
|
* pairs trading (配对交易): 基于两个相关性较高的股票或者其他证券,如果在未来时期保持着良好的相关性,一旦两者之间出现了背离的走势,且这种背离在未来是会得到纠正的,那么就可能产生套利的机会。
|
||||||
|
* 需要跟踪多个仓位并对多个金融产品进行盈亏管理。复杂度上升
|
||||||
|
|
||||||
|
### 5.7 风险管理
|
||||||
|
|
||||||
|
风控是算法交易的基石(cornerstone):
|
||||||
|
|
||||||
|
* 违反交易所的规定会导致法律性惩罚。
|
||||||
|
* 高频交易的风险在于bug
|
||||||
|
* 需要大量测试,压力测试,鲁棒测试, etc.
|
||||||
|
|
||||||
|
### 5.8 Backtesting 回测
|
||||||
|
|
||||||
|
研究交易算法需要回测。回测能够模拟交易系统的行为并且得到 expected PnL, expected risk exposure, other matrices.
|
||||||
|
|
||||||
|
高精度的回测系统非常复杂。
|
||||||
|
|
||||||
|
回测系统可以提供:
|
||||||
|
|
||||||
|
* Profit and loss (P and L): 排除交易费用所产生的盈亏
|
||||||
|
* Net profit and loss (net P and L): 包括交易费用的盈亏
|
||||||
|
* Exposure: The capital invested 投入的本金
|
||||||
|
* Number of trades 交易过程中产生的交易数量
|
||||||
|
* Annualized return: 年化收益
|
||||||
|
* Sharpe ratio (夏普率): 一项投资(例如证券或投资组合)在对其调整风险后,相对于无风险资产的表现。 计算方法为一项投资(例如证券或投资组合)在对其调整风险后,相对于无风险资产的表现
|
||||||
|
|
||||||
|
## Why Python?
|
||||||
|
|
||||||
|
此处会实现一个简单的基于趋势跟踪的交易策略(买低卖高)。算法的大致步骤是:
|
||||||
|
|
||||||
|
1. 得到GOOGLE从2014-01-01到2018-01-01的股票数据,包括当日高位、低位、开盘价、收盘价、交易量
|
||||||
|
2. 生成交易信号:
|
||||||
|
1. 使用GOOGLE股票的修正后收盘价作为基础,计算每日价格变动
|
||||||
|
2. 假如价格变动为正(价格上涨),那就卖出,反之买入(基于自身所拥有仓位的数量上)
|
||||||
|
3. 生成交易数量的图表,向上箭头表示买入,向下箭头表示卖出
|
||||||
|
4. 回测:
|
||||||
|
1. 假设拥有$1000作为启动资金,计算3年来 cash (initial amount of money) 和 holding (invest this money)
|
||||||
|
1. 若股票价格上涨,value of holding 上涨
|
||||||
|
2. 卖出后,这只股票value of the holding 被转移至 cash amount中。
|
||||||
|
3. **总资产价格 (sum total of the assets)** = cash + holding
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,37 @@
|
||||||
|
import pandas as pd
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from pandas_datareader import data
|
||||||
|
|
||||||
|
# Getting data
|
||||||
|
start_date = '2014-01-01'
|
||||||
|
end_date = '2018-01-01'
|
||||||
|
goog_data = data.DataReader('GOOG','yahoo',start_date,end_date)
|
||||||
|
print(goog_data)
|
||||||
|
|
||||||
|
# Preparing the signal
|
||||||
|
goog_data_signal = pd.DataFrame(index=goog_data.index)
|
||||||
|
goog_data_signal['price'] = goog_data['Adj Close']
|
||||||
|
goog_data_signal['daily_difference'] = goog_data_signal['price'].diff()
|
||||||
|
print(goog_data_signal.head())
|
||||||
|
|
||||||
|
goog_data_signal['signal'] = 0.0 # Create a signal based on values of daily_difference
|
||||||
|
goog_data_signal['signal'] = np.where(goog_data_signal['daily_difference'] > 0, 1.0,0.0) # 0 when we need buy
|
||||||
|
print(goog_data_signal.head())
|
||||||
|
|
||||||
|
# We cannot buy/sell limiteless, we can only buy/sell the amount of position we have
|
||||||
|
goog_data_signal['positions'] = goog_data_signal['signal'].diff()
|
||||||
|
print(goog_data_signal.head())
|
||||||
|
|
||||||
|
# Signal visualization
|
||||||
|
fig = plt.figure()
|
||||||
|
ax1 = fig.add_subplot(111,ylabel='Google price in $')
|
||||||
|
goog_data_signal['price'].plot(ax=ax1, color='r', lw=2.)
|
||||||
|
ax1.plot(goog_data_signal.loc[goog_data_signal.positions == 1.0].index, \
|
||||||
|
goog_data_signal.price[goog_data_signal.positions == 1.0],
|
||||||
|
'^', markersize=5, color='m')
|
||||||
|
ax1.plot(goog_data_signal.loc[goog_data_signal.positions == -1.0].index, \
|
||||||
|
goog_data_signal.price[goog_data_signal.positions == -1.0],
|
||||||
|
'v', markersize=5, color='k')
|
||||||
|
|
||||||
|
plt.show()
|
Binary file not shown.
After Width: | Height: | Size: 182 KiB |
Binary file not shown.
After Width: | Height: | Size: 152 KiB |
|
@ -0,0 +1,12 @@
|
||||||
|
@startmindmap
|
||||||
|
* 我们为何交易
|
||||||
|
** 交易是天性
|
||||||
|
*** 买低卖高
|
||||||
|
**** 古罗马,古罗马广场,有钱人交易货币
|
||||||
|
**** 14世纪,威尼斯,交易国债
|
||||||
|
**** 1531,比利时,首个股票交易中心
|
||||||
|
**** 1602, 荷兰东印度公司公募
|
||||||
|
**** 17世纪,法国探索路易斯安娜公募
|
||||||
|
*** 共同点:有钱人想变得更有钱;最终都失败
|
||||||
|
*** 现代交易市场已经非常透明
|
||||||
|
@endmindmap
|
|
@ -0,0 +1,11 @@
|
||||||
|
@startmindmap
|
||||||
|
* Why are we trading?
|
||||||
|
** trading = inherent part of humankind.
|
||||||
|
*** Buy at low price & sell at high price
|
||||||
|
**** Ancient Roman, Roman Forum, exchange currencies
|
||||||
|
**** 14 century, venice, government debts
|
||||||
|
**** 1531, Antwerp Belgium stock exchange
|
||||||
|
**** 1602 Dutch East India open captial for investors.
|
||||||
|
**** 1700, French expedition to Louisiana
|
||||||
|
*** Now, price become more efficient.
|
||||||
|
@endmindmap
|
|
@ -0,0 +1,25 @@
|
||||||
|
@startmindmap
|
||||||
|
* 现代交易的基本概念:
|
||||||
|
** 市场基本因素:供需
|
||||||
|
** 现代交易:高效的市场价格发现(因为科技)
|
||||||
|
*** 竞争点:
|
||||||
|
**** 精细程度
|
||||||
|
**** 洞察力
|
||||||
|
** market sector (基于标的)
|
||||||
|
*** 商品(金属,农产品)
|
||||||
|
*** 能源(油,气)
|
||||||
|
*** 股票
|
||||||
|
*** 债权
|
||||||
|
*** 外汇
|
||||||
|
** Asset classes (资产类别)
|
||||||
|
*** 债券,外汇,股票
|
||||||
|
*** 基于上述标的的衍生品
|
||||||
|
**** future contract (期货合约)
|
||||||
|
***** 必须交割
|
||||||
|
**** option contract (期权合约)
|
||||||
|
***** 可不交割,但要付premium(权利金)
|
||||||
|
***** call option
|
||||||
|
***** put option
|
||||||
|
** 现代交易系统
|
||||||
|
|
||||||
|
@endmindmap
|
|
@ -0,0 +1,29 @@
|
||||||
|
@startmindmap
|
||||||
|
* Basic concepts regarding the modern trading setup
|
||||||
|
** Market driver: supply/demand
|
||||||
|
** Modern trading: efficient market price discovery
|
||||||
|
*** competition:
|
||||||
|
**** granularity of data participants recieve & handle
|
||||||
|
**** sophistication of insight
|
||||||
|
** Market sectors
|
||||||
|
*** i.e. different kinds of underlying products that can be traded.
|
||||||
|
*** e.g.
|
||||||
|
**** commodities (metal, agricultural produce)
|
||||||
|
**** energy (oil, gas)
|
||||||
|
**** equities (stocks)
|
||||||
|
**** interest rate bonds (coupons)
|
||||||
|
**** foreign exchange
|
||||||
|
** Asset classes
|
||||||
|
*** an asset class is a group of financial instruments which have similar financial characteristics and behave similarly in the marketplace.
|
||||||
|
*** e.g.
|
||||||
|
**** cash interest rate bonds, cash foreign exchange, cash stock shares
|
||||||
|
**** derivatives of above: future & options
|
||||||
|
*** future contract: buyer/seller cannot refuse
|
||||||
|
*** option contract: buyer/seller can refuse to buy or sell
|
||||||
|
** Basics of what a modern trading
|
||||||
|
*** trading done electronically through different software.
|
||||||
|
*** Market data feed handler process/understand market data
|
||||||
|
**** market data is published in specific protocol (e.g. FIX/FAST,ITCH)
|
||||||
|
*** Handler app relay information back to buyer/seller.
|
||||||
|
*** buyer/seller made decision and communicated to exchange through similar software application (order entry gateways)
|
||||||
|
@endmindmap
|
|
@ -0,0 +1,25 @@
|
||||||
|
@startmindmap
|
||||||
|
* Algo trading concepts
|
||||||
|
** Exchange order book: maintains all incoming buy/sell orders in exchange
|
||||||
|
*** Buy order (bids): sort from high price to low
|
||||||
|
**** same price? depending matching algo (e.g. FIFO)
|
||||||
|
*** Sell order (asks): sorted from low price to high
|
||||||
|
*** participant can:
|
||||||
|
**** place new order
|
||||||
|
**** cancel order
|
||||||
|
**** modify order detail
|
||||||
|
** Exchange matching algo:
|
||||||
|
*** when match happen
|
||||||
|
**** when incoming bids >= existing ask
|
||||||
|
**** when incoming asks <= existing bids
|
||||||
|
*** FIFO matching
|
||||||
|
**** priority sequence: price > time
|
||||||
|
*** Pro-rata matching
|
||||||
|
**** priority sequence: price > quantity > time
|
||||||
|
** Limit order book:
|
||||||
|
*** built by market participants
|
||||||
|
*** market data sent out by exchange
|
||||||
|
*** purpose: collect/arrange bids/offers to get insight InterfaceBorderColor
|
||||||
|
** Exchange market data protocols
|
||||||
|
***
|
||||||
|
@endmindmap
|
|
@ -0,0 +1,10 @@
|
||||||
|
@startmindmap
|
||||||
|
* section 1: Intro and Env Setup
|
||||||
|
** Algo Trading Fundamental
|
||||||
|
*** Why are we trading?
|
||||||
|
*** Basic concepts regarding modern trading setup
|
||||||
|
*** Understanding algo trading concepts
|
||||||
|
*** From intuition to algo trading
|
||||||
|
*** Components of algo trading
|
||||||
|
*** Why Python?
|
||||||
|
@endminkmap
|
|
@ -0,0 +1,10 @@
|
||||||
|
@startmindmap
|
||||||
|
* 章节1:介绍
|
||||||
|
** 为何交易?
|
||||||
|
** 算法交易基础
|
||||||
|
*** 现代交易基本概念
|
||||||
|
*** 理解交易概念
|
||||||
|
*** 从直觉到算法交易
|
||||||
|
*** 算法交易部件
|
||||||
|
*** 为和选择Python
|
||||||
|
@endmindmap
|
Loading…
Reference in New Issue