database/upsert_trade_example.py

48 lines
1.3 KiB
Python
Raw Normal View History

import pandas as pd
from database.trades import upsert
from datetime import datetime
from pytz import timezone
now = datetime.now().astimezone(timezone('US/Eastern'))
now = now.replace(second = 0, microsecond = 0, tzinfo = None)
trade_data = {
'Date': [now.date()],
'Symbol': ['SPX'],
'Strategy': ['Iron Condor'],
'Entry Time': [now],
'Exit Time': [None],
'Spreads': [
[
{
'Legs': [
{'Action': 'BUY', 'Strike': 150, 'Type': 'CALL'},
{'Action': 'SELL', 'Strike': 155, 'Type': 'CALL'}
],
'Open': 1.5
},
{
'Legs': [
{'Action': 'SELL', 'Strike': 160, 'Type': 'PUT'},
{'Action': 'BUY', 'Strike': 155, 'Type': 'PUT'}
],
'Open': 2.5
}
]
],
'Profit': [None]
}
upsert(pd.DataFrame(trade_data))
trade_data['Exit Time'] = [pd.to_datetime('2024-01-22 16:00:00')]
trade_data['Spreads'][0][0]['High'] = 1.5
trade_data['Spreads'][0][0]['Low'] = 0.0
trade_data['Spreads'][0][0]['Close'] = 0.0
trade_data['Spreads'][0][1]['High'] = 2.5
trade_data['Spreads'][0][1]['Low'] = 0.0
trade_data['Spreads'][0][1]['Close'] = 0.0
trade_data['Profit'] = [400.0]
upsert(pd.DataFrame(trade_data))