How to count RSI touching

progy85 since: 07 Jan 2021;

30 Sep 2021, 14:31
How to count RSI touching

Hello,

This is sample. Instead of using buy or sell, I need to calculate Print when touch 80 and 20 always when touch line for specific period. Example:
RSI TOUCH 80: 5 times
RSI TOUCH 20: 15 times

How to do this?

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample RSI cBot" will create a buy order when the Relative Strength Index indicator crosses the  level 30,
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level
//    and sell orders are closed when RSI crosses the 30 level).
//
//    The cBot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleRSIcBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }

[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }

[Parameter("Periods", Group = "RSI", DefaultValue = 6)]
public int Periods { get; set; }

private RelativeStrengthIndex rsi;

protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}

protected override void OnTick()
{
if (rsi.Result.LastValue < 20)
{
}
else if (rsi.Result.LastValue > 80)
{
}
}

{
foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
ClosePosition(position);
}

{
var position = Positions.Find("SampleRSI", SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", 45, 60);
}
}
}

amusleh since: 01 Mar 2021;

01 Oct 2021, 09:51

Hi,

You can use this method to get number of RSI touches:

``````        /// <summary>
/// Returns the number of RSI level touches on a specific period
/// </summary>
/// <param name="periods">The past number of bars</param>
/// <param name="level">The RSI level</param>
/// <returns>Number of Touches</returns>
private int GetRsiTouches(int periods, double level)
{
var index = Bars.Count - 1;

var touchCount = 0;

for (int barIndex = index; barIndex > index - periods; barIndex--)
{
if ((level > 0 && rsi.Result[barIndex] >= level && rsi.Result[barIndex - 1] < level) || (level < 0 && rsi.Result[barIndex] <= level && rsi.Result[barIndex - 1] > level))
{
touchCount++;
}
}