question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass in Attributes to the onBarUpdate() Callback Function Possible?

See original GitHub issue

Is it possible to pass in features/variables to the onBarUpdate callback? I’m wanting to pass in the contract object to it so I can parse the contract’s symbol from it. What I have so far looks like this:

contracts = [Forex(pair) for pair in ('EURUSD', 'USDJPY', 'GBPUSD', 'USDCHF', 'USDCAD')]
ib.qualifyContracts(*contracts)

def onBarUpdate(bars, hasNewBar):
            
        df = util.df(bars)
        df.to_csv("datasets/" + todays_date + "/" + TICKER/CONTRACT_HERE + ".csv", index=True)


for contract in contracts:
    bars = ib.reqHistoricalData(contract, endDateTime='', durationStr='3 D', barSizeSetting='5 mins', whatToShow='MIDPOINT', useRTH=False, keepUpToDate=True)
    bars.updateEvent += onBarUpdate

…and what I envision I’m after would look something like:

def onBarUpdate(bars, hasNewBar, contract):
....
bars.updateEvent += onBarUpdate(contract)

…however I’m having issues troubleshooting this to get it working. As I’m sure can be guessed, I’d like to replace the “TICKER/CONTRACT_HERE” with something like “contract.symbol” so I could save the .csv file with the ticker for the corresponding bars contract in the filename. I’m just getting used to async callback functions as well so not sure the correct syntax. Any help would be greatly appreciated! Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
erdewitcommented, Apr 6, 2021

It’s easier to use bars.contract

1reaction
rundefcommented, Apr 5, 2021

You can use partial from functools

from functools import partial

bars.updateEvent += partial(onBarUpdate, contract)

def onBarUpdate(contract, bars, hasNewBar):
....
Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing attributes to callback functions - php - Stack Overflow
you can try this way, call_user_func_array() class foo { function bar($arg, $arg2) { echo __METHOD__, " got $arg and $arg2\n"; } ...
Read more >
OnBarUpdate() - NinjaScript - NinjaTrader 8
A number representing the current bar in a Bars object that the OnBarUpdate() method in an indicator or strategy is currently processing. IsDataSeriesRequired....
Read more >
insync@groups.io | Topics
Error 'list' object has no attribute 'conId' although there is a conId 4 messages ... Hi, I have a few question about callbacks...
Read more >
Add attributes of Script used with API Genesys Cloud to create ...
My question is regarding the Participant Data and how to put some variables ... If you want to pass a set of variables/data...
Read more >
Viewing online file analysis results for 'NinjaTrader.Core.dll'
This report is generated from a file or URL submitted to this webservice on September ... Found a potential E-Mail address in binary/memory....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found