forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTickDataFilteringAlgorithm.cs
156 lines (141 loc) · 5.39 KB
/
TickDataFilteringAlgorithm.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
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Securities.Interfaces;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Demonstration of filtering tick data so easier to use. Tick data has lots of glitchy, spikey data which should be filtered out before usagee.
/// </summary>
/// <meta name="tag" content="filtering" />
/// <meta name="tag" content="tick data" />
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="ticks event" />
public class TickDataFilteringAlgorithm : QCAlgorithm
{
/// <summary>
/// Initialize the tick filtering example algorithm
/// </summary>
public override void Initialize()
{
SetCash(25000);
SetStartDate(2013, 10, 07);
SetEndDate(2013, 10, 11);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Tick);
//Add our custom data filter.
Securities["SPY"].DataFilter = new ExchangeDataFilter(this);
}
/// <summary>
/// Data arriving here will now be filtered.
/// </summary>
/// <param name="data">Ticks data array</param>
public void OnData(Ticks data)
{
if (!data.ContainsKey("SPY")) return;
var spyTickList = data["SPY"];
//Ticks return a list of ticks this second
foreach (var tick in spyTickList)
{
Log(tick.Exchange);
}
if (!Portfolio.Invested)
{
SetHoldings("SPY", 1);
}
}
}
/// <summary>
/// Exchange filter class
/// </summary>
public class ExchangeDataFilter : ISecurityDataFilter
{
private IAlgorithm _algo;
/// <summary>
/// Save instance of the algorithm namespace
/// </summary>
/// <param name="algo"></param>
public ExchangeDataFilter(IAlgorithm algo)
{
_algo = algo;
}
/// <summary>
/// Global Market Short Codes and their full versions: (used in tick objects)
/// https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Common/Global.cs
/// </summary>
public static class MarketCodesFilter
{
/// US Market Codes
public static Dictionary<string, string> US = new Dictionary<string, string>()
{
{"A", "American Stock Exchange"},
{"B", "Boston Stock Exchange"},
{"C", "National Stock Exchange"},
{"D", "FINRA ADF"},
{"I", "International Securities Exchange"},
{"J", "Direct Edge A"},
{"K", "Direct Edge X"},
{"M", "Chicago Stock Exchange"},
{"N", "New York Stock Exchange"},
{"P", "Nyse Arca Exchange"},
{"Q", "NASDAQ OMX"},
{"T", "NASDAQ OMX"},
{"U", "OTC Bulletin Board"},
{"u", "Over-the-Counter trade in Non-NASDAQ issue"},
{"W", "Chicago Board Options Exchange"},
{"X", "Philadelphia Stock Exchange"},
{"Y", "BATS Y-Exchange, Inc"},
{"Z", "BATS Exchange, Inc"}
};
/// Canada Market Short Codes:
public static Dictionary<string, string> Canada = new Dictionary<string, string>()
{
{"T", "Toronto"},
{"V", "Venture"}
};
/// <summary>
/// Select allowed exchanges for this filter: e.g. top 4
/// </summary>
public static List<string> AllowedExchanges = new List<string>() {
"P", //NYSE ARCA - SPY PRIMARY EXCHANGE
//https://www.google.com/finance?q=NYSEARCA%3ASPY&ei=XcA2VKCSLs228waMhYCIBg
};
}
/// <summary>
/// Filter out a tick from this vehicle, with this new data:
/// </summary>
/// <param name="data">New data packet:</param>
/// <param name="asset">Vehicle of this filter.</param>
public bool Filter(Security asset, BaseData data)
{
// TRUE --> Accept Tick
// FALSE --> Reject Tick
var tick = data as Tick;
// This is a tick bar
if (tick != null)
{
if (tick.Exchange == "P") //MarketCodesFilter.AllowedExchanges.Contains()
{
return true;
}
}
//Only allow those exchanges through.
return false;
}
}
}