-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Add IB Tiered Fee Model #8446
Open
LouisSzeto
wants to merge
17
commits into
QuantConnect:master
Choose a base branch
from
LouisSzeto:feature-ib-tier-fee-model
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
Add IB Tiered Fee Model #8446
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6772fa3
regulatory fee in IB fee model
LouisSzeto 609cedf
IB Tiered Fee Model
LouisSzeto 8b2d720
option regulatory transaction clearing fee
LouisSzeto 92dab00
add crypto fee structure
LouisSzeto dced9e9
bug fix
LouisSzeto d532be2
add crypto fee test in IB original fee model
LouisSzeto 7a14926
add tier model unit test structure
LouisSzeto 03f3b33
add monthly rolling logic and test
LouisSzeto f4f3b5c
add regression algorithm base
LouisSzeto 3e8b3dc
bug fix
LouisSzeto 3837338
update tests
LouisSzeto 87dab18
empty commit
LouisSzeto e66ab8e
shared functions and objects through helper class
LouisSzeto 84e4be8
revert redundnacies, fix bugs
LouisSzeto d26361c
python version
LouisSzeto d6b94be
fix bugs
LouisSzeto fbd8c90
Merge branch 'master' into feature-ib-tier-fee-model
LouisSzeto 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
130 changes: 130 additions & 0 deletions
130
Algorithm.CSharp/InteractiveBrokersTieredFeeModelAlgorithm.cs
This file contains 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,130 @@ | ||
/* | ||
* 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.Collections.Generic; | ||
using QuantConnect.Data; | ||
using QuantConnect.Interfaces; | ||
using QuantConnect.Orders.Fees; | ||
|
||
namespace QuantConnect.Algorithm.CSharp | ||
{ | ||
/// <summary> | ||
/// Test algorithm using <see cref="InteractiveBrokersTieredFeeModel"/> | ||
/// </summary> | ||
public class InteractiveBrokersTieredFeeModelAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition | ||
{ | ||
private Symbol _spy, _aig, _bac; | ||
private IFeeModel _feeModel = new InteractiveBrokersTieredFeeModel(); | ||
|
||
/// <summary> | ||
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. | ||
/// </summary> | ||
public override void Initialize() | ||
{ | ||
SetStartDate(2013, 10, 7); //Set Start Date | ||
SetEndDate(2013, 10, 10); //Set End Date | ||
SetCash(1000000000); //Set Strategy Cash | ||
|
||
// Set the fee model to be shared by all securities to accurately track the volume/value traded to select the correct tiered fee structure. | ||
SetSecurityInitializer((security) => security.SetFeeModel(_feeModel)); | ||
|
||
_spy = AddEquity("SPY", Resolution.Minute, extendedMarketHours: true).Symbol; | ||
_aig = AddEquity("AIG", Resolution.Minute, extendedMarketHours: true).Symbol; | ||
_bac = AddEquity("BAC", Resolution.Minute, extendedMarketHours: true).Symbol; | ||
} | ||
|
||
public override void OnData(Slice slice) | ||
{ | ||
// Order at different time for various order type to elicit different fee structure. | ||
if (slice.Time.Hour == 9 && slice.Time.Minute == 0) | ||
{ | ||
MarketOnOpenOrder(_spy, 30000); | ||
MarketOnOpenOrder(_aig, 30000); | ||
MarketOnOpenOrder(_bac, 30000); | ||
} | ||
else if (slice.Time.Hour == 10 && slice.Time.Minute == 0) | ||
{ | ||
MarketOrder(_spy, 30000); | ||
MarketOrder(_aig, 30000); | ||
MarketOrder(_bac, 30000); | ||
} | ||
else if (slice.Time.Hour == 15 && slice.Time.Minute == 30) | ||
{ | ||
MarketOnCloseOrder(_spy, -60000); | ||
MarketOnCloseOrder(_aig, -60000); | ||
MarketOnCloseOrder(_bac, -60000); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. | ||
/// </summary> | ||
public bool CanRunLocally { get; } = true; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate which languages this algorithm is written in. | ||
/// </summary> | ||
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python }; | ||
|
||
/// <summary> | ||
/// Data Points count of all timeslices of algorithm | ||
/// </summary> | ||
public long DataPoints => 23076; | ||
|
||
/// <summary> | ||
/// Data Points count of the algorithm history | ||
/// </summary> | ||
public int AlgorithmHistoryDataPoints => 0; | ||
|
||
/// <summary> | ||
/// Final status of the algorithm | ||
/// </summary> | ||
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm | ||
/// </summary> | ||
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> | ||
{ | ||
{"Total Orders", "36"}, | ||
{"Average Win", "0.00%"}, | ||
{"Average Loss", "0.00%"}, | ||
{"Compounding Annual Return", "-2.237%"}, | ||
{"Drawdown", "0.000%"}, | ||
{"Expectancy", "-0.486"}, | ||
{"Start Equity", "1000000000"}, | ||
{"End Equity", "999762433.94"}, | ||
{"Net Profit", "-0.024%"}, | ||
{"Sharpe Ratio", "-8.397"}, | ||
{"Sortino Ratio", "-11.384"}, | ||
{"Probabilistic Sharpe Ratio", "0%"}, | ||
{"Loss Rate", "75%"}, | ||
{"Win Rate", "25%"}, | ||
{"Profit-Loss Ratio", "1.06"}, | ||
{"Alpha", "-0.035"}, | ||
{"Beta", "0.009"}, | ||
{"Annual Standard Deviation", "0.003"}, | ||
{"Annual Variance", "0"}, | ||
{"Information Ratio", "-5.78"}, | ||
{"Tracking Error", "0.269"}, | ||
{"Treynor Ratio", "-2.319"}, | ||
{"Total Fees", "$185772.29"}, | ||
{"Estimated Strategy Capacity", "$11000000.00"}, | ||
{"Lowest Capacity Asset", "AIG R735QTJ8XC9X"}, | ||
{"Portfolio Turnover", "2.37%"}, | ||
{"OrderListHash", "d35a4e91c145a100d4bffb7c0fc0ff35"} | ||
}; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Algorithm.Python/InteractiveBrokersTieredFeeModelAlgorithm.py
This file contains 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,48 @@ | ||
# 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. | ||
|
||
from AlgorithmImports import * | ||
|
||
### <summary> | ||
### Test algorithm using "InteractiveBrokersTieredFeeModel" | ||
### </summary> | ||
class InteractiveBrokersTieredFeeModelAlgorithm(QCAlgorithm): | ||
fee_model = InteractiveBrokersTieredFeeModel() | ||
|
||
def initialize(self): | ||
''' Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.''' | ||
self.set_start_date(2013, 10, 7) #Set Start Date | ||
self.set_end_date(2013, 10, 10) #Set End Date | ||
self.set_cash(1000000000) #Set Strategy Cash | ||
|
||
# Set the fee model to be shared by all securities to accurately track the volume/value traded to select the correct tiered fee structure. | ||
self.set_security_initializer(lambda security: security.set_fee_model(self.fee_model)) | ||
|
||
self.spy = self.add_equity("SPY", Resolution.MINUTE, extended_market_hours=True).symbol | ||
self.aig = self.add_equity("AIG", Resolution.MINUTE, extended_market_hours=True).symbol | ||
self.bac = self.add_equity("BAC", Resolution.MINUTE, extended_market_hours=True).symbol | ||
|
||
def on_data(self, slice: Slice) -> None: | ||
# Order at different time for various order type to elicit different fee structure. | ||
if slice.time.hour == 9 and slice.time.minute == 0: | ||
self.market_on_open_order(self.spy, 30000) | ||
self.market_on_open_order(self.aig, 30000) | ||
self.market_on_open_order(self.bac, 30000) | ||
elif slice.time.hour == 10 and slice.time.minute == 0: | ||
self.market_order(self.spy, 30000) | ||
self.market_order(self.aig, 30000) | ||
self.market_order(self.bac, 30000) | ||
elif slice.time.hour == 15 and slice.time.minute == 30: | ||
self.market_on_close_order(self.spy, -60000) | ||
self.market_on_close_order(self.aig, -60000) | ||
self.market_on_close_order(self.bac, -60000) |
This file contains 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.
Are these orders filled on the next day or right away? If the latest, might as well use MarketOrder? Same for the MarketOnClose ones
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.
They should be filled right away at 9:30am, same for market on close at 4pm