2 import simplejson
as json
3 from decimal
import Decimal
as D
, ROUND_DOWN
4 from datetime
import date
, datetime
6 __all__
= ["BalanceStore", "ReportStore", "TradeStore"]
9 def __init__(self
, market
, verbose_print
=True):
11 self
.verbose_print
= verbose_print
15 def print_log(self
, message
):
16 message
= str(message
)
17 if self
.verbose_print
:
20 def add_log(self
, hash_
):
21 hash_
["date"] = datetime
.now()
22 self
.logs
.append(hash_
)
25 def default_json_serial(obj
):
26 if isinstance(obj
, (datetime
, date
)):
27 return obj
.isoformat()
29 return json
.dumps(self
.logs
, default
=default_json_serial
)
31 def set_verbose(self
, verbose_print
):
32 self
.verbose_print
= verbose_print
34 def log_stage(self
, stage
):
35 self
.print_log("-" * (len(stage
) + 8))
36 self
.print_log("[Stage] {}".format(stage
))
43 def log_balances(self
, tag
=None):
44 self
.print_log("[Balance]")
45 for currency
, balance
in self
.market
.balances
.all
.items():
46 self
.print_log("\t{}".format(balance
))
51 "balances": self
.market
.balances
.as_json()
54 def log_tickers(self
, amounts
, other_currency
,
58 for currency
, amount
in amounts
.items():
59 values
[currency
] = amount
.as_json()["value"]
60 rates
[currency
] = amount
.rate
63 "compute_value": compute_value
,
65 "currency": other_currency
,
68 "total": sum(amounts
.values()).as_json()["value"]
71 def log_dispatch(self
, amount
, amounts
, liquidity
, repartition
):
74 "liquidity": liquidity
,
75 "repartition_ratio": repartition
,
76 "total_amount": amount
.as_json(),
77 "repartition": { k: v.as_json()["value"] for k, v in amounts.items() }
80 def log_trades(self
, matching_and_trades
, only
):
82 for matching
, trade
in matching_and_trades
:
83 trade_json
= trade
.as_json()
84 trade_json
["skipped"] = not matching
85 trades
.append(trade_json
)
90 "debug": self
.market
.debug
,
94 def log_orders(self
, orders
, tick
=None, only
=None, compute_value
=None):
95 self
.print_log("[Orders]")
96 self
.market
.trades
.print_all_with_order(ind
="\t")
100 "compute_value": compute_value
,
102 "orders": [order
.as_json() for order
in orders
if order
is not None]
105 def log_order(self
, order
, tick
, finished
=False, update
=None,
106 new_order
=None, compute_value
=None):
108 self
.print_log("[Order] Finished {}".format(order
))
109 elif update
== "waiting":
110 self
.print_log("[Order] {}, tick {}, waiting".format(order
, tick
))
111 elif update
== "adjusting":
112 self
.print_log("[Order] {}, tick {}, cancelling and adjusting to {}".format(order
, tick
, new_order
))
113 elif update
== "market_fallback":
114 self
.print_log("[Order] {}, tick {}, fallbacking to market value".format(order
, tick
))
115 elif update
== "market_adjust":
116 self
.print_log("[Order] {}, tick {}, market value, cancelling and adjusting to {}".format(order
, tick
, new_order
))
122 "order": order
.as_json(),
123 "compute_value": compute_value
,
124 "new_order": new_order
.as_json() if new_order
is not None else None
127 def log_move_balances(self
, needed
, moving
):
129 "type": "move_balances",
130 "debug": self
.market
.debug
,
131 "needed": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in needed.items() }
,
132 "moving": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in moving.items() }
,
135 def log_http_request(self
, method
, url
, body
, headers
, response
):
137 "type": "http_request",
142 "status": response
.status_code
,
143 "response": response
.text
146 def log_error(self
, action
, message
=None, exception
=None):
147 self
.print_log("[Error] {}".format(action
))
148 if exception
is not None:
149 self
.print_log(str("\t{}: {}".format(exception
.__class
__.__name
__, exception
)))
150 if message
is not None:
151 self
.print_log("\t{}".format(message
))
156 "exception_class": exception
.__class
__.__name
__ if exception
is not None else None,
157 "exception_message": str(exception
) if exception
is not None else None,
161 def log_debug_action(self
, action
):
162 self
.print_log("[Debug] {}".format(action
))
165 "type": "debug_action",
170 def __init__(self
, market
):
174 def currencies(self
):
175 return self
.all
.keys()
177 def in_currency(self
, other_currency
, compute_value
="average", type="total"):
179 for currency
, balance
in self
.all
.items():
180 other_currency_amount
= getattr(balance
, type)\
181 .in_currency(other_currency
, self
.market
, compute_value
=compute_value
)
182 amounts
[currency
] = other_currency_amount
183 self
.market
.report
.log_tickers(amounts
, other_currency
,
187 def fetch_balances(self
, tag
=None):
188 all_balances
= self
.market
.ccxt
.fetch_all_balances()
189 for currency
, balance
in all_balances
.items():
190 if balance
["exchange_total"] != 0 or balance
["margin_total"] != 0 or \
191 currency
in self
.all
:
192 self
.all
[currency
] = portfolio
.Balance(currency
, balance
)
193 self
.market
.report
.log_balances(tag
=tag
)
195 def dispatch_assets(self
, amount
, liquidity
="medium", repartition
=None):
196 if repartition
is None:
197 repartition
= portfolio
.Portfolio
.repartition(self
.market
, liquidity
=liquidity
)
198 sum_ratio
= sum([v
[0] for k
, v
in repartition
.items()])
200 for currency
, (ptt
, trade_type
) in repartition
.items():
201 amounts
[currency
] = ptt
* amount
/ sum_ratio
202 if trade_type
== "short":
203 amounts
[currency
] = - amounts
[currency
]
204 if currency
not in self
.all
:
205 self
.all
[currency
] = portfolio
.Balance(currency
, {})
206 self
.market
.report
.log_dispatch(amount
, amounts
, liquidity
, repartition
)
210 return { k: v.as_json() for k, v in self.all.items() }
213 def __init__(self
, market
):
217 def compute_trades(self
, values_in_base
, new_repartition
, only
=None):
219 base_currency
= sum(values_in_base
.values()).currency
220 for currency
in self
.market
.balances
.currencies():
221 if currency
== base_currency
:
223 value_from
= values_in_base
.get(currency
, portfolio
.Amount(base_currency
, 0))
224 value_to
= new_repartition
.get(currency
, portfolio
.Amount(base_currency
, 0))
226 if value_from
.value
* value_to
.value
< 0:
227 computed_trades
.append(self
.trade_if_matching(
228 value_from
, portfolio
.Amount(base_currency
, 0),
229 currency
, only
=only
))
230 computed_trades
.append(self
.trade_if_matching(
231 portfolio
.Amount(base_currency
, 0), value_to
,
232 currency
, only
=only
))
234 computed_trades
.append(self
.trade_if_matching(
235 value_from
, value_to
,
236 currency
, only
=only
))
237 for matching
, trade
in computed_trades
:
239 self
.all
.append(trade
)
240 self
.market
.report
.log_trades(computed_trades
, only
)
242 def trade_if_matching(self
, value_from
, value_to
, currency
,
244 trade
= portfolio
.Trade(value_from
, value_to
, currency
,
246 matching
= only
is None or trade
.action
== only
247 return [matching
, trade
]
249 def prepare_orders(self
, only
=None, compute_value
="default"):
251 for trade
in self
.all
:
252 if only
is None or trade
.action
== only
:
253 orders
.append(trade
.prepare_order(compute_value
=compute_value
))
254 self
.market
.report
.log_orders(orders
, only
, compute_value
)
256 def print_all_with_order(self
, ind
=""):
257 for trade
in self
.all
:
258 trade
.print_with_order(ind
=ind
)
260 def run_orders(self
):
261 orders
= self
.all_orders(state
="pending")
264 self
.market
.report
.log_stage("run_orders")
265 self
.market
.report
.log_orders(orders
)
267 def all_orders(self
, state
=None):
268 all_orders
= sum(map(lambda v
: v
.orders
, self
.all
), [])
272 return list(filter(lambda o
: o
.status
== state
, all_orders
))
274 def update_all_orders_status(self
):
275 for order
in self
.all_orders(state
="open"):