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