Fix bug/syntax in python examples (#8658)
* CustomDataRegressionAlgorithm * DescendingCustomDataObjectStoreRegressionAlgorithm * CustomDataPropertiesRegressionAlgorithm * DateTime -> should be datetime * KerasNeuralNetworkAlgorithm * OptionIndicatorsMirrorContractsRegressionAlgorithm * BybitCustomDataCryptoRegressionAlgorithm * DropboxBaseDataUniverseSelectionAlgorithm * UserDefinedUniverseAlgorithm * CompleteOrderTagUpdateAlgorithm * BasicTemplateOptionEquityStrategyAlgorithm hint * ETFConstituentUniverseFrameworkRegressionAlgorithm * FutureStopMarketOrderOnExtendedHoursRegressionAlgorithm * SecurityDynamicPropertyPythonClassAlgorithm * hint * hinting * CallbackCommandRegressionAlgorithm * CustomWarmUpPeriodIndicatorAlgorithm * CrunchDAOSignalExportDemonstrationAlgorithm * ExpiryHelperAlphaModelFrameworkAlgorithm * ClassicRenkoConsolidatorAlgorithm * SmaCrossUniverseSelectionAlgorithm * PEP8 Fix: Assigning to a Method * SliceGetByTypeRegressionAlgorithm * MarketOnCloseOrderBufferExtendedMarketHoursRegressionAlgorithm * MarketOnCloseOrderBufferRegressionAlgorithm * CustomIndicatorAlgorithm * ScheduledQueuingAlgorithm * ComboOrdersFillModelAlgorithm * CustomIndicatorWithExtensionAlgorithm * IndicatorWithRenkoBarsRegressionAlgorithm * CoarseFineOptionUniverseChainRegressionAlgorithm * NumeraiSignalExportDemonstrationAlgorithm * DropboxUniverseSelectionAlgorithm * WeeklyUniverseSelectionRegressionAlgorithm * AutoRegressiveIntegratedMovingAverageRegressionAlgorithm * DropboxBaseDataUniverseSelectionAlgorithm * IronCondorStrategyAlgorithm * LongAndShortButterflyPutStrategiesAlgorithm * FutureStopMarketOrderOnExtendedHoursRegressionAlgorithm * LongAndShortCallCalendarSpreadStrategiesAlgorithm * KerasNeuralNetworkAlgorithm * LongAndShortPutCalendarSpreadStrategiesAlgorithm * OptionPriceModelForOptionStylesBaseRegressionAlgorithm * TensorFlowNeuralNetworkAlgorithm * MarketOnCloseOrderBufferRegressionAlgorithm * MarketOnCloseOrderBufferExtendedMarketHoursRegressionAlgorithm * typing * ComboOrderTicketDemoAlgorithm * PytorchNeuralNetworkAlgorithm * MultipleSymbolConsolidationAlgorithm * fixes * revert getattr mypy syntax * address peer review * Addresses Peer-Review --------- Co-authored-by: Alexandre Catarino <AlexCatarino@users.noreply.github.com>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
from AlgorithmImports import *
|
||||
|
||||
### <summary>
|
||||
@@ -23,8 +24,7 @@ from AlgorithmImports import *
|
||||
### <meta name="tag" content="regression test" />
|
||||
class CustomDataRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
def initialize(self):
|
||||
|
||||
def initialize(self) -> None:
|
||||
self.set_start_date(2020,1,5) # Set Start Date
|
||||
self.set_end_date(2020,1,10) # Set End Date
|
||||
self.set_cash(100000) # Set Strategy Cash
|
||||
@@ -36,12 +36,12 @@ class CustomDataRegressionAlgorithm(QCAlgorithm):
|
||||
self.set_security_initializer(lambda x: seeder.seed_security(x))
|
||||
self._warmed_up_checked = False
|
||||
|
||||
def on_data(self, data):
|
||||
def on_data(self, data: Slice) -> None:
|
||||
if not self.portfolio.invested:
|
||||
if data['BTC'].close != 0 :
|
||||
self.order('BTC', self.portfolio.margin_remaining/abs(data['BTC'].close + 1))
|
||||
|
||||
def on_securities_changed(self, changes):
|
||||
def on_securities_changed(self, changes: SecurityChanges) -> None:
|
||||
changes.filter_custom_securities = False
|
||||
for added_security in changes.added_securities:
|
||||
if added_security.symbol.value == "BTC":
|
||||
@@ -49,25 +49,24 @@ class CustomDataRegressionAlgorithm(QCAlgorithm):
|
||||
if not added_security.has_data:
|
||||
raise ValueError(f"Security {added_security.symbol} was not warmed up!")
|
||||
|
||||
def on_end_of_algorithm(self):
|
||||
def on_end_of_algorithm(self) -> None:
|
||||
if not self._warmed_up_checked:
|
||||
raise ValueError("Security was not warmed up!")
|
||||
|
||||
class Bitcoin(PythonData):
|
||||
'''Custom Data Type: Bitcoin data from Quandl - https://data.nasdaq.com/databases/BCHAIN'''
|
||||
|
||||
def get_source(self, config, date, is_live_mode):
|
||||
def get_source(self, config: SubscriptionDataConfig, date: datetime, is_live_mode: bool) -> SubscriptionDataSource:
|
||||
if is_live_mode:
|
||||
return SubscriptionDataSource("https://www.bitstamp.net/api/ticker/", SubscriptionTransportMedium.REST)
|
||||
|
||||
#return "http://my-ftp-server.com/futures-data-" + date.to_string("Ymd") + ".zip"
|
||||
# OR simply return a fixed small data file. Large files will slow down your backtest
|
||||
subscription = SubscriptionDataSource("https://www.quantconnect.com/api/v2/proxy/nasdaq/api/v3/datatables/QDL/BITFINEX.csv?code=BTCUSD&api_key=WyAazVXnq7ATy_fefTqm")
|
||||
subscription.Sort = True
|
||||
subscription.sort = True
|
||||
return subscription
|
||||
|
||||
|
||||
def reader(self, config, line, date, is_live_mode):
|
||||
def reader(self, config: SubscriptionDataConfig, line: str, date: datetime, is_live_mode: bool) -> DynamicData:
|
||||
coin = Bitcoin()
|
||||
coin.symbol = config.symbol
|
||||
|
||||
@@ -77,9 +76,10 @@ class Bitcoin(PythonData):
|
||||
try:
|
||||
live_btc = json.loads(line)
|
||||
|
||||
# If value is zero, return None
|
||||
# If value is zero, return coin
|
||||
value = live_btc["last"]
|
||||
if value == 0: return None
|
||||
if value == 0:
|
||||
return coin
|
||||
|
||||
coin.time = datetime.now()
|
||||
coin.value = value
|
||||
@@ -94,12 +94,12 @@ class Bitcoin(PythonData):
|
||||
return coin
|
||||
except ValueError:
|
||||
# Do nothing, possible error in json decoding
|
||||
return None
|
||||
return coin
|
||||
|
||||
# Example Line Format:
|
||||
# code date high low mid last bid ask volume
|
||||
# BTCUSD 2024-10-08 63248.0 61940.0 62246.5 62245.0 62246.0 62247.0 477.91102114
|
||||
if not (line.strip() and line[7].isdigit()): return None
|
||||
if not (line.strip() and line[7].isdigit()): return coin
|
||||
|
||||
try:
|
||||
data = line.split(',')
|
||||
@@ -117,4 +117,4 @@ class Bitcoin(PythonData):
|
||||
|
||||
except ValueError:
|
||||
# Do nothing, possible error in json decoding
|
||||
return None
|
||||
return coin
|
||||
|
||||
Reference in New Issue
Block a user