/*
* 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.Collections.Generic;
using QuantConnect.Benchmarks;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Securities;
using QuantConnect.Python;
namespace QuantConnect.Brokerages
{
///
/// Models brokerage transactions, fees, and order
///
public interface IBrokerageModel
{
///
/// Gets the account type used by this model
///
AccountType AccountType
{
get;
}
///
/// Gets the brokerages model percentage factor used to determine the required unused buying power for the account.
/// From 1 to 0. Example: 0 means no unused buying power is required. 0.5 means 50% of the buying power should be left unused.
///
decimal RequiredFreeBuyingPowerPercent
{
get;
}
///
/// Gets a map of the default markets to be used for each security type
///
IReadOnlyDictionary DefaultMarkets { get; }
///
/// Returns true if the brokerage could accept this order. This takes into account
/// order type, security type, and order size limits.
///
///
/// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
///
/// The security being ordered
/// The order to be processed
/// If this function returns false, a brokerage message detailing why the order may not be submitted
/// True if the brokerage could process the order, false otherwise
bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message);
///
/// Returns true if the brokerage would allow updating the order as specified by the request
///
/// The security of the order
/// The order to be updated
/// The requested updated to be made to the order
/// If this function returns false, a brokerage message detailing why the order may not be updated
/// True if the brokerage would allow updating the order, false otherwise
bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message);
///
/// Returns true if the brokerage would be able to execute this order at this time assuming
/// market prices are sufficient for the fill to take place. This is used to emulate the
/// brokerage fills in backtesting and paper trading. For example some brokerages may not perform
/// executions during extended market hours. This is not intended to be checking whether or not
/// the exchange is open, that is handled in the Security.Exchange property.
///
/// The security being ordered
/// The order to test for execution
/// True if the brokerage would be able to perform the execution, false otherwise
bool CanExecuteOrder(Security security, Order order);
///
/// Applies the split to the specified order ticket
///
/// The open tickets matching the split event
/// The split event data
void ApplySplit(List tickets, Split split);
///
/// Gets the brokerage's leverage for the specified security
///
/// The security's whose leverage we seek
/// The leverage for the specified security
decimal GetLeverage(Security security);
///
/// Get the benchmark for this model
///
/// SecurityService to create the security with if needed
/// The benchmark for this brokerage
IBenchmark GetBenchmark(SecurityManager securities);
///
/// Gets a new fill model that represents this brokerage's fill behavior
///
/// The security to get fill model for
/// The new fill model for this brokerage
IFillModel GetFillModel(Security security);
///
/// Gets a new fee model that represents this brokerage's fee structure
///
/// The security to get a fee model for
/// The new fee model for this brokerage
IFeeModel GetFeeModel(Security security);
///
/// Gets a new slippage model that represents this brokerage's fill slippage behavior
///
/// The security to get a slippage model for
/// The new slippage model for this brokerage
ISlippageModel GetSlippageModel(Security security);
///
/// Gets a new settlement model for the security
///
/// The security to get a settlement model for
/// The settlement model for this brokerage
ISettlementModel GetSettlementModel(Security security);
///
/// Gets a new margin interest rate model for the security
///
/// The security to get a margin interest rate model for
/// The margin interest rate model for this brokerage
IMarginInterestRateModel GetMarginInterestRateModel(Security security);
///
/// Gets a new settlement model for the security
///
/// The security to get a settlement model for
/// The account type
/// The settlement model for this brokerage
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
ISettlementModel GetSettlementModel(Security security, AccountType accountType);
///
/// Gets a new buying power model for the security
///
/// The security to get a buying power model for
/// The buying power model for this brokerage/security
IBuyingPowerModel GetBuyingPowerModel(Security security);
///
/// Gets a new buying power model for the security
///
/// The security to get a buying power model for
/// The account type
/// The buying power model for this brokerage/security
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType);
///
/// Gets the shortable provider
///
/// Shortable provider
IShortableProvider GetShortableProvider(Security security);
}
///
/// Provides factory method for creating an from the enum
///
public static class BrokerageModel
{
///
/// Creates a new for the specified
///
/// The order provider
/// The name of the brokerage
/// The account type
/// The model for the specified brokerage
public static IBrokerageModel Create(IOrderProvider orderProvider, BrokerageName brokerage, AccountType accountType)
{
switch (brokerage)
{
case BrokerageName.Default:
case BrokerageName.TerminalLink:
return new DefaultBrokerageModel(accountType);
case BrokerageName.Alpaca:
return new AlpacaBrokerageModel();
case BrokerageName.InteractiveBrokersBrokerage:
return new InteractiveBrokersBrokerageModel(accountType);
case BrokerageName.InteractiveBrokersFix:
return new InteractiveBrokersFixModel(accountType);
case BrokerageName.TradierBrokerage:
return new TradierBrokerageModel(accountType);
case BrokerageName.OandaBrokerage:
return new OandaBrokerageModel(accountType);
case BrokerageName.FxcmBrokerage:
return new FxcmBrokerageModel(accountType);
case BrokerageName.Bitfinex:
return new BitfinexBrokerageModel(accountType);
case BrokerageName.BinanceFutures:
return new BinanceFuturesBrokerageModel(accountType);
case BrokerageName.BinanceCoinFutures:
return new BinanceCoinFuturesBrokerageModel(accountType);
case BrokerageName.Binance:
return new BinanceBrokerageModel(accountType);
case BrokerageName.BinanceUS:
return new BinanceUSBrokerageModel(accountType);
case BrokerageName.GDAX:
return new GDAXBrokerageModel(accountType);
case BrokerageName.Coinbase:
return new CoinbaseBrokerageModel(accountType);
case BrokerageName.AlphaStreams:
return new AlphaStreamsBrokerageModel(accountType);
case BrokerageName.Zerodha:
return new ZerodhaBrokerageModel(accountType);
case BrokerageName.Axos:
return new AxosClearingBrokerageModel(accountType);
case BrokerageName.TradingTechnologies:
return new TradingTechnologiesBrokerageModel(accountType);
case BrokerageName.Samco:
return new SamcoBrokerageModel(accountType);
case BrokerageName.Kraken:
return new KrakenBrokerageModel(accountType);
case BrokerageName.Exante:
return new ExanteBrokerageModel(accountType);
case BrokerageName.FTX:
return new FTXBrokerageModel(accountType);
case BrokerageName.FTXUS:
return new FTXUSBrokerageModel(accountType);
case BrokerageName.Wolverine:
return new WolverineBrokerageModel(accountType);
case BrokerageName.TDAmeritrade:
return new TDAmeritradeBrokerageModel(accountType);
case BrokerageName.RBI:
return new RBIBrokerageModel(accountType);
case BrokerageName.Bybit:
return new BybitBrokerageModel(accountType);
case BrokerageName.Eze:
return new EzeBrokerageModel(accountType);
case BrokerageName.TradeStation:
return new TradeStationBrokerageModel(accountType);
case BrokerageName.CharlesSchwab:
return new CharlesSchwabBrokerageModel(accountType);
case BrokerageName.Tastytrade:
return new TastytradeBrokerageModel(accountType);
default:
throw new ArgumentOutOfRangeException(nameof(brokerage), brokerage, null);
}
}
///
/// Gets the corresponding for the specified
///
/// The brokerage model
/// The for the specified brokerage model
public static BrokerageName GetBrokerageName(IBrokerageModel brokerageModel)
{
var model = brokerageModel;
if (brokerageModel is BrokerageModelPythonWrapper)
{
model = (brokerageModel as BrokerageModelPythonWrapper).GetModel();
}
// Case order matters to ensure we get the correct brokerage name from the inheritance chain
switch (model)
{
case AlpacaBrokerageModel:
return BrokerageName.Alpaca;
case InteractiveBrokersBrokerageModel _:
return BrokerageName.InteractiveBrokersBrokerage;
case TradierBrokerageModel _:
return BrokerageName.TradierBrokerage;
case OandaBrokerageModel _:
return BrokerageName.OandaBrokerage;
case FxcmBrokerageModel _:
return BrokerageName.FxcmBrokerage;
case BitfinexBrokerageModel _:
return BrokerageName.Bitfinex;
case BinanceUSBrokerageModel _:
return BrokerageName.BinanceUS;
case BinanceBrokerageModel _:
return BrokerageName.Binance;
case GDAXBrokerageModel _:
return BrokerageName.GDAX;
case CoinbaseBrokerageModel _:
return BrokerageName.Coinbase;
case AlphaStreamsBrokerageModel _:
return BrokerageName.AlphaStreams;
case ZerodhaBrokerageModel _:
return BrokerageName.Zerodha;
case AxosClearingBrokerageModel _:
return BrokerageName.Axos;
case TradingTechnologiesBrokerageModel _:
return BrokerageName.TradingTechnologies;
case SamcoBrokerageModel _:
return BrokerageName.Samco;
case KrakenBrokerageModel _:
return BrokerageName.Kraken;
case ExanteBrokerageModel _:
return BrokerageName.Exante;
case FTXUSBrokerageModel _:
return BrokerageName.FTXUS;
case FTXBrokerageModel _:
return BrokerageName.FTX;
case WolverineBrokerageModel _:
return BrokerageName.Wolverine;
case TDAmeritradeBrokerageModel _:
return BrokerageName.TDAmeritrade;
case RBIBrokerageModel _:
return BrokerageName.RBI;
case BybitBrokerageModel _:
return BrokerageName.Bybit;
case EzeBrokerageModel _:
return BrokerageName.Eze;
case TradeStationBrokerageModel _:
return BrokerageName.TradeStation;
case CharlesSchwabBrokerageModel:
return BrokerageName.CharlesSchwab;
case TastytradeBrokerageModel:
return BrokerageName.Tastytrade;
case DefaultBrokerageModel _:
return BrokerageName.Default;
default:
throw new ArgumentOutOfRangeException(nameof(brokerageModel), brokerageModel, null);
}
}
}
}