generated from QuantConnect/Lean.Brokerages.Template
-
Notifications
You must be signed in to change notification settings - Fork 13
Add inverse support #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arodus
wants to merge
3
commits into
QuantConnect:master
Choose a base branch
from
arodus:feature-inverse-futures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
QuantConnect.BybitBrokerage.Tests/BybitInverseFuturesBrokerageAdditionalTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using NUnit.Framework; | ||
| using QuantConnect.Brokerages; | ||
| using QuantConnect.Interfaces; | ||
| using QuantConnect.Lean.Engine.DataFeeds; | ||
|
|
||
| namespace QuantConnect.BybitBrokerage.Tests | ||
| { | ||
| [TestFixture, Explicit("Requires valid credentials to be setup and run outside USA")] | ||
| public class BybitInverseFuturesBrokerageAdditionalTests : BybitBrokerageAdditionalTests | ||
| { | ||
| protected override string BrokerageName => nameof(BybitInverseFuturesBrokerage); | ||
|
|
||
|
|
||
| protected override Brokerage CreateBrokerage(IAlgorithm algorithm, string apiKey, string apiSecret, | ||
| string apiUrl, string websocketUrl) | ||
| { | ||
| return new BybitInverseFuturesBrokerage(apiKey, apiSecret, apiUrl, websocketUrl, algorithm, new AggregationManager(), | ||
| null); | ||
| } | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
QuantConnect.BybitBrokerage.Tests/BybitInverseFuturesBrokerageHistoryProviderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace QuantConnect.BybitBrokerage.Tests | ||
| { | ||
| [TestFixture, Explicit("Requires valid credentials to be setup and run outside USA")] | ||
| public class BybitInverseFuturesBrokerageHistoryProviderTests : BybitBrokerageHistoryProviderTests | ||
| { | ||
| private static readonly Symbol ETHUSD = Symbol.Create("ETHUSDT", SecurityType.CryptoFuture, Market.Bybit); | ||
|
|
||
| private static TestCaseData[] ValidHistory | ||
| { | ||
| get | ||
| { | ||
| return new[] | ||
| { | ||
| // valid | ||
| new TestCaseData(ETHUSD, Resolution.Tick, Time.OneMinute, TickType.Trade), | ||
| new TestCaseData(ETHUSD, Resolution.Minute, Time.OneHour, TickType.Trade), | ||
| new TestCaseData(ETHUSD, Resolution.Hour, Time.OneDay, TickType.Trade), | ||
| new TestCaseData(ETHUSD, Resolution.Daily, TimeSpan.FromDays(15), TickType.Trade), | ||
| new TestCaseData(ETHUSD, Resolution.Hour, Time.OneDay, TickType.OpenInterest) | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(ValidHistory))] | ||
| public override void GetsHistory(Symbol symbol, Resolution resolution, TimeSpan period, TickType tickType) | ||
| { | ||
| base.GetsHistory(symbol, resolution, period, tickType); | ||
| } | ||
|
|
||
| [Ignore("The brokerage is shared between different product categories, therefore this test is only required in the base class")] | ||
| public override void GetEmptyHistory(Symbol symbol, Resolution resolution, TimeSpan period, TickType tickType) | ||
| { | ||
| base.GetEmptyHistory(symbol, resolution, period, tickType); | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
QuantConnect.BybitBrokerage.Tests/BybitInverseFuturesBrokerageTests.Stream.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| namespace QuantConnect.BybitBrokerage.Tests | ||
| { | ||
| [TestFixture] | ||
| public partial class BybitInverseFuturesBrokerageTests | ||
| { | ||
| private static TestCaseData[] TestParameters | ||
| { | ||
| get | ||
| { | ||
| return new[] | ||
| { | ||
| // valid parameters, for example | ||
| new TestCaseData(BTCUSD, Resolution.Tick, false), | ||
| new TestCaseData(BTCUSD, Resolution.Minute, true), | ||
| new TestCaseData(BTCUSD, Resolution.Second, true), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(TestParameters))] | ||
| public override void StreamsData(Symbol symbol, Resolution resolution, bool throwsException) | ||
| { | ||
| base.StreamsData(symbol, resolution, throwsException); | ||
| } | ||
| } | ||
| } |
138 changes: 138 additions & 0 deletions
138
QuantConnect.BybitBrokerage.Tests/BybitInverseFuturesBrokerageTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using NUnit.Framework; | ||
| using QuantConnect.BybitBrokerage.Models.Enums; | ||
| using QuantConnect.Data; | ||
| using QuantConnect.Interfaces; | ||
| using QuantConnect.Lean.Engine.DataFeeds; | ||
| using QuantConnect.Logging; | ||
| using QuantConnect.Orders; | ||
| using QuantConnect.Securities; | ||
| using QuantConnect.Tests.Brokerages; | ||
| using QuantConnect.Util; | ||
|
|
||
| namespace QuantConnect.BybitBrokerage.Tests; | ||
|
|
||
| [TestFixture, Explicit("Requires valid credentials to be setup and run outside USA")] | ||
| public partial class BybitInverseFuturesBrokerageTests : BybitBrokerageTests | ||
| { | ||
| private static Symbol BTCUSD = Symbol.Create("BTCUSD", SecurityType.CryptoFuture, "bybit"); | ||
| protected override Symbol Symbol { get; } = BTCUSD; | ||
|
|
||
| protected override SecurityType SecurityType => SecurityType.Future; | ||
| protected override BybitProductCategory Category => BybitProductCategory.Inverse; | ||
| protected override decimal TakerFee => 0.0000015m; | ||
|
|
||
| protected override decimal GetDefaultQuantity() => 10m; | ||
|
|
||
| protected override IBrokerage CreateBrokerage(string apiKey, string apiSecret, string apiUrl, | ||
| string websocketUrl, IAlgorithm algorithm, IOrderProvider orderProvider, ISecurityProvider securityProvider, | ||
| IDataAggregator aggregator) | ||
| { | ||
| return new BybitInverseFuturesBrokerage(apiKey, apiSecret, apiUrl, websocketUrl, algorithm, orderProvider, securityProvider, new AggregationManager(), null); | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provides the data required to test each order type in various cases | ||
| /// </summary> | ||
| private static TestCaseData[] OrderParameters() | ||
| { | ||
| return new[] | ||
| { | ||
| new TestCaseData(new MarketOrderTestParameters(BTCUSD)).SetName("MarketOrder"), | ||
| new TestCaseData(new LimitOrderTestParameters(BTCUSD, 50000m, 10000m)).SetName("LimitOrder"), | ||
| new TestCaseData(new StopMarketOrderTestParameters(BTCUSD, 50000m, 10000m)).SetName("StopMarketOrder"), | ||
| new TestCaseData(new StopLimitOrderTestParameters(BTCUSD, 50000m, 10000m)).SetName("StopLimitOrder"), | ||
| new TestCaseData(new LimitIfTouchedOrderTestParameters(BTCUSD, 50000m, 20000)).SetName( | ||
| "LimitIfTouchedOrder") | ||
| }; | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void CancelOrders(OrderTestParameters parameters) | ||
| { | ||
| base.CancelOrders(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void LongFromZero(OrderTestParameters parameters) | ||
| { | ||
| base.LongFromZero(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void CloseFromLong(OrderTestParameters parameters) | ||
| { | ||
| base.CloseFromLong(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void ShortFromZero(OrderTestParameters parameters) | ||
| { | ||
| base.ShortFromZero(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void CloseFromShort(OrderTestParameters parameters) | ||
| { | ||
| base.CloseFromShort(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void ShortFromLong(OrderTestParameters parameters) | ||
| { | ||
| base.ShortFromLong(parameters); | ||
| } | ||
|
|
||
| [Test, TestCaseSource(nameof(OrderParameters))] | ||
| public override void LongFromShort(OrderTestParameters parameters) | ||
| { | ||
| base.LongFromShort(parameters); | ||
| } | ||
|
|
||
|
|
||
| [Test] | ||
| public override void GetAccountHoldings() | ||
| { | ||
| Log.Trace(""); | ||
| Log.Trace("GET ACCOUNT HOLDINGS"); | ||
| Log.Trace(""); | ||
| var before = Brokerage.GetCashBalance(); | ||
|
|
||
| var order = new MarketOrder(Symbol, GetDefaultQuantity(), DateTime.UtcNow); | ||
| PlaceOrderWaitForStatus(order); | ||
|
|
||
| Thread.Sleep(3000); | ||
|
|
||
| var after = Brokerage.GetCashBalance(); | ||
|
|
||
| CurrencyPairUtil.DecomposeCurrencyPair(Symbol, out var baseCurrency, out _); | ||
| var beforeHoldings = before.FirstOrDefault(x => x.Currency == baseCurrency); | ||
| var afterHoldings = after.FirstOrDefault(x => x.Currency == baseCurrency); | ||
|
|
||
| var beforeQuantity = beforeHoldings == null ? 0 : beforeHoldings.Amount; | ||
| var afterQuantity = afterHoldings == null ? 0 : afterHoldings.Amount; | ||
|
|
||
| var fee = 0.00000015m; | ||
|
|
||
| Assert.AreEqual(0, afterQuantity - beforeQuantity + fee); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How/where is this going to be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not used but the question is if it should. Hedge mode is currently not supported and for inverse the default mode is hege which can only be changed to normal mode by using the API.
So should this be done automatically or is the user responsible for that, if so is there a way to make this function available to the user in any way. It would definitely need to be documented.