This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrisk_strategy.py
More file actions
1431 lines (1167 loc) · 59.8 KB
/
risk_strategy.py
File metadata and controls
1431 lines (1167 loc) · 59.8 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import alpaca_trade_api as tradeapi
import requests
import json
from trade_stats import download_trades
from alpha_vantage.timeseries import TimeSeries
from alpha_vantage.cryptocurrencies import CryptoCurrencies
from alpaca_trade_api.rest import REST, APIError
from datetime import datetime, timedelta
from port_op import optimize_portfolio
import os
from dotenv import load_dotenv
import logging
import pandas as pd
import numpy as np
from scipy.stats import norm
from pymongo import MongoClient
# Setup logging
logging.basicConfig(filename='logfile.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Load environment variables
load_dotenv()
MONGO_CONN_STRING = os.getenv('MONGO_DB_CONN_STRING')
# Alpaca credentials
ALPACA_API_KEY = os.getenv('ALPACA_API_KEY')
ALPACA_SECRET_KEY = os.getenv('ALPACA_SECRET_KEY')
ALPACA_BASE_URL = 'https://paper-api.alpaca.markets' # or https://api.alpaca.markets for live trading
ALPHA_VANTAGE_API = os.getenv('ALPHA_VANTAGE_API')
# Connect to MongoDB
client = MongoClient(MONGO_CONN_STRING)
alpha_vantage_ts = TimeSeries(key=ALPHA_VANTAGE_API, output_format='pandas')
alpha_vantage_crypto = CryptoCurrencies(key=ALPHA_VANTAGE_API, output_format='pandas')
api = tradeapi.REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=ALPACA_BASE_URL)
account = api.get_account()
equity = float(account.equity)
def load_risk_params():
current_script_dir = os.path.dirname(os.path.abspath(__file__))
risk_params_file_path = os.path.join(current_script_dir, 'risk_params.json')
if not os.path.exists(risk_params_file_path):
risk_params_file_path = os.path.join(current_script_dir, '..', 'risk_params.json')
risk_params_file_path = os.path.normpath(risk_params_file_path)
if os.path.exists(risk_params_file_path):
with open(risk_params_file_path, 'r') as f:
risk_params = json.load(f)
# print("Risk parameters loaded:", risk_params)
# logging.info("Risk parameters loaded successfully.")
return risk_params
else:
logging.error(f"Error: 'risk_params.json' not found at {risk_params_file_path}")
return None
risk_params = load_risk_params()
if risk_params:
print("Risk parameters loaded successfully.")
# Continue with your script using the loaded risk_params
else:
print("Failed to load risk parameters.")
print(risk_params['max_position_size'])
class CryptoAsset:
def __init__(self, symbol, quantity, value_usd):
self.symbol = symbol
self.quantity = quantity
self.value_usd = value_usd
self.value_24h_ago = None # To store the value 24 hours ago
# Connect to MongoDB and retrieve unique crypto pairs
self.crypto_symbols = self.get_unique_crypto_pairs()
def get_unique_crypto_pairs(self):
# Connect to the MongoDB database
db = client.stock_data
collection = db.crypto_data
# Retrieve unique pairs
pipeline = [
{"$group": {"_id": {"Crypto": "$Crypto", "Quote": "$Quote"}}},
{"$project": {"_id": 0, "pair": {"$concat": ["$_id.Crypto", "/", "$_id.Quote"]}}}
]
results = collection.aggregate(pipeline)
unique_pairs = [doc['pair'] for doc in results]
return unique_pairs
def profit_loss_24h(self):
if self.value_24h_ago is not None:
return (self.value_usd - self.value_24h_ago) / self.value_24h_ago * 100
else:
return None
# Define a class to manage the portfolio
class PortfolioManager:
def __init__(self, api):
self.api = api
self.assets = {}
self.operations = 0 # track the number of operations
def increment_operations(self):
self.operations += 1
def add_asset(self, symbol, quantity, value_usd):
self.assets[symbol] = CryptoAsset(symbol, quantity, value_usd)
def update_asset_value(self, symbol, value_usd):
if symbol in self.assets:
self.assets[symbol].value_usd = value_usd
def portfolio_value(self):
return sum(asset.value_usd for asset in self.assets.values())
def portfolio_balance(self):
return {symbol: (asset.value_usd / self.portfolio_value()) * 100 for symbol, asset in self.assets.items()}
def sell_decision(self, symbol):
balance = self.portfolio_balance()
if balance[symbol] > 25 or balance[symbol] > 0.4 * sum(balance.values()):
return True
else:
return False
def scale_out(self, symbol):
quantity_to_sell = int(self.assets[symbol].quantity * 0.1) # Sell 10% of holdings
return quantity_to_sell
def update_asset_values_24h(self):
for asset in self.assets.values():
asset.value_24h_ago = asset.value_usd
### Usable functions for the RiskManagement class below
def get_exchange_rate(base_currency, quote_currency):
# Your Alpha Vantage API key
api_key = ALPHA_VANTAGE_API
# Prepare the URL
url = f"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency={base_currency}&to_currency={quote_currency}&apikey={api_key}"
# Send GET request
response = requests.get(url)
# Parse JSON response
data = json.loads(response.text)
# Extract exchange rate
exchange_rate = data["Realtime Currency Exchange Rate"]["5. Exchange Rate"]
return float(exchange_rate)
def fetch_account_details():
url = "https://paper-api.alpaca.markets/v2/account"
headers = {
"accept": "application/json",
"APCA-API-KEY-ID": ALPACA_API_KEY,
"APCA-API-SECRET-KEY": ALPACA_SECRET_KEY
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
logging.error(f"Failed to fetch account details: {response.text}")
return None
def black_scholes(S, K, T, r, sigma, option_type="call"):
d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
if option_type == "call":
return S * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
else:
return K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
def calculate_greeks(S, K, T, r, sigma, option_type):
d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
return {
'delta': norm.cdf(d1) if option_type == "call" else -norm.cdf(-d1),
'gamma': norm.pdf(d1) / (S * sigma * np.sqrt(T)),
'theta': -(S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) - r * K * np.exp(-r * T) * norm.cdf(d2) if option_type == "call" else -(S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) + r * K * np.exp(-r * T) * norm.cdf(-d2),
'vega': S * norm.pdf(d1) * np.sqrt(T)
}
## RiskManagement class developed for usage on the account
class RiskManagement:
crypto_symbols = ['AAVE/USD', 'ALGO/USD', 'AVAX/USD', 'BCH/USD', 'BTC/USD', 'ETH/USD',
'LINK/USD', 'LTC/USD', 'TRX/USD', 'UNI/USD', 'USDT/USD', 'SHIB/USD']
def __init__(self, api, risk_params):
self.api = api
self.risk_params = risk_params
self.alpha_vantage_crypto = CryptoCurrencies(key=ALPHA_VANTAGE_API, output_format='pandas')
self.manager = PortfolioManager(api)
self.crypto_value = 0
self.commodity_value = 0
self.crypto_value = 0
self.commodity_value = 0
self.options_crypto_notional_value = 0
self.options_commodity_notional_value = 0
self.max_options_allocation = 0.2 # Maximum 20% of portfolio allocated to options
self.max_options_loss_threshold = 0.1
self.crypto_symbols = ['AAVE/USD', 'ALGO/USD', 'AVAX/USD', 'BCH/USD', 'BTC/USD', 'ETH/USD',
'LINK/USD', 'LTC/USD', 'TRX/USD', 'UNI/USD', 'USDT/USD', 'SHIB/USD']
self.TARGET_ALLOCATION = {
'options': 0.20, # 20%
'crypto': 0.30, # 30%
'equities': 0.50 # 50%
}
self.initialize_account_info()
def initialize_account_info(self):
account = self.api.get_account()
self.peak_portfolio_value = float(account.cash)
def calculate_current_allocation(self):
positions = self.api.list_positions()
total_value = sum(float(position.market_value) for position in positions)
allocation = {
'options': 0,
'crypto': 0,
'equities': 0
}
def calculate_options_allocation(self):
positions = self.api.list_positions()
options_value = sum(float(position.market_value) for position in positions if 'OPT' in position.symbol)
account_value = float(self.api.get_account().portfolio_value)
return options_value / account_value
def calculate_portfolio_value(self):
positions = self.api.list_positions()
portfolio_value = sum(float(position.market_value) for position in positions)
return portfolio_value
def calculate_current_allocation(self):
positions = self.api.list_positions()
total_value = sum(float(position.market_value) for position in positions)
allocation = {
'options': 0,
'crypto': 0,
'equities': 0
}
for position in positions:
symbol = position.symbol
market_value = float(position.market_value)
if 'OPT' in symbol:
allocation['options'] += market_value
elif symbol.endswith('USD'):
allocation['crypto'] += market_value
else:
allocation['equities'] += market_value
for asset_class in allocation:
if total_value != 0:
allocation[asset_class] /= total_value
return allocation
def rebalance_portfolio(self):
current_allocation = self.calculate_current_allocation()
account = self.api.get_account()
total_value = float(account.portfolio_value)
for asset_class, target_pct in self.TARGET_ALLOCATION.items():
current_pct = current_allocation[asset_class]
diff_pct = target_pct - current_pct
amount_to_trade = diff_pct * total_value
if amount_to_trade > 0:
self.buy_asset_class(asset_class, amount_to_trade)
elif amount_to_trade < 0:
self.sell_asset_class(asset_class, -amount_to_trade)
def buy_asset_class(self, asset_class, amount_to_trade):
account_details = fetch_account_details()
if not account_details:
return
available_cash = float(account_details['cash'])
if asset_class == 'crypto':
for symbol in self.crypto_symbols:
current_price = self.get_current_price(symbol)
if current_price:
qty = self.calculate_quantity(symbol)
print(f'Amount to Trade: {amount_to_trade} and suggest calc qty was: {qty}')
if self.validate_trade(symbol, 'buy'):
for attempt in range(3): # Retry mechanism
try:
self.api.submit_order(
symbol=symbol,
qty=qty,
side='buy',
type='market',
time_in_force='gtc'
)
logging.info(f"Bought {qty} of {symbol} to rebalance crypto allocation.")
break
except tradeapi.rest.APIError as e:
if 'asset is not active' in str(e):
logging.warning(f"Skipping {symbol} because it is not active.")
break
elif 'insufficient balance' in str(e):
logging.warning(f"Retrying {symbol} due to insufficient balance.")
continue
else:
logging.error(f"Failed to submit order for {symbol}: {e}")
break
elif asset_class == 'options':
pass
elif asset_class == 'equities':
pass
def sell_asset_class(self, asset_class, amount_to_trade):
if asset_class == 'crypto':
for symbol in self.crypto_symbols:
current_price = self.get_current_price(symbol)
if current_price:
qty = self.calculate_quantity(symbol) # Calculate quantity using the appropriate method
if self.validate_trade(symbol, 'sell'):
try:
self.api.submit_order(
symbol=symbol,
qty=qty,
side='sell',
type='market',
time_in_force='gtc'
)
logging.info(f"Sold {qty} of {symbol} to rebalance crypto allocation.")
break
except tradeapi.rest.APIError as e:
if 'asset is not active' in str(e):
logging.warning(f"Skipping {symbol} because it is not active.")
continue
else:
logging.error(f"Failed to submit order for {symbol}: {e}")
raise e
elif asset_class == 'options':
pass
elif asset_class == 'equities':
pass
def update_max_crypto_equity(self):
# Get the current buying power of the account
account = self.api.get_account()
buying_power = float(account.buying_power)
# Compute max_crypto_equity
max_crypto_equity = buying_power
# Update the JSON file with the new value
self.risk_params['max_crypto_equity'] = max_crypto_equity
return max_crypto_equity
def check_options_risk(self, symbol, quantity, price):
total_portfolio_value = float(self.api.get_account().portfolio_value)
options_position_value = quantity * price
options_allocation = options_position_value / total_portfolio_value
if options_allocation > self.max_options_allocation:
return False
# Check individual options position loss
if symbol in self.options_positions:
entry_price = self.options_positions[symbol]['entry_price']
if (price - entry_price) / entry_price < -self.max_options_loss_threshold:
return False
return True
def monitor_options_expiration(self):
positions = self.api.list_positions()
current_date = datetime.now().date()
for position in positions:
if 'OPT' in position.symbol:
expiration_date = position.expiration_date.date()
days_to_expiration = (expiration_date - current_date).days
if days_to_expiration <= 7:
# Close options position if it's within 7 days of expiration
self.close_position(position.symbol)
print(f"Closed options position {position.symbol} due to approaching expiration.")
def monitor_volatility(self):
# Retrieve market volatility data (e.g., VIX index)
volatility = self.get_market_volatility()
if volatility > self.risk_params['max_volatility_threshold']:
# Adjust options positions based on high volatility
self.adjust_options_positions(volatility)
print(f"Adjusted options positions due to high market volatility: {volatility}")
def calculate_position_greeks(self, position):
# Extract the necessary parameters from the position object
S = float(position.current_price)
K = float(position.strike_price)
T = (position.expiration_date - datetime.now()).days / 365 # Time to expiration in years
r = 0.01 # Risk-free rate (adjust as needed)
sigma = 0.20 # Volatility (adjust as needed)
option_type = position.option_type.lower()
# Calculate the Greeks
greeks = calculate_greeks(S, K, T, r, sigma, option_type)
return greeks
def calculate_equity_allocation(self, asset_type='crypto'):
risk_params = load_risk_params()
if not risk_params:
raise ValueError("Failed to load risk parameters.")
max_crypto_equity = risk_params.get('max_crypto_equity', 0)
max_equities_equity = risk_params.get('max_equity_equity', 0)
equity = float(self.api.get_account().equity)
positions = self.api.list_positions()
total_crypto_value = sum(float(position.market_value) for position in positions if position.symbol.endswith('USD'))
total_equities_value = sum(float(position.market_value) for position in positions if not position.symbol.endswith('USD'))
total_options_value = sum(float(position.market_value) for position in positions if 'OPT' in position.symbol)
print(f"Risk parameters loaded: {risk_params}")
print(f"Total crypto value: {total_crypto_value}, Max crypto equity: {max_crypto_equity}")
print(f"Total equities value: {total_equities_value}, Max equities equity: {max_equities_equity}")
if asset_type == 'crypto':
return max_crypto_equity
elif asset_type == 'equity':
return max_equities_equity
elif asset_type == 'options':
max_total_allocation = max_crypto_equity + max_equities_equity
remaining_allocation = max_total_allocation - (total_crypto_value + total_equities_value + total_options_value)
return remaining_allocation if remaining_allocation > 0 else 0
else:
raise ValueError("Invalid asset type specified. Choose either 'crypto', 'equity', or 'options'.")
def optimize_portfolio(self, risk_aversion):
# Get historical data for each symbol
historical_data = {}
for symbol in self.crypto_symbols:
data, _ = alpha_vantage_crypto.get_digital_currency_daily(symbol=symbol, market='USD')
historical_data[symbol] = data['4b. close (USD)']
# Calculate expected returns and covariance matrix
returns_data = pd.DataFrame(historical_data).pct_change()
expected_returns = returns_data.mean()
covariance_matrix = returns_data.cov()
# Total investment amount
total_investment = float(self.api.get_account().equity)
# Run optimization in separate script
quantities_to_purchase = optimize_portfolio(expected_returns, covariance_matrix, risk_aversion,
total_investment)
return quantities_to_purchase
def get_daily_returns(self, symbol: str, days: int = 3) -> float:
url = "https://paper-api.alpaca.markets/v2/positions"
headers = {
"accept": "application/json",
"APCA-API-KEY-ID": ALPACA_API_KEY,
"APCA-API-SECRET-KEY": ALPACA_SECRET_KEY
}
response = requests.get(url, headers=headers)
data = response.json()
# Find the position for the given symbol
position_data = None
for position in data:
if position['symbol'] == symbol:
position_data = position
break
if position_data is None:
raise ValueError(f"No position found for symbol {symbol}")
# Get the closing prices for the past `days` days
closing_prices = []
for _ in range(days):
lastday_price = position_data.get('lastday_price')
if lastday_price is not None:
closing_prices.append(float(lastday_price))
else:
print(f"Missing lastday_price for symbol {symbol}. Skipping calculation of daily returns.")
return []
# Calculate the daily returns
returns = [np.log(closing_prices[i] / closing_prices[i - 1]) for i in range(1, len(closing_prices))]
# Return the daily returns
return returns
def get_position(self, symbol):
"""
Get position details for a specific symbol
"""
positions = self.api.list_positions()
# Filter positions to find matches for the symbol
symbol_positions = [p for p in positions if p.symbol == symbol]
if not symbol_positions:
print(f"No positions found for {symbol}")
return None
# Assuming there's only one position per symbol
p = symbol_positions[0]
# Get actual qty and unsettled qty
actual_qty = float(p.qty)
unsettled_qty = float(p.unsettled_qty) if hasattr(p,
'unsettled_qty') else 0 # Assuming 'unsettled_qty' is the correct attribute name
pos = {
"symbol": p.symbol,
"qty": actual_qty,
"unsettled_qty": unsettled_qty,
"avg_entry_price": float(p.avg_entry_price) if p.avg_entry_price is not None else None
}
return pos
total_trades_today = 0
def calculate_position_values(self):
positions = self.api.list_positions()
self.crypto_value = 0.0
self.commodity_value = 0.0
# Calculate the total value of crypto and commodity positions
for position in positions:
symbol = position.symbol
current_price = float(position.current_price)
position_value = float(position.qty) * current_price
if symbol.endswith('USD'): # If the symbol ends with 'USD', it's a crypto position
self.crypto_value += position_value
else: # Otherwise, it's a commodity position
self.commodity_value += position_value
def validate_trade(self, symbol, order_type):
if self.total_trades_today >= 120:
print("Hit daily trade limit, rejecting order")
logging.info(f"Trade rejected for {symbol}: Hit daily trade limit")
return False
try:
qty = self.calculate_quantity(symbol)
if qty == 0:
print(f"Calculated quantity for {symbol} is zero, rejecting order")
logging.info(f"Trade rejected for {symbol}: Calculated quantity is zero")
return False
try:
existing_position = self.get_position(symbol)
current_qty = float(existing_position['qty']) if existing_position and existing_position[
'qty'] is not None else 0.0
except Exception:
current_qty = 0.0
new_qty = float(current_qty) + float(qty)
if new_qty > self.risk_params['max_position_size']:
print(f'Original requested quantity for {symbol}: {new_qty}')
print(f'Maximum position size according to risk parameters: {self.risk_params["max_position_size"]}')
qty = self.risk_params['max_position_size'] - current_qty
new_qty = self.risk_params['max_position_size']
print(f'Adjusted quantity to comply with max position size: {new_qty}')
logging.info(f"Adjusted buy quantity for {symbol} to {new_qty} to comply with max position size.")
elif new_qty <= current_qty:
print("No increase in position size, rejecting order")
logging.info(
f"Trade rejected for {symbol}: No increase in position size (current_qty: {current_qty}, new_qty: {new_qty})")
return False
print(f"Running validation logic against trade for {symbol}...")
portfolio = self.api.list_positions()
asset_values = {
'crypto': sum([float(p.current_price) * float(p.qty) for p in portfolio if
p.symbol.endswith('USD') and p.current_price is not None and p.qty is not None]),
'commodity': sum([float(p.current_price) * float(p.qty) for p in portfolio if
'commodity' in p.symbol and p.current_price is not None and p.qty is not None]),
'equity': sum([float(p.current_price) * float(p.qty) for p in portfolio if
p.symbol.isalpha() and p.current_price is not None and p.qty is not None]),
'options': sum([float(p.current_price) * float(p.qty) for p in portfolio if
'OPT' in p.symbol and p.current_price is not None and p.qty is not None])
}
portfolio_value = sum(asset_values.values())
print(f"Current portfolio value (market value of all positions): ${round(portfolio_value, 2)}.")
print('##################################################################')
print('##################################################################')
print('##################################################################')
print('Retrieving the price details from the get_current_price method...')
current_price = self.get_current_price(symbol)
if current_price is None:
print(
f"Failed to retrieve current price for {symbol} using primary method. Trying Alpaca API for options.")
current_price = self.get_option_price_from_alpaca(symbol)
if current_price is None:
print(f"Failed to retrieve current price for {symbol} using Alpaca API.")
logging.info(f"Trade rejected for {symbol}: Failed to retrieve current price.")
return False
print(f"Current Alpaca API price for {symbol} is: ${current_price}")
proposed_trade_value = float(current_price) * float(qty)
print(f"Total $ to purchase new order: ${round(proposed_trade_value, 2)}")
open_orders = self.api.list_orders(status='open')
open_symbols = [o.symbol for o in open_orders]
account_cash = float(self.api.get_account().cash)
print(f"Current account cash to buy: {account_cash}")
print('##################################################################')
print('##################################################################')
print('##################################################################')
print('Processing proposed_trade_value logic against current cash holdings...')
if proposed_trade_value > account_cash:
print("Proposed trade exceeds cash available to purchase crypto.")
logging.info(
f"Trade rejected for {symbol}: Proposed trade value (${proposed_trade_value}) exceeds available cash (${account_cash}).")
return False
asset_class = None
if 'USD' in symbol:
asset_class = 'crypto'
elif 'commodity' in symbol:
asset_class = 'commodity'
elif 'OPT' in symbol:
asset_class = 'options'
elif symbol.isalpha():
asset_class = 'equity'
if asset_class:
if asset_class == 'crypto':
max_allocation = self.risk_params.get('max_crypto_equity', None)
elif asset_class == 'equity':
max_allocation = self.risk_params.get('max_equity_equity', None)
else:
max_allocation = self.calculate_equity_allocation(asset_type=asset_class)
if max_allocation is not None:
print(f'Current {asset_class} equity: ${asset_values[asset_class]}')
print(f'Max {asset_class} equity: ${max_allocation}')
if float(asset_values[asset_class]) + float(proposed_trade_value) > max_allocation:
print(f"Trade exceeds max {asset_class} equity limit.")
logging.info(f"Trade rejected for {symbol}: Trade exceeds max {asset_class} equity limit.")
return False
if order_type == 'buy':
if qty > self.risk_params['max_position_size']:
print("Buy exceeds max position size")
logging.info(f"Trade rejected for {symbol}: Buy exceeds max position size.")
return False
elif order_type == 'sell':
position = self.get_position(symbol)
position_qty = float(position['qty']) if position and position['qty'] is not None else 0.0
qty = float(qty)
if qty > position_qty:
print("Sell quantity exceeds position size")
logging.info(f"Trade rejected for {symbol}: Sell quantity exceeds position size.")
return False
self.total_trades_today += 1
return True
except Exception as e:
print(f"Error validating trade: {e}")
logging.error(f"Error validating trade for {symbol}: {e}")
return False
def get_option_price_from_alpaca(self, symbol):
url = f"https://paper-api.alpaca.markets/v2/options/contracts/{symbol}"
headers = {
"accept": "application/json",
"APCA-API-KEY-ID": ALPACA_API_KEY,
"APCA-API-SECRET-KEY": ALPACA_SECRET_KEY
}
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return float(data.get('close_price', 0))
else:
logging.error(f"Failed to get option price from Alpaca: {response.status_code}, {response.text}")
return None
except Exception as e:
logging.error(f"Error getting option price from Alpaca for {symbol}: {e}")
return None
def monitor_account_status(self):
# Monitor and report on account status
try:
account = self.api.get_account()
print(f"Equity: {account.equity}")
print(f"Cash: {account.cash}")
print(f"Buying Power: {account.buying_power}")
return account
except Exception as e:
print(f"An exception occurred while monitoring account status: {str(e)}")
return None
def monitor_positions(self):
# Monitor and report on open positions
try:
positions = self.api.list_positions()
for position in positions:
pos_details = self.get_position(position.symbol)
if pos_details:
print(
f"Symbol: {pos_details['symbol']}, Quantity: {pos_details['qty']}, Avg Entry Price: {pos_details['avg_entry_price']}")
return positions
except Exception as e:
print(f"An exception occurred while monitoring positions: {str(e)}")
return None
def get_crypto_fee(self, volume):
if volume < 100_000:
return 0.0025
elif volume < 500_000:
return 0.0022
elif volume < 1_000_000:
return 0.002
elif volume < 10_000_000:
return 0.0018
elif volume < 25_000_000:
return 0.0015
elif volume < 50_000_000:
return 0.0013
elif volume < 100_000_000:
return 0.0012
else:
return 0.001
def report_profit_and_loss(self):
url = "https://paper-api.alpaca.markets/v2/account"
url_portfolio_history = "https://paper-api.alpaca.markets/v2/account/portfolio/history"
headers = {
"accept": "application/json",
"APCA-API-KEY-ID": ALPACA_API_KEY,
"APCA-API-SECRET-KEY": ALPACA_SECRET_KEY
}
try:
# Get account data
account_response = requests.get(url, headers=headers)
account_data = account_response.json()
cash_not_invested = float(account_data['cash'])
# Get portfolio history data
portfolio_history_response = requests.get(url_portfolio_history, headers=headers)
portfolio_history_data = portfolio_history_response.json()
# Filter out 'None' values
equity_values = [v for v in portfolio_history_data['equity'] if v is not None]
# Calculate PnL based on portfolio history
first_equity = float(equity_values[0]) # First equity value
last_equity = float(equity_values[-1]) # Last equity value
commissions = first_equity * 0.01
print(f'First equity is: {first_equity}.')
print(f'Last equity is: {last_equity}.')
print(f'Total commisions were: {commissions}.')
# find pnl for account
pnl_total = last_equity - first_equity - commissions
# find total equity for reporting
total_equity = pnl_total + cash_not_invested
print(
f"Total Profit/Loss: ${round(pnl_total,2)}. Total equity (cash invested plus cash not invested): ${round(total_equity,2)}")
return pnl_total
except Exception as e:
print(f"An exception occurred while reporting profit and loss: {str(e)}")
return 0
def get_equity(self):
return float(self.api.get_account().equity)
def update_risk_parameters(self):
# Dynamically adjust risk parameters based on account performance
pnl_total = self.report_profit_and_loss()
account = self.api.get_account()
current_equity = float(account.equity)
self.risk_params['max_portfolio_size'] = current_equity # Update the max_portfolio_size with the current equity
if pnl_total is None:
print("Could not calculate PnL, not updating risk parameters.")
return
pnl_total = float(round(pnl_total, 2))
print(f'pnl is accurately: {pnl_total}')
if pnl_total <= 0:
print("PnL is negative...")
if self.risk_params['max_position_size'] >= 50:
print("Reducing risk parameters...")
self.risk_params['max_position_size'] *= 0.90 # reduce by 10%
self.risk_params['max_portfolio_size'] *= 0.90 # reduce by 10%
else:
print("Max position size is less than 50. Not reducing risk parameters.")
elif pnl_total > 0:
print("PnL is positive...")
if self.risk_params['max_position_size'] >= 50:
print("Increasing risk parameters...")
self.risk_params['max_position_size'] *= 1.0015 # increase by .15%
self.risk_params['max_portfolio_size'] *= 1.0015 # increase by .15%
else:
print("Max position size is less than 50. Not increasing risk parameters.")
else:
print("PnL is neutral, no changes to risk parameters.")
with open('risk_params.json', 'w') as f:
json.dump(self.risk_params, f)
print("Risk parameters updated.")
return self.risk_params
def calculate_drawdown(self):
try:
portfolio = self.api.list_positions()
portfolio_value = sum([float(position.current_price) * float(position.qty) for position in portfolio])
# Update peak portfolio value if current portfolio value is higher
if portfolio_value > self.peak_portfolio_value:
self.peak_portfolio_value = portfolio_value
# Calculate drawdown if portfolio is not empty
if portfolio_value > 0 and self.peak_portfolio_value > 0:
drawdown = (self.peak_portfolio_value - portfolio_value) / self.peak_portfolio_value
else:
drawdown = 0
return drawdown
except Exception as e:
print(f"An exception occurred while calculating drawdown: {str(e)}")
return None
def check_risk_before_order(self, symbol, new_shares):
"""
Check the risk parameters before placing an order.
The function will prevent an order if the new shares would result in a position size
that violates the risk parameters.
"""
# Get the current position
try:
current_position = self.api.get_position(symbol)
current_shares = float(current_position.qty)
except:
current_shares = 0
# Calculate the new quantity of shares after the purchase
total_shares = current_shares + float(new_shares)
# Check if the new quantity violates the risk parameters
if total_shares > self.risk_params['max_position_size']:
return 'Order exceeded permissible balance. Total order share exceed max position size allowable.'
# If the new quantity violates the max position size, prevent the order
return False
else:
# If the new quantity doesn't violate the risk parameters, adjust the quantity and place the order
delta_shares = self.risk_params['max_position_size'] - current_shares
if delta_shares > 0:
# Get the average entry price
avg_entry_price = self.get_avg_entry_price(symbol)
if avg_entry_price is not None and avg_entry_price != 0:
# Calculate the adjusted quantity based on the average entry price
adjusted_quantity = int(delta_shares / avg_entry_price)
# Place the order with the adjusted quantity
self.api.submit_order(
symbol=symbol,
qty=adjusted_quantity,
side='buy',
type='limit',
time_in_force='gtc',
limit_price=avg_entry_price
)
return True
def check_momentum(self, symbol, momentum_signal):
"""
Checks the momentum signal and decides whether to sell the entire position.
"""
# Get position
position_list = [position for position in self.api.list_positions() if position.symbol == symbol]
if len(position_list) == 0:
print(f"No position exists for {symbol}.")
return
position = position_list[0]
# If momentum signal is 'Sell' and the percentage change is negative, sell the entire position
if momentum_signal == "Sell" and float(position.unrealized_plpc) < 0:
qty = position.qty
if self.validate_trade(symbol, qty, "sell"):
# Place a market sell order
self.api.submit_order(
symbol=symbol,
qty=qty,
side='sell',
type='market',
time_in_force='gtc'
)
print(f"Selling the entire position of {symbol} due to negative momentum.")
def calculate_quantity(self, symbol):
"""
Calculates the quantity to purchase based on available equity and current price.
"""
# Get account info
account = self.api.get_account()
available_cash = float(account.cash)
risk_params = load_risk_params()
max_crypto_equity = float(risk_params['max_crypto_equity'])
# Calculate the current total investment in cryptocurrencies
positions = self.api.list_positions()
total_investment = sum([float(p.market_value) for p in positions if p.symbol.endswith('USD')])
# If total investment is already at or exceeds max_crypto_equity, return quantity 0
if total_investment >= max_crypto_equity:
print(
f"Total investment in cryptocurrencies is already at or exceeds the maximum permitted. Returning quantity 0.")
return 0
# Calculate allowable investment as max_crypto_equity minus total investment
allowable_investment = max_crypto_equity - total_investment
# Determine investable amount
investable_amount = min(available_cash, allowable_investment)
# Check if investable amount is less than 1
if investable_amount < 1:
print(f"Investable amount for {symbol} is less than 1. Returning quantity 0.")
return 0
# Use the current price
current_price = self.get_current_price(symbol)
if current_price == 0 or current_price is None:
return 0
# Calculate a preliminary quantity based on the available cash
preliminary_quantity = investable_amount / current_price
# Tiered system for quantity adjustment
if current_price > 4001: # High-priced assets like BTC
quantity = preliminary_quantity * 0.01 # buy less of high-priced assets
elif 3001 < current_price <= 4000: # Mid-priced assets
quantity = preliminary_quantity * 0.0354
elif 1000 < current_price <= 3000: # Mid-priced assets
quantity = preliminary_quantity * 0.0334
elif 201 < current_price <= 999: # Mid-priced assets
quantity = preliminary_quantity * 0.04534
elif 20 < current_price <= 200: # Mid-priced assets