msolters / gdax-multipong

Multi-bucket range trading for the GDAX exchange
12 stars 5 forks source link

Is Profit, Cash on Hand, Net Change Right? #2

Closed peterpme closed 6 years ago

peterpme commented 6 years ago

Hey,

Thanks for putting together this awesome tool! It's been a real treat!

I'm not sure if something is off with my database or if I'm losing money, so I'd just like to make sure:

When I pull down my FILLED orders as a CSV and add them up, I'm assuming I'm getting the total amount of money that was bought vs. sold and not the profiit right?

Sorry for these noob-ish questions but hopefully you can provide some insight!

I'd be happy to help adjust these values if they're right / wrong.

Perhaps I'm just reading them wrong?

Thanks again!

peterpme commented 6 years ago

Hey,

I've reset my log and db, not do any trading on the UI and let this console work and see what happens.

I'll confirm end of day!

Thanks!

peterpme commented 6 years ago

on initial load / clean slate:

all overview columns CORRECT

if i close and re-open, all values INCORRECT

If i close, edit my initial cash to the correct value, and open it up, the values are even more skewed

msolters commented 6 years ago

Hmm, I've definitely seen something like this maybe twice before. I haven't been able to reproduce it enough to really figure out where the problem is though.

Negative contributions to cash on hand etc. are always buys. So this is definitely a case of the app thinking there's more buys than there really were. I'll continue to look into this when I have time, but see if you can determine what specific conditions reproduce this behavior.Many, including myself, have restarted the app plenty of times without this error; so there are definitely unique conditions that cause this to occur. We need to pin down what those conditions are. Full buckets? Pending buys? Maybe it just happens in the edge case where the app is restarted but the database is being written to...?

msolters commented 6 years ago

To get an idea of how these values are computed, what's rendered in the UI itself is determined by the code here: https://github.com/msolters/gdax-multipong/blob/master/ui.js#L129

Most of those numbers are coming from a struct trades.figures, which is computed here: https://github.com/msolters/gdax-multipong/blob/master/trades.js#L16

"Profit" is defined as trades.figures.net_gain+trades.figures.unrealized_buys. This can be re-written

account.account.sells-account.account.buys-account.account.fees+trades.figures.unrealized_buys

I think we can eliminate account.account.fees, which seems to be accurate as you point out. Since this bug presents as something which is caused be restarting the app, it stands to reason we're looking for some quantity which is being written to disk that keeps getting updated.

So, really, we're looking at the following variables. Do any of these get written back to disk in a way that is inconsistent with the actual trades being placed, causing a feedback loop?

msolters commented 6 years ago

Aha! account.account.buys and account.account.sells is being updated in the local DB whenever trade_fill is called: https://github.com/msolters/gdax-multipong/blob/1b7f543c099bf25da4ad24647dca4c7b609b0626/trades.js#L104

~However, the GDAX websocket listener will call trade_fill for incoming messages without first checking that the message corresponds to a GDAX trade originating from the bot. I am confident doing a quick existence check on trades.trades array will fix this.~ On further digging, this can't be the issue. I'll keep looking!