trendline stoploss

Created at 23 Jul 2021, 13:23
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!
YA

yaghouti

Joined 19.12.2019

trendline stoploss
23 Jul 2021, 13:23


Hi

how can i draw a trend line on the chart and make it as stop loss (or sometimes as target) , so when price cross it position close.

both trend line and horizontal line is needed.

it would be appreciated if anybody have a sample code to help us.

the most important point is that user can change the position of the line manually on the chart by clicking the line (similar to cTrader native stoploss line (horizontal or trendline)

thanks


@yaghouti
Replies

amusleh
23 Jul 2021, 14:02

Hi,

You can use a chart trend line CalculateY method to check if the price is touched or not, this sample might help you:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class LineAlert : Indicator
    {
        #region Parameters

        [Parameter("Single Alert", DefaultValue = true, Group = "General")]
        public bool SingleAlert { get; set; }

        [Parameter("Trend/Ray", DefaultValue = true, Group = "Line Types")]
        public bool TrendLine { get; set; }

        [Parameter("Horizontal", DefaultValue = true, Group = "Line Types")]
        public bool HorizontalLine { get; set; }

        [Parameter("Vertical", DefaultValue = true, Group = "Line Types")]
        public bool VerticalLine { get; set; }

        [Parameter("Show Comment", DefaultValue = true, Group = "Comment")]
        public bool ShowComment { get; set; }

        [Parameter("Comment Suffix", Group = "Comment")]
        public string CommentSuffix { get; set; }

        #endregion Parameters

        #region Overridden methods

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            foreach (var chartObject in Chart.Objects)
            {
                if (!string.IsNullOrEmpty(CommentSuffix) && !chartObject.Comment.EndsWith(CommentSuffix, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }


				var isAlert = false;
				
                if (chartObject.ObjectType == ChartObjectType.TrendLine || chartObject.ObjectType == ChartObjectType.HorizontalLine)
                {
                    var linePriceValue = double.NaN;

                    if (TrendLine && chartObject.ObjectType == ChartObjectType.TrendLine)
                    {
                        var chartTrendLine = chartObject as ChartTrendLine;

                        if (chartTrendLine != null) linePriceValue = chartTrendLine.CalculateY(index);
                    }
                    else if (HorizontalLine && chartObject.ObjectType == ChartObjectType.HorizontalLine)
                    {
                        var chartHorizontalLine = chartObject as ChartHorizontalLine;

                        if (chartHorizontalLine != null) linePriceValue = chartHorizontalLine.Y;
                    }

                    if (Bars.ClosePrices[index] >= linePriceValue && Bars.ClosePrices[index - 1] < linePriceValue)
                    {
                        isAlert = true;
                    }

                    if (Bars.ClosePrices[index] <= linePriceValue && Bars.ClosePrices[index - 1] > linePriceValue)
                    {
                        isAlert = true;
                    }
                }
                else if (VerticalLine && chartObject.ObjectType == ChartObjectType.VerticalLine)
                {
                    var chartVerticalLine = chartObject as ChartVerticalLine;

                    if (chartVerticalLine != null 
                        && Bars.OpenTimes[index] >= chartVerticalLine.Time 
                        && Bars.OpenTimes[index - 1] < chartVerticalLine.Time)
                    {
                        isAlert = true;
                    }
                }

                if (isAlert)
                {
                    // The code for alert goes here
                }
            }
        }

        #endregion Overridden methods
    }
}

 


@amusleh

yaghouti
23 Jul 2021, 14:18

RE:

Hi 

thanks for reply

i just need one line, which could be change by user on the chart, and in the bot side, we can get it's Y and compare with price, the most important point is that user can click and change the position or angle of the line

amusleh said:

Hi,

You can use a chart trend line CalculateY method to check if the price is touched or not, this sample might help you:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class LineAlert : Indicator
    {
        #region Parameters

        [Parameter("Single Alert", DefaultValue = true, Group = "General")]
        public bool SingleAlert { get; set; }

        [Parameter("Trend/Ray", DefaultValue = true, Group = "Line Types")]
        public bool TrendLine { get; set; }

        [Parameter("Horizontal", DefaultValue = true, Group = "Line Types")]
        public bool HorizontalLine { get; set; }

        [Parameter("Vertical", DefaultValue = true, Group = "Line Types")]
        public bool VerticalLine { get; set; }

        [Parameter("Show Comment", DefaultValue = true, Group = "Comment")]
        public bool ShowComment { get; set; }

        [Parameter("Comment Suffix", Group = "Comment")]
        public string CommentSuffix { get; set; }

        #endregion Parameters

        #region Overridden methods

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            foreach (var chartObject in Chart.Objects)
            {
                if (!string.IsNullOrEmpty(CommentSuffix) && !chartObject.Comment.EndsWith(CommentSuffix, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }


				var isAlert = false;
				
                if (chartObject.ObjectType == ChartObjectType.TrendLine || chartObject.ObjectType == ChartObjectType.HorizontalLine)
                {
                    var linePriceValue = double.NaN;

                    if (TrendLine && chartObject.ObjectType == ChartObjectType.TrendLine)
                    {
                        var chartTrendLine = chartObject as ChartTrendLine;

                        if (chartTrendLine != null) linePriceValue = chartTrendLine.CalculateY(index);
                    }
                    else if (HorizontalLine && chartObject.ObjectType == ChartObjectType.HorizontalLine)
                    {
                        var chartHorizontalLine = chartObject as ChartHorizontalLine;

                        if (chartHorizontalLine != null) linePriceValue = chartHorizontalLine.Y;
                    }

                    if (Bars.ClosePrices[index] >= linePriceValue && Bars.ClosePrices[index - 1] < linePriceValue)
                    {
                        isAlert = true;
                    }

                    if (Bars.ClosePrices[index] <= linePriceValue && Bars.ClosePrices[index - 1] > linePriceValue)
                    {
                        isAlert = true;
                    }
                }
                else if (VerticalLine && chartObject.ObjectType == ChartObjectType.VerticalLine)
                {
                    var chartVerticalLine = chartObject as ChartVerticalLine;

                    if (chartVerticalLine != null 
                        && Bars.OpenTimes[index] >= chartVerticalLine.Time 
                        && Bars.OpenTimes[index - 1] < chartVerticalLine.Time)
                    {
                        isAlert = true;
                    }
                }

                if (isAlert)
                {
                    // The code for alert goes here
                }
            }
        }

        #endregion Overridden methods
    }
}

 

 


@yaghouti

amusleh
23 Jul 2021, 15:24

Hi,

You can draw an interactive chart trend line and then whenever the user changed it you can use the chart objects updated event.


@amusleh

yaghouti
23 Jul 2021, 15:56

RE:

Hi Ahmad
thank you VERY MUCH, it works now

thanks for code.

 

amusleh said:

Hi,

You can draw an interactive chart trend line and then whenever the user changed it you can use the chart objects updated event.

 


@yaghouti

yaghouti
23 Jul 2021, 17:25

RE:

Hi again

just one more problem.

i try to use this linePriceValue in for my stoploss:

position.ModifyStopLossPrice(Math.Round(DefaultSTL,Symbol.Digits)); or position.ModifyStopLossPrice(DefaultSTL);

but unfortunately i get the error:

23/07/2021 18:50:48.947 | → Modifying position PID17057357 (SL: 1.17751257895653, TP: 1.17541361298747) FAILED with error "InvalidStopLossTakeProfit", Position PID17057357

i print the linePriceValue and the printed value is OK but when i try to assign it to my position stoploss i get the error.

 

amusleh said:

Hi,

You can draw an interactive chart trend line and then whenever the user changed it you can use the chart objects updated event.

 


@yaghouti

amusleh
24 Jul 2021, 11:15

RE: RE:

yaghouti said:

Hi again

just one more problem.

i try to use this linePriceValue in for my stoploss:

position.ModifyStopLossPrice(Math.Round(DefaultSTL,Symbol.Digits)); or position.ModifyStopLossPrice(DefaultSTL);

but unfortunately i get the error:

23/07/2021 18:50:48.947 | → Modifying position PID17057357 (SL: 1.17751257895653, TP: 1.17541361298747) FAILED with error "InvalidStopLossTakeProfit", Position PID17057357

i print the linePriceValue and the printed value is OK but when i try to assign it to my position stoploss i get the error.

 

amusleh said:

Hi,

You can draw an interactive chart trend line and then whenever the user changed it you can use the chart objects updated event.

 

Hi,

Can you post the full code of your cBot, please?


@amusleh