FindObject

Finds an object by its name and returns its index.

Syntax

FindObject(chartId: number | null = null, objectName: string): number

Parameters

  • chartId: number | null - Optional. The chart ID (null for current chart)

  • objectName: string - The name of the object to find

Return Value

Returns a number representing the object's index. Returns -1 if the object is not found.

Description

The FindObject method searches for a chart object with the specified name and returns its index. The search can be performed on a specific chart (by providing the chartId) or on the current chart (by passing null or omitting the chartId).

Example

// Find object on current chart
const index = this.api.FindObject(null, "MyTrendLine");
if (index !== -1) {
  console.log(`Object found at index: ${index}`);
} else {
  console.log("Object not found");
}

// Find object on specific chart
const chartId = this.api.GetCurrentChartId();
const objectIndex = this.api.FindObject(chartId, "MyLabel");
if (objectIndex >= 0) {
  // Use the index to get object properties
  const objectType = this.api.GetObjectType(
    this.api.GetObjectName(objectIndex)
  );
  console.log(`Object type: ${objectType}`);
}

// Find and modify object
const targetName = "SignalLine";
const foundIndex = this.api.FindObject(null, targetName);
if (foundIndex >= 0) {
  // Object exists, modify it
  const name = this.api.GetObjectName(foundIndex);
  this.api.SetObjectProperty(name, ObjProp.OBJPROP_COLOR, 0xff0000);
} else {
  // Object doesn't exist, create it
  this.api.СreateChartObject(
    targetName,
    TObjectType.TREND_LINE,
    0,
    this.api.createFTODate(Date.now()),
    1.2345
  );
}

Last updated