Skip to content

Commit fa81912

Browse files
Improve implicit conversion failure message
- Improve string to Symbol implicit conversion exceptions message
1 parent 0b3ecea commit fa81912

6 files changed

Lines changed: 172 additions & 3 deletions

Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<Compile Include="PortfolioRebalanceOnSecurityChangesRegressionAlgorithm.cs" />
198198
<Compile Include="ResolutionSwitchingAlgorithm.cs" />
199199
<Compile Include="SetHoldingsFutureRegressionAlgorithm.cs" />
200+
<Compile Include="StringToSymbolImplicitConversionRegressionAlgorithm.cs" />
200201
<Compile Include="TimeRulesDefaultTimeZoneRegressionAlgorithm.cs" />
201202
<Compile Include="SetHoldingsMultipleTargetsRegressionAlgorithm.cs" />
202203
<Compile Include="SetHoldingsMarketOnOpenRegressionAlgorithm.cs" />
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using QuantConnect.Data;
19+
using QuantConnect.Interfaces;
20+
21+
namespace QuantConnect.Algorithm.CSharp
22+
{
23+
/// <summary>
24+
/// Related to GH issue 4275, reproduces a failed string to symbol implicit conversion asserting the exception
25+
/// thrown contains the used ticker
26+
/// </summary>
27+
public class StringToSymbolImplicitConversionRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
28+
{
29+
/// <summary>
30+
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
31+
/// </summary>
32+
public override void Initialize()
33+
{
34+
SetStartDate(2013, 10, 07);
35+
SetEndDate(2013, 10, 08);
36+
37+
AddEquity("SPY", Resolution.Minute);
38+
}
39+
40+
/// <summary>
41+
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
42+
/// </summary>
43+
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
44+
public override void OnData(Slice data)
45+
{
46+
try
47+
{
48+
MarketOrder("PEPE", 1);
49+
}
50+
catch (Exception exception)
51+
{
52+
if (exception.Message.Contains("This asset symbol (PEPE 0) was not found in your security list") && !Portfolio.Invested)
53+
{
54+
SetHoldings("SPY", 1);
55+
}
56+
}
57+
}
58+
59+
/// <summary>
60+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
61+
/// </summary>
62+
public bool CanRunLocally { get; } = true;
63+
64+
/// <summary>
65+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
66+
/// </summary>
67+
public Language[] Languages { get; } = { Language.CSharp, Language.Python };
68+
69+
/// <summary>
70+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
71+
/// </summary>
72+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
73+
{
74+
{"Total Trades", "1"},
75+
{"Average Win", "0%"},
76+
{"Average Loss", "0%"},
77+
{"Compounding Annual Return", "0%"},
78+
{"Drawdown", "0%"},
79+
{"Expectancy", "0"},
80+
{"Net Profit", "0%"},
81+
{"Sharpe Ratio", "0"},
82+
{"Probabilistic Sharpe Ratio", "0%"},
83+
{"Loss Rate", "0%"},
84+
{"Win Rate", "0%"},
85+
{"Profit-Loss Ratio", "0"},
86+
{"Alpha", "0"},
87+
{"Beta", "0"},
88+
{"Annual Standard Deviation", "0"},
89+
{"Annual Variance", "0"},
90+
{"Information Ratio", "0"},
91+
{"Tracking Error", "0"},
92+
{"Treynor Ratio", "0"},
93+
{"Total Fees", "$3.26"},
94+
{"Fitness Score", "0.995"},
95+
{"Kelly Criterion Estimate", "0"},
96+
{"Kelly Criterion Probability Value", "0"},
97+
{"Sortino Ratio", "79228162514264337593543950335"},
98+
{"Return Over Maximum Drawdown", "79228162514264337593543950335"},
99+
{"Portfolio Turnover", "0.995"},
100+
{"Total Insights Generated", "0"},
101+
{"Total Insights Closed", "0"},
102+
{"Total Insights Analysis Completed", "0"},
103+
{"Long Insight Count", "0"},
104+
{"Short Insight Count", "0"},
105+
{"Long/Short Ratio", "100%"},
106+
{"Estimated Monthly Alpha Value", "$0"},
107+
{"Total Accumulated Estimated Alpha Value", "$0"},
108+
{"Mean Population Estimated Insight Value", "$0"},
109+
{"Mean Population Direction", "0%"},
110+
{"Mean Population Magnitude", "0%"},
111+
{"Rolling Averaged Population Direction", "0%"},
112+
{"Rolling Averaged Population Magnitude", "0%"},
113+
{"OrderListHash", "491919591"}
114+
};
115+
}
116+
}

Algorithm.Python/QuantConnect.Algorithm.Python.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Content Include="RegisterIndicatorRegressionAlgorithm.py" />
100100
<Content Include="SectorWeightingFrameworkAlgorithm.py" />
101101
<Content Include="SetHoldingsMultipleTargetsRegressionAlgorithm.py" />
102+
<Content Include="StringToSymbolImplicitConversionRegressionAlgorithm.py" />
102103
<Content Include="TradingEconomicsCalendarIndicatorAlgorithm.py" />
103104
<Content Include="OnEndOfDayRegressionAlgorithm.py" />
104105
<Content Include="SECReportDataAlgorithm.py" />
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from clr import AddReference
15+
AddReference("System")
16+
AddReference("QuantConnect.Algorithm")
17+
AddReference("QuantConnect.Common")
18+
19+
from System import *
20+
from QuantConnect import *
21+
from QuantConnect.Algorithm import *
22+
23+
### <summary>
24+
### Related to GH issue 4275, reproduces a failed string to symbol implicit conversion asserting the exception
25+
### thrown contains the used ticker
26+
### </summary>
27+
class StringToSymbolImplicitConversionRegressionAlgorithm(QCAlgorithm):
28+
def Initialize(self):
29+
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
30+
self.SetStartDate(2013,10, 7)
31+
self.SetEndDate(2013,10, 8)
32+
33+
self.AddEquity("SPY", Resolution.Minute)
34+
35+
def OnData(self, data):
36+
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
37+
38+
Arguments:
39+
data: Slice object keyed by symbol containing the stock data
40+
'''
41+
try:
42+
self.MarketOrder("PEPE", 1)
43+
except Exception as exception:
44+
if "This asset symbol (PEPE 0) was not found in your security list" in str(exception) and not self.Portfolio.Invested:
45+
self.SetHoldings("SPY", 1)

Common/Symbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public static implicit operator Symbol(string ticker)
515515
return new Symbol(sid, sid.Symbol);
516516
}
517517

518-
return Empty;
518+
return new Symbol(new SecurityIdentifier(ticker, 0), ticker);
519519
}
520520

521521
#endregion

Tests/Common/SymbolTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,14 @@ public void ImplicitOperatorsReturnSIDOnFailure()
422422
string stringEurusd = eurusd;
423423
Assert.AreEqual(eurusd.ID.ToString(), stringEurusd);
424424

425-
Symbol notASymbol = "this will not resolve to a proper Symbol instance";
426-
Assert.AreEqual(Symbol.Empty, notASymbol);
425+
Assert.Throws<ArgumentException>(() =>
426+
{
427+
Symbol symbol = "this will not resolve to a proper Symbol instance";
428+
});
429+
430+
Symbol notASymbol = "NotASymbol";
431+
Assert.AreNotEqual(Symbol.Empty, notASymbol);
432+
Assert.IsTrue(notASymbol.ToString().Contains("NotASymbol"));
427433
#pragma warning restore 0618
428434
}
429435

0 commit comments

Comments
 (0)