-
Notifications
You must be signed in to change notification settings - Fork 0
/
trading_client.py
214 lines (179 loc) · 9.79 KB
/
trading_client.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from polygon import RESTClient
from config import POLYGON_API_KEY, FINANCIAL_PREP_API_KEY, MONGO_DB_USER, MONGO_DB_PASS, API_KEY, API_SECRET, BASE_URL
import json
import certifi
from urllib.request import urlopen
from zoneinfo import ZoneInfo
from pymongo import MongoClient
import time
from datetime import datetime, timedelta
from helper_files.client_helper import place_order, get_ndaq_tickers, market_status, strategies, get_latest_price
from alpaca.trading.client import TradingClient
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
from alpaca.data.historical.stock import StockHistoricalDataClient
from alpaca.trading.requests import MarketOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
from strategies.trading_strategies_v1 import get_historical_data
import yfinance as yf
import logging
from collections import Counter
from statistics import median, mode
import statistics
import heapq
# MongoDB connection string
mongo_url = f"mongodb+srv://{MONGO_DB_USER}:{MONGO_DB_PASS}@cluster0.0qoxq.mongodb.net"
# Set up logging configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler('system.log'), # Log messages to a file
logging.StreamHandler() # Log messages to the console
]
)
def weighted_majority_decision_and_median_quantity(decisions_and_quantities):
"""
Determines the majority decision (buy, sell, or hold) and returns the weighted median quantity for the chosen action.
Groups 'strong buy' with 'buy' and 'strong sell' with 'sell'.
Applies weights to quantities based on strategy coefficients.
"""
buy_decisions = ['buy', 'strong buy']
sell_decisions = ['sell', 'strong sell']
weighted_buy_quantities = []
weighted_sell_quantities = []
buy_weight = 0
sell_weight = 0
hold_weight = 0
# Process decisions with weights
for decision, quantity, weight in decisions_and_quantities:
if decision in buy_decisions:
weighted_buy_quantities.extend([quantity])
buy_weight += weight
elif decision in sell_decisions:
weighted_sell_quantities.extend([quantity])
sell_weight += weight
elif decision == 'hold':
hold_weight += weight
# Determine the majority decision based on the highest accumulated weight
if buy_weight > sell_weight and buy_weight > hold_weight:
return 'buy', median(weighted_buy_quantities) if weighted_buy_quantities else 0, buy_weight, sell_weight, hold_weight
elif sell_weight > buy_weight and sell_weight > hold_weight:
return 'sell', median(weighted_sell_quantities) if weighted_sell_quantities else 0, buy_weight, sell_weight, hold_weight
else:
return 'hold', 0, buy_weight, sell_weight, hold_weight
def main():
"""
Main function to control the workflow based on the market's status.
"""
ndaq_tickers = []
early_hour_first_iteration = True
post_hour_first_iteration = True
client = RESTClient(api_key=POLYGON_API_KEY)
trading_client = TradingClient(API_KEY, API_SECRET)
data_client = StockHistoricalDataClient(API_KEY, API_SECRET)
mongo_client = MongoClient(mongo_url)
db = mongo_client.trades
asset_collection = db.assets_quantities
strategy_to_coefficient = {}
while True:
client = RESTClient(api_key=POLYGON_API_KEY)
trading_client = TradingClient(API_KEY, API_SECRET)
data_client = StockHistoricalDataClient(API_KEY, API_SECRET)
status = market_status(client) # Use the helper function for market status
db = mongo_client.trades
asset_collection = db.assets_quantities
market_db = mongo_client.market_data
market_collection = market_db.market_status
market_collection.update_one({}, {"$set": {"market_status": status}})
if status == "open":
logging.info("Market is open. Waiting for 60 seconds.")
if not ndaq_tickers:
ndaq_tickers = get_ndaq_tickers(mongo_url, FINANCIAL_PREP_API_KEY) # Fetch tickers using the helper function
sim_db = mongo_client.trading_simulator
rank_collection = sim_db.rank
r_t_c_collection = sim_db.rank_to_coefficient
for strategy in strategies:
rank = rank_collection.find_one({'strategy': strategy.__name__})['rank']
coefficient = r_t_c_collection.find_one({'rank': rank})['coefficient']
strategy_to_coefficient[strategy.__name__] = coefficient
early_hour_first_iteration = False
post_hour_first_iteration = True
account = trading_client.get_account()
buy_heap = []
for ticker in ndaq_tickers:
decisions_and_quantities = []
try:
trading_client = TradingClient(API_KEY, API_SECRET)
account = trading_client.get_account()
buying_power = float(account.cash)
portfolio_value = float(account.portfolio_value)
cash_to_portfolio_ratio = buying_power / portfolio_value
historical_data = get_historical_data(ticker, data_client)
ticker_yahoo = yf.Ticker(ticker)
data = ticker_yahoo.history()
current_price = get_latest_price(ticker)
asset_info = asset_collection.find_one({'symbol': ticker})
portfolio_qty = asset_info['quantity'] if asset_info else 0.0
"""
use weight from each strategy to determine how much each decision will be weighed. weights will be in decimal
"""
for strategy in strategies:
decision, quantity, _ = strategy(ticker, current_price, historical_data,
buying_power, portfolio_qty, portfolio_value)
weight = strategy_to_coefficient[strategy.__name__]
decisions_and_quantities.append((decision, quantity, weight))
decision, quantity, buy_weight, sell_weight, hold_weight = weighted_majority_decision_and_median_quantity(decisions_and_quantities)
print(f"Ticker: {ticker}, Decision: {decision}, Quantity: {quantity}, Weights: Buy: {buy_weight}, Sell: {sell_weight}, Hold: {hold_weight}")
"""
later we should implement buying_power regulator depending on vix strategy
for now in bull: 15000
for bear: 5000
"""
if decision == "buy" and float(account.cash) > 15000 and (current_price(ticker) * (quantity + portfolio_qty))/portfolio_value < 0.05:
heapq.heappush(buy_heap, (-(buy_weight-sell_weight), quantity, ticker))
elif decision == "sell" and portfolio_qty > 0:
order = place_order(trading_client, ticker, OrderSide.SELL, qty=quantity, mongo_url=mongo_url) # Place order using helper
logging.info(f"Executed SELL order for {ticker}: {order}")
else:
logging.info(f"Holding for {ticker}, no action taken.")
except Exception as e:
logging.error(f"Error processing {ticker}: {e}")
while buy_heap and float(account.cash) > 15000:
try:
buy_coeff, quantity, ticker = heapq.heappop(buy_heap)
print(f"buy_coeff: {buy_coeff}, quantity: {quantity}, ticker: {ticker}")
order = place_order(trading_client, ticker, OrderSide.BUY, qty=quantity, mongo_url=mongo_url) # Place order using helper
logging.info(f"Executed BUY order for {ticker}: {order}")
trading_client = TradingClient(API_KEY, API_SECRET)
account = trading_client.get_account()
except:
print("Error occurred while executing buy order. Continuing...")
break
print("Sleeping for 30 seconds...")
time.sleep(30)
elif status == "early_hours":
if early_hour_first_iteration:
ndaq_tickers = get_ndaq_tickers(mongo_url, FINANCIAL_PREP_API_KEY)
sim_db = mongo_client.trading_simulator
rank_collection = sim_db.rank
r_t_c_collection = sim_db.rank_to_coefficient
for strategy in strategies:
rank = rank_collection.find_one({'strategy': strategy.__name__})['rank']
coefficient = r_t_c_collection.find_one({'rank': rank})['coefficient']
strategy_to_coefficient[strategy.__name__] = coefficient
early_hour_first_iteration = False
post_hour_first_iteration = True
logging.info("Market is in early hours. Waiting for 60 seconds.")
time.sleep(30)
elif status == "closed":
if post_hour_first_iteration:
early_hour_first_iteration = True
post_hour_first_iteration = False
logging.info("Market is closed. Performing post-market operations.")
time.sleep(30)
else:
logging.error("An error occurred while checking market status.")
time.sleep(60)
if __name__ == "__main__":
main()