aboutsummaryrefslogtreecommitdiff
path: root/helper.py
diff options
context:
space:
mode:
Diffstat (limited to 'helper.py')
-rw-r--r--helper.py81
1 files changed, 34 insertions, 47 deletions
diff --git a/helper.py b/helper.py
index 23b2d0b..87539f1 100644
--- a/helper.py
+++ b/helper.py
@@ -4,6 +4,8 @@ from store import *
4 4
5def move_balances(market, debug=False): 5def move_balances(market, debug=False):
6 needed_in_margin = {} 6 needed_in_margin = {}
7 moving_to_margin = {}
8
7 for currency in BalanceStore.all: 9 for currency in BalanceStore.all:
8 if BalanceStore.all[currency].margin_free != 0: 10 if BalanceStore.all[currency].margin_free != 0:
9 needed_in_margin[currency] = 0 11 needed_in_margin[currency] = 0
@@ -14,18 +16,16 @@ def move_balances(market, debug=False):
14 needed_in_margin[trade.value_to.currency] += abs(trade.value_to) 16 needed_in_margin[trade.value_to.currency] += abs(trade.value_to)
15 for currency, needed in needed_in_margin.items(): 17 for currency, needed in needed_in_margin.items():
16 current_balance = BalanceStore.all[currency].margin_free 18 current_balance = BalanceStore.all[currency].margin_free
17 delta = (needed - current_balance).value 19 moving_to_margin[currency] = (needed - current_balance)
18 # FIXME: don't remove too much if there are open margin position 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
19 if delta > 0: 24 if delta > 0:
20 if debug: 25 market.transfer_balance(currency, delta, "exchange", "margin")
21 print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency, delta))
22 else:
23 market.transfer_balance(currency, delta, "exchange", "margin")
24 elif delta < 0: 26 elif delta < 0:
25 if debug: 27 market.transfer_balance(currency, -delta, "margin", "exchange")
26 print("market.transfer_balance({}, {}, 'margin', 'exchange')".format(currency, -delta)) 28 ReportStore.log_move_balances(needed_in_margin, moving_to_margin, debug)
27 else:
28 market.transfer_balance(currency, -delta, "margin", "exchange")
29 29
30 BalanceStore.fetch_balances(market) 30 BalanceStore.fetch_balances(market)
31 31
@@ -73,6 +73,7 @@ def fetch_fees(market):
73 return fees_cache[market.__class__] 73 return fees_cache[market.__class__]
74 74
75def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", debug=False): 75def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", debug=False):
76 ReportStore.log_stage("prepare_trades")
76 BalanceStore.fetch_balances(market) 77 BalanceStore.fetch_balances(market)
77 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) 78 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
78 total_base_value = sum(values_in_base.values()) 79 total_base_value = sum(values_in_base.values())
@@ -82,6 +83,7 @@ def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_valu
82 TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug) 83 TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
83 84
84def update_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", only=None, debug=False): 85def update_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", only=None, debug=False):
86 ReportStore.log_stage("update_trades")
85 BalanceStore.fetch_balances(market) 87 BalanceStore.fetch_balances(market)
86 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) 88 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
87 total_base_value = sum(values_in_base.values()) 89 total_base_value = sum(values_in_base.values())
@@ -89,91 +91,76 @@ def update_trades(market, base_currency="BTC", liquidity="medium", compute_value
89 TradeStore.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug) 91 TradeStore.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug)
90 92
91def prepare_trades_to_sell_all(market, base_currency="BTC", compute_value="average", debug=False): 93def prepare_trades_to_sell_all(market, base_currency="BTC", compute_value="average", debug=False):
94 ReportStore.log_stage("prepare_trades_to_sell_all")
92 BalanceStore.fetch_balances(market) 95 BalanceStore.fetch_balances(market)
93 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) 96 values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
94 total_base_value = sum(values_in_base.values()) 97 total_base_value = sum(values_in_base.values())
95 new_repartition = BalanceStore.dispatch_assets(total_base_value, repartition={ base_currency: (1, "long") }) 98 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) 99 TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
97 100
98def follow_orders(verbose=True, sleep=None): 101def follow_orders(sleep=None):
99 if sleep is None: 102 if sleep is None:
100 sleep = 7 if TradeStore.debug else 30 103 sleep = 7 if TradeStore.debug else 30
104 if TradeStore.debug:
105 ReportStore.log_debug_action("Set follow_orders tick to {}s".format(sleep))
101 tick = 0 106 tick = 0
107 ReportStore.log_stage("follow_orders_begin")
102 while len(TradeStore.all_orders(state="open")) > 0: 108 while len(TradeStore.all_orders(state="open")) > 0:
103 time.sleep(sleep) 109 time.sleep(sleep)
104 tick += 1 110 tick += 1
105 for order in TradeStore.all_orders(state="open"): 111 open_orders = TradeStore.all_orders(state="open")
112 ReportStore.log_stage("follow_orders_tick_{}".format(tick))
113 ReportStore.log_orders(open_orders, tick=tick)
114 for order in open_orders:
106 if order.get_status() != "open": 115 if order.get_status() != "open":
107 if verbose: 116 ReportStore.log_order(order, tick, finished=True)
108 print("finished {}".format(order))
109 else: 117 else:
110 order.trade.update_order(order, tick) 118 order.trade.update_order(order, tick)
111 if verbose: 119 ReportStore.log_stage("follow_orders_end")
112 print("All orders finished")
113 120
114def print_orders(market, base_currency="BTC"): 121def print_orders(market, base_currency="BTC"):
122 ReportStore.log_stage("print_orders")
115 prepare_trades(market, base_currency=base_currency, compute_value="average", debug=True) 123 prepare_trades(market, base_currency=base_currency, compute_value="average", debug=True)
116 TradeStore.prepare_orders(compute_value="average") 124 TradeStore.prepare_orders(compute_value="average")
117 for currency, balance in BalanceStore.all.items():
118 print(balance)
119 TradeStore.print_all_with_order()
120 125
121def print_balances(market, base_currency="BTC"): 126def print_balances(market, base_currency="BTC"):
122 BalanceStore.fetch_balances(market) 127 BalanceStore.fetch_balances(market)
123 for currency, balance in BalanceStore.all.items():
124 print(balance)
125 if base_currency is not None: 128 if base_currency is not None:
126 print("total:") 129 ReportStore.print_log("total:")
127 print(sum(BalanceStore.in_currency(base_currency, market).values())) 130 ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values()))
128 131
129def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False): 132def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False):
133 ReportStore.log_stage("process_sell_needed__1_sell_begin")
130 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug) 134 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
131 TradeStore.prepare_orders(compute_value="average", only="dispose") 135 TradeStore.prepare_orders(compute_value="average", only="dispose")
132 print("------------------")
133 for currency, balance in BalanceStore.all.items():
134 print(balance)
135 print("------------------")
136 TradeStore.print_all_with_order()
137 print("------------------")
138 TradeStore.run_orders() 136 TradeStore.run_orders()
139 follow_orders() 137 follow_orders()
138 ReportStore.log_stage("process_sell_needed__1_sell_end")
140 139
141def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC", debug=False): 140def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC", debug=False):
141 ReportStore.log_stage("process_sell_needed__2_buy_begin")
142 update_trades(market, base_currency=base_currency, liquidity=liquidity, debug=debug, only="acquire") 142 update_trades(market, base_currency=base_currency, liquidity=liquidity, debug=debug, only="acquire")
143 TradeStore.prepare_orders(compute_value="average", only="acquire") 143 TradeStore.prepare_orders(compute_value="average", only="acquire")
144 print("------------------")
145 for currency, balance in BalanceStore.all.items():
146 print(balance)
147 print("------------------")
148 TradeStore.print_all_with_order()
149 print("------------------")
150 move_balances(market, debug=debug) 144 move_balances(market, debug=debug)
151 TradeStore.run_orders() 145 TradeStore.run_orders()
152 follow_orders() 146 follow_orders()
147 ReportStore.log_stage("process_sell_needed__2_buy_end")
153 148
154def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False, liquidity="medium"): 149def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False, liquidity="medium"):
150 ReportStore.log_stage("process_sell_all__1_all_sell_begin")
155 prepare_trades_to_sell_all(market, base_currency=base_currency, debug=debug) 151 prepare_trades_to_sell_all(market, base_currency=base_currency, debug=debug)
156 TradeStore.prepare_orders(compute_value="average") 152 TradeStore.prepare_orders(compute_value="average")
157 print("------------------")
158 for currency, balance in BalanceStore.all.items():
159 print(balance)
160 print("------------------")
161 TradeStore.print_all_with_order()
162 print("------------------")
163 TradeStore.run_orders() 153 TradeStore.run_orders()
164 follow_orders() 154 follow_orders()
155 ReportStore.log_stage("process_sell_all__1_all_sell_end")
165 156
166def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False, liquidity="medium"): 157def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False, liquidity="medium"):
158 ReportStore.log_stage("process_sell_all__2_all_buy_begin")
167 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug) 159 prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
168 TradeStore.prepare_orders(compute_value="average") 160 TradeStore.prepare_orders(compute_value="average")
169 print("------------------")
170 for currency, balance in BalanceStore.all.items():
171 print(balance)
172 print("------------------")
173 TradeStore.print_all_with_order()
174 print("------------------")
175 move_balances(market, debug=debug) 161 move_balances(market, debug=debug)
176 TradeStore.run_orders() 162 TradeStore.run_orders()
177 follow_orders() 163 follow_orders()
164 ReportStore.log_stage("process_sell_all__2_all_buy_end")
178 165
179 166