Interactive price line indicator
Created at 27 May 2022, 23:40
Interactive price line indicator
27 May 2022, 23:40
Hi,
I'm trying to create a simple indicator that shows an interactive Priceline with a static text that displays the distance from the price but I am experiencing 2 problems:
On activating the price line, it should start on the close price at the time of start, but activates on random spots.
The second problem is that if I switch the display back to No, the object is not deleted.
Thanks,
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class InteractivePriceLine : Indicator
{
[Parameter("Show Interactive Price Line?", Group = "Chart", DefaultValue = false)]
public bool _show_interactive_price_line { get; set; }
ChartStaticText _price_info;
ExponentialMovingAverage _fast;
ChartHorizontalLine _price_line;
bool _price_line_exists;
protected override void Initialize()
{
_fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 12);
_price_info = Chart.DrawStaticText("_price_info", string.Empty, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Chocolate);
_price_line_exists = false;
}
public override void Calculate(int index)
{
if (_show_interactive_price_line)
{
double _current_pips_from_line;
if (!_price_line_exists)
{
_price_line = Chart.DrawHorizontalLine("_price_line", Bars.ClosePrices.LastValue, Color.White, 2, LineStyle.LinesDots);
_price_line_exists = true;
}
_price_line.IsInteractive = true;
_price_info.Color = Color.White;
if (_price_line.Y > Bars.ClosePrices.LastValue)
{
_price_line.Color = Color.Blue;
_current_pips_from_line = Math.Round((_price_line.Y - Bars.ClosePrices.LastValue) / Symbol.PipSize, 1);
_price_info.Text = string.Format("Price Lower Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
}
if (_price_line.Y < Bars.ClosePrices.LastValue)
{
_price_line.Color = Color.Yellow;
_current_pips_from_line = Math.Round((Bars.ClosePrices.LastValue - _price_line.Y) / Symbol.PipSize, 1);
_price_info.Text = string.Format("Price Higher Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
}
}
if (!_show_interactive_price_line)
{
if (_price_line != null)
{
Chart.RemoveObject("_price_line");
_price_line_exists = false;
}
}
}
}
}
amusleh
30 May 2022, 09:22
Hi,
Try this:
@amusleh