]>
Commit | Line | Data |
---|---|---|
1 | import time | |
2 | from ccxt import ExchangeError | |
3 | from store import * | |
4 | ||
5 | def move_balances(market, debug=False): | |
6 | needed_in_margin = {} | |
7 | for trade in TradeStore.all: | |
8 | if trade.trade_type == "short": | |
9 | if trade.value_to.currency not in needed_in_margin: | |
10 | needed_in_margin[trade.value_to.currency] = 0 | |
11 | needed_in_margin[trade.value_to.currency] += abs(trade.value_to) | |
12 | for currency, needed in needed_in_margin.items(): | |
13 | current_balance = BalanceStore.all[currency].margin_free | |
14 | delta = (needed - current_balance).value | |
15 | # FIXME: don't remove too much if there are open margin position | |
16 | if delta > 0: | |
17 | if debug: | |
18 | print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency, delta)) | |
19 | else: | |
20 | market.transfer_balance(currency, delta, "exchange", "margin") | |
21 | elif delta < 0: | |
22 | if debug: | |
23 | print("market.transfer_balance({}, {}, 'margin', 'exchange')".format(currency, -delta)) | |
24 | else: | |
25 | market.transfer_balance(currency, -delta, "margin", "exchange") | |
26 | ||
27 | BalanceStore.fetch_balances(market) | |
28 | ||
29 | ticker_cache = {} | |
30 | ticker_cache_timestamp = time.time() | |
31 | def get_ticker(c1, c2, market, refresh=False): | |
32 | global ticker_cache, ticker_cache_timestamp | |
33 | def invert(ticker): | |
34 | return { | |
35 | "inverted": True, | |
36 | "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2, | |
37 | "original": ticker, | |
38 | } | |
39 | def augment_ticker(ticker): | |
40 | ticker.update({ | |
41 | "inverted": False, | |
42 | "average": (ticker["bid"] + ticker["ask"] ) / 2, | |
43 | }) | |
44 | ||
45 | if time.time() - ticker_cache_timestamp > 5: | |
46 | ticker_cache = {} | |
47 | ticker_cache_timestamp = time.time() | |
48 | elif not refresh: | |
49 | if (c1, c2, market.__class__) in ticker_cache: | |
50 | return ticker_cache[(c1, c2, market.__class__)] | |
51 | if (c2, c1, market.__class__) in ticker_cache: | |
52 | return invert(ticker_cache[(c2, c1, market.__class__)]) | |
53 | ||
54 | try: | |
55 | ticker_cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2)) | |
56 | augment_ticker(ticker_cache[(c1, c2, market.__class__)]) | |
57 | except ExchangeError: | |
58 | try: | |
59 | ticker_cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1)) | |
60 | augment_ticker(ticker_cache[(c2, c1, market.__class__)]) | |
61 | except ExchangeError: | |
62 | ticker_cache[(c1, c2, market.__class__)] = None | |
63 | return get_ticker(c1, c2, market) | |
64 | ||
65 | fees_cache = {} | |
66 | def fetch_fees(market): | |
67 | global fees_cache | |
68 | if market.__class__ not in fees_cache: | |
69 | fees_cache[market.__class__] = market.fetch_fees() | |
70 | return fees_cache[market.__class__] | |
71 | ||
72 | def prepare_trades(market, base_currency="BTC", compute_value="average", debug=False): | |
73 | BalanceStore.fetch_balances(market) | |
74 | values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) | |
75 | total_base_value = sum(values_in_base.values()) | |
76 | new_repartition = BalanceStore.dispatch_assets(total_base_value) | |
77 | # Recompute it in case we have new currencies | |
78 | values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) | |
79 | TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug) | |
80 | ||
81 | def update_trades(market, base_currency="BTC", compute_value="average", only=None, debug=False): | |
82 | BalanceStore.fetch_balances(market) | |
83 | values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) | |
84 | total_base_value = sum(values_in_base.values()) | |
85 | new_repartition = BalanceStore.dispatch_assets(total_base_value) | |
86 | TradeStore.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug) | |
87 | ||
88 | def prepare_trades_to_sell_all(market, base_currency="BTC", compute_value="average", debug=False): | |
89 | BalanceStore.fetch_balances(market) | |
90 | values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) | |
91 | total_base_value = sum(values_in_base.values()) | |
92 | new_repartition = BalanceStore.dispatch_assets(total_base_value, repartition={ base_currency: (1, "long") }) | |
93 | TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug) | |
94 | ||
95 | def follow_orders(verbose=True, sleep=None): | |
96 | if sleep is None: | |
97 | sleep = 7 if TradeStore.debug else 30 | |
98 | tick = 0 | |
99 | while len(TradeStore.all_orders(state="open")) > 0: | |
100 | time.sleep(sleep) | |
101 | tick += 1 | |
102 | for order in TradeStore.all_orders(state="open"): | |
103 | if order.get_status() != "open": | |
104 | if verbose: | |
105 | print("finished {}".format(order)) | |
106 | else: | |
107 | order.trade.update_order(order, tick) | |
108 | if verbose: | |
109 | print("All orders finished") | |
110 | ||
111 | def print_orders(market, base_currency="BTC"): | |
112 | prepare_trades(market, base_currency=base_currency, compute_value="average", debug=True) | |
113 | TradeStore.prepare_orders(compute_value="average") | |
114 | for currency, balance in BalanceStore.all.items(): | |
115 | print(balance) | |
116 | TradeStore.print_all_with_order() | |
117 | ||
118 | def process_sell_needed__1_sell(market, base_currency="BTC", debug=False): | |
119 | prepare_trades(market, base_currency=base_currency, debug=debug) | |
120 | TradeStore.prepare_orders(compute_value="average", only="dispose") | |
121 | print("------------------") | |
122 | for currency, balance in BalanceStore.all.items(): | |
123 | print(balance) | |
124 | print("------------------") | |
125 | TradeStore.print_all_with_order() | |
126 | print("------------------") | |
127 | TradeStore.run_orders() | |
128 | follow_orders() | |
129 | ||
130 | def process_sell_needed__2_buy(market, base_currency="BTC", debug=False): | |
131 | update_trades(market, base_currency=base_currency, debug=debug, only="acquire") | |
132 | TradeStore.prepare_orders(compute_value="average", only="acquire") | |
133 | print("------------------") | |
134 | for currency, balance in BalanceStore.all.items(): | |
135 | print(balance) | |
136 | print("------------------") | |
137 | TradeStore.print_all_with_order() | |
138 | print("------------------") | |
139 | move_balances(market, debug=debug) | |
140 | TradeStore.run_orders() | |
141 | follow_orders() | |
142 | ||
143 | def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False): | |
144 | prepare_trades_to_sell_all(market, base_currency=base_currency, debug=debug) | |
145 | TradeStore.prepare_orders(compute_value="average") | |
146 | print("------------------") | |
147 | for currency, balance in BalanceStore.all.items(): | |
148 | print(balance) | |
149 | print("------------------") | |
150 | TradeStore.print_all_with_order() | |
151 | print("------------------") | |
152 | TradeStore.run_orders() | |
153 | follow_orders() | |
154 | ||
155 | def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False): | |
156 | prepare_trades(market, base_currency=base_currency, debug=debug) | |
157 | TradeStore.prepare_orders() | |
158 | print("------------------") | |
159 | for currency, balance in BalanceStore.all.items(): | |
160 | print(balance) | |
161 | print("------------------") | |
162 | TradeStore.print_all_with_order() | |
163 | print("------------------") | |
164 | move_balances(market, debug=debug) | |
165 | TradeStore.run_orders() | |
166 | follow_orders() | |
167 | ||
168 |