ATR Switch with button
Created at 20 Oct 2022, 17:50
ATR Switch with button
20 Oct 2022, 17:50
Hi,
I created an indicator that should show the selected ATR SL value on the chart, with a button that should switch the type between buy and sell.
However, I am having problems getting the button to work, both in function and chart placement.
Any advice would be appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
public class ATRSLValue : Indicator
{
[Parameter("Period", Group = "ATR", DefaultValue = 14, MinValue = 1)]
public int PeriodATR { get; set; }
[Parameter("MA Type", Group = "ATR", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType MAtypeATR { get; set; }
[Parameter("Multiplier", Group = "ATR", DefaultValue = 1.0, MinValue = 0.1, Step = 0.1)]
public double MultiplierATR { get; set; }
AverageTrueRange _atr;
double CalculatedResult;
bool SellOrBuy;
Button button;
[Output("Main Up", LineColor = "Green", Thickness = 2)]
public IndicatorDataSeries ResultUp { get; set; }
[Output("Main Down", LineColor = "Red", Thickness = 2)]
public IndicatorDataSeries ResultDown { get; set; }
protected override void Initialize()
{
_atr = Indicators.AverageTrueRange(PeriodATR, MAtypeATR);
button = new Button();
button.BackgroundColor = Color.Green;
button.Text = "SWITCH TO SELL";
button.Margin = 300;
button.HorizontalAlignment = HorizontalAlignment.Right;
button.VerticalAlignment = VerticalAlignment.Bottom;
button.IsVisible = true;
button.IsEnabled = true;
SellOrBuy = false;
Chart.AddControl(button);
}
public override void Calculate(int index)
{
button.Click += args => SwitchType();
CalculatedResult = _atr.Result.LastValue * MultiplierATR;
if (SellOrBuy)
{
button.BackgroundColor = Color.Green;
button.Text = "SWITCH TO SELL";
ResultUp[index] = Bars.ClosePrices.LastValue - CalculatedResult;
}
if (!SellOrBuy)
{
button.BackgroundColor = Color.OrangeRed;
button.Text = "SWITCH TO BUY";
ResultDown[index] = Bars.ClosePrices.LastValue + CalculatedResult;
}
}
private void SwitchType()
{
if (SellOrBuy)
{
SellOrBuy = false;
}
if (!SellOrBuy)
{
SellOrBuy = true;
}
}
}
}
pick
20 Oct 2022, 18:12
First of all, you need to re-assess your SwitchType method:
You are first setting SellOrBuy to false if it is true, then afterwards, if it is false (which it would be if the first operation was ran): you're setting it back to true.
This function can be improved by simply:
For your placement of the button: have you tried lowering the margin? As I understand: it is pushed away from the alignment given - so 300 margin would be 300 units away from the bottom right in both x&y. Try reducing it to 10, then fiddling around with the horizontal and vertical alignment until you understand how it's drawing.
For your button functionality: you should only subscribe to an event once - do this in the initialisation, not in the calculate method.
On top of that, from what I skim-read, you do not appear to be recalculating the indicator values when you change mode.
@pick