fix: serialize datetime objects to ISO format for JSON storage
The signals contain datetime objects for created_at which can't be serialized to JSON directly. Convert them to ISO format strings before storing.
This commit is contained in:
@@ -39,6 +39,16 @@ def run_simulation_sync(
|
|||||||
# Run simulation (now synchronous - processes klines quickly)
|
# Run simulation (now synchronous - processes klines quickly)
|
||||||
results = await engine.run()
|
results = await engine.run()
|
||||||
|
|
||||||
|
# Serialize signals for JSON storage (convert datetime to string)
|
||||||
|
def serialize_signal(s):
|
||||||
|
created = s.get("created_at")
|
||||||
|
if hasattr(created, "isoformat"):
|
||||||
|
created = created.isoformat()
|
||||||
|
return {
|
||||||
|
**s,
|
||||||
|
"created_at": created
|
||||||
|
}
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
simulation = (
|
simulation = (
|
||||||
@@ -46,7 +56,7 @@ def run_simulation_sync(
|
|||||||
)
|
)
|
||||||
if simulation:
|
if simulation:
|
||||||
simulation.status = engine.status
|
simulation.status = engine.status
|
||||||
simulation.signals = engine.signals
|
simulation.signals = [serialize_signal(s) for s in engine.signals]
|
||||||
# Save klines for chart display (only time and close price)
|
# Save klines for chart display (only time and close price)
|
||||||
simulation.klines = [
|
simulation.klines = [
|
||||||
{"time": k.get("time"), "close": k.get("close")}
|
{"time": k.get("time"), "close": k.get("close")}
|
||||||
@@ -57,6 +67,10 @@ def run_simulation_sync(
|
|||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
for signal in engine.signals:
|
for signal in engine.signals:
|
||||||
|
created_at = signal.get("created_at")
|
||||||
|
if hasattr(created_at, "isoformat"):
|
||||||
|
created_at = created_at.isoformat()
|
||||||
|
|
||||||
db_signal = Signal(
|
db_signal = Signal(
|
||||||
id=signal["id"],
|
id=signal["id"],
|
||||||
bot_id=signal["bot_id"],
|
bot_id=signal["bot_id"],
|
||||||
@@ -67,7 +81,7 @@ def run_simulation_sync(
|
|||||||
confidence=signal.get("confidence"),
|
confidence=signal.get("confidence"),
|
||||||
reasoning=signal.get("reasoning"),
|
reasoning=signal.get("reasoning"),
|
||||||
executed=signal.get("executed", False),
|
executed=signal.get("executed", False),
|
||||||
created_at=signal["created_at"],
|
created_at=created_at,
|
||||||
)
|
)
|
||||||
db.add(db_signal)
|
db.add(db_signal)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user