Plotting array based on date and time for x

I'm lost on this one.
I'm experimenting with plotting a line from where my system gets into a trade, to where the current close price is, through concatenating a date and time string that I have pulled from a csv file.

I'm doing this;

start = StrToDateTime("12/22/2024 11:00:00");
finish = StrToDateTime("12/20/2024 10:00:00");

xo = Lookup(BarIndex(),start,0);
x1 = Lookup(BarIndex(), finish,0);
yo = 1;
y1 = 100;
line = LineArray(xo,yo,x1,y1,0,True);
Plot(line,"Line", colorBlack, styleLine|styleThick);

But I am not getting a line.

Can StrToDateTime take the format I have typed above? Can it take more than 1 date time string format and if so, will it automaticlaly recognise the date time string format I have used or will I need to specify it?

To create the line and then plot it, am I using the correct functions?

First
AFL Function Reference - GFXMOVETO

then
AFL Function Reference - GFXLINETO

For LineArray() you are building a real Array with values in it. Is that required?
IF its just drawing a line, use gfx, like you only have 2 DateTime values and 2 price points.

1 Like

First you need to check (_TRACE or debug) what values you get from StrToDateTime.
Officially supported format is ISO (YYYY-MM-DD HH:MM:SS), other formats might work IF and ONLY IF your Windows is set to recognize given region format.

Secondly, check for what Lookup returns (again _TRACE). If it returns NULL the exact date cannot be found (then you might want mode -1 that uses nearest predecesor)

Thirdly as @nsm51 wrote you - it would be easier to use GfxMoveTo/GfxLineTo/GfxSetCoordsMode to achieve easier (without Lookup).

1 Like

Brilliant, I’ll check that out. Thanks.

Ok, still struggling but moving forward. I'm not certain whether I need to use a loop or not, but this does not work;


start_time = StrToDateTime("2024-12-16 08:00:00");
end_time = StrToDateTime("2024-12-16 08:00:00");

GfxSetCoordsMode(1);

bi = BarIndex();
start = ValueWhen(DateTime()==start_time,bi, 0);
end = ValueWhen(DateTime()==end_time,bi, 0);

step_number = end - start;


y_val = 60000;
end_y = 100000;

step_val = ((end_y - y_val)/step_number);
 

for (bi =start ; bi <= end; bi++)
{	
		GfxSelectPen(colorBlack,1);
		GfxMoveTo(i,y_val[i]);

		new_Y_val = y_val[i] + step_val;
		y_val = IIf(new_Y_val < end_y,new_Y_val, end_y);
		
		GfxLineTo(i,y_val[i]);
		
}

First, do I need to use a loop?
Second, if I do, then why is my for loop not recognised as a boolean condition

I very clearly have no idea how to do this. I have read the manual. I just want to draw a line on a chart.

your start and end times are the same?

_SECTION_BEGIN("Gfx line example");

Plot( C, "C", colorDefault , styleLine );

GfxSetCoordsMode( 1 );

StartDt = "2024-12-20 11:15:00";
EndDt   = "2024-12-23 09:20:00";

bi 		 = BarIndex();
StartBar = Lookup( bi, _DT( StartDt ), 0 );
EndBar   = Lookup( bi, _DT( EndDt ), 0 );

GfxSelectPen( colorLime, 2 );

GfxMoveTo( StartBar, C[ StartBar ] );

GfxLineTo( EndBar, C[ EndBar ] );

_SECTION_END();

is it so hard? :grin:

:man_facepalming:
Ok, that certainly doesn't help
:joy:

I copied your code exactly and got;

Error 51. Array subscript has Null value. Subscript must be within 0...BarCount-1 range.

Any ideas on where I am going wrong?

You should have valid bars for the datetime in my example and it is a 5min chart

Ah, ok. That makes sense. Thank you.

It appears switching to a higher timeframe then, I won't get the plot, unless I round to the nearest hour (for 1h timeframe) etc?

see how Lookup() works

I wrote many posts ago:

Secondly, check for what Lookup returns (again _TRACE). If it returns NULL the exact date cannot be found (then you might want mode -1 that uses nearest predecesor)

You would save yourself a lot of trouble if your code was using -1 mode:

StartBar = Lookup( bi, _DT( StartDt ), -1 );
EndBar   = Lookup( bi, _DT( EndDt ), -1 );

Amazing @nsm51 and @Tomasz, thank you. That's working perfectly!

Both have a wonderful Christmas.

The following snippet works well...

_SECTION_BEGIN("Plot Line");

GfxSetCoordsMode( 1 );

bi 		 = BarIndex();

StartDt = "2024-12-10 02:40:00";
StartBar = Lookup( bi, _DT( StartDt ), -1 );
startPrice = 94000;

EndDt   = "2024-12-23 11:00:00";
EndBar   = Lookup( bi, _DT( EndDt ), -1 );
endPrice = 100000;

GfxSelectPen( colorBlack, width =2, penstyle = 2 );
GfxMoveTo( StartBar, startPrice );
GfxLineTo( EndBar, endPrice );

_SECTION_END();

From here, It is easy enough to parse date and time strings to suit the format regardless of what my local copy of windows is doing with datetime strings and feed them into the above as a function.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.