cbot successfully created but does not execute orders when the trigger is triggered

Created at 06 Jun 2023, 14:30
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!
UB

ubiratamoreira.trader

Joined 03.06.2023

cbot successfully created but does not execute orders when the trigger is triggered
06 Jun 2023, 14:30


hey
I'm a beginner in cbot and I'm trying to compile a theoretically simple one, but I'm not able to finalize the idea and create a cbot that executes orders based on the pips of the current candle, ex: if the current candle goes more than 40 pips, trigger buy or sell with trailing stop the amounts of pips / trailing stop and batch configurable I was able to write a script and it was successfully created but plotted on the graph triggers the trigger and the cbot does not execute the order (does nothing) could someone help me to solve the question?
below is the written script

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PipsDeVelas : Robot
    {
        [Parameter("Pips de Compra", DefaultValue = 40)]
        public int CompraPips { get; set; }

        [Parameter("Pips de Venda", DefaultValue = 40)]
        public int VendaPips { get; set; }

        [Parameter("Trailing Stop (Pips)", DefaultValue = 30)]
        public int TrailingStopPips { get; set; }

        [Parameter("Tamanho do Lote", DefaultValue = 0.01)]
        public double LotSize { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.NormalizeVolumeInUnits(LotSize, RoundingMode.ToNearest), "Compra", null, null, null, GetTrailingStop());
            Positions.Closed += OnPositionClosed;
        }

        protected override void OnBar()
        {
            double currentCandleRange = Symbol.Bid - Symbol.Ask;
            double compraPipsValue = CompraPips * Symbol.PipSize;
            double vendaPipsValue = VendaPips * Symbol.PipSize;

            if (currentCandleRange > compraPipsValue)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.NormalizeVolumeInUnits(LotSize, RoundingMode.ToNearest), "Compra", null, null, null, GetTrailingStop());
            }

            if (currentCandleRange > vendaPipsValue)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.NormalizeVolumeInUnits(LotSize, RoundingMode.ToNearest), "Venda", null, null, null, GetTrailingStop());
            }
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            Print("Position closed: " + args.Position.Label);
        }

        private bool GetTrailingStop()
        {
            return true;
        }
    }
}
 


@ubiratamoreira.trader
Replies

PanagiotisChar
06 Jun 2023, 15:13

Hi there,

The problem is here

                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.NormalizeVolumeInUnits(LotSize, RoundingMode.ToNearest), "Compra", null, null, null, GetTrailingStop());

You pass Lots as volume, while you should pass units instead. Convert it using QuantityToVolumeInUnits

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

ubiratamoreira.trader
06 Jun 2023, 17:35

modificado e continua nao abrindo ardens deve haver outro erro nas modificaçoes que fiz

 

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PipsDeVelas : Robot
    {
        [Parameter("Pips de Compra", DefaultValue = 40)]
        public int CompraPips { get; set; }

        [Parameter("Pips de Venda", DefaultValue = 40)]
        public int VendaPips { get; set; }

        [Parameter("Trailing Stop (Pips)", DefaultValue = 30)]
        public int TrailingStopPips { get; set; }

        [Parameter("Tamanho do Lote", DefaultValue = 0.01)]
        public double LotSize { get; set; }

        protected override void OnStart()
        {
            Positions.Closed += OnPositionClosed;
        }

        protected override void OnBar()
        {
            double currentCandleRange = Symbol.Bid - Symbol.Ask;
            double compraPipsValue = CompraPips * Symbol.PipSize;
            double vendaPipsValue = VendaPips * Symbol.PipSize;

            if (currentCandleRange > compraPipsValue)
            {
                double volume = Symbol.QuantityToVolumeInUnits(LotSize);
                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Compra", null, null, null, GetTrailingStop());
            }

            if (currentCandleRange > vendaPipsValue)
            {
                double volume = Symbol.QuantityToVolumeInUnits(LotSize);
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume, "Venda", null, null, null, GetTrailingStop());
            }
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            Print("Position closed: " + args.Position.Label);
        }

        private bool GetTrailingStop()
        {
            return true;
        }
    }
}
 


@ubiratamoreira.trader

PanagiotisChar
07 Jun 2023, 08:29

Hi there,

What is this line supposed to do? This is definitely not the candle range, it's meaningless to me.

double currentCandleRange = Symbol.Bid - Symbol.Ask;

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

ubiratamoreira.trader
07 Jun 2023, 18:32

I have already tried several unsuccessful modifications unfortunately now an error appears in the error log area
follow below modified script
if anyone can help me and correct this error and post here the corrected script i would be grateful

 

07/06/2023 11:50:22.737 | CBot instance [pips de velas, XAUUSD, m1] started.
07/06/2023 11:51:00.458 | Failed to place Market Order Sell XAUUSD 1 with error code InvalidRequest


using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PipsDeVelas : Robot
    {
        [Parameter("Pips de Compra", DefaultValue = 40)]
        public int CompraPips { get; set; }

        [Parameter("Pips de Venda", DefaultValue = 40)]
        public int VendaPips { get; set; }

        [Parameter("Trailing Stop (Pips)", DefaultValue = 30)]
        public int TrailingStopPips { get; set; }

        [Parameter("Tamanho do Lote", DefaultValue = 0.01)]
        public double LotSize { get; set; }

        protected override void OnStart()
        {
            Positions.Closed += OnPositionClosed;
        }

        protected override void OnBar()
        {
            double compraPipsRange = CompraPips * Symbol.PipSize;
            double vendaPipsRange = VendaPips * Symbol.PipSize;

            if (Symbol.Bid - Symbol.Ask > compraPipsRange)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.QuantityToVolumeInUnits(LotSize), "Compra", null, null, null, GetTrailingStop());
            }

            if (Symbol.Ask - Symbol.Bid > vendaPipsRange)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.QuantityToVolumeInUnits(LotSize), "Venda", null, null, null, GetTrailingStop());
            }
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            Print("Position closed: " + args.Position.Label);
        }

        private bool GetTrailingStop()
        {
            return true;
        }
    }
}


@ubiratamoreira.trader

PanagiotisChar
08 Jun 2023, 08:22

Hi there,

Try the following

double currentCandleRange = Math.Abs(Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1));

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar