How do I draw on the oscillator panel?
How do I draw on the oscillator panel?
11 Jun 2024, 18:48
Hello!
I want to draw trend lines at the points of the minimum and maximum of the Stochastic Oscillator, but I could not find how to draw on the oscillator itself.
How can I use methods like DrawTrendLine() not on the main chart (price chart), but on the panel where the oscillator is located?
I'm adding an oscillator to the chart:
protected override void Initialize()
{
_stochastic = Indicators.StochasticOscillator(KPeriod, DPeriod, Slowing, MovingAverageType.Simple);
StochChart = _stochastic.AddToChart();
}
I've tried calling drawing methods via:
Chart.IndicatorAreas[id].DrawSomething();
But nothing appears on the charts.
Example of how it should look with my hands:
Replies
PanagiotisCharalampous
12 Jun 2024, 06:19
Hi there,
Please provide us with the complete cBot code so that we can check.
Best regards,
Panagiotis
@PanagiotisCharalampous
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
PanagiotisCharalampous
12 Jun 2024, 06:19
Hi there,
Please provide us with the complete cBot code so that we can check.
Best regards,
Panagiotis
@PanagiotisCharalampous