Monday, 30 January 2017

Risk Control Rectangle

In this article, I would like to talk about risk management over the course of trading. Amateur investors tend to put most of their attention on how to invest to maximise their profits. There is nothing wrong about this, the point of investment is to make profit after all. However professional investors will consider seriously about risk management before the investment and during the investment.

What is the top priority of investment? To make money! One would naturally say. The truth is that the top priority of investment is to protect most of your capital intact during your investment process. The result of investment can only be two: profit or loss. You just need to be sure when loss happen, you don’t lose all or most of your money, such that you can still have the capital to continue to invest and hopefully make profit at the end. This is what a risk management system does.

If we consider investment as a battle, and chasing for profit is the offence aspect of a battle, then risk management is the defence aspect of the battle. If we fight long term battles, a pure offensive strategy will not last us very long while a good mix of offence and defence will keep us going and wining.

There are two risk controls in risk management
  • Credit risk control
  • Profit/Loss control

Credit risk control

Credit risk control is about choosing how much from your money pool to invest. It is the risk control before the investment.

There is a saying about stock market investment: “10% of investors making money, 20% of investors just breaking even, and the remaining 70% of investors losing money”. I am not sure whether this figure is accurate but I guess over 50% of the investors losing money in stock market probably not an under estimation. Here comes the question, why is that?

Before jump into conclusion, let me take an example. Say Joe has $1000 to invest, and he invests into a stock that has 50% of going up and 50% of going down. Joe is not a total amateur investor and he has strict win/loss control. Whenever the stock goes up 20% or goes down 20%, he will close the position and stop win/loss. And he chooses to only invest twice.

Then Joe wasn’t extremely luck and didn’t win on both investments, and he wasn’t extremely unlucky and didn’t lose on both investments. He ended up one won and one lost. With that, there are two possibilities:
  1. Joe won on the first, and lost on the second. His capital after first investment = 1000 + 1000 x 20% = 1200; His capital after second investment = 1200 – 1000 x 20% = 1000
  2. Joe lost on the first, and won on the second. His capital after first investment = 1000 - 1000 x 20% = 800; His capital after second investment = 800 + 800 * 20% = 960
What is happening there in scenario 2)? He won and lost exactly once, we would expect the capital is back to 1000 but why it is not? This is because after the first lost, Joe’s capital is down to 800, even he won 20% in the second investment, it's the 20% of 800 gain versus 20% of 1000 lost in the first investment.

The lost hurt more than the cure of won. This is always true in the investment world and some serious lost can make investor forever lost the opportunity of seeing the future win. This is where credit control comes in. You should always control how much you invest, do not dump all your cash in one go. In Joe’s example, if he had chosen to invest only $800 and not all his $1000 cash, he would have ended up even after a lost and a win.

Credit risk control is to calculate the potential risk of loss and choose a reasonable proportion of your cash out from your capital to invest to avoid run into credit risk.

Profit/Loss control

Nowadays, most of people know or have heard about stop loss or loss control. This is when you have picked a wrong investment; you decide to take a reasonable loss to exit and avoid possible greater future loss. The concept is straight forward.

Why do we need to control the profit? Isn’t that the bigger the better? Well the reason behind it is similar to why we need to control loss:

  • We predict the price will go down when we have a long position or the price will go up if we have a short position
  • Through historical data study we learn that the stock has reach the peak and most likely will go the other way
  • We need to lock in our profit.
As part of profit/risk control, we also have the time control. This is best to illustrate in the following diagram, which I call it the Risk Control Rectangle.

The following graph is a FX price chart taken for USDJPY. I have put a rectangle on a segment of the chart. As you can see each edge of the rectangle is drawn with different colors. I will explain the meaning of each edge and how they work together to form a strict risk control frame. For the purpose of easier to explain, I will assume we take long position in this example.

Blue edge – This is when we get into a position. The intersection of blue edge and the price line is the position price. When a position is taken, we have invested into something, opening up the opportunity of profit, as well as loss.

Red edge – This is where the stop loss control is. When the market price drops below this line, we will need to exit the position and cut loss. Depending on our trading strategy, this line can be fixed or can be dynamically changed over time, e.g. trailing stop loss control.

Green edge – This is where stop win control is. When stock price has gone up this line, we exit our position and lock down the profit. Again subject to trading strategy, this line can be fixed or can be dynamically changed over time.

Black edge – This when we consider our invest period is end and when we need to exit the position just because time runs out. The end time control is especially important for day trading strategy – at least we need to exit all positions before the market closed for day. It also makes sense for quantitative trading. If our strategy is based on x period of data, then our position held should be shorten then x period. Why is that? Quantitative trading is a game of predict the future based on the pass. It wouldn’t make sense if you study 3 days data and try to predict what will happen over 4 days, would it?


Through the risk control rectangle, we firmly control our profit/loss within a price range and time series range. Although the risk control rectangle itself doesn’t guarantee us the profit, it does protect our capital from serious loss in a single investment. This is extremely important for automated trading strategy since you won’t have a trader to watch the market for you.

Tuesday, 17 January 2017

量化交易系统: 宏观看

量化交易对普罗大众来说好像带有一种神秘的面纱,网上可以找到不少有关量化交易的文章,许多这些文章只是限于对某种策略或某个角度的特定分析,在深度上面 发掘,但缺乏对量化交易从一个宏观的角度去介绍。在这篇文章里,我想对量化交易平行展开并进行多维度的分析,希望你在阅读这篇文章后对量化交易得到一个较 为全面的理解。


量化交易系统,英文叫Algorithmic Trading或Quantitative Trading,是通过借助人的经验和数据分析以及电脑的强大计算能力,找出交易的制胜策略,并且把策略实现自动化执行。简而言之量化交易系统就是一个证 劵分析和交易的人工智能系统(Artificial Intelligence)。

  • 模拟人类的交易策略,并且更精准更快
  • 可以进行大数据分析和发掘,能够找出人类经验之外的制胜策略
  • 可以进行策略回测和前测,提高制胜几率
  • 人类无法比拟的交易速度
  • 钢铁般的神经,不会犯人类因情绪波动而犯的错误


  • 死板,没有模糊思维的能力
  • 比较于人体系统,量化系统是相对封闭的系统,缺少对宏观和市场数据以外的认知
  • 对突发事件和市场的异变的应变能力比较差



在市场信息对称的情况下,我们是无法预知未来的。量化交易却是是建立在历史会一定程度上重演过去的哲学思维上面(history will repeat itself),通过研究过去找出规律,并且相信历史会重演。

可是历史一定会重演吗?重演的程度有多少?每个市场,每种商品都会有一定的规律。更多时候是某些时期很有规律,让我们相信已经找到了它的规律,有些时候却 变得杂乱无章,让我们怀疑这个规律是否已经消失。

市场本身的千变万化,能否在万千现象中找出可以持续一段时期的规律正是量化交易的挑战所在。我喜欢告诉别人的一句话是:量化交易是一种在不确定性的迷雾中 寻找确定性的游戏(A game of looking for certainty in the mist of uncertainty)



代 理量化交易

这种量化策略在机构投资者的卖方比较普遍,服务于大的基金和私募公司,针对的是大的单一或者一篮子股票的单边交易。这类的量化策略一向被大的国际投行以及 个别专业做量化交易系统的公司垄断。外人不明白其需求,也缺乏多元资产交易系统的基础建设去做这个业务。这类量化系统的目的其实只有一个,就是避免市场冲 击以达到比较好的成交价格。代理量化交易策略发展到今天除了在交易所的交易还包括了黑池的交易。我的另外一篇文章对这类量化交易策略有比较详细的描述代 理量化交易策略

自 营量化交易

这类交易策略种类很多,诸如投机跟风,对冲套利,长期或高频,琳琅满目如万花筒一般。其目的性倒是非常简单,就是通过交易实现赢利。无论是高端上档次的根 据复杂数学模型构造的策略,还是简单原始的定价买进卖出策略,在赢利这个绝对标准面前,一律平等!这也是从机构投资者到个人投资者最多人从事的量化交易。


量化交易的周期和频率也因为策略的不同有很大的差别,有几天数个星期长到跨年的,也有毫秒级到微秒级别的。策略周期差别虽然很大,但没有好坏之分,能到达 我们交易的目的就是好策略。一般来说长周期的策略的模型可以容许比较复杂的数学和逻辑模型,越是短的周期和高频率的策略的模型应该相对简单以减低运算消耗 时间,因为拼的是速度。



选定商品 – 比如通过某些经济指标,技术指标以及历史数据发掘,从可交易商品中选出N个最可以预测的商品作为交易对象,这是商品选定策略决定的

制定策略– 选好了商品之后就是要通过交易商品获利,然后什么时候进行交易?买进的价位是多少?卖出的价位应该是多少?这就是交易策略决定的

执行订单 – 决定买进或卖出的时间和价格之后就是执行订单,许多量化系统都包括订单的自动执行,这就涉及到基本交易系统的支持和应付订单未能成交或出错的结果。不要轻 视这部分的挑战,越是专业的系统对交易系统的自动化要求越高!

基于实际需求的不同,这三个步骤并不是每个步骤都是必须的。比如有许多人他们根据自身经验,已经知道他们想要交易什么商品,自然可以免去第一步。而对于很 长周期的交易策略来说,也许自动执行订单没有那么重要,毕竟十天半个月都等了,还在乎一两分钟的价格差别吗?他们也许会选择等信号出来之后,用人工确定一 下市场状况之后采用人手下单的方式。



-    收 集原始历史数据和静态/参考数据 - 收集数据
-    验 证和整理原始历史行情数据 - 收集的数据是否齐全和正确?
-    提 炼和推导历史行情数据 - 根据原始数据进一步推算出对我们数学模型有帮助的数据
-    建 立量化模型和策略 - 建立模型和交易策略
-    制 定风控框架 - 必须考虑到资金的限制,考虑可以承受最大的回撤
-    回 测和前测 - 必须考虑买卖差价/滑价/手续费/资金成本等各种消耗
-    实 盘测试 - 如果回测和前测都正面的话,我们要投放少量资金进行实盘测试,真实交易永远和纸上谈兵有一定距离
-    上 线 - 投入生产



-    用 户模块 - 用户的登录安全控制
-    资 金账户模块 - 一个用户根据交易商品的币种和市场,可以有多个资金账户
-    信 用控制模块 - 根据资金的额度进行信用的控制
-    仓 位管理模块 - 保存和管理仓位,计算损益
-    行 情模块 - 从行情信号源引入实时行情
-    静 态数据模块 - 保存静态数据比如商品名称,手数,杠杆,单位价格,价格跳动单位等等
-    策 略数据模块 - 这里指的是推演过的历史数据,比如K线,移动平均线,MACD,以及各人自己定义的数据模块 等等
-    订 单管理模块 - 保管和处理订单
-    交 易执行模块 - 包括劵商/交易所接口模块等等
-    量 化交易框架 - 包括策略管理/执行/中断/脚本语言嵌入等等
-    损 益实时计算模块 - 这是交易员最关心的,进仓之后我这一刻的浮动盈亏是多少?
-    风 险控制模块 - 基于浮动盈亏和未来市场走势,自动及时止盈或者止损
-    用 户使用界面 - 用于对策略运行的监控和管理,简单策略的构造,策略脚本语言的输入




事 件驱动系统

投资银行在量化交易系统的开发上面也走过很长的路(包括弯路),目前比较一致认可的是采用事件驱动系统作为量化交易系统的基础框架。原因是量化交易的本身 就是由许许多多不同种类的事件去驱动。比如行情驱动信号的计算,信号的产生会触发下单的请求,下单请求驱动交易接口订单的传发,然后会有成交的推送,有成 交就有仓位的变化,仓位的变化就会有盈亏或者浮动盈亏的变化,然后还有账户资金的变动。这种通过一个事件去触发另外一连串事件的最好解决方法就是采用事件 驱动系统,另外交易系统本身存在着高并发,异步处理的现实中间混合着同布处理的要求,等等这种特性都需要一个好的事件驱动系统去解决。

记 忆体和数据库存储

所有的交易系统都希望越快越好,能够在“看”到这个价钱的时候“拿”到这个价钱是量化交易最希望的结果。越低的延迟越能改善滑价的状况,长期交易下来的话 也许利润就从这里来。那么主要的下单延迟从哪里来呢?

  1. 行情推动的策略运算
  2. 风控系统的运算
  3. 下单系统的计算
  4. 劵商到交易所的延迟
  5. 网络的延迟

对于4我们通常无能为力,对于5我们可以做的是使用专线,合理选择服务器地理位置或者co-location等硬件手段来加快。对于软件设计来说1-3是 我们可以对延迟做改善的地方。除了优化算法之外,一个明显有效的方法就是需要把所有和1-3有关的数据都放在记忆体里面,尽量避免对磁盘的读写。然而,假 如系统崩溃发生的话,记忆体里面的信息是会消失的,所以有许多放在记忆体里面的数据是需要存储在数据库里面的。有什么方法可以避免运行速度受限于磁盘读写 而又避免崩溃的时候丢失数据呢?有两种方法:

  • 记忆体写完之后,再用另外一个线程同步到数据库
  • 用记忆体数据库(内部其实用的也是第一种方法)
异 步/次序/线程

在上面的章节已经提到过,交易系统的外来信息都是异步的,可是我们为了增加处理数据的频宽和并发性,一般都需要采用多线程的体系结构。多线程的结构,如果 没有特殊机制的话,会导致信息处理的次序错乱。一般来说,对于同一个策略的信息,必须同一条线程处理以防止次序错乱,这是一个比较简单的的处理方法。


文章不算太长,提及了量化交易的各个层面以及量化交易系统的基本设计考虑。量化交易的种类繁多,手段五花八门,这边文章也未能全部覆盖,对此量化交易系统也需 要做出调整以应付各种需求,不过在量化交易基本模块这 个段落里提到的却是大部分专业量化交易系统所必备的。感谢阅读并欢迎讨论。