- ago
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??
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.
0
924
Solved
7 Replies

Reply

Bookmark

Sort
- ago
#1
You can accomplish this with ScaledInd:

CODE:
TimeSeries dsHigh = ScaledInd.Series(bars,new Highest(bars.High,52),HistoryScale.Weekly);
0
Best Answer
- ago
#2
Thanks that worked.

Are there issues with the code I wrote or is there a problem with the TimeSeriesCompressor / TimeSeriesSynchronizer?
0
- ago
#3
Taking your code for example, the correct way would be:

CODE:
TimeSeries dsHigh = TimeSeriesCompressor.ToWeekly(Highest.Series(bars.High, 52));
0
Cone8
 ( 6.71% )
- ago
#4
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".
0
- ago
#5
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.
0
Cone8
 ( 6.71% )
- ago
#6
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);


1
- ago
#7
Thanks! Now I understand the apis better.
0

Reply

Bookmark

Sort