Help With RSI & Stochastics Programming

shisha1999

Newbie
Messages
4
Likes
0
Hello,

I am trying to write a strategy based on the Stochastics and RSI technicial indicators that enters the market when both have reached the overbought or oversold levels. I would like to write the code such that the following conditions must be achieved in order for entry:

In the case of a long entry, the RSI indicator must cross above the oversold level FIRST. When this has been reached, wait for a confirmation from the Stochastics to cross above the oversold level SECOND. When this occurs, enter at the market. Conversely the same for a short position where the RSI must cross below the overbought level FIRST followed by waiting for a confirmation from the Stochastics to cross below the overbought level to enter the market.

I tried to code this myself but the way it is worded only works when both indicators cross the overbought or oversold levels at the same time. I tried to use condition statements but that didnt change anything. I was wondering if a loop was needed that was worded to the extent that it told the code to not do anything and wait till a confirmation from the stochastics was reached. I am new at this, so any help is appreciated. Below is the code that I am working with:

Code:
inputs: Price (close),Length( 14 ), Length1(14), OverSold( 20 ), OverBought( 80 ), OverSold1( 30 ), OverBought1( 70 ),Amount( 5 ), Amount1 ( 5 ), PositionBasis( false ); 
variables: oFastK( 0 ), oFastD( 0 ), oSlowK( 0 ), oSlowD( 0 ), MyRSI(0) ; 
 
Value1 = Stochastic( H, L, C, Length, 3, 3, 1, oFastK, oFastD, oSlowK, oSlowD ) ; 
MyRSI = RSI (Price, Length1); 
if (time > 0700 and time < 0830) or (time > 1300 and time < 1400) then begin 
 
{Condition1 = currentbar > 2 and MyRSI crosses over OverSold1;  
Condition2 = currentbar > 2 and oSlowK crosses over OverSold;} 
	 
{If Condition2 => Condition1 then} 
if MyRSI crosses over OverSold1 and oSlowK crosses over OverSold then 
	Buy ( "StochLE" ) next bar at market; 
 
if MyRSI crosses under OverBought1 and oSlowK crosses under OverBought then 
	Sell Short ( "StochSE" ) next bar at market ; 
end; 
 
if PositionBasis then 
	SetStopPosition 
else 
	SetStopShare ; 
 
if MarketPosition = 1 then 
	SetProfitTarget( Amount ) ; 
if MarketPosition = 1 then 
	SetStopLoss( Amount1 ); 
if MarketPosition = -1 then 
	SetProfitTarget( Amount ); 
if MarketPosition = -1 then 
	SetStopLoss( Amount1 );
 
Hello,

I am trying to write a strategy based on the Stochastics and RSI technicial indicators that enters the market when both have reached the overbought or oversold levels. I would like to write the code such that the following conditions must be achieved in order for entry:

In the case of a long entry, the RSI indicator must cross above the oversold level FIRST. When this has been reached, wait for a confirmation from the Stochastics to cross above the oversold level SECOND. When this occurs, enter at the market. Conversely the same for a short position where the RSI must cross below the overbought level FIRST followed by waiting for a confirmation from the Stochastics to cross below the overbought level to enter the market.

I tried to code this myself but the way it is worded only works when both indicators cross the overbought or oversold levels at the same time. I tried to use condition statements but that didnt change anything. I was wondering if a loop was needed that was worded to the extent that it told the code to not do anything and wait till a confirmation from the stochastics was reached. I am new at this, so any help is appreciated. Below is the code that I am working with:

Code:
inputs: Price (close),Length( 14 ), Length1(14), OverSold( 20 ), OverBought( 80 ), OverSold1( 30 ), OverBought1( 70 ),Amount( 5 ), Amount1 ( 5 ), PositionBasis( false ); 
variables: oFastK( 0 ), oFastD( 0 ), oSlowK( 0 ), oSlowD( 0 ), MyRSI(0) ; 
 
Value1 = Stochastic( H, L, C, Length, 3, 3, 1, oFastK, oFastD, oSlowK, oSlowD ) ; 
MyRSI = RSI (Price, Length1); 
if (time > 0700 and time < 0830) or (time > 1300 and time < 1400) then begin 
 
{Condition1 = currentbar > 2 and MyRSI crosses over OverSold1;  
Condition2 = currentbar > 2 and oSlowK crosses over OverSold;} 
	 
{If Condition2 => Condition1 then} 
if MyRSI crosses over OverSold1 and oSlowK crosses over OverSold then 
	Buy ( "StochLE" ) next bar at market; 
 
if MyRSI crosses under OverBought1 and oSlowK crosses under OverBought then 
	Sell Short ( "StochSE" ) next bar at market ; 
end; 
 
if PositionBasis then 
	SetStopPosition 
else 
	SetStopShare ; 
 
if MarketPosition = 1 then 
	SetProfitTarget( Amount ) ; 
if MarketPosition = 1 then 
	SetStopLoss( Amount1 ); 
if MarketPosition = -1 then 
	SetProfitTarget( Amount ); 
if MarketPosition = -1 then 
	SetStopLoss( Amount1 );

One way to achieve this is to use a variable as a counter. Start it with value = 0.

When RSI crosses oversold then add 1 to the variable . Then have a condition saying that when Stoch crosses oversold AND variable > 0 and Market position = 0 then BUY. Also at this point reset the variable to zero (unless you only want to do this when you have exited the trade).

I would also advise that you print out the values of any variables and calculations you use to the message log together with the timestamp. You can then run the strategy backtest and look at the log to see whether the buy/sell signals and the values of the variables i.e. the logic is working correctly

Charlton
 
Thanks Charlton for replying.

I programmed your recommendation but I am not getting any trades generated. Any insight into what I am doing incorrectly or missing? It seems like I need to tell the program to move onto the next bar until the condition appears or even set a limit of move on to the next 5 bars until condition appears, otherwise start all over again.

Code:
inputs: Price (close),Length( 14 ), Length1(14), OverSold( 20 ), OverBought( 80 ), OverSold1( 30 ), OverBought1( 70 ), Amount( 5 ), Amount1( 5 ), PositionBasis( false );
variables: oFastK( 0 ), oFastD( 0 ), oSlowK( 0 ), oSlowD( 0 ), MyRSI( 0 ), counter ( 0 ) ;

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, oFastK, oFastD, oSlowK, oSlowD ) ;
MyRSI = RSI (Price, Length1);
{if (time > 0700 and time < 0830) or (time > 1300 and time < 1400) then begin}

Condition1 = currentbar > 1 and MyRSI crosses over OverSold1; 
Condition2 = currentbar > 1 and oSlowK crosses over OverSold;
Condition3 = currentbar > 1 and MyRSI crosses under OverBought1; 
Condition4 = currentbar > 1 and oSlowK crosses under OverBought;
Condition5 = counter > 0;
Condition6 = MarketPosition = 0;

IF Condition1 then
	counter = counter + 1;	
	If Condition2 and Condition5 and Condition6 then
	Buy ( "StochLE" ) next bar at market;
	counter = 0;
	

if PositionBasis then
	SetStopPosition
else
	SetStopShare ;

if MarketPosition = 1 then
	SetProfitTarget( Amount ) ;
if MarketPosition = 1 then
	SetStopLoss( Amount );
if MarketPosition = -1 then
	SetProfitTarget( Amount1 );
if MarketPosition = -1 then
	SetStopLoss( Amount1 );
 
You will probably need to do further work on this, but I have pasted some code below that seems to work and, at least, demonstrates the principles.

The print statements embedded in the document can be commented out later, but are useful for debugging. I have attached a copy of the message log from running this from 01-jan. Use view>easy language output bar making sure you right click in the message area and choose clear before you run a strategy.

The attached screen print shows how at 13:50 on 23rd June the yellow RSI line crossed above the oversold 20 level within the time of day required. I have added a vertical line to show it. Look at the message log to see the value of rsi_count before and after this event.

Eventually on 24th June at 13:30 the blue slowK line crosses above oversold whilst rsi_count is above 0 and within the required time of day, so a buy signal occurs. Again look at the message log to see the values before and after this event.

Charlton





inputs: Price (close),Length( 14 ), Length1(14), OverSold( 20 ), OverBought( 80 ), OverSold1( 20 ), OverBought1( 80 ),Amount( 5 ), Amount1 ( 5 ), PositionBasis( false );
variables: oFastK( 0 ), oFastD( 0 ), oSlowK( 0 ), oSlowD( 0 ), MyRSI(0), rsi_count(0), rsi2_count(0) ;

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, oFastK, oFastD, oSlowK, oSlowD ) ;
MyRSI = RSI (Price, Length1);
if (time > 0700 and time < 0830) or (time > 1300 and time < 1400) then begin

{Condition1 = currentbar > 2 and MyRSI crosses over OverSold1;
Condition2 = currentbar > 2 and oSlowK crosses over OverSold;}

{If Condition2 => Condition1 then}

print(eldatetostring(date), " ", time, " rsi_count ", rsi_count, " rsi2_count ", rsi2_count);

if MyRSI crosses over OverSold1 then rsi_count = rsi_count + 1;



if rsi_count > 0 and oSlowK crosses over OverSold then begin
Buy ( "StochLE" ) next bar at market;
print("StochLE ",eldatetostring(date), " ", time, " rsi_count ", rsi_count, " oSlowK ", oSlowK);
rsi_count = 0;
end;

if MyRSI crosses under Overbought1 then rsi2_count = rsi2_count + 1;

if rsi2_count > 0 and oSlowK crosses under OverBought then begin
Sell Short ( "StochSE" ) next bar at market;
print("StochSE ",eldatetostring(date), " ", time, " rsi2_count ", rsi2_count, " oSlowK ", oSlowK);
rsi2_count = 0;
end;

if PositionBasis then
SetStopPosition
else
SetStopShare ;

if MarketPosition = 1 then
SetProfitTarget( Amount ) ;
if MarketPosition = 1 then
SetStopLoss( Amount1 );
if MarketPosition = -1 then
SetProfitTarget( Amount );
if MarketPosition = -1 then
SetStopLoss( Amount1 );

end;
 

Attachments

  • buy 1.png
    buy 1.png
    35.7 KB · Views: 466
  • buy print log.txt
    126.9 KB · Views: 386
Thanks Charlton. Your modification to the code worked on my end. I also verified it on the message log. I am a little confused on how the logic works but nonetheless it is producing the results I desire.
 
Thanks Charlton. Your modification to the code worked on my end. I also verified it on the message log. I am a little confused on how the logic works but nonetheless it is producing the results I desire.
Shisha

Let's turn the logic into plain English and remember that most of the logic i.e. code is yours. I have only added the bits that only action the signal when the conditions coincide on the same bar.

The first IF statement says only create a buy or sell if the time is between 07:00 and 8:30 or between 13:00 and 14:00. This is followed by the word BEGIN, so you know that there will be a series of actions that will take place under this condiion. This condition ends at the very last END word in your code. In other words all buys/sells will only occur between those hours.

Now let's look at your BUY condition:

if rsi_count > 0 and oSlowK crosses over OverSold then begin
Buy ( "StochLE" ) next bar at market;

This is only buying when the SlowK line crosses above the Oversold level AND the counter rsi_count is set to at least 1. This counter starts with a default value of 0. Each time the RSI line crosses the oversold line 1 is added to this counter (if MyRSI crosses over OverSold1 then rsi_count = rsi_count + 1;). It might cross many times before a buy so if it crosses 10 times before SlowK crosses Oversold it would have reached a value of 10.

When eventually SlowK crosses over oversold and the counter is abover zero then the buy takes place. In the same Immediately AFTER the buy action has taken place the line
"rsi_count = 0;" sets the counter back to zero. If it did not do this then you would get further BUYs taking place every time the SlowK line re-crosses over Oversold, which I assume you don't want. I assume that you want a fresh crossing of the RSI line to take place before another BUY. If that assumption is not correct then you would have to change that part of the code. Notice that the re-setting of the counter back to zero takes place within the same IF statement (IF...BEGIN....END as the BUY condition, so the reset only takes place when a BUY occurs.

The same logic applies to the SELL short condition. Here we have a second counter
rsi2_count that starts at zero and has 1 added to it every time the RSI line crosses below the oversold line. This might happen many times before the rest of the SELL SHORT condition occurs, so the counter could end up well above zero. Immediately AFTER SlowK crosses under the oversold line the counter is re-set back to zero, so no further shorts can take place until RSI crosses below oversold again.

The print lines just print what you want to see in the message log during testing and can be commented out when you no longer want them. The first one is positioned outside the BUY and SELL conditions, so it tells you at each bar what the counters are. The other two are positioned within the BUY or SELL condition, so only get printed if these actions take place. They are positioned prior to the resetting of the counter, so you see the results at the time the BUY or SELL action takes place.

The rest of the code on profit targets, stops etc is all your original code that I have not altered in any way.

The key with reading anything like this is to work out where each condition begins and ends. You could print the code out and use coloured highlighters to mark this. Some conditions are simple single ones (like the RED arrowed ones in the attached). In this case they all fall within the red overarching condition. Other conditions contain several actions inside them like the Green and red highlighted ones.

I hope this helps to make sense of the logic/code

Charlton
 

Attachments

  • highlighting code.png
    highlighting code.png
    108.6 KB · Views: 479
Charlton,

Thanks for the explanation. I should have been clearer, I understood the part of the code I wrote and was trying to understand your suggestion of using the "rsi_count + 1" logic. Thanks anyways for the explanation.

I have spent a considerable amount of time working with the code and backtesting and I noticed there is one little bug in the code. I noticed that I was getting buy (sell) signals when the indicators didnt show it. I also noticed that this only happened within 3 - 5 minutes of the start time of 0700. It appears that on certain times, the rsi count before 0700 can be greater than 0 resulting in a buy condition immediately that the stoch crosses after 0700. I verified this by changing the start time of 0700 to different valus and was getting the same results, entries with 3 -5 minutes of the start time. So I wrote in code that resets the rsi count to 0 before 0700. I dont know if this is the best way to write it but so far appears to get rid of the incorrect entries. I will continue testing to see if there are any other strange bugs.

if time < 0700 then
rsi_count = 0;
rsi2_count = 0;

Al
 
Top