Saturday, September 04, 2010

Multi Analyst Average Signal with Bar Exit [MAASBE]

Descripton
Summary:
This trading strategy script generates entry orders based on the signals of multiple analysts, and exit signals after a specified number of bars.

Rules:
1. Buy Market - When the average analyst signal is higher than a specified threshold.
2. Sell Market - When the specified number of bars passed since the trade was opened.
3. Sell Short Market - When the average analyst signal is lower than a specified threshold.
4. Buy To Cover Market -  When the specified number of bars passed since the trade was opened.


Variables
TypeIdentifierDescription
IntegerArray_analystKeysUse for holding all of the analyst keys duplicated from the specified analyst key and indexed by symbol index.
Integer_buyAboveUse for the minimum average signal value above which to generate a buy order.
Integer_sellBelowUse for the maximum average signal value below which to generate a sell order.
Integer_barsToHoldUse for the number of bars to hold a trade open.
Boolean_enableShortingUse for indicating whether to enable the trading system script to short symbols.
Integer_analystKeyCountUse for the number of analyst 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 stop loss, take profit and trailing stop are measured in percentages or absolute values.

OnInitialize
Function Parameters
TypeIdentifierDescription
AnalystArrayanalystKeysUse for the analyst keys to duplicate for all of the account symbols and with which trading signals will be generated.
IntegerbuyAboveUse for the minimum signal value above which to generate a buy order. [Default 50]
IntegersellBelowUse for the maximum signal value below which to generate a sell order. [Default -50]
IntegerbarsToHoldUse for the number of bars to hold a trade open.
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.
	_buyAbove = buyAbove
	_sellBelow = sellBelow
	_barsToHold = barsToHold
	_enableShorting = enableShorting
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent
	
	_analystKeyCount = analystKeys.Length
	 ' Use for holding analyst keys based on the specified analyst key, indexed by symbol index.
	Redefine _analystKeys(SymbolCount() - 1, _analystKeyCount - 1)
	 ' Iterate over all of the account symbols.
	For i As Integer = 0 To SymbolCount() - 1
		 ' Iterate over all of the analyst keys creating copies for the current symbol index.
		For j As Integer = 0 To _analystKeyCount - 1
			  ' Create an analyst for each account symbol, based on the current analyst key.
			_analystKeys(i,j) = AnalystCopy(analystKeys(j), i)
			 ' Add the analyst to the symbol chart.
			ChartAnalyst(i, 0, _analystKeys(i,j),1, 2)
		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
	
	 ' Get the average analyst signel value.
	Define signalValue As Number 
	 ' Iterate over all of the analyst keys of the specified symbol index.
	For i As Integer = 0 To _analystKeyCount - 1
		 ' Add the analyst signal of the current analyst.
		signalValue += AnalystSignal(_analystKeys(symbolIndex,i))
	Next
	 ' Calculate the average analyst signal.
	signalValue = signalValue / _analystKeyCount
	
	 ' If the analyst signal value is above the buy threshold, generate a buy order.
	If (signalValue >= _buyAbove) Then
		 ' 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, "Analyst signal " & signalValue)
			 ' Set the maximum number of bars to hold the trade open.
			BrokerSetMaxBarHold(orderIndex, _barsToHold)
			 ' 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 the second analyst signal value is below the sell threshold, generate a sell order.
	ElseIf (signalValue <= _sellBelow) Then
		 ' 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, "Analyst signal " & signalValue)
			 ' Set the maximum number of bars to hold the trade open.
			BrokerSetMaxBarHold(orderIndex, _barsToHold)
			 ' 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.