Skip to content

Commit e9722bb

Browse files
Fix dydx holdings value
- dYdX crypto futures holdings value behaves like normal positions qtty * price
1 parent 8f2fd3d commit e9722bb

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using QuantConnect.Data;
2+
/*
3+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
4+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using QuantConnect.Interfaces;
18+
using System.Collections.Generic;
19+
using QuantConnect.Securities.CryptoFuture;
20+
using System;
21+
22+
namespace QuantConnect.Algorithm.CSharp
23+
{
24+
/// <summary>
25+
/// Regression algorithm asserting DYDX Crypto Future support
26+
/// </summary>
27+
public class DYDXCryptoFuturesRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
28+
{
29+
private CryptoFuture _cryptoFuture;
30+
31+
/// <summary>
32+
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
33+
/// </summary>
34+
public override void Initialize()
35+
{
36+
SetStartDate(2026, 1, 1);
37+
SetEndDate(2026, 1, 1);
38+
39+
SetBrokerageModel(Brokerages.BrokerageName.DYDX, AccountType.Margin);
40+
_cryptoFuture = AddCryptoFuture("BTCUSD");
41+
}
42+
43+
/// <summary>
44+
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
45+
/// </summary>
46+
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
47+
public override void OnData(Slice slice)
48+
{
49+
if (!Portfolio.Invested)
50+
{
51+
Buy("BTCUSD", 1);
52+
}
53+
else
54+
{
55+
if (Math.Abs(Portfolio.TotalFees - Portfolio.TotalHoldingsValue * 0.0005m) > 1
56+
|| Math.Abs(Portfolio.TotalFees - _cryptoFuture.Price * 0.0005m) > 1)
57+
{
58+
throw new RegressionTestException("Unexpected fees value!");
59+
}
60+
if (Math.Abs(Portfolio.TotalHoldingsValue - _cryptoFuture.Price) > 1)
61+
{
62+
throw new RegressionTestException("Unexpected holdings value!");
63+
}
64+
Quit();
65+
}
66+
}
67+
68+
/// <summary>
69+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
70+
/// </summary>
71+
public bool CanRunLocally { get; } = true;
72+
73+
/// <summary>
74+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
75+
/// </summary>
76+
public List<Language> Languages { get; } = new() { Language.CSharp };
77+
78+
/// <summary>
79+
/// Data Points count of all timeslices of algorithm
80+
/// </summary>
81+
public long DataPoints => 5;
82+
83+
/// <summary>
84+
/// Data Points count of the algorithm history
85+
/// </summary>
86+
public int AlgorithmHistoryDataPoints => 15;
87+
88+
/// <summary>
89+
/// Final status of the algorithm
90+
/// </summary>
91+
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;
92+
93+
/// <summary>
94+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
95+
/// </summary>
96+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
97+
{
98+
{"Total Orders", "1"},
99+
{"Average Win", "0%"},
100+
{"Average Loss", "0%"},
101+
{"Compounding Annual Return", "0%"},
102+
{"Drawdown", "0%"},
103+
{"Expectancy", "0"},
104+
{"Start Equity", "100000"},
105+
{"End Equity", "99929.57"},
106+
{"Net Profit", "0%"},
107+
{"Sharpe Ratio", "0"},
108+
{"Sortino Ratio", "0"},
109+
{"Probabilistic Sharpe Ratio", "0%"},
110+
{"Loss Rate", "0%"},
111+
{"Win Rate", "0%"},
112+
{"Profit-Loss Ratio", "0"},
113+
{"Alpha", "0"},
114+
{"Beta", "0"},
115+
{"Annual Standard Deviation", "0"},
116+
{"Annual Variance", "0"},
117+
{"Information Ratio", "0"},
118+
{"Tracking Error", "0"},
119+
{"Treynor Ratio", "0"},
120+
{"Total Fees", "$43.71"},
121+
{"Estimated Strategy Capacity", "$33000.00"},
122+
{"Lowest Capacity Asset", "BTCUSD 38Z"},
123+
{"Portfolio Turnover", "87.48%"},
124+
{"Drawdown Recovery", "0"},
125+
{"OrderListHash", "637a937cda83ce88d29a3b279832401d"}
126+
};
127+
}
128+
}

Common/Securities/CryptoFuture/CryptoFutureHolding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public CryptoFutureHolding(Security security, ICurrencyConverter currencyConvert
4040
/// <returns>The value of the quantity of shares in the account currency</returns>
4141
public override ConvertibleCashAmount GetQuantityValue(decimal quantity, decimal price)
4242
{
43+
if (Symbol.ID.Market == Market.DYDX)
44+
{
45+
// common math quantity * quote price
46+
return base.GetQuantityValue(quantity, price);
47+
}
4348
var cryptoFuture = (CryptoFuture)Security;
4449

4550
Cash cash;
29.4 KB
Binary file not shown.
18 KB
Binary file not shown.

0 commit comments

Comments
 (0)