Skip to content

Commit

Permalink
Adds Alpaca Brokerage and Fee Models
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCatarino committed Jul 16, 2024
1 parent 5e67f3d commit a052ffe
Show file tree
Hide file tree
Showing 70 changed files with 209 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>The brokerage model of your algorithm automatically sets the fill model for each security. The default brokerage model is the <code>DefaultBrokerageModel</code>, which set the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#02-Constant-Model'>ConstantFeeModel</a> with no fees for Forex, CFD, and Crypto assets and sets the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#03-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> for the remaining asset classes.</p>
<p>The brokerage model of your algorithm automatically sets the fill model for each security. The default brokerage model is the <code>DefaultBrokerageModel</code>, which set the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#02-Constant-Model'>ConstantFeeModel</a> with no fees for Forex, CFD, and Crypto assets and sets the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#11-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> for the remaining asset classes.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<p>The <code>AlpacaFeeModel</code> models <a rel="nofollow" target="_blank" href="https://docs.alpaca.markets/docs/crypto-fees">the fees of Alpaca</a>.</p>


<div class="section-example-container">
<pre class="csharp">security.SetFeeModel(new AlpacaFeeModel());</pre>
<pre class="python">security.set_fee_model(AlpacaFeeModel())</pre>
</div>

<p>The <code>AlpacaFeeModel</code> charges zero fees for Equity and Equity Options trading. It charges order fees of Alpaca for crypto trading which is a 0.15% maker and 0.25% taker fee. If you add liquidity to the order book by placing a limit order that doesn't cross the spread, you pay maker fees. If you remove liquidity from the order book by placing an order that crosses the spread, you pay taker fees. Alpaca adjusts your fees based on your 30-day trading volume, but we don't currently model these metrics to adjust fees.</p>

<p>The <code>AlpacaFeeModel</code> charges fees in the currency you receive from a trade. For example, if you buy ETHBTC, you pay fees in ETH. If you sell ETHBTC, you pay fees in BTC.</p>

<p>To view the implementation of this model, see the <a target="_blank" rel="nofollow" href="https://github.com/QuantConnect/Lean/blob/master/Common/Orders/Fees/AlpacaFeeModel.cs">LEAN GitHub repository</a>.</p>

<style>
#binance-fee-table td:last-child,
#binance-fee-table th:last-child {
text-align: right;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"03": "",
"04": "",
"05": "",
"06": "",
"07": "",
"08": "",
"09": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>The <code>DefaultBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#02-Constant-Model'>ConstantFeeModel</a> with no fees for Forex, CFD, Crypto, and Crypto Future assets and the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#03-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> for the remaining asset classes.</p>
<p>The <code>DefaultBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#02-Constant-Model'>ConstantFeeModel</a> with no fees for Forex, CFD, Crypto, and Crypto Future assets and the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#11-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> for the remaining asset classes.</p>

<div class="section-example-container">
<pre class="csharp">// For Forex, CFD, Crypto, and Crypto Future assets:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>This page explains the <code>AlpacaBrokerageModel</code>, including the asset classes it supports, its default <a href='/docs/v2/writing-algorithms/reality-modeling/key-concepts#02-Security-Level-Models'>security-level models</a>, and it's default markets.</p>


<div class="section-example-container">
<pre class="csharp">SetBrokerageModel(BrokerageName.Alpaca, AccountType.Cash);
SetBrokerageModel(BrokerageName.Alpaca, AccountType.Margin);</pre>
<pre class="python">self.set_brokerage_model(BrokerageName.ALPACA, AccountType.CASH)
self.set_brokerage_model(BrokerageName.ALPACA, AccountType.MARGIN)</pre>
</div>

<p>To view the implementation of this model, see the <a rel='nofollow' target="_blank" href='https://github.com/QuantConnect/Lean/blob/master/Common/Brokerages/AlpacaBrokerageModel.cs'>LEAN GitHub repository</a>.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<? include(DOCS_RESOURCES."/brokerages/alpaca/asset-classes.php"); ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>The <code>AlpacaBrokerageModel</code> supports several order types, order properties, and most order updates.</p>

<? include(DOCS_RESOURCES."/brokerages/alpaca/orders.php"); ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/trade-fills/supported-models/equity-model'>EquityFillModel</a> for Equity trades and the <a href='/docs/v2/writing-algorithms/reality-modeling/trade-fills/supported-models/immediate-model'>ImmediateFillModel</a> for Option and Crypto trades.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/slippage/supported-models#02-Null-Model'>NullSlippageModel</a>.</p>

<div class="section-example-container">
<pre class="csharp">security.set_slippage_model(NullSlippageModel.instance);</pre>
<pre class="python">security.set_slippage_model(NullSlippageModel.instance)</pre>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#03-Alpaca-Model'>AlpacaFeeModel</a> with the default argument values. We model current Alpaca fees on all assets.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <code>CashBuyingPowerModel</code> for cash accounts, the <code>SecurityMarginModel</code> for margin accounts, and <code>OptionMarginModel</code> for Option trades.</p>

<p>If you have a margin account, the <code>AlpacaBrokerageModel</code> allows 2x leverage for Equities, and 3x leverage for Crypto.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/settlement/supported-models#02-Immediate-Model'>ImmediateSettlementModel</a> for margin accounts and the <a href='/docs/v2/writing-algorithms/reality-modeling/settlement/supported-models#03-Delayed-Model'>DelayedSettlementModel</a> with the <a href='/docs/v2/writing-algorithms/reality-modeling/settlement/key-concepts#03-Default-Behavior'>default settlement rules</a> for cash accounts with Equity and Equity Options.</p>


<div class="section-example-container">
<pre class="csharp">// For US Equities with a cash account:
security.SetSettlementModel(new DelayedSettlementModel(Equity.DefaultSettlementDays, Equity.DefaultSettlementTime));

// For Equity Options with a cash account:
security.SetSettlementModel(new DelayedSettlementModel(Option.DefaultSettlementDays, Option.DefaultSettlementTime));

// For remaining cases:
security.SetSettlementModel(new ImmediateSettlementModel());</pre>
<pre class="python"># For US Equities with a cash account:
security.set_settlement_model(DelayedSettlementModel(Equity.DEFAULT_SETTLEMENT_DAYS, Equity.DEFAULT_SETTLEMENT_TIME))

# For Equity Options with a cash account:
security.set_settlement_model(DelayedSettlementModel(Option.DEFAULT_SETTLEMENT_DAYS, Option.DEFAULT_SETTLEMENT_TIME))

# For remaining cases:
security.set_settlement_model(ImmediateSettlementModel())</pre>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The <code>AlpacaBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/margin-interest-rate/supported-models#02-Null-Model'>NullMarginInterestRateModel</a>.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The default market of the <code>AlpacaBrokerageModel</code> is <code>Market.USA</code> for Equity and Equity Options, and <code class="csharp">Market.Coinbase</code><code class="python">Market.COINBASE</code> for Crypto.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "metadata",
"values": {
"description": "This page explains the default security-level models and markets of the AlpacaBrokerageModel.",
"keywords": "brokerage model, fill model, slippage model, fee model, buying power model, margin model, settlement model, default markets",
"og:description": "This page explains the default security-level models and markets of the AlpacaBrokerageModel.",
"og:title": "Alpaca - Documentation QuantConnect.com",
"og:type": "website",
"og:site_name": "Alpaca - QuantConnect.com",
"og:image": "https://cdn.quantconnect.com/docs/i/writing-algorithms/reality-modeling/brokerages/supported-models/alpaca.png"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
<tbody>
<tr>
<td><code>BinanceBrokerageModel</code></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#05-Binance-Model'>BinanceFeeModel</a></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#04-Binance-Model'>BinanceFeeModel</a></td>
</tr>
<tr>
<td><code>BinanceFuturesBrokerageModel</code></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#06-Binance-Futures-Model'>BinanceFuturesFeeModel</a></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#05-Binance-Futures-Model'>BinanceFuturesFeeModel</a></td>
</tr>
<tr>
<td><code>BinanceCoinFuturesBrokerageModel</code></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#07-Binance-Coin-Futures-Model'>BinanceCoinFuturesFeeModel</a></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#06-Binance-Coin-Futures-Model'>BinanceCoinFuturesFeeModel</a></td>
</tr>
<tr>
<td><code>BinanceUSBrokerageModel</code></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#05-Binance-Model'>BinanceFeeModel</a></td>
<td><a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#04-Binance-Model'>BinanceFeeModel</a></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>The <code>BitfinexBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#08-Bitfinex-Model'>BitfinexFeeModel</a> with the default argument values that models the current Bitfinex fee schedule.</p>
<p>The <code>BitfinexBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#07-Bitfinex-Model'>BitfinexFeeModel</a> with the default argument values that models the current Bitfinex fee schedule.</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>The <code>BybitBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#09-Bybit-Model'>BybitFeeModel</a> for Crypto trades and the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#10-Bybit-Futures-Model'>BybitFuturesFeeModel</a> for Crypto Future trades. In both cases, the brokerage model uses the default argument values that models the Bybit fee schedule for VIP Level 0.</p>
<p>The <code>BybitBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#08-Bybit-Model'>BybitFeeModel</a> for Crypto trades and the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#09-Bybit-Futures-Model'>BybitFuturesFeeModel</a> for Crypto Future trades. In both cases, the brokerage model uses the default argument values that models the Bybit fee schedule for VIP Level 0.</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>The <code>CoinbaseBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#11-Coinbase-Model'>CoinbaseFeeModel</a>.</p>
<p>The <code>CoinbaseBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#10-Coinbase-Model'>CoinbaseFeeModel</a>.</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>The <code>InteractiveBrokersBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#03-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> with the default argument values. We model current Interactive Brokers fees on all assets.</p>
<p>The <code>InteractiveBrokersBrokerageModel</code> uses the <a href='/docs/v2/writing-algorithms/reality-modeling/transaction-fees/supported-models#11-Interactive-Brokers-Model'>InteractiveBrokersFeeModel</a> with the default argument values. We model current Interactive Brokers fees on all assets.</p>
7 changes: 7 additions & 0 deletions Resources/brokerages/alpaca/asset-classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p><?= $writingAlgorithms ? "The <code>AlpacaBrokerageModel</code>" : "Our Alpaca integration" ?> supports the following asset classes:</p>

<ul>
<li><a href='/docs/v2/writing-algorithms/securities/asset-classes/us-equity'>US Equities</a></li>
<li><a href='/docs/v2/writing-algorithms/securities/asset-classes/equity-options'>Equity Options</a></li>
<li><a href='/docs/v2/writing-algorithms/securities/asset-classes/crypto'>Crypto</a></li>
</ul>
110 changes: 110 additions & 0 deletions Resources/brokerages/alpaca/orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<h4>Order Types</h4>

<p>The following table describes the available order types for each asset class that <?= $cloudPlatform ? "our Alpaca integration" : "the <code>AlpacaBrokerageModel</code>" ?> supports:</p>

<table class="qc-table table" id='order-types-table'>
<thead>
<tr>
<th style='width: 40%'>Order Type</th>
<th style='width: 20%'>Equity</th>
<th style='width: 20%'>Equity Options</th>
<th style='width: 20%'>Crypto</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/market-orders'>Market</a></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
</tr>
<tr>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/limit-orders'>Limit</a></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
</tr>
<tr>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/stop-market-orders'>Stop market</a></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td></td>
</tr>
<tr>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/stop-limit-orders'>Stop limit</a></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
</tr>
<tr>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/trailing-stop-orders'>Trailing stop</a></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td><img src="https://cdn.quantconnect.com/i/tu/check.png" alt="green check" width="15px;"></td>
<td></td>
</tr>
</tbody>
</table>

<style>
#order-types-table td:not(:first-child),
#order-types-table th:not(:first-child) {
text-align: center;
}
</style>

<h4>Time In Force</h4>
<p><?= $writingAlgorithms ? "The <code>AlpacaBrokerageModel</code> supports" : "We model the Alpaca API by supporting" ?> the following <a href='/docs/v2/writing-algorithms/trading-and-orders/order-properties#03-Time-In-Force'>TimeInForce</a> instructions:</p>

<ul>
<li><code class="csharp">Day</code><code class="python">DAY</code></li>
<li><code class="csharp">GoodTilCanceled</code><code class="python">GOOD_TIL_CANCELED</code></li>
<li><code class="csharp">GoodTilDate</code><code class="python">good_til_date</code></li>
</ul>

<? if ($writingAlgorithms) { ?>
<div class="section-example-container">
<pre class="csharp">public override void Initialize()
{
// Set the default order properties
DefaultOrderProperties.TimeInForce = TimeInForce.GoodTilCanceled;
}

public override void OnData(Slice slice)
{
// Use default order order properties
LimitOrder(_symbol, quantity, limitPrice);

// Override the default order properties
LimitOrder(_symbol, quantity, limitPrice,
orderProperties: new TradeStationOrderProperties
{
TimeInForce = TimeInForce.Day
});
LimitOrder(_symbol, quantity, limitPrice,
orderProperties: new TradeStationOrderProperties
{
TimeInForce = TimeInForce.GoodTilDate(new DateTime(year, month, day))
});
}</pre>
<pre class="python">def initialize(self) -&gt; None:
# Set the default order properties
self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED

def on_data(self, slice: Slice) -&gt; None:
# Use default order order properties
self.limit_order(self._symbol, quantity, limit_price)

# Override the default order properties
order_properties = TradeStationOrderProperties()
order_properties.time_in_force = TimeInForce.DAY
self.limit_order(self._symbol, quantity, limit_price, order_properties=order_properties)

order_properties.time_in_force = TimeInForce.good_til_date(datetime(year, month, day))
self.limit_order(self._symbol, quantity, limit_price, order_properties=order_properties)</pre>
</div>
<? } ?>

<h4>Updates</h4>
<p><?= $writingAlgorithms ? "The <code>AlpacaBrokerageModel</code> supports" : "We model the Alpaca API by supporting" ?> <a href='/docs/v2/writing-algorithms/trading-and-orders/order-management/order-tickets#04-Update-Orders'>order updates</a>.</p>

<? include(DOCS_RESOURCES."/brokerages/handling-splits.html"); ?>

0 comments on commit a052ffe

Please sign in to comment.