Replies

travkinsm1
09 Dec 2021, 13:56

Thanks!


@travkinsm1

travkinsm1
22 Sep 2021, 17:40

At the same time, there can be no confusion when calling several indicators of the following type.

ind0.Calculate(Bars.Count - 1);
ind1.Calculate(Bars.Count - 1);
ind2.Calculate(Bars.Count - 1);
ind3.Calculate(Bars.Count - 1);
ind4.Calculate(Bars.Count - 1);
           

Because on backtesting, the variable I call behaves strangely. Very strangely.


@travkinsm1

travkinsm1
21 Sep 2021, 15:05

Thanks a lot. It helped me.


@travkinsm1

travkinsm1
15 Sep 2021, 16:57

RE:

Thanks for the quick response. Do I understand correctly that the Calculate function will be called again?

 


@travkinsm1

travkinsm1
25 Mar 2021, 14:13

Stupid mistake. Indeed, in the indicator, to which all these files are linked in one way or another, the error is in line 60.


@travkinsm1

travkinsm1
16 Mar 2021, 16:22

Error in my CAlgo

I have a problem with the latest version of calgo too. But of a different kind. All robots and indicators that I compiled gave an "} expected" error with the cursor on the 60th line. (I don't know if this is an accurate translation, in Russian it sounds like "ожидалась }")


@travkinsm1

travkinsm1
12 Mar 2021, 12:22 ( Updated at: 21 Dec 2023, 09:22 )

Re5:

Fixed indicator and bot. All the same, the values partially coincide, partially not. (As in the screenshot)


@travkinsm1

travkinsm1
10 Mar 2021, 13:54

Re4:

 Thanks for the answer. I think that in order to localize the error, you don't need to parse the entire code. Look, there is a _isLong variable in the indicator. It returns true if the indicator is at the bottom, and false if the indicator is at the top. And, according to my observations, there is no error here. To make sure of this, it is enough to insert into line 163

Print("Indicator" + Bars.OpenPrices.LastValue + "" + Bars.OpenTimes.LastValue + "" + Period + "" + Weight + "" + _isLong);

Lines 159 through 162 bind _isLong to _myIsLong. In the bot, _myIsLong is bound to dirInd in OnTick, and often is sent to Print() a different value.

As for the time error, it seems to me that this is unlikely to be, because _myIsLong receives LastValue. And I send the ATRMTF_time_History indicator.

Sorry for not thinking about it right away

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ATRMTF_time_History : Indicator
    {
        [Parameter("Period", DefaultValue = 30)]
        public int Period { get; set; }

        [Parameter("Timeframe", DefaultValue = 0.0)]
        public TimeFrame TF { get; set; }

        [Parameter(DefaultValue = 200)]
        public int LoadHistory_for_ind { get; set; }
        [Parameter("Загрузка ист. в индикаторе", DefaultValue = false)]
        public bool LoadIndHist { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private AverageTrueRange atr;
        private MarketSeries series;
        private Bars Bars3;
        public int loaded = 0;
        protected override void Initialize()
        {
            if (LoadIndHist)
            {
                if (loaded == 0)
                {
                    Print("Start");
                    Bars3 = MarketData.GetBars(TF, Symbol.Name);
                    // Print("2");
                    Print("{0} bar on the chart. Loading 2.000.000 bars", Bars3.Count);
                    //Print("3");

                    while (Bars3.Count < LoadHistory_for_ind)
                    {
                        //  Print("3");
                        var loadedCount = Bars3.LoadMoreHistory();
                        //Print("3");
                        //Print("Loaded {0} bars", loadedCount);
                        //Print("Total bars {0}", Bars2.Count);
                        if (loadedCount == 0)
                            break;
                    }
                    Print("Finished, total bars {0}", Bars3.Count);

                    Print("2");
                    loaded = 1;
                }
            }

            series = MarketData.GetSeries(TF);
            atr = Indicators.AverageTrueRange(series, Period, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            var index1 = series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
            // Print(index1, " ", Result[index - 1], " ", atr.Result[index1]);
            if (Result[index - 1] != atr.Result[index1])
                Result[index] = atr.Result[index1];
            else
                Result[index] = Result[index - 1];
            // (int)Math.Round(minJamp * Symbol.PipSize / Math.Pow(0.1, Symbol.Digits));
        }
    }
}
//ATRMTF_time_History

 


@travkinsm1

travkinsm1
08 Mar 2021, 18:06

Re3:

members of the forum, and first of all amusleh, have any thoughts about my problem?


@travkinsm1

travkinsm1
04 Mar 2021, 16:22 ( Updated at: 04 Mar 2021, 16:27 )

RE: RE:

 

Perhaps this is because I am using an array of indicators, but when I corrected the code as follows, nothing changed.

using System;
using System.Linq;
using System.Windows;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MycBot : Robot
    {
        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }


        [Parameter("True:High_Low False:Close", DefaultValue = false)]
        public bool UseHighAndLow { get; set; }

        [Parameter("Timeframe1", DefaultValue = "Ticks10")]
        public TimeFrame TF { get; set; }

        [Parameter("Загрузить Баров", DefaultValue = 30000)]
        public int BarCount_set { get; set; }

        [Parameter("Загрузка ист. в индикаторе", DefaultValue = false)]
        public bool LoadIndHist { get; set; }

        private Int32 weightCount = 3;
        private double weightMin = 2, weightStep = 1;
        private Int32 perCount = 3;
        private Int32 perMin = 10, perStep = 5;
        private Int32 arPeriod, arWeight;

        private ATR_Stops_time_MarketData_History[,] stop_ind;

        private Bars Bars2;
        private StreamWriter file;
        private int LoadHistory_for_ind = 3000;
        private int period;
        private double weight;
        private bool dirInd;

        protected override void OnStart()
        {
            while (Bars.Count < BarCount_set)
            {
                var loadedCount = Bars.LoadMoreHistory();
                Print("Loaded {0} bars", loadedCount);
                Print("Total bars {0}", Bars.Count);
                if (loadedCount == 0)
                    break;
            }
            Print("Finished, total bars {0}", Bars.Count);
            Bars2 = MarketData.GetBars(TF, Symbol.Name);
            while (Bars2.Count < (BarCount_set / 10))
            {
                var loadedCount = Bars2.LoadMoreHistory();
                if (loadedCount == 0)
                    break;
            }
            stop_ind = new ATR_Stops_time_MarketData_History[perCount, weightCount];
            for (int z2 = 0; z2 < perCount; z2++)
                for (int z3 = 0; z3 < weightCount; z3++)
                {
                    int Per = perMin + perStep * z3;
                    double Weight = weightMin + weightStep * z2;
                    stop_ind[z2, z3] = Indicators.GetIndicator<ATR_Stops_time_MarketData_History>(TF, MaType, Per, Weight, UseHighAndLow, LoadHistory_for_ind, LoadIndHist);
                }
            Print("Finished, total bars {0}", Bars2.Count);
            file = new StreamWriter("C:\\Users\\travk_000\\Documents\\bot1.3_is_long2.txt", false);
            file.WriteLine("Begin");
            file.Flush();
            file.Close();
            Calc();
            Timer.Start(120);
        }
        protected override void OnTimer()
        {
            Calc();
        }
        protected void Calc()
        {
            var rand = new Random();
            arPeriod = rand.Next(0, perCount - 1);
            period = perMin + arPeriod * perStep;
            arWeight = rand.Next(0, weightCount - 1);
            weight = weightMin + arWeight * weightStep;
            Print("weight=" + weight + " period=" + period);
        }
        protected override void OnTick()
        {
            if (stop_ind[arWeight, arPeriod]._myIsLong.LastValue == 1)
                dirInd = true;
            else
                dirInd = false;
            file = new StreamWriter("C:\\Users\\travk_000\\Documents\\bot1.3_is_long2.txt", true);
            file.WriteLine("Bot " + Bars.OpenPrices.LastValue + " " + Bars.OpenTimes.LastValue + " " + weight + " " + period + " " + dirInd);
            file.Flush();
            file.Close();
            Print("Bot " + Bars.OpenPrices.LastValue + " " + Bars.OpenTimes.LastValue + " " + weight + " " + period + " " + dirInd);
        }
    }
}
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
    [Indicator("ATR Trailing Stop", AutoRescale = false, IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class ATR_Stops_time_MarketData_History : Indicator
    {

        [Parameter("Timeframe", DefaultValue = 0.0)]
        public TimeFrame TF { get; set; }

        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("Period", DefaultValue = 10, MinValue = 2, MaxValue = 50000)]
        public int Period { get; set; }

        [Parameter("Weight", DefaultValue = 4.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = false)]
        public bool UseHighAndLow { get; set; }

        [Parameter(DefaultValue = 16000)]
        public int LoadHistory_for_ind { get; set; }

        [Parameter("Загрузка ист. в индикаторе", DefaultValue = false)]
        public bool LoadIndHist { get; set; }


        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        [Output("My")]
        public IndicatorDataSeries _myIsLong { get; set; }
        private ATRMTF_time_History _atr;
        public bool _isLong;


        private MarketSeries _marketSeries;

        //private AverageTrueRange_Cust_lag[,] stop_ind = new ATRStops[perCount, weightCount];
        private Bars Bars2;
        public StreamWriter file3;

        protected override void Initialize()
        {
            Print("Start");

            Bars2 = MarketData.GetBars(TF, Symbol.Name);
            // Print("2");
            Print("{0} bar on the chart. Loading 2.000.000 bars", Bars2.Count);
            //Print("3");
            if (LoadIndHist == true)
            {
                while (Bars2.Count < LoadHistory_for_ind)
                {
                    //  Print("3");
                    var loadedCount = Bars2.LoadMoreHistory();
                    //Print("3");
                    //Print("Loaded {0} bars", loadedCount);
                    //Print("Total bars {0}", Bars2.Count);
                    if (loadedCount == 0)
                        break;
                }
                Print("Finished, total bars {0}", Bars2.Count);
            }
            /*file3 = new StreamWriter("C:\\Users\\travk_000\\Documents\\bot1.3_is_long.txt", false);
            file3.WriteLine("Indicator Begin");
            file3.Flush();
            file3.Close();
            */
            Print("1");
            // Result = CreateDataSeries();
            //ATR = CreateDataSeries();
            // _marketSeries = MarketData.GetSeries(TF);
            _atr = Indicators.GetIndicator<ATRMTF_time_History>(Period, TF, LoadHistory_for_ind, LoadIndHist);
            // _atr = Indicators.AverageTrueRange(Period, MaType);
            ////Print("_marketSeries : {0}", _marketSeries);
            ////Print("_atr : {0}", _atr);
            this._marketSeries = MarketData.GetSeries(MarketData.GetSymbol(Symbol.Name), TF);

            //else
            //Print("Не загружено {0} баров", BarCount_set);
        }


        public override void Calculate(int index)
        {

            ////Print(index, " ", _atr.Result[index]);

            var currentAtr = Weight * _atr.Result[index];

            if (double.IsNaN(currentAtr))
                return;
            /* Print("_marketSeries.Close {0}", this._marketSeries.Close[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);
            Print("_marketSeries.High {0}", this._marketSeries.High[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);
            Print("_marketSeries.Low {0}", this._marketSeries.Low[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);
            */
            //Print("MarketData..close..IsRising: {0}", MarketData.GetBars(TF, Symbol.Name).ClosePrices.IsRising());
                        /*
            Print("MarketData..close[index] {0}", MarketData.GetBars(TF, Symbol.Name).ClosePrices[index]);
            Print("MarketData..LastValue: {0}", MarketData.GetBars(TF, Symbol.Name).ClosePrices.LastValue);
            Print("MarketData..Last: {0}", MarketData.GetBars(TF, Symbol.Name).ClosePrices.Count);
*/

if (double.IsNaN(Result[index - 1]) && !double.IsNaN(_atr.Result[index - 1]))
            {
                var previousATR = Weight * _atr.Result[index - 1];

                _isLong = MarketSeries.Close.IsRising();

                var previous = UseHighAndLow ? (_isLong ? this._marketSeries.High[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index - 1])] : this._marketSeries.Low[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index - 1])]) : this._marketSeries.Close[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index - 1])];

                Result[index] = _isLong ? previous - previousATR : previous + previousATR;
            }
            else
            {
                var current = this._marketSeries.Close[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])];

                if (_isLong)
                {
                    if (current >= Result[index - 1])
                    {
                        if (UseHighAndLow)
                            current = this._marketSeries.High[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])];
                        Result[index] = Math.Max(Result[index - 1], current - currentAtr);
                    }
                    else
                    {
                        _isLong = false;
                        if (UseHighAndLow)
                            current = this._marketSeries.Low[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])];
                        Result[index] = current + currentAtr;
                    }
                }
                else
                {
                    if (current <= Result[index - 1])
                    {
                        if (UseHighAndLow)
                            current = this._marketSeries.Low[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])];
                        Result[index] = Math.Min(Result[index - 1], current + currentAtr);
                    }
                    else
                    {
                        _isLong = true;
                        if (UseHighAndLow)
                            current = this._marketSeries.High[_marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])];
                        Result[index] = current - currentAtr;
                    }
                }
            }
            if (_isLong)
                _myIsLong[index] = 1;
            else
                _myIsLong[index] = -1;
        }
    }
    /*file3 = new StreamWriter("C:\\Users\\travk_000\\Documents\\bot1.3_is_long.txt", true);
            file3.WriteLine("Indicator " + Bars.OpenPrices.LastValue + " " + Bars.OpenTimes.LastValue + " " + Period + " " + Weight + " " + _isLong);
            file3.Flush();
            file3.Close();*/

}

naturally in my original code weight and period are not random


@travkinsm1

travkinsm1
03 Mar 2021, 15:36

I forgot to say that I launch the robot on the 1 tick timeframe, and the indicator on 10 ticks. The timeframe parameter in the indicator is also 10 ticks


@travkinsm1

travkinsm1
02 May 2020, 14:23

RE:

Thanks

 


@travkinsm1

travkinsm1
10 Apr 2020, 19:35

RE:

Then, to whom you can apply for help.

 


@travkinsm1

travkinsm1
31 Mar 2020, 14:51

What do you think?


@travkinsm1

travkinsm1
24 Mar 2020, 11:48

Sorry, I forgot to say that I am talking about Moscow time, i.e. UTC+3.


@travkinsm1

travkinsm1
23 Mar 2020, 19:10

As I already said, in places the indicator shows the correct values, and in some places, like 04/03/2020 02:02:00 it lies. 1.1184, while the graph is about 1.117. (This is an indicator with parameters period = 60 weight = 6 and a minute chart)


@travkinsm1

travkinsm1
21 Mar 2020, 12:52

tried so.

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 ATRStopbot : Robot
    {
        public DateTime t;
        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

        [Parameter("Period1", DefaultValue = 15, MinValue = 2, MaxValue = 50000)]
        public int Period1 { get; set; }

        [Parameter("Weight1", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight1 { get; set; }

        [Parameter("Period2", DefaultValue = 60, MinValue = 8, MaxValue = 50000)]
        public int Period2 { get; set; }

        [Parameter("Weight2", DefaultValue = 6.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight2 { get; set; }

        [Parameter("Period3", DefaultValue = 240, MinValue = 7, MaxValue = 50000)]
        public int Period3 { get; set; }

        [Parameter("Weight3", DefaultValue = 12.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight3 { get; set; }

        private ATRStops atrS1 = new ATRStops();
        private ATRStops atrS2 = new ATRStops();
        protected override void OnStart()
        {
            t = Bars.LastBar.OpenTime;
            atrS1 = Indicators.GetIndicator<ATRStops>(MaType, Period1, Weight1, UseHighAndLow);
            atrS2 = Indicators.GetIndicator<ATRStops>(MaType, Period2, Weight2, UseHighAndLow);

        }

        protected override void OnTick()
        {
            if (Bars.LastBar.OpenTime > t)
            {
                t = Bars.LastBar.OpenTime;
                Print("atrS1.Result.LastValue=", atrS1.Result[atrS1.Result.Count - 2]);
                Print("atrS2.Result.LastValue=", atrS2.Result[atrS2.Result.Count - 2]);
            }
                                            }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

but this did not solve the problem


@travkinsm1

travkinsm1
19 Mar 2020, 17:52

Anyone have thoughts on my problem?


@travkinsm1

travkinsm1
13 Mar 2020, 17:18

RE:

PanagiotisCharalampous said:

Hi travkinsm1,

Thanks, can you post the indicator as well?

Best Regards,

Panagiotis 

Join us on Telegram

Edited the message. Now it is with an indicator.


@travkinsm1

travkinsm1
11 Mar 2020, 14:38

RE:

PanagiotisCharalampous said:

Hi travkinsm1,

Can we have the indicator and cBot code so that we can reproduce this?

Best Regards,

Panagiotis 

Join us on Telegram

 

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 ATRStopbot : Robot
    {
        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

        [Parameter("Period1", DefaultValue = 15, MinValue = 2, MaxValue = 50000)]
        public int Period1 { get; set; }

        [Parameter("Weight1", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight1 { get; set; }

        [Parameter("Period2", DefaultValue = 60, MinValue = 8, MaxValue = 50000)]
        public int Period2 { get; set; }

        [Parameter("Weight2", DefaultValue = 6.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight2 { get; set; }

        private ATRStops atrS1 = new ATRStops();
        private ATRStops atrS2 = new ATRStops();
        protected override void OnStart()
        {
            var bars = MarketData.GetBars(TimeFrame.Minute, Bars.SymbolName);
            bars.BarOpened += NewBar;
            atrS1 = Indicators.GetIndicator<ATRStops>(MaType, Period1, Weight1, UseHighAndLow);
            atrS2 = Indicators.GetIndicator<ATRStops>(MaType, Period2, Weight2, UseHighAndLow);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }
        private void NewBar(BarOpenedEventArgs obj)
        {
            Print("atrS1.Result.LastValue=", atrS1.Result.LastValue);
            Print("atrS2.Result.LastValue=", atrS2.Result.LastValue);
        }
                Positions[j].Close();
                protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator("ATR Trailing Stop", AutoRescale = false, IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class ATRStops : Indicator
    {

        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("Period", DefaultValue = 15, MinValue = 2, MaxValue = 50000)]
        public int Period { get; set; }

        [Parameter("Weight", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private AverageTrueRange _atr;
        private bool _isLong;

        protected override void Initialize()
        {
            _atr = Indicators.AverageTrueRange(Period, MaType);
        }

        public override void Calculate(int index)
        {
            var currentAtr = Weight * _atr.Result[index];

            if (double.IsNaN(currentAtr))
                return;

            if (double.IsNaN(Result[index - 1]) && !double.IsNaN(_atr.Result[index - 1]))
            {
                var previousATR = Weight * _atr.Result[index - 1];

                _isLong = MarketSeries.Close.IsRising();

                var previous = UseHighAndLow ? (_isLong ? MarketSeries.High[index - 1] : MarketSeries.Low[index - 1]) : MarketSeries.Close[index - 1];

                Result[index] = _isLong ? previous - previousATR : previous + previousATR;
            }
            else
            {
                var current = MarketSeries.Close[index];

                if (_isLong)
                {
                    if (current >= Result[index - 1])
                    {
                        if (UseHighAndLow)
                            current = MarketSeries.High[index];
                        Result[index] = Math.Max(Result[index - 1], current - currentAtr);
                    }
                    else
                    {
                        _isLong = false;
                        if (UseHighAndLow)
                            current = MarketSeries.Low[index];
                        Result[index] = current + currentAtr;
                    }
                }
                else
                {
                    if (current <= Result[index - 1])
                    {
                        if (UseHighAndLow)
                            current = MarketSeries.Low[index];
                        Result[index] = Math.Min(Result[index - 1], current + currentAtr);
                    }
                    else
                    {
                        _isLong = true;
                        if (UseHighAndLow)
                            current = MarketSeries.High[index];
                        Result[index] = current - currentAtr;
                    }
                }
            }
        }
    }
}

 


@travkinsm1