-
Notifications
You must be signed in to change notification settings - Fork 0
/
RXstats_2old.fsx
177 lines (148 loc) · 5.11 KB
/
RXstats_2old.fsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//http://enumeratethis.com/2011/07/26/financial-charts-reactive-extensions/
(*
var rand = new Random();
var prices = Observable.Generate(
5d,
i => i > 0,
i => i + rand.NextDouble() - 0.5,
i => i,
i => TimeSpan.FromSeconds(0.1)
);
from buffer in prices.Buffer(TimeSpan.FromSeconds(1))
select new
{
Open = buffer.First(),
High = buffer.Max(),
Low = buffer.Min(),
Close = buffer.Last()
}
ar source = new Subject<char>();
var timer = Observable.Interval(TimeSpan.FromSeconds(1));
var buffer = source.Buffer(() => timer);
buffer.Subscribe(Console.WriteLine);
source.OnNext('a');
source.OnNext('b');
source.OnNext('c');
Thread.Sleep(1100);
source.OnNext('d');
source.OnNext('e');
source.OnNext('f');
source.OnCompleted();
class OHLC
{
public double? Open;
public double? High;
public double? Low;
public double Close;
}
// (TAccumulate, TSource) -> TAccumulate
static OHLC Accumulate(OHLC state, double price)
{
// Take the current values & apply the price update.
state.Open = state.Open ?? price;
state.High = state.High.HasValue ? state.High > price ? state.High : price : price;
state.Low = state.Low.HasValue ? state.Low < price ? state.Low : price : price;
state.Close = price;
return state;
}
from window in prices.Window(TimeSpan.FromSeconds(1))
from result in window.Aggregate(new OHLC(), Accumulate)
select result
using System;
namespace ConsoleApplication124
{
using System.Reactive.Linq;
class Program
{
static void Main()
{
var rand = new Random();
var prices = Observable.Generate(
5d, i => i > 0, i => i + rand.NextDouble() - 0.5, i => i, i => TimeSpan.FromSeconds(0.1));
var query = from window in prices.Window(TimeSpan.FromSeconds(1))
from result in window.Aggregate(new Ohlc(), Accumulate)
select result;
query.Subscribe(Console.WriteLine);
Console.ReadLine();
}
class Ohlc
{
public double? Open;
public double? High;
public double? Low;
public double Close;
public override string ToString()
{
return (new { Open, High, Low, Close }).ToString();
}
}
static Ohlc Accumulate(Ohlc current, double price)
{
current.Open = current.Open ?? price;
current.High = current.High.HasValue ? current.High > price ? current.High : price : price;
current.Low = current.Low.HasValue ? current.Low < price ? current.Low : price : price;
current.Close = price;
return current;
}
}
}
using System;
using System.Windows.Forms;
using System.Reactive.Linq;
using System.Windows.Forms.DataVisualization.Charting;
namespace Chart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Configure the chart
var series = chart1.Series[0];
series.ChartType = SeriesChartType.Candlestick;
series.XValueType = ChartValueType.Time;
var area = chart1.ChartAreas[0];
area.Axes[0].Title = "Time";
area.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
area.AxisX.LabelStyle.Format = "T";
// Test prices
var rand = new Random();
var prices = Observable.Generate(5d, i => i > 0, i => i + rand.NextDouble() - 0.5, i => i, i => TimeSpan.FromSeconds(0.1));
// OHLC query
var query =
from window in prices.Window(TimeSpan.FromSeconds(1))
from ohlc in window.Aggregate(new OHLC(), Accumulate)
select ohlc;
// Subscribe & display results
query.ObserveOn(this).Subscribe(x => series.Points.AddXY(DateTime.Now, x.High, x.Low, x.Open, x.Close));
}
class OHLC
{
public double? Open;
public double? High;
public double? Low;
public double Close;
}
static OHLC Accumulate(OHLC current, double price)
{
current.Open = current.Open ?? price;
current.High = current.High.HasValue ? current.High > price ? current.High : price : price;
current.Low = current.Low.HasValue ? current.Low < price ? current.Low : price : price;
current.Close = price;
return current;
}
}
}
// Colours
chart1.BackColor = Color.Black;
chart1.ChartAreas[0].Axes[0].LineColor = Color.LimeGreen;
chart1.ChartAreas[0].Axes[0].TitleForeColor = Color.LimeGreen;
chart1.ChartAreas[0].AxisX.MajorTickMark.LineColor = Color.LimeGreen;
chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.LimeGreen;
chart1.ChartAreas[0].Axes[1].LineColor = Color.LimeGreen;
chart1.ChartAreas[0].Axes[1].TitleForeColor = Color.LimeGreen;
chart1.ChartAreas[0].AxisY.MajorTickMark.LineColor = Color.LimeGreen;
chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.LimeGreen;
chart1.ChartAreas[0].BackColor = Color.Black;
series["PriceDownColor"] = "Red";
*)