Saturday, September 04, 2010

Envelopes Support / Resistance [ESR]

Descripton
Summary:
This trading system script uses Envelope indicators to generate trading signals whenever an underlying indicator touches the envelopes.

Rules:
1. Buy Market - When the underlying indicator touces the lower envelope.
2. Sell Market - When the underlying indicator touches the upper envelope.
3. Sell Short Market - When the underlying indicator touches the upper envelope.
4. Buy To Cover Market - When the underlying indicator touches the lower envelope.


Variables
TypeIdentifierDescription
IntegerArray_indicatorKeysUse for holding all of the specified indicator keys indexed by symbol index.
IntegerArray_ENVLKeysUse for holding all of the ENVL indicator keys indexed by symbol index.
IntegerArray_ENVUKeysUse for holding all of the ENVU indicator keys indexed by symbol index.
Number_hitDistanceUse for indicating the percentage distance from which the specified indicator is considered near one of the envelopes.
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 trailing stop is measured in percent or absolute value.

OnInitialize
Function Parameters
TypeIdentifierDescription
IndicatorindicatorKeyUse for the underlying indicator key on which to calculate the Envelope indicators.
NumberpercentageUse for the percentage shift of the envelope. (0 to 100) [Default 5]
IntegerperiodsUse for the number of periods in the Envelope calculations. [Default 10]
IntegerfirstPeriodsUse for the number of periods to include in the calculation of the first value of the exponential moving averages. [Default 0]
NumberhitDistanceUse for indicating the percentage distance from which the specified indicator is considered near one of the envelopes. [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 trailing stop is measured in percent or absolute value. [Default True]
Implementation
	 ' Assign the script parameters to script variables.
	_hitDistance = hitDistance
	_enableShorting = enableShorting
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent

	 ' Use for holding the specified indicator keys, indexed by symbol index.
	Redefine _indicatorKeys(SymbolCount() - 1)
	 ' Use for holding the ENVL indicator keys based on the specified indicator key, indexed by symbol index.
	Redefine _ENVLKeys(SymbolCount() - 1)
	 ' Use for holding the ENVU indicator keys based on the specified indicator key, indexed by symbol index.
	Redefine _ENVUKeys(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)
		 ' Create a ENVL indicator for each account symbol, based on the specified indicator.
		_ENVLKeys(i) = IndicatorENVL(_indicatorKeys(i),percentage, periods, firstPeriods)
		 ' Create a ENVU indicator for each account symbol, based on the specified indicator.
		_ENVUKeys(i) = IndicatorENVU(_indicatorKeys(i),percentage, periods, firstPeriods)
		 ' Add the indicators to the symbol chart.
		ChartIndicator(i, 0, C_MERGE_RIGHT, _indicatorKeys(i),C_LINE, 1,0)
		ChartIndicator(i, 0, C_MERGE_RIGHT, _ENVLKeys(i),C_LINE, 1,1)
		ChartIndicator(i, 0, C_MERGE_RIGHT, _ENVUKeys(i),C_LINE, 1,1)
	Next

OnSymbolBar
Function Parameters
TypeIdentifierDescription
IntegersymbolIndex
Implementation
	 ' Get the indicator value for the specified symbol index.
	Define value As Number = IndicatorValue(_indicatorKeys(symbolIndex))
	 ' Get the ENVL indicator value for the specified symbol index.
	Define ENVL As Number = IndicatorValue(_ENVLKeys(symbolIndex))
	 ' Get the ENVU indicator value for the specified symbol index.
	Define ENVU As Number = IndicatorValue(_ENVUKeys(symbolIndex))
	 ' Calculate the percentage distance between the BBL and the underlying indicator.
	Define ENVLDistance As Number = 100 * (value - ENVL) / ENVL
	 ' Calculate the percentage distance between the BBU and the underlying indicator.
	Define ENVUDistance As Number = 100 * (ENVU - value) / value
	 ' If the indicator value is near the lower band.
	If (ENVLDistance > 0 And ENVLDistance < _hitDistance) 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_GTC, "Near lower envelope.")
		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_GTC, "Near lower envelope.")
			 ' 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 indicator value is near the upper band.
	ElseIf (ENVUDistance > 0 And ENVUDistance < _hitDistance) 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_GTC, "Near upper envelope.")
		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_GTC, "Near upper envelope.")
			 ' 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.