Differents values from RSI indicators in two different robots

Created at 28 Nov 2021, 01:08
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!
AS

astevani

Joined 11.03.2019

Differents values from RSI indicators in two different robots
28 Nov 2021, 01:08


I have a robot the write same data to file that i use for AI training, and i have a different robot that use the same indicators to comunicate with a WebService to get a prediction from AI.

Many data are the same like open, close, high, low, MovingAvarage and many others but RSI and BollingerBands are not the same, some decimal numbers are different...

For example:

 

this are RSI from the 2 differents robot and the difference

RSI one      RSI two     difference

34,346564    34,309287    0,037277
36,156472    36,123631    0,032841
35,216511    35,18139    0,035121
35,168786    35,133548    0,035238
36,524863    36,492901    0,031962
34,721973    34,685809    0,036164
34,988265    34,952583    0,035682
34,529831    34,493502    0,036329
34,125175    34,088511    0,036664
33,267478    33,230163    0,037315
36,006483    35,973282    0,033201
34,637252    34,601015    0,036237
33,218823    33,181474    0,037349
33,743925    33,706961    0,036964
33,941592    33,904782    0,03681
36,193877    36,161365    0,032512
36,617144    36,585633    0,031511
39,444082    39,419023    0,025059
37,678542    37,651624    0,026918
39,376986    39,351852    0,025134
 

this is the first robot

 

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.IO;
using System.Text.RegularExpressions;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet | AccessRights.FileSystem)]
    public class SaveData2 : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("TimeFrame1")]
        public TimeFrame tFrame { get; set; }

        [Parameter("TimeFrame2")]
        public TimeFrame tFrame2 { get; set; }

        [Parameter("TimeFrame3")]
        public TimeFrame tFrame3 { get; set; }

        private StreamWriter output;
        MovingAverage S_Slow;
        MovingAverage S_Slow2;
        MovingAverage S_Fast;
        RelativeStrengthIndex S_RSI;
        RelativeStrengthIndex S_RSI2;
        MovingAverage S2_Slow;
        MovingAverage S2_Slow2;
        MovingAverage S2_Fast;
        RelativeStrengthIndex S2_RSI;
        RelativeStrengthIndex S2_RSI2;
        MovingAverage S3_Slow;
        MovingAverage S3_Slow2;
        MovingAverage S3_Fast;
        RelativeStrengthIndex S3_RSI;
        RelativeStrengthIndex S3_RSI2;
        BollingerBands S_BAND;
        BollingerBands S2_BAND;
        BollingerBands S3_BAND;
        StochasticOscillator S_STO;
        StochasticOscillator S2_STO;
        StochasticOscillator S3_STO;
        AverageDirectionalMovementIndexRating S_ADX;
        AverageDirectionalMovementIndexRating S2_ADX;
        protected override void OnStart()
        {

            S_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 15, MovingAverageType.Simple);
            S_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 10, MovingAverageType.Simple);
            S_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 5, MovingAverageType.Simple);
            S_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame).ClosePrices, 14);
            S_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame).ClosePrices, 7);


            S2_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 15, MovingAverageType.Simple);
            S2_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 10, MovingAverageType.Simple);
            S2_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 5, MovingAverageType.Simple);
            S2_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame2).ClosePrices, 14);
            S2_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame2).ClosePrices, 7);

            S3_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 15, MovingAverageType.Simple);
            S3_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 10, MovingAverageType.Simple);
            S3_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 5, MovingAverageType.Simple);
            S3_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame3).ClosePrices, 14);
            S3_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame3).ClosePrices, 7);
            /*
            S_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame).ClosePrices, 20, 2, MovingAverageType.Simple);
            S2_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame2).ClosePrices, 20, 2, MovingAverageType.Simple);
            S3_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame3).ClosePrices, 20, 2, MovingAverageType.Simple);
            */
            S_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame).ClosePrices, 12, 2, MovingAverageType.Simple);
            S2_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame2).ClosePrices, 12, 2, MovingAverageType.Simple);
            S3_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame3).ClosePrices, 12, 2, MovingAverageType.Simple);

            S_ADX = Indicators.AverageDirectionalMovementIndexRating(20);
            S2_ADX = Indicators.AverageDirectionalMovementIndexRating(9);
            S_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame), 9, 3, 9, MovingAverageType.Simple);
            S2_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame2), 9, 3, 9, MovingAverageType.Simple);
            S3_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame3), 9, 3, 9, MovingAverageType.Simple);
            output = new StreamWriter("c:\\marketdata\\" + Symbol.Name + "-m60-360-720.txt");
            //"-m60-360-720.txt");
            string head = "date\tn_open\tn_close\tn_low\tn_high\tn_volume\t";
            head += "open\tclose\tlow\thigh\tvolume\t";
            head += "M1\tM2\tM3\tRSI1\tRSI2\tM21\tM22\tM23\tRSI21\tRSI22\tM31\tM32\tM33\tRSI31\tRSI32\tBB\tBT\tB2B\tB2T\tB3B\tB3T\t";
            head += "STD1\tSTK1\tSTD2\tSTK2\tSTD3\tSTK3\t";
            head += "ADX1\tADXR1\tADXM1\tADXP1";
            output.WriteLine(head);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            output.Close();
            // Put your deinitialization logic here
        }

        string buf = "";
        string buf2 = "";
        protected override void OnBar()
        {
            Bar b = Bars[Bars.Count - 2];
            string temp = string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4}", b.Open, b.Close, b.Low, b.High, b.TickVolume);
            buf2 = "";
            buf2 += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S_Slow.Result.LastValue, S_Slow2.Result.LastValue, S_Fast.Result.LastValue, S_RSI.Result.LastValue, S_RSI2.Result.LastValue);
            buf2 += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S2_Slow.Result.LastValue, S2_Slow2.Result.LastValue, S2_Fast.Result.LastValue, S2_RSI.Result.LastValue, S2_RSI2.Result.LastValue);
            buf2 += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S3_Slow.Result.LastValue, S3_Slow2.Result.LastValue, S3_Fast.Result.LastValue, S3_RSI.Result.LastValue, S3_RSI2.Result.LastValue);
            buf2 += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}\t{5:F6}", S_BAND.Bottom.LastValue, S_BAND.Top.LastValue, S2_BAND.Bottom.LastValue, S2_BAND.Top.LastValue, S3_BAND.Bottom.LastValue, S3_BAND.Top.LastValue);
            buf2 += string.Format("\t{0:F2}\t{1:F2}\t{2:F2}\t{3:F2}\t{4:F2}\t{5:F2}", S_STO.PercentD.LastValue, S_STO.PercentK.LastValue, S2_STO.PercentD.LastValue, S2_STO.PercentK.LastValue, S3_STO.PercentD.LastValue, S3_STO.PercentK.LastValue);
            buf2 += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}", S_ADX.ADX.LastValue, S_ADX.ADXR.LastValue, S_ADX.DIMinus.LastValue, S_ADX.DIPlus.LastValue);

            if (buf != "")
            {
                Regex r = new Regex("^(\t([0-9]*)(,?)([0-9]*))+$");
                if (r.IsMatch(buf))
                    output.WriteLine(string.Format("{0}{1}{2}", b.OpenTime, temp, buf));
            }
            buf = temp + buf2;
        }
    }
}
 

 

this is the second one

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.IO;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet | AccessRights.FileSystem)]
    public class pass9 : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("TimeFrame1")]
        public TimeFrame tFrame { get; set; }

        [Parameter("TimeFrame2")]
        public TimeFrame tFrame2 { get; set; }

        [Parameter("TimeFrame3")]
        public TimeFrame tFrame3 { get; set; }

        [Parameter("StopLoss")]
        public Double mStopLoss { get; set; }

        [Parameter("TakeProfit")]
        public Double mTakeProfit { get; set; }

        [Parameter("Lots")]
        public Double mLots { get; set; }

        /*
        MovingAverage UK;
        MovingAverage US;
        MovingAverage DE;
        MovingAverage FR;
        MovingAverage IT;
        MovingAverage GOLD;
        MovingAverage OIL;
        RelativeStrengthIndex UK_RSI;
        RelativeStrengthIndex US_RSI;
        RelativeStrengthIndex DE_RSI;
        RelativeStrengthIndex FR_RSI;
        RelativeStrengthIndex IT_RSI;
        RelativeStrengthIndex GOLD_RSI;
        RelativeStrengthIndex OIL_RSI;
        ChaikinVolatility vol;
        */
        MovingAverage S_Slow;
        MovingAverage S_Slow2;
        MovingAverage S_Fast;
        RelativeStrengthIndex S_RSI;
        RelativeStrengthIndex S_RSI2;
        MovingAverage S2_Slow;
        MovingAverage S2_Slow2;
        MovingAverage S2_Fast;
        RelativeStrengthIndex S2_RSI;
        RelativeStrengthIndex S2_RSI2;
        MovingAverage S3_Slow;
        MovingAverage S3_Slow2;
        MovingAverage S3_Fast;
        RelativeStrengthIndex S3_RSI;
        RelativeStrengthIndex S3_RSI2;
        BollingerBands S_BAND;
        BollingerBands S2_BAND;
        BollingerBands S3_BAND;
        StochasticOscillator S_STO;
        StochasticOscillator S2_STO;
        StochasticOscillator S3_STO;
        AverageDirectionalMovementIndexRating S_ADX;
        AverageDirectionalMovementIndexRating S2_ADX;
        StreamWriter log;

        protected override void OnStart()
        {
            /*
            UK = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "UK100").ClosePrices, 14, MovingAverageType.Simple);
            US = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "US500").ClosePrices, 14, MovingAverageType.Simple);
            DE = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "DE30").ClosePrices, 14, MovingAverageType.Simple);
            FR = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "F40").ClosePrices, 14, MovingAverageType.Simple);
            IT = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "IT40").ClosePrices, 14, MovingAverageType.Simple);
            GOLD = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "XAUUSD").ClosePrices, 14, MovingAverageType.Simple);
            OIL = Indicators.MovingAverage(MarketData.GetBars(TimeFrame.Daily, "XBRUSD").ClosePrices, 14, MovingAverageType.Simple);
            */

            S_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 15, MovingAverageType.Simple);
            S_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 10, MovingAverageType.Simple);
            S_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame).ClosePrices, 5, MovingAverageType.Simple);
            S_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame).ClosePrices, 14);
            S_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame).ClosePrices, 7);

            S2_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 15, MovingAverageType.Simple);
            S2_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 10, MovingAverageType.Simple);
            S2_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame2).ClosePrices, 5, MovingAverageType.Simple);
            S2_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame2).ClosePrices, 14);
            S2_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame2).ClosePrices, 7);

            S3_Slow = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 15, MovingAverageType.Simple);
            S3_Slow2 = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 10, MovingAverageType.Simple);
            S3_Fast = Indicators.MovingAverage(MarketData.GetBars(tFrame3).ClosePrices, 5, MovingAverageType.Simple);
            S3_RSI = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame3).ClosePrices, 14);
            S3_RSI2 = Indicators.RelativeStrengthIndex(MarketData.GetBars(tFrame3).ClosePrices, 7);

            /*
            S_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame).ClosePrices, 20, 2, MovingAverageType.Simple);
            S2_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame2).ClosePrices, 20, 2, MovingAverageType.Simple);
            S3_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame3).ClosePrices, 20, 2, MovingAverageType.Simple);
            */
            S_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame).ClosePrices, 12, 2, MovingAverageType.Simple);
            S2_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame2).ClosePrices, 12, 2, MovingAverageType.Simple);
            S3_BAND = Indicators.BollingerBands(MarketData.GetBars(tFrame3).ClosePrices, 12, 2, MovingAverageType.Simple);

            S_ADX = Indicators.AverageDirectionalMovementIndexRating(20);
            S2_ADX = Indicators.AverageDirectionalMovementIndexRating(9);
            S_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame), 9, 3, 9, MovingAverageType.Simple);
            S2_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame2), 9, 3, 9, MovingAverageType.Simple);
            S3_STO = Indicators.StochasticOscillator(MarketData.GetBars(tFrame3), 9, 3, 9, MovingAverageType.Simple);
            hlen = lookback * maxTimeFrame + 1;
            if (hlen < otherBars)
                hlen = otherBars;
            history = new string[hlen];
            log = new StreamWriter("c:\\marketdata\\" + Symbol.Name + "pass9.txt");

            header = "date\tn_open\tvolume\thigh\tlow\topen\tclose\t";
            header += "M1\tM2\tM3\tRSI1\tRSI2\tM21\tM22\tM23\tRSI21\tRSI22\tM31\tM32\tM33\tRSI31\tRSI32\tBB\tBT\tB2B\tB2T\tB3B\tB3T\t";
            header += "STD1\tSTK1\tSTD2\tSTK2\tSTD3\tSTK3\t";
            header += "ADX1\tADXR1\tADXM1\tADXP1\n";
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            log.Close();
        }

        protected int lookback = 6;
        protected int maxTimeFrame = 6;
        protected int otherBars = 24 * 3;
        protected string[] history;
        protected int hpos = 0;
        protected int hcount = 0;
        protected int hlen;
        string header = "";
        int bars = 0;

        protected override void OnBar()
        {

            Bar b = Bars[Bars.Count - 1];
            Bar bm1 = Bars[Bars.Count - 2];
            string buf = "";

            buf += string.Format("{0}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}\t{5:F6}\t{6:F6}", b.OpenTime, b.Open, bm1.TickVolume, bm1.High, bm1.Low, bm1.Open, bm1.Close);
            buf += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S_Slow.Result.LastValue, S_Slow2.Result.LastValue, S_Fast.Result.LastValue, S_RSI.Result.LastValue, S_RSI2.Result.LastValue);
            buf += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S2_Slow.Result.LastValue, S2_Slow2.Result.LastValue, S2_Fast.Result.LastValue, S2_RSI.Result.LastValue, S2_RSI2.Result.LastValue);
            buf += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}", S3_Slow.Result.LastValue, S3_Slow2.Result.LastValue, S3_Fast.Result.LastValue, S3_RSI.Result.LastValue, S3_RSI2.Result.LastValue);
            buf += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\t{4:F6}\t{5:F6}", S_BAND.Bottom.LastValue, S_BAND.Top.LastValue, S2_BAND.Bottom.LastValue, S2_BAND.Top.LastValue, S3_BAND.Bottom.LastValue, S3_BAND.Top.LastValue);
            buf += string.Format("\t{0:F2}\t{1:F2}\t{2:F2}\t{3:F2}\t{4:F2}\t{5:F2}", S_STO.PercentD.LastValue, S_STO.PercentK.LastValue, S2_STO.PercentD.LastValue, S2_STO.PercentK.LastValue, S3_STO.PercentD.LastValue, S3_STO.PercentK.LastValue);
            buf += string.Format("\t{0:F6}\t{1:F6}\t{2:F6}\t{3:F6}\n", S_ADX.ADX.LastValue, S_ADX.ADXR.LastValue, S_ADX.DIMinus.LastValue, S_ADX.DIPlus.LastValue);
            history[hpos] = buf;

            hpos = (hpos + 1) % hlen;

            if (hcount < hlen)
            {
                hcount++;
                return;
            }

            foreach (var p in Positions)
            {
                var span = b.OpenTime - p.EntryTime;
                // Print(span.TotalMinutes);
                if (span.TotalMinutes > 60 * 4 - 10)
                    p.Close();
            }

            string csv = header;
            for (int i = 0; i < hlen; i++)
                csv += history[(hpos + i) % hlen];

            bars++;
            if(bars>350)
            {
                log.WriteLine(csv);
                log.Close();
                throw new Exception("pippo");

            }

            var values = new Dictionary<string, string> 
            {
                {
                    "csv",
                    csv
                }
            };
            HttpClient client = new HttpClient();
            HttpContent content = new FormUrlEncodedContent(values);
            Task<HttpResponseMessage> response = Task.Run(async() => await client.PostAsync("http://localhost:5000/eurusd", content));
            string res = Task.Run(async() => await response.Result.Content.ReadAsStringAsync()).Result;
            content.Dispose();
            response.Dispose();
            client.Dispose();

            var results = res.Split(',');
            var pips = 1;
            var pred = results[0];
            // Print(pred);
            if (pred == "cutted")
                return;
            //string text = "";
            double amount = mLots * 100000;
            // amount = Symbol.QuantityToVolumeInUnits((int)(Account.Balance * 250 / 100000));

            if (amount > 10000000)
                amount = 10000000;
            //Print(amount);
            //text += string.Format("{0} amount {1} ", pred, amount);

            if (pred == "sell" && b.Open<bm1.High)
            {
                var price = b.Open;
                var result = ExecuteMarketRangeOrder(TradeType.Sell, Symbol.Name, amount, pips, b.Open, "pass7");
                if (result.IsSuccessful)
                {
                    result.Position.ModifyTakeProfitPrice(b.Open - mTakeProfit * Symbol.PipSize);
                    double stopLoss = bm1.High;
                    if (stopLoss > b.Open + mStopLoss * Symbol.PipSize)
                        stopLoss = b.Open + mStopLoss * Symbol.PipSize;
                    result.Position.ModifyStopLossPrice(stopLoss);
                }

            }

            if (pred == "buy" && b.Open>bm1.Low)
            {
                var price = b.Open;
                var result = ExecuteMarketRangeOrder(TradeType.Buy, Symbol.Name, amount, pips, b.Open, "pass7");
                if (result.IsSuccessful)
                {
                    result.Position.ModifyTakeProfitPrice(b.Open + mTakeProfit * Symbol.PipSize);
                    double stopLoss = bm1.Low;
                    if (stopLoss < b.Open - mStopLoss * Symbol.PipSize)
                        stopLoss = b.Open - mStopLoss * Symbol.PipSize;
                    result.Position.ModifyStopLossPrice(stopLoss);
                }
            }

            //log.WriteLine(text);
            //log.Flush();
        }
    }
}
 

 

can you help me to understand why the indicators give different numbers?

 

seems that some indicators have memory of old history over the indicator period..... the problem seems to be that i use the 2 robot with different start datetime for testing.... savedata start from 2014 but pass9 start in 2021

 

best regards 

amerigo


@astevani
Replies

amusleh
29 Nov 2021, 08:59

Hi,

Please try to use your indicator and cBot only on a single current chart time frame and then check the data, it might be due to data difference when you are loading multiple time frames data.

Or it can also be caused by double type precision issue, try to round and change the type to decimal if you want to use the same exact data.


@amusleh