- ago
I'm trying to figure out how I can get a TimeSpan (.NET datatype) out of BarHistory.Scale without writing some kind of complicated code or lookup table.

BarHistory.Scale.Interval sort of fits the bill when the BarHistory scale is in minutes. But if the scale is in Days or Weeks, the Interval call doesn't work. A BarHistory.Scale.ToTimeSpan method would be perfect. Has anyone written an extension method?

What I'm really trying to do is compute a SamplingRate parameter I can pass into ScottPlot to get an x-axis time scale. https://scottplot.net/cookbook/4.1/category/axis-and-ticks/#plotting-datetime-data-on-a-signal-plot

Oops, looks like the above ScottPlot example requires ScottPlot 4.1.70 and WL8 B70 is running ScottPlot 4.1.68 instead. I'm not having a good day.
0
204
Solved
2 Replies

Reply

Bookmark

Sort
- ago
#1
@superticker - the following may be good enough for what you need. It doesn't support Monthly, Quarterly, Yearly because they're not fixed time spans. I didn't know what to do with Tick, Volume or WeeklyStartDay, but your post suggested to me that you didn't need them. You could expand this to fit additional requirements...

CODE:
using System; using WealthLab.Core; namespace WLUtility.Extensions { public static class HistoryScaleExtensions { public static TimeSpan ToTimeSpan(this HistoryScale historyScale) { switch (historyScale.Frequency) { case Frequency.Daily: case Frequency.NDays: return TimeSpan.FromDays(historyScale.Interval); case Frequency.Weekly: return TimeSpan.FromDays(historyScale.Interval * 7); case Frequency.Second: return TimeSpan.FromSeconds(historyScale.Interval); case Frequency.Minute: return TimeSpan.FromMinutes(historyScale.Interval); case Frequency.Hour: return TimeSpan.FromHours(historyScale.Interval); default: throw new ArgumentOutOfRangeException(nameof(historyScale), $"Frequency {historyScale.Frequency} is not supported."); } } } }
0
- ago
#2
Yes, that might work as an extension method for HistoryScales. In the HistoryScales QuickRef docs, it says "Possible values are Daily, Weekly, Monthly, Quarterly, Yearly, Tick, Second, and Minute."

NDays is missing, so that case probably should be omitted. Likewise for Hours. "Tick" is an unknown TimeSpan, so that one can't be included either.

CODE:
using System; using WealthLab.Core; namespace WLUtility.Extensions { public static class HistoryScaleExtensions { public static TimeSpan ToTimeSpan(this HistoryScale historyScale) { switch (historyScale.Frequency) { case Frequency.Daily: return TimeSpan.FromDays(1); case Frequency.Minute: return TimeSpan.FromMinutes(historyScale.Interval); case Frequency.Weekly: return TimeSpan.FromDays(7); case Frequency.Second: return TimeSpan.FromSeconds(historyScale.Interval); //valid? possible? default: throw new ArgumentOutOfRangeException(nameof(historyScale), $"Frequency {historyScale.Frequency} is not supported."); } } } }
I rearranged them with the most common ones first.

So to use in the ScottPlot link at the top, something like this may work. Someone can post any corrections. These equity curve ScottPlot plots need to be done in the Cleanup{block} after all trades are complete.
CODE:
   TimeSpan timeSpan = HistoryScale.ToTimeSpan(bars.Scale); //time between data bars    double sampleRate = (double)TimeSpan.TicksPerDay / timeSpan.Ticks; //convert ticks into bars per day    var equityPlot = plt.AddSignal(Backtester.EquityCurve.Values.ToArray(), sampleRate, color: Color.DarkOrange);    equityPlot.OffsetX = Backtester.EquityCurve.DateTimes[0].ToOADate(); //set start date    plt.XAxis.DateTimeFormat(true); //display tick labels using a time format
0
Best Answer

Reply

Bookmark

Sort