Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (81 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
96 lines (81 loc) · 4.01 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using CsvHelper.Configuration;
using FinanceSharp;
using FinanceSharp.Consolidators;
using FinanceSharp.Examples.Data;
using FinanceSharp.Indicators;
using Ionic.Zip;
namespace FinanceSharp.Examples {
class Program {
[STAThread]
static void Main(string[] args) {
//load data
(long Time, DoubleArray Value)[] ticks;
using (var zip = new Ionic.Zip.ZipFile("./Data/BTCUSDT.2018-10-04.csv.zip"))
using (var zipfile = zip.Entries.First().OpenReader())
using (var reader = new StreamReader(zipfile)) {
var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) {
PrepareHeaderForMatch = args => char.ToUpperInvariant(args.Header[0]) + args.Header.Substring(1),
MissingFieldFound = null,
};
using (var csv = new CsvReader(reader, csvConfig)) {
ticks = csv.GetRecords<BinanceTick>()
.Select(tick => (
Time: (long) (tick.time * 1000d), //we add 3 zeros to turn it from seconds to milliseconds.
Value: DoubleArray.From(new TickValue(tick.Price, tick.Price, 0, tick.Price, tick.Quantity, 0))))
.ToArray();
}
}
//build model
(IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) model = EMAx3();
//(IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) model = HeikinAshi();
//(IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) model = EMA();
//feed the model data
for (var i = 0; i < ticks.Length; i++) {
var tick = ticks[i];
model.Input.Update(tick.Time, tick.Value);
}
//plot
var x = Enumerable.Range(0, model.Datas.Min(l => l.Count)).Select(i => (double) i).ToArray();
var y = model.Datas.Select(m => m.Take(x.Length).ToArray()).ToArray();
Plotter.Show(
x, y
);
}
public static (IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) EMA() {
var tickCons = new TickConsolidator(TimeSpan.FromMinutes(1d));
var ema = new ExponentialMovingAverage(12).Of(tickCons);
var bars = tickCons.ThenToList();
var emas = ema.ThenToList();
return (tickCons, new IUpdatable[] {ema}, new List<DoubleArray>[] {bars, emas});
}
public static (IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) EMAx3() {
var tickCons = new TickConsolidator(TimeSpan.FromMinutes(1d));
var ema6 = tickCons.Then(new ExponentialMovingAverage(6));
var ema12 = tickCons.Then(new ExponentialMovingAverage(12));
var ema24 = tickCons.Then(new ExponentialMovingAverage(24));
var bars = tickCons.ThenToList();
var emas6 = ema6.ThenToList();
var emas12 = ema12.ThenToList();
var emas24 = ema24.ThenToList();
return (tickCons, new IUpdatable[] {ema6, ema12, ema24}, new List<DoubleArray>[] {bars, emas6, emas12, emas24});
}
public static (IUpdatable Input, IUpdatable[] Outputs, List<DoubleArray>[] Datas) HeikinAshi() {
var tickCons = new TickConsolidator(TimeSpan.FromMinutes(1d));
var ha = new HeikinAshi().Of(tickCons);
var bars = tickCons.ThenToList();
var has = new List<DoubleArray>();
//bind model manually
ha.Updated += (time, updated) => has.Add(DoubleArray.From(ha.CurrentBar) + 25d); //note that we append +25 offset so ha will be visible
return (tickCons, new IUpdatable[] {ha}, new List<DoubleArray>[] {bars, has});
}
}
}