Draw a line every X minutes

21 Jun 2019, 18:58Draw a line every X minutes#1
alex_mihailposts: 39since: 09 Aug 2018

Hi guys - do you know if its possible to write an indicator that draws a line X amount of time set by the user?

24 Jun 2019, 09:59#2
Panagiotis Charalampousposts: 3006since: 13 Jan 2017

Hi Alex,

You can consider using OnTimer for this.

Best Regards,

Panagiotis


Head of Community Management at cTrader
12 Jul 2019, 13:20RE:#3
igorjrmedeirosposts: 8since: 10 Jul 2019

alex_mihail said:

Hi guys - do you know if its possible to write an indicator that draws a line X amount of time set by the user?

Hi Alex,

In the indicator class I have been trying to use the Timer (from Algo.API) but without sucess.

So, I used the System.Timers class and it work...

Try this:

Put the namespace as the example bellow: (NOTE: put it in this order... because if you reference the System.Timers afterthe cAlgo.API a ambiguos error is showed.)

 

using System;
using System.Timers; // This is the Timer class from System namespace
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

After this you need create the Class for it...

Declare this class inside your Public Class Indicator

private static System.Timers.Timer aTimer;

So now... you need to initialize the timer and set the interval.

In the Initialize function do it:

        protected override void Initialize()
        {
            aTimer = new System.Timers.Timer(1000);  // 1000 is the time in milliseconds
            aTimer.Elapsed += OnTimedEvent;      // ---------->> Attention here!!
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

look into the code a line aTimer.Elapsed += OnTimedEvent. This is the function that will be called when the Timer elapsed happen.

So you need to created this function.... so try it:

 

private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            // Put here your code that will be executed in timer elapsed.
            Print("something");

        }

So... I hope this helps

:)