Topics
06 Jun 2019, 17:34
 1226
 3
16 May 2019, 04:33
 1495
 3
08 May 2019, 00:41
 1217
 3
07 May 2019, 13:26
 1080
 1
26 Apr 2019, 04:45
 1726
 5
24 Apr 2019, 21:34
 1105
 1
Replies

lec0456
20 Nov 2012, 08:48

RE:
admin said:

We will add the ability to access the timeframe from the cAlgo API soon.

For the time being you might be able to use a workaround method. Please see this sample code: /forum/calgo-reference-samples/163

 


One problem, if you start the robot on saturday or more important on sunday, it gives an error "index was out of range". 

@lec0456

lec0456
19 Nov 2012, 21:20

RE:
admin said:
// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;


namespace cAlgo.Robots
{
    [Robot]
    public class SampleTimeFrame : Robot
    {
        private TimeSpan _timeFrame;

        protected override void OnStart()
        {
            // Get the timeframe in timespan format
            _timeFrame = GetTimeFrame();
            Print("{0}", _timeFrame);            
            // convert the timeframe to it's name representation
            string timeFrameName = GetTimeFrameName(_timeFrame);
            if (timeFrameName == "0")
                Print("Not enough data.");
            else
                Print("{0}", timeFrameName);    
        }

        /// <summary>
        /// Get the name representation of the timeframe used
        /// </summary>
        /// <param name="timeFrame">Time span between two consecutive bars OpenTime</param>
        /// <returns>The name representation of the TimeFrame</returns>
        private string GetTimeFrameName(TimeSpan timeFrame)
        {
            int totalMin = (int)timeFrame.TotalMinutes;
            string timeFrameName;

            if (totalMin > 10080)
                timeFrameName = "M1";
            else
            {
                switch (totalMin)
                {
                    case 1:
                        timeFrameName = "m1";
                        break;
                    case 2:
                        timeFrameName = "m2";
                        break;
                    case 3:
                        timeFrameName = "m3";
                        break;
                    case 4:
                        timeFrameName = "m4";
                        break;
                    case 5:
                        timeFrameName = "m5";
                        break;
                    case 10:
                        timeFrameName = "m10";
                        break;
                    case 15:
                        timeFrameName = "m15";
                        break;
                    case 30:
                        timeFrameName = "m30";
                        break;
                    case 60:
                        timeFrameName = "h1";
                        break;
                    case 240:
                        timeFrameName = "h4";
                        break;
                    case 720:
                        timeFrameName = "h12";
                        break;
                    case 1440:
                        timeFrameName = "D1";
                        break;
                    case 10080:
                        timeFrameName = "W1";
                        break;
                    default:
                        timeFrameName = "0";
                        break;

                }
            }

            return timeFrameName;
        }

        /// <summary>
        /// Get the time span between two consecutive bars OpenTime
        /// </summary>
        private TimeSpan GetTimeFrame()
        {
            if (MarketSeries.Close.Count > 0)
            {
                int currentIndex = MarketSeries.Close.Count - 1;
                DateTime currentOpenTime = MarketSeries.OpenTime[currentIndex];
                DateTime previousOpenTime = MarketSeries.OpenTime[currentIndex - 1];

                TimeSpan timeFrame = currentOpenTime - previousOpenTime;

                if (currentOpenTime.DayOfWeek == DayOfWeek.Monday && previousOpenTime.DayOfWeek != DayOfWeek.Monday)
                {
                    currentOpenTime = previousOpenTime;
                    previousOpenTime = MarketSeries.OpenTime[currentIndex - 2];
                    timeFrame = currentOpenTime - previousOpenTime;
                }

                return timeFrame;
            }
            // if bars are not available
            return TimeSpan.Zero;
        }
    }
}

 

Great! works perfectly!

@lec0456

lec0456
18 Nov 2012, 07:19

RE:
lec0456 said:

I am trying to use the System.Data.SqlClient namespace but it gives me an error.  It says the namespace does not exist and asks if I am missing an assembly.

Do you know whats wrong?

 

Thats ok I figured it out!

@lec0456

lec0456
15 Nov 2012, 19:59

How do you get daily High and Low?


@lec0456