RSI indicator, Multiple timeframes, incorrect calculations
RSI indicator, Multiple timeframes, incorrect calculations
17 Aug 2023, 04:19
Good day, I use the following bot, on EST+2, and if I back test EURGBP on 3 July 2023 to 3 July 2023 on a 15 minute timeframe. On 7:15 for example the readings for LastValue & Last(1) is the same. (There is actually much more times when they are the same). Why is this the same?
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("RSI Periods", Group = "RSI", DefaultValue = 14)]
public int RsiPeriods { get; set; }
private RelativeStrengthIndex rsi1Hour;
private RelativeStrengthIndex rsi15Min;
protected override void OnStart()
{
Bars bars15m = MarketData.GetBars(TimeFrame.Minute15);
Bars bars1h = MarketData.GetBars(TimeFrame.Hour);
rsi1Hour = Indicators.RelativeStrengthIndex(bars1h.ClosePrices, RsiPeriods);
rsi15Min = Indicators.RelativeStrengthIndex(bars15m.ClosePrices, RsiPeriods);
}
protected override void OnBar()
{
Print(" rsi15Min.Result.LastValue ", rsi15Min.Result.LastValue);
Print(" rsi15Min.Result.Last(1) ", rsi15Min.Result.Last(1));
}
protected override void OnTick()
{
if (rsi1Hour.Result.LastValue < 30 && rsi15Min.Result.LastValue < 30)
{
Close(TradeType.Sell);
Open(TradeType.Buy);
}
else if (rsi1Hour.Result.LastValue > 70 && rsi15Min.Result.LastValue > 70)
{
Close(TradeType.Buy);
Open(TradeType.Sell);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
ClosePosition(position);
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("SampleRSI", SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI");
}
}
}
PanagiotisChar
17 Aug 2023, 04:53
Hi there,
Because OnBar is executed on bar opening. Therefore at that moment the close bar of the current bar and the close bar of the previous bar are the same. So it is possible for the two values to be the same
Aieden Technologies
Need help? Join us on Telegram
@PanagiotisChar