Skip to content

Can't access variable with .updateEvent #180

@ironhak

Description

@ironhak

Hi everyone, this is a snippet from my code:

async def monitor_entry_signal(ib: IB, symbol: str, df: pd.DataFrame):
    
    ticker = ib.reqRealTimeBars(
        contract = ContFuture(symbol= symbol,exchange= "COMEX"),
        barSize = 5,
        whatToShow = 'TRADES',
        useRTH = False
    )
    
    def on_bar(bars: list[RealTimeBar], hasNewBar: bool):
        bar_number = len(bars)
        last_bar = bars[-1]
        print(f'\n--- {symbol} 5-sec bars (count={bar_number}) ---')
        print(f'Bar {bar_number} {last_bar.time.strftime('%H:%M:%S')}: O={last_bar.open_:.2f}, '
              f'H={last_bar.high:.2f}, L={last_bar.low:.2f}, C={last_bar.close:.2f}')
        print(len(df))
        
    ticker.updateEvent += on_bar

    await asyncio.Event().wait()

async def main(symbols):
    ib = IB()
    await ib.connectAsync("127.0.0.1", 7497, clientId=1)
    # Make sure we always disconnect
    try:
        fetch_data_coroutines = [
            fetch_data(ib, symbol) for symbol in symbols
        ]
       
        results = await asyncio.gather(*fetch_data_coroutines)

        monitors = []
        for result in results:
            symbol, df = result
            monitors.append(monitor_entry_signal(ib, symbol, df))

        # Tell python to run a list of commands asynchronously 
        await asyncio.gather(*monitors)

    except Exception as e:
        print(f"Error: {e}")

    ib.disconnect()

tickers = ['GC']
asyncio.run(main(tickers))

When I run this I'll get a successful data subscription and every 5 seconds a new string will be printed with informations... But the print(len(df)) (and whathever thing I do with ðf inside on_bar will just get skipped. Even if I do print(type(df)) it'll just skip it without printing anything. Even doing things like if len(df) > 10: print(True) else: print(False), it'll skip it.

Any hint on how to go about this?
Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions