“活”的简单移动平均值流 - 快速实现 [英] simple moving average of "live" stream - fast implementation

查看:217
本文介绍了“活”的简单移动平均值流 - 快速实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的交易应用程序中,我有股票价格的活。我需要维护SMA。让我们假设我想要SMA的20蜡烛,其中每个蜡烛的持续时间是10秒。这意味着



每10秒我有一个checkpoint,其中:


  1. 我关闭当前蜡烛并存储最近10秒的平均价格。平均为(max - min)/ 2

  2. 我开始新蜡烛和商店最后价格。

  3. 清除过时的蜡烛。

每一刻:


  1. 我更新当前形成蜡烛的最后价格,并重新计算SMA。

需要重新计算SMA。在大多数情况下,只有最后蜡烛的价格发生变化(因为我们使用最后价格)。每10秒钟一次,我需要多一些额外的工作 - 我需要忘记平均过时的蜡烛,存储平均刚刚创建蜡烛。



<你能建议如何实现这一最低延迟吗?低延迟是主要需求。

解决方案

我不知道这是否是你要找的方法,伪代码用于非常快速的SMA。



简单移动平均线



您的数据以某种流的形式存储在连续的内存位置(至少具有可连续映射的地址)

  x [ 1]到x [2000]包含前2000个数据点

//它们不必是一个真正的数组,只是一个管理
的函数// a'circular阵列并将位置映射到存储器中的真实位置。
//这两种情况下,它足够小,足以完全在缓存中
//
//后续价格将在'locations x [2001]等等可访问
//这里我们正在计算最近2000年的MA的价格

MA2000(1,2000)=(x [1] + x [2] + ... + x [2000] / 2000 //通常平均
//只做一次

MA2000(2,2001)= MA2000(1,2000)* 2000 + x [2001] - x [1]
MA2000(2,2001)/ = 2000

这样有两个加法和一个乘法

指数移动平均线:
这是一个不错的替代方案,如上所述:

  //对于N天EMA 
Alpha = 2 /(N + 1) //一次计算

EMA(新)= Alpha * NewTickPrice +(1-Alpha)* EMA(旧)

这里不是一个N日移动平均线。这只是一个加权移动平均值,在最近N天的重量约为87%,所以几乎N天是更喜欢的。



关于编译器优化的注意事项



请注意,启用SSE或AVX选项(如果可用)将大大加快这些算法的速度,因为多个计算可以在单个CPU周期。


In my trading application I have "live ticks" of stock prices. I need to maintain SMA. Let's assume I want SMA of 20 candles, where duration of each candle is 10 seconds. This means that

Every 10 seconds I have "checkpoint" where:

  1. I "close" current candle and store average price for the last 10 seconds. Average is (max - min) / 2
  2. I "start" new candle and store last price.
  3. I clean-up "outdated" candle.

Every tick:

  1. I update "last" price of current "forming" candle and recalculate SMA.

So on any tick I need to "recalculate" SMA. In most cases only price of the last candle is changed (cause we using last price). Once per 10 seconds I need a little bit more extra work - I need to "forget" average of the outdated candle, and "store" average of "just created" candle.

Can you suggest how to implement this with lowest latency? Low latency is primary requirement.

解决方案

I'm not sure whether this is the approach you're looking for but here is the pseudocode for very quick SMAs.

Simple Moving Average:

I assume that your data is coming in the form of some stream and stored in continuous memory location (at least with continuously mappable addresses)

x[1] to x[2000] contain the first 2000 data points

// they don't have to be a real array, could just be a function which manages
// a 'circular' array and maps the 'locations' to real locations in memory.
// Either case, it's small enough to be fully in the cache hopefully
//
// Subsequent prices will be accessible in 'locations x[2001], etc.
// Here we are looking to calculate the MA over the latest 2000 ticks

MA2000(1,2000) = (x[1] + x[2] + ... + x[2000]) / 2000 // Usual average
                                                      // Done only once

MA2000(2,2001) = MA2000(1,2000) * 2000 + x[2001] - x[1]
MA2000(2,2001) /= 2000

That way with two additions and one multiplication ( with 1/2000 ) you can generate subsequent moving averages for the new ticks.

Exponential moving average: That is a decent alternative, as mentioned above:

// For an N-day EMA
Alpha = 2 / (N+1)      // one time calculation

EMA(new) = Alpha * NewTickPrice + (1-Alpha) * EMA(old)

Here it's not really an N-day moving average. It's just a weighted moving average with ~87% weightage to the last N-days, so almost N-days is more like it.

Note on compiler optimizations:

Do note that turning on SSE or AVX options if available will enable massive speedup of these algorithms as multiple calculations can be churned out in a single CPU cycle.

这篇关于“活”的简单移动平均值流 - 快速实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆