Support margin trading (on exchanges that support that)
From my experience as a human trader using margin, there are higher risk management requirements with regards to 1) orders, 2) strategy and 3) money.
1. Order Execution
Order Types (Limit, Market, Stop, OCO, hidden, trailing)
Due to the risk of trading on margin, it's critical to use all available order types to most efficiently enter and exit positions. This includes 'walking' into positions, rather than triggering an 'all in' order. It would be critical to refactor Gekko's portfolioManger.js trading functionality for margin trading to make entries and exits cleaner.
2) Strategy
It would be nice to separate Gekko's strategic analysis into several different dimensions. I'm thinking, Recursion to Mean, Momentum and Machine Learning. This would be a very ambitious undertaking. At present, Gekko does Recursion alright with TA, but we could make use of ws orderbook data to get a better picture of momentum and volume, and possibly use sentiment from an rss aggregation of news events. And, we could possibly add some ML models that take the recursion and momentum inputs.
3) Risk
As margin trading is significantly more risky, it would be very helpful to integrate a risk profiling strategy. I'm thinking a Kelly Criterion or even a simple loss prevention mechanism that activates during unfavorable market conditions.
Please let me know your thoughts. This would be an ambitious undertaking but I look forward to helping out with my limited, but steadily improving coding skills.
I guess there is enough for me to worry about with exchange api wrappers and maybe updating the docs. :)
That sounds very good! I do think that the current setup is too simple to build any serious prediction modelling on top of (with proper risk management). I'd like to see this improved.
To go in on some points:
Order Types (Limit, Market, Stop, OCO, hidden, trailing)
Not all exchanges supported by Gekko support all order types, we got to figure out how to handle those scenarios? We could use fallbacks, to fallback to an order type supported by the exchange.
This includes 'walking' into positions, rather than triggering an 'all in' order.
An advice within Gekko is triggered from a trading method and is a simple object:
{
recommandation: newPosition,
portfolio: 1
}
recommandation is the position (long or short)
portfolio is how much of the portfolio should be invested.
This sounds like a start for what we need, however the portfolioManager ignores this property for now (we can change this object to however we want).
- Strategy
It would be nice to separate Gekko's strategic analysis into several different dimensions. I'm thinking, Recursion to Mean, Momentum and Machine Learning.
Sounds great!
but we could make use of ws orderbook data to get a better picture of momentum and volume, and possibly use sentiment from an rss aggregation of news events. And, we could possibly add some ML models that take the recursion and momentum inputs.
This is something that we need to think out, if you look at How Gekko currently works:

you will find that Gekko currently only relies on candle data (aggregated market information about 1 minute of market activity).
While it would be super easy to load a bitstamp websocket order book listener in a trading method, this will mean that you can never backtest (against historical data). In order to overcome this we need to extend a candle from:
{
start (date)
open
high
low
close
vwp
trades
}
To something like:
{
start (date)
open
high
low
close
vwp
trades
order_book
}
Where the order_book is an object that is has information about the current orderbook state. The hard thing about this is that it is relatively easy to get historical candle data (sometimes even directly from exchanges, see importing), it is very hard to get the historical state of the orderbook.
and possibly use sentiment from an rss aggregation of news events
This would also have to be added to the output of a market (maybe in a candle, since this is an aggregation of 1 minute of a "market", eg. if a news event was published at a minute it has to be part of a specific candle).
it would be very helpful to integrate a risk profiling strategy.
Good idea, besides volatile or uncertain times we could also implement something for #334. But do you just want to "stop" placing orders or do you want to get out of the "asset"? If you are trading USD/BTC and shit hits the fan, you probably want to kill your USD position asap? But if you are trading BTC/ETH we cannot assume that BTC is always the "safe" option versus the "asset" that is ETH.
This would be an ambitious undertaking
I think next steps would be:
- Prioritize/design features
- Split them up in different parts
- Work on each part separately
I guess there is enough for me to worry about with exchange api wrappers and maybe updating the docs. :)
I am already really glad for your input (and PRs)! Current todos can be found here, those are small things that are relatively easy to pick up. But everything in this discussion is of course also very interesting and would be awesome to put in Gekko.
Before I can be of much help regarding these big plans I've suggested, it would be more practical to work on todos. I don't have much coding experience, so it would be good for me to accelerate that learning process by doing many small things and at the same time working around Gekko and learning how everything works.
Perhaps I can return to this discussion after working away on some smaller stuff. :)
may i suggest as a start to just implement a variable in config so we can decide how much gekko will trade in each buy or sell order? it could be useful to test real trader and to let user limit how much of his/her portfolio gekko should trade with. looking at the code i think it could be easily accomplished in two ways:
- patching exchanges.js adding a "maximumOrder" variable to each value/asset pair and patching portfolioManager.js to use this value
- adding a variable in config.js in which we define the amount of our portfolio we want gekko to use and and patching portfolioManager.js to use this value
a combination of these two solutions could be used to have a "trading portfolio" and set max orders too. If you think this can be useful i could try coding such patches.
This would be great. Would you like to code these changes for Bitfinex (to start) and maybe the exchange you are using and open a PR against the new-bitfinex branch? I would test them out and if everything looks good, we could ask Mike if he wants to role them out across the other exchanges. :)
it could be useful to test real trader and to let user limit how much of his/her portfolio gekko should trade with.
This sounds like something Gekko needs (and "adding a variable in config.js" is the way to go here, since config.js hosts all configurables for the end user)!
This would be great. Would you like to code these changes for Bitfinex (to start)
I am pretty sure this can be handled purely in the Portfolio Manager (that just needs to buy/sell less).
However the reason why this should be handled with extreme caution:
Imagine we add a maximumOrder and someone configures that to $50 while having $5000 dollar on his account. If his strategy doesn't perform well Gekko might lose $50 on every trade. After enough trades this can drain the full account anyway. We could store something like start amount in memory in the PortfolioManager, but everyone I know that uses Gekko in production uses something (forever, pm2, etc) that automatically restarts Gekko on crash (happens rarely now).
We could write this to disk (using the sqlite adapter). But honestly: A single bug in this code can result in massive losses of funds Gekko shouldn't have had access to..
I still feel safer just moving the amount of capital I'm willing to risk into the account that I'm going to be running Gekko on. However, I think this is going in a very important direction with regards to margin trading. How much to trade should be a percentage of total available capital + margin.
Not all signals are equal.
Some have more or less probability. Moreover, if Gekko's method is performing poorly, it would be good to limit the amount of risk dynamically. So, to move in the direction of risk analysis, we could start with putting a maximumOrder. However, I think we should move this functionality into a new component, #3 as considered above. In this way, we could design and build a more comprehensive risk analysis method that determines how much as a percentage to trade, based on other parameters. I'm suggesting the Kelly Criterion as a starting point:

What are you thoughts? Although it would be helpful for testing, a maximumOrder does not help with risk exposure while trading on margin.
Imagine we add a maximumOrder and someone configures that to $50 while having $5000 dollar on his account. If his strategy doesn't perform well Gekko might lose $50 on every trade. After enough trades this can drain the full account anyway
I agree with you but as a start i would like to have it as an option. if something goes wrong it would take time to drain account if maxOrder is not a big value , instead of a single "all in" like it is now.
Maybe i should watch into paper trader code and redefine this problem, since what we want to do here it seems quite similar: define a "virtual" portfolio that we can use instead of values returned by exchange portfolio. we could allow user to set its contents as currencies and assets via config.js and then let trader run as it is now or using maximumOrder. in this way the max amount risked in trading will be those user defined ( and updated with subsequent trades ).
I agree with you but as a start i would like to have it as an option. if something goes wrong it would take time to drain account if maxOrder is not a big value , instead of a single "all in" like it is now.
Yes, let's implement this! Alternatively/also we could implement a SL and TP strategy (see this discussion).
Love both ideas, and hopefully I can contribute something to that in the future.
This is a really old issue. Gekko has a UI now and it is shifting in a different direction: a lot of statistics and risk metrics are being calculated. Supporting margin trading would require completely different calculations, additionally risk becomes a lot more complex to manage. Because of the size feature I don't see it happening anyime soon (all though I love to see it. if anyone wants to work on this feel free).