close position w trendline

Created at 16 Oct 2021, 19:13
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!
R.

r.stipriaan

Joined 05.02.2021

close position w trendline
16 Oct 2021, 19:13


hi, does anyone know how to automatically close a position after it crosses a trendline?

method i use: 

Chart.DrawTrendLine("TrendLine 30", x1, y1, x2, y2, Color.Blue);

 

thanks in advance!

 


@r.stipriaan
Replies

amusleh
18 Oct 2021, 09:04

Hi,

You can use ChartTrendLine CalculateY method, this will give you the price of trend line based on x axis of chart (bar index or time).

Here is an example:

using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        private ChartTrendLine _trendLine;

        protected override void OnStart()
        {
            _trendLine = Chart.DrawTrendLine("trendline", Chart.FirstVisibleBarIndex, Chart.BottomY, Chart.LastVisibleBarIndex, Chart.TopY, Color.Red);
        }

        protected override void OnTick()
        {
            var trendLinePriceValue = _trendLine.CalculateY(Bars.OpenTimes.LastValue);

            // if price crossd the trend line upward then close the position
            if (Symbol.Bid >= trendLinePriceValue)
            {
                // close position here
            }
        }
    }
}

 


@amusleh

r.stipriaan
19 Oct 2021, 19:50

thanks a lot Amusleh!


@r.stipriaan