2 from ccxt
import ExchangeError
5 def move_balances(market
, debug
=False):
7 for currency
in BalanceStore
.all
:
8 if BalanceStore
.all
[currency
].margin_free
!= 0:
9 needed_in_margin
[currency
] = 0
10 for trade
in TradeStore
.all
:
11 if trade
.value_to
.currency
not in needed_in_margin
:
12 needed_in_margin
[trade
.value_to
.currency
] = 0
13 if trade
.trade_type
== "short":
14 needed_in_margin
[trade
.value_to
.currency
] += abs(trade
.value_to
)
15 for currency
, needed
in needed_in_margin
.items():
16 current_balance
= BalanceStore
.all
[currency
].margin_free
17 delta
= (needed
- current_balance
).value
18 # FIXME: don't remove too much if there are open margin position
21 print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency
, delta
))
23 market
.transfer_balance(currency
, delta
, "exchange", "margin")
26 print("market.transfer_balance({}, {}, 'margin', 'exchange')".format(currency
, -delta
))
28 market
.transfer_balance(currency
, -delta
, "margin", "exchange")
30 BalanceStore
.fetch_balances(market
)
33 ticker_cache_timestamp
= time
.time()
34 def get_ticker(c1
, c2
, market
, refresh
=False):
35 global ticker_cache
, ticker_cache_timestamp
39 "average": (1/ticker
["bid"] + 1/ticker
["ask"]) / 2,
42 def augment_ticker(ticker
):
45 "average": (ticker
["bid"] + ticker
["ask"] ) / 2,
48 if time
.time() - ticker_cache_timestamp
> 5:
50 ticker_cache_timestamp
= time
.time()
52 if (c1
, c2
, market
.__class
__) in ticker_cache
:
53 return ticker_cache
[(c1
, c2
, market
.__class
__)]
54 if (c2
, c1
, market
.__class
__) in ticker_cache
:
55 return invert(ticker_cache
[(c2
, c1
, market
.__class
__)])
58 ticker_cache
[(c1
, c2
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c1
, c2
))
59 augment_ticker(ticker_cache
[(c1
, c2
, market
.__class
__)])
62 ticker_cache
[(c2
, c1
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c2
, c1
))
63 augment_ticker(ticker_cache
[(c2
, c1
, market
.__class
__)])
65 ticker_cache
[(c1
, c2
, market
.__class
__)] = None
66 return get_ticker(c1
, c2
, market
)
69 def fetch_fees(market
):
71 if market
.__class
__ not in fees_cache
:
72 fees_cache
[market
.__class
__] = market
.fetch_fees()
73 return fees_cache
[market
.__class
__]
75 def prepare_trades(market
, base_currency
="BTC", compute_value
="average", debug
=False):
76 BalanceStore
.fetch_balances(market
)
77 values_in_base
= BalanceStore
.in_currency(base_currency
, market
, compute_value
=compute_value
)
78 total_base_value
= sum(values_in_base
.values())
79 new_repartition
= BalanceStore
.dispatch_assets(total_base_value
)
80 # Recompute it in case we have new currencies
81 values_in_base
= BalanceStore
.in_currency(base_currency
, market
, compute_value
=compute_value
)
82 TradeStore
.compute_trades(values_in_base
, new_repartition
, market
=market
, debug
=debug
)
84 def update_trades(market
, base_currency
="BTC", compute_value
="average", only
=None, debug
=False):
85 BalanceStore
.fetch_balances(market
)
86 values_in_base
= BalanceStore
.in_currency(base_currency
, market
, compute_value
=compute_value
)
87 total_base_value
= sum(values_in_base
.values())
88 new_repartition
= BalanceStore
.dispatch_assets(total_base_value
)
89 TradeStore
.compute_trades(values_in_base
, new_repartition
, only
=only
, market
=market
, debug
=debug
)
91 def prepare_trades_to_sell_all(market
, base_currency
="BTC", compute_value
="average", debug
=False):
92 BalanceStore
.fetch_balances(market
)
93 values_in_base
= BalanceStore
.in_currency(base_currency
, market
, compute_value
=compute_value
)
94 total_base_value
= sum(values_in_base
.values())
95 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
)
98 def follow_orders(verbose
=True, sleep
=None):
100 sleep
= 7 if TradeStore
.debug
else 30
102 while len(TradeStore
.all_orders(state
="open")) > 0:
105 for order
in TradeStore
.all_orders(state
="open"):
106 if order
.get_status() != "open":
108 print("finished {}".format(order
))
110 order
.trade
.update_order(order
, tick
)
112 print("All orders finished")
114 def print_orders(market
, base_currency
="BTC"):
115 prepare_trades(market
, base_currency
=base_currency
, compute_value
="average", debug
=True)
116 TradeStore
.prepare_orders(compute_value
="average")
117 for currency
, balance
in BalanceStore
.all
.items():
119 TradeStore
.print_all_with_order()
121 def process_sell_needed__1_sell(market
, base_currency
="BTC", debug
=False):
122 prepare_trades(market
, base_currency
=base_currency
, debug
=debug
)
123 TradeStore
.prepare_orders(compute_value
="average", only
="dispose")
124 print("------------------")
125 for currency
, balance
in BalanceStore
.all
.items():
127 print("------------------")
128 TradeStore
.print_all_with_order()
129 print("------------------")
130 TradeStore
.run_orders()
133 def process_sell_needed__2_buy(market
, base_currency
="BTC", debug
=False):
134 update_trades(market
, base_currency
=base_currency
, debug
=debug
, only
="acquire")
135 TradeStore
.prepare_orders(compute_value
="average", only
="acquire")
136 print("------------------")
137 for currency
, balance
in BalanceStore
.all
.items():
139 print("------------------")
140 TradeStore
.print_all_with_order()
141 print("------------------")
142 move_balances(market
, debug
=debug
)
143 TradeStore
.run_orders()
146 def process_sell_all__1_all_sell(market
, base_currency
="BTC", debug
=False):
147 prepare_trades_to_sell_all(market
, base_currency
=base_currency
, debug
=debug
)
148 TradeStore
.prepare_orders(compute_value
="average")
149 print("------------------")
150 for currency
, balance
in BalanceStore
.all
.items():
152 print("------------------")
153 TradeStore
.print_all_with_order()
154 print("------------------")
155 TradeStore
.run_orders()
158 def process_sell_all__2_all_buy(market
, base_currency
="BTC", debug
=False):
159 prepare_trades(market
, base_currency
=base_currency
, debug
=debug
)
160 TradeStore
.prepare_orders()
161 print("------------------")
162 for currency
, balance
in BalanceStore
.all
.items():
164 print("------------------")
165 TradeStore
.print_all_with_order()
166 print("------------------")
167 move_balances(market
, debug
=debug
)
168 TradeStore
.run_orders()