aboutsummaryrefslogtreecommitdiff
path: root/helper.py
diff options
context:
space:
mode:
Diffstat (limited to 'helper.py')
-rw-r--r--helper.py151
1 files changed, 151 insertions, 0 deletions
diff --git a/helper.py b/helper.py
new file mode 100644
index 0000000..8a29f40
--- /dev/null
+++ b/helper.py
@@ -0,0 +1,151 @@
1import time
2from ccxt import ExchangeError
3from store import *
4
5def 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
29ticker_cache = {}
30ticker_cache_timestamp = time.time()
31def 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
65fees_cache = {}
66def 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
72def 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
81def 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
88def 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
95def 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
111def print_orders(market, base_currency="BTC"):
112 prepare_trades(market, base_currency=base_currency, compute_value="average")
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
118def make_orders(market, base_currency="BTC"):
119 prepare_trades(market, base_currency=base_currency)
120 for trade in TradeStore.all:
121 print(trade)
122 for order in trade.orders:
123 print("\t", order, sep="")
124 order.run()
125
126def process_sell_all_sell(market, base_currency="BTC", debug=False):
127 prepare_trades_to_sell_all(market, debug=debug)
128 TradeStore.prepare_orders(compute_value="average")
129 print("------------------")
130 for currency, balance in BalanceStore.all.items():
131 print(balance)
132 print("------------------")
133 TradeStore.print_all_with_order()
134 print("------------------")
135 TradeStore.run_orders()
136 follow_orders()
137
138def process_sell_all_buy(market, base_currency="BTC", debug=False):
139 prepare_trades(market, debug=debug)
140 TradeStore.prepare_orders()
141 print("------------------")
142 for currency, balance in BalanceStore.all.items():
143 print(balance)
144 print("------------------")
145 TradeStore.print_all_with_order()
146 print("------------------")
147 move_balances(market, debug=debug)
148 TradeStore.run_orders()
149 follow_orders()
150
151