just one trade at Day

Created at 20 Jan 2021, 03:40
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!
SA

samuel.jus.cornelio

Joined 19.03.2020

just one trade at Day
20 Jan 2021, 03:40


 

Hi (Hi Pani)
how can i write a line of code that causes my bot to make only one trade a day?
 Below is my code lines

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.ESouthAmericaStandardTime, AccessRights = AccessRights.None)]
    public class bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

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


        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 10)]
        public double StopLoss { get; set; }

        [Parameter("Take_Profit", DefaultValue = 100, MinValue = 10)]
        public double TakeProfit { get; set; }


        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }

        [Parameter("Stop Hour", DefaultValue = 12.0)]
        public double StopTime { get; set; }

        private DateTime _startTime;
        private DateTime _stopTime;



        private StochasticOscillator _1hstoch2;

        protected override void OnStart()
        {
            _startTime = Server.Time.Date.AddHours(StartTime);
            _stopTime = Server.Time.Date.AddHours(StopTime);


        }



        protected override void OnTick()
        {



            {


            }







            var day = Server.Time.DayOfWeek;

            var hour1 = MarketData.GetBars(TimeFrame.Hour);

            if (Trade.IsExecuting)
                return;

            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;

            if (!tradeTime)
                return;











            _1hstoch2 = Indicators.StochasticOscillator(hour1, 12, 3, 12, MovingAverageType.Simple);



            if (_1hstoch2.PercentK.LastValue > 80)

                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)
                    if (Positions.Count == 0)



                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);











        }
    }
}


@samuel.jus.cornelio
Replies

PanagiotisCharalampous
20 Jan 2021, 08:20

Hi samuel.jus.cornelio,

It's not one line of code :) You need to record the datetime at which the trade took place and then check if your current Server.Time day has changed before placing another one.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
20 Jan 2021, 11:39

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

It's not one line of code :) You need to record the datetime at which the trade took place and then check if your current Server.Time day has changed before placing another one.

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Pani

could you please give me a practical example of how to do this?


@samuel.jus.cornelio

PanagiotisCharalampous
20 Jan 2021, 12:24

Hi samuel.jus.cornelio,

Here you go

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        DateTime _lastTrade;
        protected override void OnStart()
        {
            _lastTrade = Server.Time.AddDays(-1);
        }

        protected override void OnTick()
        {
            if (_lastTrade.DayOfYear != Server.Time.DayOfYear)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 1000);
                _lastTrade = Server.Time;
            }
        }

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

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous