MoveObject
Syntax
MoveObject(objectName: string, pointIndex: number, time: number, price: number): booleanParameters
Return Value
Description
Example
Last updated
MoveObject(objectName: string, pointIndex: number, time: number, price: number): booleanLast updated
// Move the first point of a trend line
const success1 = this.api.MoveObject(
"MyTrendLine",
0, // first point
1641024000000, // new time
1.2 // new price
);
console.log(`Move successful: ${success1}`);
// Move both points of a trend line
function moveTrendLine(
name: string,
startTime: number,
startPrice: number,
endTime: number,
endPrice: number
): boolean {
const success1 = this.api.MoveObject(name, 0, startTime, startPrice);
const success2 = this.api.MoveObject(name, 1, endTime, endPrice);
return success1 && success2;
}
// Move a rectangle maintaining its shape
function moveRectangle(
name: string,
newTime: number,
newPrice: number
): boolean {
try {
// Get current dimensions
const time1 = this.api.GetObjectNumberProperty(name, ObjProp.OBJPROP_TIME1);
const time2 = this.api.GetObjectNumberProperty(name, ObjProp.OBJPROP_TIME2);
const price1 = this.api.GetObjectNumberProperty(
name,
ObjProp.OBJPROP_PRICE1
);
const price2 = this.api.GetObjectNumberProperty(
name,
ObjProp.OBJPROP_PRICE2
);
// Calculate offsets
const timeOffset = time2 - time1;
const priceOffset = price2 - price1;
// Move both points maintaining the shape
const success1 = this.api.MoveObject(name, 0, newTime, newPrice);
const success2 = this.api.MoveObject(
name,
1,
newTime + timeOffset,
newPrice + priceOffset
);
return success1 && success2;
} catch (error) {
console.log("Error moving rectangle:", error.message);
return false;
}
}