Don't use ChatGPT, Seeing "runtime error: 'chart' is null or not an object"

Seeing "runtime error: 'chart' is null or not an object" at line 19 chart.GotoDateTime(dateTime);

// Define the year, month, and date
var year = 2023;
var month = 8; 
var date = 11;

// Create a Date object
var dateObj = new Date(year, month - 1, date); // Subtract 1 from month

// Create the main OLE automation object for AmiBroker
var AB = new ActiveXObject("Broker.Application");

// Get the active chart
var chart = AB.ActiveDocument.ActiveWindow.Chart;

// Format the date in the required format
var dateTime = dateObj.getFullYear() + "-" + ("0" + (dateObj.getMonth() + 1)).slice(-2) + "-" + ("0" + dateObj.getDate()).slice(-2) + " 00:00:00";

// Go to the specific date
chart.GotoDateTime(dateTime);

You are referring to non-existing property.

There is no "Chart" property in the Window object. See the documentation
https://www.amibroker.com/guide/objects.html#AWindow

I guess you copy-pasted ChatGPT generated code. Remember ChatGPT simply HALLUCINATES code.

ChatGPT can generate poems and other blue sky nonsense but pretty often fails with code.

2 Likes

Thanks, will start afresh.
I see that AmiBroker Object Model has 'chart' type, but i am not sure how to proceed ahead to go to the date i need

I see i can get active chart window with below code:

// Get the active chart
docs = AB.Documents();
doc = docs( 0 );
doc.Activate();

if you want to "Zoom to Range" the current chart


dtFrom = DateTimeToStr( /* AB datetime value */ );
dtTo   =  DateTimeToStr( /* AB datetime value */ );
// or just your own date time strings

AB = CreateObject( "Broker.Application" );
AW = AB.ActiveWindow;
AW.ZoomToRange( dtFrom, dtTo );

this is more appropriate approach rather than going to specific date. Get you date and add / subtract some bars to create a range.

@nsm51 Thank you!!
I looked up this forum based on your response and found few responses, i stitched together pieces to make it work the way i wanted. Posting the code below just in case it might be helpful for anyone

Please take a look, appreciate your feedback.


_SECTION_BEGIN("ZoomRange");
/// code works for native EOD bars as well as daily bars created out of intraday data
/// @link https://forum.amibroker.com/t/chart-set-timeframe-from-1-1-2018-till-now/10534/2
/// @link https://www.amibroker.com/members/library/formula.php?id=1215
/// sample code by fxshrat@gmail.com

Version(6.20); // minimum AB version being required
zoom_activate = ParamToggle( "Activate Zoom", "OFF|ON", 1 ); // 1 means activated by default
Days= 232 ; // no of days as look back
end_date = ParamDate( "End Date", Now(1), 2);
zoom_trigger = ParamTrigger( "Zoom to Date Range", "CLICK HERE" ); // Zoom is activated only after clicking zoom_trigger

if ( zoom_activate ) {
	//SetBarsRequired( -2, -2 );
	BlankBars 		= 5; // Enter the number of Blank Bars you have defined under Preferences- Charting.
	NumberOfBars	= Days;
	BI 				= BarIndex();
	dt = DateTime();
	BeginBarIndex 	= ValueWhen( LastValue(BarIndex())- BarIndex() > NumberOfBars + BlankBars, BI );
	BeginDateTime 	= dt[LastValue( BeginBarIndex - BI[0] + 2 )];

	start_dt = DateTimeFormat("%Y-%m-%d", BeginDateTime);	
	Lookedup_end_dt = Lookup(dt, end_date, -1);
	end_dt = DateTimeFormat("%Y-%m-%d", Lookedup_end_dt);	
	if ( zoom_trigger ) {		
		AB = CreateObject( "Broker.Application" );
		AW = AB.ActiveWindow;
		AW.ZoomToRange( start_dt, end_dt );
	}
}
_SECTION_END();

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