Skip to content

Commit

Permalink
bug fixes for sending data to server + deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed Nov 5, 2024
1 parent 48ab572 commit d51fa06
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lumibot/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ def to_dict(self):
if isinstance(value, datetime.datetime):
order_dict[key] = value.isoformat()

# If it is a Decimal object, convert it to a float
elif isinstance(value, Decimal):
order_dict[key] = float(value)

# Recursively handle objects that have their own to_dict method (like asset, quote, etc.)
elif hasattr(value, "to_dict"):
order_dict[key] = value.to_dict()
Expand Down
6 changes: 3 additions & 3 deletions lumibot/entities/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ def to_dict(self):
return {
"strategy": self.strategy,
"asset": self.asset.to_dict(),
"quantity": self.quantity,
"quantity": float(self.quantity),
"orders": [order.to_dict() for order in self.orders],
"hold": self.hold,
"available": self.available,
"avg_fill_price": self.avg_fill_price,
"available": float(self.available) if self.available else None,
"avg_fill_price": float(self.avg_fill_price) if self.avg_fill_price else None,
}

@classmethod
Expand Down
25 changes: 22 additions & 3 deletions lumibot/strategies/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from typing import Union
from sqlalchemy import create_engine, inspect, text, bindparam
from sqlalchemy.exc import OperationalError
import traceback
import math

import jsonpickle
import matplotlib
Expand Down Expand Up @@ -4613,11 +4615,28 @@ def send_update_to_cloud(self):
"orders": [order.to_dict() for order in orders],
}

# Helper function to recursively replace NaN in dictionaries
def replace_nan(value):
if isinstance(value, float) and math.isnan(value):
return None # or 0 if you prefer
elif isinstance(value, dict):
return {k: replace_nan(v) for k, v in value.items()}
elif isinstance(value, list):
return [replace_nan(v) for v in value]
else:
return value

# Apply to your data dictionary
data = replace_nan(data)

try:
# Send the data to the cloud
response = requests.post(LUMIWEALTH_URL, headers=headers, json=data)
json_data = json.dumps(data)
response = requests.post(LUMIWEALTH_URL, headers=headers, data=json_data)
except Exception as e:
self.logger.error(f"Failed to send update to the cloud. Error: {e}")
self.logger.error(f"Failed to send update to the cloud because of lumibot error. Error: {e}")
# Add the traceback to the log
self.logger.error(traceback.format_exc())
return False

# Check if the message was sent successfully
Expand All @@ -4626,7 +4645,7 @@ def send_update_to_cloud(self):
return True
else:
self.logger.error(
f"Failed to send update to the cloud. Status code: {response.status_code}, message: {response.text}"
f"Failed to send update to the cloud because of cloud error. Status code: {response.status_code}, message: {response.text}"
)
return False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="lumibot",
version="3.8.4",
version="3.8.5",
author="Robert Grzesik",
author_email="[email protected]",
description="Backtesting and Trading Library, Made by Lumiwealth",
Expand Down

0 comments on commit d51fa06

Please sign in to comment.