/
OBV.cs
74 lines (65 loc) · 1.83 KB
/
OBV.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Drawing;
using OsEngine.Entity;
using OsEngine.Indicators;
namespace CustomIndicators.Scripts
{
public class OBV:Aindicator
{
private IndicatorDataSeries _series;
public override void OnStateChange(IndicatorState state)
{
if (state == IndicatorState.Configure)
{
_series = CreateSeries("OBV", Color.Orange, IndicatorChartPaintType.Line, true);
}
else if (state == IndicatorState.Dispose)
{
if (_temp != null)
{
_temp.Clear();
}
}
}
public override void OnProcess(List<Candle> candles, int index)
{
_series.Values[index] = GetValue(candles, index);
}
private decimal GetValue(List<Candle> candles, int index)
{
if(index == 0)
{
_temp.Clear();
}
while(index >= _temp.Count)
{
_temp.Add(0);
}
if (index < 2)
{
return 0;
}
if (index > 2)
{
decimal p1 = candles[index].Close;
decimal p2 = candles[index - 1].Close;
if (p1 > p2)
{
_temp[index] = _temp[index - 1] + candles[index].Volume;
}
if (p1 < p2)
{
_temp[index] = _temp[index - 1] - candles[index].Volume;
}
if (p1 == p2)
{
_temp[index] = _temp[index - 1];
}
return Math.Round(_temp[index]);
}
return 0;
}
private List<decimal> _temp = new List<decimal>();
}
}