ctubio / Krypto-trading-bot

Self-hosted crypto trading bot (automated high frequency market making) written in C++
https://127.0.0.1:3000
Other
3.37k stars 823 forks source link

Support margin trading (initially on Bitfinex) #476

Closed jthomaskerr closed 6 years ago

jthomaskerr commented 6 years ago

I'd really like to use this bot with my margin wallet on Bitfinex.

As far as I can see this is purely a matter of adding a flag to select the margin wallet, and using this flag to change a couple of parameters in the orders REST and WS api calls.

I'd be very happy to implement this improvement, but having looked through the code in this repo, it appears that these parameters are set in the precompiled binary that is not open source.

Is there a process for making improvements to these? Is this outside the scope of this project?

ctubio commented 6 years ago

yea im sorry the api integrations are outside of this github project, but thanks for the suggestion, i will consider soOn''

jthomaskerr commented 6 years ago

can i pay you a small bounty to get it done very soon?

It seems like a very small amount of work.

ctubio commented 6 years ago

only if you are happy with the final result; im currently updating json to v3 and later i look for thiz if so you can grab then my addr from the commit msg easy

ctubio commented 6 years ago

if you have any issue please open a new one; v1 of bitfinex margin is done :dancer:

jthomaskerr commented 6 years ago

Fantastic, thanks so much! How does one activate it? It doesnt look like its in the ui and I also dont see any changes in the config options. I tried "BITFINEX MARGIN" but that doesn't work.

ctubio commented 6 years ago

reinstall and make use of the new config value (see https://github.com/ctubio/Krypto-trading-bot/blob/master/etc/K.sh.dist#L50)

jthomaskerr commented 6 years ago

Unfortunately this isn't quite right. In margin mode we are not trading with the funds in our wallet. Consequently the way that this is implemented is not quite right.

It actually requires a couple of separate calls.

https://docs.bitfinex.com/v1/reference#rest-auth-active-positions gives you the balance of the currency you are trading, and

https://docs.bitfinex.com/v1/reference#rest-auth-margin-information gives the balance of the base currency (in margin_limits).

Note also that balances may be negative (if you are shorting).

ctubio commented 6 years ago

we dont use REST v1 for wallet data

WS v2 is what we use (see https://docs.bitfinex.com/v2/reference#ws-auth-wallets)

do you mean we miss this https://docs.bitfinex.com/v2/reference#ws-auth-margin ? or can you point to the reference in the ws v2 docs? thanks for your time''

also we are not reading exchange funds when using margin, we ask for WALLET_TYPE = margin

jthomaskerr commented 6 years ago

Sorry for the delay. Yes, good point re websocket rather than rest.

also we are not reading exchange funds when using margin, we ask for WALLET_TYPE = margin

The margin wallet works slightly differently to the exchange wallet. You do not trade with the funds in your margin wallet, these are used as collateral to access borrowed funds, which are what you trade with.

This means that "balance" is effectively split into two separate figures:

  1. The amount of the base currency that you have available to borrow. (for the symbol LTC/USD, for example, you are borrowing USD to buy LTC); and
  2. The amount of the trading currency that you have currently borrowed. (LTC, in the above example).

To retrieve these:

  1. To get the available funds for any coin, you would look at the https://docs.bitfinex.com/v2/reference#ws-auth-margin TRADEABLE_BALANCE field, although it will be often desirable to be able to add a user-configurable hard limit to that, since it is borrowed money, and it is also shared between all coins.

I can add the UI and adapter code for this if you like.

  1. To get the current "balance" of the coin you are buying/selling, you would look at the AMOUNT field of https://docs.bitfinex.com/v2/reference#ws-auth-position.
ctubio commented 6 years ago

thanks for the info''