Saturday, September 04, 2010

Two Analysts Signals [TAS]

Descripton
Summary:
This trading strategy script generates orders based on the signals of two analysts. 

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


Variables
TypeIdentifierDescription
IntegerArray_buyAnalystKeysUse for holding all of the analyst keys duplicated from the first specified analyst key and indexed by symbol index.
IntegerArray_sellAnalystKeysUse 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.
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 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
AnalystbuyAnalystKeyUse for the analyst key to duplicate for all of the account symbols and with which buy trading signals will be generated.
AnalystsellAnalystKeyUse for the analyst key to duplicate for all of the account symbols and with which sell 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 _buyAnalystKeys(SymbolCount()-1)
	 ' Use for holding analyst keys based on the second specified analyst key, indexed by symbol index.
	Redefine _sellAnalystKeys(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.
		_buyAnalystKeys(i) = AnalystCopy(buyAnalystKey, i)
		 ' Add the analyst to the symbol chart.
		ChartAnalyst(i, 0, _buyAnalystKeys(i),1,2)
		 ' Create an analyst for each account symbol, based on the second specified analyst.
		_sellAnalystKeys(i) = AnalystCopy(sellAnalystKey, i)
		 ' Add the analyst to the symbol chart.
		ChartAnalyst(i, 0, _sellAnalystKeys(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(_buyAnalystKeys(symbolIndex))
	 ' Get the second analyst signal value for the specified symbol index.
	Define signalValue2 As Integer = AnalystSignal(_sellAnalystKeys(symbolIndex))
	
	 ' If the first analyst signal value is above the buy threshold.
	If (signalValue1 >= _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, "First analyst signal " & signalValue1)
		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, "First analyst signal " & signalValue1)
			 ' 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.
	ElseIf (signalValue2 <= _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, "Second analyst signal " & signalValue2)
		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, "Second analyst signal " & signalValue2)
			 ' 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.