draw trendline

Created at 01 Aug 2021, 16:26
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

draw trendline
01 Aug 2021, 16:26


 

Hi,

I want to draw a trendline when a position is taken, unfortunately I can't. can someone explain to me how to formulate this command?

thank you in advance!


method:

 


for example: 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.WEuropeStandardTime, AccessRights = AccessRights.None)]
    public class RSmacd : Robot
    {

        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter(DefaultValue = 70)]
        public int RSI_MaxLevel { get; set; }

        [Parameter(DefaultValue = 30)]
        public int RSI_MinLevel { get; set; }

        [Parameter(DefaultValue = 80)]
        public int MF_MaxLevel { get; set; }

        [Parameter(DefaultValue = 20)]
        public int MF_MinLevel { get; set; }

        [Parameter(DefaultValue = 0.1)]
        public double ASI_MaxLevel { get; set; }

        [Parameter(DefaultValue = -0.1)]
        public double ASI_MinLevel { get; set; }

        [Parameter(" Volume Percent", DefaultValue = 15, MinValue = 0)]
        public double VolumePercent { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int MACD_LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int MACD_ShortCycle { get; set; }


        private MacdCrossOver macd;
        private RelativeStrengthIndex rsi;
        private MoneyFlowIndex mf;
        private AccumulativeSwingIndex asi;


        protected override void OnStart()
        {
            
            macd = Indicators.MacdCrossOver(MACD_LongCycle, MACD_ShortCycle, 9);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            mf = Indicators.MoneyFlowIndex(14);
            asi = Indicators.AccumulativeSwingIndex(12);

        }

        protected override void OnTick()
        {
            
            var MACDline = macd.MACD.Last(1);
            var prevMACDline = macd.MACD.Last(2);
            var Signal = macd.Signal.Last(1);
            var PrevSignal = macd.Signal.Last(2);

            var volumne = Math.Floor(Account.Balance * 1 * VolumePercent / 1000) * 1000;

            
            if (MACDline > Signal & prevMACDline < PrevSignal & rsi.Result.LastValue < RSI_MinLevel & mf.Result.LastValue < MF_MinLevel & asi.Result.LastValue < -0.01 & Positions.Count < 1)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volumne, "Long", 150, 150);
                 Chart.DrawTrendLine("trenddraw", Chart.Bars.Last(55).Low, Bars.ClosePrices, Chart.Bars.Last(0).Low, Bars.ClosePrices, "red");
            }  
        }
    }
}


 


@r.stipriaan
Replies

amusleh
02 Aug 2021, 07:55

Hi,

Please check the code example on API references for the Chart trend line.


@amusleh

r.stipriaan
07 Aug 2021, 11:44

RE:

thank you, the link has the following example:

 

        protected override void Initialize()
        {
            var trendLine = Chart.DrawTrendLine("trendLine", Chart.FirstVisibleBarIndex, Bars.LowPrices[Chart.FirstVisibleBarIndex], Chart.LastVisibleBarIndex, Bars.HighPrices[Chart.LastVisibleBarIndex], Color.Red, 2, LineStyle.Dots);
            trendLine.IsInteractive = true;
        }

 

here a trendline is drawn from the first visible point to the last visible point. when I change FirstVisibleBarIndex to bar.last(55) it shows an error saying invalid arguments.

can you please tell me how to enter this correctly?

@r.stipriaan

amusleh
10 Aug 2021, 08:11

Hi,

The Chart.DrawTrendLine method has two overloads, one gets bar index for x axis and the other gets time for x axis.

You can't pass bar.last(55), the argument type must be an int or DateTime, instead pass Bars.Last(55).OpenTime.


@amusleh

r.stipriaan
13 Aug 2021, 22:43 ( Updated at: 13 Aug 2021, 22:45 )

RE:
thanks for your answer Musleh, unfortunately it still doesn't work when I enter Bars.Last(55).OpenTime at the x axis. with the following error: unable to convert from int to System.DateTime
do you know what's going wrong?

for example: 

                        var trendLine = Chart.DrawTrendLine("trendLine",Bars.Last(55).OpenTime , 0.75555, Chart.LastVisibleBarIndex, Bars.HighPrices[Chart.LastVisibleBarIndex], Color.Red, 2, LineStyle.Dots);
                        trendLine.IsInteractive = true;

 


@r.stipriaan