Skip to content

Commit

Permalink
Merge pull request #6 from sndmndss/simulations
Browse files Browse the repository at this point in the history
Add new simulated features
  • Loading branch information
sndmndss authored Apr 30, 2024
2 parents 82f5e34 + 1442f76 commit 575e2b2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
33 changes: 33 additions & 0 deletions bpx/simulations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from bpx.public import Public

default_public: Public = Public()


def get_volume(filled_orders: list[dict]) -> float | int:
prices = [float(item['price']) * float(item["quantity"]) for item in filled_orders]
return sum(prices)


def get_fees(filled_orders: list[dict]) -> float | int:
fees = [float(item['price']) * float(item["fee"])
if item["feeSymbol"] != 'USDC' else float(item["fee"]) for item in filled_orders]
return sum(fees)


def get_approximate_balance_in_usdc(balance: dict,
time_interval: str = "1m",
public: Public = default_public) -> float:
usdc_symbol = "USDC"
balance_usdc = 0
close_price = 0
for symbol in balance:
if symbol != usdc_symbol:
k_lines = public.get_klines(f"{symbol}_{usdc_symbol}", time_interval)
close_price = float(k_lines[-1]['close'])

for status in balance[symbol]:
if symbol == usdc_symbol:
balance_usdc += float(balance[symbol][status])
else:
balance_usdc += float(balance[symbol][status]) * close_price
return balance_usdc
2 changes: 1 addition & 1 deletion examples/public_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def public_example():
depth = public.get_depth("SOL_USDC")
print("Depth for SOL_USDC:", depth)

klines = public.get_klines("SOL_USDC", "1d")
klines = public.get_klines("SOL_USDC", "1m")
print("K-lines for SOL_USDC:", klines)

status = public.get_status()
Expand Down
18 changes: 18 additions & 0 deletions examples/simulations_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from bpx.simulations import *
from bpx.account import Account
from bpx.public import Public


public_key = "<KEY>"
secret_key = "<KEY>"


account = Account(public_key, secret_key, debug=True)

fills = account.get_fill_history_query("SOL_USDC", limit=1000)
print(get_fees(fills))
print(get_volume(fills))


balance = account.get_balances()
print(get_approximate_balance_in_usdc(balance))

0 comments on commit 575e2b2

Please sign in to comment.