Search Framework:
PriceGrid
Namespace: WealthLab.Core
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.
Constructors
PriceGrid
public PriceGrid(
int width,
int height)
public PriceGrid(
int width,
int height,
BarHistory bars,
int startIndex,
int endIndex)
public PriceGrid(
int width,
int height,
TimeSeries ts,
int startIndex,
int endIndex)

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.



Members
Compare
public double Compare(
PriceGrid pg,
bool legacyCompareLogic = false)
public double Compare(
PriceGrid pg,
PriceGridCalculation calc)

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.
Example Code
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);
            }
        }
    }
}

CopyFrom
public void CopyFrom(PriceGrid pg)

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.


Fill
public void Fill(char c)
public void Fill(
BarHistory bars,
int startIndex,
int endIndex)
public void Fill(
TimeSeries tsLow,
TimeSeries tsHigh,
int startIndex,
int endIndex)
public void Fill(
TimeSeries ts,
int startIndex,
int endIndex)

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 '.'.


FixEmptyCols
public void FixEmptyCols()

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.


FlipHorizontal
public void FlipHorizontal()

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.


FlipVertical
public void FlipVertical()

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.


HasEmptyCols
public bool HasEmptyCols

Returns true if one or more columns contain no populated cells. A column is considered empty when all of its cells contain '.'.


Height
public int Height

Gets or sets the number of rows in the PriceGrid. The constructor assigns this property to the requested grid height.


Matrix
public char[,] Matrix

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.


Parse
public static PriceGrid Parse(string s)

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:

Example Code
Parse throws an ArgumentException if the supplied string does not contain enough tokens to represent a PriceGrid.
### Persist

Pictogram
public string Pictogram

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.

Example Code
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)
        {
        }
    }
}

Resample
public PriceGrid Resample(
int newWidth,
int newHeight)

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.


Width
public int Width

Gets or sets the number of columns in the PriceGrid. The constructor assigns this property to the requested grid width.