#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_3014_ImportedScripts import *
# ---------------------------------------------------------------
#endregion
## <summary>
## 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.
## </summary>
class MyTradingStrategy(ScriptCode.TradingStrategyScriptBase): # NEVER CHANGE THE CLASS NAME
#region Variables
# Variables Content
#endregion
#region OnInitialize
## <summary>
## 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.
## </summary>
## --------------------------------------------------------------------------------------------------
## 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: <param name="" type="" default="" min="" max="">Enter the parameter description here.</param>
### --------------------------------------------------------------------------------------------------
def OnInitialize(self):
# Create an SMA indicator over the Close of the underlying symbol.
self._sma = IndicatorSMA(self, IndicatorCLOSE(self, SymbolIndex()), 12)
# Plot the SMA indicator.
smaIndicatorId = ChartIndicatorPlot(SymbolIndex(), self._sma, "SMA", -1, 1)
# Set a color for the SMA indicator on the chart.
ChartIndicatorSetPenByIndex(SymbolIndex(), smaIndicatorId, 0, C_Color.RED, C_DashStyle.SOLID, 2)
# Set a plot style for the indicator.
ChartIndicatorSetPlotStyle(SymbolIndex(), smaIndicatorId, C_PlotStyle.SQUARE)
# Customize the chart panel background (there are many more chart panel customization options)
ChartPanelSetBackground(SymbolIndex(), 1, C_Color.DARK_BLUE)
# Customize the x-axis colors (there are many more x-axis customization options)
ChartXAxisSetColors(SymbolIndex(), C_Color.DARK_BLUE, C_Color.WHITE)
# Customize the y-axis colors (there are many more y-axis customization options)
ChartYAxisSetColors(SymbolIndex(), 1, C_Color.DARK_BLUE, C_Color.WHITE, False)
# Customize the data box colors (there are many more data box customization options)
ChartDataBoxSetColors(SymbolIndex(), 1, C_Color.DARK_BLUE, C_Color.WHITE)
# Create an RSI indicator for underlying symbol.
self._rsi = IndicatorRSI(self, IndicatorCLOSE(self, SymbolIndex()), 10)
# Plot the RSI indicator in the second panel.
rsiIndicatorId = ChartIndicatorPlot(SymbolIndex(), self._rsi, "RSI", -1, 2)
# Set a color for the RSI indicator on the chart.
ChartIndicatorSetPenByIndex(SymbolIndex(), rsiIndicatorId, 0, C_Color.ORANGE, C_DashStyle.DASH, 2)
# Set a plot style for the indicator.
ChartIndicatorSetPlotStyle(SymbolIndex(), rsiIndicatorId, C_PlotStyle.LINE)
# Create a volume indicator for underlying symbol.
self._volume = IndicatorVOLUME(self, SymbolIndex())
# Plot the volume indicator in the third panel.
volumeIndicatorId = ChartIndicatorPlot(SymbolIndex(), self._volume, "Volume", -1, 3)
# Set a color for the volume indicator on the chart.
ChartIndicatorSetPenByIndex(SymbolIndex(), volumeIndicatorId, 0, C_Color.WHITE, C_DashStyle.SOLID, 2)
# Set a plot style for the indicator.
ChartIndicatorSetPlotStyle(SymbolIndex(), volumeIndicatorId, C_PlotStyle.BOX_BOTTOM)
# Create to Drawing object to plot on the chart.
self._buyText = DrawingTEXT(self, SymbolIndex(), "Time to buy")
# Create to Drawing object to plot on the chart.
self._sellText = DrawingTEXT(self, SymbolIndex(), "Time to sell")
#endregion
#region OnBarUpdate
## <summary>
## 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.
## </summary>
## <param name="symbolIndex" type="Integer">The index of the symbol in the strategy symbol table</param>
## <param name="dataSeries" type="Integer">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).</param>
## <param name="completedBars" type="Integer">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).</param>
def OnBarUpdate(self, symbolIndex, dataSeries, completedBars):
# Check whether a buy signal is generated.
if self._rsi[1] > 30 and self._rsi[0] < 30 and self._sma[5] < self._sma[0]:
# Set the anchor point of the text drawing object.
DrawingObjectSetAnchorPoint(self._buyText, 0, 0, self._rsi[0])
# Plot the text on the chart.
ChartDrawingPlot(symbolIndex, self._buyText, "", -1, 2)
# Check whether a sell signal is generated.
elif self._rsi[1] < 70 and self._rsi[0] > 70 and self._sma[5] > self._sma[0]:
# Set the anchor point of the text drawing object.
DrawingObjectSetAnchorPoint(self._sellText, 0, 0, self._rsi[0])
# Plot the text on the chart.
ChartDrawingPlot(symbolIndex, self._sellText, "", -1, 2)
#endregion
#region OnOrderFillUpdate
## <summary>
## This function is called for each new order fill.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index</param>
## <param name="orderIndex" type="Integer">The order index</param>
## <param name="orderFillIndex" type="Integer">The order fill index</param>
def OnOrderFillUpdate(self, symbolIndex, orderIndex, status):
# OnOrderFillUpdate Content
pass
#endregion
#region OnOrderUpdate
## <summary>
## This function is called when an order is executed or cancelled.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index</param>
## <param name="orderIndex" type="Integer">The order index</param>
## <param name="status" type="C_Status">The updated status of the order</param>
def OnOrderUpdate(self, symbolIndex, orderIndex, status):
# OnOrderUpdate Content
pass
#endregion
#region OnPositionUpdate
## <summary>
## This function is called when a position is opened or closed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index</param>
## <param name="positionIndex" type="Integer">The position index</param>
## <param name="status" type="C_PositionStatus">The updated status of the position</param>
def OnPositionUpdate(self, symbolIndex, positionIndex, status):
# OnPositionUpdate Content
pass
#endregion
#region OnSessionUpdate
## <summary>
## This function is called when a session is opened or closed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index whose session is updated</param>
## <param name="status" type="C_SessionStatus">The session status</param>
def OnSessionUpdate(self, symbolIndex, status):
# OnSessionUpdate Content
pass
#endregion
#region OnNewsUpdate
## <summary>
## This function is called when a news update is received and only if the NO_NEWS_UPDATES comment is removed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index for the update</param>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="title" type="String">The update title</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The update message type</param>
def OnNewsUpdate(self, symbolIndex, dateTime, title, message, type):
# OnNewsUpdate Content
# [NO_NEWS_UPDATES] - Delete this comment to enable news updates to this strategy.
pass
#endregion
#region OnRSSUpdate
## <summary>
## This function is called when an RSS update is received and only if the NO_RSS_UPDATES comment is removed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index for the update</param>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="title" type="String">The update title</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The message type</param>
def OnRSSUpdate(self, symbolIndex, dateTime, title, message, type):
# OnRSSUpdate Content
# [NO_RSS_UPDATES] - Delete this comment to enable RSS updates to this strategy.
pass
#endregion
#region OnAlertUpdate
## <summary>
## This function is called when an alert update is received and only if the NO_ALERT_UPDATES comment is removed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index for the update</param>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The update message type</param>
def OnAlertUpdate(self, symbolIndex, dateTime, message, type):
# OnAlertUpdate Content
# [NO_ALERT_UPDATES] - Delete this comment to enable alert updates to this strategy.
pass
#endregion
#region OnJournalUpdate
## <summary>
## This function is called when a journal update is received and only if the NO_JOURNAL_UPDATES comment is removed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index for the update</param>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="title" type="String">The update title</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The update message type</param>
def OnJournalUpdate(self, symbolIndex, dateTime, title, message, type):
# OnJournalUpdate Content
# [NO_JOURNAL_UPDATES] - Delete this comment to enable journal updates to this strategy.
pass
#endregion
#region OnDataConnectionUpdate
## <summary>
## This function is called when a data connection update is received and only if the NO_DATA_CONNECTION_UPDATES comment is removed.
## </summary>
## <param name="symbolIndex" type="Integer">The symbol index for the update</param>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The update message type</param>
def OnDataConnectionUpdate(self, symbolIndex, dateTime, message, type):
# OnDataConnectionUpdate Content
# [NO_DATA_CONNECTION_UPDATES] - Delete this comment to enable data connection updates to this strategy.
pass
#endregion
#region OnBrokerConnectionUpdate
## <summary>
## This function is called when a broker connection update is received and only if the NO_BROKER_CONNECTION_UPDATES comment is removed.
## </summary>
## <param name="dateTime" type="DateTime">The date/time in which the update was received by the platform</param>
## <param name="message" type="String">The update message</param>
## <param name="type" type="C_MessageType">The update message type</param>
def OnBrokerConnectionUpdate(self, dateTime, message, type):
# OnBrokerConnectionUpdate Content
# [NO_BROKER_CONNECTION_UPDATES] - Delete this comment to enable broker connection updates to this strategy.
pass
#endregion
#region OnShutdown
## <summary>
## This function is called when the script is shutdown.
## </summary>
def OnShutdown(self):
# OnShutdown Content
pass
#endregion