How to trigger a function when a new bar comes out?
Author: cheers32
Creation Date: 4/4/2017 8:47 PM
profile picture

cheers32

#1
Hi,

I would like to have my scanning function called when a new bar comes out, particularly during intraday. E.g. in a 5-min interval scenario, when a new bar shows every 5 minutes, I intend to have my function being called, so that a scanning of a set of symbols can happen. I think the same is possible for daily chart too (at market close, do a scan, and send me an email).

I searched in the forum but I didn't find an answer. Please help if anyone knows the solution.

Thank you.
profile picture

Eugene

#2
Hi,

So what you've described is how processing Strategies works in Wealth-Lab:

FAQ > Is it necessary to have access to intra-bar tick data to daytrade with Wealth-Lab?

It's not clear what problem you're having with this. Could you explain with more detail?
profile picture

cheers32

#3
Hi Eugene,

I didn't find related answer to my question from the FAQ. I believe my question is a common one though.

Basically, I created a strategy to determine market condition at any given bar. I want to run my strategy about 30 mins before market closes on a daily chart, on a list of symbols, one by one. My strategy prints info to the log. Then I will use the info printed in the log to determine which one to trade.

Today when I tried this, I found that before the market closes, the price of the last bar on a daily chart was yesterday's price, even when I logged in to stream the data as current. So this is issue #1.

Secondly, I wanted to try this on 5-min intraday chart, so it means I want to run my strategy every 5 minutes when a new bar comes out, but I don't know how to do this.

Thank you for the help.
Wei
profile picture

Eugene

#4
Hi Wei,

Now that's a completely different question of its own. Either you simply run the strategy every 5 minutes or anything else. And that "anything else" could be, for example, covered by workaround in post #4 in this thread:

Eliminate overnight trades

Namely, the WealthScript Programming Guide > Programming Trading Strategies > Alerts > How to: Alert for AtClose Signals.

QUOTE:
Today when I tried this, I found that before the market closes, the price of the last bar on a daily chart was yesterday's price, even when I logged in to stream the data as current. So this is issue #1.

Which is by design. For instance, Intraday / Multi-Time Frame | Compressed price series alignment.

QUOTE:
Secondly, I wanted to try this on 5-min intraday chart, so it means I want to run my strategy every 5 minutes when a new bar comes out,

Run your strategy on 5-minute data. Again, please refer to the same Wiki FAQ for the mechanics.
profile picture

Cone

#5
QUOTE:
So this is issue #1.
The ability to update a daily bar during the market session depends on the Data Provider. You can create daily bars by compressing intraday data as Eugene suggested, or, find a EOD provider that will update your symbols in real time during the data. You can configure the Yahoo! provider to do that, for example, but data are subject to delay. Depending on the market you're covering, I'd recommend IQFeed.

QUOTE:
I want to run my strategy every 5 minutes when a new bar comes out
Simple. Use either the Strategy Monitor (for many symbols) or Streaming charts (up to a dozen symbols or more).
profile picture

cheers32

#6
Thank you Eugene and Cone. I will try this tomorrow.
profile picture

cheers32

#7
Hi Eugene and Cone, I tried using your compress price from a lower scale approach, but I did not get the expected result. My most simple objective is to just view the daily chart, while displaying the current day's price change in percentage on the label, during the market open time, but no current day's latest price is available from the latest daily bar.

I was trying to think alternatives. For example, if I can dynamically change the scale to 15 mins, and then save the latest price to a file, and then change the scale back to daily, that should solve the problem. But I didn't find a way to set the scale from daily to 15 mins dynamically in the code.

I feel I need additional help on this issue. Thank you.
profile picture

Eugene

#8
Hi,

Scaling works from the lower to the higher scale, not the other way round. But if you only want to peek at the current day's data after the open, there's a smart workaround that may help if you keep a separate 15-min DataSet up to date by updating its data:

Accessing Intraday data from Daily

But if your strategy is streaming then things get more complicated. Here's some ideas:

* GetExternalSymbol and 1 minute streaming data, see post #3
* Choosing optimal trading tool: Strategy Monitor or Strategy window, see the Workarounds section
profile picture

cheers32

#9
Hi Eugene,

The external symbol approach worked! I will test it using live market data on Monday. Thanks very much for your advice.

Wei
profile picture

cheers32

#10
Hi Eugene,

During the test today, I found that the intraday bars retrieved in this method is somewhat cached. It means it always gets the same number of intraday bars as the first time it was called, even if additional bars should have been generated.

CODE:
Please log in to see this code.


I tried to update the DataSet using Data Manager, but it is still the same.

I tried restart WealthLab, and tried re-create the intraday data set, and both worked, new prices are shown.

Therefore, I wonder if there is a way to auto-refresh the data set, or clear the cached values.

Thanks,
Wei
profile picture

Eugene

#11
Hi Wei,

Like Cone said in a thread I referred to above, "Usually what you get from a secondary symbol in a streaming window is one bar old since the request is static." Please see if some of the workarounds in the forum topics/Wiki apply to your case.