different tf

Created at 12 Oct 2020, 10:39
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!
LU

luca.tocchi

Joined 25.03.2020

different tf
12 Oct 2020, 10:39


hi I would like that to whatever tf the bot works, the zig zag prints the monthly tf values

how can I do?

thanks 

 

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.UTC, AccessRights = AccessRights.None)]
    public class Botdellasvolta : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 0)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 5)]
        public int TakeProfit { get; set; }

        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        private ZigZag zigzag;

        protected override void OnStart()
        {
            MarketSeries data = MarketData.GetSeries(TimeFrame.Monthly);
            zigzag = Indicators.GetIndicator<ZigZag>(Depth, Deviation, BackStep);
        }

        double GetZigZagValue(DataSeries dataSeries, int indexFromEnd)
        {
            for (var i = MarketSeries.Close.Count - 1; i >= 0; i--)
            {
                if (!double.IsNaN(zigzag.Result[i]))
                {
                    if (indexFromEnd == 0)
                        return zigzag.Result[i];
                    indexFromEnd--;
                }
            }
            return double.NaN;
        }

        protected override void OnTick()
        {
            var lastValue = GetZigZagValue(zigzag.Result, 0);
            var previousValue = GetZigZagValue(zigzag.Result, 1);

            Print(lastValue);
            Print(previousValue);
        }
    }
}


@luca.tocchi
Replies

fxtradersystems
12 Oct 2020, 18:03

RE:

luca.tocchi said:

hi I would like that to whatever tf the bot works, the zig zag prints the monthly tf values

how can I do?

thanks 

 

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.UTC, AccessRights = AccessRights.None)]
    public class Botdellasvolta : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 0)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 5)]
        public int TakeProfit { get; set; }

        [Parameter(DefaultValue = 12)]
        public int Depth { get; set; }

        [Parameter(DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int BackStep { get; set; }

        [Output("ZigZag", Color = Colors.OrangeRed)]
        public IndicatorDataSeries Result { get; set; }

        private ZigZag zigzag;

        protected override void OnStart()
        {
            MarketSeries data = MarketData.GetSeries(TimeFrame.Monthly);
            zigzag = Indicators.GetIndicator<ZigZag>(Depth, Deviation, BackStep);
        }

        double GetZigZagValue(DataSeries dataSeries, int indexFromEnd)
        {
            for (var i = MarketSeries.Close.Count - 1; i >= 0; i--)
            {
                if (!double.IsNaN(zigzag.Result[i]))
                {
                    if (indexFromEnd == 0)
                        return zigzag.Result[i];
                    indexFromEnd--;
                }
            }
            return double.NaN;
        }

        protected override void OnTick()
        {
            var lastValue = GetZigZagValue(zigzag.Result, 0);
            var previousValue = GetZigZagValue(zigzag.Result, 1);

            Print(lastValue);
            Print(previousValue);
        }
    }
}

 

This may be a little overcomplicated, but you could modify the ZigZag indicator to take a timeframe parameter as input.

You can then create your own custom bars by using: customBars = MarketData.GetBars(Timeframe);

In OnCalculate() you can then find the corresponding index of the current bar with the custom timeframe bar using:
int customIndex = customBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

You can then have:

Result[index] = function(customIndex);

You have to be careful though if the function relies on the Result (ie. if you're using a moving average of the Result for another output), as it starts to generate an incorrect series.

 

Let us know if you need any further help or development work done @ fxtradersystems.com/support/ :)

Sam

fxtradersystems.com

 


@fxtradersystems