askmike / gekko

A bitcoin trading bot written in node - https://gekko.wizb.it/
MIT License
10.07k stars 3.94k forks source link

[DISCUSSION] trade with partial balance #937

Closed metachris closed 5 years ago

metachris commented 7 years ago

I would like to experiment with strategies that issue multiple buy advices, each only buying for a part of the current balance (eg. in percent). Perhaps a nice implementation would be like this: this.advice('long', 10) (10 for 10% of balance).

See also issue #438, PRs #448, #449, #450

Any thoughts on this topic or any reasons why this feature might not be desirable?

askmike commented 7 years ago

This has been a major point of discussion, so here are the hard parts:

metachris commented 7 years ago

Thanks for your thoughts.

I was primarily thinking of strategy development; to play around with strategies issuing multiple buys and later selling (or vice versa). Not sure if this has potential or not though.

thegamecat commented 7 years ago

What risk metric calculations exist when you make a bid/ask decision?

askmike commented 7 years ago

Not sure if this has potential or not though.

To build a strat that can buy in small parts over time? I think it definitely has, this way you can have different levels of risk exposure your strat is willing to accept at certain points. Though the next step would be to have this logic close to the orderbook, to allow for specific types of orders in specific cases (which Gekko does not expose to strategies at all).


What risk metric calculations exist when you make a bid/ask decision?

The profit metric as well as all roundtrips (and thus risk metrics like sharpe ratio) are based on how much your portfolio increased because of the trades. If you could configure Gekko to only do 50 USD trades whilst having 100,000 USD on your exchange account your profit and loss (as calculated by gekko) would never be low or high, since if you make 50 euro your balance would only go to 100,050 USD.

thegamecat commented 7 years ago

Ah I see. I thought you meant there was a predictive element based on a potential trade....I was going to say that's a hell of a miss by me!

metachris commented 7 years ago

@askmike do you think this would be a good addition to gekko // do you want to have that?

askmike commented 7 years ago

Yes definitely! And not just me if you look at all the issues around this.

This is what I was thinking:

We just need to make it VERY clear (in the docs, in the UI) that even if we limit order size to X {currency}, Gekko could still lose more over time. Do you think any of these options (I'd say the second one makes the most sense) would satisfy what people are asking for?

askmike commented 7 years ago

Another possible solution: https://github.com/askmike/gekko/pull/968#issuecomment-322512018

decoye commented 7 years ago

Hey hey,

ive made a similar request some time ago.

We just need to make it VERY clear (in the docs, in the UI) that even if we limit order size to X {currency}, Gekko could still lose more over time. Do you think any of these options (I'd say the second one makes the most sense) would satisfy what people are asking for?

This has also been your answer to it back then. but i didnt get the chance to follow up on it.

I see the whole thing not as something to protect my currency on the trading platform. Not at all. i Dont wish to put 10k there and only trade with 1k. What i think this is useful for, is to diversify my account balance to invest on different markets or asset pairs that i think are profitable.

In the current behaviour of gekko, i have to setup another account on that platform, to make this work. but this can be a huge time consuming task dependend on the platform.

i for myself think i have a profitable strategy for eth/usd, but as well i have one for dash/eth. so id like to trade both of them at the same time, as i belive this will be even more performant.

askmike commented 7 years ago

@metachris @thegamecat @decoye

What would your ideal implementation be?

  1. Gekko trades a percentage of total balance (this will always be based on a % of the value of currency + asset, NOT on other any other assets/currencies you have money in on the exchange).
  2. Gekko can use a fixed amount (specified in either asset or currency)
  3. Gekko can NOT use a fixed amount (specified in either asset or currency)
  4. When a Trading Gekko lost a fixed amount (specified in either asset or currency) the trader will stop trading.
  5. other.
decoye commented 7 years ago

Hey hey,

my idea of how the implementation would be, is to have a variable in the config.js where you setup the platform on what to trade on then the asset pair and the percentage of the total amount available to that account, plus also a max limit of what is able to be lost by the strategy.

config.trader = {
  enabled: true,
  key: '',
  secret: '',
  username: '', // your username, only required for specific exchanges.
  passphrase: '' // GDAX, requires a passphrase.
  percentage: '100'  //specify the total percentage that gekko is able to trade with from the total 
                       amount
  lossprotection: '100' //how much is this strategy able to loose in currency before it stops trading.
}

I would pro to a percentage, because a percentage runs well with every research you can do about risk management, and a percentage will always scale to a growing or dying account, a fixed amount wont do that.

thats why my vote would be on percentage with a stop at a certain loss.

askmike commented 7 years ago

@decoye so you propose:

Gekko can NOT use a fixed amount (specified in either asset or currency)

except percentage based. But how would that work? If I set it to 50% and I have 3BTC and 1000USD in my account. How much can Gekko lose? 50% of each? 50% of the value as calculed on start?

decoye commented 7 years ago

in this case i would take it for granted that the 3 btc are already handled by another strategy, or at least it is managed in some other way. so id suggest that gekko only sees the values that are on the account when the strategy is started.

so in this specific case. with this settings applied.

config.trader = {
  enabled: true,
  key: '',
  secret: '',
  username: '', // your username, only required for specific exchanges.
  passphrase: '' // GDAX, requires a passphrase.
  percentage: '100'  //specify the total percentage that gekko is able to trade with from the total 
                       amount
  lossprotection: '100' //how much is this strategy able to loose in currency before it stops trading.
}

Gekko can trade 1000€ and is able to loose 100€ tops.

there is one exception to this thought tho. what is when a user wants to trade btc/eth?

Also in this case i would handle it similar. lets imagine we have 3 btc and 10 eth.

then gekko is able to manage, with the above settings, 10 eth, and is able to loose 100 in currency.

decoye commented 7 years ago

Maybe also another variable to add is currency:' ' // EUR, USD, USDT, CAD, JPY, whatever. So that its always clear what gekko will assume what a currency is.

decoye commented 7 years ago
config.trader = {
  enabled: true,
  key: '',
  secret: '',
  username: '', // your username, only required for specific exchanges.
  passphrase: '' // GDAX, requires a passphrase.
  percentage: '100'  //specify the total percentage that gekko is able to trade with from the total 
                       amount
  lossprotection: '100' //how much is this strategy able to loose in currency before it stops trading.
  currency:' ' // EUR, USD, USDT, CAD, JPY, whatever.
}
jaguar666 commented 7 years ago

in addition to this topic, i would suggest that gekko have this features:

percentage_buy: '60' //50-80% currency percentage that gekko will use to buy but of course gekko can still use another 60% from the remaining currency balance if the price will still continue dropping.

percentage_sell: '100' //asset percentage that gekko will use to sell (asset total amount from 60% currency used) - it should be 100% to sell to prevent losses on future price drop, no need to wait when the price will continue go higher in order to sell the remaining asset.

lossprotection: '5' // 5-10% maximum losses you allow that gekko can sell your last bought asset in the case of abrupt big price drop.

LooOOooM commented 6 years ago

what about simply manipulating the portfolio (in papertrade) , well for the realtrady I would need to switch money around accounts - In Bitstamp there are supaccounts so this would not be a problem. However for know I am just thinking about papertrading.

In case I have 1000$ in and I got a spotted a nice trade, however the Stoploss is 100$ away that would mean I would have a risk of 100$ or 10% or my portfolio. However my rule of my strategy says i.e Never risk more than 3% per trade of the portfolio. hmmmm Up to know I have no option to take the trade without breaking my own rules which definatly point in the direction of a losing strategy.

In that example I only would like to send 333$ into the trade in order not to break my strat, risking more than 3% of my portfolio.

If we somehow would have a possibility to access the calculated Portfolio and simply cut off the differnce (666$), storing that in a variable we still could make the deal with gekko (100% of portfolio). After the deal would be closed I would like to ass again the cut off (666$) back.

So the main question with that thoughts I have: "How to read/write something to the papertrade portfolio"?

... This would be for me a acceptable way, not caring if the portfoliocalculation wouold be then messt up in the end or not, because I still could do my own one. I would be very happy about a positv reply

Regards

Ali1 commented 6 years ago

My idea would be this:

// the below settings are for partial balance trading. must use either proportion, or amount, or neither. Not both.
proportionFiatTraded: 0.5, // -1 means disable, must be between 0 and 1
amountFiatTraded: -1, // -1 means disable.

The above config attribute would only be relevant to make an initial calculation of available to buy. The above definitions would mean 50% of the initial fiat balance is up for for trade (in addition to all the asset). Then, a recalculation is made at every trade as to what's available to buy.

If I had 1btc and 100usd at the start.:

Trading starts. Balance = 1btc, 100usd, available to buy = 50usd
Trade advice to sell 1btc at 100usd/btc. Sold 1btc for 100usd. Balance = 0btc, 200usd, available to buy = 150usd.
Trade advice to buy 1.5btc at 100usd/btc. Bought 1.5btc for 150usd. Balance = 1.5btc, 50usd. available to buy = 0usd.
Trade advice to buy. Available balance too low. Balance = 1.5btc, 50usd. available to buy = 0usd.
Trade advice to sell 1.5btc at 200usd/btc. Sold 1.5btc for 300usd. Balance = 0btc, 350usd, available to buy = 300usd.
topdev01 commented 6 years ago

I think that just havin a percentage parameter in the live trader/paper settings would do the trick. Let's say you are trading eth against neo have a balance of 100 eth and set the percent parameter at 10%.

WHEN OPENING A LONG POSITION gekko will buy x amount of neo and spend 10 eth. When closing the position the whole amount of neo is sold back to keep it simple. THIS would allow safer trading by taking smaller risks. Allow for trades with other pairs like eth /btc and eth/xrp simultaneously using multiple gekko instances with the same account.

bennycode commented 6 years ago

@metachris How do you get the balance from within a strategy? I tried to query this.getBalance(this.currency) but this.getBalance is not an inherited function. 😢

Is it possible to require the portfolioManager in a strategy?

askmike commented 6 years ago

@topdev01 WHEN OPENING A LONG POSITION gekko will buy x amount of neo and spend 10 eth. When closing the position the whole amount of neo is sold back to keep it simple.

And what if people already had NEO? What if they had 1 ETH and 200 NEO? What if Gekko keeps losing money: after only having 90 ETH can it still lose another 10% (another 9 ETH)? Even talking about it like this is confusing. Let alone making it clear to users.

How do you get the balance from within a strategy? I tried to query this.getBalance(this.currency) but this.getBalance is not an inherited function

You can't this goes against the idea of Gekko, read more about what's in scope here: https://gekko.wizb.it/docs/introduction/scope.html#Strategies

bennycode commented 6 years ago

You can't this goes against the idea of Gekko

That's a pity. 😢 But is it possible to access the trading fee to take it into consideration? I saw that the fee is shown when running the trading bot in the CLI.

askmike commented 6 years ago

@bennyn I would love to add some more advanced modules around a Gekko setup so people can do cooler stuff. And I will work on this (given I'm changing Gekko from my hobby to my job).

Right now it's hard enough to even provide a simple framework (trade on one market, trade full balance, provide proper statistics, provide a proper UI). And the more advanced you go (partial orders, portfolio of different coins) the more complex everything becomes for the end user (especially those with no background in finance).


That's a pity. 😢 But is it possible to access the trading fee to take it into consideration?

Ah yes this is very important:

EDIT: you mean inside your strategy? Gekko tries to seperate the strategy from the execution, but you can do this yourself easily. Not that if you require your strat to only trade after making a profit you might wait for a long time, eg. if your strat bought at 15,000 (USD/BTC) it would still be waiting to sell until the price hits 15,000 + fee costs.

bennycode commented 6 years ago

I would love to add some more advanced modules around a Gekko setup so people can do cooler stuff.

I totally understand your situation. Building and maintaining such a project is a huge amount of work. Thanks for sharing it with us! 👍

Is there somewhere a feature list / wish list where we can add ourselves to vote for certain functionality? For example I would need to know the names of asset and currency. At the moment I am doing the following but I think it's not the most optimal way:

var log = require('../core/log');
var config = require('../config');

var method = {};

method.init = function () {
  log.info('Asset', config.watch.asset)
  log.info('Currency', config.watch.currency)
}
askmike commented 6 years ago

Right now that's the only way. But I am working on upgrading events, that way your strategy can subscribe to portfolio changes: https://github.com/askmike/gekko/blob/0c35666f19c848e35839d89892ad9f62061cdcb7/docs/internals/events.md

On 11 Feb 2018 18:41, "Benny Neugebauer" notifications@github.com wrote:

I would love to add some more advanced modules around a Gekko setup so people can do cooler stuff.

I totally understand your situation. Building and maintaining such a project is a huge amount of work. Thanks for sharing it with us! 👍

Is there somewhere a feature list / wish list where we can add ourselves to vote for certain functionality? For example I would need to know the names of asset and currency. At the moment I am doing the following but I think it's not the most optimal way:

var log = require('../core/log');var config = require('../config'); var method = {}; method.init = function () { log.info('Asset', config.watch.asset) log.info('Currency', config.watch.currency) }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/askmike/gekko/issues/937#issuecomment-364741542, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7MD_oxagLU9lR3uT4MrFkfzr8z4LvAks5tTsPrgaJpZM4Owzbs .

askmike commented 6 years ago

Though that will be quite hard to combine with the backtester (tons of possible race conditions), so no guarantee that those will be available in all gekko modes.

On 11 Feb 2018 18:50, "Mike van Rossum" mike@mikevanrossum.nl wrote:

Right now that's the only way. But I am working on upgrading events, that way your strategy can subscribe to portfolio changes: https://github.com/ askmike/gekko/blob/0c35666f19c848e35839d89892ad9f 62061cdcb7/docs/internals/events.md

On 11 Feb 2018 18:41, "Benny Neugebauer" notifications@github.com wrote:

I would love to add some more advanced modules around a Gekko setup so people can do cooler stuff.

I totally understand your situation. Building and maintaining such a project is a huge amount of work. Thanks for sharing it with us! 👍

Is there somewhere a feature list / wish list where we can add ourselves to vote for certain functionality? For example I would need to know the names of asset and currency. At the moment I am doing the following but I think it's not the most optimal way:

var log = require('../core/log');var config = require('../config'); var method = {}; method.init = function () { log.info('Asset', config.watch.asset) log.info('Currency', config.watch.currency) }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/askmike/gekko/issues/937#issuecomment-364741542, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7MD_oxagLU9lR3uT4MrFkfzr8z4LvAks5tTsPrgaJpZM4Owzbs .

hilkoc commented 6 years ago

I agree with much that is said above already. This is clearly one of the most wanted features for Gekko, besides a stop button to kill a live Gekko from the UI.

The purpose of this feature should NOT be to limit the trading balance available to Gekko. That is too prone for bugs, misunderstandings and user error. Furthermore, that doesn't belong in Gekko. A trading bot should take the best possible actions it can take with the balance that is available. If you want to limit the balance available, that has to be done manually in the way you fund your account on the exchange.

So what should this feature do? It should allow for more sophisticated strategies. For example after a steep price drop a strategy wants to use 70% of available currency to buy, keep another 30% to buy even lower in case the price drops even more.

Here is a suggestion for how to implement it:

Define trading balance = Asset * price + Currency, where 'price' is the amount of currency to buy one Asset.

Within your trading strategy, instead of writing this.advice(x); // x is either 'long' or 'short'. you'd write this.longAdvice(percentage); // percentage is a number between 0% and 100%.

After this function is executed you will be long 'percentage'.

In other words, Gekko will execute an order that makes your position such that percentage = Asset * price / (Asset * Price + Currency)

If the amount required to trade is below the minimum trading volume, this happens when the current long percentage is close to the requested one, Gekko will not execute any trade.

This is important to make the function idempotent. Also, in this way the strategy does not need to know what the trading balance is and it does not depend on events from the portfolio manager. That keeps things much simpler.

wchannaa commented 6 years ago

Hello. I know I'm coming late to this discussion,

But what this guy asked for is exactly what I want and need

WHEN OPENING A LONG POSITION gekko will buy x amount of neo and spend 10 eth. When closing the position the whole amount of neo is sold back to keep it simple. THIS would allow safer trading by taking smaller risks. Allow for trades with other pairs like eth /btc and eth/xrp simultaneously using multiple gekko instances with the same account.

Basically, I want to diversify my coin trading a bit. I want to utilize the same strategy (or maybe even different strategies) against several coins that I like. Neo, ICX, Trx, EOS, etc... so let's say I pick 5 different coins. some will have bigger gains than others, some will suffer losses.

I want to be able to say, use 5 ETH for this trade - or 20% of my ETH on that trade

AskMike's concern was stated as follows:

And what if people already had NEO? What if they had 1 ETH and 200 NEO? What if Gekko keeps losing money: after only having 90 ETH can it still lose another 10% (another 9 ETH)? Even talking about it like this is confusing. Let alone making it clear to users.

Which is a good point. It can get confusing. To keep it simple I say, if in TopDev's example we spent 10ETH to buy x Neo. Then when we go short, well sell the same number of x Neos. If people already had Neos this won't impact them. If we buy 10 Neos then we sell 10 Neos - and become rich. :)

Diversifying coins and possibly strategies can yield much better results on the long term. All we need for that is the ability to say buy using %ETH or #ETH and sell what we buy.

Profitability reports should still work. If I have 10ETH now and end up with 12ETH, then I made 20%. It doesn't matter how I got there, whether through a hundred trades of 5 different coins or a hundred trades of the same coin.

I don't expect backtesting to simulate multiple bots on multiple coins. Backtesting can continue to work as is, but instead of buying using 100% ETH everytime, it buys a specified amount and losses or gains accordingly. If I have 10ETH and purchased Neos using 1 ETH and made 20%, I end up with 10.2ETH which is only a 2% gain overall.

Looking forward to hearing your response. Thanks

xFFFFF commented 6 years ago

Check this fork: https://github.com/roelplieger/gekko and MultiTrader plugin

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you feel this is very a important issue please reach out the maintainer of this project directly via e-mail: gekko at mvr dot me.

scottie commented 5 years ago

Did this end up anywhere ?