Tuesday, September 07, 2010

Stochastic RSI Overbought / Oversold [SRSIOO]

Descripton
Summary:
This trading system script uses a Stochastic RSI indicator to generate trading signals whenever the underlying symbol is overbought or oversold.

Rules:
1. Buy Market - When the Stochastic RSI shows the symbol is oversold.
2. Sell Market - When the Stochastic RSI shows the symbol is overbought.
3. Sell Short Market - When the Stochastic RSI shows the symbol is oversold.
4. Buy To Cover Market - When the Stochastic RSI shows the symbol is overbought.


Variables
TypeIdentifierDescription
IntegerArray_SRSIKeysUse for holding all of the SRSI indicator keys indexed by symbol index.
Number_overboughtValueUse for the minimum value above which the underlying indicator is considered overbought.
Number_oversoldValueUse for the maximum value below which the underlying indicator is considered oversold.
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 underlying indicator key on which to calculate the Stochastic RSI indicator.
IntegerperiodsUse for the number of periods to include in the Stochastic RSI indicator. [Default 10]
IntegerfirstPeriodsUse for the number of periods to include in the calculation of the first value of the exponential moving average. [Default 0]
NumberoverboughtValueUse for the minimum value for the Stochastic RSI above which the underlying indicator is considered overbought. [Default 80]
NumberoversoldValueUse for the maximum value for the Stochastic RSI below which the underlying indicator is considered oversold. [Default 20]
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.
	_overboughtValue = overboughtValue
	_oversoldValue = oversoldValue
	_enableShorting = enableShorting
	_stopLoss = stopLoss
	_takeProfit = takeProfit
	_trailingStop = trailingStop
	_isPercent = isPercent
	
	 ' Use for holding indicator keys based on the specified indicator key, indexed by symbol index.
	Redefine _SRSIKeys(SymbolCount() - 1)
	 ' Iterate over all of the account symbols.
	For i As Integer = 0 To SymbolCount() - 1
		 ' Create an SRSI indicator for each account symbol, based on the specified indicator.
		_SRSIKeys(i) = IndicatorSRSI(IndicatorCopy(indicatorKey, i),periods, firstPeriods)
		 ' Add the indicator to the symbol chart.
		ChartIndicator(i, 0, C_MERGE_LEFT, _SRSIKeys(i),C_LINE, 1,0)
	Next

OnSymbolBar
Function Parameters
TypeIdentifierDescription
IntegersymbolIndex
Implementation
	 ' Get the indicator value for the specified symbol index.
	Define value As Number = IndicatorValue(_SRSIKeys(symbolIndex))
	
	 ' If the indicator value crosses over zero, generate a buy order.
	If (value < _oversoldValue) 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 order.
			BrokerMarket(C_BUY_TO_COVER,symbolIndex, 0, C_DAY, "Oversold.")
		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, "Oversold.")
			 ' 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 crosses under zero, generate a sell order.
	ElseIf (value > _overboughtValue) 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, "Overbought.")
		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, "Overbought.")
			 ' 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.