Back to TS code links
STOCHASTIC RSI STRATEGY based on John Ehlers indicators
Back to Ehlers links


Anyone using information or codes on these pages does so at their own risk - no guarantees of stability or profitability are claimed. These codes are all for testing purposes only.

Note: It has been brought to my attention that there is an undeclared variable "data2" in one or more modules of this code. I no longer have access to my TS account or codes for varification thus I cannot compile and verify it, so please be aware of this error.

//// STOCHASTIC RSI STRATEGY ////
//// Set properties for $30 slippage & $3.15 commission per contract
//// This is only in test mode - short trade not written yet but coming
//// Takes limited number of trades but results are fairly successful.

//// EXIT CONTROLS ///////////////////////////////////////////
var: Y(true), N(False);
Var{Sub for inputs}: MultiX (y), {Multi-exits -turn on here or in "MultiExits" & adjust at bottom -switch - T=ON, F=OFF}
HdStop (n), {Turn on Hard Stop & Hard Target here - settings (bottom) -switch - T=ON, F=OFF}
StoRSIx (n); {turn on RVI crossover exits and stops}

////////////////////////// SHORT - LONG TRADE CONTROL //////////////////////
Var{Sub for inputs}: Tradetype (0); {1 For Long - 2 for short - 3 for both---->} Tradetype =3

/////////////////////////////// STOCHASTIC DELIMITER sets filters or minimum levels for overbought/oversold
Var {Inputs}: Delimiter(80), MA(0), MAlength(1{144}), TradeSessionStart(0000), TradeSessionEnd(2400);
///////////////// STOCHASTIC RSI CODE BEGIN //////////////////
Var{Input}: RSILength(8), StocLength(8), WMALength(8), StoRSI1(0), StoRSI2(0),StoRSI3(0),StoRSI4(0),StoRSI5(0);
MA=(Average(C, MAlength));
StoRSI1=RSI (Close, RSILength) - Lowest(RSI(Close, RSILength), StocLength);
StoRSI2 = Highest(RSI(Close, RSILength), StocLength) - Lowest(RSI(Close, RSILength), StocLength);
If StoRSI2<> 0 then StoRSI3 = StoRSI1/StoRSI2;
StoRSI4 = 2*(WAverage(StoRSI3, WMALength) - .5);
StoRSI5 = StoRSI4[1]; //define trigger
//Plot1(StoRSI4, "StocRSI",blue); Plot2(StoRSI5, "Trig",green); Plot3(0,"Zero",yellow); Plot4(.6,"",black);Plot5(-.6,"",black);

////////////////////////////////////////////////////////////////// STOCHASTIC RSI---END Use code above for plotting if desired
//////////////////////////////////////////////////////////////////TRADE TRIGGERS

If 1=1 //Stochastic RSI Long Entry
and (tradetype = 1 or tradetype = 3)
and MarketPosition(0)=0
and StoRSI5 crosses over StoRSI4
and MA< MA[1]
and StoRSI4 < (-delimiter*.01)
and close data2 > average (close,4) data2
//and average (close,21)data2 > average (close,21)[1] data2
////and average (close,34)> average (close,34)[5]
////and close< average(close,3)
////and RangeLeader[1]=1
and Time >TradeSessionStart
and Time <TradeSessionEnd
then buy ("StoRVIlong") next bar at market;

If 1=1 //Stochastic RSI Long Exit
and StoRSIx=true
and MarketPosition(0)=1
and StoRSI5 crosses under StoRSI4
and StoRSI4> delimiter*.01
and average (close,34)< average (close,34)[5]
//and close> average(close,21)
//and average (close,21)data2 < average (close,21)[5] data2
then sell ("Longexit") next bar at market;

/////StoRSI Stop - Input true to turn on, false to turn off
Var: StoRSIStop (HdStop), StoRSILongStopPrice(.0030){.0035},StoRSIShortStopPrice(.0025){.0035}; //hard stop

If 1=1 //long stop
and StoRSIx = true //test for on or off
and MarketPosition(0)=1
then begin
SetStopContract;
Sell Next Bar at EntryPrice - StoRSILongStopPrice stop; //stoploss trigger
SetProfitTarget (.035);
end;

If 1=1 //Stochastic RSI short entry
and (tradetype = 2 or tradetype = 3)
and MarketPosition(0)=0
and StoRSI5 crosses under StoRSI4
and MA> MA[1]
and StoRSI4 > (delimiter*.01)
and close data2 < average (close,4) data2
//and average (close,21)data2 < average (close,21)[1] data2
////and average (close,34)< average (close,34)[5]
////and close> average(close,3)
////and RangeLeader[1]=1
and Time >TradeSessionStart
and Time <TradeSessionEnd
then sellshort ("StoRVIshort") next bar market;

If 1=1 //Stochastic RSI Short Exit
and StoRSIx=true //test for on or off
and MarketPosition(0)=-1
and StoRSI5 crosses over StoRSI4
and StoRSI4< -delimiter*.01
and average (close,34)> average (close,34)[5]
//and close< average(close,21)
//and average (close,21)data2 > average (close,21)[5] data2
then buytocover ("Shortexit") next bar at market;

If 1=1 //StoRSI short Stop
and StoRSIx = true //test for on or off
and MarketPosition(0)=-1
then begin
SetStopContract;
BuytoCover Next Bar at EntryPrice + StoRSIShortStopPrice stop; //stoploss trigger
SetProfitTarget (.025);
end;

///////////////////////////
////////// EXITS //////////
///////////////////////////
//These exits are controlled with MultiExits below or at "MultiX" top of page. Set to "false" in BOTH places to turn off.
{Exit strategy group includes stops and targets. It's set for divergence from the entry price instead of a specific price point.
There is a stop, a profit target, a breakeven stop, 2 different trailing stops, and an end-of-day exit.
This will exit your entire position, either long or short.
IntrabarOrderGeneration is set to false because these commands evaluate intrabar even when intrabar order generation is disabled.}
[IntrabarOrderGeneration = false]
var{Inputs}:{these are exit inputs: Suggested Starting Points (SSPs) are for EUR.USD and similar Forex instruments 15-30 minute bars}
MultiExits(false),
ShareOrPosition( 1 ), //1 = per share basis, 2 = position basis
ProfitTargetAmt( .01), //SSP .01 or 0 if you don't want a specific profit target
StopLossAmt( .001 ), // SSP .001 or 0 if you don't want a stop loss
BreakevenFloorAmt( 0 ), //SSP .004 or 0 if you don't want a breakeven stop
DollarTrailingAmt( .004 ), //SSP .004 or 0 if you don't want a dollar trailing stop
PctTrailingFloorAmt( 0 ), //SSP .002 or 0 here and/or in next input if you don't want a percent trailing stop or XX for XX percent
PctTrailingPct( 0 ); // SSP .01. Enter 0 here and/or in previous input if you don't want percent trailing stop or XX for XX percent
{ ExitOnClose( false ); Turned off, not needed - Set to TRUE only for back-testing, if at all; in automated execution, the exit order
will NOT be filled at the close of the day; instead, the order can be sent into the extended session as a limit order. }

if MultiExits=true or MultiX=true then begin
if ShareOrPosition = 1 then SetStopShare else SetStopPosition;
if ProfitTargetAmt > 0 then SetProfitTarget( ProfitTargetAmt );
if StopLossAmt > 0 then SetStopLoss( StopLossAmt );
if BreakevenFloorAmt > 0 then SetBreakeven( BreakevenFloorAmt );
if DollarTrailingAmt > 0 then SetDollarTrailing( DollarTrailingAmt );
if PctTrailingFloorAmt > 0 and PctTrailingPct > 0 then SetPercentTrailing( PctTrailingFloorAmt, PctTrailingPct );
//if ExitOnClose = true then SetExitOnClose;
end;

//////////////////////////////////
////// HARD STOP & TARGET ////////
//////////////////////////////////
Var: HardStop (HdStop), HardStopPrice(.0005){.0009}; //true to turn on, false to turn off
If HardStop = true then begin
SetStopContract;
Sell Next Bar at EntryPrice - HardStopPrice stop;
SetProfitTarget (.08);
end;

{Rangeleader tests if the mid-point of the current bar is > previous High or < previous Low
and if the Range of the current bar is > Range of the previous bar. Both true = 1; One or both false = 0}

//Daily cost of carry = (qty of units traded X interest rate differential) / 360