How to Make CBot Stop and get back on the next day

Created at 07 Aug 2023, 17:49
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!
Dollar_Joker's avatar

Dollar_Joker

Joined 07.08.2023

How to Make CBot Stop and get back on the next day
07 Aug 2023, 17:49


How to make my cbot stop and wait until next trading day to get back to trade after I lose 3 trades in a row?

I managed to count the last 3 trades and stop, but how do I make it back to trade on the following day?

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//
//    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.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    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 cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewRSI : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

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

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 30)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }

           foreach (var hist in History)
            {
                if (hist.Label == "NewRSI") 
                {
                    if (hist.NetProfit < 0)
                    {
                        losercount++;
                        if (losercount == 3)
                            Stop();
                    }
                    else
                    {
                        losercount = 0;
                    } 
                }
            }
        }

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

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

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "NewRSI");
        }
    }
}


@Dollar_Joker
Replies

firemyst
07 Aug 2023, 23:58

If there's losing trades, you may want to consider leaving it as it is so you have to manually restart it. This does two things:

  1. makes sure your account doesn't blow up if it keeps continuously losing more than 3 trades
  2. gives you a chance to review the trades and what's happening.

 

If you really want it to start again the next day, you can try this:

  1. reset the losing count to 0
  2. keep track of the time. When your new day starts, allow the bot to proceed again. Something like this:

if (!allowTrades && Server.Time > 9am)

{

    allowTrades = true;

}

 

In your OnTick method:

if (allowTrades)

{

 ////rest of method

}

else

return;

 

and you'd have to update your code:

if (losercount == 3)

{

    allowTrades = false;

                            Stop();

}


@firemyst

PanagiotisChar
08 Aug 2023, 05:35

Hi there,

Instead of stopping it, you should use a flag and set it to false. When the flag is set to false, don't trade. On the change of each day, set the flag back to true.

Aieden Technologies

Need help? Join us on Telegram


 

 


@PanagiotisChar