Timeframe change in indicator does not work

Created at 24 Apr 2020, 19:59
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!
SH

Shares4UsDevelopment

Joined 14.10.2018

Timeframe change in indicator does not work
24 Apr 2020, 19:59


 Hi Panagiotis

I've made little sample Bot and indicator

shows you can not change timeframe in indicator.

is there another way to do it?


Indicator timeframe change works but not when it is parameterized 

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 60, Step=60)]
        public int Secs { get; set; }
        Bars TimeBars = null;
        TestIndic Indic1,Indic2;
        
        protected override void OnStart()
        {
        try
            {
                Print("NewcBot.TimeFrame Req =",Secs," secs.");
                TimeFrame Tf  = TimeFrame.Minute;
                if(Secs==120) Tf = TimeFrame.Minute2;
                if(Secs==180) Tf = TimeFrame.Minute3;
                if(Secs==240) Tf = TimeFrame.Minute4;
            
                Indic1 = Indicators.GetIndicator<TestIndic>(Secs);
                Print("NewcBot.TimeFrame Indic1 ",Indic1.Chart.TimeFrame.ToString());
                
                Print("NewcBot.TimeFrame try Load ",Tf.ToString());
                TimeBars = MarketData.GetBars(Tf);
                Print("NewcBot OnStart: TimeBars=",TimeBars.TimeFrame.ToString());
                
                Indic2 = Indicators.GetIndicator<TestIndic>(Secs);
                Print("NewcBot.TimeFrame Indic2 ",Indic2.Chart.TimeFrame.ToString());
                
                Indic1.Chart.TryChangeTimeFrame(Tf);
                Print("NewcBot OnStart: Indic1.Chart= ", Indic1.Chart.TimeFrame.ToString());
                Print("NewcBot OnStart: Chart =",Chart.TimeFrame.ToString());
            }
            catch (Exception ex)
            {
                Print("NewcBot OnStart: ",ex.Message, " ", ex.StackTrace.ToString());
            }
        }
    }
}

Some Indicator :

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestIndic : Indicator
    {
        [Parameter(DefaultValue = 60, Step=60)]
        public int Secs { get; set; }
        
        [Output("OSC", LineColor = "yellow", PlotType = PlotType.DiscontinuousLine, Thickness = 2)]
        public IndicatorDataSeries OSC { get; set; }
        string log="";
        protected override void Initialize()
        {
          try
            {
                log+="indik.TimeFrame Req ="+Secs+" secs.\n";
                TimeFrame Tf  = TimeFrame.Minute;
                if(Secs==120) Tf = TimeFrame.Minute2;
                if(Secs==180) Tf = TimeFrame.Minute3;
                if(Secs==240) Tf = TimeFrame.Minute4;
            
                log+="indik.TimeFrame try Set ="+Tf.ToString()+"\n";
                Chart.TryChangeTimeFrame(Tf);      
                log+="indik.TimeFrame="+Chart.TimeFrame.ToString()+"\n";
            }
            catch (Exception ex)
            {
                log+="indik.TimeFrame Initialize: "+ex.Message+ " "+ ex.StackTrace.ToString();
            }
            Chart.DrawStaticText("xx",log,VerticalAlignment.Top,HorizontalAlignment.Left,"yellow");
       }
        public override void Calculate(int index)
        {
            try
            {
                OSC[index] = Bars.OpenPrices.LastValue<Bars.ClosePrices.LastValue ? 1 : 0;
            }
            catch (Exception ex)
            {
                Print("indik.TimeFrame Calculate: ",ex.Message, " ", ex.StackTrace.ToString());
            }
        }
    }
}


Output:
     24/04/2020 15:07:33.079 | cBot "TestBot" was started successfully for EURUSD, h1.
     24/04/2020 15:07:33.079 | NewcBot.TimeFrame Req =120 secs.
     24/04/2020 15:07:33.079 | indik.TimeFrame Req =120 secs.
     24/04/2020 15:07:33.079 | indik.TimeFrame try Set =Minute2
?? 24/04/2020 15:07:33.095 | indik.TimeFrame=Hour
?? 24/04/2020 15:07:33.095 | NewcBot.TimeFrame Indic1 Hour
     24/04/2020 15:07:33.095 | NewcBot.TimeFrame try Load Minute2
     24/04/2020 15:07:33.126 | NewcBot OnStart: TimeBars=Minute2
?? 24/04/2020 15:07:33.126 | NewcBot.TimeFrame Indic2 Hour
?? 24/04/2020 15:07:33.126 | NewcBot OnStart: Indic1.Chart= Hour
     24/04/2020 15:07:33.126 | NewcBot OnStart: Chart =Hour

 


@Shares4UsDevelopment
Replies

PanagiotisCharalampous
27 Apr 2020, 09:05

Hi Ton,

Chart timeframes cannot change when a cBot is running.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous