Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Improved PnL reporting #2028

Closed
hilkoc opened this issue Mar 13, 2018 · 6 comments
Closed

Improved PnL reporting #2028

hilkoc opened this issue Mar 13, 2018 · 6 comments

Comments

@hilkoc
Copy link
Contributor

hilkoc commented Mar 13, 2018

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

  • Price : The price at which was traded, in units of term currency.

  • Signed Volume: The amount in base that was traded. Positive for buy, negative for sell. In units of base currency.

  • Position : The total amount of base currency in the portfolio. In units of base currency.
    = Total Sum of all signed volume up to and including this trade = Previous position + Signed Volume.

  • Average Open : The volume weighted average price of all buy trades at which the Position was bought. In units of term currency.
    For two trades with volumes v1, v2 at prices p1, p2 this is
    Average Open = (v1 * p1 + v2 * p2) / (v1 + v2). Important is that all volumes have the same sign, e.g. v1 > 0 and v2 > 0. The sum must include all previous buy trades. To incrementally calculate this one simply takes v1 = the previous position and p1 the previous 'Average Open' and v2 = volume of current trade and p2 the current price.
    If the traded volume is negative, i.e. it is a sell trade. The average open is not updated, it stays equal to its previous value.

  • Invested : = Position * Average Open. In units of term currency.

  • Current Value: = Position * Price (term). In units of term currency.

  • Cash PnL : In units of term currency. This only occurs for Sell trades. For buy trades this is by definition set equal to zero. For a sell trade it is calculated as
    Abs(Signed Volume) * ( Price (term) - Average Open).
    Conceptually this is the amount of profit taken (or loss taken) by closing out a part of the position.

  • PnL (term): = Current Value - Invested. In units of term currency.

  • PnL % : = Price / Average Open - 1.

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
Copy link
Owner

askmike commented Mar 15, 2018

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
Copy link
Contributor Author

hilkoc commented Mar 16, 2018

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
Copy link
Contributor Author

hilkoc commented Apr 5, 2018

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
Copy link
Owner

askmike commented Apr 6, 2018

[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:

  • Crypto is bleeding edge tech, based (in some cases) on bleeding edge cryptography. The fact of owning a bitcoin is extremely risky business from a technical perspective.
  • Wallet implementations can (and will) have bugs, these bugs might cause loss of funds. I am not just talking about the bitcoin you store on your phone but any bitcoin stored anywhere (from a trezor to wallet software used by exchanges).
  • All exchanges are not insured, if they get hacked (and they will) people will end up losing money.
  • The crypto prices are volatile, I don't think it's fair to say that trading with swapping full balance (like gekko does right now) is dangerous but owning bitcoin and expecting the $ value to not drop by 50% is not. If you bought in at the peak on december you would be down an awful lot, can Gekko (or any system that swaps balance) lose as much? Sure I guess..

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
Copy link
Contributor Author

hilkoc commented Apr 8, 2018

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:

  • Every trade is managed and reported as a leveraged position. Even though the leverage is 1x. This means trades need to be opened and closed, forcing round trips. You can however, open long and short positions at the same time, thus enabling you to buy or sell any amount at any time.
  • Everything is traded against USD. Cross asset trades are impossible.

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

@askmike
Copy link
Owner

askmike commented Apr 8, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants