#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 short-term indicator with which trading signals will be generated.
private Indicator _indicatorShort;
// The medium-term indicator with which trading signals will be generated.
private Indicator _indicatorMedium;
// The long-term indicator with which trading signals will be generated.
private Indicator _indicatorLong;
// The number of periods used in the construction of the short-term indicator.
private int _indicatorShortPeriods;
// The number of periods used in the construction of the medium-term indicator.
private int _indicatorMediumPeriods;
// The number of periods used in the construction of the long-term indicator.
private int _indicatorLongPeriods;
// The number of periods used to determine if the price action has converged with the moving averages.
private int _priceActionConvergencePeriods;
#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="indicatorShortPeriods" type="Integer" default="50">The number of periods in the short-term indicator.</param>
/// <param name="indicatorMediumPeriods" type="Integer" default="100">The number of periods in the medium-term indicator.</param>
/// <param name="indicatorLongPeriods" type="Integer" default="200">The number of periods in the long-term indicator.</param>
/// <param name="priceActionConvergencePeriods" type="Integer" default="3" min="0">The number of periods used to determine if the price action has converged with the moving averages.</param>
public void OnInitialize(int symbolIndex,
int indicatorShortPeriods,
int indicatorMediumPeriods,
int indicatorLongPeriods,
int priceActionConvergencePeriods) {
// Set the script parameters to script variables.
_indicatorShortPeriods = indicatorShortPeriods;
_indicatorMediumPeriods = indicatorMediumPeriods;
_indicatorLongPeriods = indicatorLongPeriods;
_priceActionConvergencePeriods = priceActionConvergencePeriods;
// Create the short-term EMA indicator over the bar close indicator of the underlying symbol.
_indicatorShort = IndicatorEMA(IndicatorCLOSE(SymbolIndex()), indicatorShortPeriods);
// Create the medium-term EMA indicator over the bar close indicator of the underlying symbol.
_indicatorMedium = IndicatorEMA(IndicatorCLOSE(SymbolIndex()), indicatorMediumPeriods);
// Create the long-term EMA indicator over the bar close indicator of the underlying symbol.
_indicatorLong = IndicatorEMA(IndicatorCLOSE(SymbolIndex()), indicatorLongPeriods);
}
#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() {
// Create variable to hold whether the price action converged with the moving averages.
bool priceActionConverged = false;
// Iterate backward until the number of periods used to determing if the price action coverges is exhausted or it is determined that the price action has converged.
for(int barShift = 0; barShift <= (_priceActionConvergencePeriods - 1); barShift++){
// Check whether the price action has converged with either the short-term or medium-term indicators.
if(DataLow(barShift) <= _indicatorShort[barShift] && _indicatorShort[barShift] <= DataHigh(barShift)
|| DataLow(barShift) <= _indicatorMedium[barShift] && _indicatorMedium[barShift] <= DataHigh(barShift)){
priceActionConverged = true;
break;
}
}
// Check whether the short-term indicator crossed below the medium-term indicator.
if(_indicatorShort[0] < _indicatorMedium[0] && _indicatorShort[1] >= _indicatorMedium[1]
// Check whether the price action converged.
&& priceActionConverged
// Check whether the short-term indicator is above the long-term indicator.
&& _indicatorShort[0] > _indicatorLong[0]
// Check whether the underlying symbol closes below the short-term indicator.
&& DataClose() < _indicatorShort[0]
// Ensure that enough bars have past to make indicator values meaningful.
&& PatternValueCount() >= _indicatorShortPeriods
&& PatternValueCount() >= _indicatorMediumPeriods
&& PatternValueCount() >= _indicatorLongPeriods)
return _priceActionConvergencePeriods;
else
return 0;
}
#endregion
}
}