how to fix Boolean in this cBot

Created at 07 Jun 2022, 07:40
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!
RA

RadoWay

Joined 07.06.2022

how to fix Boolean in this cBot
07 Jun 2022, 07:40


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        #region Parameters
        [Parameter("Volume", DefaultValue = 1)]
        public double Volume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 0)]
        public double SL { get; set; }

        [Parameter("Take Profit", DefaultValue = 0)]
        public double TP { get; set; }

        [Parameter("Period", DefaultValue = 50, Group = "Moving Average")]
        public int MAPeriod { get; set; }

        [Parameter("Type", Group = "Moving Average")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Period", DefaultValue = 15, Group = "Relative Strength")]
        public int RSIPeriod { get; set; }
        #endregion

        #region Private Variables
        RelativeStrengthIndex _rsi;
        MovingAverage _ma;
        #endregion

        #region Methods
        protected override void OnStart()
        {
            // Put your initialization logic here
            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RSIPeriod);
            _ma = Indicators.MovingAverage(Bars.ClosePrices, MAPeriod, MAType);
        }

        protected override void OnBar()
        {
            if (Positions.Count(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy) == 0 && Bars.ClosePrices.Last(1) > _ma.Result.Last(1) && _rsi.Result.Last(1) > 30 && _rsi.Result.Last(2) <= 30)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "", SL, TP);
            }

            if (_rsi.Result.Last(1) < 70 && _rsi.Result.Last(2) >= 70)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }

            if (_rsi.Result.Last(1) >= 30 && _rsi.Result.Last(2) < 30)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }

            if (Positions.Count(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell) == 0 && Bars.ClosePrices.Last(1) < _ma.Result.Last(1) && _rsi.Result.Last(1) < 70 && _rsi.Result.Last(2) >= 70)
            {

                ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "", SL, TP);
            }

            if (_rsi.Result.Last(1) > 30 && _rsi.Result.Last(2) <= 30)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell))
                    position.Close();
            }
            
            if (_rsi.Result.Last(1) <= 70 && _rsi.Result.Last(2) > 70)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }
            
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
        #endregion
    }
}
 


@RadoWay
Replies

amusleh
07 Jun 2022, 09:28

Hi,

What do you mean by "How to fix Boolean"?


@amusleh

RadoWay
07 Jun 2022, 09:35 ( Updated at: 07 Jun 2022, 10:43 )

RE:

amusleh said:

Hi,

What do you mean by "How to fix Boolean"?

sorry i am very beginner,
this part i think need to be combined in one line

            if (_rsi.Result.Last(1) < 70 && _rsi.Result.Last(2) >= 70)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }

            if (_rsi.Result.Last(1) >= 30 && _rsi.Result.Last(2) < 30)
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }

do i need to describe the strategy idea of the cBot?


@RadoWay

amusleh
08 Jun 2022, 08:49

Hi,

You can use or (||):

            if ((_rsi.Result.Last(1) < 70 && _rsi.Result.Last(2) >= 70) || (_rsi.Result.Last(1) >= 30 && _rsi.Result.Last(2) < 30))
            {
                foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
                    position.Close();
            }

I recommend you to read: Boolean logical operators - C# reference | Microsoft Docs


@amusleh

RadoWay
08 Jun 2022, 09:04

RE: RE:

thanks a lot

 


@RadoWay