Parent: Object
Maps price data into a 2D grid, and allows you to compare grids to see how well different slices of the price data match.
PriceGrid supports two connstructors. The first one accepts Width and Height parameters, which describe the dimensions of the grid. This constructor fills the grid with '.' characters.
The second constructor lets you pass a BarHistory instance, and an accompanying startIndex and endIndex. This constructor will examine the data in this range, and divide into rows and columns that match the dimensions of the PriceGrid. Where data occurs in a cell, the grid is filled with 'X', and where no data occurs, with '.'.
Lets you compare one PriceGrid instance with another that has matching Width and Height. Returns a value between 0 and 100 that represents the percentage of cells that match between the two grids.
As of WL8 Build 12 there is a new calculation used. If you want to use the legacy calculation, pass true for legacyCompareLogic.
- New Compare Logic: Compares only cells that contain data in the base PriceGrid. If the comparison PriceGrid contains data as well, this counts as a "hit". The resulting score is equal to the percentage of "hits" based on the total number of cells with data. This method results in more accurate calculations, and generally lower score values.
- Legacy Compare Logic: Compares each cell of the two PriceGrids, and counts a match as long as the cells in both PriceGrids contain the same values (either on or off.) For PriceGrids that contain a large amount of empty space, this will lead to higher score results since both PriceGrid will match many "off" cells regardless of where the data occurs.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) { //find the first 10% 10-bar gain, and base the PriceGrid from the preceding 10 bars of data 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; pg = new PriceGrid(20, 10, bars, gridEnd - 19, gridEnd); SetTextDrawingFont(new WLFont("Courier New", 12)); DrawHeaderText(pg.Pictogram); StartIndex = n; //plot a TimeSeries showing how close the price data maps to the PriceGrid mapValues = new TimeSeries(bars.DateTimes); PlotTimeSeries(mapValues, "Map Values", "MV", WLColor.Purple); break; } } //Execute trading system rules public override void Execute(BarHistory bars, int idx) { //compare current price data to the map and get the confidence factor if (mapValues != null) { PriceGrid pgCurrent = new PriceGrid(20, 10, bars, idx - 19, idx); mapValues[idx] = pg.Compare(pgCurrent); } if (OpenPositions.Count > 0) { //hold positions 10 bars if (idx - LastOpenPosition.EntryBar >= 9) PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } else { //enter a position if confidence factor >= 80 if (mapValues[idx] >= 80.0) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } //private members private PriceGrid pg = null; private TimeSeries mapValues = null; } }
There are two overloads of the Fill method. The first fills the grid with the character specified in the c parameter.
The second overload accepts a BarHistory instance in the bars parameter, and an accompanying startIndex and endIndex. This overload will examine the data in this range, and divide into rows and columns that match the dimensions of the PriceGrid. Where data occurs in a cell, the grid is filled with 'X', and where no data occurs, with '.'.
Returns the height of the PriceGrid, as specified in the constructor.
Lets you reconstitute a PriceGrid instance from a string that was the result of a previous Persist call. The format of the PriceGrid persist string is relatively simple, and with some counting you can even encode them by hand. The format consists of:
Width,Height,StartChar,{Count1, Count2, ..., CountN}
Where StartChar is either '.' or 'X' and is the character occuping cell 0,0. The following token is a count of the number of times this starting character continues in the grid, going left to right and wrapping down to the next row if possible. The parsing then swaps the character from '." to 'X' or vice versa, and continues to the next run, until the parsing is completed and the grid is filled.
Returns a string representation of the PriceGrid. You can use this string to reconstitute the same PriceGrid using the Parse method.
Returns a string representation of the grid, where you can visualize the '.' and 'X' data contained in the rows and columns.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) { //find the first 10% 10-bar gain, and base the PriceGrid from the preceding 10 bars of data 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; pg = new PriceGrid(20, 10, bars, gridEnd - 19, gridEnd); SetTextDrawingFont(new WLFont("Courier New", 12)); DrawHeaderText(pg.Pictogram); StartIndex = n; //plot a TimeSeries showing how close the price data maps to the PriceGrid mapValues = new TimeSeries(bars.DateTimes); PlotTimeSeries(mapValues, "Map Values", "MV", WLColor.Purple); break; } } //Execute trading system rules public override void Execute(BarHistory bars, int idx) { //compare current price data to the map and get the confidence factor if (mapValues != null) { PriceGrid pgCurrent = new PriceGrid(20, 10, bars, idx - 19, idx); mapValues[idx] = pg.Compare(pgCurrent); } if (OpenPositions.Count > 0) { //hold positions 10 bars if (idx - LastOpenPosition.EntryBar >= 9) PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } else { //enter a position if confidence factor >= 80 if (mapValues[idx] >= 80.0) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } //private members private PriceGrid pg = null; private TimeSeries mapValues = null; } }
Returns the width of the PriceGrid, as specified in the constructor.