Seeking Assistance with cAlgo C# Code Error

Created at 02 Sep 2023, 18:02
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!
AL

alenjose17

Joined 02.09.2023

Seeking Assistance with cAlgo C# Code Error
02 Sep 2023, 18:02


Hello cTrader Forum Members,

I hope this message finds you well. I am a professional forex trader, specializing in day trading XAUUSD during the New York and London crossover session. Recently, I've been working on automating my trading strategy using cAlgo C# within the cTrader platform.

However, I've run into a challenging issue during the compilation process, which has resulted in the following error:

Build Result Build failed (1 error) Error CS0117: 'DataSeries' does not contain a definition for 'Tick' (C:\Users\alenj\OneDrive\Documents\cAlgo\Sources\Robots\New cBot (6)\New cBot (6)\New cBot (6).cs, line: 35, column: 73)

I have attached the relevant segment of my code below for your reference.

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MyTradingSystemBot : Robot
    {
        private new const string SymbolName = "XAUUSD"; // Used 'new' keyword to avoid hiding inherited member
        private const double PositionSize = 10000; // 0.01 lot size
        private const double StopLossInPips = 500;
        private const double TakeProfitInPips = 500;
        private const double MaxOpenCloseHeight = 110; // Maximum open to close height in pips
        private const double MinUpperWick = 390; // Minimum upper wick length in pips
        private const double MinLowerWick = 390; // Minimum lower wick length in pips

        private System.Timers.Timer _timer;
        private Symbol symbol;

        protected override void OnStart()
        {
            symbol = Symbol;
            _timer = new System.Timers.Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
            _timer.Elapsed += OnTimerTick;
            _timer.Start();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            if (!IsValidTradingTime())
                return;

            Bars rangeBars = MarketData.GetBars(symbol.Name, DataSeries.Tick);

            double currentRangeHigh = rangeBars.HighPrices.LastValue;
            double currentRangeLow = rangeBars.LowPrices.LastValue;

            if (IsBuySignal(currentRangeHigh, currentRangeLow))
                ExecuteTrade(TradeType.Buy, currentRangeHigh, currentRangeLow);
            else if (IsSellSignal(currentRangeHigh, currentRangeLow))
                ExecuteTrade(TradeType.Sell, currentRangeHigh, currentRangeLow);
        }

        private bool IsValidTradingTime()
        {
            DayOfWeek dayOfWeek = Server.Time.DayOfWeek;
            int hour = Server.Time.Hour;
            int minute = Server.Time.Minute;

            bool isTradingDay = dayOfWeek >= 
            DayOfWeek.Monday && dayOfWeek <= DayOfWeek.Friday;
            bool isTradingTime = hour == 17 && minute >= 30 && hour <= 20 && minute <= 30;

            return isTradingDay && isTradingTime;
        }

        private bool IsBuySignal(double high, double low)
        {
            double bodySize = Math.Abs(symbol.Bid - symbol.Ask);
            double upperWick = Math.Max(high - symbol.Bid, high - symbol.Ask);

            return bodySize <= MaxOpenCloseHeight * symbol.PipSize && upperWick >= MinUpperWick * symbol.PipSize && IsHammerPattern();
        }

        private bool IsSellSignal(double high, double low)
        {
            double bodySize = Math.Abs(symbol.Bid - symbol.Ask);
            double lowerWick = Math.Max(symbol.Bid - low, symbol.Ask - low);

            return bodySize <= MaxOpenCloseHeight * symbol.PipSize && lowerWick >= MinLowerWick * symbol.PipSize && IsShootingStarPattern();
        }

        private bool IsHammerPattern()
        {
            // Implement your Hammer candlestick pattern detection logic here
            // For example, you can check for the specified criteria
            return false;
        }

        private bool IsShootingStarPattern()
        {
            // Implement your Shooting Star candlestick pattern detection logic here
            // For example, you can check for the specified criteria
            return false;
        }

        private void ExecuteTrade(TradeType tradeType, double entryPrice, double stopLossPrice)
        {
            double takeProfitPrice = CalculateTakeProfit(tradeType, entryPrice);

            TradeResult result = ExecuteMarketOrder(tradeType, symbol.Name, PositionSize, "Trade", StopLossInPips, takeProfitPrice);

            if (result.IsSuccessful)
            {
                Print("Trade executed successfully. Trade type: " + tradeType);
            }
            else
            {
                Print("Error executing trade. Error code: " + result.Error);
            }
        }

        private double CalculateTakeProfit(TradeType tradeType, double entryPrice)
        {
            return tradeType == TradeType.Buy ? entryPrice + TakeProfitInPips * symbol.PipSize : entryPrice - TakeProfitInPips * symbol.PipSize;
        }

        protected override void OnTick()
        {
            // You can implement additional tick-based logic here
        }

        protected override void OnStop()
        {
            _timer.Stop();
            _timer.Elapsed -= OnTimerTick;
        }
    }
}

Despite my best efforts to troubleshoot, I have not been able to resolve this error, which is preventing me from successfully automating my trading strategy with range bars.

I kindly request your expertise and assistance in diagnosing and resolving this issue. Range bars are crucial to my trading approach, and I believe that automating them would significantly enhance my trading efficiency.

I am open to any guidance, insights, or suggestions that you can provide. Your help would be greatly appreciated, and I thank you in advance for your time and support.

Best regards,                                                                                                                                                                                                                                                                               Alen Rayen                                                                                                                                                                                                                                                                                 cTrader Client


@alenjose17
Replies

PanagiotisChar
04 Sep 2023, 05:23

Hi there,

I think the error message is clear and any programmer that programmed this would know how to fix it based on the what needs to be actually done. May I ask where you found the code?


@PanagiotisChar