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);
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();