I wanted to calculate the 52-week high, but the result of this code appears to be off. Is there something I'm doing incorrect??
Backtesting with SPY (using B49) I get the following results:
According to this the latest high is 476.86 when it should be around 479.98.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript2 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { TimeSeries dsHigh = Highest.Series(TimeSeriesCompressor.ToWeekly(bars.High), 52); dsHigh = TimeSeriesSynchronizer.Synchronize(dsHigh, bars); PlotTimeSeriesLine(dsHigh, dsHigh.Description, "Price", Color.Blue, 1, LineStyles.Solid); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { //code your buy conditions here } else { //code your sell conditions here } } //declare private variables below } }
Backtesting with SPY (using B49) I get the following results:
According to this the latest high is 476.86 when it should be around 479.98.
Rename
You can accomplish this with ScaledInd:
CODE:
TimeSeries dsHigh = ScaledInd.Series(bars,new Highest(bars.High,52),HistoryScale.Weekly);
Thanks that worked.
Are there issues with the code I wrote or is there a problem with the TimeSeriesCompressor / TimeSeriesSynchronizer?
Are there issues with the code I wrote or is there a problem with the TimeSeriesCompressor / TimeSeriesSynchronizer?
Taking your code for example, the correct way would be:
CODE:
TimeSeries dsHigh = TimeSeriesCompressor.ToWeekly(Highest.Series(bars.High, 52));
Maybe Eugene can debate me, but I don't think there should be a difference between the two methods. The ToWeekly method that you used looks to be a bug because it's only catching the higher highs if they occur at the end of the week. It's scaling the High series in the same way that it would the "Close".
Just looking at Eugene's code (Post #3) wouldn't that take the last 52 days high and compress it into weekly? If so, it would be more appropriate to take the last 200 days high. I want to compress the daily into weekly and then calculate the 52-weekly high.
Cone, I was trying to see the pattern as I could see my code came out with new highs, but they weren't quite right. If it was taking the last day of the week high, that would explain the issue.
Cone, I was trying to see the pattern as I could see my code came out with new highs, but they weren't quite right. If it was taking the last day of the week high, that would explain the issue.
After reading the "ToWeekly" documentation myself, Eugene's right. The TimeSeriesCompressor synchronizes with the final bar of the period, i.e., week in this case. Instead you could also use the BarHistoryCompressor like this -
CODE:
TimeSeries dsHigh = Highest.Series(BarHistoryCompressor.ToWeekly(bars).High, 52); dsHigh = TimeSeriesSynchronizer.Synchronize(dsHigh, bars); PlotTimeSeriesLine(dsHigh, dsHigh.Description, "Price", Color.Blue, 1, LineStyles.Solid);
Thanks! Now I understand the apis better.
Your Response
Post
Edit Post
Login is required