-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathProgram.cs
161 lines (139 loc) · 5.61 KB
/
Program.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
// -----------------------------------------------------------------------
// <copyright file="Program.cs" company="">
// Copyright 2013 Alexander Soffronow Pagonidis
// </copyright>
// -----------------------------------------------------------------------
using QDMS;
using QDMSClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace SampleApp
{
class Program
{
static void Main()
{
//create the client, assuming the default port settings
QDMSClient.QDMSClient client = new QDMSClient.QDMSClient(
"SampleClient",
"127.0.0.1",
5556,
5557,
5555,
5559,
"");
//hook up the events needed to receive data & error messages
client.HistoricalDataReceived += client_HistoricalDataReceived;
client.RealTimeDataReceived += client_RealTimeDataReceived;
client.Error += client_Error;
//connect to the server
client.Connect();
//make sure the connection was succesful before we continue
if (!client.Connected)
{
Console.WriteLine("Could not connect.");
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
return;
}
//request the list of available instruments
ApiResponse<List<Instrument>> response = client.GetInstruments().Result; //normally you'd use await here
if (!response.WasSuccessful)
{
Console.WriteLine("Failed to get instrument data:");
foreach (var error in response.Errors)
{
Console.WriteLine(error);
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
return;
}
List<Instrument> instruments = response.Result;
foreach (Instrument i in instruments)
{
Console.WriteLine("Instrument ID {0}: {1} ({2}), Datasource: {3}",
i.ID,
i.Symbol,
i.Type,
i.Datasource.Name);
}
Thread.Sleep(3000);
//then we grab some historical data from Yahoo
//start by finding the SPY instrument
response = client.GetInstruments(x => x.Symbol == "SPY" && x.Datasource.Name == "Yahoo").Result;
var spy = response.Result.FirstOrDefault();
if (spy != null)
{
var req = new HistoricalDataRequest(
spy,
BarSize.OneDay,
new DateTime(2013, 1, 1),
new DateTime(2013, 1, 15),
dataLocation: DataLocation.Both,
saveToLocalStorage: true,
rthOnly: true);
client.RequestHistoricalData(req);
Thread.Sleep(3000);
//now that we downloaded the data, let's make a request to see what is stored locally
var storageInfo = client.GetLocallyAvailableDataInfo(spy).Result;
if (storageInfo.WasSuccessful)
{
foreach (var s in storageInfo.Result)
{
Console.WriteLine("Freq: {0} - From {1} to {2}", s.Frequency, s.EarliestDate, s.LatestDate);
}
}
Thread.Sleep(3000);
//grab some dividends data
var divReq = new DividendRequest(DateTime.Now.AddDays(-365), DateTime.Now, symbol: "AAPL", dataLocation: DataLocation.ExternalOnly);
var dividendResult = client.GetDividends(divReq).Result;
if (dividendResult.WasSuccessful)
{
foreach (var dividend in dividendResult.Result)
{
Console.WriteLine($"${dividend.Amount} payable on {dividend.PaymentDate}");
}
}
//finally send a real time data request (from the simulated data datasource)
spy.Datasource.Name = "SIM";
var rtReq = new RealTimeDataRequest(spy, BarSize.OneSecond);
client.RequestRealTimeData(rtReq);
Thread.Sleep(3000);
//And then cancel the real time data stream
client.CancelRealTimeData(spy, BarSize.OneSecond);
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
client.Disconnect();
client.Dispose();
}
static void client_Error(object sender, ErrorArgs e)
{
Console.WriteLine("Error {0}: {1}", e.ErrorCode, e.ErrorMessage);
}
static void client_RealTimeDataReceived(object sender, RealTimeDataEventArgs e)
{
Console.WriteLine("Real Time Data Received: O: {0} H: {1} L: {2} C: {3}",
e.Open,
e.High,
e.Low,
e.Close);
}
static void client_HistoricalDataReceived(object sender, HistoricalDataEventArgs e)
{
Console.WriteLine("Historical data received:");
foreach (OHLCBar bar in e.Data)
{
Console.WriteLine("{0} - O: {1} H: {2} L: {3} C: {4}",
bar.DT,
bar.Open,
bar.High,
bar.Low,
bar.Close);
}
}
}
}