Category Trend  Published on 08/03/2024

Multi Time-frame RSI (Algo Builder)

Description

The cTrader Relative Strength Index (RSI) is a popular momentum oscillator used in technical analysis to measure the speed and change of price movements, this open-source cBot will automatically open, close and manage your trades.

This cBot was created by the ClickAlgo Strategy Building Tool in a few minutes, it uses a friendly interface to select your indicators and trade rules and generate Microsoft C# source code to build this cBot. The strategy uses Two Relative Strength Indicators using  Hourly and Daily timeframes to open and close trades. 

* FULL SOURCE CODE INCLUDED.

 

Parameter Optimisation

We ran the optimization application of cTrader Algo to find settings which produce the backtest results below, this took 8 minutes on our computer.

1-year backtest 08.03.2023 - 07.03.2024 (1-min bar data), Net profit: 38%, Max equity drawdown: 4.92%

Usually, any Sharpe ratio greater than 1.0 is considered acceptable to good by investors. A ratio higher than 2.0 is rated as very good. A ratio of 3.0 or higher is considered excellent.

These results are optimised for the backtest data range, this means the values are data fitted for the price data. If you were to run this cBot of fresh live data, the results will be different. 

We recomend that you read our algo trading hard facts.

 

 

 

cTrader Algo Strategy Builder

You can download the Algo Strategy Builder and start creating your strategies, it can also be used to create a system that will send Telegram signals.

 

Watch Video Demo's

Watch a video demonstration of a multi-timeframe Relative Strength Index Strategy complete with source code, with no coding experience.

 

Need Modifications?

You can contact our development team if you need to add additional indicators or features to this cBot.

If you also would like us to convert your ideas or manual strategy into an automated system, we can help

 

ClickAlgo is a leading supplier of cTrader cBots & indicators.

 

DISCLAIMER

This product is for educational use only and should not be considered financial advice. Trading and Investing Activities are Inherently Risky, and Users of Our Material Should Be Prepared to Incur Losses in Connection with Such Activities.


using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

/*
   This code was created using the Algo Strategy Building Tool by ClickAlgo Limited
   https://clickalgo.com/strategy-builder
 
   This cBot does not guarantee any particular outcome or
   profit of any kind. Use it at your own risk.

   Created: 08/03/2024
   Name: RSI Hour, Daily

   This stratgey will open trades based on two RSI indicators being bullish or bearish on a 1 hour and daily timeframe.
*/

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class AlgoBuilderBot : Robot
    {
        // user parameter settings

        [Parameter("Quantity (Lots)", Group = "Protection", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop loss", Group = "Protection", DefaultValue = 86)]
        public int StopLoss { get; set; }

        [Parameter("Take profit", Group = "Protection", DefaultValue = 5)]
        public int TakeProfit { get; set; }

        // Indicator settings for bullish signals

        [Parameter("Timeframe", DefaultValue = "h1", Group = "RSI Buy #1")]
        public TimeFrame RsiTimeFrameBuy1 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  13, Group = "RSI Buy #1")]
        public int RsiPeriodsBuy1 { get; set; }
        
        [Parameter("RSI Upper", DefaultValue =  71, Group = "RSI Buy #1")]
        public int RsiUpperBuy1 { get; set; }
        
        [Parameter("RSI Lower", DefaultValue =  33, Group = "RSI Buy #1")]
        public int RsiLowerBuy1 { get; set; }

        [Parameter("Timeframe", DefaultValue = "D1", Group = "RSI Buy #2")]
        public TimeFrame RsiTimeFrameBuy2 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  9, Group = "RSI Buy #2")]
        public int RsiPeriodsBuy2 { get; set; }
        
        [Parameter("RSI Upper", DefaultValue =  72, Group = "RSI Buy #2")]
        public int RsiUpperBuy2 { get; set; }
        
        [Parameter("RSI Lower", DefaultValue =  33, Group = "RSI Buy #2")]
        public int RsiLowerBuy2 { get; set; }

        // Indicator settings for bearish signals

        [Parameter("Timeframe", DefaultValue = "h1", Group = "RSI Sell #1")]
        public TimeFrame RsiTimeFrameSell1 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  8, Group = "RSI Sell #1")]
        public int RsiPeriodsSell1 { get; set; }
        
        [Parameter("RSI Upper", DefaultValue =  65, Group = "RSI Sell #1")]
        public int RsiUpperSell1 { get; set; }
        
        [Parameter("RSI Lower", DefaultValue =  30, Group = "RSI Sell #1")]
        public int RsiLowerSell1 { get; set; }

        [Parameter("Timeframe", DefaultValue = "D1", Group = "RSI Sell #2")]
        public TimeFrame RsiTimeFrameSell2 { get; set; }
        
        [Parameter("RSI Periods", DefaultValue =  15, Group = "RSI Sell #2")]
        public int RsiPeriodsSell2 { get; set; }
        
        [Parameter("RSI Upper", DefaultValue =  65, Group = "RSI Sell #2")]
        public int RsiUpperSell2 { get; set; }
        
        [Parameter("RSI Lower", DefaultValue =  30, Group = "RSI Sell #2")]
        public int RsiLowerSell2 { get; set; }

        // declaring the standard bullish indicators

        private RelativeStrengthIndex _rsi_Buy1;
        private RelativeStrengthIndex _rsi_Buy2;

        // declaring the standard bearish indicators

        private RelativeStrengthIndex _rsi_Sell1;
        private RelativeStrengthIndex _rsi_Sell2;

        // declaring the market data variables for bullish signals.

        private Bars _rsiBars_Buy1;
        private Bars _rsiBars_Buy2;

        // declaring the market data variables for bearish signals.

        private Bars _rsiBars_Sell1;
        private Bars _rsiBars_Sell2;

        // Private fields.

        private string StrategyName { get; set; }

        // makes sure only 1 trade opens per signal
        bool IsBullish { get; set; }
        bool IsBearish { get; set; }    

        // Initialise variables when the cBot starts.

        protected override void OnStart()
        {
            StrategyName = "RSI Hour, Daily";

            // constructing the market data for the bullish indicators
            _rsiBars_Buy1 = MarketData.GetBars(RsiTimeFrameBuy1);
            _rsiBars_Buy2 = MarketData.GetBars(RsiTimeFrameBuy2);

            // constructing the market data for the bearish indicators
            _rsiBars_Sell1 = MarketData.GetBars(RsiTimeFrameSell1);
            _rsiBars_Sell2 = MarketData.GetBars(RsiTimeFrameSell2);

            // constructing the BULLISH indicators
            _rsi_Buy1 = Indicators.RelativeStrengthIndex(_rsiBars_Buy1.ClosePrices, RsiPeriodsBuy1);
            _rsi_Buy2 = Indicators.RelativeStrengthIndex(_rsiBars_Buy2.ClosePrices, RsiPeriodsBuy2);

            // constructing the BEARISH indicators
            _rsi_Sell1 = Indicators.RelativeStrengthIndex(_rsiBars_Sell1.ClosePrices, RsiPeriodsSell1);
            _rsi_Sell2 = Indicators.RelativeStrengthIndex(_rsiBars_Sell2.ClosePrices, RsiPeriodsSell2);
        }

        // The onTick method is called on each price change.
        protected override void OnTick()
        {
          
        }

         // this method is called when the candle closes and is used to check the trade signals, execute trades and send alerts.
        protected override void OnBarClosed()
        {
            TradeRulesBullish();            
            TradeRulesBearish();
        }

        // parent method to check rules and open bullish trade or send signal.
        private void TradeRulesBullish()
        {
            // flag to open a trade if all rules true.
            bool OpenBuyTrade = false;

            if (IsBullishSignal())
            {
                OpenBuyTrade = true;
            }
 
            if (OpenBuyTrade)
            {
                if (!IsBullish)
                {
                    if (!IsTradeOpen(TradeType.Buy))
                    {
                        OpenTrade(TradeType.Buy);
                    }
                }

                IsBullish = true;
            }
            else
            {
                IsBullish = false;
            }
        }


        // parent method to check rules and open bearish trade or send signal.
        private void TradeRulesBearish()
        {
            // flag to open a trade if all rules true.
            bool OpenSellTrade = false;

           if (IsBearishSignal())
            {
                OpenSellTrade = true;
            }

            if (OpenSellTrade)
            {
                if (!IsBearish)
                {
                    if (!IsTradeOpen(TradeType.Sell))
                    {
                        OpenTrade(TradeType.Sell);
                    }
                }

                IsBearish = true;
            }
            else
            {
                IsBearish = false;
            }
        }

        // returns true if all signals are bullish
        private bool IsBullishSignal() 
        {
            if (_rsi_Buy1.Result.LastValue > RsiLowerBuy1)
            {
                return false;
            }

            if (_rsi_Buy2.Result.LastValue > RsiLowerBuy2)
            {
                return false;
            }

            return true;
        }

        // returns true if all signals are bearish
        private bool IsBearishSignal() 
        {
            if (_rsi_Sell1.Result.LastValue < RsiUpperSell1)
            {
                return false;
            }

            if (_rsi_Sell2.Result.LastValue < RsiUpperSell2)
            {
                return false;
            }

            return true;
        }

        private void OpenTrade(TradeType type)
        {
            // calculate volume from lots
            var volume = Symbol.QuantityToVolumeInUnits(Quantity);
            volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);

            ExecuteMarketOrder(type, SymbolName, volume, StrategyName, StopLoss, TakeProfit);
        }

        // returns true if buy trade is open.
        private bool IsTradeOpen(TradeType type)
        {
            var positions = Positions.FindAll(StrategyName, SymbolName, type);
            if (positions.Count() > 0)
            {
                return true;
            }
            else return false;
        }
    }
}

ClickAlgo's avatar
ClickAlgo

Joined on 05.02.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Multi Timeframe RSI.algo
  • Rating: 0
  • Installs: 305
Comments
Log in to add a comment.
No comments found.