Need help with RSI cross over

Created at 08 Nov 2016, 10:50
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AR

armstr.tradie

Joined 30.03.2016

Need help with RSI cross over
08 Nov 2016, 10:50


I'm having trouble writing up an RSI system where buy and sell signals are generated with a crossover of the 80 and 20 value lines respectively - eg. cross below 80 = sell, cross above 20 = buy. 

 

Here is my code thus far. I'm unsure how to reference the cAlgo "HasCrossedAbove" and "HasCrossedBelow" functions to make it work correctly. Is there anyone out there who can help?

protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(SourceSeries, rsiPeriod);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(cBotLabel, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(cBotLabel, Symbol, TradeType.Sell);

            // ################################### - Open & Close Data - ########################################

            double C = MarketSeries.Close.LastValue;
            double O = MarketSeries.Open.LastValue;

            double C2 = MarketSeries.Close.Last(1);
            double O2 = MarketSeries.Open.Last(1);

            double H = MarketSeries.High.LastValue;
            double L = MarketSeries.Low.LastValue;

            double H2 = MarketSeries.High.Last(1);
            double L2 = MarketSeries.Low.Last(1);

            // ################################### - Trade conditions - #########################################

            // Condition to Buy
            if ((rsi.Result.LastValue < 20) && (rsi.Result.IsRising()) && (C > O) && (longPosition == null))
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, cBotLabel, StopLoss, TakeProfit);
            }
            //Condition to Sell            
            else if ((rsi.Result.LastValue > 70) && (rsi.Result.IsFalling()) && (C < O) && (shortPosition == null))
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, cBotLabel, StopLoss, TakeProfit);
            }

            StopLossOne();
        }

Note:

rsi.Result.LastValue < 20 
rsi.Result.LastValue > 80

I wish to replace the two statements above in case the price continues to rise above or fall below these values, and avoid opening premature buy or sell positions. 


@armstr.tradie