Summary:
This is a generalized trading strategy that can be used with any indicators that implement the Crossover signal generation template. Examples of indicators that can use such a template include simple moving average [SMA] indicators, exponentially weighted moving average [EMA] indicators, and volume weighted average price [VWAP] indicators.
This strategy will take positions when the two specified indicators cross one another. A positive crossover generates a long signal and occurs when the first indicator crosses above the second indicator from below. A negative crossover generates a short signal and occurs when the first indicator crosses below the second indicator from above.
Trading Rules:
Long Entry: A buy market order is generated when the first specified indicator crosses above the second specified indicator from below.
Long Exit: A sell market order is generated when the first specified indicator crosses below the second specified indicator from above.
Short Entry: A sell short market order is generated when the first specified indicator crosses below the second specified indicator from above.
Short Exit: A buy market order is generated when the first specified indicator crosses above the second specified indicator from below.
#region Namespaces
# ---------- DON'T REMOVE OR EDIT THESE LINES -------------------
# These lines are required for integrating Python with our .NET platform.
import clr
clr.AddReference["Tickblaze.Model"]
import ScriptCode
from TradingStrategyAPI import *
from AssemblyTradingStrategy_6001_ImportedScripts import *
# ---------------------------------------------------------------
#endregion
##
## Trading Strategy scripts are used for trading one symbol at a time such that each symbol gets its own strategy instance.
## Common use-cases include momentum strategies, crossover strategies and overbought / oversold strategies, all of which need to evaluate only a single symbol at a time in order to make trading decisions.
##
class MyTradingStrategy[ScriptCode.TradingStrategyScriptBase]: # NEVER CHANGE THE CLASS NAME
#region Variables
# Variables Content
#endregion
#region OnInitialize
##
## This function is used for accepting the script parameters and for initializing the script prior to all other function calls.
## Once the script is assigned to a Desktop, its parameter values can be specified by the user and can be selected for optimization.
##
## --------------------------------------------------------------------------------------------------
## INSTRUCTIONS - PLEASE READ CAREFULLY
## --------------------------------------------------------------------------------------------------
## 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' [The 'long' data type can only be used for date/time representation]
## Set to "DateTimeArray" when the data type is 'long[]' [The 'long' data type can only be used for date/time representation]
## 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'
## 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: Enter the parameter description here.
### --------------------------------------------------------------------------------------------------
## The first indicator with which trading signals will be generated.
## The color of the first indicator.
## The plot style of the first indicator.
## The second indicator with which trading signals will be generated.
## The color of the second indicator.
## The plot style of the second indicator.
## Indicates whether to enable the trading strategy to short symbols.
## Indicates whether to enable the trading strategy to long symbols.
## The percent distance from the entry price in which to place a stop loss order. [0 to ignore].
## The percent distance from the entry price in which to place a take profit order. [0 to ignore].
def OnInitialize[self,
indicator1,
indicator1Color,
indicator1PlotStyle,
indicator2,
indicator2Color,
indicator2PlotStyle,
enableShorting,
enableLonging,
stopLoss,
takeProfit]:
# Set the script parameters to script variables.
self._enableShorting = enableShorting
self._enableLonging = enableLonging
self._stopLoss = stopLoss
self._takeProfit = takeProfit
# Remove all of the indicators from the chart so that we don't get duplicates.
ChartIndicatorRemoveAll[SymbolIndex[]]
# Keep the specified indicator.
self._indicator1 = indicator1
# Plot the indicator on the underlying symbol's chart.
indicator1ItemID = ChartIndicatorPlot[SymbolIndex[], self._indicator1, "", - 1, 1]
# Set the indicator pen.
ChartIndicatorSetPenByIndex[SymbolIndex[], indicator1ItemID, 0, indicator1Color, C_DashStyle.SOLID, 2]
# Set the indicator style.
ChartIndicatorSetPlotStyle[SymbolIndex[], indicator1ItemID, indicator1PlotStyle]
# Keep the specified indicator.
self._indicator2 = indicator2
# Plot the indicator on the underlying symbol's chart.
indicator2ItemID = ChartIndicatorPlot[SymbolIndex[], self._indicator2, "", - 1, 1]
# Set the indicator pen.
ChartIndicatorSetPenByIndex[SymbolIndex[], indicator2ItemID, 0, indicator2Color, C_DashStyle.SOLID, 2]
# Set the indicator style.
ChartIndicatorSetPlotStyle[SymbolIndex[], indicator2ItemID, indicator2PlotStyle]
#endregion
#region OnBarUpdate
##
## This function is called after each new bar of each symbol assigned to the Desktop strategy.
## It should evaluate the specified symbol and its new bar in order to determine whether to generate new orders for it.
## Never create indicators, signals or patterns from OnBarUpdate, for performance reasons those should be created from OnInitialize.
##
## The index of the symbol in the strategy symbol table
## The number indicating the data series from which the symbol was updated.
## According to the Desktop strategy data series settings: 0 for the main data series, 1 for the second data series, etc. [See the DataSeriesSwitch function].
## The number of completed bars for the specified symbol since the last call to OnBarUpdate.
## Always 1 unless the bar type can generate multiple completed bars from a single tick/minute/day update [depending on the underlying bar source].
def OnBarUpdate[self, symbolIndex, dataSeries, completedBars]:
# Check whether only one bar has been processed so far by the script.
if SessionTotalBarCount[] self._indicator2[0] and self._indicator1[1]