Jumping to OnStart()

Created at 04 Mar 2018, 18:49
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!
alexander.n.fedorov's avatar

alexander.n.fedorov

Joined 02.01.2018

Jumping to OnStart()
04 Mar 2018, 18:49


Dear Panagiotis,

Is it possible to jump to OnStart() , let us say from the OnBar()  ( or OnTick()) , when certain conditions are met?

For example, if a new Day opens.

If yes, please advise how to do the jump, if not what shall I do redo all the initialisation logic?

Regards

Alexander

 

 


@alexander.n.fedorov
Replies

alexander.n.fedorov
04 Mar 2018, 19:29

use of OnBar()

One more thing.

Let' us say my Bot is working on a chart H1, which means H1 is a major timeframe.

And suppose I am using second Timeframe Daily

How can a set up a function OnBar() for a second TimeFrame, for example if a new day opens?

Regards

 


@alexander.n.fedorov

alexander.n.fedorov
04 Mar 2018, 19:41

OnStart() - continuation

what I put and it does not show any errors is this

"

       protected override void OnBar()
        {
            var newDate = MarketSeries.OpenTime.LastValue.Date;
            if (newDate >= NextDay[0])
            {
                OnStart();
            }

           else

          {

                  // OnBar () logic here

          }


    }

"

But I cannot test it because no signal from the server on the weekend :)

 

 


@alexander.n.fedorov

PanagiotisCharalampous
05 Mar 2018, 10:58

Hi Alexander,

The way you implemented this in your last example should work. 

Best Regards,

Panagiotis


@PanagiotisCharalampous

alexander.n.fedorov
05 Mar 2018, 12:37

OnBar() ?

Pangiotis, 

Thank you for your kind answer.

Yes, it does

But what about my second question?

 

 


@alexander.n.fedorov

PanagiotisCharalampous
05 Mar 2018, 12:52

Hi Alexander,

Sorry, I missed the second question. Currently there is no event to handle a bar change for a timeframe that is not set on the current graph. You will need to find a programmatic workaround for this. See below an example that could guide your implementation

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(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private DateTime _lastValue;
        protected override void OnStart()
        {
            _lastValue = MarketData.GetSeries(TimeFrame.Daily).OpenTime.LastValue;
        }

        protected override void OnBar()
        {
            var timeFrame = MarketData.GetSeries(TimeFrame.Daily);
            if (_lastValue != timeFrame.OpenTime.LastValue)
            {
                Print("Last value changed");
                _lastValue = timeFrame.OpenTime.LastValue;
            }
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

alexander.n.fedorov
05 Mar 2018, 12:55

OnBar() ?

Thank  you. 

I am using smth simiilar, I was not sure that I have to use a workaround, but now I do

 


@alexander.n.fedorov