pep8 conversion of python algorithms #3 (#7934)
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
* raw pep8 conversion * Minor fixes --------- Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
@@ -12,66 +12,67 @@
|
||||
# limitations under the License.
|
||||
|
||||
from AlgorithmImports import *
|
||||
import datetime
|
||||
|
||||
### <summary>
|
||||
### Algorithm demonstrating and ensuring that Bybit crypto brokerage model works as expected with custom data types
|
||||
### </summary>
|
||||
class BybitCustomDataCryptoRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
def Initialize(self):
|
||||
self.SetStartDate(2022, 12, 13)
|
||||
self.SetEndDate(2022, 12, 13)
|
||||
def initialize(self):
|
||||
self.set_start_date(2022, 12, 13)
|
||||
self.set_end_date(2022, 12, 13)
|
||||
|
||||
self.SetAccountCurrency("USDT")
|
||||
self.SetCash(100000)
|
||||
self.set_account_currency("USDT")
|
||||
self.set_cash(100000)
|
||||
|
||||
self.SetBrokerageModel(BrokerageName.Bybit, AccountType.Cash)
|
||||
self.set_brokerage_model(BrokerageName.BYBIT, AccountType.CASH)
|
||||
|
||||
symbol = self.AddCrypto("BTCUSDT").Symbol
|
||||
self.btcUsdt = self.AddData(CustomCryptoData, symbol, Resolution.Minute).Symbol;
|
||||
symbol = self.add_crypto("BTCUSDT").symbol
|
||||
self.btc_usdt = self.add_data(CustomCryptoData, symbol, Resolution.MINUTE).symbol;
|
||||
|
||||
# create two moving averages
|
||||
self.fast = self.EMA(self.btcUsdt, 30, Resolution.Minute)
|
||||
self.slow = self.EMA(self.btcUsdt, 60, Resolution.Minute)
|
||||
self.fast = self.ema(self.btc_usdt, 30, Resolution.MINUTE)
|
||||
self.slow = self.ema(self.btc_usdt, 60, Resolution.MINUTE)
|
||||
|
||||
def OnData(self, data):
|
||||
if not self.slow.IsReady:
|
||||
def on_data(self, data):
|
||||
if not self.slow.is_ready:
|
||||
return
|
||||
|
||||
if self.fast.Current.Value > self.slow.Current.Value:
|
||||
if self.Transactions.OrdersCount == 0:
|
||||
self.Buy(self.btcUsdt, 1)
|
||||
if self.fast.current.value > self.slow.current.value:
|
||||
if self.transactions.orders_count == 0:
|
||||
self.buy(self.btc_usdt, 1)
|
||||
else:
|
||||
if self.Transactions.OrdersCount == 1:
|
||||
self.Liquidate(self.btcUsdt)
|
||||
if self.transactions.orders_count == 1:
|
||||
self.liquidate(self.btc_usdt)
|
||||
|
||||
def OnOrderEvent(self, orderEvent):
|
||||
self.Debug(f"{self.Time} {orderEvent}");
|
||||
def on_order_event(self, order_event):
|
||||
self.debug(f"{self.time} {order_event}");
|
||||
|
||||
class CustomCryptoData(PythonData):
|
||||
def GetSource(self, config, date, isLiveMode):
|
||||
tickTypeString = Extensions.TickTypeToLower(config.TickType)
|
||||
formattedDate = date.strftime("%Y%m%d")
|
||||
def get_source(self, config, date, is_live_mode):
|
||||
tick_type_string = Extensions.tick_type_to_lower(config.tick_type)
|
||||
formatted_date = date.strftime("%Y%m%d")
|
||||
source = os.path.join(Globals.DataFolder, "crypto", "bybit", "minute",
|
||||
config.Symbol.Value.lower(), f"{formattedDate}_{tickTypeString}.zip")
|
||||
config.symbol.value.lower(), f"{formatted_date}_{tick_type_string}.zip")
|
||||
|
||||
return SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv)
|
||||
return SubscriptionDataSource(source, SubscriptionTransportMedium.LOCAL_FILE, FileFormat.CSV)
|
||||
|
||||
def Reader(self, config, line, date, isLiveMode):
|
||||
def reader(self, config, line, date, is_live_mode):
|
||||
csv = line.split(',')
|
||||
|
||||
data = CustomCryptoData()
|
||||
data.Symbol = config.Symbol
|
||||
data.symbol = config.symbol
|
||||
|
||||
data_datetime = datetime.combine(date.date(), time()) + timedelta(milliseconds=int(csv[0]))
|
||||
data.Time = Extensions.ConvertTo(data_datetime, config.DataTimeZone, config.ExchangeTimeZone)
|
||||
data.EndTime = data.Time + timedelta(minutes=1)
|
||||
data_datetime = datetime.datetime.combine(date.date(), datetime.time()) + timedelta(milliseconds=int(csv[0]))
|
||||
data.time = Extensions.convert_to(data_datetime, config.data_time_zone, config.exchange_time_zone)
|
||||
data.end_time = data.time + timedelta(minutes=1)
|
||||
|
||||
data["Open"] = float(csv[1])
|
||||
data["High"] = float(csv[2])
|
||||
data["Low"] = float(csv[3])
|
||||
data["Close"] = float(csv[4])
|
||||
data["Volume"] = float(csv[5])
|
||||
data.Value = float(csv[4])
|
||||
data.value = float(csv[4])
|
||||
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user