Replies

Falcorest
03 Apr 2025, 08:04

RE: Sessions Indicator

hi danielscales, and thank you so much for your help!

i tried to apply your code, but i still have some issues about the indicator..
this is the result:

 

this is the code:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DailySessionHighlighter : Indicator
    {
        [Parameter("Sessione 1 Inizio (HH:MM)", DefaultValue = "00:00")]
        public string Session1StartStr { get; set; }
        
        [Parameter("Sessione 1 Fine (HH:MM)", DefaultValue = "15:30")]
        public string Session1EndStr { get; set; }
        
        [Parameter("Sessione 2 Inizio (HH:MM)", DefaultValue = "15:30")]
        public string Session2StartStr { get; set; }
        
        [Parameter("Sessione 2 Fine (HH:MM)", DefaultValue = "22:00")]
        public string Session2EndStr { get; set; }
        
        [Parameter("Colore Sessione 1", DefaultValue = "Blue")]
        public string Session1ColorStr { get; set; }
        
        [Parameter("Colore Sessione 2", DefaultValue = "Green")]
        public string Session2ColorStr { get; set; }
        
        [Parameter("Spessore Linea", DefaultValue = 1)]
        public int LineThickness { get; set; }
        
        [Parameter("Opacità", DefaultValue = 50, MinValue = 0, MaxValue = 100)]
        public int Opacity { get; set; }
        
        [Parameter("Num. Sessioni da Mostrare", DefaultValue = 100, MinValue = 1)]
        public int SessionsToShow { get; set; }

        private TimeSpan _session1Start, _session1End, _session2Start, _session2End;
        private Color _session1Color, _session2Color;
        private List<SessionData> _pastSessions = new List<SessionData>();
        private SessionData _currentSession1, _currentSession2;

        protected override void Initialize()
        {
            if (!TimeSpan.TryParse(Session1StartStr, out _session1Start))
                _session1Start = new TimeSpan(22, 0, 0);
            if (!TimeSpan.TryParse(Session1EndStr, out _session1End))
                _session1End = new TimeSpan(15, 0, 0);
            if (!TimeSpan.TryParse(Session2StartStr, out _session2Start))
                _session2Start = new TimeSpan(15, 0, 0);
            if (!TimeSpan.TryParse(Session2EndStr, out _session2End))
                _session2End = new TimeSpan(22, 0, 0);
                
            _session1Color = GetColor(Session1ColorStr, Opacity);
            _session2Color = GetColor(Session2ColorStr, Opacity);
        }

        private Color GetColor(string colorStr, int opacity)
        {
            try
            {
                var color = Color.FromName(colorStr);
                return Color.FromArgb(opacity * 255 / 100, color);
            }
            catch
            {
                return Color.FromArgb(opacity * 255 / 100, Color.Blue);
            }
        }

        public override void Calculate(int index)
        {
            if (index < 0 || index >= Bars.Count) return;

            var bar = Bars[index];
            var barTime = bar.OpenTime;
            
            if (_pastSessions.Count > SessionsToShow * 2)
            {
                RemoveSessionDrawings(_pastSessions[0]);
                _pastSessions.RemoveAt(0);
            }

            // Session 1 Logic
            var session1StartTime = new DateTime(barTime.Year, barTime.Month, barTime.Day).Add(_session1Start);
            var session1EndTime = new DateTime(barTime.Year, barTime.Month, barTime.Day).Add(_session1End);
            if (_session1End < _session1Start) session1EndTime = session1EndTime.AddDays(1); 
            
            if (barTime >= session1StartTime && barTime <= session1EndTime)
            {
                if (_currentSession1 == null || _currentSession1.EndTime.Date != session1StartTime.Date)
                {
                    _currentSession1 = new SessionData 
                    {
                        StartTime = session1StartTime,
                        EndTime = session1EndTime,
                        High = bar.High,
                        Low = bar.Low,
                        Color = _session1Color,
                        IsActive = true
                    };
                }
                else
                {
                    _currentSession1.High = Math.Max(_currentSession1.High, bar.High);
                    _currentSession1.Low = Math.Min(_currentSession1.Low, bar.Low);
                }
                DrawSessionRectangle(_currentSession1);
            }
            else if (_currentSession1 != null && _currentSession1.IsActive && barTime > session1EndTime)
            {
                _currentSession1.IsActive = false;
                _pastSessions.Add(_currentSession1);
                _currentSession1 = null;
            }

            // Session 2 Logic
            var session2StartTime = new DateTime(barTime.Year, barTime.Month, barTime.Day).Add(_session2Start);
            var session2EndTime = new DateTime(barTime.Year, barTime.Month, barTime.Day).Add(_session2End);
            if (_session2End < _session2Start) session2EndTime = session2EndTime.AddDays(1); 

            if (barTime >= session2StartTime && barTime <= session2EndTime)
            {
                if (_currentSession2 == null || _currentSession2.EndTime.Date != session2StartTime.Date)
                {
                    _currentSession2 = new SessionData 
                    {
                        StartTime = session2StartTime,
                        EndTime = session2EndTime,
                        High = bar.High,
                        Low = bar.Low,
                        Color = _session2Color,
                        IsActive = true
                    };
                }
                else
                {
                    _currentSession2.High = Math.Max(_currentSession2.High, bar.High);
                    _currentSession2.Low = Math.Min(_currentSession2.Low, bar.Low);
                }
                DrawSessionRectangle(_currentSession2);
            }
            else if (_currentSession2 != null && _currentSession2.IsActive && barTime > session2EndTime)
            {
                _currentSession2.IsActive = false;
                _pastSessions.Add(_currentSession2);
                _currentSession2 = null;
            }

            foreach (var session in _pastSessions)
            {
                DrawSessionRectangle(session);
            }
        }
        
        private void DrawSessionRectangle(SessionData session)
        {
            string prefix = session.Color == _session1Color ? "Sess1_" : "Sess2_";
            prefix += session.StartTime.ToString("yyyyMMdd_HHmm");
            
            DateTime endX = session.IsActive ? Bars.LastBar.OpenTime : session.EndTime;
            if (endX > session.EndTime) endX = session.EndTime;
            
            Chart.DrawTrendLine(prefix + "_left", session.StartTime, session.Low, session.StartTime, session.High, session.Color, LineThickness);
            Chart.DrawTrendLine(prefix + "_right", endX, session.Low, endX, session.High, session.Color, LineThickness);
            Chart.DrawTrendLine(prefix + "_top", session.StartTime, session.High, endX, session.High, session.Color, LineThickness);
            Chart.DrawTrendLine(prefix + "_bottom", session.StartTime, session.Low, endX, session.Low, session.Color, LineThickness);
        }
        
        private void RemoveSessionDrawings(SessionData session)
        {
            string prefix = session.Color == _session1Color ? "Sess1_" : "Sess2_";
            prefix += session.StartTime.ToString("yyyyMMdd_HHmm");
            
            Chart.RemoveObject(prefix + "_left");
            Chart.RemoveObject(prefix + "_right");
            Chart.RemoveObject(prefix + "_top");
            Chart.RemoveObject(prefix + "_bottom");
        }
    }
    
    public class SessionData
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public double High { get; set; }
        public double Low { get; set; }
        public Color Color { get; set; }
        public bool IsActive { get; set; }
    }
}

thank you again!!


@Falcorest

Falcorest
10 Mar 2025, 15:34

RE: Problem with Pending Orders and ProtectionType

CNsofia said: 

I have exactly the same problem. “Error | Crashed in OnBar with TypeLoadException: Could not load type 'cAlgo.API.ProtectionType' from assembly 'cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880'.”

 

i just deleted the protection.type parameter and now the bot works, even if the console get the “obsolete” error message


@Falcorest

Falcorest
03 Mar 2025, 16:20

RE: RE: RE: Problem with Pending Orders and ProtectionType

firemyst said: 

 

Right now, I don't even think they're working on or investigating the issue because they've said absolutely nothing and people have been reporting Cloud issues since Nov/Dec.

 

O_O

So, at this point, I think a good solution might be to get a VPS.

Do you know if cTrader can run on a server with Windows Server 2022?


@Falcorest

Falcorest
03 Mar 2025, 08:39 ( Updated at: 03 Mar 2025, 08:54 )

RE: Problem with Pending Orders and ProtectionType

firemyst said: 

Two suggestions from me:

 

  1. Don't run the bot in the cloud. There's too many people having too many issues. Run it locally, especially if you're just testing and playing around.
  2. The obsolete issue. Ignore it. Compile it anyway without the protection type parameter. It'll still run. That's what I've done when editing code in Visual Studio. Spotware has yet to post documentation or respond to threads asking what the protection type parameter is, or does.

hi, firemyst,
Thank you for your help!
This morning I tested the cBot on the latest version of cTrader, and the error message is still the same. However, if I run the cBot locally, everything works fine.

I can't understand the reason.

It's a shame not to be able to use the "On Cloud" setting, which allows the cBot to stay active all day and be managed from any device.


@Falcorest

Falcorest
01 Oct 2024, 06:47 ( Updated at: 01 Oct 2024, 07:28 )

hi Ali, i have your same needs

Have you solved?
Could you give me some advices?

thank you


@Falcorest