Supported Models

Alpaca

Introduction

This page explains the AlpacaBrokerageModel, including the asset classes it supports, its default security-level models, and it's default markets.

SetBrokerageModel(BrokerageName.Alpaca, AccountType.Cash);
SetBrokerageModel(BrokerageName.Alpaca, AccountType.Margin);
self.set_brokerage_model(BrokerageName.ALPACA, AccountType.CASH)
self.set_brokerage_model(BrokerageName.ALPACA, AccountType.MARGIN)

To view the implementation of this model, see the LEAN GitHub repository.

Asset Classes

The AlpacaBrokerageModel supports the following asset classes:

Orders

The AlpacaBrokerageModel supports several order types, order properties, and most order updates.

Order Types

The following table describes the available order types for each asset class that the AlpacaBrokerageModel supports:

Order TypeEquityEquity OptionsCrypto
Marketgreen checkgreen checkgreen check
Limitgreen checkgreen checkgreen check
Stop marketgreen checkgreen check
Stop limitgreen checkgreen checkgreen check
Trailing stopgreen checkgreen check
Market on Opengreen check
Market on Closegreen check

Time In Force

The AlpacaBrokerageModel supports the following TimeInForce instructions:

  • DayDAY
  • GoodTilCanceledGOOD_TIL_CANCELED
  • GoodTilDategood_til_date
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))
               });
}
def initialize(self) -> None:
    # Set the default order properties
    self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED

def on_data(self, slice: Slice) -> 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)

Updates

The AlpacaBrokerageModel supports order updates.

Handling Splits

If you're using raw data normalization and you have active orders with a limit, stop, or trigger price in the market for a US Equity when a stock split occurs, the following properties of your orders automatically adjust to reflect the stock split:

  • Quantity
  • Limit price
  • Stop price
  • Trigger price

Fills

The AlpacaBrokerageModel uses the EquityFillModel for Equity trades and the ImmediateFillModel for Option and Crypto trades.

Slippage

The AlpacaBrokerageModel uses the NullSlippageModel.

Fees

The AlpacaBrokerageModel uses the AlpacaFeeModel with the default argument values. We model current Alpaca fees on all assets.

Buying Power

The AlpacaBrokerageModel uses the CashBuyingPowerModel for cash accounts, the SecurityMarginModel for margin accounts, and the OptionMarginModel for Option trades.

If you have a margin account, the AlpacaBrokerageModel allows 2x leverage for Equities and 3x leverage for Crypto.

Settlement

The AlpacaBrokerageModel uses the ImmediateSettlementModel for margin accounts and the DelayedSettlementModel with the default settlement rules for cash accounts with Equity and Equity Options.

// 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());
# 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())

Margin Interest Rate

The AlpacaBrokerageModel uses the NullMarginInterestRateModel.

Default Markets

The default market of the AlpacaBrokerageModel is Market.USA for Equity and Equity Options. It's Market.CoinbaseMarket.COINBASE for Crypto.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: