-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrade_logs.py
59 lines (50 loc) · 1.86 KB
/
trade_logs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import pymongo
import pandas as pd
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
URI = os.getenv('MONGO_URI')
DB_NAME = os.getenv('DATABASE_NAME')
COLL_NAME = os.getenv('MARKET_LOG')
client = pymongo.MongoClient(URI)
db = client[DB_NAME]
collection = db[COLL_NAME]
data = collection.find()
# print("Successfully exported training data")
def log_trade(timestamp, stock, initial_funds, initial_price, trade_price,
initial_quantity, quantity_traded, trade_type):
total_cost = trade_price * quantity_traded
remaining_quantity = initial_quantity
funds_remaining = initial_funds
if trade_type.lower() == "buy":
remaining_quantity = initial_quantity + quantity_traded
funds_remaining = initial_funds - total_cost
elif trade_type.lower() == "sell":
remaining_quantity = initial_quantity - quantity_traded
funds_remaining = initial_funds + total_cost
log_message = (
f"{trade_type} {quantity_traded} shares of {stock} at ${trade_price:.2f} per share. "
f"Initial quantity: {initial_quantity:.2f}. "
f"Remaining quantity: {remaining_quantity:.2f}."
)
document = {"timestamp": timestamp,
"initial_funds": initial_funds,
"initial_price": initial_price,
"trade_price": trade_price,
"initial_quantity": initial_quantity,
"quantity_traded": quantity_traded,
"remaining_quantity": remaining_quantity,
"funds_remaining": funds_remaining,
"log_message": log_message}
collection.insert_one(document)
print("Executed trade: ", log_message)
# timestamp = datetime.now()
# stock = "AAPL"
# initial_price = 150.0
# trade_price = 152.0
# initial_funds = 10000.0
# quantity = 10
# initial_quantity = 2234
# trade_type = "sell"
# log_trade(timestamp, stock, initial_funds, trade_price, initial_quantity, quantity, trade_type)