PaulSquires / TradeTracker

Stocks, Futures and Options positions and market portfolio tracker (includes optional Interactive Brokers IBKR TWS API integration)
https://www.tradetracker.planetsquires.com
MIT License
54 stars 9 forks source link

Missing data for trade details #16

Closed Flippo24 closed 10 months ago

Flippo24 commented 10 months ago

When I am connected to the TWS, the individual prices of the single legs are missing as shown on your picture in the readme. Each line has its own P/L, which I would like to see. I am also interested in the current option price. Maybe you can display it in a column.

grafik

PaulSquires commented 10 months ago

There was a reason I removed it in this new version..... I just can't remember 100% why at the moment. I think it was related to the leg market price being correct but if the leg had been rolled previously then the leg cost basis was wrong. I will check into this again and put it back if it works without causing confusion for the user.

Flippo24 commented 10 months ago

At this point you can view the calculation of the positions again. The amount on a leg is already the amount of the entire position. The individual position value is incorrect here. grafik

PaulSquires commented 10 months ago

I am taking another look at this issue and will add back the leg cost, market value, difference, and percentage. Much like the older IB-Tracker functionality. Once I implement the changes then we should discuss it again to be sure that it meets our needs. I am hoping to have this change done today.

PaulSquires commented 10 months ago

Just finished adding the new functionality. Source code updated in repository. Looks pretty good. Here is a screenshot:

image

Flippo24 commented 10 months ago

It looks good visually, but please check the calculation again. How can the values in both rows be the same in this case? Please describe how the values are calculated. I assume that the red values on the right should signal a loss. Actually, it is still in profit.

grafik

PaulSquires commented 10 months ago

image

For the option legs:

PaulSquires commented 10 months ago

Actually, it is still in profit.

Looks like the current market value of your Put legs would cost you $2851.02 each to close (total $5702.04). Your current income received on the trade is $3721.30, so it appears you are not currently profitable for this trade(?)

Flippo24 commented 10 months ago

Last close is 7.00$ 4 100 7 = 2800. so I need 2800 to close the whole position. Not just one leg.

PaulSquires commented 10 months ago

Ah, okay, I see what the problem is now. Both option legs are exactly the same.

image

The data retrieved from TWS is getting the full 4 positions whereas in TradeTracker you have them split into two different transactions and when I loop the grid, I (incorrectly) display the TWS 4 contract value for both of your legs.

I need to fix it so that duplicate option legs are combined into one.

Flippo24 commented 10 months ago

Please use the values entered in the transaction as the starting point. Please do not divide the value of the TWS by the number of legs.

PaulSquires commented 10 months ago

I will combine both of the legs so that it shows in the grid as -4 Feb 16 42d 65 P. That way it will match what shows in TWS and also match the value that TWS sends to TradeTracker.

Flippo24 commented 10 months ago

No, please don't do that. Because you can look at both positions separately. Each has its own PnL. You also do this separately in your strategy.

Flippo24 commented 10 months ago

Take the value from the transaction for the first column. In my opinion, it is not necessary to take the value from the TWS. If you were to combine it, then the same would appear in the line as in the line with the underlying. It would make even less sense to see the same thing twice.

PaulSquires commented 10 months ago

Maybe I am not understanding correctly. Maybe more talk about this may help.

The first column is calculated from incoming TWS data: position_cost = (pd.average_cost * ld->leg->open_quantity); // pd.average comes from TWS and is cost per 1 PUT

Instead of using that data, I could use the two values that show in the Trade History for your legs (the values that you had manually entered: 1920.70 and 1800.60). I like that idea.

It is the second column that I was mostly referring to previously. That amount is not calculated by me, and is simply the amount that comes in from TWS. The problem is that the amount is the total of all 4 of your Puts and not just 2. To be correct, I would have to take the 2851.02, divide it by 2, and display 1425.51 for each leg in column 2.

Does that sound right?

Flippo24 commented 10 months ago

For column one: The value of the transaction history is completely ok. The background is this: Sell 1 put for 10$ premium if you want to buy it back for 50% it would be 5$. If you sell the same put again during the term, say for 6$ and want to buy it back at 50%, that would be 3$. So both separately. If you combine them now, 50% would be $4.

For the second column: Just calculate it yourself. I prefer to use the TWS model price in my spreadsheet. You certainly won't get it. You could calculate the last close * position. You also have the multiplier in the transaction. If you want to make it even more precise, then take the ask price for puts/calls sold and the bid price for puts/calls bought. This would have the advantage that a price is available even if there is little trading activity.

PaulSquires commented 10 months ago

Added new code to calculate the cost basis of individual Option legs. Needs to be tested on new data because the calculation can not be applied to transactions created before this patch. I will see how well this code works this week in a live setting when connected to TWS. Actually, older existing Trades may display correct data UNLESS they contain rolled transactions. In that case, the calculation will be incorrect.

Flippo24 commented 10 months ago

The value for column 1 is now correct. Great so far. But I don't understand why you always calculate the other columns from the total position and not row by row? The value for column 2 is again the value for the entire position. Why is it not the current price * the number in this row times the multiplier? The value in column 3 also seems to come from the total position. Please switch to line-by-line calculation here as well. The profit in column 4 is also clearly wrong. It is somewhere between 21-26%. It depends on which price you take from the TWS for the calculation.

grafik

PaulSquires commented 10 months ago

Yes, I see the problem and I have a solution for it. I just started working on the fix but I only have it about 50% finished. I will finish it today and post again here when the code is done. Thanks!

PaulSquires commented 10 months ago

I uploaded new code for the live data grid. I changed where some column data is displayed as well as use better data for the individual line calculations. I have not tested it a 100% yet but hopefully I will have time today to test.

Flippo24 commented 10 months ago

That's what I wanted to see. Thanks a lot.

However, I still have one problem. But this has nothing to do with your current change. Can you have a look at why no values are displayed for these items?

grafik

Flippo24 commented 10 months ago

The width of the fourth column could be slightly larger. Maybe an automatic adjustment of the width across all values of the column makes sense, in combination with a minimum width. What about a space between the value and the %? Does that make sense? Could be my eyes are not the best anymore. Do the brackets make sense at all or can you leave them out?

grafik

Flippo24 commented 10 months ago

If the brackets only show that the position is in loss, then I would prefer a "-" in front of the value. I think the red color is right.

PaulSquires commented 10 months ago

The width of the fourth column could be slightly larger. Maybe an automatic adjustment of the width across all values of the column makes sense, in combination with a minimum width. What about a space between the value and the %? Does that make sense? Could be my eyes are not the best anymore. Do the brackets make sense at all or can you leave them out?

grafik

Thanks, I will look at this tomorrow. There is code that should automatically resize the columns contents based on min/max widths. I will check to ensure that code is being applied to that fourth column. I should be able to change from "( )" to negative signs.

I haven't had much time to day to test or code anything so hopefully tonight and tomorrow I can get things correct.

Flippo24 commented 10 months ago

I don't expect you to solve things for me immediately. I'm already quite happy with your work.

Speaking of work. I have something else for you... When I pull data from Yahoo, it only shows the price of the underlying and that the position is ITM. But all other values are missing. No values for the legs and the total PnL. grafik

PaulSquires commented 10 months ago

Here is what it looks like with the minus sign and space before the %.

image

Here is what it looks like with ZERO decimal places instead of TWO.

image

Do you have a preference??? (I just committed the source using MINUS sign, SPACE before %, and ZERO decimal places if you want to see it for yourself.

I have a few other areas in the program that use parenthesis instead of minus. I should just change everything to minus, right?

Flippo24 commented 10 months ago

My favorite would be the second one, without the decimal points and with the space. It looks cleaner.

Your TradeTracker is getting better every day!

PaulSquires commented 10 months ago

My favorite would be the second one, without the decimal points and with the space. It looks cleaner.

I think that I like that version also. 😀

Your TradeTracker is getting better every day!

Thanks! I am very happy with all of the suggestions you've provided to make this program better. It has come a long way in just a week or so. 👍

Flippo24 commented 10 months ago

You should have asked for my help earlier. :laughing:

Flippo24 commented 10 months ago

Not so quick to close. Please note these two comments from me, which have not yet been processed.

https://github.com/PaulSquires/TradeTracker/issues/16#issuecomment-1881649268 https://github.com/PaulSquires/TradeTracker/issues/16#issuecomment-1881831017

PaulSquires commented 10 months ago

I swear I responded to the Yahoo data question! I must have typed it and not hit submit. For Yahoo data, I am only scrapping the price data because I expected that the Options data would be very hard to scrap. I think that I will convert this into a Discussion for a future enhancement.

PaulSquires commented 10 months ago

I admit, I skipped or forgot about the other issue! Let me take a look at that one.

PaulSquires commented 10 months ago

As an aside, I only added the Yahoo data because when checking my positions after market close I didn't want to have to start TWS and log into it just to quickly check my positions.

Flippo24 commented 10 months ago

But exactly this possibility gives people without Interactive Brokers the opportunity to use your program.

PaulSquires commented 10 months ago

I would also have to set up a thread in the program to fetch/scrap the data every 15 minutes because the Yahoo data is delayed.

PaulSquires commented 10 months ago

That's what I wanted to see. Thanks a lot.

However, I still have one problem. But this has nothing to do with your current change. Can you have a look at why no values are displayed for these items?

grafik

I have uploaded new source code that enables you passing the "-console" (with the double quotes) command line switch to the TradeTracker.exe program and it will display incoming TWS ticker data. Try it and see if you are receiving any data for those tickers in your screenshot.

Flippo24 commented 10 months ago

What happens under the hood? Do you retrieve the portfolio from the TWS and then look for all the tickers in the portfolio? Or do you look for the ticker entered in the trade in your app?

PaulSquires commented 10 months ago

There is really two processes that happen. The first is subscribing and receiving Ticker data (basically the real time price action); and second, is retrieving Portfolio data for each open position. I create a vector (resizable array) of all Trades that are open. I then loop that vector and send calls to TWS to initiate sending me the data.

There are callbacks in the TradeTracker code that receive the data from TWS. When the Ticker and Portfolio data is received I store (or update existing) that data in an unordered_map (basically an associative array). I have a background thread running every second that loops the active trades grid and updates any information that has changed based on the map data. This is when you will see the ticker prices change or option legs data update.

Flippo24 commented 10 months ago

I create a vector (resizable array) of all Trades that are open.

Are you doing this with the open trades from TWS or the open trades from TradeTracker?

Flippo24 commented 10 months ago

Ok my background on the trade and why I want to know: There was a stock split at TMF. The current option is now continued under the ticker TMF1. I also find the same option in the TMF options chain, with the adjusted strike price. So TMF Jan19 6 put became TMF Jan19 60 put. The original option is now called TMF1 Jan19 6 Put. Which I still have in my portfolio. I have entered the new name in the TradeTracker because otherwise the price of the underlying would not fit.

PaulSquires commented 10 months ago

Are you doing this with the open trades from TWS or the open trades from TradeTracker?

Open trades within TradeTracker. If I ever need to determine that trades exist that I have forgotten to enter, or have entered incorrectly, I run the Reconcile (the circle Checkmark botton next to the Connect button). That compares all of TradeTracker's open Trades to all open trades on IBKR's servers.

Flippo24 commented 10 months ago

Ok since it's the ticker from TradeTracker, I have to pass the ball to you. Please recreate the trade as in my picture and check why no data is being fetched for it. TMF1 is displayed in the console, although I have not entered it anywhere in TradeTracker.

Flippo24 commented 10 months ago

I am sure that this excerpt from Reconcile will help you to understand what I mean.

grafik

PaulSquires commented 10 months ago

That does help actually. What was your original Trade BEFORE the split and before you made any additional entries in TradeTracker. If I know what your original starting point was then maybe I can offer suggestion on how the subsequent split should have been entered.

It was probably this, right?

TMF -20 PUT 2024-01-19 @ $6 -40 PUT 2024-02-16 @ $6

PaulSquires commented 10 months ago

When I look at "TMF1" on TWS it displays "Basket". I'll have research what that means and how it affects the data.

image

Flippo24 commented 10 months ago

These were the trades before the split. -20 PUT 2024-01-19 @ $6 -40 PUT 2024-02-16 @ $6 After the split, another one was added, which is why I added them all to the post-split options. -2 PUT 2024-02-16 @ $60

I'm not interested in the split at this point, but rather in why the one I entered doesn't work.

Flippo24 commented 10 months ago

As you can see, they are available in the right ticker. grafik

PaulSquires commented 10 months ago

image

Those -2 PUT entries do not have matching expiry dates so TradeTracker will not match the incoming TWS data to your local data for that Trade.

PaulSquires commented 10 months ago

I have to go out for an hour! Damn. I will dig deeper into your posts as soon as I get back. Hang in there, we'll figure it all out.

Flippo24 commented 10 months ago

Lol. You seem to be there with heart and soul.

PaulSquires commented 10 months ago

I don't know why IBKR is not correcting the TMF1 -20 PUT 2024-01-19 $6 ---> TMF -2 PUT 2024-01-29 $60 when it sends you the data. Likewise the -40 TMF1 and -2 TMF 2024-02-16 $60 are not being combined. Granted, they are separate symbols in IBKR's eyes (TMF1 and TMF).

I wish I could explain why IBKR handles the data the way that they do for this scenario.

I wonder if you enter the Trades as follows would they then receive the TWS data:

TMF1 -40 PUT 2024-02-16 @ $6 TMF1 -20 PUT 2024-01-19 @ $6

TMF -2 PUT 2024-02-16 @ $60

I have done some online searches to see if this kind of situation has been explained but I have not found anything relevant.