Topics
Replies
rdebacker
22 Jul 2023, 22:06
Hello, Thank you for your answer.
I am looking for something similar but the date has to be the current day, and show this result standard for every maximum number of days in the past, for backtesting purposes .I know it is sometimes though to convert between times, timespan and datetimes and am awhere off all the functions and tricks to handle this, but
the difficulty lies within working with the ctrader algo.api.
Any ideas are very welcome.
Thanks in advance
Rafael
@rdebacker
rdebacker
22 Jul 2023, 21:59
( Updated at: 21 Dec 2023, 09:23 )
I took an example from the forum to show the developing high/low for the day for number of days.
I made some adjustments to have only the high/low between8 and 9 AM and after that the 2 lines have to remain flat until the end off the day.
The adjustments do not produce an error but the result stays the same.
- changes from Original =
- drop seperatorline stuff
- add var dayEndBarIndex = Bars.OpenTimes.GetIndexByTime(dayEndTime);
- for : for (var iBarIndex = dayStartBarIndex; iBarIndex <= dayEndBarIndex; iBarIndex++)
Does anybody have an idea what i am doing wrong?
using cAlgo.API;
using System;
using System.Globalization;
using System.Linq;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.RomanceStandardTime, AccessRights = AccessRights.FullAccess)]
public class DailyHighLowSample : Indicator
{
private readonly string _name = "Daily High Low";
private string _chartObjectNamesSuffix;
private int _lastBarIndex;
private TimeSpan _dayStartTime;
private TimeSpan _dayEndTime;
[Parameter("Use Bar Bodies", DefaultValue = false, Group = "General")]
public bool UseBarBodies { get; set; }
[Parameter("Day Start Time", DefaultValue = "08:00:00", Group = "General")]
public string DayStartTime { get; set; }
[Parameter("Day End Time", DefaultValue = "09:00:00", Group = "General")]
public string DayEndTime { get; set; }
[Parameter("Days #", DefaultValue = 5, MinValue = 0, Group = "General")]
public int DaysNumber { get; set; }
[Output("High", LineColor = "Lime", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries High { get; set; }
[Output("Low", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Low { get; set; }
protected override void Initialize()
_chartObjectNamesSuffix = string.Format("{0}_{1}", _name, DateTime.Now.Ticks);
if (!TimeSpan.TryParse(DayStartTime, CultureInfo.InvariantCulture, out _dayStartTime))
{
Print("Your provided value for 'Day Start Time' parameter is not valid");
}
if (!TimeSpan.TryParse(DayEndTime, CultureInfo.InvariantCulture, out _dayEndTime))
{
Print("Your provided value for 'Day End Time' parameter is not valid");
}
Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;
}
public override void Calculate(int index)
{
if (index == _lastBarIndex)
{
return;
}
_lastBarIndex = index;
//var currentBarTime = Bars.OpenTimes[index].Add(Application.UserTimeOffset);
var currentBarTime = Bars.OpenTimes[index];
var dayStartTime = currentBarTime.TimeOfDay >= _dayStartTime ? currentBarTime.Date.Add(_dayStartTime) : currentBarTime.Date.AddDays(-1).Add(_dayStartTime);
var dayEndTime = currentBarTime.TimeOfDay <= _dayEndTime ? currentBarTime.Date.Add(_dayEndTime) : currentBarTime.Date.AddDays(-1).Add(_dayEndTime);
//dayStartTime = dayStartTime.Add(-Application.UserTimeOffset);
//dayEndTime = dayEndTime.Add(-Application.UserTimeOffset);
if (DaysNumber > 0)
{
//var currentBarTimeDiffWithToday = Server.Time.Add(Application.UserTimeOffset) - dayStartTime;
//var currentBarTimeDiffWithTodayEnd = Server.Time.Add(Application.UserTimeOffset) - dayEndTime;
var currentBarTimeDiffWithToday = Server.Time - dayStartTime;
var currentBarTimeDiffWithTodayEnd = Server.Time - dayEndTime;
if (currentBarTimeDiffWithToday.TotalDays > DaysNumber)
{
return;
}
}
var dayStartBarIndex = Bars.OpenTimes.GetIndexByTime(dayStartTime);
var dayEndBarIndex = Bars.OpenTimes.GetIndexByTime(dayEndTime);
var dayHigh = double.MinValue;
var dayLow = double.MaxValue;
// for (var iBarIndex = dayStartBarIndex; iBarIndex <= index; iBarIndex++)
for (var iBarIndex = dayStartBarIndex; iBarIndex <= dayEndBarIndex; iBarIndex++)
{
if (UseBarBodies)
{
if (Bars[iBarIndex].Close > Bars[iBarIndex].Open)
{
dayHigh = Math.Max(Bars.ClosePrices[iBarIndex], dayHigh);
dayLow = Math.Min(Bars.OpenPrices[iBarIndex], dayLow);
}
else
{
dayHigh = Math.Max(Bars.OpenPrices[iBarIndex], dayHigh);
dayLow = Math.Min(Bars.ClosePrices[iBarIndex], dayLow);
}
}
else
{
dayHigh = Math.Max(Bars.HighPrices[iBarIndex], dayHigh);
dayLow = Math.Min(Bars.LowPrices[iBarIndex], dayLow);
}
}
High[index] = dayHigh;
Low[index] = dayLow;
}
public void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
{
_lastBarIndex = -1;
//for (var iBarIndex = 0; iBarIndex < Bars.Count; iBarIndex++)
for (var iBarIndex = dayStartBarIndex; iBarIndex < Bars.Count; iBarIndex++)
{
High[iBarIndex] = double.NaN;
Low[iBarIndex] = double.NaN;
Calculate(iBarIndex);
}
}
}
}
@rdebacker
rdebacker
27 Sep 2023, 08:19
Thanks for your reply.
Somebody found a code sample in the past that can be used for this?
Thank you
Rafael
@rdebacker