BitBotFactory / MikaLendingBot

Automated lending on Cryptocurrency exchanges Poloniex and Bitfinex
http://poloniexlendingbot.readthedocs.io/en/latest/index.html
MIT License
1.11k stars 345 forks source link

Feature: Delayed Auto-transfer #371

Open bundabrg opened 7 years ago

bundabrg commented 7 years ago

When using auto-transfer AND using any of the maxtolend or maxpercentolend options, it would be nice if the bot only did the transfer of balances when it needed them.

This way I can set the bot to leave 20% for high interest rate items, and actively trade with them. If anything is not traded (and thus available) AND high interest kicks in, the bot can then transfer what it needs to the lending account.

Looking through the code it looks relatively straight forward, but will just need to take into account all available balances rather than just the lending balance. My additional feature I'm going to code in (but is out of scope for this bot to suggest here) is for it to be able to cancel any existing stop/limit orders (that is, any cancellable orders) as needed to transfer the funds to lending.

Evanito commented 7 years ago

Hmm, that would mean maxtolend would make the calculation using both your exchange and lending balances if transfercurrecies is enabled. If it wanted to make a lend using balance tied up in exchange it would require one extra api request.

Not a bad idea however seems very niche in use.

Your second suggestion sounds like it could be made into an optional plugin (documentation on how to make a plugin is pending, sorry.)

bundabrg commented 7 years ago

I've just managed to implement the first but it required creating a function that stored the full results of api.return_available_account_balances('all'), then summing up the transferable currencies into their own structure, then referencing that.

Its a little hacky at the moment but I'll play around and see if I can tidy it up. One thing I noticed is that when an order is created, its amount is truncated using Data.truncate, but the results returned is a float. This means that on the odd occasion a round error results later when the loan attempts to be made. I'm playing with the Decimal.quantize method to properly round that to 8 decimal places whilst not converting back to float which seems to minimize that error.

Do like the idea of the plugins. I had a look and so far they only support before and after lends, but additional hooks will be useful.

bundabrg commented 7 years ago

My changes, with lots of debugging prints: https://github.com/bundabrg/poloniexlendingbot I'll clean it up once I know its working (seems to be so far).

Started WebServer, lendingbot status available at http://127.0.0.1:8000/lendingbot.html, 
http://10.64.11.43:8000/lendingbot.html
We need 0.01423843 in lending account for BTC
We now have enough
We need 0.01423841 in lending account for BTC
We now have enough
We need 0.01423841 in lending account for BTC
We now have enough
bundabrg commented 7 years ago

I ended up solving this on my fork https://github.com/bundabrg/poloniexlendingbot. Solves all the points in my original post by:

  1. Implementing a FundManager - This provides an interface for fund sources. By default it provides ones from the 'exchange', 'lending' and 'margin' accounts, controllable through a config entry. Funding sources are added to the manager in priority so when the bot needs funds it will go through in order of priority to get the amount it needs ONLY at the point it needs the funds.

  2. Added a new plugin 'FundOption' that adds a fund source of any open orders. Its added with a higher priority (so used as a last resort) and if funds are are pulled from them it will cancel the order and transfer the exact amount into the correct account.

This allows the existing automaticTransfer code to still work as before, results in minimal changes to Lending.py, and makes use of the plugin interface. For example I can envisage a plugin that provides a fund source from a private wallet that when it needs the funds it can do a full bitcoin transaction to the exchange first.

praeluceo commented 7 years ago

No useful input other than that looks pretty slick @bundabrg!

rnevet commented 7 years ago

Im very happy to see the Plug-in system in work, are you making a PR?

bundabrg commented 7 years ago

I'll be honest. I've never done a PR before so I was going to let it run for a while to be absolutely sure it looks useful whilst merging in updates from here, then I was going to investigate exactly the process to perform a PR.

rnevet commented 7 years ago

Make a PR, we'll review your code, give feedback, people will test it... Don't worry. :)

Evanito commented 7 years ago

From #424: Also include ability to keep not only a percentage in exchange, but also possibly a raw value.