Category Other  Published on 12/05/2021

Open Close Timer

Description

Robot places buy and sell orders at specific time and robot can close them at a specific time too.

Parameters:

  • Open Hour - Hour when order will be placed (your local time)
  • Open Minute - Minute when order will be placed (your local time)
  • Close Hour - Hour when order will be closed (your local time)
  • Close Minute - Minute when order will be closed (your local time)
  • Take Profit - Take Profit in pips for sell order
  • Take Profit2 - Take Profit in pips for buy order
  • Stop Loss - Stop Loss in pips for sell order
  • Stop Loss2 - Stop Loss in pips for buy order
  • Volume - trading volume for sell order
  • Volume2 - trading volume for buy order
  • Seconds before - Seconds Before robot will place Pending Orders

using System;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OpenCloseTimer : Robot
    {
        [Parameter("Open Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int OpenHour { get; set; }

        [Parameter("Open Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int OpenMinute { get; set; }

        [Parameter("Close Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int CloseHour { get; set; }

        [Parameter("Close Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int CloseMinute { get; set; }

        [Parameter("Take Profit", DefaultValue = 50)]
        public int TakeProfit { get; set; }

        [Parameter("Take Profit2", DefaultValue = 50)]
        public int TakeProfit2 { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10)]
        public int StopLoss { get; set; }

        [Parameter("Stop Loss2", DefaultValue = 10)]
        public int StopLoss2 { get; set; }

        [Parameter("Volume", DefaultValue = 1000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Volume2", DefaultValue = 1000, MinValue = 1000)]
        public int Volume2 { get; set; }

        [Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]
        public int SecondsBefore { get; set; }

        private DateTime _triggerTimeInServerTimeZone;

        private DateTime _closeDateTime;

        private const string Label = "Robot";

        protected override void OnStart()
        {
            Timer.Start(1);

            var triggerTimeInLocalTimeZone = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, OpenHour, OpenMinute, 0);
            if (triggerTimeInLocalTimeZone < DateTime.Now)
                triggerTimeInLocalTimeZone = triggerTimeInLocalTimeZone.AddDays(1);
            _triggerTimeInServerTimeZone = TimeZoneInfo.ConvertTime(triggerTimeInLocalTimeZone, TimeZoneInfo.Local, TimeZone);

            _closeDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, CloseHour, CloseMinute, 0);
        }

        protected override void OnTimer()
        {
            var sellOrderTargetPrice = Symbol.Bid;
            ChartObjects.DrawHorizontalLine("sell target", sellOrderTargetPrice, Colors.Red, 1, LineStyle.DotsVeryRare);
            var buyOrderTargetPrice = Symbol.Ask;
            ChartObjects.DrawHorizontalLine("buy target", buyOrderTargetPrice, Colors.Blue, 1, LineStyle.DotsVeryRare);

            if (Server.Time <= _triggerTimeInServerTimeZone && (_triggerTimeInServerTimeZone - Server.Time).TotalSeconds <= SecondsBefore)
            {
                PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, StopLoss, TakeProfit);
                PlaceStopOrder(TradeType.Buy, Symbol, Volume2, buyOrderTargetPrice, Label, StopLoss2, TakeProfit2);
            }
        }

        protected override void OnTick()
        {
            if (DateTime.Now > _closeDateTime)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                    Stop();
                }
            }

        }

        protected override void OnStop()
        {
            //Put your deinitialization logic here
        }

    }
}


HE
hejnekem

Joined on 10.05.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Open Close Timer.algo
  • Rating: 5
  • Installs: 1680
Comments
Log in to add a comment.
JE
jerzykisiel.fx · 3 weeks ago

Does anyone know why this cbot is not giving results in the backtests? I read somewhere that the problem is the use of time parameters. Anyone have any idea how to change the code to make testing possible?

AL
alinar7482 · 3 years ago

Hi
Thanks for your good robot
It works great in live
But it does not work in the backtesting section and does not do any trading
Please update
Thanks