]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blame - helper.py
Add tags to balance log
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / helper.py
CommitLineData
6ca5a1ec
IB
1import time
2from ccxt import ExchangeError
3from store import *
4
5def move_balances(market, debug=False):
6 needed_in_margin = {}
3d0247f9
IB
7 moving_to_margin = {}
8
0c79fad3
IB
9 for currency in BalanceStore.all:
10 if BalanceStore.all[currency].margin_free != 0:
11 needed_in_margin[currency] = 0
6ca5a1ec 12 for trade in TradeStore.all:
0c79fad3
IB
13 if trade.value_to.currency not in needed_in_margin:
14 needed_in_margin[trade.value_to.currency] = 0
6ca5a1ec 15 if trade.trade_type == "short":
6ca5a1ec
IB
16 needed_in_margin[trade.value_to.currency] += abs(trade.value_to)
17 for currency, needed in needed_in_margin.items():
18 current_balance = BalanceStore.all[currency].margin_free
3d0247f9
IB
19 moving_to_margin[currency] = (needed - current_balance)
20 delta = moving_to_margin[currency].value
21 if debug:
22 ReportStore.log_debug_action("Moving {} from exchange to margin".format(moving_to_margin[currency]))
23 continue
6ca5a1ec 24 if delta > 0:
3d0247f9 25 market.transfer_balance(currency, delta, "exchange", "margin")
6ca5a1ec 26 elif delta < 0:
3d0247f9
IB
27 market.transfer_balance(currency, -delta, "margin", "exchange")
28 ReportStore.log_move_balances(needed_in_margin, moving_to_margin, debug)
6ca5a1ec
IB
29
30 BalanceStore.fetch_balances(market)
31
32ticker_cache = {}
33ticker_cache_timestamp = time.time()
34def get_ticker(c1, c2, market, refresh=False):
35 global ticker_cache, ticker_cache_timestamp
36 def invert(ticker):
37 return {
38 "inverted": True,
39 "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2,
40 "original": ticker,
41 }
42 def augment_ticker(ticker):
43 ticker.update({
44 "inverted": False,
45 "average": (ticker["bid"] + ticker["ask"] ) / 2,
46 })
47
48 if time.time() - ticker_cache_timestamp > 5:
49 ticker_cache = {}
50 ticker_cache_timestamp = time.time()
51 elif not refresh:
52 if (c1, c2, market.__class__) in ticker_cache:
53 return ticker_cache[(c1, c2, market.__class__)]
54 if (c2, c1, market.__class__) in ticker_cache:
55 return invert(ticker_cache[(c2, c1, market.__class__)])
56
57 try:
58 ticker_cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2))
59 augment_ticker(ticker_cache[(c1, c2, market.__class__)])
60 except ExchangeError:
61 try:
62 ticker_cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1))
63 augment_ticker(ticker_cache[(c2, c1, market.__class__)])
64 except ExchangeError:
65 ticker_cache[(c1, c2, market.__class__)] = None
66 return get_ticker(c1, c2, market)
67
68fees_cache = {}
69def fetch_fees(market):
70 global fees_cache
71 if market.__class__ not in fees_cache:
72 fees_cache[market.__class__] = market.fetch_fees()
73 return fees_cache[market.__class__]
74
e246023e 75def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", debug=False):
3d0247f9 76 ReportStore.log_stage("prepare_trades")
6ca5a1ec
IB
77 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
78 total_base_value = sum(values_in_base.values())
e246023e 79 new_repartition = BalanceStore.dispatch_assets(total_base_value, liquidity=liquidity)
6ca5a1ec
IB
80 # Recompute it in case we have new currencies
81 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
82 TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
83
e246023e 84def update_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", only=None, debug=False):
3d0247f9 85 ReportStore.log_stage("update_trades")
6ca5a1ec
IB
86 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
87 total_base_value = sum(values_in_base.values())
e246023e 88 new_repartition = BalanceStore.dispatch_assets(total_base_value, liquidity=liquidity)
6ca5a1ec
IB
89 TradeStore.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug)
90
91def prepare_trades_to_sell_all(market, base_currency="BTC", compute_value="average", debug=False):
3d0247f9 92 ReportStore.log_stage("prepare_trades_to_sell_all")
6ca5a1ec
IB
93 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
94 total_base_value = sum(values_in_base.values())
95 new_repartition = BalanceStore.dispatch_assets(total_base_value, repartition={ base_currency: (1, "long") })
96 TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
97
3d0247f9 98def follow_orders(sleep=None):
6ca5a1ec
IB
99 if sleep is None:
100 sleep = 7 if TradeStore.debug else 30
3d0247f9
IB
101 if TradeStore.debug:
102 ReportStore.log_debug_action("Set follow_orders tick to {}s".format(sleep))
6ca5a1ec 103 tick = 0
3d0247f9 104 ReportStore.log_stage("follow_orders_begin")
6ca5a1ec
IB
105 while len(TradeStore.all_orders(state="open")) > 0:
106 time.sleep(sleep)
107 tick += 1
3d0247f9
IB
108 open_orders = TradeStore.all_orders(state="open")
109 ReportStore.log_stage("follow_orders_tick_{}".format(tick))
110 ReportStore.log_orders(open_orders, tick=tick)
111 for order in open_orders:
6ca5a1ec 112 if order.get_status() != "open":
3d0247f9 113 ReportStore.log_order(order, tick, finished=True)
6ca5a1ec
IB
114 else:
115 order.trade.update_order(order, tick)
3d0247f9 116 ReportStore.log_stage("follow_orders_end")
6ca5a1ec
IB
117
118def print_orders(market, base_currency="BTC"):
3d0247f9 119 ReportStore.log_stage("print_orders")
18167a3c 120 BalanceStore.fetch_balances(market, tag="print_orders")
2f0939e1 121 prepare_trades(market, base_currency=base_currency, compute_value="average", debug=True)
6ca5a1ec 122 TradeStore.prepare_orders(compute_value="average")
6ca5a1ec 123
97922ff1
IB
124def print_balances(market, base_currency="BTC"):
125 BalanceStore.fetch_balances(market)
97922ff1 126 if base_currency is not None:
3d0247f9
IB
127 ReportStore.print_log("total:")
128 ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values()))
97922ff1 129
e246023e 130def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False):
3d0247f9 131 ReportStore.log_stage("process_sell_needed__1_sell_begin")
18167a3c 132 BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_begin")
e246023e 133 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
5a72ded7 134 TradeStore.prepare_orders(compute_value="average", only="dispose")
5a72ded7
IB
135 TradeStore.run_orders()
136 follow_orders()
18167a3c 137 BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_end")
3d0247f9 138 ReportStore.log_stage("process_sell_needed__1_sell_end")
5a72ded7 139
e246023e 140def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC", debug=False):
3d0247f9 141 ReportStore.log_stage("process_sell_needed__2_buy_begin")
18167a3c 142 BalanceStore.fetch_balances(market, tag="process_sell_needed__2_buy_begin")
e246023e 143 update_trades(market, base_currency=base_currency, liquidity=liquidity, debug=debug, only="acquire")
5a72ded7 144 TradeStore.prepare_orders(compute_value="average", only="acquire")
5a72ded7
IB
145 move_balances(market, debug=debug)
146 TradeStore.run_orders()
147 follow_orders()
18167a3c 148 BalanceStore.fetch_balances(market, tag="process_sell_needed__2_buy_end")
3d0247f9 149 ReportStore.log_stage("process_sell_needed__2_buy_end")
6ca5a1ec 150
e246023e 151def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False, liquidity="medium"):
3d0247f9 152 ReportStore.log_stage("process_sell_all__1_all_sell_begin")
18167a3c 153 BalanceStore.fetch_balances(market, tag="process_sell_all__1_all_sell_begin")
5a72ded7 154 prepare_trades_to_sell_all(market, base_currency=base_currency, debug=debug)
6ca5a1ec 155 TradeStore.prepare_orders(compute_value="average")
6ca5a1ec
IB
156 TradeStore.run_orders()
157 follow_orders()
18167a3c 158 BalanceStore.fetch_balances(market, tag="process_sell_all__1_all_sell_end")
3d0247f9 159 ReportStore.log_stage("process_sell_all__1_all_sell_end")
6ca5a1ec 160
e246023e 161def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False, liquidity="medium"):
3d0247f9 162 ReportStore.log_stage("process_sell_all__2_all_buy_begin")
18167a3c 163 BalanceStore.fetch_balances(market, tag="process_sell_all__2_all_buy_begin")
e246023e
IB
164 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
165 TradeStore.prepare_orders(compute_value="average")
6ca5a1ec
IB
166 move_balances(market, debug=debug)
167 TradeStore.run_orders()
168 follow_orders()
18167a3c 169 BalanceStore.fetch_balances(market, tag="process_sell_all__2_all_buy_end")
3d0247f9 170 ReportStore.log_stage("process_sell_all__2_all_buy_end")
6ca5a1ec
IB
171
172