Hello,
Wondering if I could get some help on an issue I've ran into.
Say you have 200 bars of data, in that 200 bars I want to take a subset (say distance from low to high) and check where price was for each close in relation to a moving average (price is 10% above the EMA on that day, etc)
How can I go about looping through each bar & EMA? I'm assuming I need a RollingWindow but I am yet to use one anywhere so I'm a bit fuzzy. I'm currently selecting my Universe, then hoping to filter it down even further in OnSecuritiesChanged before sending a trade signal.
Here is what I've got so far:
public partial class SymbolData
{
public readonly ExponentialMovingAverage EMA200;
public readonly Maximum PeriodHigh;
public readonly Minimum PeriodLow;
public bool IsReady() { return
EMA200.IsReady
&& PeriodHigh.IsReady
&& PeriodLow.IsReady; }
public SymbolData(IEnumerable<TradeBar> history)
{
EMA200 = new ExponentialMovingAverage(200);
PeriodHigh = new Maximum(200);
PeriodLow = new Minimum(200);
foreach (var bar in history)
{
Update(bar.EndTime, bar.Close);
}
}
public bool Update(DateTime time, decimal value)
{
EMA200.Update(time, value);
PeriodHigh.Update(time, value);
PeriodLow.Update(time, value);
return IsReady();
}
}
public override void OnSecuritiesChanged(SecurityChanges changes)
{
//foreach (var security in changes.RemovedSecurities)
//{
// if (security.Invested)
// {
// Liquidate(security.Symbol);
// }
//}
foreach (var added in changes.AddedSecurities)
{
var history = History(added.Symbol, TradingMonthDays * 8, Resolution.Dailyy);
SymbolData symbolData = new SymbolData(history);
if (symbolData.IsReady())
{
//Loop through the last N period (PeriodLow - PeriodHigh)
and calculate where Daily close was in relation to EMA200, how?
}
}
}
Thank you
Fred Painchaud
Hi Borishu,
I am not sure I understand your requirement. You want to calculate where the daily close was in relation to EMA200. What do you mean exactly? You want the absolute difference between the close and EMA200, like abs(EMA200 - close)? Also, what is the relation between “EMA200 and the daily close” and “PeriodLow and PeriodHigh”?
I will be able to give you code when I understand what you want to do.
Fred
Borishu
Hi Fred,
Appreciate the reply
I'm trying to determine where the close of the day was in relation to the EMA, how do I loop through all all the days of a symbol and find what the EMA was that day?
Fred Painchaud
Ok I see.
There is one thing you need to keep in mind before I proceed with code. The EMA200 takes 200 bars to warmup/be ready. After those 200 bars, you get your first EMA200. So if you want to iterate over the last EMA200, you will need to wait another 200 bars, so you can have 200 EMA200 values.
Is that clear and understood, and ok for you? It means you need 400 days of data before you can start your first iteration on the last 200 EMA200…
If it is ok, here's the added RollingWindow in your code:
Since your code is not complete, I did not try to run my modified code - it won't compile as-is. You will need to add RollingWindow in your “using” statements.
Fred
Borishu
Hi Fred,
I think this got my closer but not quite there
Do I need to create a RollingWindow with a custom class as the type? Something like:
Also, does EMA200.IsReady only fire when the entire EMA is filled? If so I feel like I would need to do some manual matching of bar + EMA and I would like to avoid that. Can I calculate the EMA for a bar somehow?
I think the issue is I want to compare a particular day's close with that same day's MovingAverage and I'm not sure how to link those two data points together so I can loop and compare
Fred Painchaud
Hi Borishu,
Your so-called requirement is getting more precise. 😊 That is what I meant by “what do you want to do when you say compare - compare what, when and how”. Yes, from what I get right now, you need a custom class to hold your values and THAT will need to be put in the rolling window…
I believe the best would be to wrap all needed values into one class so you would then have all values for one single day at one place.
But honestly, coding without complete requirements is an issue and it's been like that since programming is a thing. So you have, for each day:
You already told me that you want to see where the close is to the EMA200. That's classic. Would you please tell me what you want to do with Max and Min. I'm guessing that you want to see where close is wrt them. In other words, you probably want an oscillator between max and min of the current close and a signal for the crosses of close with EMA200. Right?
Would you please give me the maths you want to do with those 4 values? I could then know what you want and code it to you so you understand it and learn. Without requirements, one is just coding in the dark, only betting if it fills the intention or not… 😊
Cheers,
Fred
Borishu
Haha, yes, you are correct Fred, I should have detailed things clearer in the beginning. A lesson in being too verbose.
I'm hoping to do some high tight flag analysis with my algorithm and one of the conditions I have is to check how well the “price” is surfing along the moving average, I'll attach a sample image of what I mean:
So I figure if I have both the OHLC of the day and where the MA was on that day, I can determine if the price has been moving in this controlled manner that I usually spot with my eyes when looking at a chart.
As for the maximum and minimum, I'm hoping to use those to determine which ‘window’ I want to pay attention to for the surfing action, ideally I'll be able to check the low and high from my desired period and determine how well the price was ‘surfing' between the two candles.
Fred Painchaud
“How well the price is surfing along the moving average”. 😊 This makes me think I need to go back to the ocean sooner than later…
While the physics of surfing is a nice math problem, I believe what you want to do here as a foundation operation is simply “close - EMA”. The higher this distance is, the better for you. If it is negative, it means the close was below EMA…
Now, “how well the price is”. That sounds like a mean of distances to me. So, that would be : (sum for i = 1 to 200 of close(i) - EMA(i) ) / 200. That will give you an oscillator around 0 for the mean distance between the price action and its EMA over the last 200 days. The higher the better.
So if I follow you properly, you would not need to associate TradeBars with EMA for that. You would need close. With your current code, I would not add another class for that. I would add a RollingWindow of close to SymbolData and the associated updating code. You would then have two rolling windows to work with, one for EMA and one for close, onto which you could iterate to perform your calculations. You could also go a bit further and encapsulate everything in an indicator that simply gives you the current value of “how well the price has been surfing along the moving average". If your indicator answers “Gabriel Medina”, then you know it's been going well. 😊
If you really want to compare OHLC with EMA, then yes, something like a TradeBar would be ok. You would even have volume then.
Fred
P.S. You most likely need to use Add() instead of Update() on RollingWindow. My bad.
Borishu
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!