Skip to content

Commit

Permalink
milestones wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Amichay Oren committed Sep 1, 2023
1 parent c87f4db commit cb8dae2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
19 changes: 16 additions & 3 deletions apigateway/get_missions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from flask import abort

from infra import auth, authenticated_user_id # type: ignore
from infra.alpaca_action import get_account_balance
from infra.data.missions import Mission, Missions
from infra.logger import log_error
from infra.stytch_actions import get_alpaca_account_id


def process_mission(mission_details: Mission) -> dict:
def process_mission(account_balance: int, mission_details: Mission) -> dict:
"""create mission payload"""

payload: dict = {
Expand All @@ -15,6 +17,7 @@ def process_mission(mission_details: Mission) -> dict:
"forecast": mission_details.forecaster(),
"name": mission_details.data["name"],
"strategy": mission_details.data["strategy"],
"milestones": mission_details.calculate_milestones(account_balance),
}

return payload
Expand All @@ -31,10 +34,20 @@ def get_missions(request):
"get_missions()",
"could not load authenticated user_id, this shouldn't have happened",
)
return ("Something went wrong", 403)
abort(403)

if not (alpaca_account_id := get_alpaca_account_id(user_id)):
log_error("get_missions()", "alpaca account not ready")
abort(400)

# TODO: account balance is not per mission!
if not (account_balance := get_account_balance(alpaca_account_id)):
abort(400)

missions = Missions(user_id=user_id)
payload = [process_mission(mission) for mission in missions]
payload = [
process_mission(account_balance, mission) for mission in missions
]
if not payload:
log_error(
"get_missions()", f"{user_id} does not have any active missions"
Expand Down
2 changes: 1 addition & 1 deletion apigateway/get_missions/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1623,5 +1623,5 @@ zipp==3.15.0 \


--extra-index-url https://us-east4-python.pkg.dev/development-380917/python-repos/simple/
infra==0.1.106 \
infra==0.1.107 \
--hash=sha256:0e81d2b54b3d5449789ab70a3c493c9bbd147a2365999c909e32e14489fe1fd2
28 changes: 28 additions & 0 deletions infra/infra/alpaca_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ def validate_before_transfer(account_id: str) -> bool:
return True


def get_account_balance(alpaca_account_id: str) -> int | None:
r = alpaca_proxy(
method="GET",
url=f"v1/accounts/{alpaca_account_id}",
args=None,
headers=None,
payload=None,
)

if r.status_code != 200:
log_error(
"get_account_balance()",
f"failed to get account details {alpaca_account_id}: {r.text}",
)
return None

usd = r.json().get("usd")

portfolio_value = int(usd.get("portfolio_value", 0))
cash = int(usd.get("cash", 0))

print(
f"account balances for {alpaca_account_id}: {portfolio_value}, {cash}"
)

return portfolio_value + cash


def transfer_amount(
alpaca_account_id: str, relationship_id: str, amount: int
) -> dict | None:
Expand Down
20 changes: 20 additions & 0 deletions infra/infra/data/missions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@


class Mission:
level_step: dict = {
200: 25,
1000: 50,
2500: 100,
5000: 250,
15000: 500,
20000: 750,
40000: 1000,
150000: 2000,
}

def __init__(self, data: dict):
self.data = data

Expand Down Expand Up @@ -47,6 +58,15 @@ def forecaster(self, bins: int = 30) -> list:

return new_df.values.tolist()

def calculate_milestones(self, account_size: int) -> list[int]:
level = min(
self.level_step.keys(), key=lambda x: abs(x - account_size)
)

print(level)

return []


class Missions:
def __init__(self, user_id: str | None = None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion infra/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ license = {text = "MIT"}
name = "infra"
readme = "README.md"
requires-python = ">=3.11"
version = "0.1.106"
version = "0.1.107"

[build-system]
build-backend = "pdm.backend"
Expand Down

0 comments on commit cb8dae2

Please sign in to comment.