Symbol "XAUUSD m5" cached data not available or corrupted

Created at 09 Jan 2023, 11:16
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!
NO

noolohp

Joined 11.03.2022

Symbol "XAUUSD m5" cached data not available or corrupted
09 Jan 2023, 11:16


Cbot cannot gather data.

best regards, 

NL

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SwinghighSwinglow : Robot
    {
        #region Parameters

        [Parameter(Group = "Trading")]
        public TradeType Direction { get; set; }

        [Parameter("TP LEVEL for position before start", DefaultValue = 1.13, Group = "Trading")]
        public double TP_Level { get; set; }

        [Parameter("SL LEVEL for position before start", DefaultValue = 1.13, Group = "Trading")]
        public double SL_Level { get; set; }

        [Parameter("BE LEVEL for position before start", DefaultValue = 1.13, Group = "Trading")]
        public double BE_Level { get; set; }

        [Parameter("Stop Loss", DefaultValue = 1.5, Group = "Trading")]
        public double SL { get; set; }

        [Parameter("Take Profit", DefaultValue = 3, Group = "Trading")]
        public double TP { get; set; }

        [Parameter("Take Profit exception handling", DefaultValue = 1.7, Group = "Trading")]
        public double TP_exception { get; set; }

        [Parameter("Break-Even", DefaultValue = 2, Group = "Trading")]
        public double BE { get; set; }

        [Parameter("Break-Even exception handling", DefaultValue = 0.7, Group = "Trading")]
        public double BE_exception { get; set; }

        [Parameter("Minimum R:R", DefaultValue = 1.1, Group = "Trading")]
        public double Min_RR { get; set; }

        [Parameter("Risk Per Trade (%)", DefaultValue = 2, Group = "Trading")]
        public double RiskPerTrade { get; set; }

        [Parameter("Risk Per Trade2 (%)", DefaultValue = 1.5, Group = "Trading")]
        public double LowSL_RiskPerTrade { get; set; }

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

        [Parameter("Periods", DefaultValue = 14, Group = "ATR")]
        public int ATRPeriods { get; set; }

        [Parameter("Timeframe", Group = "Support/Resistance")]
        public TimeFrame TF { get; set; }

        #endregion Parameters
        #region Private Variables

        #endregion
        #region Global Variables

        #region data stacks
        int i = 1;
        bool _datareached = false;
        bool isRedDriven = false;
        bool isDowntrend = false;
        bool isPreviousBarRed = false;
        bool neitherRedNorGreenDriven = true;
        double last_resistance = 0;
        double last_support = 0;
        Stack<double> l_stack = new Stack<double>();
        Stack<double> h_stack = new Stack<double>();
        Stack<double> r_stack = new Stack<double>();
        Stack<double> s_stack = new Stack<double>();
        Stack<double> resistance_stack = new Stack<double>();
        Stack<double> support_stack = new Stack<double>();
        Stack<double> high_stack = new Stack<double>();
        Stack<double> low_stack = new Stack<double>();
        double current_resistance = Double.NegativeInfinity;
        double current_support = Double.PositiveInfinity;
        double current_high = Double.NegativeInfinity;
        double current_low = Double.PositiveInfinity;
        #endregion


        #endregion

        protected override void OnStart()
        {
            // Put your initialization logic here
            #region Tools
            
            #endregion
            #region getstacks
            double currentBarOpen = Bars.OpenPrices[i];
            double currentBarClose = Bars.ClosePrices[i];
            double lastBarOpen = Bars.OpenPrices.Last(0);
            double lastBarClose = Bars.ClosePrices.Last(0);
            var curreBarOpenTime = Bars.OpenTimes[i];
            var preBarOpentime = Bars.OpenTimes[i - 1];
            bool isCurrentBarRed = (Bars.OpenPrices[i - 1] > Bars.ClosePrices[i - 1]) || (Bars.OpenPrices[i - 1] == Bars.ClosePrices[i - 1] && (Bars.OpenPrices[i - 1] <= (Bars.LowPrices[i - 1] + (Bars.HighPrices[i - 1] - Bars.LowPrices[i - 1]) * 0.382)));
            double currentBarHigh = Bars.HighPrices[i];
            double currentBarLow = Bars.LowPrices[i];

            double previousBarOpen = Bars.OpenPrices[i - 1];
            double previousBarClose = Bars.ClosePrices[i - 1];
            double previousBarHigh = Bars.HighPrices[i - 1];
            double previousBarLow = Bars.LowPrices[i - 1];

            double last_resistance = 0;
            double last_support = 0;

            r_stack.Push(Double.PositiveInfinity);
            s_stack.Push(Double.NegativeInfinity);
            l_stack.Push(Double.PositiveInfinity);
            h_stack.Push(Double.NegativeInfinity);

            while (curreBarOpenTime != Bars.Last(1).OpenTime)
            {
                isPreviousBarRed = isCurrentBarRed;
                isCurrentBarRed = (currentBarOpen > currentBarClose) || (currentBarClose == currentBarOpen && (currentBarOpen <= (currentBarLow + (currentBarHigh - currentBarLow) * 0.382)));

                if (isCurrentBarRed && isPreviousBarRed && !isRedDriven)
                {
                    isRedDriven = true;
                    neitherRedNorGreenDriven = false;

                    current_support = currentBarClose;
                    r_stack.Push(current_resistance);
                    resistance_stack.Push(r_stack.Peek());
                    current_low = currentBarLow;
                    h_stack.Push(current_high);
                    high_stack.Push(h_stack.Peek());

                    if (resistance_stack.Count() >= 2)
                    {
                        last_resistance = resistance_stack.ElementAt(1);

                        if (current_resistance > last_resistance)
                        {
                            isDowntrend = false;
                        }
                    }
                }
                else if (!isCurrentBarRed && !isPreviousBarRed && (isRedDriven || neitherRedNorGreenDriven))
                {
                    isRedDriven = false;
                    neitherRedNorGreenDriven = false;

                    current_resistance = currentBarClose;
                    s_stack.Push(current_support);
                    support_stack.Push(s_stack.Peek());

                    current_high = currentBarHigh;
                    l_stack.Push(current_low);
                    low_stack.Push(l_stack.Peek());

                    if (support_stack.Count() >= 2)
                    {
                        last_support = support_stack.ElementAt(1);

                        if (!isDowntrend && current_support < last_support)
                        {
                            isDowntrend = true;
                        }
                    }
                }
                else if (isRedDriven)
                {
                    current_support = Math.Min(current_support, Math.Min(currentBarOpen, currentBarClose));
                    current_low = Math.Min(current_low, currentBarLow);

                    if ((Math.Max(currentBarOpen, currentBarClose) > current_resistance))
                    {
                        current_resistance = Math.Max(current_resistance, Math.Max(currentBarOpen, currentBarClose));
                        if (resistance_stack.Count() > 0)
                        {
                            resistance_stack.Pop();
                        }
                        resistance_stack.Push(current_resistance);
                        if (currentBarHigh > current_high)
                        {
                            current_high = currentBarHigh;
                            if (high_stack.Count() > 0)
                            {
                                high_stack.Pop();
                            }

                            high_stack.Push(current_high);
                        }
                    }
                    else if (current_high < currentBarHigh)
                    {
                        current_high = currentBarHigh;
                        if (high_stack.Count() > 0)
                        {
                            high_stack.Pop();
                        }
                        high_stack.Push(current_high);
                    }
                }
                else if (!isRedDriven || !neitherRedNorGreenDriven)
                {
                    current_resistance = Math.Max(current_resistance, Math.Max(currentBarOpen, currentBarClose));
                    current_high = Math.Max(current_high, currentBarHigh);

                    if (Math.Min(currentBarOpen, currentBarClose) < current_support)
                    {
                        current_support = Math.Min(current_support, Math.Min(currentBarOpen, currentBarClose));
                        if (support_stack.Count() > 0)
                        {
                            support_stack.Pop();
                        }

                        support_stack.Push(current_support);

                        if (currentBarLow < current_low)
                        {
                            current_low = currentBarLow;

                            if (low_stack.Count() > 0)
                            {
                                low_stack.Pop();
                            }
                            low_stack.Push(current_low);
                        }
                    }
                    else if (current_low > currentBarLow)
                    {
                        current_low = currentBarLow;

                        if (low_stack.Count() > 0)
                        {
                            low_stack.Pop();
                        }
                        low_stack.Push(current_low);
                    }
                }
               
                i++;
                curreBarOpenTime = Bars.OpenTimes[i];
                currentBarOpen = Bars.OpenPrices[i];
                currentBarClose = Bars.ClosePrices[i];
                currentBarHigh = Bars.HighPrices[i];
                currentBarLow = Bars.LowPrices[i];
                previousBarOpen = Bars.OpenPrices[i - 1];
                previousBarClose = Bars.ClosePrices[i - 1];
                previousBarHigh = Bars.HighPrices[i - 1];
                previousBarLow = Bars.LowPrices[i - 1];
            }
            if (curreBarOpenTime == Bars.Last(1).OpenTime)
            {
                _datareached = true;
            }
            #endregion
        }
      
    }
}


@noolohp