DoesChartObjectExist

Checks if a chart object with the specified name exists.

Syntax

DoesChartObjectExist(uniqueObjectName: string, isStatic: boolean = false): boolean

Parameters

Parameter
Type
Description

uniqueObjectName

string

The unique name of the object to check

isStatic

boolean

Optional. Whether to check static objects (default: false)

Return Value

Returns a boolean indicating whether the object exists (true) or not (false).

Description

The DoesChartObjectExist method checks for the existence of a chart object with the specified name. It can check for both regular and static objects, depending on the isStatic parameter.

Example

// Check if object exists before using it
if (this.api.DoesChartObjectExist("MyTrendLine")) {
  // Object exists, safe to use
  this.api.SetObjectProperty("MyTrendLine", ObjProp.OBJPROP_COLOR, 0xff0000);
} else {
  console.log("Object not found");
}

// Check static object
const staticExists = this.api.DoesChartObjectExist("MyStaticLabel", true);
console.log(`Static object exists: ${staticExists}`);

// Create object only if it doesn't exist
const objectName = "UniqueObject";
if (!this.api.DoesChartObjectExist(objectName)) {
  this.api.СreateChartObject(
    objectName,
    TObjectType.TEXT,
    0,
    this.api.createFTODate(Date.now()),
    1.2345
  );
}

// Remove object if it exists
if (this.api.DoesChartObjectExist("OldObject")) {
  this.api.RemoveChartObject("OldObject");
}

Last updated