take profit on trendline

Created at 21 Aug 2017, 16:20
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!
swingfish's avatar

swingfish

Joined 25.06.2016

take profit on trendline
21 Aug 2017, 16:20


is it somehow possible to find the price at a trendline is on at the current moment ?

the mission is simple.

draw a tilted trendline .. and once price touches it .. do some action ... like sell half or close trade or whatnot.
the issue is .. how do i find out the price the trendline is at in realtime ? 

 


@swingfish
Replies

Spotware
21 Aug 2017, 16:35

Dear swingfish,

From what we understand, you need to get the current value of an indicator. Please see the following example that prints the value of the Simple Moving Average indicator on each tick

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SamplecBotReferenceSMA : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("SMA Period", DefaultValue = 14)]
        public int SmaPeriod { get; set; }

        private SampleSMA sma;

        protected override void OnStart()
        {
            sma = Indicators.GetIndicator<SampleSMA>(Source, SmaPeriod);
        }

        protected override void OnTick()
        {
            Print("{0}", sma.Result.LastValue);
        }
    }
}

Let us know if the above helps or if you meant something different.

Best Regards,

cTrader Team


@Spotware

swingfish
22 Aug 2017, 05:30

hello .. and thanks for the repluy but no 

i want to Draw a Tilted Line on a chart .. and have calgo find out the price of it in realtime as if the line is tilted the price will change based of the time.

the idea is to make a Algo that takes profit on a certain line.

 


@swingfish

swingfish
22 Aug 2017, 05:34

RE:

Spotware said:

Let us know if the above helps or if you meant something different.

the idea is simple .. draw a Trendline on the chart .. and once the price reached or crossed the line .. Close the trade.
everything straight forward, exept that i could not figure out how i can get the price of a trendline at the current moment in order to compare it to the market-price 

 


@swingfish

Spotware
22 Aug 2017, 14:47

Dear swingfish,

An easy way to achieve this is to construct an ideal trendline in your cBot and check the price on each tick. See below an example of creating such a trendline and getting the price in the OnTick() function. You need to put your trendline's chart coordinates (start price, start time, end price, end time) and the current price is printed in the cBot's log on each tick

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendlinecBot : Robot
    {
        private double _startPrice;
        private double _endPrice;
        private double _slope;
        private DateTime _startDate;
        private DateTime _endDate;
        protected override void OnStart()
        {
            _startPrice = 1.15;
            _endPrice = 1.2;
            _startDate = DateTime.Now;
            _endDate = DateTime.Now.AddHours(1);
            TimeSpan diff = (_endDate - _startDate);
            _slope = (_endPrice - _startPrice) / diff.TotalSeconds;
        }

        protected override void OnTick()
        {
            TimeSpan diff = (DateTime.Now - _startDate);
            var trendlinePrice = _slope * diff.TotalSeconds + _startPrice;
            Print(trendlinePrice);
        }

        protected override void OnStop()
        {

        }
    }
}

Let us know if this solution helps you,

Best Regards,

cTrader Team


@Spotware