Only trade within certain hours

Created at 09 Aug 2018, 01:33
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!
AL

alex_mihail

Joined 09.08.2018

Only trade within certain hours
09 Aug 2018, 01:33


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?


@alex_mihail
Replies

PanagiotisCharalampous
09 Aug 2018, 09:30

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


@PanagiotisCharalampous

alex_mihail
09 Aug 2018, 14:06

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?


@alex_mihail

PanagiotisCharalampous
09 Aug 2018, 14:12

Hi Alex,

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

alex_mihail
09 Aug 2018, 19:49

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?


@alex_mihail

PanagiotisCharalampous
10 Aug 2018, 09:20

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


@PanagiotisCharalampous

alex_mihail
16 Aug 2018, 22:21

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?


@alex_mihail

PanagiotisCharalampous
17 Aug 2018, 09:47

Ηι alex_mihail,

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

Boring
16 Jul 2022, 00:32 ( Updated at: 16 Jul 2022, 00:33 )

RE: Setting Parameter for Hours

PanagiotisCharalampous said:

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

This works, thanks!

But is there anyway to add an input field (parameter) to change the start and stop hour like there is a field to change the RSI period and source? For now, I have to go into the code and manually set the hours I want to backtest.

 


@Boring

PanagiotisCharalampous
18 Jul 2022, 08:17

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------
//
//    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; }

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

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (Server.Time.Hour >= From && Server.Time.Hour < To)
            {
                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 

Join us on Telegram and Facebook


@PanagiotisCharalampous

Boring
18 Jul 2022, 09:22 ( Updated at: 18 Jul 2022, 09:23 )

RE: thanks!

PanagiotisCharalampous said:

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------
//
//    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; }

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

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (Server.Time.Hour >= From && Server.Time.Hour < To)
            {
                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 

Join us on Telegram and Facebook

Thanks a lot!

Looking at the code, that was so simple, lol!


@Boring

paolo.panicali
28 Jul 2022, 14:53 ( Updated at: 21 Dec 2023, 09:22 )

Maybe you want an indicator, more reusable and with some minor changes can work ok every timeframe perfectly

Hi, I made an indicator for this because trading the australian session from 21 to 6 central european time the < and > caused me troubles.

Just add a reference to your bot and if Hourok.Result.LastValue is 1 you can place your trade.

If you want to use it on timeframes < 1 hour you will have to do some minor changes adding minutes filtering, if you want to use it for timeframes >1 hour you have to decode bar time to and index(1 to 6 for 4hours timeframe) otherwise you will be missing lot of bars( 4h bars starting time changes over time....).

Ctrader , Indicator and bot on the same timezone, this is central european. Remember that if the timezone has a daylight saving time in order to show correctly the time you have to change ctrader time(we are on summer and you wanna check the indicator on winter you will have to change ctrader time)

// TIME TO TRADE INDICATOR by Paolo Panicali 2022

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.CentralEuropeStandardTime, AccessRights = AccessRights.None)]
    public class Hourok : Indicator
    {
        [Parameter("Start Hour", Group = "Time To Trade", DefaultValue = 0, MinValue = 0, MaxValue = 23)]
        public int StartHour { get; set; }

        [Parameter("End Hour", Group = "Time To Trade", DefaultValue = 23, MinValue = 0, MaxValue = 23)]
        public int EndHour { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        DateTime StartTime, EndTime;
        bool HourOk;

        public override void Calculate(int index)
        {
            DateTime DT, DC;
            DC = Bars.OpenTimes[index];
            HourOk = false;

            //IF START AND END HOUR ARE EQUAL THE RESULT IS ALWAYS TRUE
            if (StartHour == DC.Hour || EndHour == DC.Hour || StartHour == EndHour)
            {
                HourOk = true;
            }
            else
            {
                for (int i = 0; i <= 24; i++)
                {
                    DT = DC.AddHours(-i);

                    if (DT.Hour == EndHour)
                    {
                        break;
                    }


                    if (DT.Hour == StartHour)
                    {
                        StartTime = DT;
                        break;
                    }
                }

                for (int i = 0; i <= 24; i++)
                {
                    DT = DC.AddHours(i);

                    if (DT.Hour == StartHour)
                    {
                        break;
                    }

                    if (DT.Hour == EndHour)
                    {
                        EndTime = DT;
                        break;
                    }
                }
                HourOk = Bars.OpenTimes[index] >= StartTime && Bars.OpenTimes[index] <= EndTime ? true : false;
            }

            Result[index] = HourOk == true ? 1 : 0;
        }
    }
}

 

Hope it will help, before using it on real trading check the code carefully.

Bye.


@paolo.panicali

Boring
14 Oct 2022, 18:56 ( Updated at: 15 Oct 2022, 08:03 )

RE: Setting Parameter for Minutes

PanagiotisCharalampous said:

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------
//
//    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; }

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

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (Server.Time.Hour >= From && Server.Time.Hour < To)
            {
                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 

Join us on Telegram and Facebook

The parameters for the hours work well but is it possible to add "minutes" to the parameters. For example, instead of starting a trade at 14:00, it starts at 14:15 or even 14:18.

Is this possible? All help is highly appreciated!


@Boring

sarvann24
25 Oct 2023, 02:44 ( Updated at: 25 Oct 2023, 05:49 )

RE: Only trade within certain hours

PanagiotisCharalampous said: 

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------////    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; }        [Parameter("From", DefaultValue = 7)]        public int From { get; set; }        [Parameter("To", DefaultValue = 10)]        public int To { get; set; }        private RelativeStrengthIndex rsi;        protected override void OnStart()        {            rsi = Indicators.RelativeStrengthIndex(Source, Periods);        }        protected override void OnTick()        {            if (Server.Time.Hour >= From && Server.Time.Hour < To)            {                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 

Join us on Telegram and Facebook

Hi, can anybody help me how to close all the open position after “ X ” hour

 


@sarvann24

PanagiotisChar
25 Oct 2023, 06:11 ( Updated at: 25 Oct 2023, 06:13 )

RE: RE: Only trade within certain hours

sarvann24 said: 

PanagiotisCharalampous said: 

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------////    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; }        [Parameter("From", DefaultValue = 7)]        public int From { get; set; }        [Parameter("To", DefaultValue = 10)]        public int To { get; set; }        private RelativeStrengthIndex rsi;        protected override void OnStart()        {            rsi = Indicators.RelativeStrengthIndex(Source, Periods);        }        protected override void OnTick()        {            if (Server.Time.Hour >= From && Server.Time.Hour < To)            {                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 

Join us on Telegram and Facebook

Hi, can anybody help me how to close all the open position after “ X ” hour

 

Hi there,

You can write something like this

            if(Server.Time.TimeOfDay > TimeSpan.ParseExact("21:00", "hh\\:mm", null))
            {
                foreach (var position in Positions)
                {
                    position.Close();
                }
            }


@PanagiotisChar

sarvann24
25 Oct 2023, 06:48 ( Updated at: 25 Oct 2023, 10:53 )

RE: RE: RE: Only trade within certain hours

PanagiotisChar said: 

sarvann24 said: 

PanagiotisCharalampous said: 

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------////    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; }        [Parameter("From", DefaultValue = 7)]        public int From { get; set; }        [Parameter("To", DefaultValue = 10)]        public int To { get; set; }        private RelativeStrengthIndex rsi;        protected override void OnStart()        {            rsi = Indicators.RelativeStrengthIndex(Source, Periods);        }        protected override void OnTick()        {            if (Server.Time.Hour >= From && Server.Time.Hour < To)            {                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 

Join us on Telegram and Facebook

Hi, can anybody help me how to close all the open position after “ X ” hour

 

Hi there,

You can write something like this

            if(Server.Time.TimeOfDay > TimeSpan.ParseExact("21:00", "hh\\:mm", null))
            {
                foreach (var position in Positions)
                {
                    position.Close();
                }
            }

Thanks :)


@sarvann24

sarvann24
28 Oct 2023, 06:26 ( Updated at: 29 Oct 2023, 06:15 )

RE: RE: RE: RE: Only trade within certain hours

sarvann24 said: 

PanagiotisChar said: 

sarvann24 said: 

PanagiotisCharalampous said: 

Hi Liquidity,

Here you go

// -------------------------------------------------------------------------------------------------////    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; }        [Parameter("From", DefaultValue = 7)]        public int From { get; set; }        [Parameter("To", DefaultValue = 10)]        public int To { get; set; }        private RelativeStrengthIndex rsi;        protected override void OnStart()        {            rsi = Indicators.RelativeStrengthIndex(Source, Periods);        }        protected override void OnTick()        {            if (Server.Time.Hour >= From && Server.Time.Hour < To)            {                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 

Join us on Telegram and Facebook

Hi, can anybody help me how to close all the open position after “ X ” hour

 

Hi there,

You can write something like this

            if(Server.Time.TimeOfDay > TimeSpan.ParseExact("21:00", "hh\\:mm", null))
            {
                foreach (var position in Positions)
                {
                    position.Close();
                }
            }

Thanks :)

HI, i am new to this ctrader and i don't know how to write the codes, so could you help me to find the highest high of last set of bars and not including the current bar, the code below includes current bar i believe. i want to check whether the current bar close price has crossed previous maximum for set of bars or minimum for set of bars 

      Bars.ClosePrices.Last(1) >   Bars.HighPrices.Maximum(3) 
      Bars.ClosePrices.Last(1) <   Bars.LowPrices.Minimum(3) 

 


@sarvann24