METASTOCK CODE FOR STOCHRSI TRADING SYSTEM Here is the MetaStock script I captured for the StochRSI trading system, with explanations from MetaStock's Help function (the "syntax," "function," and "example" text). I have also annotated the various sections of code with my comments in italics. --Dennis Peterson MetaStock code Fix the periods for finding the standard deviations (standarddev) and the number of periods used in RSI: standarddev:= 60; periods:= 14; LLV is the lowest low value: see below. Here is the explanation from MetaStocks's Help function: Syntax LLV( Data Array, Periods ) Function Calculates the lowest value in the Data Array over the preceding Periods (Periods includes the current day). Example The formula "LLV( Close, 14 )" returns the lowest closing price over the preceding 14 periods. HHV does a like thing for highest high value: stochrsi:=(RSI(periods)-LLV(RSI(periods),periods))/(HHV(RSI(periods),periods)-LLV(RSI(periods),periods)); Syntax round( Data Array ) Function Rounds Data Array to the nearest integer. Example The formula "round( +10.5 )" returns +11. The formula "round( -10.4 )" returns -10. Syntax stdev( Data Array, Periods ) Function Calculates the predefined Standard Deviation indicator. Example stdev( Close, 21 ) Use the rounding functions to get an integer to be used for periods: rdp1:=Round(Stdev(stochrsi,standarddev)/.053); rdp2:=Round(Stdev(stochrsi,standarddev)/.035); rdv1:=Round(Stdev(Mov(V,periods,S)/1000000,standarddev)); I need two adjustments. If the initial calculation is less than 8, then set adjust1 to 8, and if it's greater than 12, set adjust1 to 12. This is because I want Rsi to range between eight and 12 periods. Similarly for adjust2, if the initial calculation is less than 12, then set it to 12, and if greater than 20, set it equal to 20. This way the Bollinger Band periods will range between 12 and 20. adjust1:= rdv1-rdp1+11; adjust1:=if(adjust1<8,8,adjust1); adjust1:=if(adjust1>12,12,adjust1); adjust2:=rdv1-rdp2+14; adjust2:=if(adjust1<12,12,adjust2); adjust2:=if(adjust1>20,20,adjust2); ENTRY CONDITIONS periods:=adjust1; BBpds:=adjust2; Both thresholds (howclosetoBBbot and longthresholdentry) need to be a factor of either adjust1 or adjust2, but here they will be set to constants, and I will substitute in the code of what I want to use. howclosetoBBbot:=0.9; longthresholdentry:=0.3; wprice:=(2*C+H+L)/4; deviations:=.0625*BBpds+0.75; StochRSI:=(RSI(periods)-LLV(RSI(periods),periods))/(HHV(RSI(periods),periods)-LLV(RSI(periods),periods)); Syntax BBandBot(Data Array, Periods, Method, Deviations ) Function Calculates the bottom Bollinger Band of data array using method calculation method and shifted downward deviation standard deviations. Valid methods are simple, exponential, weighted, time series, triangular, and variable (these can be abbreviated as S, E, W, T, TRI, and Var). Example BBandBot(close, 10, S, 2 ) Syntax BBandtop(Data Array, Periods, Method, Deviations ) Function Calculates the top Bollinger Band of data array using method calculation method and shifted upward deviation standard deviations. Valid methods are simple, exponential, weighted, time series, triangular, and variable (these can be abbreviated as S, E, W, T, TRI, and Var). Example bbandtop( close, 10, S, 2 ) botpercentage:=Abs((wprice-BBandBot(wprice,BBpds,S,deviations))/(BBandTop(wprice,BBpds,S,deviations)-BBandBot(wprice,BBpds,S,deviations))); {entry conditions} entry1:=botpercentage-howclosetoBBbot<0.3; The constant 1.05 in the following statement may also need adjustment, but will require further testing. entry2:=C*1.05>BBandBot(wprice,BBpds,S,deviations) and StochRSI>longthresholdentry; volbb:=If(C>Ref(C,-1),V,0); I can't get MetaStock to do the right thing with this next statement. Volbb is the volume for an up day (today's close>yesterday's close). What I want for an entry condition is: if today is an up day and the volume for today is greater than the last up day, set entry3 to be true. entry3:=Volbb>Ref(volbb,-1); entry4:=(C-O)/(H-L)>.2; If all four entry conditions are true, then enter: entry1 and entry2 and entry3 and entry4 Exit conditions standarddev:= 60; periods:= 14; StochRSI:=(RSI(periods)-LLV(RSI(periods),periods))/(HHV(RSI(periods),periods)-LLV(RSI(periods),periods)); rdp1:=Round(Stdev(stochrsi,standarddev)/.053); rdp2:=Round(Stdev(stochrsi,standarddev)/.035); StochRSIvol:=(V-LLV(V,periods))/(HHV(V,periods)-LLV(V,periods)); rdv1:=Round(Stdev(Mov(V,periods,S)/1000000,standarddev)); adjust1:=rdv1-rdp2+14; adjust1:=if(adjust1<8,8,adjust1); adjust1:=if(adjust1>12,12,adjust1); adjust2:=rdv1-rdp2+14; adjust2:=if(adjust1<12,12,adjust2); adjust2:=if(adjust1>20,20,adjust2); periods:=adjust1; BBpds:=adjust2; Same comment as above - both of these thresholds need to be adjusted slightly, but until I see how the trades go, I won't know. For now, I'll just set them equal to two constants. longthresholdexit:=0.7; howclosetoBBtop:=0.8; wprice:=(2*C+H+L)/4; deviations:=.0625*BBpds+0.75; StochRSI:=(RSI(periods)-LLV(RSI(periods),periods))/(HHV(RSI(periods),periods)-LLV(RSI(periods),periods)); toppercentage:=Abs((wprice-BBandTop(wprice,BBpds,S,deviations))/(BBandTop(wprice,BBpds,S,deviations)-BBandBot(wprice,BBpds,S,deviations))); {exit conditions} exit1:=stochrsi < longthresholdexit; exit2:=toppercentage < howclosetoBBtop; exit3:=C > 0.95*BBandTop(wprice,BBpds,S,deviations); exit4:=C < BBandBot(wprice,BBpds,S,deviations); (exit1 and exit2 and exit3) or exit4 |