marketcalls / openalgo

Open Source Algo Trading Platform for Everyone
https://docs.openalgo.in
GNU Affero General Public License v3.0
150 stars 68 forks source link

Mismatch of holding stock from Kotak Securities #68

Closed surisetty closed 2 weeks ago

surisetty commented 3 weeks ago

Hi After setting up open algo on my Linux Desktop, for broker Kotak securities. I see a mismatch of stock names on openalog web interface.

Out of 4 holding on I see 2 stocks appearing with different names. screen shots attached.

image

image

marketcalls commented 3 weeks ago

image

This is my existing test holdings. So far there is no issues

image

I also tried adding some more Test Holdings to check the issue

image

Possibly by next week we should be able to figure it out why it is happening.

surisetty commented 3 weeks ago

Ok thanks, Do let me know if logs are required from my setup

surisetty commented 3 weeks ago

Today, I deleted db file and did fresh setup again. First run I could see correct data, but when relogged in I'm observing issue again. Little bit a debugging I could see mapping issue with openalgo symbol mapping.

def transform_holdings_data(holdings_data):
    transformed_data = []
    for holding in holdings_data:
        transformed_position = {
            "symbol": holding.get('symbol', ''),
            "exchange": holding.get('exchangeSegment', ''),
            "quantity": holding.get('quantity', 0),
            "product": holding.get('instrumentType', ''),
            "pnl": round((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0))),2),
            "pnlpercent": round((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0)))/float(holding.get('holdingCost', 0.0))*100,2)
        }
        transformed_data.append(transformed_position)
        print(transformed_position)
    return transformed_data

{'data': [{'displaySymbol': 'INDUSINDBK', 'averagePrice': 1206.0072, 'quantity': 3, 'exchangeSegment': 'nse_cm', 'exchangeIdentifier': '5258', 'holdingCost': 3618.0215, 'mktValue': 3169.2, 'scripId': '34d945dd3e571353d704d106c8589d9bba62677e', 'instrumentToken': 1372, 'instrumentType': 'Equity', 'isAlternateScrip': False, 'closingPrice': 1056.4, 'symbol': 'INDUSINDBK', 'sellableQuantity': 3}, {'displaySymbol': 'SBIN', 'averagePrice': 796.2765, 'quantity': 1, 'exchangeSegment': 'nse_cm', 'exchangeIdentifier': '3045', 'holdingCost': 796.2765, 'mktValue': 822.45, 'scripId': '58577151cf11c30372ddffc722b9cfa7f110528f', 'instrumentToken': 1900, 'instrumentType': 'Equity', 'isAlternateScrip': False, 'closingPrice': 822.45, 'symbol': 'SBIN', 'sellableQuantity': 1}, {'displaySymbol': 'ITC', 'averagePrice': 483.3443, 'quantity': 1, 'exchangeSegment': 'nse_cm', 'exchangeIdentifier': '1660', 'holdingCost': 483.3443, 'mktValue': 491.55, 'scripId': 'afb99bb20514693721700d23629bbb829d2d0219', 'instrumentToken': 1407, 'instrumentType': 'Equity', 'isAlternateScrip': False, 'closingPrice': 491.55, 'symbol': 'ITC', 'sellableQuantity': 1}, {'displaySymbol': 'TCS', 'averagePrice': 4117.2431, 'quantity': 11, 'exchangeSegment': 'nse_cm', 'exchangeIdentifier': '11536', 'holdingCost': 45289.6736, 'mktValue': 44931.15, 'scripId': 'c0694bd35d4fed6dc20fccc455f54754817c019b', 'instrumentToken': 4106, 'instrumentType': 'Equity', 'isAlternateScrip': False, 'closingPrice': 4084.65, 'symbol': 'TCS', 'sellableQuantity': 11}, {'displaySymbol': 'BEML', 'averagePrice': 4098.2037, 'quantity': 1, 'exchangeSegment': 'nse_cm', 'exchangeIdentifier': '395', 'holdingCost': 4098.2037, 'mktValue': 3979.5, 'scripId': '8bd0f877ea91f30622962dd0fa1892916bfb9bba', 'instrumentToken': 445, 'instrumentType': 'Equity', 'isAlternateScrip': False, 'closingPrice': 3979.5, 'symbol': 'BEML', 'sellableQuantity': 1}]}

{'symbol': 'HINDCOMPOS', 'exchange': 'NSE', 'quantity': 3, 'product': 'CNC', 'pnl': -448.82, 'pnlpercent': -12.41} {'symbol': 'SBIN', 'exchange': 'NSE', 'quantity': 1, 'product': 'CNC', 'pnl': 26.17, 'pnlpercent': 3.29} {'symbol': 'ITC', 'exchange': 'NSE', 'quantity': 1, 'product': 'CNC', 'pnl': 8.21, 'pnlpercent': 1.7} {'symbol': 'TCS', 'exchange': 'NSE', 'quantity': 11, 'product': 'CNC', 'pnl': -358.52, 'pnlpercent': -0.79} {'symbol': '5PAISA', 'exchange': 'NSE', 'quantity': 1, 'product': 'CNC', 'pnl': -118.7, 'pnlpercent': -2.9}

This might help you Thanks

surisetty commented 3 weeks ago

Here is the fix given by chatgpt

def transform_holdings_data(holdings_data): transformed_data = [] for holding in holdings_data:

Use 'displaySymbol' if available, otherwise fall back to 'symbol'

    **stock_symbol = holding.get('displaySymbol', holding.get('symbol', ''))**

    # Transform the stock data
    transformed_position = {
        "symbol": stock_symbol,
        "exchange": holding.get('exchangeSegment', ''),
        "quantity": holding.get('quantity', 0),
        "product": holding.get('instrumentType', ''),
        "pnl": round((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0))), 2),
        "pnlpercent": round(((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0)))
                             / float(holding.get('holdingCost', 1.0))) * 100, 2)
    }
    transformed_data.append(transformed_position)
    print(transformed_position)
return transformed_data

It is working, but another issue PLN % is slightly mismatching with actual data from kotak mobile app

image

marketcalls commented 3 weeks ago

Thanks that helps. And let me also confirm from my end as well.

surisetty commented 3 weeks ago

Fix I gave is workaround. I don't know why this ticket is closed. There is no stock with name HINDCOMPOS , you need to fix this mapping issue.

marketcalls commented 3 weeks ago

I havent closed the ticket. May be accidentally closed it

marketcalls commented 2 weeks ago

Hi,

I updated a minor bug fix release. the kotak issue with holdings (symbol mismatch) and PNL is now corrected. can you please check it out and confirm from your side.

surisetty commented 2 weeks ago

Hi,

Thanks for the bug fix, Symbol mismatch issue resolved now, PLN still not matching.

image

image

surisetty commented 2 weeks ago

image

marketcalls commented 2 weeks ago

Hi the PNL and PNL percentage is properly coded under broker/kotak/mapping/order_data.py

"pnl": round((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0))),2), "pnlpercent": round((float(holding.get('mktValue', 0.0)) - float(holding.get('holdingCost', 0.0)))/float(holding.get('holdingCost', 0.0))*100,2)

        The problem is that mktValue always represents yesterday's closing value and it looks like it is not updating live.
        The alternative is calculating LTP values from the quotes and then calculating them. Which is already part of the upcoming roadmap. Until then this issue remains
Screenshot 2024-11-06 at 12 55 14 PM
surisetty commented 2 weeks ago

OK thank you for the support, closing this ticket then.