c bot stops running

Created at 16 Feb 2021, 01:18
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!
R.

r.stipriaan

Joined 05.02.2021

c bot stops running
16 Feb 2021, 01:18


Hi everyone, 

I have a question,

I just have added a command to my bot to close a position when the RSI indicator is below 30.

the command works, only every time the RSI reaches 30 the bot is stopped.

I would like to hear from someone how I can make sure that the bot does not stop running.

 

thank you in advance.

 

Robin

 

bot code:
 

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

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


        private RelativeStrengthIndex rsi;


        protected override void OnStart()
        {

            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }


        protected override void OnBar()
        {

            if (rsi.Result.LastValue > 70)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "Short", 100, 100);
            }

            if (rsi.Result.LastValue < 30)
            {
                ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));                
            }
        }
    }
}



 


@r.stipriaan
Replies

PanagiotisCharalampous
16 Feb 2021, 08:26

Hi r.stipriaan,

Check your log and you will see the exception below

The problem is that you are trying to close a position even if there is no position open.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

r.stipriaan
16 Feb 2021, 13:15

HI Panagiots, 

Thank you so much for answering my question. 

 

Robin 


@r.stipriaan

r.stipriaan
16 Feb 2021, 23:02 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi r.stipriaan,

Check your log and you will see the exception below

The problem is that you are trying to close a position even if there is no position open.

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

 

Thanks for the quick reply!

 

The solution was more difficult than it seemed, after a few hours of trying still no results. Can you tell what is going wrong from the code?

 

The last 2 lines have been added but the bot does not return.

 

Thanks in advance,

 

Robin

 


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

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


        private RelativeStrengthIndex rsi;


        protected override void OnStart()
        {

            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }


        protected override void OnBar()
        {

            if (rsi.Result.LastValue > 70)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "Short", 100, 100);
            }

            if (rsi.Result.LastValue < 30)
            {
                ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
            }
            if ((Positions.Find("Short", SymbolName, TradeType.Sell)) == null)
                return;
        }
    }
}



 

 


@r.stipriaan

PanagiotisCharalampous
17 Feb 2021, 08:17

Hi r.stipriaan,

Here you go

            if ((Positions.Find("Short", SymbolName, TradeType.Sell)) != null && rsi.Result.LastValue < 30)
            {
                ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

r.stipriaan
17 Feb 2021, 16:27

RE: RE:

Thank you sir !!


@r.stipriaan