]>
git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blob - store.py
3 __all__
= ["BalanceStore", "TradeStore"]
13 def in_currency(cls
, other_currency
, market
, compute_value
="average", type="total"):
15 for currency
, balance
in cls
.all
.items():
16 other_currency_amount
= getattr(balance
, type)\
17 .in_currency(other_currency
, market
, compute_value
=compute_value
)
18 amounts
[currency
] = other_currency_amount
22 def fetch_balances(cls
, market
):
23 all_balances
= market
.fetch_all_balances()
24 for currency
, balance
in all_balances
.items():
25 if balance
["exchange_total"] != 0 or balance
["margin_total"] != 0 or \
27 cls
.all
[currency
] = portfolio
.Balance(currency
, balance
)
30 def dispatch_assets(cls
, amount
, liquidity
="medium", repartition
=None):
31 if repartition
is None:
32 repartition
= portfolio
.Portfolio
.repartition(liquidity
=liquidity
)
33 sum_ratio
= sum([v
[0] for k
, v
in repartition
.items()])
35 for currency
, (ptt
, trade_type
) in repartition
.items():
36 amounts
[currency
] = ptt
* amount
/ sum_ratio
37 if trade_type
== "short":
38 amounts
[currency
] = - amounts
[currency
]
39 if currency
not in BalanceStore
.all
:
40 cls
.all
[currency
] = portfolio
.Balance(currency
, {})
48 def compute_trades(cls
, values_in_base
, new_repartition
, only
=None, market
=None, debug
=False):
49 cls
.debug
= cls
.debug
or debug
50 base_currency
= sum(values_in_base
.values()).currency
51 for currency
in BalanceStore
.currencies():
52 if currency
== base_currency
:
54 value_from
= values_in_base
.get(currency
, portfolio
.Amount(base_currency
, 0))
55 value_to
= new_repartition
.get(currency
, portfolio
.Amount(base_currency
, 0))
57 if value_from
.value
* value_to
.value
< 0:
58 cls
.add_trade_if_matching(
59 value_from
, portfolio
.Amount(base_currency
, 0),
60 currency
, only
=only
, market
=market
)
61 cls
.add_trade_if_matching(
62 portfolio
.Amount(base_currency
, 0), value_to
,
63 currency
, only
=only
, market
=market
)
65 cls
.add_trade_if_matching(value_from
, value_to
,
66 currency
, only
=only
, market
=market
)
69 def add_trade_if_matching(cls
, value_from
, value_to
, currency
,
70 only
=None, market
=None):
71 trade
= portfolio
.Trade(value_from
, value_to
, currency
,
73 if only
is None or trade
.action
== only
:
79 def prepare_orders(cls
, only
=None, compute_value
="default"):
81 if only
is None or trade
.action
== only
:
82 trade
.prepare_order(compute_value
=compute_value
)
85 def print_all_with_order(cls
):
87 trade
.print_with_order()
91 for order
in cls
.all_orders(state
="pending"):
95 def all_orders(cls
, state
=None):
96 all_orders
= sum(map(lambda v
: v
.orders
, cls
.all
), [])
100 return list(filter(lambda o
: o
.status
== state
, all_orders
))
103 def update_all_orders_status(cls
):
104 for order
in cls
.all_orders(state
="open"):