Adding X on MA Crossover to this script?

Created at 01 Oct 2019, 15:03
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AL

alex_mihail

Joined 09.08.2018

Adding X on MA Crossover to this script?
01 Oct 2019, 15:03


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    [Levels(25)]
    public class ADXR : Indicator
    {

        [Parameter(DefaultValue = 14)]
        public int interval { get; set; }

        [Output("ADXR", Color = Colors.Orange)]
        public IndicatorDataSeries adxr { get; set; }

        [Output("DiMinus", Color = Colors.Red)]
        public IndicatorDataSeries diminus { get; set; }

        [Output("DiPlus", Color = Colors.Green)]
        public IndicatorDataSeries diplus { get; set; }

        [Output("Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Green")]
        public IndicatorDataSeries Up { get; set; }

        [Output("Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red")]
        public IndicatorDataSeries Down { get; set; }

        [Output("Neutral", PlotType = PlotType.DiscontinuousLine, LineColor = "Gray")]
        public IndicatorDataSeries Neutral { get; set; }

        private DirectionalMovementSystem _dmi;

        protected override void Initialize()
        {
            _dmi = Indicators.DirectionalMovementSystem(interval);
        }

        public override void Calculate(int index)
        {
            var trend = _dmi.DIPlus.LastValue > _dmi.DIMinus.LastValue ? "Uptrend" : "Downtrend";
            var trend2 = _dmi.ADX.LastValue <= 20 ? "Ranging" : "Ranging";
            var upc = ((_dmi.DIPlus.IsRising()) && (_dmi.ADX.LastValue > 25)) ? Color.Green : Color.Gray;
            var downc = ((_dmi.DIPlus.IsFalling()) && (_dmi.ADX.LastValue > 25)) ? "Downtrend" : "Ranging";

            //adxr[index] = (_dmi.ADX[index] + _dmi.ADX[index - interval]) / 2;
            //diminus[index] = _dmi.DIMinus[index];
            //diplus[index] = _dmi.DIPlus[index];

            // Calculate value at specified index

                if (_dmi.ADX.LastValue > 25 && (_dmi.DIPlus.LastValue > _dmi.DIMinus.LastValue))
                {
                    Up[index - 1] = _dmi.ADX[index - 1];
                    Up[index] = _dmi.ADX[index];

                }

                else if (_dmi.ADX.LastValue > 25 && (_dmi.DIMinus.LastValue > _dmi.DIPlus.LastValue))
                {
                    Down[index - 1] = _dmi.ADX[index - 1];
                    Down[index] = _dmi.ADX[index];

                }

                else
                {
                    Neutral[index - 1] = _dmi.ADX[index - 1];
                    Neutral[index] = _dmi.ADX[index];
                }
        }
    }
}

Here is what I have so far but I would like to draw an X or some sort of marker when the price crosses below or above the MA - anyone got an example I could work off?


@alex_mihail
Replies

firemyst
07 Oct 2019, 03:59

RE:

alex_mihail said:

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    [Levels(25)]
    public class ADXR : Indicator
    {

        [Parameter(DefaultValue = 14)]
        public int interval { get; set; }

        [Output("ADXR", Color = Colors.Orange)]
        public IndicatorDataSeries adxr { get; set; }

        [Output("DiMinus", Color = Colors.Red)]
        public IndicatorDataSeries diminus { get; set; }

        [Output("DiPlus", Color = Colors.Green)]
        public IndicatorDataSeries diplus { get; set; }

        [Output("Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Green")]
        public IndicatorDataSeries Up { get; set; }

        [Output("Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red")]
        public IndicatorDataSeries Down { get; set; }

        [Output("Neutral", PlotType = PlotType.DiscontinuousLine, LineColor = "Gray")]
        public IndicatorDataSeries Neutral { get; set; }

        private DirectionalMovementSystem _dmi;

        protected override void Initialize()
        {
            _dmi = Indicators.DirectionalMovementSystem(interval);
        }

        public override void Calculate(int index)
        {
            var trend = _dmi.DIPlus.LastValue > _dmi.DIMinus.LastValue ? "Uptrend" : "Downtrend";
            var trend2 = _dmi.ADX.LastValue <= 20 ? "Ranging" : "Ranging";
            var upc = ((_dmi.DIPlus.IsRising()) && (_dmi.ADX.LastValue > 25)) ? Color.Green : Color.Gray;
            var downc = ((_dmi.DIPlus.IsFalling()) && (_dmi.ADX.LastValue > 25)) ? "Downtrend" : "Ranging";

            //adxr[index] = (_dmi.ADX[index] + _dmi.ADX[index - interval]) / 2;
            //diminus[index] = _dmi.DIMinus[index];
            //diplus[index] = _dmi.DIPlus[index];

            // Calculate value at specified index

                if (_dmi.ADX.LastValue > 25 && (_dmi.DIPlus.LastValue > _dmi.DIMinus.LastValue))
                {
                    Up[index - 1] = _dmi.ADX[index - 1];
                    Up[index] = _dmi.ADX[index];

                }

                else if (_dmi.ADX.LastValue > 25 && (_dmi.DIMinus.LastValue > _dmi.DIPlus.LastValue))
                {
                    Down[index - 1] = _dmi.ADX[index - 1];
                    Down[index] = _dmi.ADX[index];

                }

                else
                {
                    Neutral[index - 1] = _dmi.ADX[index - 1];
                    Neutral[index] = _dmi.ADX[index];
                }
        }
    }
}

Here is what I have so far but I would like to draw an X or some sort of marker when the price crosses below or above the MA - anyone got an example I could work off?

If you want to draw text on a chart, Chart.DrawText is your friend:

Example:

Chart.DrawText("X text", "X", MarketSeries.Close.Count - 1, Symbol.Bid, Color.Goldenrod);

You can also search for examples:

https://ctrader.com/search?q=DrawText


@firemyst

alex_mihail
07 Oct 2019, 16:02

RE: RE:

FireMyst said:

 

If you want to draw text on a chart, Chart.DrawText is your friend:

Example:

Chart.DrawText("X text", "X", MarketSeries.Close.Count - 1, Symbol.Bid, Color.Goldenrod);

You can also search for examples:

https://ctrader.com/search?q=DrawText

Not text - I want to draw a shape ("X" for example) on the candlestick when the price crosses over Hull MA.


@alex_mihail

firemyst
06 Nov 2019, 02:28

RE: RE: RE:

alex_mihail said:

FireMyst said:

 

If you want to draw text on a chart, Chart.DrawText is your friend:

Example:

Chart.DrawText("X text", "X", MarketSeries.Close.Count - 1, Symbol.Bid, Color.Goldenrod);

You can also search for examples:

https://ctrader.com/search?q=DrawText

Not text - I want to draw a shape ("X" for example) on the candlestick when the price crosses over Hull MA.

Then you have to draw lines. But you'll have to decide how far you want the lines to extend, and how steep ofan angle to form the "x". For instance, you might have to draw one line with the coordinates (x-2, priceCrossPoint + 2) and (x+2, priceCrossPoint - 2). That will draw from top left to bottom right. You'll have to do the same for the other leg.

The DrawText approach is a lot simpler to get started so you can see if you like how it's working. Then from there I would graduate to drawing the lines to form the X.


@firemyst

hackmushroom
23 Nov 2019, 20:42

RE: RE: RE: RE:

FireMyst said:

alex_mihail said:

FireMyst said:

 

If you want to draw text on a chart, Chart.DrawText is your friend:

Example:

Chart.DrawText("X text", "X", MarketSeries.Close.Count - 1, Symbol.Bid, Color.Goldenrod);

You can also search for examples:

https://ctrader.com/search?q=DrawText

Not text - I want to draw a shape ("X" for example) on the candlestick when the price crosses over Hull MA.

Then you have to draw lines. But you'll have to decide how far you want the lines to extend, and how steep ofan angle to form the "x". For instance, you might have to draw one line with the coordinates (x-2, priceCrossPoint + 2) and (x+2, priceCrossPoint - 2). That will draw from top left to bottom right. You'll have to do the same for the other leg.

The DrawText approach is a lot simpler to get started so you can see if you like how it's working. Then from there I would graduate to drawing the lines to form the X.

I know you want to draw an x but using DrawText has the advantage of being simpler and also has horizontal and vertical alignment. You can also us unicode characters to get a proper cross. Just use \u2716 as the text string for example. more example unicode crosses here - https://en.m.wikipedia.org/wiki/X_mark


@hackmushroom

srubtsov
26 Nov 2019, 17:13

RE: RE: RE:

alex_mihail said:

Not text - I want to draw a shape ("X" for example) on the candlestick when the price crosses over Hull MA.

Maybe this will help you

Chart.DrawIcon("uniqueIconName", ChartIconType.Star, barIndex, yPrice, Color.White);

@srubtsov

applusplus
03 Oct 2024, 12:12 ( Updated at: 03 Oct 2024, 13:05 )

You can draw an “X” easily with two arrows. Or just use an arrow by itself.

Just a quick example:

var yPosition = Bars.MedianPrices[index];
IndicatorArea.DrawArrow("XFirstHalf" + index, index, yPosition + Symbol.PipSize, index, yPosition, Color.Red, 2);
IndicatorArea.DrawArrow("XSecondHalf" + index, index, yPosition - Symbol.PipSize, index, yPosition, Color.Red, 2);

@applusplus