Parent: Object
PriceGrid maps a section of price or TimeSeries data into a two-dimensional grid that can be used for pattern comparison.
Each grid cell contains either '.', representing an empty cell, or 'X', representing price data. PriceGrid instances can be compared to determine how closely one price pattern resembles another.
public string Persist()
Returns a compact string representation of the PriceGrid.
The representation contains the Width, Height, first cell character, and run-length encoded counts for alternating `'.'` and `'X'` cells.
You can pass the returned string to **Parse** to recreate the grid.
Creates a PriceGrid with the specified width and height.
The first constructor creates an empty grid and fills every cell with '.'.
The second constructor creates the grid and fills it using the High and Low values of bars between startIndex and endIndex.
The third constructor creates the grid using values from the specified TimeSeries between startIndex and endIndex.
Compares this PriceGrid with pg and returns a score from 0 to 100 indicating how closely the grids match. The two PriceGrid instances must have identical Width and Height values. Otherwise, Compare throws an ArgumentException. The calc overload supports these PriceGridCalculation values:
- HitsAndMisses - Compares every cell. A match occurs when both grids contain the same value, whether
'X'or'.'. This can produce relatively high scores for sparse grids because matching empty cells count toward the result. - HitsOnly - Considers only cells containing
'X'in this PriceGrid. A hit occurs when the corresponding cell in pg also contains'X'. The score is the percentage of populated cells in this PriceGrid that match. If this PriceGrid contains no populated cells, the result is zero. - Proximity - Compares the vertical location of populated cells within each column. The score decreases as corresponding price data becomes farther apart vertically. This calculation can be useful for sparse grids created from a TimeSeries.
The overload containing legacyCompareLogic uses HitsAndMisses when legacyCompareLogic is true and HitsOnly when false. Since false is the default, calling
Compare(pg)uses HitsOnly.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript { public class PriceGridExample : UserStrategyBase { private PriceGrid _pattern; private TimeSeries _mapValues; public override void Initialize(BarHistory bars) { ROC roc = ROC.Series(bars.Close, 10); PlotIndicator(roc); for (int n = 40; n < bars.Count; n++) { if (roc[n] > 10.0) { int gridEnd = n - 20; _pattern = new PriceGrid( 20, 10, bars, gridEnd - 19, gridEnd); SetTextDrawingFont( new WLFont("Courier New", 12)); DrawHeaderText(_pattern.Pictogram); _mapValues = new TimeSeries(bars.DateTimes); PlotTimeSeries( _mapValues, "Map Values", "MV", WLColor.Purple); StartIndex = n; break; } } } public override void Execute(BarHistory bars, int idx) { if (_mapValues == null || _pattern == null) return; PriceGrid current = new PriceGrid( 20, 10, bars, idx - 19, idx); _mapValues[idx] = _pattern.Compare( current, PriceGridCalculation.HitsOnly); if (HasOpenPosition(bars, PositionType.Long)) { if (idx - LastPosition.EntryBar >= 9) ClosePosition( LastPosition, OrderType.Market); } else if (_mapValues[idx] >= 80.0) { PlaceTrade( bars, TransactionType.Buy, OrderType.Market); } } } }
Copies cells from pg into this PriceGrid without resampling.
This PriceGrid is first filled with '.', after which cells from pg are copied wherever their coordinates fit within this grid.
If this PriceGrid is larger than pg, the additional cells remain blank. If it is smaller, cells outside its Width or Height are discarded.
Fills the PriceGrid.
The first overload fills every cell with the specified character c.
The BarHistory overload maps the High and Low values between startIndex and endIndex into the grid.
The two-TimeSeries overload uses tsLow and tsHigh to define the lower and upper price range at each index. The TimeSeries must have identical Counts or an ArgumentException is thrown.
The final overload uses a single TimeSeries for both the low and high values, producing a grid based on individual TimeSeries values.
Before price data is mapped, these overloads clear the grid by filling it with '.'.
Attempts to populate empty interior columns in the PriceGrid.
When an empty column occurs between populated columns, the method attempts to connect price data from the preceding and following columns by adding 'X' cells to the empty column.
The first and last columns are not modified by this process.
Flips the PriceGrid along its horizontal axis. The top row becomes the bottom row, the second row becomes the second-to-last row, and so on. The Width remains unchanged.
Flips the PriceGrid along its vertical axis. The leftmost column becomes the rightmost column, the second column becomes the second-to-last column, and so on. The Height remains unchanged.
Returns true if one or more columns contain no populated cells.
A column is considered empty when all of its cells contain '.'.
Gets or sets the number of rows in the PriceGrid. The constructor assigns this property to the requested grid height.
Returns the underlying two-dimensional character array containing the PriceGrid data.
Cells containing '.' represent empty space, while cells containing 'X' represent mapped price data.
The first array dimension represents the x coordinate, or column, and the second represents the y coordinate, or row.
Creates and returns a PriceGrid from a string previously generated by Persist.
The persisted format begins with the Width, Height, and first cell character, followed by run-length counts representing alternating runs of '.' and 'X' cells.
For example, the general format is:
Parse throws an ArgumentException if the supplied string does not contain enough tokens to represent a PriceGrid. ### Persist
Returns a multi-line string representation of the PriceGrid.
Each row of the grid becomes a line in the string, allowing the pattern of '.' and 'X' cells to be viewed directly.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript { public class PriceGridPictogramExample : UserStrategyBase { public override void Initialize(BarHistory bars) { if (bars.Count < 20) return; PriceGrid pg = new PriceGrid( 20, 10, bars, bars.Count - 20, bars.Count - 1); SetTextDrawingFont( new WLFont("Courier New", 12)); DrawHeaderText(pg.Pictogram); } public override void Execute(BarHistory bars, int idx) { } } }
Creates and returns a new PriceGrid with the specified dimensions based on the data in the current grid.
Each destination cell is populated with 'X' if the corresponding region of the original grid contains at least one populated cell.
Returns null if newWidth or newHeight is less than or equal to 1.
Gets or sets the number of columns in the PriceGrid. The constructor assigns this property to the requested grid width.