Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into ib-retry-policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Al4ise committed Nov 21, 2024
2 parents 97581cb + 160f534 commit 4b2e761
Show file tree
Hide file tree
Showing 16 changed files with 1,454 additions and 534 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_bot.py
lumi_tradier
lumiwealth_tradier
ThetaTerminal.jar
pytest.ini

# Pypi deployment
build
Expand Down
2 changes: 2 additions & 0 deletions lumibot/backtesting/backtesting_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ def _process_cash_settlement(self, order, price, quantity):

def submit_order(self, order):
"""Submit an order for an asset"""
self._conform_order(order)

# NOTE: This code is to address Tradier API requirements, they want is as "to_open" or "to_close" instead of just "buy" or "sell"
# If the order has a "buy_to_open" or "buy_to_close" side, then we should change it to "buy"
if order.is_buy_order():
Expand Down
27 changes: 27 additions & 0 deletions lumibot/brokers/alpaca.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from lumibot.data_sources import AlpacaData
from lumibot.entities import Asset, Order, Position
from lumibot.tools.helpers import has_more_than_n_decimal_places

from .broker import Broker

Expand Down Expand Up @@ -467,6 +468,32 @@ def _submit_order(self, order):

return order

def _conform_order(self, order):
"""Conform an order to Alpaca's requirements
See: https://docs.alpaca.markets/docs/orders-at-alpaca
"""
if order.asset.asset_type == "stock" and order.type == "limit":
"""
The minimum price variance exists for limit orders.
Orders received in excess of the minimum price variance will be rejected.
Limit price >=$1.00: Max Decimals = 2
Limit price <$1.00: Max Decimals = 4
"""
orig_price = order.limit_price
conformed = False
if order.limit_price >= 1.0 and has_more_than_n_decimal_places(order.limit_price, 2):
order.limit_price = round(order.limit_price, 2)
conformed = True
elif order.limit_price < 1.0 and has_more_than_n_decimal_places(order.limit_price, 4):
order.limit_price = round(order.limit_price, 4)
conformed = True

if conformed:
logging.warning(
f"Order {order} was changed to conform to Alpaca's requirements. "
f"The limit price was changed from {orig_price} to {order.limit_price}."
)

def cancel_order(self, order):
"""Cancel an order
Expand Down
7 changes: 6 additions & 1 deletion lumibot/brokers/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,9 +958,14 @@ def _pull_all_orders(self, strategy_name, strategy_object):
return result

def submit_order(self, order):
"""Submit an order for an asset"""
"""Conform an order for an asset to broker constraints and submit it."""
self._conform_order(order)
self._submit_order(order)

def _conform_order(self, order):
"""Conform an order to broker constraints. Derived brokers should implement this method."""
pass

def submit_orders(self, orders, **kwargs):
"""Submit orders"""
self._submit_orders(orders, **kwargs)
Expand Down
Empty file added lumibot/components/__init__.py
Empty file.
Loading

0 comments on commit 4b2e761

Please sign in to comment.