aboutsummaryrefslogtreecommitdiff
path: root/ccxt_wrapper.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-01 13:14:41 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-01 13:14:41 +0100
commitaca4d4372553110ab5d76740ff536de83d5617b2 (patch)
treea9bfdca4226daf422273da97a9e139721469c9f1 /ccxt_wrapper.py
parent2033e7fef780298be2ec15455a0ec1d26515de55 (diff)
downloadTrader-aca4d4372553110ab5d76740ff536de83d5617b2.tar.gz
Trader-aca4d4372553110ab5d76740ff536de83d5617b2.tar.zst
Trader-aca4d4372553110ab5d76740ff536de83d5617b2.zip
Various fixes/improvementsv0.3
- Use pending gains to compute the move_balance - Use ttl_cache for tickers
Diffstat (limited to 'ccxt_wrapper.py')
-rw-r--r--ccxt_wrapper.py109
1 files changed, 96 insertions, 13 deletions
diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py
index b79cd37..903bfc4 100644
--- a/ccxt_wrapper.py
+++ b/ccxt_wrapper.py
@@ -38,12 +38,13 @@ class poloniexE(poloniex):
38 """ 38 """
39 portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) 39 portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"})
40 See DASH/BTC positions 40 See DASH/BTC positions
41 {'amount': '-0.10000000', -> DASH empruntés 41 {'amount': '-0.10000000', -> DASH empruntés (à rendre)
42 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) 42 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%))
43 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur 43 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur en intérêts
44 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) 44 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin)
45 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) 45 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu)
46 'total': '0.00681856', -> valeur totale empruntée en BTC 46 'total': '0.00681856', -> valeur totale empruntée en BTC (au moment de l'échange)
47 = amount * basePrice à erreur d'arrondi près
47 'type': 'short'} 48 'type': 'short'}
48 """ 49 """
49 positions = self.privatePostGetMarginPosition({"currencyPair": "all"}) 50 positions = self.privatePostGetMarginPosition({"currencyPair": "all"})
@@ -70,8 +71,7 @@ class poloniexE(poloniex):
70 for key, balance in balances.items(): 71 for key, balance in balances.items():
71 result[key] = {} 72 result[key] = {}
72 for currency, amount in balance.items(): 73 for currency, amount in balance.items():
73 if currency not in result: 74 result.setdefault(currency, {})
74 result[currency] = {}
75 result[currency][key] = decimal.Decimal(amount) 75 result[currency][key] = decimal.Decimal(amount)
76 result[key][currency] = decimal.Decimal(amount) 76 result[key][currency] = decimal.Decimal(amount)
77 return result 77 return result
@@ -83,6 +83,7 @@ class poloniexE(poloniex):
83 83
84 all_balances = {} 84 all_balances = {}
85 in_positions = {} 85 in_positions = {}
86 pending_pl = {}
86 87
87 for currency, exchange_balance in exchange_balances.items(): 88 for currency, exchange_balance in exchange_balances.items():
88 if currency in ["info", "free", "used", "total"]: 89 if currency in ["info", "free", "used", "total"]:
@@ -96,25 +97,106 @@ class poloniexE(poloniex):
96 "exchange_used": exchange_balance["used"], 97 "exchange_used": exchange_balance["used"],
97 "exchange_total": exchange_balance["total"] - balance_per_type.get("margin", 0), 98 "exchange_total": exchange_balance["total"] - balance_per_type.get("margin", 0),
98 "exchange_free": exchange_balance["free"] - balance_per_type.get("margin", 0), 99 "exchange_free": exchange_balance["free"] - balance_per_type.get("margin", 0),
99 "margin_free": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), 100 # Disponible sur le compte margin
100 "margin_borrowed": 0, 101 "margin_available": balance_per_type.get("margin", 0),
102 # Bloqué en position
103 "margin_in_position": 0,
104 # Emprunté
105 "margin_borrowed": -margin_balance.get("amount", 0),
106 # Total
101 "margin_total": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), 107 "margin_total": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0),
108 "margin_pending_gain": 0,
102 "margin_lending_fees": margin_balance.get("lendingFees", 0), 109 "margin_lending_fees": margin_balance.get("lendingFees", 0),
103 "margin_pending_gain": margin_balance.get("pl", 0), 110 "margin_pending_base_gain": margin_balance.get("pl", 0),
104 "margin_position_type": margin_balance.get("type", None), 111 "margin_position_type": margin_balance.get("type", None),
105 "margin_liquidation_price": margin_balance.get("liquidationPrice", 0), 112 "margin_liquidation_price": margin_balance.get("liquidationPrice", 0),
106 "margin_borrowed_base_price": margin_balance.get("total", 0), 113 "margin_borrowed_base_price": margin_balance.get("total", 0),
107 "margin_borrowed_base_currency": margin_balance.get("baseCurrency", None), 114 "margin_borrowed_base_currency": margin_balance.get("baseCurrency", None),
108 } 115 }
109 if len(margin_balance) > 0: 116 if len(margin_balance) > 0:
110 if margin_balance["baseCurrency"] not in in_positions: 117 in_positions.setdefault(margin_balance["baseCurrency"], 0)
111 in_positions[margin_balance["baseCurrency"]] = 0
112 in_positions[margin_balance["baseCurrency"]] += margin_balance["total"] 118 in_positions[margin_balance["baseCurrency"]] += margin_balance["total"]
113 119
120 pending_pl.setdefault(margin_balance["baseCurrency"], 0)
121 pending_pl[margin_balance["baseCurrency"]] += margin_balance["pl"]
122
123 # J’emprunte 0.12062983 que je revends à 0.06003598 BTC/DASH, soit 0.00724213 BTC.
124 # Sur ces 0.00724213 BTC je récupère 0.00724213*(1-0.0015) = 0.00723127 BTC
125 #
126 # -> ordertrades ne tient pas compte des fees
127 # amount = montant vendu (un seul mouvement)
128 # rate = à ce taux
129 # total = total en BTC (pour ce mouvement)
130 # -> marginposition:
131 # amount = ce que je dois rendre
132 # basePrice = prix de vente en tenant compte des fees
133 # (amount * basePrice = la quantité de BTC que j’ai effectivement
134 # reçue à erreur d’arrondi près, utiliser plutôt "total")
135 # total = la quantité de BTC que j’ai reçue
136 # pl = plus value actuelle si je rachetais tout de suite
137 # -> marginaccountsummary:
138 # currentMargin = La marge actuelle (= netValue/totalBorrowedValue)
139 # totalValue = BTC actuellement en margin (déposé)
140 # totalBorrowedValue = sum (amount * ticker[lowestAsk])
141 # pl = sum(pl)
142 # netValue = BTC actuellement en margin (déposé) + pl
143 # Exemple:
144 # In [38]: m.ccxt.private_post_returnordertrades({"orderNumber": "XXXXXXXXXXXX"})
145 # Out[38]:
146 # [{'amount': '0.11882982',
147 # 'currencyPair': 'BTC_DASH',
148 # 'date': '2018-02-26 22:48:35',
149 # 'fee': '0.00150000',
150 # 'globalTradeID': 348891380,
151 # 'rate': '0.06003598',
152 # 'total': '0.00713406',
153 # 'tradeID': 9634443,
154 # 'type': 'sell'},
155 # {'amount': '0.00180000',
156 # 'currencyPair': 'BTC_DASH',
157 # 'date': '2018-02-26 22:48:30',
158 # 'fee': '0.00150000',
159 # 'globalTradeID': 348891375,
160 # 'rate': '0.06003598',
161 # 'total': '0.00010806',
162 # 'tradeID': 9634442,
163 # 'type': 'sell'}]
164 #
165 # In [51]: m.ccxt.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"})
166 # Out[51]:
167 # {'amount': '-0.12062982',
168 # 'basePrice': '0.05994587',
169 # 'lendingFees': '0.00000000',
170 # 'liquidationPrice': '0.15531479',
171 # 'pl': '0.00000122',
172 # 'total': '0.00723126',
173 # 'type': 'short'}
174 # In [52]: m.ccxt.privatePostGetMarginPosition({"currencyPair": "BTC_BTS"})
175 # Out[52]:
176 # {'amount': '-332.97159188',
177 # 'basePrice': '0.00002171',
178 # 'lendingFees': '0.00000000',
179 # 'liquidationPrice': '0.00005543',
180 # 'pl': '0.00029548',
181 # 'total': '0.00723127',
182 # 'type': 'short'}
183 #
184 # In [53]: m.ccxt.privatePostReturnMarginAccountSummary()
185 # Out[53]:
186 # {'currentMargin': '1.04341991',
187 # 'lendingFees': '0.00000000',
188 # 'netValue': '0.01478093',
189 # 'pl': '0.00029666',
190 # 'totalBorrowedValue': '0.01416585',
191 # 'totalValue': '0.01448427'}
192
114 for currency, in_position in in_positions.items(): 193 for currency, in_position in in_positions.items():
115 all_balances[currency]["total"] += in_position 194 all_balances[currency]["total"] += in_position
195 all_balances[currency]["margin_in_position"] += in_position
116 all_balances[currency]["margin_total"] += in_position 196 all_balances[currency]["margin_total"] += in_position
117 all_balances[currency]["margin_borrowed"] += in_position 197
198 for currency, pl in pending_pl.items():
199 all_balances[currency]["margin_pending_gain"] += pl
118 200
119 return all_balances 201 return all_balances
120 202
@@ -228,8 +310,9 @@ class poloniexE(poloniex):
228 'lendingFees': '0.00000000', -> fees totaux 310 'lendingFees': '0.00000000', -> fees totaux
229 'netValue': '0.01008254', -> balance + plus-value 311 'netValue': '0.01008254', -> balance + plus-value
230 'pl': '0.00008254', -> plus value latente (somme des positions) 312 'pl': '0.00008254', -> plus value latente (somme des positions)
231 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée 313 'totalBorrowedValue': '0.00673602', -> valeur empruntée convertie en BTC.
232 'totalValue': '0.01000000'} -> valeur totale en compte 314 (= sum(amount * ticker[lowerAsk]) pour amount dans marginposition)
315 'totalValue': '0.01000000'} -> balance (collateral déposé en margin)
233 """ 316 """
234 summary = self.privatePostReturnMarginAccountSummary() 317 summary = self.privatePostReturnMarginAccountSummary()
235 318