#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion
namespace ScriptCode {
/// <summary>
/// Pattern scripts are used for recognizing and highlighting chart patterns based on the price, volume and open interest of an underlying symbol.
/// Common use-cases include plotting them on a chart, displaying them as watchlist columns and using them to implement other scripts.
/// </summary>
public partial class MyPattern : PatternScriptBase // NEVER CHANGE THE CLASS NAME
{
#region Variables
// The upper donchian indicator.
private Indicator _donchianUpper;
// The lower donchian indicator.
private Indicator _donchianLower;
// The middle donchian indicator.
private Indicator _donchianMiddle;
// The number of consecutive bars considered when determining if pattern holds true.
private int _patternLookback;
// The minimum required percentage of bars that make new highs throughout the lookback of the pattern.
private double _percentNewHighsRequired;
#endregion
#region OnInitialize
/// <summary>
/// This function accepts the user parameters for the script and is called when a new pattern instance is created.
/// One of the parameters accepted by it must be that of a symbol or another script that is
/// based on a symbol (drawing, indicator, pattern or signal). This symbol will be used as the underlying symbol for the pattern.
///
/// The parameter values can be specified from the user interface (UI) or from another script, depending on usage.
/// </summary>
/// --------------------------------------------------------------------------------------------------
/// PLEASE USE THE SCRIPT WIZARD (CTRL+W) TO ADD, EDIT AND REMOVE THE SCRIPT PARAMETERS
/// --------------------------------------------------------------------------------------------------
/// YOU MUST SET A PARAM TAG FOR EACH PARAMETER ACCEPTED BY THIS FUNCTION.
/// ALL PARAM TAGS SHOULD BE SET IN THE 'OnInitialize' REGION, RIGHT ABOVE THE 'OnInitialize' FUNCTION.
/// THE ORDER OF THE TAGS MUST MATCH THE ORDER OF THE ACTUAL PARAMETERS.
/// REQUIRED ATTRIBUTES:
/// (1) name: The exact parameter name.
/// (2) type: The type of data to collect from the user:
/// Set to "Integer" when the data type is 'int'
/// Set to "IntegerArray" when the data type is 'int[]'
/// Set to "DateTime" when the data type is 'long'
/// Set to "DateTimeArray" when the data type is 'long[]'
/// Set to "Boolean" when the data type is 'bool'
/// Set to "BooleanArray" when the data type is 'bool[]'
/// Set to "Double" when the data type is 'double'
/// Set to "DoubleArray" when the data type is 'double[]'
/// Set to "String" when the data type is 'string'
/// Set to "StringArray" when the data type is 'string[]'
/// Set to "Indicator" when the data type is 'Indicator'
/// Set to "Pattern" when the data type is 'Pattern'
/// Set to "Signal" when the data type is 'Signal'
/// Set to "Drawing" when the data type is 'Drawing'
/// Set to "Symbol" when the data type is 'int' representing a symbol index.
/// OPTIONAL ATTRIBUTES:
/// (3) default: The default parameter value is only valid when the type is Integer, Boolean, Double, String or an API Type.
/// (4) min: The minimum parameter value is only valid when the type is Integer or Double.
/// (5) max: The maximum parameter value is only valid when the type is Integer or Double.
/// EXAMPLE: <param name="" type="" default="" min="" max="">Enter the parameter description here.</param>
/// --------------------------------------------------------------------------------------------------
/// <param name="symbolIndex" type="Symbol">The symbol on which to calculate the pattern,
/// it can be replaced with a script that is based on a symbol (drawing, indicator, pattern or signal).</param>
/// <param name="donchianLookback" type="Integer" default="80" min="1">The number of periods used in calculating the donchian channel. </param>
/// <param name="patternLookback" type="Integer" default="80" min="1">The number of consecutive bars considered when determining if pattern holds true. </param>
/// <param name="percentNewHighsRequired" type="Double" default="20" min="0" max="100">The minimum required percentage of bars that make new highs throughout the lookback of the pattern. </param>
public void OnInitialize(
int symbolIndex,
int donchianLookback,
int patternLookback,
double percentNewHighsRequired) {
// Set the script parameters to script variables.
_patternLookback = patternLookback;
_percentNewHighsRequired = percentNewHighsRequired;
// Create the upper donchian indicator for the underlying symbol.
_donchianUpper = IndicatorDCU(SymbolIndex(), donchianLookback);
// Create the lower donchian indicator for the underlying symbol.
_donchianLower = IndicatorDCL(SymbolIndex(), donchianLookback);
// Create the middle donchian indicator for the underlying symbol.
_donchianMiddle = IndicatorDIVC(IndicatorADD(_donchianLower, _donchianUpper), 2);
}
#endregion
#region OnBarUpdate
/// <summary>
/// This function is used to determine whether a pattern match exists in which the latest bar is the last bar of the pattern.
/// If such a pattern match is found, the function returns the number of bars in the pattern, otherwise it returns zero.
/// </summary>
/// <returns type="Integer">The total number of bars in the pattern ending at the latest bar (or zero if the pattern is not found).</returns>
public override int OnBarUpdate() {
// Check whether the symbol traded at or above the middle donchian indicator throughout the current bar.
if(DataLow() >= _donchianMiddle[0]){
return 0;
} else {
// Create a variable to hold whether the symbol traded at or above the middle donchian indicator throughout the lookback of the pattern.
bool priceAboveMiddle = true;
// Create a variable to hold the number of bars making new highs.
int newHighsAchieved = 0;
// Iterate backwards throughout the lookback of the pattern starting with the previous bar.
for(int barShift = 1; barShift <= _patternLookback; barShift++){
// Record whether the symbol a makes new high.
if(_donchianUpper[barShift + 1] < _donchianUpper[barShift])
newHighsAchieved++;
// Check whether the symbol traded below the middle donchian indicator throughout the bar.
if(DataLow(barShift) < _donchianMiddle[barShift]){
priceAboveMiddle = false;
break;
}
}
// Calculate percent of new highs acheived.
double percentNewHighsAcheived = newHighsAchieved / (double)_patternLookback * 100;
// Check whether all conditions of the pattern hold true.
if(priceAboveMiddle && percentNewHighsAcheived >= _percentNewHighsRequired && DataLow() < _donchianMiddle[0])
return _patternLookback;
else
return 0;
}
}
#endregion
}
}