Skip to content

Commit ec64d98

Browse files
Add python syntax check
1 parent 57adac5 commit ec64d98

File tree

139 files changed

+616
-534
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+616
-534
lines changed

.github/workflows/syntax-tests.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Syntax Tests
2+
3+
on:
4+
push:
5+
branches: ['*']
6+
tags: ['*']
7+
pull_request:
8+
branches: [master]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-24.04
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- name: Liberate disk space
18+
uses: jlumbroso/free-disk-space@main
19+
with:
20+
tool-cache: true
21+
large-packages: false
22+
docker-images: false
23+
swap-storage: false
24+
- name: Run Syntax Test
25+
uses: addnab/docker-run-action@v3
26+
with:
27+
image: quantconnect/lean:foundation
28+
options: --workdir /__w/Lean/Lean -v /home/runner/work:/__w
29+
shell: bash
30+
run: |
31+
pip install --no-cache-dir quantconnect-stubs && \
32+
python run_syntax_check.py

Algorithm.Python/AlgorithmModeAndDeploymentTargetAlgorithm.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ def initialize(self):
2626
self.debug(f"Algorithm Mode: {self.algorithm_mode}. Is Live Mode: {self.live_mode}. Deployment Target: {self.deployment_target}.")
2727

2828
if self.algorithm_mode != AlgorithmMode.BACKTESTING:
29-
raise Exception(f"Algorithm mode is not backtesting. Actual: {self.algorithm_mode}")
29+
raise AssertionError(f"Algorithm mode is not backtesting. Actual: {self.algorithm_mode}")
3030

3131
if self.live_mode:
32-
raise Exception("Algorithm should not be live")
32+
raise AssertionError("Algorithm should not be live")
3333

3434
if self.deployment_target != DeploymentTarget.LOCAL_PLATFORM:
35-
raise Exception(f"Algorithm deployment target is not local. Actual{self.deployment_target}")
35+
raise AssertionError(f"Algorithm deployment target is not local. Actual{self.deployment_target}")
3636

3737
# For a live deployment these checks should pass:
38-
# if self.algorithm_mode != AlgorithmMode.LIVE: raise Exception("Algorithm mode is not live")
39-
# if not self.live_mode: raise Exception("Algorithm should be live")
38+
# if self.algorithm_mode != AlgorithmMode.LIVE: raise AssertionError("Algorithm mode is not live")
39+
# if not self.live_mode: raise AssertionError("Algorithm should be live")
4040

4141
# For a cloud deployment these checks should pass:
42-
# if self.deployment_target != DeploymentTarget.CLOUD_PLATFORM: raise Exception("Algorithm deployment target is not cloud")
42+
# if self.deployment_target != DeploymentTarget.CLOUD_PLATFORM: raise AssertionError("Algorithm deployment target is not cloud")
4343

4444
self.quit()

Algorithm.Python/AllShortableSymbolsCoarseSelectionRegressionAlgorithm.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def on_data(self, data):
5959
continue
6060
shortable_quantity = security.shortable_provider.shortable_quantity(symbol, self.time)
6161
if not shortable_quantity:
62-
raise Exception(f"Expected {symbol} to be shortable on {self.time.strftime('%Y%m%d')}")
62+
raise AssertionError(f"Expected {symbol} to be shortable on {self.time.strftime('%Y%m%d')}")
6363

6464
"""
6565
Buy at least once into all Symbols. Since daily data will always use
@@ -76,22 +76,22 @@ def coarse_selection(self, coarse):
7676
if self.time.date() == self._20140327.date():
7777
gme = Symbol.create("GME", SecurityType.EQUITY, Market.USA)
7878
if gme not in shortable_symbols.keys():
79-
raise Exception("Expected unmapped GME in shortable symbols list on 2014-03-27")
79+
raise AssertionError("Expected unmapped GME in shortable symbols list on 2014-03-27")
8080
if "GME" not in list(map(lambda x: x.symbol.value, coarse)):
81-
raise Exception("Expected mapped GME in coarse symbols on 2014-03-27")
81+
raise AssertionError("Expected mapped GME in coarse symbols on 2014-03-27")
8282

8383
expected_missing = 1
8484

8585
missing = list(filter(lambda x: x not in selected_symbols, self.expected_symbols[self.time]))
8686
if len(missing) != expected_missing:
87-
raise Exception(f"Expected Symbols selected on {self.time.strftime('%Y%m%d')} to match expected Symbols, but the following Symbols were missing: {', '.join(list(map(lambda x:x.value, missing)))}")
87+
raise AssertionError(f"Expected Symbols selected on {self.time.strftime('%Y%m%d')} to match expected Symbols, but the following Symbols were missing: {', '.join(list(map(lambda x:x.value, missing)))}")
8888

8989
self.coarse_selected[self.time] = True
9090
return selected_symbols
9191

9292
def on_end_of_algorithm(self):
9393
if not all(x for x in self.coarse_selected.values()):
94-
raise Exception(f"Expected coarse selection on all dates, but didn't run on: {', '.join(list(map(lambda x: x.key.strftime('%Y%m%d'), filter(lambda x:not x.value, self.coarse_selected))))}")
94+
raise AssertionError(f"Expected coarse selection on all dates, but didn't run on: {', '.join(list(map(lambda x: x.key.strftime('%Y%m%d'), filter(lambda x:not x.value, self.coarse_selected))))}")
9595

9696
class AllShortableSymbolsRegressionAlgorithmBrokerageModel(DefaultBrokerageModel):
9797
def __init__(self, shortable_provider):

Algorithm.Python/Alphas/ShareClassMeanReversionAlpha.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, *args, **kwargs):
6767
self.alpha = None
6868
self.beta = None
6969
if 'tickers' not in kwargs:
70-
raise Exception('ShareClassMeanReversionAlphaModel: Missing argument: "tickers"')
70+
raise AssertionError('ShareClassMeanReversionAlphaModel: Missing argument: "tickers"')
7171
self.tickers = kwargs['tickers']
7272
self.position_value = None
7373
self.invested = False

Algorithm.Python/BaseFrameworkRegressionAlgorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ def on_end_of_algorithm(self):
4646
# The base implementation checks for active insights
4747
insights_count = len(self.insights.get_insights(lambda insight: insight.is_active(self.utc_time)))
4848
if insights_count != 0:
49-
raise Exception(f"The number of active insights should be 0. Actual: {insights_count}")
49+
raise AssertionError(f"The number of active insights should be 0. Actual: {insights_count}")

Algorithm.Python/BasicTemplateCryptoFutureAlgorithm.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def on_data(self, slice):
6060
self.interest_per_symbol[interest_rate.key] += 1
6161
self.cached_interest_rate = self.securities[interest_rate.key].cache.get_data[MarginInterestRate]()
6262
if self.cached_interest_rate != interest_rate.value:
63-
raise Exception(f"Unexpected cached margin interest rate for {interest_rate.key}!")
63+
raise AssertionError(f"Unexpected cached margin interest rate for {interest_rate.key}!")
6464

6565
if self.fast > self.slow:
6666
if self.portfolio.invested == False and self.transactions.orders_count == 0:
6767
self.ticket = self.buy(self.btc_usd.symbol, 50)
6868
if self.ticket.status != OrderStatus.INVALID:
69-
raise Exception(f"Unexpected valid order {self.ticket}, should fail due to margin not sufficient")
69+
raise AssertionError(f"Unexpected valid order {self.ticket}, should fail due to margin not sufficient")
7070

7171
self.buy(self.btc_usd.symbol, 1)
7272

@@ -76,14 +76,14 @@ def on_data(self, slice):
7676
self.holdings_value_btc_usd = 100
7777

7878
if abs(self.btc_usd_holdings.total_sale_volume - self.holdings_value_btc_usd) > 1:
79-
raise Exception(f"Unexpected TotalSaleVolume {self.btc_usd_holdings.total_sale_volume}")
79+
raise AssertionError(f"Unexpected TotalSaleVolume {self.btc_usd_holdings.total_sale_volume}")
8080

8181
if abs(self.btc_usd_holdings.absolute_holdings_cost - self.holdings_value_btc_usd) > 1:
82-
raise Exception(f"Unexpected holdings cost {self.btc_usd_holdings.holdings_cost}")
82+
raise AssertionError(f"Unexpected holdings cost {self.btc_usd_holdings.holdings_cost}")
8383

8484
# margin used is based on the maintenance rate
8585
if (abs(self.btc_usd_holdings.absolute_holdings_cost * 0.05 - self.margin_used) > 1) or (BuyingPowerModelExtensions.get_maintenance_margin(self.btc_usd.buying_power_model, self.btc_usd) != self.margin_used):
86-
raise Exception(f"Unexpected margin used {self.margin_used}")
86+
raise AssertionError(f"Unexpected margin used {self.margin_used}")
8787

8888
self.buy(self.ada_usdt.symbol, 1000)
8989

@@ -94,29 +94,29 @@ def on_data(self, slice):
9494
self.holdings_value_usdt = self.ada_usdt.price * self.ada_usdt.symbol_properties.contract_multiplier * 1000
9595

9696
if abs(self.ada_usdt_holdings.total_sale_volume - self.holdings_value_usdt) > 1:
97-
raise Exception(f"Unexpected TotalSaleVolume {self.ada_usdt_holdings.total_sale_volume}")
97+
raise AssertionError(f"Unexpected TotalSaleVolume {self.ada_usdt_holdings.total_sale_volume}")
9898

9999
if abs(self.ada_usdt_holdings.absolute_holdings_cost - self.holdings_value_usdt) > 1:
100-
raise Exception(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
100+
raise AssertionError(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
101101

102102
if (abs(self.ada_usdt_holdings.absolute_holdings_cost * 0.05 - self.margin_used) > 1) or (BuyingPowerModelExtensions.get_maintenance_margin(self.ada_usdt.buying_power_model, self.ada_usdt) != self.margin_used):
103-
raise Exception(f"Unexpected margin used {self.margin_used}")
103+
raise AssertionError(f"Unexpected margin used {self.margin_used}")
104104

105105
# position just opened should be just spread here
106106
self.profit = self.portfolio.total_unrealized_profit
107107

108108
if (5 - abs(self.profit)) < 0:
109-
raise Exception(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
109+
raise AssertionError(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
110110

111111
if (self.portfolio.total_profit != 0):
112-
raise Exception(f"Unexpected TotalProfit {self.portfolio.total_profit}")
112+
raise AssertionError(f"Unexpected TotalProfit {self.portfolio.total_profit}")
113113

114114
else:
115115
if self.time.hour > 10 and self.transactions.orders_count == 3:
116116
self.sell(self.btc_usd.symbol, 3)
117117
self.btc_usd_holdings = self.btc_usd.holdings
118118
if abs(self.btc_usd_holdings.absolute_holdings_cost - 100 * 2) > 1:
119-
raise Exception(f"Unexpected holdings cost {self.btc_usd_holdings.holdings_cost}")
119+
raise AssertionError(f"Unexpected holdings cost {self.btc_usd_holdings.holdings_cost}")
120120

121121
self.sell(self.ada_usdt.symbol, 3000)
122122
ada_usdt_holdings = self.ada_usdt.holdings
@@ -125,22 +125,22 @@ def on_data(self, slice):
125125
holdings_value_usdt = self.ada_usdt.price * self.ada_usdt.symbol_properties.contract_multiplier * 2000
126126

127127
if abs(ada_usdt_holdings.absolute_holdings_cost - holdings_value_usdt) > 1:
128-
raise Exception(f"Unexpected holdings cost {ada_usdt_holdings.holdings_cost}")
128+
raise AssertionError(f"Unexpected holdings cost {ada_usdt_holdings.holdings_cost}")
129129

130130
# position just opened should be just spread here
131131
profit = self.portfolio.total_unrealized_profit
132132
if (5 - abs(profit)) < 0:
133-
raise Exception(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
133+
raise AssertionError(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
134134
# we barely did any difference on the previous trade
135135
if (5 - abs(self.portfolio.total_profit)) < 0:
136-
raise Exception(f"Unexpected TotalProfit {self.portfolio.total_profit}")
136+
raise AssertionError(f"Unexpected TotalProfit {self.portfolio.total_profit}")
137137

138138

139139
def on_end_of_algorithm(self):
140140
if self.interest_per_symbol[self.ada_usdt.symbol] != 1:
141-
raise Exception(f"Unexpected interest rate count {self.interest_per_symbol[self.ada_usdt.symbol]}")
141+
raise AssertionError(f"Unexpected interest rate count {self.interest_per_symbol[self.ada_usdt.symbol]}")
142142
if self.interest_per_symbol[self.btc_usd.symbol] != 3:
143-
raise Exception(f"Unexpected interest rate count {self.interest_per_symbol[self.btc_usd.symbol]}")
143+
raise AssertionError(f"Unexpected interest rate count {self.interest_per_symbol[self.btc_usd.symbol]}")
144144

145145
def on_order_event(self, order_event):
146146
self.debug("{0} {1}".format(self.time, order_event))

Algorithm.Python/BasicTemplateCryptoFutureHourlyAlgorithm.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def on_data(self, slice):
5858
self.interest_per_symbol[interest_rate.key] += 1
5959
self.cached_interest_rate = self.securities[interest_rate.key].cache.get_data[MarginInterestRate]()
6060
if self.cached_interest_rate != interest_rate.value:
61-
raise Exception(f"Unexpected cached margin interest rate for {interest_rate.key}!")
61+
raise AssertionError(f"Unexpected cached margin interest rate for {interest_rate.key}!")
6262

6363
if self.fast > self.slow:
6464
if self.portfolio.invested == False and self.transactions.orders_count == 0:
6565
self.ticket = self.buy(self.ada_usdt.symbol, 100000)
6666
if self.ticket.status != OrderStatus.INVALID:
67-
raise Exception(f"Unexpected valid order {self.ticket}, should fail due to margin not sufficient")
67+
raise AssertionError(f"Unexpected valid order {self.ticket}, should fail due to margin not sufficient")
6868

6969
self.buy(self.ada_usdt.symbol, 1000)
7070

@@ -76,22 +76,22 @@ def on_data(self, slice):
7676
self.holdings_value_usdt = self.ada_usdt.price * self.ada_usdt.symbol_properties.contract_multiplier * 1000
7777

7878
if abs(self.ada_usdt_holdings.total_sale_volume - self.holdings_value_usdt) > 1:
79-
raise Exception(f"Unexpected TotalSaleVolume {self.ada_usdt_holdings.total_sale_volume}")
79+
raise AssertionError(f"Unexpected TotalSaleVolume {self.ada_usdt_holdings.total_sale_volume}")
8080

8181
if abs(self.ada_usdt_holdings.absolute_holdings_cost - self.holdings_value_usdt) > 1:
82-
raise Exception(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
82+
raise AssertionError(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
8383

8484
if (abs(self.ada_usdt_holdings.absolute_holdings_cost * 0.05 - self.margin_used) > 1) or (BuyingPowerModelExtensions.get_maintenance_margin(self.ada_usdt.buying_power_model, self.ada_usdt) != self.margin_used):
85-
raise Exception(f"Unexpected margin used {self.margin_used}")
85+
raise AssertionError(f"Unexpected margin used {self.margin_used}")
8686

8787
# position just opened should be just spread here
8888
self.profit = self.portfolio.total_unrealized_profit
8989

9090
if (5 - abs(self.profit)) < 0:
91-
raise Exception(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
91+
raise AssertionError(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
9292

9393
if (self.portfolio.total_profit != 0):
94-
raise Exception(f"Unexpected TotalProfit {self.portfolio.total_profit}")
94+
raise AssertionError(f"Unexpected TotalProfit {self.portfolio.total_profit}")
9595

9696
else:
9797
# let's revert our position and double
@@ -104,23 +104,23 @@ def on_data(self, slice):
104104
self.holdings_value_usdt = self.ada_usdt.price * self.ada_usdt.symbol_properties.contract_multiplier * 2000
105105

106106
if abs(self.ada_usdt_holdings.absolute_holdings_cost - self.holdings_value_usdt) > 1:
107-
raise Exception(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
107+
raise AssertionError(f"Unexpected holdings cost {self.ada_usdt_holdings.holdings_cost}")
108108

109109
# position just opened should be just spread here
110110
self.profit = self.portfolio.total_unrealized_profit
111111
if (5 - abs(self.profit)) < 0:
112-
raise Exception(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
112+
raise AssertionError(f"Unexpected TotalUnrealizedProfit {self.portfolio.total_unrealized_profit}")
113113

114114
# we barely did any difference on the previous trade
115115
if (5 - abs(self.portfolio.total_profit)) < 0:
116-
raise Exception(f"Unexpected TotalProfit {self.portfolio.total_profit}")
116+
raise AssertionError(f"Unexpected TotalProfit {self.portfolio.total_profit}")
117117

118118
if self.time.hour >= 22 and self.transactions.orders_count == 3:
119119
self.liquidate()
120120

121121
def on_end_of_algorithm(self):
122122
if self.interest_per_symbol[self.ada_usdt.symbol] != 1:
123-
raise Exception(f"Unexpected interest rate count {self.interest_per_symbol[self.ada_usdt.symbol]}")
123+
raise AssertionError(f"Unexpected interest rate count {self.interest_per_symbol[self.ada_usdt.symbol]}")
124124

125125
def on_order_event(self, order_event):
126126
self.debug("{0} {1}".format(self.time, order_event))

0 commit comments

Comments
 (0)