Only trade within certain hours

09 Aug 2018, 01:33Only trade within certain hours#1
alex_mihailposts: 5since: 09 Aug 2018

Can somebody point me in the right direction on how to add code to an already developed bot of mine to only trade between certain hours?

09 Aug 2018, 09:30#2
Panagiotis Charalampousposts: 1484since: 13 Jan 2017

Hi Alex,

Thanks for posting in our forum. However it is not clear what kind of advice do you need. Could you please explain further?

Best Regards,

Panagiotis


Head of Community Management at cTrader
09 Aug 2018, 14:06#3
alex_mihailposts: 5since: 09 Aug 2018

Yes, I'm aware of some code you can implement to a cAlgo bot to have it only trade between specified hours - how would I go about implementing that to a bot I have already written?

09 Aug 2018, 14:12#4
Panagiotis Charalampousposts: 1484since: 13 Jan 2017

Hi Alex,

If you share your code, we might be able to make some suggestions.

Best Regards,

Panagiotis


Head of Community Management at cTrader
09 Aug 2018, 19:49#5
alex_mihailposts: 5since: 09 Aug 2018

Right now I'm using the Sample RSI bot as my template - what code would I add to that to only let it trade within certain hours?

10 Aug 2018, 09:20#6
Panagiotis Charalampousposts: 1484since: 13 Jan 2017

Hi Alex,

See below the modified code that trades between 7 and 10 am.

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    The "Sample RSI cBot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The cBot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {
            if (Server.Time.Hour >= 7 && Server.Time.Hour < 10)
            {
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "SampleRSI");
        }
    }
}

Best Regards,

Panagiotis


Head of Community Management at cTrader
16 Aug 2018, 22:21#7
alex_mihailposts: 5since: 09 Aug 2018

Do you know if this code works in back tests? I can't tell if its working or not to be honest ... perhaps a timezone issue?

17 Aug 2018, 09:47#8
Panagiotis Charalampousposts: 1484since: 13 Jan 2017

Ηι alex_mihail,

You can backtest this and check if positions are openen or closed during those hours in history.

Best Regards,

Panagiotis


Head of Community Management at cTrader