Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
167 lines (152 loc) · 6.51 KB

File metadata and controls

167 lines (152 loc) · 6.51 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* 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;
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Orders;
using QuantConnect.Interfaces;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Demonstration of using the Delisting event in your algorithm. Assets are delisted on their last day of trading, or when their contract expires.
/// This data is not included in the open source project.
/// </summary>
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="data event handlers" />
/// <meta name="tag" content="delisting event" />
public class DelistingEventsAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private bool _receivedDelistedWarningEvent;
private bool _receivedDelistedEvent;
private int _dataCount;
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2007, 05, 16); //Set Start Date
SetEndDate(2007, 05, 25); //Set End Date
SetCash(100000); //Set Strategy Cash
// Find more symbols here: http://quantconnect.com/data
AddSecurity(SecurityType.Equity, "AAA", Resolution.Daily);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily);
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice data)
{
_dataCount += data.Bars.Count;
if (Transactions.OrdersCount == 0)
{
SetHoldings("AAA", 1);
Debug("Purchased Stock");
}
foreach (var kvp in data.Bars)
{
var symbol = kvp.Key;
var tradeBar = kvp.Value;
Debug($"OnData(Slice): {Time}: {symbol}: {tradeBar.Close.ToStringInvariant("0.00")}");
}
// the slice can also contain delisting data: data.Delistings in a dictionary string->Delisting
var aaa = Securities["AAA"];
if (aaa.IsDelisted && aaa.IsTradable)
{
throw new Exception("Delisted security must NOT be tradable");
}
if (!aaa.IsDelisted && !aaa.IsTradable)
{
throw new Exception("Securities must be marked as tradable until they're delisted or removed from the universe");
}
}
public void OnData(Delistings data)
{
foreach (var kvp in data)
{
var symbol = kvp.Key;
var delisting = kvp.Value;
if (delisting.Type == DelistingType.Warning)
{
_receivedDelistedWarningEvent = true;
Debug($"OnData(Delistings): {Time}: {symbol} will be delisted at end of day today.");
// liquidate on delisting warning
SetHoldings(symbol, 0);
}
if (delisting.Type == DelistingType.Delisted)
{
_receivedDelistedEvent = true;
Debug($"OnData(Delistings): {Time}: {symbol} has been delisted.");
// fails because the security has already been delisted and is no longer tradable
SetHoldings(symbol, 1);
}
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
Debug($"OnOrderEvent(OrderEvent): {Time}: {orderEvent}");
}
public override void OnEndOfAlgorithm()
{
if (!_receivedDelistedEvent)
{
throw new Exception("Did not receive expected delisted event");
}
if (!_receivedDelistedWarningEvent)
{
throw new Exception("Did not receive expected delisted warning event");
}
if (_dataCount != 13)
{
throw new Exception($"Unexpected data count {_dataCount}. Expected 13");
}
}
/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;
/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public Language[] Languages { get; } = { Language.CSharp, Language.Python };
/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Trades", "2"},
{"Average Win", "0%"},
{"Average Loss", "-5.58%"},
{"Compounding Annual Return", "-87.694%"},
{"Drawdown", "5.600%"},
{"Expectancy", "-1"},
{"Net Profit", "-5.578%"},
{"Sharpe Ratio", "-10.227"},
{"Loss Rate", "100%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "-1.297"},
{"Beta", "-1.062"},
{"Annual Standard Deviation", "0.156"},
{"Annual Variance", "0.024"},
{"Information Ratio", "-8.704"},
{"Tracking Error", "0.215"},
{"Treynor Ratio", "1.499"},
{"Total Fees", "$36.70"}
};
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.