Lean  $LEAN_TAG$
KrakenBrokerageModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System.Collections.Generic;
18 using System.Linq;
20 using QuantConnect.Orders;
23 using QuantConnect.Util;
24 
26 {
27  /// <summary>
28  /// Kraken Brokerage model
29  /// </summary>
31  {
32  private readonly List<string> _fiatsAvailableMargin = new() {"USD", "EUR"};
33  private readonly List<string> _onlyFiatsAvailableMargin = new() {"BTC", "USDT", "USDC"};
34  private readonly List<string> _ethAvailableMargin = new() {"REP", "XTZ", "ADA", "EOS", "TRX", "LINK" };
35 
36  private readonly HashSet<OrderType> _supportedOrderTypes = new()
37  {
38  OrderType.Limit,
39  OrderType.Market,
40  OrderType.StopMarket,
41  OrderType.StopLimit,
42  OrderType.LimitIfTouched
43  };
44 
45  /// <summary>
46  /// Gets a map of the default markets to be used for each security type
47  /// </summary>
48  public override IReadOnlyDictionary<SecurityType, string> DefaultMarkets { get; } = GetDefaultMarkets();
49 
50  /// <summary>
51  /// Leverage map of different coins
52  /// </summary>
53  public IReadOnlyDictionary<string, decimal> CoinLeverage { get; } = new Dictionary<string, decimal>
54  {
55  {"BTC", 5}, // only with fiats
56  {"ETH", 5},
57  {"USDT", 2}, // only with fiats
58  {"XMR", 2},
59  {"REP", 2}, // eth available
60  {"XRP", 3},
61  {"BCH", 2},
62  {"XTZ", 2}, // eth available
63  {"LTC", 3},
64  {"ADA", 3}, // eth available
65  {"EOS", 3}, // eth available
66  {"DASH", 3},
67  {"TRX", 3}, // eth available
68  {"LINK", 3}, // eth available
69  {"USDC", 3}, // only with fiats
70  };
71 
72  /// <summary>
73  /// Constructor for Kraken brokerage model
74  /// </summary>
75  /// <param name="accountType">Cash or Margin</param>
76  public KrakenBrokerageModel(AccountType accountType = AccountType.Cash) : base(accountType)
77  {
78 
79  }
80 
81  /// <summary>
82  /// Returns true if the brokerage could accept this order. This takes into account
83  /// order type, security type, and order size limits.
84  /// </summary>
85  /// <remarks>
86  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
87  /// </remarks>
88  /// <param name="security">The security of the order</param>
89  /// <param name="order">The order to be processed</param>
90  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
91  /// <returns>True if the brokerage could process the order, false otherwise</returns>
92  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
93  {
94  if (!IsValidOrderSize(security, order.Quantity, out message))
95  {
96  return false;
97  }
98 
99  message = null;
100  if (security.Type != SecurityType.Crypto)
101  {
102  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
103  Messages.DefaultBrokerageModel.UnsupportedSecurityType(this, security));
104 
105  return false;
106  }
107 
108  if (!_supportedOrderTypes.Contains(order.Type))
109  {
110  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
111  Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportedOrderTypes));
112 
113  return false;
114  }
115 
116  return base.CanSubmitOrder(security, order, out message);
117  }
118 
119  /// <summary>
120  /// Kraken does not support update of orders
121  /// </summary>
122  /// <param name="security">Security</param>
123  /// <param name="order">Order that should be updated</param>
124  /// <param name="request">Update request</param>
125  /// <param name="message">Outgoing message</param>
126  /// <returns>Always false as Kraken does not support update of orders</returns>
127  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
128  {
129  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, 0, Messages.DefaultBrokerageModel.OrderUpdateNotSupported);
130  return false;
131  }
132 
133  /// <summary>
134  /// Provides Kraken fee model
135  /// </summary>
136  /// <param name="security">Security</param>
137  /// <returns>Kraken fee model</returns>
138  public override IFeeModel GetFeeModel(Security security)
139  {
140  return new KrakenFeeModel();
141  }
142 
143  /// <summary>
144  /// Kraken global leverage rule
145  /// </summary>
146  /// <param name="security"></param>
147  /// <returns></returns>
148  public override decimal GetLeverage(Security security)
149  {
150  if (AccountType == AccountType.Cash)
151  {
152  return 1m;
153  }
154 
155  // first check whether this security support margin only with fiats.
156  foreach (var coin in _onlyFiatsAvailableMargin.Where(coin => security.Symbol.ID.Symbol.StartsWith(coin)).Where(coin => _fiatsAvailableMargin.Any(rightFiat => security.Symbol.Value.EndsWith(rightFiat))))
157  {
158  return CoinLeverage[coin];
159  }
160 
161  List<string> extendedCoinArray = new() {"BTC", "ETH"};
162  extendedCoinArray.AddRange(_fiatsAvailableMargin);
163  // Then check whether this security support margin with ETH.
164  foreach (var coin in _ethAvailableMargin.Where(coin => security.Symbol.ID.Symbol.StartsWith(coin)).Where(coin => extendedCoinArray.Any(rightFiat => security.Symbol.Value.EndsWith(rightFiat))))
165  {
166  return CoinLeverage[coin];
167  }
168 
169  extendedCoinArray.Remove("ETH");
170  // At the end check all others.
171  foreach (var coin in CoinLeverage.Keys.Where(coin => security.Symbol.ID.Symbol.StartsWith(coin)).Where(coin => extendedCoinArray.Any(rightFiat => security.Symbol.Value.EndsWith(rightFiat))))
172  {
173  return CoinLeverage[coin];
174  }
175 
176  return 1m;
177  }
178 
179  /// <summary>
180  /// Get the benchmark for this model
181  /// </summary>
182  /// <param name="securities">SecurityService to create the security with if needed</param>
183  /// <returns>The benchmark for this brokerage</returns>
184  public override IBenchmark GetBenchmark(SecurityManager securities)
185  {
186  var symbol = Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Kraken);
187  return SecurityBenchmark.CreateInstance(securities, symbol);
188  }
189 
190  /// <summary>
191  /// Get default markets and specify Kraken as crypto market
192  /// </summary>
193  /// <returns>default markets</returns>
194  private static IReadOnlyDictionary<SecurityType, string> GetDefaultMarkets()
195  {
196  var map = DefaultMarketMap.ToDictionary();
197  map[SecurityType.Crypto] = Market.Kraken;
198  return map.ToReadOnlyDictionary();
199  }
200  }
201 }