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:
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 );