Hello,
I keep struggling to create a Cbot that open a market order at a certain time.

using System;
using cAlgo.API;

namespace MyCTraderBot
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MyBot : Robot
    {
        [Parameter("Stop Loss (pips)", DefaultValue = 20)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 40)]
        public int TakeProfitPips { get; set; }

        [Parameter("Lot Size", DefaultValue = 1.0)]
        public double LotSize { get; set; }

        private bool positionOpenedAt9 = false;
        private bool positionOpenedAt17 = false;

        protected override void OnStart()
        {
            // Set up a timer to check for trade opportunities every minute
            Timer.Start(60);
        }

        protected override void OnTimer()
        {
            DateTime serverTime = Server.Time.AddHours(2); // Adjust for UTC+2

            if (!positionOpenedAt9 && serverTime.Hour == 9 && serverTime.Minute == 0)
            {
                OpenPosition();
                positionOpenedAt9 = true;
            }

            if (!positionOpenedAt17 && serverTime.Hour == 17 && serverTime.Minute == 0)
            {
                OpenPosition();
                positionOpenedAt17 = true;
            }
        }

        private void OpenPosition()
        {
            double stopLossPrice = CalculateStopLoss();
            double takeProfitPrice = CalculateTakeProfit();

            // Place the buy order
            ExecuteMarketOrder(TradeType.Buy, SymbolName, LotSize, "MyBotBuy", stopLossPrice, takeProfitPrice);
        }

        private double CalculateStopLoss()
        {
            return Symbol.Bid - StopLossPips * Symbol.PipSize;
        }

        private double CalculateTakeProfit()
        {
            return Symbol.Bid + TakeProfitPips * Symbol.PipSize;
        }
    }
}

The bot open position only for the first day of the period of backtesting and also don't keep the order open until Tp or SL but automaticaly one minute after the open
If someone can help me please,
Appreciate it

Sophia changed the title to How to program trading hours .

First, try explaining your strategy, and what should happen when you start the cBot.

You only stated the cBot should open an order at a set time.

ClickAlgo Limited - Copyright © 2024. All rights reserved.
Privacy Policy | Cookies | Risk Disclosure