如何在烛台图中检测离群值

时间:2019-11-30 17:32:47

标签: c# math mathematical-optimization

我想实施离群值检测,因为我应该在市场不景气的时候找到它。换句话说,我有一个给定的蜡烛阵列,对于每5个蜡烛,我应该确定情况是否安静。

这种情况什么时候安静?

满足以下条件的情况很安静:

  1. 所有这5支蜡烛都差不多。蜡烛的体积通过以下公式计算:Math.Abs(candle.Open - candle.Close)

这里的问题是我不知道如何区分小蜡烛和大蜡烛。

enter image description here

  1. 最高和最低元素之间的距离不应太大。

enter image description here

这里的问题是如何确定最大值和最小值之间的距离是大还是小

我的尝试:

我通过计算标准偏差进行了尝试,但由于结果过于随机,因此似乎无济于事。这些高容量的蜡烛不符合条件。

  • StdDev =标准偏差
  • CV =变化系数
Open time: 11/27/2019 1:00:00 AM | StdDev: 7.188532534530273E-05 | CV: 0.004710394164556892
Open time: 11/27/2019 2:00:00 AM | StdDev: 6.919176251549048E-05 | CV: 0.004528256709128957
Open time: 11/27/2019 3:00:00 AM | StdDev: 3.946517452134247E-05 | CV: 0.0025799290397687433
Open time: 11/27/2019 4:00:00 AM | StdDev: 3.6979724174201114E-05 | CV: 0.002417133418798687
Open time: 11/27/2019 5:00:00 AM | StdDev: 3.110466202999165E-05 | CV: 0.002034447120805262
Open time: 11/27/2019 6:00:00 AM | StdDev: 5.2985847166956244E-05 | CV: 0.003472887669067067
Open time: 11/27/2019 7:00:00 AM | StdDev: 8.348652585896753E-05 | CV: 0.0054882018050859535
Open time: 11/27/2019 8:00:00 AM | StdDev: 9.300537618869152E-05 | CV: 0.006126836376066635
Open time: 11/27/2019 9:00:00 AM | StdDev: 8.136338242723194E-05 | CV: 0.0053698114062323095
Open time: 11/27/2019 10:00:00 AM | StdDev: 3.840572873934283E-05 | CV: 0.002541742471167626
Open time: 11/27/2019 11:00:00 AM | StdDev: 7.612489737267272E-05 | CV: 0.00505410286633068
Open time: 11/27/2019 12:00:00 PM | StdDev: 0.0001445423813281074 | CV: 0.009635516387447998
Open time: 11/27/2019 1:00:00 PM | StdDev: 0.00016103571032538092 | CV: 0.010780272481281359
Open time: 11/27/2019 2:00:00 PM | StdDev: 0.0001487615541731129 | CV: 0.009964602731134899
Open time: 11/27/2019 3:00:00 PM | StdDev: 0.00019730686759461785 | CV: 0.013188080181446285
Open time: 11/27/2019 4:00:00 PM | StdDev: 0.00025021490762942194 | CV: 0.016638842108619628
Open time: 11/27/2019 5:00:00 PM | StdDev: 0.00024766408702111035 | CV: 0.016331294890940345
Open time: 11/27/2019 6:00:00 PM | StdDev: 0.0001475466028073844 | CV: 0.009644829572975841
Open time: 11/27/2019 7:00:00 PM | StdDev: 0.0002146625258399797 | CV: 0.01390211293568938
Open time: 11/27/2019 8:00:00 PM | StdDev: 0.00034918476484520355 | CV: 0.022346394780827054
Open time: 11/27/2019 9:00:00 PM | StdDev: 0.0003890790408130464 | CV: 0.02460501111826006
Open time: 11/27/2019 10:00:00 PM | StdDev: 0.0003559950842357241 | CV: 0.022273358207828574

代码:

/// <summary>
/// Calculates the standard deviation.
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public static double StandardDeviation(this List<BinanceKline> values)
{
    if (values.Count > 0)
    {
        // Compute average
        double mean = values.Mean();

        // Perform the Sum of (value - avg)_2_2
        double sum = values.Sum(e => Math.Pow(Convert.ToDouble((e.GetUpperValue() + e.GetLowerValue()) / 2) - mean, 2));

        // Put it all together
        return Math.Sqrt(sum / (values.Count - 1));
    }

    return 0;
}

顺便说一句,它获取蜡烛(the highest value + the lowest value) / 2的平均值。

可能仍然不清楚我想要什么,所以这是一张照片:

enter image description here

在图片的左侧,没有大蜡烛,但是图形在上升,这意味着它与其余的蜡烛相去甚远。在图片的右侧,图形保持安静(“嵌入式”)。它不会向上或向下而是直线。

您能建议我如何检测那些模式吗?我的想法不是预测未来的价值,而是使用已经获取的数据。例如,这些检查将出现在每支新蜡烛上,并且将花费前五支蜡烛(不包括新蜡烛)。

编辑:

标准偏差实际上不起作用,因为如果您在此处查看:

Open time: 11/27/2019 12:00:00 PM | StdDev: 0.0001445423813281074 | CV: 0.009635516387447998
Open time: 11/27/2019 1:00:00 PM | StdDev: 0.00016103571032538092 | CV: 0.010780272481281359

标准偏差应为较高的值,并且绝对不能接近0。代码可以正常工作,但不适用于这种情况,或者至少不是单独适用。结合其他方法,可能会起作用。

enter image description here

编辑2:

变异系数(CV)似乎更准确。我认为这可能有效。满足条件的最佳值可能是 0.001 作为两个图的摘要。您对此有何看法?

enter image description here

上图为该示例的输出:

Open time: 11/15/2019 5:00:00 AM | StdDev: 49.24148243605151 | CV: 0.005714494887752725
Open time: 11/15/2019 6:00:00 AM | StdDev: 48.10100328995206 | CV: 0.005597444742963025
Open time: 11/15/2019 7:00:00 AM | StdDev: 34.570228051026604 | CV: 0.00403204199023016
Open time: 11/15/2019 8:00:00 AM | StdDev: 15.476614859199533 | CV: 0.0018079612622890022
Open time: 11/15/2019 9:00:00 AM | StdDev: 11.576726437123352 | CV: 0.0013526856869483194
Open time: 11/15/2019 10:00:00 AM | StdDev: 13.672853762107772 | CV: 0.0015963730504533972
Open time: 11/15/2019 11:00:00 AM | StdDev: 22.617282551182015 | CV: 0.0026365899074065743
Open time: 11/15/2019 12:00:00 PM | StdDev: 31.647747155208304 | CV: 0.0036823320018835684
Open time: 11/15/2019 1:00:00 PM | StdDev: 30.173184825271647 | CV: 0.0035044316829057126
Open time: 11/15/2019 2:00:00 PM | StdDev: 26.069585871279347 | CV: 0.0030231190376703017
Open time: 11/15/2019 3:00:00 PM | StdDev: 12.839695966026326 | CV: 0.0014866559587110115
Open time: 11/15/2019 4:00:00 PM | StdDev: 6.756121853548856 | CV: 0.0007815331369451161
Open time: 11/15/2019 5:00:00 PM | StdDev: 41.376141434406144 | CV: 0.004795673597019467
Open time: 11/15/2019 6:00:00 PM | StdDev: 79.81362075172356 | CV: 0.00928666074947359
Open time: 11/15/2019 7:00:00 PM | StdDev: 88.35104423548115 | CV: 0.010319785070149313
Open time: 11/15/2019 8:00:00 PM | StdDev: 79.06051554031244 | CV: 0.009271338691229074
Open time: 11/15/2019 9:00:00 PM | StdDev: 34.327389319609104 | CV: 0.00404133486502223
Open time: 11/15/2019 10:00:00 PM | StdDev: 8.048344239655792 | CV: 0.0009489408690167187
Open time: 11/15/2019 11:00:00 PM | StdDev: 6.030277356805678 | CV: 0.0007108047078810328
Open time: 11/16/2019 12:00:00 AM | StdDev: 7.8962145994142245 | CV: 0.0009308828374337032
Open time: 11/16/2019 1:00:00 AM | StdDev: 8.47613886153356 | CV: 0.00099890434169011
Open time: 11/16/2019 2:00:00 AM | StdDev: 9.912337262220127 | CV: 0.0011679248762150303
Open time: 11/16/2019 3:00:00 AM | StdDev: 12.102422484775284 | CV: 0.0014266919637413724
Open time: 11/16/2019 4:00:00 AM | StdDev: 12.490426133643089 | CV: 0.0014727030576166967
Open time: 11/16/2019 5:00:00 AM | StdDev: 12.640563970804447 | CV: 0.0014904318275539364
Open time: 11/16/2019 6:00:00 AM | StdDev: 15.174553947315598 | CV: 0.001790813176679162
Open time: 11/16/2019 7:00:00 AM | StdDev: 8.421461719915296 | CV: 0.0009946885198633634
Open time: 11/16/2019 8:00:00 AM | StdDev: 8.337805916426555 | CV: 0.0009848295065977384
Open time: 11/16/2019 9:00:00 AM | StdDev: 6.463572348167512 | CV: 0.0007636420370893617
Open time: 11/16/2019 10:00:00 AM | StdDev: 5.982922153931075 | CV: 0.0007068892832431122
Open time: 11/16/2019 11:00:00 AM | StdDev: 6.279278023786336 | CV: 0.0007414932821942903
Open time: 11/16/2019 12:00:00 PM | StdDev: 10.051010521335781 | CV: 0.0011860719031101202
Open time: 11/16/2019 1:00:00 PM | StdDev: 10.679646178596128 | CV: 0.0012597437184238111
Open time: 11/16/2019 2:00:00 PM | StdDev: 8.387603948685346 | CV: 0.0009891293173925218
Open time: 11/16/2019 3:00:00 PM | StdDev: 6.277716145224007 | CV: 0.0007401768729218733
Open time: 11/16/2019 4:00:00 PM | StdDev: 6.0087794101637115 | CV: 0.0007084052927520973
Open time: 11/16/2019 5:00:00 PM | StdDev: 5.152108063696313 | CV: 0.0006074484521114183
Open time: 11/16/2019 6:00:00 PM | StdDev: 5.4574552219879005 | CV: 0.0006434300416311673
Open time: 11/16/2019 7:00:00 PM | StdDev: 5.885869731823884 | CV: 0.000693675723796855

1 个答案:

答案 0 :(得分:1)

编辑:

我希望有人提出一个更好的主意。

https://pastebin.com/CG3EApQJ-TRXUSDT(在问题的第一张图中)-25.11.2019-1小时间隔(Binance)

https://pastebin.com/mP6rFMBh-BTCUSDT(在问题的最后一张图中)-2019年11月15日-1小时间隔(Binance)

这些是数据集。

我的解决方案不一定在所有情况下都有效:

当图表发生巨大变化时,此解决方案将无法在较小的间隔内工作。

一种解决方案是使用变异系数(CV)。我将以前的蜡烛数量从5减少到3。如果我们看下面的图片,CV的最佳值似乎低于0.003。当然,它也有其自身的风险,例如,我在图表上标记了两个蜡烛,它们满足了不应该满足的条件。原因是它们都具有低于0.003的CV 〜0.0024 ,但是如果我降低最佳值,则将使条件触发的时间小于现在。我愿意冒险。

数量似乎对我的雇主来说无关紧要。

让我知道是否有人有更好的主意。

double mean = previousThree.Average(e => Convert.ToDouble((e.GetUpperValue() + e.GetLowerValue()) / 2));
double stdDev = previousThree.StandardDeviation();
double CV = stdDev / mean;

if (CV < 0.003)
{
    ...

enter image description here

完成输出(顺便说一下,您可以在图像的底部看到时间):

Open time: 11/15/2019 6:00:00 AM | StdDev: 21.79939238449838 | CV: 0.002546419014161643
Open time: 11/15/2019 7:00:00 AM | StdDev: 5.527038839499387 | CV: 0.0006463308292687913
Open time: 11/15/2019 8:00:00 AM | StdDev: 6.721079402397433 | CV: 0.0007855694359396128
Open time: 11/15/2019 9:00:00 AM | StdDev: 9.884363071706904 | CV: 0.0011540322400728816
Open time: 11/15/2019 10:00:00 AM | StdDev: 10.56525595209722 | CV: 0.0012323968402993787
Open time: 11/15/2019 11:00:00 AM | StdDev: 20.77817023545048 | CV: 0.002418664495847048
Open time: 11/15/2019 1:00:00 PM | StdDev: 14.03211524800573 | CV: 0.0016258230041103752
Open time: 11/15/2019 2:00:00 PM | StdDev: 2.5677925020016534 | CV: 0.00029718593815783684
Open time: 11/15/2019 3:00:00 PM | StdDev: 4.399211103520008 | CV: 0.0005089810112396999
Open time: 11/15/2019 4:00:00 PM | StdDev: 5.89065856533281 | CV: 0.0006811122779212644
Open time: 11/15/2019 8:00:00 PM | StdDev: 3.4549831162148847 | CV: 0.0004076268215686829
Open time: 11/15/2019 9:00:00 PM | StdDev: 6.009339259297565 | CV: 0.0007085389024100516
Open time: 11/15/2019 10:00:00 PM | StdDev: 6.966699960048646 | CV: 0.000820984413750046
Open time: 11/15/2019 11:00:00 PM | StdDev: 3.8589765482570897 | CV: 0.0004546572963875896
Open time: 11/16/2019 12:00:00 AM | StdDev: 9.872978274057026 | CV: 0.0011639766843458792
Open time: 11/16/2019 1:00:00 AM | StdDev: 10.532862858691386 | CV: 0.0012417051013596555
Open time: 11/16/2019 2:00:00 AM | StdDev: 13.48298310216733 | CV: 0.0015886661458626638
Open time: 11/16/2019 3:00:00 AM | StdDev: 14.516428624148151 | CV: 0.0017105560467653054
Open time: 11/16/2019 4:00:00 AM | StdDev: 14.166371271429696 | CV: 0.001670413927937984
Open time: 11/16/2019 5:00:00 AM | StdDev: 3.1662714665675957 | CV: 0.0003737283544812391
Open time: 11/16/2019 6:00:00 AM | StdDev: 11.023689642461964 | CV: 0.001301947284349989
Open time: 11/16/2019 7:00:00 AM | StdDev: 8.144219320065119 | CV: 0.0009624218864802652
Open time: 11/16/2019 8:00:00 AM | StdDev: 7.101746850833978 | CV: 0.0008392963759194558
Open time: 11/16/2019 9:00:00 AM | StdDev: 3.861995382355842 | CV: 0.0004562276725712975
Open time: 11/16/2019 10:00:00 AM | StdDev: 2.094411214001339 | CV: 0.0002473434553656345
Open time: 11/16/2019 11:00:00 AM | StdDev: 6.657929482955174 | CV: 0.0007859942652924222
Open time: 11/16/2019 12:00:00 PM | StdDev: 10.629339505977429 | CV: 0.001253610389059389
Open time: 11/16/2019 1:00:00 PM | StdDev: 6.061025765770376 | CV: 0.0007143462774168898
Open time: 11/16/2019 2:00:00 PM | StdDev: 7.244390933681763 | CV: 0.0008538886060445265
Open time: 11/16/2019 3:00:00 PM | StdDev: 5.592790746428975 | CV: 0.0006595600071814469
Open time: 11/16/2019 4:00:00 PM | StdDev: 3.195912754337602 | CV: 0.0003769570371269494
Open time: 11/16/2019 5:00:00 PM | StdDev: 5.220498858666279 | CV: 0.0006154845426749733
Open time: 11/16/2019 6:00:00 PM | StdDev: 3.1226471142281893 | CV: 0.0003679999616082095
Open time: 11/16/2019 7:00:00 PM | StdDev: 2.799403210209787 | CV: 0.0003297732337714766
Open time: 11/16/2019 8:00:00 PM | StdDev: 3.1363367697571047 | CV: 0.000369478868446045
Open time: 11/16/2019 9:00:00 PM | StdDev: 5.763087280268954 | CV: 0.0006791052255747596
Open time: 11/16/2019 10:00:00 PM | StdDev: 4.148072845712606 | CV: 0.0004890656230962655
Open time: 11/16/2019 11:00:00 PM | StdDev: 9.092711825046113 | CV: 0.0010729950221168257
Open time: 11/17/2019 12:00:00 AM | StdDev: 7.247889922821099 | CV: 0.000855676558811373
Open time: 11/17/2019 1:00:00 AM | StdDev: 8.573442035340188 | CV: 0.0010120719571685153
Open time: 11/17/2019 2:00:00 AM | StdDev: 7.988032194060166 | CV: 0.0009421901067363134