I am creating my own custom brokerage in the Lean engine (for dYdX), but I'm encountering some issues with currency conversions. How can I define my own conversion rules? If you check the logs, you'll see the problem.
+ Expand
20250423 12:39:30.567 TRACE:: Config.GetValue(): debug-mode - Using default value: False
20250423 12:39:30.574 TRACE:: Config.Get(): Configuration key not found. Key: version-id - Using default value:
20250423 12:39:30.574 TRACE:: Config.Get(): Configuration key not found. Key: cache-location - Using default value:
20250423 12:39:30.576 TRACE:: Config.GetValue(): project-id - Using default value: 0
20250423 12:39:30.576 TRACE:: Config.Get(): Configuration key not found. Key: api-url - Using default value: https://www.quantconnect.com/api/v2/
20250423 12:39:30.576 TRACE:: Config.Get(): Configuration key not found. Key: results-destination-folder - Using default value: D:\MyProjects\doxia-investing\DoxiaInvesting-QuantConnect-dYdx\Lean\Launcher\bin\Debug
20250423 12:39:30.577 TRACE:: Config.Get(): Configuration key not found. Key: plugin-directory - Using default value:
20250423 12:39:30.596 TRACE:: Config.Get(): Configuration key not found. Key: composer-dll-directory - Using default value:
20250423 12:39:30.598 TRACE:: Composer(): Loading Assemblies from D:\MyProjects\doxia-investing\DoxiaInvesting-QuantConnect-dYdx\Lean\Launcher\bin\Debug\
20250423 12:39:31.479 TRACE:: Python for .NET Assembly: Python.Runtime, Version=2.0.42.0, Culture=neutral, PublicKeyToken=5000fea6cba702dd
20250423 12:39:31.592 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit) Host: DESKTOP-057CRGC
20250423 12:39:31.607 TRACE:: Engine.Main(): Started 17:39
20250423 12:39:31.698 TRACE:: Config.Get(): Configuration key not found. Key: lean-manager-type - Using default value: LocalLeanManager
20250423 12:39:31.746 TRACE:: JobQueue.NextJob(): Selected QuantConnect.Algorithm.CSharp.dll
20250423 12:39:32.121 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-capacity - Using default value: 120
20250423 12:39:32.156 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-time-interval-minutes - Using default value: 1440
20250423 12:39:32.157 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-refill-amount - Using default value: 18
20250423 12:39:32.164 TRACE:: Config.GetValue(): storage-limit - Using default value: 10737418240
20250423 12:39:32.165 TRACE:: Config.GetValue(): storage-file-count - Using default value: 10000
20250423 12:39:32.165 TRACE:: Config.GetValue(): storage-permissions - Using default value: 3
20250423 12:39:32.165 TRACE:: Config.Get(): Configuration key not found. Key: algorithm-id - Using default value: MyTestTradingAlgorithm
20250423 12:39:32.181 TRACE:: Config.Get(): Configuration key not found. Key: history-provider - Using default value: SubscriptionDataReaderHistoryProvider
20250423 12:39:32.181 TRACE:: Config.Get(): Configuration key not found. Key: data-channel - Using default value:
20250423 12:39:32.182 TRACE:: Config.Get(): Configuration key not found. Key: python-venv - Using default value:
20250423 12:39:35.859 TRACE:: Config.Get(): Configuration key not found. Key: data-permission-manager - Using default value: DataPermissionManager
20250423 12:39:35.866 TRACE:: Config.GetValue(): streamed-chart-limit - Using default value: 12
20250423 12:39:35.867 TRACE:: Config.GetValue(): streamed-chart-group-size - Using default value: 3
20250423 12:39:35.876 TRACE:: Config.GetValue(): downloader-data-update-period - Using default value: 7
20250423 12:39:36.496 TRACE:: Config.GetValue(): zip-data-cache-provider - Using default value: 10
20250423 12:39:36.501 TRACE:: Config.Get(): Configuration key not found. Key: fundamental-data-provider - Using default value: CoarseFundamentalDataProvider
20250423 12:39:36.505 TRACE:: AlgorithmManager.CreateTokenBucket(): Initializing LeakyBucket: Capacity: 120 RefillAmount: 18 TimeInterval: 1440
20250423 12:39:36.506 TRACE:: Config.GetValue(): algorithm-manager-time-loop-maximum - Using default value: 20
20250423 12:39:36.526 TRACE:: Engine.Run(): Resource limits '0' CPUs. 2147483647 MB RAM.
20250423 12:39:36.526 TRACE:: TextSubscriptionDataSourceReader.SetCacheSize(): Setting cache size to 71582788 items
20250423 12:39:36.532 TRACE:: Config.GetValue(): algorithm-creation-timeout - Using default value: 90
20250423 12:39:36.535 TRACE:: Loader.TryCreateILAlgorithm(): Loading only the algorithm assembly
20250423 12:39:36.562 TRACE:: Config.Get(): Configuration key not found. Key: databases-refresh-period - Using default value: 1.00:00:00
20250423 12:39:36.789 TRACE:: Loader.TryCreateILAlgorithm(): Loaded MyTestTradingAlgorithm
20250423 12:39:36.790 TRACE:: Config.Get(): Configuration key not found. Key: object-store-root - Using default value: ./storage
20250423 12:39:36.794 TRACE:: LocalObjectStore.Initialize(): Storage Root: D:\MyProjects\doxia-investing\DoxiaInvesting-QuantConnect-dYdx\Lean\Launcher\bin\Debug\storage. StorageFileCount 10000. StorageLimit 10240MB
20250423 12:39:36.795 TRACE:: LiveTradingDataFeed.GetDataChannelProvider(): will use DataChannelProvider
20250423 12:39:36.796 TRACE:: BrokerageSetupHandler.CreateBrokerage(): creating brokerage 'DydxBrokerage'
20250423 12:39:36.812 TRACE:: DydxBrokerage.Initialize(): found no data aggregator instance, creating QuantConnect.Lean.Engine.DataFeeds.AggregationManager
20250423 12:39:39.147 TRACE:: BrokerageMultiWebSocketSubscriptionManager(): WebSocket connections will be restarted every: 23:45:00
20250423 12:39:39.162 TRACE:: CompositeDataQueueHandler.SetJob(): will use [
"DydxBrokerage"
]
20250423 12:39:39.168 TRACE:: BaseDataExchange(CustomDataExchange) Starting...
20250423 12:39:39.168 TRACE:: Config.GetValue(): zip-data-cache-provider - Using default value: 10
20250423 12:39:39.196 TRACE:: HistoryProviderManager.Initialize(): history providers [SubscriptionDataReaderHistoryProvider]
20250423 12:39:39.199 TRACE:: BaseSetupHandler.Setup(LocalPlatform): UID: 346944, PID: 0, Version: 2.5.0.0, Source: WebIDE
20250423 12:39:39.201 TRACE:: LiveTradingResultHandler.SendStatusUpdate(): status: 'LoggingIn'. Logging into brokerage...
20250423 12:39:39.201 TRACE:: BrokerageSetupHandler.Setup(): Connecting to brokerage...
20250423 12:39:39.202 TRACE:: BrokerageSetupHandler.Setup(): dydx account base currency: USD
20250423 12:39:39.203 TRACE:: BrokerageSetupHandler.Setup(): Initializing algorithm...
20250423 12:39:39.203 TRACE:: LiveTradingResultHandler.SendStatusUpdate(): status: 'Initializing'. Initializing algorithm...
20250423 12:39:39.208 TRACE:: Config.Get(): Configuration key not found. Key: security-data-feeds - Using default value:
20250423 12:39:39.216 TRACE:: SecurityPortfolioManager.SetAccountCurrency(): setting account currency to USD
20250423 12:39:39.260 TRACE:: BrokerageSetupHandler.Setup(): Fetching cash balance from brokerage...
20250423 12:39:39.262 TRACE:: BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...
20250423 12:39:39.263 TRACE:: BrokerageSetupHandler.Setup(): Fetching holdings from brokerage...
20250423 12:39:39.264 TRACE:: AggregationManager.Initialize(): daily strict end times: True
20250423 12:39:39.279 TRACE:: LiveTradingDataFeed.CreateUniverseSubscription(): Creating user defined universe: QC-UNIVERSE-USERDEFINED-BINANCE-CRYPTOFUTURE 18R
20250423 12:39:39.284 TRACE:: DataManager.AddSubscription(): Added QC-UNIVERSE-USERDEFINED-BINANCE-CRYPTOFUTURE,#0,QC-UNIVERSE-USERDEFINED-BINANCE-CRYPTOFUTURE,Tick,Tick,Trade,Adjusted,OpenInterest,Internal. Start: 23.04.2025 12:39:39. End: 31.12.2050 00:00:00
20250423 12:39:39.297 ERROR:: No tradeable pair was found for currency BTC, conversion rate to account currency (USD) will be set to zero. Markets: [Forex:oanda,Cfd:oanda,Crypto:coinbase]
20250423 12:39:39.317 TRACE:: Failed to assign conversion rates for the following cash: BTC. Attempting to request daily resolution history to resolve conversion rate
20250423 12:39:39.322 TRACE:: BaseSetupHandler.SetupCurrencyConversions():
Account Type: Margin
Symbol Quantity Conversion = Value in USD
USD: $ 0.00 @ 1.00 = $0
BTC: â‚¿ 0.00 @ 0.00 = $0
-------------------------------------------------
CashBook Total Value: $0
20250423 12:39:39.325 TRACE:: Total margin information: TotalMarginUsed: 0.00, MarginRemaining: 0.00
20250423 12:39:39.329 TRACE:: JOB HANDLERS:
DataFeed: QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed
Setup: QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler
RealTime: QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler
Results: QuantConnect.Lean.Engine.Results.LiveTradingResultHandler
Transactions: QuantConnect.Lean.Engine.TransactionHandlers.DydxBrokerageTransactionHandler
Object Store: QuantConnect.Lean.Engine.Storage.LocalObjectStore
History Provider: QuantConnect.Lean.Engine.HistoricalData.HistoryProviderManager
Brokerage: QuantConnect.Brokerages.DydxBrokerage
Data Provider: QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider
20250423 12:39:39.347 TRACE:: Event Name "Daily Sampling", scheduled to run.
20250423 12:39:39.347 TRACE:: AlgorithmManager.Run(): Begin DataStream - Start: 23.04.2025 00:00:00 Stop: 31.12.2050 00:00:00 Time: 23.04.2025 08:39:39 Warmup: False
+ Expand
public class MyTestTradingAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private decimal _lastTradePrice;
public override void Initialize()
{
SetAccountCurrency("USD");
const string market = "dydx";
Transactions.MarketOrderFillTimeout = TimeSpan.FromMinutes(15);
SetBrokerageModel(new DydxBrokerageModel());
var marketHoursDatabase = QuantConnect.Securities.MarketHoursDatabase.FromDataFolder();
var alwaysOpen = QuantConnect.Securities.SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc);
marketHoursDatabase.SetEntry(market, "[*]", SecurityType.CryptoFuture, alwaysOpen);
_symbol = AddCryptoFuture("BTCUSD", Resolution.Tick).Symbol;
SetBenchmark(_symbol);
}
public override void OnData(Slice data)
{
// .................
}
}
+ Expand
{
"environment": "live-dydx", // "live-dydx", "backtesting"
//"algorithm-type-name": "DydxBacktestAlgorithm", // Algorithm for backtesting using historical data.
"algorithm-type-name": "MyTestTradingAlgorithm", // Algorithm for Testing Integration with dYdX
// Algorithm language selector - options CSharp, Python
"algorithm-language": "CSharp",
"algorithm-location": "QuantConnect.Algorithm.CSharp.dll",
//"algorithm-location": "../../../Algorithm.Python/BasicTemplateFrameworkAlgorithm.py",
//Research notebook
//"composer-dll-directory": ".",
// engine
"data-folder": "../../../Data/",
// debugging configuration - options for debugging-method LocalCmdLine, VisualStudio, Debugpy, PyCharm
"debugging": false,
"debugging-method": "LocalCmdline",
// location of a python virtual env to use libraries from
//"python-venv": "/venv",
// handlers
"log-handler": "QuantConnect.Logging.CompositeLogHandler",
"messaging-handler": "QuantConnect.Messaging.Messaging",
"job-queue-handler": "QuantConnect.Queues.JobQueue",
"api-handler": "QuantConnect.Api.Api",
"map-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider",
"factor-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskFactorFileProvider",
"data-provider": "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider",
"data-channel-provider": "DataChannelProvider",
"object-store": "QuantConnect.Lean.Engine.Storage.LocalObjectStore",
"data-aggregator": "QuantConnect.Lean.Engine.DataFeeds.AggregationManager",
// limits on number of symbols to allow
"symbol-minute-limit": 10000,
"symbol-second-limit": 10000,
"symbol-tick-limit": 10000,
// log missing data files, useful for debugging
"show-missing-data-logs": false,
// For live trading during warmup we limit the amount of historical data fetched from the history provider and expect the data to be on disk for older data
"maximum-warmup-history-days-look-back": 5,
// limits the amount of data points per chart series. Applies only for backtesting
"maximum-data-points-per-chart-series": 1000000,
"maximum-chart-series": 30,
// if one uses true in following token, market hours will remain open all hours and all days.
// if one uses false will make lean operate only during regular market hours.
"force-exchange-always-open": false,
// save list of transactions to the specified csv file
"transaction-log": "",
// Prefix for Windows reserved filenames
"reserved-words-prefix": "@",
// To get your api access token go to quantconnect.com/account
"job-user-id": "346944",
"api-access-token": "c52abbbde7140ffc1bd86293a24b25d524278f7c18f743f7b0b7f3f93267fc35",
"job-organization-id": "",
// live data configuration
"live-data-url": "ws://www.quantconnect.com/api/v2/live/data/",
"live-data-port": 8020,
// live portfolio state
"live-cash-balance": "",
"live-holdings": "[]",
// parameters to set in the algorithm (the below are just samples)
"parameters": {
// Intrinio account user and password
"intrinio-username": "",
"intrinio-password": "",
"ema-fast": 10,
"ema-slow": 20
},
// specify supported languages when running regression tests
"regression-test-languages": [ "CSharp", "Python" ],
// Additional paths to include in python for import resolution
"python-additional-paths": [],
"environments": {
// defines the 'backtesting' environment
"backtesting": {
"live-mode": false,
"setup-handler": "QuantConnect.Lean.Engine.Setup.BacktestingSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.BacktestingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.FileSystemDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler",
"history-provider": [ "QuantConnect.Lean.Engine.HistoricalData.SubscriptionDataReaderHistoryProvider" ],
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler"
},
"live-dydx": {
"live-mode": true,
"live-mode-brokerage": "DydxBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
//"data-queue-handler": [ "DydxBrokerageDataQueue" ],
"data-queue-handler": [ "DydxBrokerage" ],
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
//"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.DydxBrokerageTransactionHandler"
}
}
}
Mia Alissi
Here is a custom brokerage converter for dYdX in Lean, which defines specific currency conversion rules, focusing on the conversion from BTC to USD. This implementation includes error handling for unsupported currencies and integrates with the
CashBook
class to fetch conversion data when available.This code includes a
DydxCurrencyConverter
class that performs currency conversion primarily for BTC to USD using a predefined conversion rate as a fallback. It also includes necessary error handling for unsupported currencies and leverages theCashBook
to ensure currency data feeds when available.Sources: Cashbook - QuantConnect.com, Two Leg Currency Conversion Regression Algorithm, Basic Set Account Currency Algorithm, Bybit Custom Data Crypto Regression Algorithm, Requesting Data - QuantConnect.com
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Oilan Online
Can you convert it to c#?
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!