how to switch from OnTick to OnBar on the same bot using Parameter

Created at 14 Aug 2018, 11:01
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!
mparama's avatar

mparama

Joined 11.10.2016

how to switch from OnTick to OnBar on the same bot using Parameter
14 Aug 2018, 11:01


Dear Panagiotis,

Using Parameter as:

[Parameter("OnBars", DefaultValue = "false")]
public bool OnBars { get; set; }

I need the chance to choose in running OnBar or OnTick

I tried has follow but does'nt work

    

private CommodityChannelIndex cci;
        private string Label = "ReverseCCI";
//===============================================
        protected override void OnStart()
        {
            cci = Indicators.CommodityChannelIndex(Period);

        }
//===============================================

        private void ManageOnBarOnTick()
        {
            if (OnBars == false)
            {
               OnTick();
            }
            if (OnBars)
            {
               OnBar();
            }
        }

//===============================================
        protected override void OnTick()
        {

           var positionBuy = Positions.Find(Label, Symbol, TradeType.Buy);
            var positionSell = Positions.Find(Label, Symbol, TradeType.Sell);
            var cci2 = cci.Result.Last(2);
            var current_cci = cci.Result.Last(0);
            //-------------------------

//-------------------------------------------------------------------------------------------------

protected override void OnBar()
        {
            var positionBuy = Positions.Find(Label, Symbol, TradeType.Buy);
            var positionSell = Positions.Find(Label, Symbol, TradeType.Sell);
            var posBuy = Positions.FindAll(Label, Symbol, TradeType.Buy);
            var posSell = Positions.FindAll(Label, Symbol, TradeType.Sell);
            var cci2 = cci.Result.Last(2);
            var current_cci = cci.Result.Last(0);
            //-------------------------
//===============================================

Thank a lot for your always kind help !!!


@mparama
Replies

... Deleted by UFO ...

PanagiotisCharalampous
14 Aug 2018, 11:06

Hi mparama,

Try the code below

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
    {
        [Parameter("OnBars", DefaultValue = "false")]
        public bool OnBars { get; set; }
        private CommodityChannelIndex cci;
        private string Label = "ReverseCCI";
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            if (!OnBars)
            {
                var positionBuy = Positions.Find(Label, Symbol, TradeType.Buy);
                var positionSell = Positions.Find(Label, Symbol, TradeType.Sell);
                var cci2 = cci.Result.Last(2);
                var current_cci = cci.Result.Last(0);
            }
        }
        protected override void OnBar()
        {
            if (OnBars)
            {
                var positionBuy = Positions.Find(Label, Symbol, TradeType.Buy);
                var positionSell = Positions.Find(Label, Symbol, TradeType.Sell);
                var posBuy = Positions.FindAll(Label, Symbol, TradeType.Buy);
                var posSell = Positions.FindAll(Label, Symbol, TradeType.Sell);
                var cci2 = cci.Result.Last(2);
                var current_cci = cci.Result.Last(0);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

Best Regards,

Panagiotis


@PanagiotisCharalampous

mparama
14 Aug 2018, 21:12

Thank you very much it works fine !!!!!!!!!!!


@mparama