Move logic from Indicator to Bot (calculate index problem)

Created at 23 Aug 2022, 17:54
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!
IN

intense_country0m

Joined 23.08.2022

Move logic from Indicator to Bot (calculate index problem)
23 Aug 2022, 17:54


Hi,

How to get logic (how it work) from these indicator into cbot? 

I don't know how to relace Calculate Index to working in cbot. Or maybe is different way to have simillar results? 

Regards

using cAlgo.API;
using cAlgo.API.Indicators;
 
namespace cAlgo.Indicators
{
    [Levels(0)]
    [Indicator(AccessRights = AccessRights.None)]
    public class ElliotOscillator : Indicator
    {
        private SimpleMovingAverage _fastSma;
        private SimpleMovingAverage _slowSma;
        private SimpleMovingAverage _sma100;
        private SimpleMovingAverage _sma200;
        private SimpleMovingAverage _sma20;
 
        private double _d;
        private bool _upTrend;
        private bool _neutral;
        private IndicatorDataSeries _elliot;
 
 
        [Parameter]
        public DataSeries Source { get; set; }
 
        [Parameter("FastPeriod", DefaultValue = 5)]
        public int FastPeriod { get; set; }
         
        [Parameter("SlowPeriod", DefaultValue = 34)]
        public int SlowPeriod { get; set; }
 
        [Output("UpTrend", Color = Colors.Green, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries UpTrend { get; set; }
        [Output("DownTrend", Color = Colors.Red, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries DownTrend { get; set; }
        [Output("Neutral", Color = Colors.Gray, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries Neutral { get; set; }
         
        [Output("Line", Color = Colors.Red)]
        public IndicatorDataSeries Line { get; set; }
 
        protected override void Initialize()
        {
            _fastSma = Indicators.SimpleMovingAverage(Source, FastPeriod);
            _slowSma = Indicators.SimpleMovingAverage(Source, SlowPeriod);
            _sma100 = Indicators.SimpleMovingAverage(Source, 100);
            _sma200 = Indicators.SimpleMovingAverage(Source, 200);
            _sma20 = Indicators.SimpleMovingAverage(Source, 20);
            _elliot = CreateDataSeries();
 
        }
        public override void Calculate(int index)
        {
            if (index < 3)
                return;
 
            _elliot[index] = _fastSma.Result[index] - _slowSma.Result[index];
            Line[index] = _fastSma.Result[index - 3] - _slowSma.Result[index - 3];
 
            if (_sma100.Result.LastValue > _sma200.Result.LastValue
                && _sma20.Result.LastValue >_sma100.Result.LastValue)
            {
                UpTrend[index] = _elliot[index];
                DownTrend[index] = double.NaN;
                Neutral[index] = double.NaN;
            }
            else if (_sma100.Result.LastValue < _sma200.Result.LastValue
                && _sma20.Result.LastValue < _sma100.Result.LastValue)
            {
                DownTrend[index] = _elliot[index];
                UpTrend[index] = double.NaN;
                Neutral[index] = double.NaN;
            }
            else
            {
                Neutral[index] = _elliot[index];
                UpTrend[index] = double.NaN;
                DownTrend[index] = double.NaN;
            }
        }
    }
}

 


@intense_country0m
Replies

PanagiotisCharalampous
24 Aug 2022, 08:29

Hi there,

It is not clear what are you trying to achieve here. What do you want the cBot to do? In general, Calculate is called on once for each bar for historical data and on each tick for the current bar.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

intense_country0m
24 Aug 2022, 10:11 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Hi Panagiotis,

I try to achieve something like on screen below:
1. When is colour green or red then :
    1a. If red line above 0 sell
    1b. If red line below 0 buy
2. Keep position open until colour green or red become gray

 


@intense_country0m

PanagiotisCharalampous
24 Aug 2022, 10:24

Hi there,

Thanks, you don't need to reproduce the entire logic inside the cBot. You just need to reference the indicator and check the values.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

intense_country0m
24 Aug 2022, 12:21

RE:

Hi,

Thanks for replay, but can you show how to reference in bot? I don't know how to check value? Or I need add some output in indicator?

Best regards

 


@intense_country0m

PanagiotisCharalampous
25 Aug 2022, 08:41

Hi there,

You can check the sample cBots in cTrader. like the Sample cBot Reference SMA

// -------------------------------------------------------------------------------------------------
//
//    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.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SamplecBotReferenceSMA : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        private SampleSMA sma;

        protected override void OnStart()
        {
            sma = Indicators.GetIndicator<SampleSMA>(Source, SmaPeriod);
        }

        protected override void OnTick()
        {
            Print("{0}", sma.Result.LastValue);
        }
    }
}

and the Sample RSI cBot

// -------------------------------------------------------------------------------------------------
//
//    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 System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : 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);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", 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, "SampleRSI");
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous