Replies

svsvkusnsnjc
12 Jun 2024, 22:24 ( Updated at: 13 Jun 2024, 05:24 )

RE: How do I draw on the oscillator panel?

PanagiotisCharalampous said: 

Hi there,

Please provide us with the complete cBot code so that we can check.

Best regards,

Panagiotis

 

Hello,

Today I rewrote the indicator code and this method worked (via IndicatorAreas[]). I don't know what I made a mistake last time - yesterday I spent the whole day searching for a solution, but now everything is being drawn correctly.

However, I would like to clarify - how to determine which IndicatorAreas[id] a panel belongs to if there are several panels (more than one)? For example, if I add a Stochastic Oscillator and a MACD to the chart, then I will have 2 panels with indicators. How is it guaranteed to determine inside the code which indicator IndicatorAreas[1] belongs to and which IndicatorAreas[2] belongs to?

Indicator code:

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class StohDangerZoneAndTrend : Indicator
    {
        private StochasticOscillator _stochastic;

        [Parameter("K Period", DefaultValue = 5)]
        public int KPeriod { get; set; }

        [Parameter("D Period", DefaultValue = 3)]
        public int DPeriod { get; set; }

        [Parameter("Slowing", DefaultValue = 3)]
        public int Slowing { get; set; }
        
        [Parameter("Neutral Color", DefaultValue = Colors.White)]
        public Color NeutralColor { get; set; }
        
        [Parameter("Up-Trend Color", DefaultValue = Colors.Green)]
        public Color UpColor { get; set; }
        
        [Parameter("Down-Trend Color", DefaultValue = Colors.Red)]
        public Color DownColor { get; set; }
        
        [Parameter("Shift Amount", DefaultValue = 10)]
        public int ShiftAmount { get; set; }

        [Output("Shifted Line", PlotType = PlotType.DiscontinuousLine)]
        public IndicatorDataSeries ShiftedLineSeries { get; set; }
        
        public struct ChartPoint
        {
            public DateTime Time { get; set; }
            public double Price { get; set; }
            public double PercentK { get; set; }
        
            public ChartPoint(DateTime time, double price, double percentK)
            {
                Time = time;
                Price = price;
                PercentK = percentK;
            }
        }
        
        private List<ChartPoint> _UpPoints;
        private List<ChartPoint> _DownPoints;
        
        private enum DirectionStates
        {
            DOWN = -1,
            UP = 1,
            NORMAL = 0
        }
        
        
        private DirectionStates CurrState;

        protected override void Initialize()
        {
            _UpPoints = new List<ChartPoint>();
            _DownPoints = new List<ChartPoint>();
            
            CurrState = DirectionStates.NORMAL;
            _stochastic = Indicators.StochasticOscillator(KPeriod, Slowing, DPeriod, MovingAverageType.Simple);
            _stochastic.AddToChart();
        }

        public override void Calculate(int index)
        {
            
            if (index < 3)
                return;
            
            CheckForNewPoints(index);
            
        }
        
        private void CheckForNewPoints(int index)
        {
            double stoh_right = _stochastic.PercentK[index];
            double stoh_mid = _stochastic.PercentK[index-1];
            double stoh_left = _stochastic.PercentK[index-2];
            
            ChartPoint new_point = new ChartPoint(Bars.OpenTimes[index-1], Bars.ClosePrices[index-1], stoh_mid);
            DirectionStates TrendLineType = DirectionStates.NORMAL;
            
            if (stoh_mid < stoh_right && stoh_mid < stoh_left)
            {
                AddPoints(ref _UpPoints, new_point);
                TrendLineType = DirectionStates.UP;
                CheckForTrendLine(_UpPoints, TrendLineType, index);
            }
            else if (stoh_mid > stoh_right && stoh_mid > stoh_left)
            {
                AddPoints(ref _DownPoints, new_point);
                TrendLineType = DirectionStates.DOWN;
                CheckForTrendLine(_DownPoints, TrendLineType, index);
            }
            
        }
        
        private void CheckForTrendLine(List<ChartPoint> list, DirectionStates TrendLineType, int index)
        {
            double left_point = list[0].PercentK;
            double right_point = list[1].PercentK;
            
            switch (TrendLineType)
            {
                case DirectionStates.UP:
                    if (left_point < right_point)
                    {
                        DrawStohTrend(list, TrendLineType, index);
                    }
                    return;
                    
                case DirectionStates.DOWN:
                if (left_point > right_point)
                    {
                        DrawStohTrend(list, TrendLineType, index);
                    }
                    return;
                    
                default:
                    return;
            }
        }
        
        
        private void AddPoints(ref List<ChartPoint> list, ChartPoint newPoint)
        {
            if (list.Count == 2)
            {
                list.RemoveAt(0);
            }
            list.Add(newPoint);
        }
        
        private Color SwitchColor(DirectionStates State)
        {
            switch (State)
            {
                case DirectionStates.UP:
                    return UpColor;
                case DirectionStates.DOWN:
                    return DownColor;
                default:
                    return NeutralColor;
            }
        }
        
        
        private void DrawStohTrend(List<ChartPoint> list, DirectionStates TrendLineType, int index)
        {
            Color lineColor = SwitchColor(TrendLineType);
            Chart.IndicatorAreas[1].DrawTrendLine("line_stoh_", list[0].Time, list[0].PercentK, list[1].Time, list[1].PercentK, lineColor, 2, LineStyle.Solid);
        }
    }
}

@svsvkusnsnjc