Topics
06 Apr 2021, 06:58
1214
2
05 Apr 2021, 19:05
1037
1
21 Mar 2021, 10:00
1428
5
09 Nov 2020, 09:54
1478
7
19 Dec 2019, 10:05
1242
4
11 Oct 2019, 07:30
1077
2
12 Aug 2019, 21:15
2474
2
28 Jun 2019, 14:46
1002
1
16 Jun 2019, 05:35
1360
5
06 Jun 2019, 17:34
1226
3
04 Jun 2019, 04:28
1287
2
01 Jun 2019, 00:07
2484
2
16 May 2019, 04:33
1495
3
08 May 2019, 00:41
1217
3
07 May 2019, 13:26
1080
1
02 May 2019, 04:16
1376
5
26 Apr 2019, 04:45
1726
5
24 Apr 2019, 21:34
1105
1
24 Apr 2019, 05:15
1152
5
11 Apr 2019, 23:55
2330
4
Replies
lec0456
19 Nov 2012, 21:20
RE:
admin said:
Great! works perfectly!// ------------------------------------------------------------------------------- // // 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; } } }
@lec0456
lec0456
20 Nov 2012, 08:48
RE:
One problem, if you start the robot on saturday or more important on sunday, it gives an error "index was out of range".
@lec0456