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_parameters_auto_adj.py
More file actions
79 lines (65 loc) · 2.64 KB
/
risk_parameters_auto_adj.py
File metadata and controls
79 lines (65 loc) · 2.64 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
import os
import requests
from dotenv import load_dotenv
import json
# Load environment variables
load_dotenv()
ALPACA_API_KEY = os.getenv('ALPACA_API_KEY')
ALPACA_API_SECRET_KEY = os.getenv('ALPACA_SECRET_KEY')
ALPACA_ENDPOINT = 'https://paper-api.alpaca.markets/v2/account'
def fetch_account_details():
headers = {
"accept": "application/json",
"APCA-API-KEY-ID": ALPACA_API_KEY,
"APCA-API-SECRET-KEY": ALPACA_API_SECRET_KEY
}
response = requests.get(ALPACA_ENDPOINT, headers=headers)
if response.status_code == 200:
account_info = response.json()
account_details = {
"equity": float(account_info.get('equity', 0)),
"cash": float(account_info.get('cash', 0)),
"portfolio_value": float(account_info.get('portfolio_value', 0)),
"buying_power": float(account_info.get('buying_power', 0)),
"status": account_info.get('status', 'UNKNOWN')
}
print(f"Account Details: {account_details}")
return account_details
else:
print(f"Failed to fetch account details: {response.text}")
return None
def calculate_risk_parameters(account_size):
max_position_size = account_size * 0.15
max_portfolio_size = account_size * 0.89
max_drawdown = 20
max_risk_per_trade = 0.05
max_crypto_equity = account_size * 0.4
max_equity_equity = account_size * 0.4
return {
"max_position_size": max_position_size,
"max_portfolio_size": max_portfolio_size,
"max_drawdown": max_drawdown,
"max_risk_per_trade": max_risk_per_trade,
"max_crypto_equity": max_crypto_equity,
"max_equity_equity": max_equity_equity
}
def update_risk_parameters_json(new_risk_parameters):
file_path = os.path.join(os.getcwd(), 'risk_params.json')
if os.path.exists(file_path):
with open(file_path, 'r') as file:
existing_risk_parameters = json.load(file)
else:
existing_risk_parameters = {}
existing_risk_parameters.update(new_risk_parameters)
with open(file_path, 'w') as file:
json.dump(existing_risk_parameters, file, indent=4)
if __name__ == "__main__":
account_details = fetch_account_details()
if account_details:
account_size = account_details['equity']
risk_parameters = calculate_risk_parameters(account_size)
print(risk_parameters)
update_risk_parameters_json(risk_parameters)
print("Risk parameters updated successfully.")
else:
print("Failed to fetch account details. Risk parameters not updated.")