-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataLoader.cs
213 lines (177 loc) · 6.81 KB
/
DataLoader.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows;
using StockSharp.Algo.Candles;
using StockSharp.Algo.Storages;
using System.Collections.ObjectModel;
using StockSharp.Localization;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace SDataExt
{
public class DataLoader
{
public string Code { get; set; }
public TimeSpan TimeFrame { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public string DType { get; set; }
public string Path { get; set; }
public double[] dlDates { get; set; }
public double[,] dlInfo { get; set; }
public string[] SecArray{ get; set; }
public List<string> SecList { get; set; }
private StorageRegistry str { get; set; }
private LocalMarketDataDrive lmd { get; set; }
private class SecCatalogEntry
{
public string SecCode { get; set; }
public string SecPath { get; set; }
}
public DataLoader(string _Code, TimeSpan _TimeFrame, DateTime _From, DateTime _To, string _DType, string _Path)
{
Code = _Code;
TimeFrame = _TimeFrame;
From = _From;
To = _To;
DType = _DType;
Path = _Path != null ? _Path : @"D:\SH\Hydra";
if (dlDates==null) InitDl();
}
public DataLoader()
{
Path = @"D:\SH\Hydra";
InitDl();
}
public void InitDl()
{
dlDates = new double[0];
dlInfo = new double[0,0];
SecList = new List<string>();
SecArray = new string[0];
//SStor = new SimStorage();
str = new StorageRegistry();
lmd = new LocalMarketDataDrive();
lmd.Path = Path;
str.DefaultDrive = lmd; //
}
public double GetList()
{
double result = 0; // Неудачный результат
int i = 0;
var DList = Directory.GetDirectories(Path, "*",SearchOption.AllDirectories).Where(ds=>ds.Contains("@") && !ds.Contains("20") && !ds.Contains("Temp"));
foreach (string secDir in DList)
{
string secName = secDir.Split("\\".ToCharArray()).FirstOrDefault(f => f.Contains("@"));
if (secName == null) secName = secDir;
SecList.Add(secName);
i = i +1;
}
SecArray = SecList.ToArray();
if (SecArray.Length>1) result = 1; // Yдачный результат
return result;
}
public double GetData()
{
double[] dlClose;
double[] dlLow;
double[] dlHigh;
double[] dlOpen;
double[] dlVol;
if (Code == null || TimeFrame == null || From == null || To == null || DType == null || Path == null)
MessageBox.Show("Not all parameters are specified!");
var candles = str.GetCandleStorage(typeof (TimeFrameCandle), CreateSecurity(Code), TimeFrame).Load(From, To);
double result = 0; // Неудачный результат
if (candles.Any() == false)
{ result = 0;// Неудачный результат
}
else
{
result = 1;// Удачный результат
dlDates = candles.Select(c => (double)c.CloseTime.Ticks / 10000000 / 3600 / 24 + 367).ToArray();
switch (DType)
{
case "A":
case "ALL":
dlClose = candles.Select(c => (double)c.ClosePrice).ToArray();
dlOpen = candles.Select(c => (double)c.OpenPrice).ToArray();
dlLow = candles.Select(c => (double)c.LowPrice).ToArray();
dlHigh = candles.Select(c => (double)c.HighPrice).ToArray();
dlVol = candles.Select(c => (double)c.TotalVolume).ToArray();
dlInfo = ConcatCW(dlOpen,dlHigh,dlLow,dlClose,dlVol);
break;
case "C":
case "CLOSE":
dlClose = candles.Select(c => (double)c.ClosePrice).ToArray();
dlInfo = ConcatCW(dlClose);
break;
case "O":
case "OPEN":
dlOpen = candles.Select(c => (double)c.OpenPrice).ToArray();
dlInfo = ConcatCW(dlOpen);
break;
case "V":
case "VOL":
dlVol = candles.Select(c => (double)c.TotalVolume).ToArray();
dlInfo = ConcatCW(dlVol);
break;
default:
dlClose = candles.Select(c => (double)c.ClosePrice).ToArray();
dlInfo = ConcatCW(dlClose);
break;
}
}
return result;
}
public Security CreateSecurity(string secStr)
{
Security security;
int strLn = secStr.IndexOfAny("@".ToCharArray());
string secBorad = secStr.Substring(strLn + 1);
ExchangeBoard eb = new ExchangeBoard();
SecurityTypes st = new SecurityTypes();
switch (secBorad)
{
case "FORTS":
eb = ExchangeBoard.Forts;
st = SecurityTypes.Future;
break;
case "EQBR":
eb = ExchangeBoard.MicexEqbr;
st = SecurityTypes.Stock;
break;
default:
eb = ExchangeBoard.Micex;
st = SecurityTypes.Future;
break;
}
string secCode = secStr.Substring(0, strLn);
security = new Security()
{
Id = secStr,
Code = secCode,
Board = eb,
Type = st
};
return security;
}
public static T[,] ConcatCW<T>(params T[][] arrays)
{
int length = arrays.Max(ar=>ar.Length);
int n = arrays.Count();
T[,] result = new T[n,length];
int ind = 0;
foreach (T[] array in arrays)
{
T[,] source = new T[1,length];
for (int i = 0; i < length; i++)
source.SetValue(array[i], 0,i);
Array.Copy(source, 0, result, ind, length);
ind += length;
}
return result;
}
}
}