Thursday, September 09, 2010

Two Analysts Average Signal [TAAS]

Descripton
Summary:
This trading strategy script generates orders based on the average signal from two analysts. 

Rules:
1. Buy Market - When the average analyst signal is higher than a specified threshold.
2. Sell Market - When the average analyst signal is lower than a specified threshold.
3. Sell Short Market - When the average analyst signal is lower than a specified threshold.
4. Buy To Cover Market - When the average analyst signal is higher than a specified threshold.


Variables
TypeIdentifierDescription
IntegerArray_analystKeys1Use for holding all of the analyst keys duplicated from the first specified analyst key and indexed by symbol index.
IntegerArray_analystKeys2Use for holding all of the analyst keys duplicated from the second specified analyst key and indexed by symbol index.
Integer_buyAboveUse for the minimum signal average value above which to generate a buy order.
Integer_sellBelowUse for the maximum signal average value below which to generate a sell order.
Boolean_enableShortingUse for indicating whether to enable the trading system script to short symbols. [Default False]
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 measuresd in percent or absolute values.

OnInitialize
Function Parameters
TypeIdentifierDescription
AnalystanalystKey1Use for the analyst key to duplicate for all of the account symbols and with which trading signals will be generated.
AnalystanalystKey2Use for the analyst key to duplicate for all of the account symbols and with which trading signals will be generated.
IntegerbuyAboveUse for the minimum signal average value above which to generate a buy order. [Default 50]
IntegersellBelowUse for the maximum signal average value below which to generate a sell order. [Default -50]
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
	_enableShorting = enableShorting
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent
	
	 ' Use for holding analyst keys based on the first specified analyst key, indexed by symbol index.
	Redefine _analystKeys1(SymbolCount() - 1)
	 ' Use for holding analyst keys based on the second specified analyst key, indexed by symbol index.
	Redefine _analystKeys2(SymbolCount() - 1)
	 ' Iterate over all of the account symbols.
	For i As Integer = 0 To SymbolCount() - 1
		 ' Create an analyst for each account symbol, based on the first specified analyst.
		_analystKeys1(i) = AnalystCopy(analystKey1, i)
		 ' Add the analyst to the symbol chart.
		ChartAnalyst(i, 0, _analystKeys1(i),1, 2)
		 ' Create an analyst for each account symbol, based on the second specified analyst.
		_analystKeys2(i) = AnalystCopy(analystKey2, i)
		 ' Add the analyst to the symbol chart.
		ChartAnalyst(i, 0, _analystKeys2(i),1, 2)
	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 first analyst signal value for the specified symbol index.
	Define signalValue1 As Integer = AnalystSignal(_analystKeys1(symbolIndex))
	 ' Get the second analyst signal value for the specified symbol index.
	Define signalValue2 As Integer = AnalystSignal(_analystKeys2(symbolIndex))
	 ' Use for the average signal value.
	Define signal As Number = (signalValue1 + signalValue2) / 2
	
	 ' If the average analyst signal value is above the buy threshold.
	If (signal >= _buyAbove) 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, "Analyst signals average " & signal)
		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, "Analyst signals average " & signal)
			 ' 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
	
	 ' If the average analyst signal value is below the sell threshold.
	If (signal <= _sellBelow) 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, "Analyst signals average " & signal)
		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, "Analyst signals average " & signal)
			 ' 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.