Saturday, September 04, 2010

Indicator Support / Resistance [ISR]

Descripton
Summary:
This trading strategy script generates orders based on support and resistance levels of an indicator.

Rules:
1. Buy Market - When the main indicator touches or nearly touches the support indicator.
2. Sell Market - When the main indicator touches or nearly touches the resistance indicator.
3. Sell Short Market - When the main indicator touches or nearly touches the resistance indicator.
4. Buy To Cover Market -  When the main indicator touches or nearly touches the support indicator.


Variables
TypeIdentifierDescription
IntegerArray_indicatorKeysUse for holding all of the indicator keys duplicated from the specified indicator key and indexed by symbol index.
IntegerArray_supportIndicatorKeysUse for holding all of the support indicator keys duplicated from the specified support indicator key and indexed by symbol index.
IntegerArray_resistanceIndicatorKeysUse for holding all of the resistance indicator keys duplicated from the specified resistance indicator key and indexed by symbol index.
Number_supportMarginUse for the percentage distance from the support level in which support starts.
Number_resistanceMarginUse for the percentage distance from the resistance level in which resistance starts.
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 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
IndicatorindicatorKeyUse for the indicator key to duplicate for all of the account symbols and with which trading signals will be generated.
IndicatorsupportIndicatorKeyUse for the support indicator key to duplicate for all of the account symbols and with which trading signals will be generated.
IndicatorresistanceIndicatorKeyUse for the resistance indicator key to duplicate for all of the account symbols and with which trading signals will be generated.
NumbersupportMarginUse for the percentage distance from the support level in which support starts. [Default 0.2]
NumberresistanceMarginUse for the percentage distance from the resistance level in which resistance starts. [Default 0.2]
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 stop loss, take profit and trailing stop are measured in percentages or absolute values. [Default True]
Implementation
	 ' Assign the script parameters to script variables.
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent
	_supportMargin = supportMargin
	_resistanceMargin = resistanceMargin
	_enableShorting = enableShorting
	
	 ' Use for holding indicator keys based on the specified indicator key, indexed by symbol index.
	Redefine _indicatorKeys(SymbolCount() - 1)
	 ' Use for holding indicator keys based on the specified support indicator key, indexed by symbol index.
	Redefine _supportIndicatorKeys(SymbolCount() - 1)
	 ' Use for holding indicator keys based on the specified resistance indicator key, indexed by symbol index.
	Redefine _resistanceIndicatorKeys(SymbolCount() - 1)
	 ' Iterate over all of the account symbols.
	For i As Integer = 0 To SymbolCount() - 1
		 ' Create an indicator for each account symbol, based on the specified indicator.
		_indicatorKeys(i) = IndicatorCopy(indicatorKey, i)
		 ' Add the indicator to the symbol chart.
		ChartIndicator(i, 0,C_MERGE_RIGHT, _indicatorKeys(i),C_LINE,1, 0)
		
		 ' Create an indicator for each account symbol, based on the specified support indicator.
		_supportIndicatorKeys(i) = IndicatorCopy(supportIndicatorKey, i)
		 ' Add the indicator to the symbol chart.
		ChartIndicator(i, 0,C_MERGE_RIGHT, _supportIndicatorKeys(i),C_LINE,1, 1)
		
		 ' Create an indicator for each account symbol, based on the specified resistance indicator.
		_resistanceIndicatorKeys(i) = IndicatorCopy(resistanceIndicatorKey, i)
		 ' Add the indicator to the symbol chart.
		ChartIndicator(i, 0,C_MERGE_RIGHT, _resistanceIndicatorKeys(i),C_LINE,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 indicator value, indexed by bar index.
	Define value As Number = IndicatorValue(_indicatorKeys(symbolIndex))
	 ' Get the support indicator value, indexed by bar index.
	Define supportValue As Number = IndicatorValue(_supportIndicatorKeys(symbolIndex))
	 ' Get the resistance indicator value, indexed by bar index.
	Define resistanceValue As Number = IndicatorValue(_resistanceIndicatorKeys(symbolIndex))
	
	 ' Support.
	If (supportValue <= value And value <= supportValue * (1 + _supportMargin)) 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, "Indicator hit support.")
		End If
		 ' If no long open positions exist.
		If (Not PositionExists(C_OPEN, symbolIndex, C_LONG))
			OutputWriteLine(supportValue & " " & value & " " & supportValue * (1 + _supportMargin))
			 ' Generate a buy market order.
			Define orderIndex As Number = BrokerMarket(C_BUY, symbolIndex, 0, C_DAY, "Indicator hit support. ")
			 ' 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
	 ' Resistance.
	ElseIf  (resistanceValue * (1 - _resistanceMargin) <= value And value <= resistanceValue) 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, "Indicator hit resistance.")
		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, "Indicator hit resistance.")
			 ' 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.