Cbot Script Created but not taking any trades even when the rules are met

Created at 24 Dec 2024, 14:05
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!
78

7835660

Joined 18.12.2024

Cbot Script Created but not taking any trades even when the rules are met
24 Dec 2024, 14:05


I created a script for cBot, but when I add it to the chart or backtest it, it's not taking any trades even when the rules are met. What could the issue be?

 

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

namespace cAlgo 

   [Robot(TimeZone = TimeZones.UTC)] public class RedBarTradingBot : Robot  
   { 
    [Parameter("Lot Size", DefaultValue = 0.1)] public double LotSize { get; set; }
 
 

    [Parameter("EMA Period", DefaultValue = 200)]
    public int EmaPeriod { get; set; }

    private ExponentialMovingAverage ema;
    private bool isPositionOpen = false;

    protected override void OnStart()
    {
        ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EmaPeriod);
    }

    protected override void OnBar()
    {
        // Check for sufficient bars
        if (Bars.Count < EmaPeriod + 1) return;

        int lastBarIndex = Bars.Count - 1;
        int previousBarIndex = lastBarIndex - 1;

        // Check for red bar and EMA condition
        bool isRedBar = Bars.ClosePrices[lastBarIndex] < Bars.OpenPrices[lastBarIndex];
        bool aboveEma = Bars.LowPrices[lastBarIndex] > ema.Result[lastBarIndex];
        bool belowEma = Bars.HighPrices[lastBarIndex] < ema.Result[lastBarIndex];

        // Handle open positions
        if (isPositionOpen)
        {
            if (Positions[0].TradeType == TradeType.Buy && belowEma)
            {
                ClosePosition(Positions[0]);
                isPositionOpen = false;
            }
            else if (Positions[0].TradeType == TradeType.Sell && aboveEma)
            {
                ClosePosition(Positions[0]);
                isPositionOpen = false;
            }
        }
       
        {

            }
            // Check for sell condition
             if (isRedBar && aboveEma)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, LotSize, "RedBarSellOrder");
                isPositionOpen = true;
            }
        }
    }
}

 

thank You


@7835660
Replies

PanagiotisCharalampous
27 Dec 2024, 07:41

Hi there,

Probably your issue is the volume. You are using lots instead of units. ExecuteMarketOrder() takes units as an input and not lots. You can use QuantityToVolume() for the conversion.

Best regards,

Panagiotis


@PanagiotisCharalampous