Why only buys?

Created at 06 Aug 2023, 21:09
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!
EQ

equip.thyself

Joined 06.08.2023

Why only buys?
06 Aug 2023, 21:09


Thank you for checking this out. For some reason this is only performing BUYs in the Backtesting. Where is the error here?

 

 

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class RSIBotwithMA : Robot
    {
        private Position _position;
        private MovingAverage ma;
        private RelativeStrengthIndex rsi;

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

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

        [Parameter("RSI Overbought", DefaultValue = 70)]
        public int Overbought { get; set; }

        [Parameter("RSI Oversold", DefaultValue = 30)]
        public int Oversold { get; set; }

        [Parameter("MA Period", DefaultValue = 200)]
        public int MAPeriod { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 100, MinValue = 1)]
        public int TP { get; set; }

        [Parameter("Stop Loss", DefaultValue = 100, MinValue = 1)]
        public int SL { get; set; }

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        protected override void OnBar()
        {
            if (Trade.IsExecuting)
                return;

            if (rsi.Result.LastValue < Oversold && (ma.Result.LastValue < MarketSeries.Close.LastValue) && _position == null)
            {
                OpenPosition(TradeType.Buy);
            }

            if (rsi.Result.LastValue > Overbought && (ma.Result.LastValue > MarketSeries.Close.LastValue) && _position == null)
            {
                OpenPosition(TradeType.Sell);
            }
            if (_position != null)
            {
                if (_position.TradeType == TradeType.Buy && rsi.Result.LastValue > Overbought)
                {
                    Trade.Close(_position);
                }

                if (_position.TradeType == TradeType.Sell && rsi.Result.LastValue < Oversold)
                {
                    Trade.Close(_position);
                }
            }
        }

        private void OpenPosition(TradeType command)
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, null, SL, TP);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            _position = null;
        }
    }
}


@equip.thyself
Replies

firemyst
07 Aug 2023, 00:03

Of course it's only doing buys. That's all you tell it to do. :-)

 

   private void OpenPosition(TradeType command)
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, null, SL, TP);
        }

@firemyst

equip.thyself
07 Aug 2023, 14:37

RE: Why only buys?

firemyst said: 

Of course it's only doing buys. That's all you tell it to do. :-)

 

   private void OpenPosition(TradeType command)        {            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, null, SL, TP);        }

Thank you. i changed it now to command and it workds


@equip.thyself