Call in value Uptrend/DwonTrend doesn't work

Created at 04 Dec 2019, 07:15
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!
MI

Call in value Uptrend/DwonTrend doesn't work
04 Dec 2019, 07:15


All

appreciate your help in coding a Cbots using the supertrend indicator, im unsuccessful to call in the signal Uptrend/DownTrend, below coding for the cbots, your inputs will be highly appreciated

 

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 SuBot : Robot
    {
        
        private Position _position;
        private Supertrend _supertrend;

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

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

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

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

        [Output("Downtrend")]
        public IndicatorDataSeries DownTrend { get; set; }

        protected override void OnStart()
        {
            _supertrend = Indicators.GetIndicator<Supertrend>(Period, Multiplier)
            DownTrend = _supertrend.DownTrend;
            UpTrend = _supertrend.UpTrend;
        }


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


            bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
          
            if (!isLongPositionOpen)
            {
                ClosePosition();
                Buy();
            }


            
            if (!isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }

        }
        private void ClosePosition()
        {
            if (_position != null)
            {
                Trade.Close(_position);
                _position = null;

            }
        }

        private void Buy()
        {

            //DownTrend = _supertrend.DownTrend;
            Trade.CreateBuyMarketOrder(Symbol, Volume);

        }

        private void Sell()
        {
            //UpTrend = _supertrend.UpTrend;
            Trade.CreateSellMarketOrder(Symbol, Volume);

        }

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

    }
}
 


@michaelocampo1104@gmail.com
Replies

PanagiotisCharalampous
04 Dec 2019, 09:08

Hi there,

I cannot see where is the problem. Can you please elaborate?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

michaelocampo1104@gmail.com
04 Dec 2019, 09:59

RE:

PanagiotisCharalampous said:

Hi there,

I cannot see where is the problem. Can you please elaborate?

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis

Thanks for your prompt response,

the cbots built successfully 

However the intend is that the cbots should buy on uptrend signal and cancel/open sell position if receive  downtrend signal

 

 


@michaelocampo1104@gmail.com

PanagiotisCharalampous
04 Dec 2019, 10:33

From what I can see in the indicator code, you need to check the last value of DownTrend/UpTrend. If it is not NaN then the indicator is trending into that Direction. So the conditions should look like the below

            if (!double.IsNaN(_supertrend.DownTrend.LastValue))
            {
                //Close buy and open sell
            }

            if (!double.IsNaN(_supertrend.UpTrend.LastValue))
            {
                //Close sell and open buy
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

michaelocampo1104@gmail.com
04 Dec 2019, 14:48

RE:

Hi  Panagiotis

Thank you for your help I got it working now

PanagiotisCharalampous said:

From what I can see in the indicator code, you need to check the last value of DownTrend/UpTrend. If it is not NaN then the indicator is trending into that Direction. So the conditions should look like the below

            if (!double.IsNaN(_supertrend.DownTrend.LastValue))
            {
                //Close buy and open sell
            }

            if (!double.IsNaN(_supertrend.UpTrend.LastValue))
            {
                //Close sell and open buy
            }

Best Regards,

Panagiotis 

Join us on Telegram

 


@michaelocampo1104@gmail.com