Dont know why trades arent being put on, has no problems in log

Created at 18 Apr 2023, 01:56
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!
BJ

bjarman16

Joined 18.04.2023

Dont know why trades arent being put on, has no problems in log
18 Apr 2023, 01:56


Dont know why trades arent being placed this is the algo and the indicator used

 

protected override void OnBar()
{
    // Check if price is in a supply or demand zone
    foreach (var zone in supplyAndDemand.sList)
    {
        // Check if current bar's high price is greater than zone's high price 
        // AND previous bar's high price was lower than zone's high price
        if (Bars.HighPrices.Last(1) > zone.high && Bars.HighPrices.Last(2) <= zone.high)
        {
            // Enter short trade as price entered a supply zone
            ExecuteMarketOrder(TradeType.Sell, Symbol, TradeVolume, Instance, null, null, null, "Supply Zone");
        }
    }

    foreach (var zone in supplyAndDemand.dList)
    {
        // Check if current bar's low price is lower than zone's low price 
        // AND previous bar's low price was higher than zone's low price
        if (Bars.LowPrices.Last(1) < zone.low && Bars.LowPrices.Last(2) >= zone.low)
        {
            // Enter long trade as price entered a demand zone
            ExecuteMarketOrder(TradeType.Buy, Symbol, TradeVolume, Instance, null, null, null, "Demand Zone");
        }
    }
}
 

 

 

 

 

Indicator

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SupplyandDemand : Indicator
    {
        [Parameter("Period", DefaultValue = 5)]
        public int per { get; set; }
        [Parameter("TimeFrame")]
        public TimeFrame tf { get; set; }

        [Parameter("Suplly Color", DefaultValue = "Red")]
        public string sColor { get; set; }
        [Parameter("Demand Color", DefaultValue = "Lime")]
        public string dColor { get; set; }

        [Parameter("Opacity %", DefaultValue = 20)]
        public int opt { get; set; }

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

        private MarketSeries series;
        public List<Supply> sList = new List<Supply>();
        public List<Demand> dList = new List<Demand>();
        private Color AsColor, AdColor;

        protected override void Initialize()
        {
            series = MarketData.GetSeries(tf);
            AsColor = Color.FromArgb((int)(255 * 0.01 * opt), Color.FromName(sColor).R, Color.FromName(sColor).G, Color.FromName(sColor).B);
            AdColor = Color.FromArgb((int)(255 * 0.01 * opt), Color.FromName(dColor).R, Color.FromName(dColor).G, Color.FromName(dColor).B);
        }

        public override void Calculate(int index)
        {
            var index2 = series.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index]);
            var index3 = MarketSeries.OpenTime.GetIndexByExactTime(series.OpenTime[index2 - per]);

            bool s = true;
            bool t = true;

            //SUPPLY

            for (int i = 1; i < per; i++)
            {
                if (series.High[index2 - per + i] > series.High[index2 - per] && s == true)
                {
                    s = false;
                }
            }
            for (int i = 1; i < per; i++)
            {
                if (series.High[index2 - per - i] > series.High[index2 - per] && s == true)
                {
                    s = false;
                }
            }
            if (s == true)
            {
                sList.Add(new Supply(index3, series.High[index2 - per]));
            }

            //DEMAND

            for (int i = 1; i < per; i++)
            {
                if (series.Low[index2 - per + i] < series.Low[index2 - per] && t == true)
                {
                    t = false;
                }

            }
            for (int i = 1; i < per; i++)
            {
                if (series.Low[index2 - per - i] < series.Low[index2 - per] && t == true)
                {
                    t = false;
                }

            }
            if (t == true)
            {
                dList.Add(new Demand(index3, series.Low[index2 - per]));
            }


            if (!IsLastBar)
                return;

            //DRAWING

            foreach (var zone in sList)
            {
                for (int i = zone.index; i < index; i++)
                {
                    if (MarketSeries.High[i] > zone.high)
                    {
                        int fIndex = series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[zone.index]);
                        Chart.DrawRectangle("supply" + zone.index + " " + i, zone.index, Math.Max(series.Close[fIndex], series.Open[fIndex]), i, zone.high, AsColor, 1).IsFilled = true;
                        break;
                    }
                }
            }

            foreach (var zone in dList)
            {
                for (int i = zone.index; i < index; i++)
                {
                    if (MarketSeries.Low[i] < zone.low)
                    {
                        int fIndex = series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[zone.index]);
                        Chart.DrawRectangle("demand" + zone.index + " " + i, zone.index, Math.Min(series.Close[fIndex], series.Open[fIndex]), i, zone.low, AdColor, 1).IsFilled = true;
                        break;
                    }
                }
            }
        }
    }

    public class Supply
    {

        public int index;
        public double high;

        public Supply(int index, double high)
        {
            this.index = index;
            this.high = high;
        }

    }

    public class Demand
    {

        public int index;
        public double low;

        public Demand(int index, double low)
        {
            this.index = index;
            this.low = low;
        }

    }
}


cTrader Automate
@bjarman16
Replies

bjarman16
18 Apr 2023, 01:56

sorry this is the algo

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class SupplyDemandRobot : Robot
    {
        [Parameter("Trade Volume", Group = "Basic Setup", DefaultValue = 10000)]
    public int TradeVolume { get; set; }
    [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        private SupplyandDemand supplyAndDemand;

        protected override void OnStart()
        {
            supplyAndDemand = Indicators.GetIndicator<SupplyandDemand>(5, TimeFrame.Hour, "Red", "Lime", 20);
        }

        protected override void OnBar()
{
    // Check if price is in a supply or demand zone
    foreach (var zone in supplyAndDemand.sList)
    {
        // Check if current bar's high price is greater than zone's high price 
        // AND previous bar's high price was lower than zone's high price
        if (Bars.HighPrices.Last(1) > zone.high && Bars.HighPrices.Last(2) <= zone.high)
        {
            // Enter short trade as price entered a supply zone
            ExecuteMarketOrder(TradeType.Sell, Symbol, TradeVolume, Instance, null, null, null, "Supply Zone");
        }
    }

    foreach (var zone in supplyAndDemand.dList)
    {
        // Check if current bar's low price is lower than zone's low price 
        // AND previous bar's low price was higher than zone's low price
        if (Bars.LowPrices.Last(1) < zone.low && Bars.LowPrices.Last(2) >= zone.low)
        {
            // Enter long trade as price entered a demand zone
            ExecuteMarketOrder(TradeType.Buy, Symbol, TradeVolume, Instance, null, null, null, "Demand Zone");
        }
    }
}

    }
}


@bjarman16

firemyst
18 Apr 2023, 09:33

I see in your code you're not capturing the trade result, which would tell you if it's successful or not.

See example:

 


@firemyst

ctid6002427
19 Apr 2023, 17:02 ( Updated at: 19 Apr 2023, 17:46 )

RE:

bjarman16 said:

sorry this is the algo

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class SupplyDemandRobot : Robot
    {
        [Parameter("Trade Volume", Group = "Basic Setup", DefaultValue = 10000)]
    public int TradeVolume { get; set; }
    [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        private SupplyandDemand supplyAndDemand;

        protected override void OnStart()
        {
            supplyAndDemand = Indicators.GetIndicator<SupplyandDemand>(5, TimeFrame.Hour, "Red", "Lime", 20);
        }

        protected override void OnBar()
{
    // Check if price is in a supply or demand zone
    foreach (var zone in supplyAndDemand.sList)
    {
        // Check if current bar's high price is greater than zone's high price 
        // AND previous bar's high price was lower than zone's high price
        if (Bars.HighPrices.Last(1) > zone.high && Bars.HighPrices.Last(2) <= zone.high)
        {
            // Enter short trade as price entered a supply zone
            ExecuteMarketOrder(TradeType.Sell, Symbol, TradeVolume, Instance, null, null, null, "Supply Zone");
        }
    }

    foreach (var zone in supplyAndDemand.dList)
    {
        // Check if current bar's low price is lower than zone's low price 
        // AND previous bar's low price was higher than zone's low price
        if (Bars.LowPrices.Last(1) < zone.low && Bars.LowPrices.Last(2) >= zone.low)
        {
            // Enter long trade as price entered a demand zone
            ExecuteMarketOrder(TradeType.Buy, Symbol, TradeVolume, Instance, null, null, null, "Demand Zone");
        }
    }
}

    }
}

...


@ctid6002427

jim.tollan
19 Apr 2023, 17:47 ( Updated at: 20 Apr 2023, 11:57 )

RE:

bjarman16 said:

sorry this is the algo

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class SupplyDemandRobot : Robot
    {
        [Parameter("Trade Volume", Group = "Basic Setup", DefaultValue = 10000)]
    public int TradeVolume { get; set; }
    [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        private SupplyandDemand supplyAndDemand;

        protected override void OnStart()
        {
            supplyAndDemand = Indicators.GetIndicator<SupplyandDemand>(5, TimeFrame.Hour, "Red", "Lime", 20);
        }

        protected override void OnBar()
{
    // Check if price is in a supply or demand zone
    foreach (var zone in supplyAndDemand.sList)
    {
        // Check if current bar's high price is greater than zone's high price 
        // AND previous bar's high price was lower than zone's high price
        if (Bars.HighPrices.Last(1) > zone.high && Bars.HighPrices.Last(2) <= zone.high)
        {
            // Enter short trade as price entered a supply zone
            ExecuteMarketOrder(TradeType.Sell, Symbol, TradeVolume, Instance, null, null, null, "Supply Zone");
        }
    }

    foreach (var zone in supplyAndDemand.dList)
    {
        // Check if current bar's low price is lower than zone's low price 
        // AND previous bar's low price was higher than zone's low price
        if (Bars.LowPrices.Last(1) < zone.low && Bars.LowPrices.Last(2) >= zone.low)
        {
            // Enter long trade as price entered a demand zone
            ExecuteMarketOrder(TradeType.Buy, Symbol, TradeVolume, Instance, null, null, null, "Demand Zone");
        }
    }
}

    }
}

Hello there  - I actually took a look at this inside visual studio and noticed that the indicator Calculate() method isn't getting fired (when using the Robot - it's actually fine when using purely as an indicator).

I'm using cTrader v4.7.7 - could there be an issue here?? I tried creating a basic indicator from scratch inside cTrader and wiring it up to your Robot also and it also didn't fire the Calculate()  override. I then went back to basics and created a vanilla custom indicator and robot and referenced the indicator from the new robot. same outcome - Calculate() doesn't get fired.

Can anyone else confirm the same outcome?? - oh and hello everyone, been lurking for a long-time due to my username getting blocked for no apparent reason!! However, big thanks to the cTrader community team, now unblocked and ready for, ermmm responding!! ;).

Happy to attach my vanilla code examples that illustrate the issue when using an indicator inside a robot script using v4.7.7.

anyway -cheers and hello from me.


@jim.tollan

firemyst
16 Dec 2023, 14:45 ( Updated at: 17 Dec 2023, 17:00 )

RE: RE:

jim.tollan said: 

bjarman16 said:

sorry this is the algo

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class SupplyDemandRobot : Robot
    {
        [Parameter("Trade Volume", Group = "Basic Setup", DefaultValue = 10000)]
    public int TradeVolume { get; set; }
    [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        private SupplyandDemand supplyAndDemand;

        protected override void OnStart()
        {
            supplyAndDemand = Indicators.GetIndicator<SupplyandDemand>(5, TimeFrame.Hour, "Red", "Lime", 20);
        }

        protected override void OnBar()
{
    // Check if price is in a supply or demand zone
    foreach (var zone in supplyAndDemand.sList)
    {
        // Check if current bar's high price is greater than zone's high price 
        // AND previous bar's high price was lower than zone's high price
        if (Bars.HighPrices.Last(1) > zone.high && Bars.HighPrices.Last(2) <= zone.high)
        {
            // Enter short trade as price entered a supply zone
            ExecuteMarketOrder(TradeType.Sell, Symbol, TradeVolume, Instance, null, null, null, "Supply Zone");
        }
    }

    foreach (var zone in supplyAndDemand.dList)
    {
        // Check if current bar's low price is lower than zone's low price 
        // AND previous bar's low price was higher than zone's low price
        if (Bars.LowPrices.Last(1) < zone.low && Bars.LowPrices.Last(2) >= zone.low)
        {
            // Enter long trade as price entered a demand zone
            ExecuteMarketOrder(TradeType.Buy, Symbol, TradeVolume, Instance, null, null, null, "Demand Zone");
        }
    }
}

    }
}

Hello there  - I actually took a look at this inside visual studio and noticed that the indicator Calculate() method isn't getting fired (when using the Robot - it's actually fine when using purely as an indicator).

I'm using cTrader v4.7.7 - could there be an issue here?? I tried creating a basic indicator from scratch inside cTrader and wiring it up to your Robot also and it also didn't fire the Calculate()  override. I then went back to basics and created a vanilla custom indicator and robot and referenced the indicator from the new robot. same outcome - Calculate() doesn't get fired.

Can anyone else confirm the same outcome?? - oh and hello everyone, been lurking for a long-time due to my username getting blocked for no apparent reason!! However, big thanks to the cTrader community team, now unblocked and ready for, ermmm responding!! ;).

Happy to attach my vanilla code examples that illustrate the issue when using an indicator inside a robot script using v4.7.7.

anyway -cheers and hello from me.

 

Of course it's not being fired - you aren't calling the indicator in a way to force its values to be calculated. You do this directly by calling the Calculate method, or indirectly by calling methods such as “.LastValue" to get the last value of the indicator, “.Last(1)”, “.Last(2)” (to get the value 2 bars ago), etc etc.

Examples (look at examples 2 & 3):

https://help.ctrader.com/ctrader-automate/references/Collections/DataSeries/IndicatorDataSeries/#examples

https://ctrader.com/forum/calgo-support/36512

 

https://help.ctrader.com/ctrader-automate/articles/for-developers/how-to-use-indicators-in-cbots/

https://ctrader.com/forum/cbot-support/37542

 

 

 


@firemyst