Replies

YesOrNot2
25 Nov 2024, 07:57

RE: Probleme with Package

Alleluiaaaaaa !

Thx PanagiotisCharalampous

 

 


@YesOrNot2

YesOrNot2
27 Aug 2024, 05:55 ( Updated at: 27 Aug 2024, 06:36 )

1. The best pass corresponds to the genetic search that gives the best results based on the desired optimization criteria.

2. Click on the column names; you can find the best pass, Sharpe ratio, etc.

3. To get rid of certain criteria, you need to use the get fitness function. You can find an example in this code: https://ctrader.com/algos/cbots/show/3768/


@YesOrNot2

YesOrNot2
26 Aug 2024, 16:53 ( Updated at: 26 Aug 2024, 16:54 )

RE: Create an indicator with data received from a robot.

 

Hi there,

Specifically, the goal is to run an optimization in cTrader, save information on positions, profit, max consecutive wins/losses, etc., and then perform analyses like average RSI, moving averages, etc., to see if, similar to candlestick charts, there’s a possibility of finding correlations in the wins and losses of a strategy.


@YesOrNot2

YesOrNot2
26 Aug 2024, 16:53 ( Updated at: 26 Aug 2024, 16:54 )

RE: Create an indicator with data received from a robot.

 

Hi there,

Specifically, the goal is to run an optimization in cTrader, save information on positions, profit, max consecutive wins/losses, etc., and then perform analyses like average RSI, moving averages, etc., to see if, similar to candlestick charts, there’s a possibility of finding correlations in the wins and losses of a strategy.


@YesOrNot2

YesOrNot2
26 Aug 2024, 16:53

RE: Create an indicator with data received from a robot.

Hi there,

Specifically, the goal is to run an optimization in cTrader, save information on positions, profit, max consecutive wins/losses, etc., and then perform analyses like average RSI, moving averages, etc., to see if, similar to candlestick charts, there’s a possibility of finding correlations in the wins and losses of a strategy.


@YesOrNot2

YesOrNot2
20 Aug 2024, 02:39

Spread v2

Better This :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;
        private Bars barstick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
            barstick = MarketData.GetBars(TimeFrame.Tick);

            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])
                barstick.LoadMoreHistory();
        }

        public override void Calculate(int index)
        {
            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));
            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;
        }

        private int GetIndexByDate(DateTime date)
        {
            var bars = MarketData.GetBars(TimeFrame.Tick);
            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)
            {
                if (bars.OpenTimes[i] <= date)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}

@YesOrNot2

YesOrNot2
20 Aug 2024, 02:39

Spread V2

Better this : 
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;
        private Bars barstick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
            barstick = MarketData.GetBars(TimeFrame.Tick);

            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])
                barstick.LoadMoreHistory();
        }

        public override void Calculate(int index)
        {
            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));
            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;
        }

        private int GetIndexByDate(DateTime date)
        {
            var bars = MarketData.GetBars(TimeFrame.Tick);
            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)
            {
                if (bars.OpenTimes[i] <= date)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}

@YesOrNot2

YesOrNot2
20 Aug 2024, 02:14

11 Years later… ^^

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        private Ticks tick;
        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
        }
        public override void Calculate(int index)
        {
            SpredResult[index] = (tick.Last(0).Ask - tick.Last(0).Bid) / Symbol.PipSize;
        }
    }
}

@YesOrNot2

YesOrNot2
20 Aug 2024, 02:11

Spread Calculation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        [Parameter("FastLimit", DefaultValue = "Tick")]
        public TimeFrame TF1 { get; set; }

        [Output("AskResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "Lime", Thickness = 1)]
        public IndicatorDataSeries AskResult { get; set; }
        [Output("BidResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries BidResult { get; set; }
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
        }

        public override void Calculate(int index)
        {
            //var trying = (tick.Last(0).Ask - tick.Last(0).Bid) * Symbol.PipSize;

            //AskResult[index] = tick.Last(0).Ask;
            //BidResult[index] = tick.Last(0).Bid;
            SpredResult[index] = (tick.Last(0).Ask - tick.Last(0).Bid) / Symbol.PipSize;

        }
    }
}


@YesOrNot2

YesOrNot2
02 Aug 2024, 13:35

Hi, you should know that Chat GPT is really not good for Calgo. It can help you sketch a draft, but you constantly have to go over it afterward.

So, I recommend that after your GPT draft, you do the following:

  • Go to the algo section
  • Click on your code (new or existing code)
  • Once on this page, press Ctrl + E
  • In the window that opens, you can write the names of your indicators, and typical examples are displayed directly. For example, they would have helped you understand the errors in your code.

Here is your code : 

using cAlgo.API;
using cAlgo.API.Indicators;

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

    {
        [Parameter("Bollinger Bands Length", DefaultValue = 20)]
        public int BBLength { get; set; }
        [Parameter("Bollinger Bands Multiplier", DefaultValue = 2.0)]
        public double BBMultiplier { get; set; }
        [Parameter("Supertrend ATR Length", DefaultValue = 8)]
        public int ATRLength { get; set; }
        [Parameter("Supertrend Factor", DefaultValue = 3.0)]
        public double SupertrendFactor { get; set; }
        [Parameter("Stop Loss Percent", DefaultValue = 0.009)]
        public double StopLossPercent { get; set; }
        [Parameter("Take Profit Percent", DefaultValue = 0.00095)]
        public double TakeProfitPercent { get; set; }
        private BollingerBands _bollingerBands;
        private SuperTrend _supertrend;

        protected override void OnStart()
        {
            _bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BBLength, BBMultiplier, MovingAverageType.Simple); _supertrend = Indicators.SuperTrend(ATRLength, SupertrendFactor);
        }

        protected override void OnBar()
        {
            var lowerBB = _bollingerBands.Lower.LastValue; 
            var upperBB = _bollingerBands.Upper.LastValue; 
            var close = MarketSeries.Close.LastValue; 
            var direction = _supertrend.UpSeries.LastValue > 0 ? 1 : -1;
            
            //Handelslogik 
            if (close > lowerBB && direction > 0)
            {
                double stopLossPrice = close * (1 - StopLossPercent);
                double takeProfitPrice = close * (1 + TakeProfitPercent);
                double volume = Symbol.VolumeInUnitsMin;
                // Long  Position 
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Long", stopLossPrice, takeProfitPrice);
            }
            else if (close < upperBB && direction < 0)
            {
                double stopLossPrice = close * (1 + StopLossPercent);
                double takeProfitPrice = close * (1 - TakeProfitPercent);
                double volume = Symbol.VolumeInUnitsMin;
                // Short Position 
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Short", stopLossPrice, takeProfitPrice);
            }
        }
    }
}
  • SuperTrend is underlined, so it’s an error.
  • _bollingerBands.Lower.LastValue, so it’s an error.
  • _bollingerBands.Lower.Upper, so it’s an error.

Here is the perfect road to debug your simple GPT programme:

  1. Automate section
  2. click on your code or new button
  3. ctrl + e
  4. In the window that opens in the search section, type Supertrend and click on it.

Here is what we can find : 

using cAlgo.API;
 using cAlgo.API.Indicators;
 namespace cAlgo.Robots
 {
     // This sample cBot shows how to use the Supertrend indicator
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class SupertrendSample : Robot
     {
         private double _volumeInUnits;
         private Supertrend _supertrend;
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
         public double StopLossInPips { get; set; }
         [Parameter("Take Profit (Pips)", DefaultValue = 10)]
         public double TakeProfitInPips { get; set; }
         [Parameter("Label", DefaultValue = "Sample")]
         public string Label { get; set; }
         public Position[] BotPositions
         {
             get
             {
                 return Positions.FindAll(Label);
             }
         }
         protected override void OnStart()
         {
             _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
             _supertrend = Indicators.Supertrend(10, 3);
         }
         protected override void OnBar()
         {
             if (_supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1) && _supertrend.DownTrend.Last(2) > Bars.HighPrices.Last(2))
             {
                 ClosePositions(TradeType.Sell);
                 ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
             }
             else if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1) && _supertrend.UpTrend.Last(2) < Bars.LowPrices.Last(2))
             {
                 ClosePositions(TradeType.Buy);
                 ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
             }
         }
         private void ClosePositions(TradeType tradeType)
         {
             foreach (var position in BotPositions)
             {
                 if (position.TradeType != tradeType) continue;
                 ClosePosition(position);
             }
         }
     }
 }

 - SUPERTREND: You wrote "SuperTrend". Calgo needs it to be written as "Supertrend".

   5. In the window that opens in the search section, type Bollinger Bands and click on it..

Here is what we can find :

 using cAlgo.API;
 using cAlgo.API.Indicators;
 namespace cAlgo.Robots
 {
     /// 
     /// This sample cBot shows how to use the Bollinger Bands indicator
     /// 
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class BollingerBandsSample : Robot
     {
         private double _volumeInUnits;
         private BollingerBands _bollingerBands;
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Label", DefaultValue = "Sample")]
         public string Label { get; set; }
         [Parameter("Source")]
         public DataSeries Source { get; set; }
         public Position[] BotPositions
         {
             get
             {
                 return Positions.FindAll(Label);
             }
         }
         protected override void OnStart()
         {
             _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
             _bollingerBands = Indicators.BollingerBands(Source, 14, 2, MovingAverageType.Exponential);
         }
         protected override void OnBar()
         {
             if (Bars.LowPrices.Last(1) <= _bollingerBands.Bottom.Last(1) && Bars.LowPrices.Last(2) > _bollingerBands.Bottom.Last(2))
             {
                 ClosePositions(TradeType.Sell);
                 ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label);
             }
             else if (Bars.HighPrices.Last(1) >= _bollingerBands.Top.Last(1) && Bars.HighPrices.Last(2) < _bollingerBands.Top.Last(2))
             {
                 ClosePositions(TradeType.Buy);
                 ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label);
             }
         }
         private void ClosePositions(TradeType tradeType)
         {
             foreach (var position in BotPositions)
             {
                 if (position.TradeType != tradeType) continue;
                 ClosePosition(position);
             }
         }
     }
 }

BOLLINGER BANDS: 

You wrote "bollingerBands.Lower". Calgo needs it to be written as "bollingerBands.Bottom".

You wrote "bollingerBands.Upper". Calgo needs it to be written as "bollingerBands.Top".

 

Now that you have the method to debug very simple code of GPT for next time.

Here is your wanted code : 

using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BollingerSupertrendAlgo : Robot

    {
        [Parameter("Bollinger Bands Length", DefaultValue = 20)]
        public int BBLength { get; set; }
        [Parameter("Bollinger Bands Multiplier", DefaultValue = 2.0)]
        public double BBMultiplier { get; set; }
        [Parameter("Supertrend ATR Length", DefaultValue = 8)]
        public int ATRLength { get; set; }
        [Parameter("Supertrend Factor", DefaultValue = 3.0)]
        public double SupertrendFactor { get; set; }
        [Parameter("Stop Loss Percent", DefaultValue = 0.009)]
        public double StopLossPercent { get; set; }
        [Parameter("Take Profit Percent", DefaultValue = 0.00095)]
        public double TakeProfitPercent { get; set; }
        private BollingerBands _bollingerBands;
        private Supertrend _supertrend;

        protected override void OnStart()
        {
            _bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BBLength, BBMultiplier, MovingAverageType.Simple);
            _supertrend = Indicators.Supertrend(ATRLength, SupertrendFactor);
        }

        protected override void OnBar()
        {
            var lowerBB = _bollingerBands.Bottom.LastValue;
            var upperBB = _bollingerBands.Top.LastValue;
            var close = MarketSeries.Close.LastValue;
            var direction = _supertrend.UpTrend.LastValue > 0 ? 1 : -1;

            //Handelslogik 

            if (close > lowerBB && direction > 0)
            {
                double stopLossPrice = close * (1 - StopLossPercent);
                double takeProfitPrice = close * (1 + TakeProfitPercent);
                double volume = Symbol.VolumeInUnitsMin;
                // Long Position 
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Long", stopLossPrice, takeProfitPrice);
            }
            else if (close < upperBB && direction < 0)
            {
                double stopLossPrice = close * (1 + StopLossPercent);
                double takeProfitPrice = close * (1 - TakeProfitPercent);
                double volume = Symbol.VolumeInUnitsMin;
                // Short Position 
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Short", stopLossPrice, takeProfitPrice);
            }
        }
    }
}

You welcome. =)

 


@YesOrNot2

YesOrNot2
23 Jul 2024, 14:47 ( Updated at: 24 Jul 2024, 06:21 )

RE: RE: Custom indicator with TimeFrame.Hour4 not showing Last(1) value in cBot.

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

This happens because you only assign values to indicator data series, when the index of each higher timeframe changes. Hence for the rest of the array elements the value stays NaN. The solution to this depends on what you want to achieve. For example, why do you 

@PanagiotisCharalampous

Thank you for your reply. I want to test my GMMA strategy with multi-timeframe.

My cBot staretegy is 1hour resolution and should be able to access to the Last(n) values of above custom indicator(multi-timeframe GMMA)'s 

TimeFrame.Hour2 ,TimeFrame.Hour4, TimeFrame.Hour8 ,TimeFrame.Hour16, TimeFrame.Daily, TimeFrame.Weekly.

Can you show me an example on how to code this correctly?

I hope that help you : Loading Bars for all Timerframe you need. (I don't try your probleme)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{

    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HeikinAshi : Indicator
    {
        [Output("Result", LineColor = "White")]
        public IndicatorDataSeries Result { get; set; }

        private IndicatorDataSeries[] Open, Close, High, Low;
        private Bars[] barsTF;
        private int[] indexTF, indexTF2;

        protected override void Initialize()
        {
            barsTF = new Bars[19];
            barsTF[0] = MarketData.GetBars(TimeFrame.Renko1);
            barsTF[1] = MarketData.GetBars(TimeFrame.Renko2);
            barsTF[2] = MarketData.GetBars(TimeFrame.Renko3);
            barsTF[3] = MarketData.GetBars(TimeFrame.Renko4);
            barsTF[4] = MarketData.GetBars(TimeFrame.Renko5);
            barsTF[5] = MarketData.GetBars(TimeFrame.Renko6);
            barsTF[6] = MarketData.GetBars(TimeFrame.Renko7);
            barsTF[7] = MarketData.GetBars(TimeFrame.Renko8);
            barsTF[8] = MarketData.GetBars(TimeFrame.Renko9);
            barsTF[9] = MarketData.GetBars(TimeFrame.Renko10);
            barsTF[10] = MarketData.GetBars(TimeFrame.Renko15);
            barsTF[11] = MarketData.GetBars(TimeFrame.Renko20);
            barsTF[12] = MarketData.GetBars(TimeFrame.Renko25);
            barsTF[13] = MarketData.GetBars(TimeFrame.Renko30);
            barsTF[14] = MarketData.GetBars(TimeFrame.Renko35);
            barsTF[15] = MarketData.GetBars(TimeFrame.Renko40);
            barsTF[16] = MarketData.GetBars(TimeFrame.Renko45);
            barsTF[17] = MarketData.GetBars(TimeFrame.Renko50);
            barsTF[18] = MarketData.GetBars(TimeFrame.Renko100);
            /*barsTF[19] = MarketData.GetBars(TimeFrame.Renko150);
            barsTF[20] = MarketData.GetBars(TimeFrame.Renko200);
            barsTF[21] = MarketData.GetBars(TimeFrame.Renko300);
            barsTF[22] = MarketData.GetBars(TimeFrame.Renko500);
            barsTF[23] = MarketData.GetBars(TimeFrame.Renko800);
            barsTF[24] = MarketData.GetBars(TimeFrame.Renko1000);
            barsTF[25] = MarketData.GetBars(TimeFrame.Renko2000);*/

            indexTF = new int[19];
            indexTF2 = new int[19];

            Open = new IndicatorDataSeries[19];
            Close = new IndicatorDataSeries[19];
            High = new IndicatorDataSeries[19];
            Low = new IndicatorDataSeries[19];

            for (int i = 0; i < 19; i++)
            {
                if (!IsBacktesting)
                {

                    while (barsTF[i].OpenTimes[0] > Bars.OpenTimes[0])
                        barsTF[i].LoadMoreHistory();
                }
                
                Open[i] = CreateDataSeries();
                Close[i] = CreateDataSeries();
                High[i] = CreateDataSeries();
                Low[i] = CreateDataSeries();
            }
        }

        public override void Calculate(int index)
        {
            if (index == 0)
                return;

            var up = 0.0;
            var down = 0.0;

            for (int i = 0; i < 19; i++)
            {
                indexTF[i] = barsTF[i].OpenTimes.GetIndexByTime(Bars.OpenTimes.Last(0));

                Open[i][index] = barsTF[i].OpenPrices[indexTF[i] - 1];
                Close[i][index] = barsTF[i].ClosePrices[indexTF[i] - 1];
                High[i][index] = barsTF[i].HighPrices[indexTF[i] - 1];
                Low[i][index] = barsTF[i].LowPrices[indexTF[i] - 1];

                up += Open[i][index] == Low[i][index] ? 2 : Close[i][index] > Open[i][index] ? 1 : 0;
                down += Open[i][index] == High[i][index] ? -2 : Close[i][index] < Open[i][index] ? -1 : 0;
            }

            Result[index] = up + down;

        }
    }
}

@YesOrNot2