How to fill OHLC when provider quotes are delayed
Author: akuzn
Creation Date: 8/4/2012 2:24 AM
profile picture

akuzn

#1
Some times real quotes in my real time provider are delayed.
It may be due to not trading activity in instrument.
But at the same time i can get best offer and best bid prices from calling some methods developed by my data provider.
The problem is best offer and best bid are not DataSeries - they are integer variables avalaible only on new bar.
So each new Execute method run i can get these prices: best_bid, best_offer. Certainly there are some additional variables - volume, next bids and offers etc, but it is not important.

Could you help me with code how to store these prices in DataSeries and modify Bars.Close Series.
Certainly i can use even last prices to make decision. But if i want to use any indicator based on Series or on Bars i need to modify OHLC.
Execution of strategy on each new bar (1 minute)is garanteed by using strategy monitor or other frequently traded stock in pair strategy.

These prices are coming as follows:
CODE:
Please log in to see this code.
profile picture

Cone

#2
I don't think we can help with a delay with your data provider or adapter.

QUOTE:
Certainly i can use even last prices to make decision.
Wealth-Lab processes the Execute() method of a script when a new bar is added to the chart. Consequently, you can use 'Last Price' only when using a 'Tick' chart, which I would not recommend attempting when trading highly liquid instruments.

QUOTE:
Could you help me with code how to store these prices in DataSeries
You mean best_bid and _offer?

QUOTE:
and modify Bars.Close Series.
In what way do you want to modify Bars.Close? Your data adapter has complete control over what you put in that series. If you want, you could put the data for _bid or _offer there.

profile picture

Eugene

#3
Just to add to Robert's reply:
QUOTE:
Could you help me with code how to store these prices in DataSeries

Have you considered storing these prices in NamedSeries?
profile picture

akuzn

#4
This is very thin question of real time fully automated trading.
For example If you are testing strategies on historical data or using WLB for end of day such kind of synchronization or verification
is not needed.

I use pair strategies and instruments i use are liquid - there is always good spread - thanks to market makers.
But if there is no trades during timeframe ( i use 1 minute ) OHLC provided by adapter may not be updated by prices of transactions. If strategy is executed by strategy monitor every minute or by updated new bar in chart i may be in position when 1st symbol in updated but without updated bar in other symchronized symbols ( they certainly will be synchronized but in reality OHLC series wont be actual).
Certainly stop loss and target prices are staying in exchange system.

Now im working on reducing price and other risks of open positions and my provider is giving me the possibility to use best offer and bid (real quotes are different from OHLC).
Unfortunately they are coming in one dimension variables renewed on every execution of strategy.
And im not so friendly with C# and all possibilities of wealthlab.
But i m sure that it is possible to store these prices in some kind of Series - some kind of adding new value on every new execution of strategy.
I suppose if i try to do as follows:
CODE:
Please log in to see this code.

only last prices will be be stored and previuos will be deleted.
Certainly if they were stored it wont be a problem to update Close and other Series or use them directly in indicators.

My question is - how to put it in something like "static" or "global" arrayes or series that wont be deleted on every execution of strategy.
profile picture

akuzn

#5
Seems the answer must be bery simple. But not yet sure am i right.
If i put declaration of DataSeries not in execute method but earlier - after declaratioin of Strategy class, and they will be filled in Execute method after each new execution of strategy - they must be stored and kept, right?
//
// May be i write little bit more code than needed
// Just want u understand me better
//
public class PairsTrading : WealthScript
// Declaration
private DataSeries Cup_Bid;
private DataSeries Cup_Offer;

public PairsTrading ()
{
new System.Threading.Thread(StartRegister).Start();
}
private void StartRegister()
{
RegisterGlass ( stock_class, symbol1 );
}
//

protected override void Execute()
{
............
Cup_Bid[Bars.Count-1] = (double) Glass[symbol1][-1].Price;
Cup_Offer[Bars.Count-1] = (double) Glass[symbol1][1].Price;

............
}
//
//------
I hope suppose that strategy will execute in order:
1. Run and compilation, objects, series and variables declared in PairsTrading will be made and will have new added values each new bar in execute method.
All values will be kept till new strategy rerun/recompilation.
profile picture

Eugene

#6
QUOTE:
If i put declaration of DataSeries not in execute method but earlier - after declaratioin of Strategy class, and they will be filled in Execute method after each new execution of strategy - they must be stored and kept, right?

I was about to suggest a static variable. Your line of thinking is correct. In fact, defining a variable like that makes it class-level.

P.S. Here's a distantly related example from our knowledge base: WealthScript Techniques | Don't execute Strategy on open
profile picture

akuzn

#7
Thank You very much for my idea support.
If i use static variable - will it be kept even after recompilation?
What does it mean for me - if i recompile/ rerun strategy i loose all added data and have to wait some bars to fill these dataseries again. Seems language logic doesnt allow that, but who knows?)
To be honest i dont want to make now DataFiles ... But if there is only this way one more week and this construction will be done.

Another question - how can i get time of strategy compilation/run. Must be method wich i have to put in strategy constructor. Is GetDateTime convenient here? So after this strategy will know how many time/bars are stored in my Cup_Offer, Cup_Bid series. i think Bars.Count cant give right answer in such situation.

profile picture

akuzn

#8
To finish with this question
developed code:
//
CODE:
Please log in to see this code.

//
But when i try to declare an object
CODE:
Please log in to see this code.

in Strategy Class definitions section i receive compilation error
in current context this is unavailable.

The main idea of this class - is to store discussed values and to have the number of bars passed after creating this object.
profile picture

Eugene

#9
Use CODE tags, please.

QUOTE:
If i use static variable - will it be kept even after recompilation?

Should be kept after you press "Go" and re-initiated next time you recompile (e.g. by clicking "Execute").
QUOTE:
So after this strategy will know how many time/bars are stored in my Cup_Offer, Cup_Bid series.

All DataSeries contain the same number of values as bars in the chart.
QUOTE:
But when i try to declare an object

Passing a WealthScript instance to an object can't be done here. It won't work. (You could only use lazy initialization from inside void Execute() but ending up with assigning a new instance every time the procedure runs is the opposite of what you're after). Rewrite your Cup class to avoid having this WealthScript dependence in it.
profile picture

akuzn

#10
Yes number of bars is the same and depends on preferences set up - DataRange;
Usually i use 2 days data range in trading.
But what will give me time of compilation ( time of object creating )- if i compare time on Bars.Count-1 to time when object was created i ll have the difference in minutes. If if divide it by timeframe i ll have the number of DataSeries values filled by actual best bid and best offer. It will give me possibility to know -may i use indicators based on cup series ( for example more than 25-40 bars ) or not.

Could u help me please - how to get compilation time? if there is such possiblity? Only way i know - to get it from Bars object or to take it from Utility object wich needs bar as parameter.)
Seems i cant get time this way.


Or as an alternative way.
When strategy will start to fill series on every new coming bar, what will contain not filled Series - 0?
if yes it will be possible to find number of filled bars in simple cycle.
profile picture

Cone

#11
Whether it works or not, your code doesn't assign a reference to the class variable ws, so it will certainly not work when you call Bars_Held.

Tip: Use an underscore as the first character for private class variables so that you don't get confused. Example:

CODE:
Please log in to see this code.
profile picture

akuzn

#12
yes i missed it.
Seems i realised class and methods for multisymbol filling and managing values.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).