askmike / gekko

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

Improved PnL reporting #2028

Closed hilkoc closed 6 years ago

hilkoc commented 6 years ago

Improved PnL reporting (Feature Request)

The PnL reporting in Gekko is based on 'round trips'. This is nice because it keeps things simple, but is also limiting Gekko's capabilities. As a prerequisite for enabling strategies in Gekko to trade parts of the balance, a more flexible PnL reporting is needed.

Here is a proposal for how to implement this. It is really quite simple and can be done in 10 min in a spread sheet.


PnL is reported per currency pair. For a currency pair we refer to the left hand side as base or asset and to the right hand side as term or currency. To report PnL correctly, we set up a table with the history of all trades, and next to each trade the current position and PnL.

The first two are trade columns. They record what was traded. The middle columns show the current position, which is derived from the two trade columns. The last three columns indicate the PnL. The PnL is calculated based on the market price, i.e. last traded price, which is stored in the first column.

An example for BTCUSD would look like this:

Price Signed Volume Position Average Open Invested Current Value Cash PnL PnL (term) PnL %
9,700 1.00 1.00 9700 9,700 9,700 0.00 0.00 0.0%
10,000 2.00 3.00 9900 29,700 30,000 0.00 300 1.0%
10,400 -1.00 2.00 9900 19,800 20,800 500 1,000 5.1%
10,500 -2.00 0.00 9900 0 0 1,200 0.00 6.1%

The meaning of each column is as follows

The final cumulative PnL would be the sum of all the Cash PnL plus the amount of PnL (term) in the last row (i.e. the Cash PnL for closing out the position).

The above definitions assume long positions only. If one also considers the possibility of naked short positions, i.e. where total Position is negative, things become slightly more complicated. We will not go into details here.

Show a thumbs up if you'd want this feature. If there's enough enthusiasm for this I might have a go at implementing this myself.

askmike commented 6 years ago

This is what I am currently thinking:

You are 100% right that the current PnL representation (as well as order execution of swapping 100% of the balance back and forth) is very limited. However it also keeps things simple to understand for everyone. See for example the difference between an exchange like Poloniex and an exchange like Bitmex. You definitely have a ton more control and statistics in the latter, but this is at the cost of requiring a deeper understanding of finance (math).

I am liking your proposal however, I don't want to make the current one more complex for everyone. What about some kind of an advanced mode? I think the majority of Gekko users are people getting into automated trading, it's really meant as a starterskit and I don't want to raise the entry bar even higher.


Can we move this discussion to the forum?

hilkoc commented 6 years ago

For those who don't find their way to the forum, let me just give a warning here.

1. Swinging your full balance at every trade is very risky.

2. To run a successful trading strategy (automated or not), it is crucial to understand your position and what drives your PnL. If you don't understand this or have no interest in it, be advised to stay far away from the markets. Then your place is at the roulette table.

Having something that is simple, can lead to the misunderstanding that it is also safer. Do you feel safe in a car that can only make 90 degree turns? Having only a switch to make sharp left/right turns is surely simpler than something as advanced as a steering wheel.

Just like you need a drivers licence before you can drive on the roads, you need to have some basic understanding of what you're doing before you trade in the markets. When it comes to the markets however, that is purely for you own safety. Letting newcomers jump in with very risky strategies is not doing them a favor.

May I ask what exactly is the difficulty here? There is no advanced maths, only sums and multiplications.

Having said that, Gekko is great and I'm impressed with the things it can do already. Just trying to help make it even better.

hilkoc commented 6 years ago

I've made a start with implementing this. Simply because I need this for myself. For now it is a standalone application, but feel free to have a look here.

I've added a compatible license, so I hope one day this functionality can find its way into Gekko.

askmike commented 6 years ago

[want to post this on the forum but can't find your thread]

Great stuff, and I agree with most things.

  1. To run a successful trading strategy (automated or not), it is crucial to understand your position and what drives your PnL. If you don't understand this or have no interest in it, be advised to stay far away from the markets. Then your place is at the roulette table.

100% agreed, this is super dangerous stuff. However to put this into perspective, you are already exposed to a ton of risk:

The biggest thing though:

To start: selling is never dangerous in Gekko's model (build on the assumption that the asset is safe and secure, eg. USD in BTC/USD market), so you are only really talking about the buying part. I don't agree that this strategy is necessarily not safe, it depends on how you (your strategy) came to the conclusion to buy. You can easily build in a complete risk model that only ever buys when it has sufficient reason to believe the price will not go down before the next sell. All the strategies I run are build this way.

May I ask what exactly is the difficulty here? There is no advanced maths, only sums and multiplications.

This is 100% an interface problem, the biggest part of this is to build a visual interface that clearly shows how a position evolved over time and how it performed (without lumping everything together in a single uPnL number). After that a more complex interface is needed between the strategy and the code within gekko that manages and creates orders (portfolio manager).

If you can show me any well used trading reporting tool (crypto, stocks, etc) that does not have at least a 100 people complaining about how it displays some information not as ideal as they would have liked we can start looking into this.

hilkoc commented 6 years ago

These are very good (bullet) points, you make here. Everyone even thinking about getting involved in crypto trading should be very much aware of them.

When it comes to Gekko, only one of these points is in scope: Gekko is responsible for managing your market risk. Buying and holding can be just as risky as actively trading an asset. Anyone who's been hodling crypto the last month will know that.

The biggest thing though:

To start: selling is never dangerous in Gekko's model (build on the assumption that the asset is safe and secure, eg. USD in BTC/USD market), so you are only really talking about the buying part. I don't agree that this strategy is necessarily not safe, it depends on how you (your strategy) came to the conclusion to buy. You can easily build in a complete risk model that only ever buys when it has sufficient reason to believe the price will not go down before the next sell. All the strategies I run are build this way.

When trading, selling is just as risky as buying. The situation is symmetrical, selling one currency is the same as buying the other. The risks are that the price goes up further after you've sold, or goes down even lower after you've bought. When your strategy has sufficient reason to believe that it is a good time to buy (or sell), it can still be wrong. Trading a partial balance helps mitigating this risk. It allows you to 'walk into' your position. When you've bought some and the price unexpectedly drops, you can buy some more. This brings the average price at which you bought, closer to the bottom price. In exactly the same way, you can better maximize your average sell price. This is what trading partial balance is for.

This is 100% an interface problem

I have nothing fancy in mind for the interface. Simply a table for each traded currency pair as above in the OP. And maybe one overview table that shows all traded currency pairs and a summary of the result.

If you can show me any well used trading reporting tool (crypto, stocks, etc) that does not have at least a 100 people complaining about how it displays some information not as ideal as they would have liked we can start looking into this.

One tool that has received a lot of praise for the clear interface is eToro. You can open a free account for paper trading to have a look. They keep things very simple by using some restrictions:

I've created a topic on the forum here: https://forum.gekko.wizb.it/thread-56679.html

askmike commented 6 years ago

I replied on the forum, let's keep the discussion there :)