Tuesday, September 07, 2010

Multi Pattern Match [MPM]

Descripton
Summary:
This trading strategy script generates orders based on the signals of two pattern lists, such that one pattern list holds bullish patterns and the other holds bearish patterns.

Rules:
1. Buy Market - When one of the bullish patterns is matched.
2. Sell Market - When one of the bearish patterns is matched.
3. Sell Short Market - When one of the bearish patterns is matched.
4. Buy To Cover Market - When one of the bullish patterns is matched.


Variables
TypeIdentifierDescription
IntegerArray_buyPatternKeysUse for the pattern keys to duplicate for all of the account symbols and with which buy trading signals are generated.
IntegerArray_sellPatternKeysUse for the pattern keys to duplicate for all of the account symbols and with which sell trading signals are generated.
Boolean_enableShortingUse for indicating whether to enable the trading system script to short symbols.
Integer_buyPatternKeyCountUse for the number of buy pattern keys.
Integer_sellPatternKeyCountUse for the number of sell pattern keys.
Number_stopLossUse for the percent distance from the current price in which to place a stop order which would limit the maximum loss.
Number_takeProfitUse for the percent distance from the current price in which to place a limit order which would take profit after a specified gain.
Number_trailingStopUse for placing a trailing stop order which would limit the maximum trailing loss.
Boolean_isPercentUse for indicating whether the trailing stop is measured in percent or absolute value.

OnInitialize
Function Parameters
TypeIdentifierDescription
PatternArraybuyPatternKeysUse for the pattern keys to duplicate for all of the account symbols and with which buy trading signals are generated.
PatternArraysellPatternKeysUse for the pattern keys to duplicate for all of the account symbols and with which sell trading signals are generated.
BooleanenableShortingUse for indicating whether to enable the trading system script to short symbols. [Default False]
NumberstopLossUse for the percent distance from the current price in which to place a stop order which would limit the maximum loss. [Default 0] (0 to ignore).
NumbertakeProfitUse for the percent distance from the current price in which to place a limit order which would take profit after a specified gain. [Default 0] (0 to ignore).
NumbertrailingStopUse for placing a trailing stop order which would limit the maximum trailing loss. [Default 0] (0 to ignore).
BooleanisPercentUse for indicating whether the trailing stop is measured in percent or absolute value. [Default True]
Implementation
	 ' Assign the script parameters to script variables.
	_enableShorting = enableShorting
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent
	
	_buyPatternKeyCount = buyPatternKeys.Length
	_sellPatternKeyCount = sellPatternKeys.Length
	 ' Use for holding all of the buy pattern keys indexed by symbol index. 
	Redefine _buyPatternKeys(SymbolCount() - 1, _buyPatternKeyCount - 1)
	 ' Use for holding all of the sell pattern keys indexed by symbol index. 
	Redefine _sellPatternKeys(SymbolCount() - 1, _sellPatternKeyCount - 1)
	 ' Iterate over all of the account symbols.
	For i As Integer = 0 To SymbolCount() - 1
		 ' Iterate over all of the pattern keys creating copies for the current symbol index.
		For j As Integer = 0 To _buyPatternKeyCount - 1
			 ' Create a pattern for each account symbol, based on the current buy pattern key.
			_buyPatternKeys(i,j) = PatternCopy(buyPatternKeys(j), i)
			 ' Add the pattern to the symbol chart.
			ChartPattern(i, 0, _buyPatternKeys(i,j),1, 1)
		Next
		For j As Integer = 0 To _sellPatternKeyCount - 1	
			 ' Create a pattern for each account symbol, based on the current sell pattern key.
			_sellPatternKeys(i,j) = PatternCopy(sellPatternKeys(j), i)
			 ' Add the pattern to the symbol chart.
			ChartPattern(i, 0, _sellPatternKeys(i,j),2, 1)
		Next	
	Next

OnSymbolBar
Function Parameters
TypeIdentifierDescription
IntegersymbolIndex
Implementation
	 ' Check whether the current symbol is a futures contract which isn 't a front month contract
	If (SymbolInstrument(symbolIndex) = C_FUTURES And Not SymbolIsFrontMonthContract(symbolIndex))
		Return
	End If
	
	 ' Use for the number of bullish pattern matches.
	Define bullishMatch As Number 
	 ' Use for the number of bearish pattern matches.
	Define bearishMatch As Number 
	 ' Iterate over all of the pattern keys of the specified symbol index.
	For i As Integer = 0 To _buyPatternKeyCount - 1
		bullishMatch += PatternMatch(_buyPatternKeys(symbolIndex,i))
	Next
	For i As Integer = 0 To _sellPatternKeyCount - 1
		bearishMatch += PatternMatch(_sellPatternKeys(symbolIndex,i))
	Next
	
	 ' If a bullish pattern has been matched, generate a buy order.
	If (bullishMatch >= 1) Then
		 ' If a short position exists then generate a buy to cover order.
		If (_enableShorting And PositionExists(C_OPEN, symbolIndex, C_SHORT)) Then
			 ' Generate a buy to cover market order.
			BrokerMarket(C_BUY_TO_COVER,symbolIndex, 0, C_DAY, "Bullish pattern match.")
		End If
		 ' If no long open positions exist.
		If (Not PositionExists(C_OPEN, symbolIndex, C_LONG))
			 ' Generate a buy market order.
			Define orderIndex As Number = BrokerMarket(C_BUY,symbolIndex, 0, C_DAY, "Bullish pattern match.")
			 ' Set a stop loss on the order.
			BrokerSetStopLossPercent(orderIndex, _stopLoss, True)
			 ' Set a take profit on the order.
			BrokerSetTakeProfitPercent(orderIndex, _takeProfit, True)
			 ' Set a trailing stop loss on the order.
			BrokerSetTrailingStopLoss(orderIndex, _isPercent, _trailingStop)
		End If
	 ' If a bearish pattern has been matched, generate a sell order.
	ElseIf (bearishMatch >= 1) Then
		 ' If a long open position exists.
		If (PositionExists(C_OPEN, symbolIndex, C_LONG))
			 ' Generate a sell market order.
			BrokerMarket(C_SELL,symbolIndex, 0, C_DAY, "Bearish pattern match.")
		End If
		 ' If no short open positions exist.
		If ( _enableShorting And Not PositionExists(C_OPEN, symbolIndex, C_SHORT)) Then
			 ' Generate a sell short market order.
			Define orderIndex As Number = BrokerMarket(C_SELL_SHORT,symbolIndex, 0, C_DAY, "Bearish pattern match.")
			 ' Set a stop loss on the order.
			BrokerSetStopLossPercent(orderIndex, _stopLoss, True)
			 ' Set a take profit on the order.
			BrokerSetTakeProfitPercent(orderIndex, _takeProfit, True)
			 ' Set a trailing stop loss on the order.
			BrokerSetTrailingStopLoss(orderIndex, _isPercent, _trailingStop)
		End If
	End If

OnExchangeBar
Function Parameters
TypeIdentifierDescription
IntegerArraysymbolIndexes
Implementation

Copyright © 2010 IQBroker, LLC. All rights reserved.