aboutsummaryrefslogtreecommitdiff
path: root/script.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-14 03:02:11 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-14 03:02:11 +0100
commit89b80539deeca58cf8d063a8c75706050be485ab (patch)
tree16915e91e3ab7685a809f341a45048dfdebbb0a3 /script.py
parent48bedc24dc34159fdf0ddaa5daaa055596c11d5c (diff)
downloadTrader-89b80539deeca58cf8d063a8c75706050be485ab.tar.gz
Trader-89b80539deeca58cf8d063a8c75706050be485ab.tar.zst
Trader-89b80539deeca58cf8d063a8c75706050be485ab.zip
Add order processing functions
Diffstat (limited to 'script.py')
-rw-r--r--script.py114
1 files changed, 89 insertions, 25 deletions
diff --git a/script.py b/script.py
index c702be6..c5ac4aa 100644
--- a/script.py
+++ b/script.py
@@ -83,7 +83,7 @@ def dispatch_assets(base_currency_value, repartition_pertenthousand, market, bas
83 repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand) 83 repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand)
84 return repartition_in_base_currency 84 return repartition_in_base_currency
85 85
86def give_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"): 86def compute_moves(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
87 value_in_base = assets_value(current_assets, market, base_currency=base_currency) 87 value_in_base = assets_value(current_assets, market, base_currency=base_currency)
88 total_base_value = sum([ v for k, v in value_in_base.items()]) 88 total_base_value = sum([ v for k, v in value_in_base.items()])
89 89
@@ -93,33 +93,97 @@ def give_orders(current_assets, repartition_pertenthousand, market, base_currenc
93 for key in set(value_in_base.keys()).union(set(new_repartition.keys())): 93 for key in set(value_in_base.keys()).union(set(new_repartition.keys())):
94 mouvements[key] = value_in_base.get(key, 0) - new_repartition.get(key, 0) 94 mouvements[key] = value_in_base.get(key, 0) - new_repartition.get(key, 0)
95 95
96 print("assets before repartition:") 96 return mouvements
97 for currency, value in current_assets.items(): 97
98 print("holding {} {}".format( 98def compute_order(currency, value, market, base_currency="BTC"):
99 formatted_price(value), 99 if currency == base_currency or value == 0:
100 currency)) 100 return [None, 0, False]
101 print("------------") 101
102 for currency, value in mouvements.items(): 102 asset_ticker = get_ticker(currency, base_currency, market)
103 if currency == base_currency: 103 if asset_ticker["inverted"]:
104 continue 104 asset_ticker = get_ticker(base_currency, currency, market)
105 asset_ticker = get_ticker(currency, base_currency, market) 105 if value > 0:
106 rate = asset_ticker["askA"]
107 return ["buy", rate, True]
108 else:
109 rate = asset_ticker["bidA"]
110 return ["sell", rate, True]
111 else:
106 if value > 0: 112 if value > 0:
107 action = "sell" 113 rate = asset_ticker["bidA"]
108 currency_price = int(value / asset_ticker["bid"]) 114 return ["sell", rate, False]
109 else: 115 else:
110 action = "buy" 116 rate = asset_ticker["askA"]
111 currency_price = int(value / asset_ticker["ask"]) 117 return ["buy", rate, False]
112 if value != 0: 118
119def make_order(currency, value, market, base_currency="BTC"):
120 action, rate, inverted = compute_order(currency, value, market, base_currency=base_currency)
121 amount = formatted_price(abs(value))
122 if not inverted:
123 symbol = "{}/{}".format(currency, base_currency)
124 else:
125 symbol = "{}/{}".format(base_currency, currency)
126 return market.create_order(symbol, 'limit', action, amount, price=rate)
127
128def make_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
129 mouvements = compute_moves(
130 current_assets,
131 repartition_pertenthousand,
132 market,
133 base_currency=base_currency)
134
135 results = []
136 for currency, value in sorted(mouvements.items(), key=lambda x: x[1]):
137 # FIXME: wait for sales to finish
138 results.append(make_order(currency, value, market, base_currency=base_currency))
139 return results
140
141def print_assets(assets, indent="", market=None, base_currency="BTC"):
142 if market is not None:
143 format_string = "{}{} {} ({} {})"
144 else:
145 format_string = "{}{} {}"
146 base_currency_price = 0
147
148 for currency, value in assets.items():
149 if market is not None:
150 asset_ticker = get_ticker(currency, base_currency, market)
151 base_currency_price = asset_ticker["bidA"] * value
152 print(format_string.format(
153 indent,
154 formatted_price(value),
155 currency,
156 formatted_price(base_currency_price),
157 base_currency))
158
159def print_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
160 mouvements = compute_moves(
161 current_assets,
162 repartition_pertenthousand,
163 market,
164 base_currency=base_currency)
165
166 for currency, value in mouvements.items():
167 action, rate, inverted = compute_order(
168 currency,
169 value,
170 market,
171 base_currency=base_currency)
172 if action is not None:
173 currency_price = int(value / rate)
174
175 if not inverted:
176 c1, c2 = [base_currency, currency]
177 v1, v2 = [value, currency_price]
178 else:
179 c1, c2 = [currency, base_currency]
180 v1, v2 = [currency_price, value]
181
113 print("need to {} {} {}'s worth of {}, i.e. {} {}".format( 182 print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
114 action, 183 action,
115 formatted_price(abs(value)), 184 formatted_price(abs(v1)), c1,
116 base_currency, 185 c2,
117 currency, 186 formatted_price(abs(v2)), c2))
118 formatted_price(abs(currency_price)),
119 currency))
120 print("------------\nassets after repartition:")
121 for currency, value in new_repartition.items():
122 print("holding {} {}".format(formatted_price(value), currency))
123 187
124current_assets = fetch_balances(market) 188current_assets = fetch_balances(market)
125give_orders(current_assets, repartition_pertenthousand, market) 189print_orders(current_assets, repartition_pertenthousand, market)