Skip to content

Commit

Permalink
Merge pull request #610 from Lumiwealth/un-revert-backtesting-optimiz…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
grzesir authored Nov 5, 2024
2 parents ae5fcdf + e90c44f commit 899df67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lumibot/brokers/interactive_brokers_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,11 @@ def _pull_positions(self, strategy) -> list[Position]:
for position in positions:
# Create the Asset object for the position
symbol = position["contractDesc"]
if symbol.startswith("C"):
symbol = symbol[1:].replace(" ", "")
asset_class = ASSET_CLASS_MAPPING[position["assetClass"]]


# If asset class is stock, create a stock asset
if asset_class == Asset.AssetType.STOCK:
asset = Asset(symbol=symbol, asset_type=asset_class)
Expand All @@ -417,6 +420,7 @@ def _pull_positions(self, strategy) -> list[Position]:
right=right,
)
elif asset_class == Asset.AssetType.FUTURE:
#contract_details = self.data_source.get_contract_details(position['conid'])
expiry = position["expiry"]
multiplier = position["multiplier"]
asset = Asset(
Expand Down
56 changes: 28 additions & 28 deletions lumibot/data_sources/interactive_brokers_rest_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def __init__(self, config):
self.base_url = f"{self.api_url}/v1/api"

self.account_id = config["ACCOUNT_ID"] if "ACCOUNT_ID" in config else None
self.ib_username = config["IB_USERNAME"]
self.ib_password = config["IB_PASSWORD"]

# Check if we are running on a server
running_on_server = (
Expand All @@ -54,9 +52,9 @@ def __init__(self, config):
else:
self.running_on_server = False

self.start()
self.start(config["IB_USERNAME"], config["IB_PASSWORD"])

def start(self):
def start(self, ib_username, ib_password):
if not self.running_on_server:
# Run the Docker image with the specified environment variables and port mapping
if (
Expand All @@ -76,8 +74,8 @@ def start(self):

inputs_dir = "/srv/clientportal.gw/root/conf.yaml"
env_variables = {
"IBEAM_ACCOUNT": self.ib_username,
"IBEAM_PASSWORD": self.ib_password,
"IBEAM_ACCOUNT": ib_username,
"IBEAM_PASSWORD": ib_password,
"IBEAM_GATEWAY_BASE_URL": f"https://localhost:{self.port}",
"IBEAM_LOG_TO_FILE": False,
"IBEAM_REQUEST_RETRIES": 1,
Expand Down Expand Up @@ -466,21 +464,22 @@ def post_to_endpoint(self, url, json: dict, allow_fail=True):
return self.post_to_endpoint(url, json, allow_fail=allow_fail)

else:
if "error" in response.json():
logging.error(
colored(
f"Task '{url}' Failed. Error: {response.json()['error']}",
"red",
if allow_fail:
if "error" in response.json():
logging.error(
colored(
f"Task '{url}' Failed. Error: {response.json()['error']}",
"red",
)
)
)
else:
logging.error(
colored(
f"Task '{url}' Failed. Status code: {response.status_code}, "
f"Response: {response.text}",
"red",
else:
logging.error(
colored(
f"Task '{url}' Failed. Status code: {response.status_code}, "
f"Response: {response.text}",
"red",
)
)
)
to_return = None

except requests.exceptions.RequestException as e:
Expand Down Expand Up @@ -528,12 +527,13 @@ def delete_to_endpoint(self, url, allow_fail=True):
return self.delete_to_endpoint(url)

else:
logging.error(
colored(
f"Task '{url}' Failed. Status code: {response.status_code}, Response: {response.text}",
"red",
if allow_fail:
logging.error(
colored(
f"Task '{url}' Failed. Status code: {response.status_code}, Response: {response.text}",
"red",
)
)
)
to_return = None

except requests.exceptions.RequestException as e:
Expand Down Expand Up @@ -958,18 +958,18 @@ def get_historical_prices(

return bars

def get_last_price(self, asset, quote=None, exchange=None) -> float:
def get_last_price(self, asset, quote=None, exchange=None) -> float | None:
field = "last_price"
response = self.get_market_snapshot(asset, [field]) # TODO add exchange

if response is None or field not in response:
if asset.asset_type in ["option", "future"]:
logging.error(
logging.debug(
f"Failed to get {field} for asset {asset.symbol} with strike {asset.strike} and expiration date {asset.expiration}"
)
else:
logging.error(f"Failed to get {field} for asset {asset.symbol} of type {asset.asset_type}")
return -1
logging.debug(f"Failed to get {field} for asset {asset.symbol} of type {asset.asset_type}")
return None

price = response[field]

Expand Down

0 comments on commit 899df67

Please sign in to comment.