This handler runs when a position is being closed. It allows you to perform actions like logging profit/loss, sending notifications, or resetting the state after a trade is closed.
/**
- This defines an asynchronous function that takes two inputs: `state` and `update`.
- `state` is the memory box for your strategy.
- `update` contains the latest market data.
**/
async (state = {}, update = {}) => {
const position = HFS.getPosition(state); // This checks if there’s an open trade position by looking at the state object.
// This checks if a position exists.
if (position) {
/**
- This calculates the profit or loss by:
- Subtracting the entry price (`position.price`) from the closing price (`update.price`).
- Multiplying the result by the position size (`position.amount`).
**/
const profit = (update.price - position.price) * position.amount;
// This logs the profit or loss to the console.
console.log(`Position closed. Profit/Loss: ${profit}`);
}
return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}
/**
- This defines an asynchronous function that takes two inputs: `state` and `update`.
- `state` is the memory box for your strategy.
- `update` contains the latest market data.
**/
async (state = {}, update = {}) => {
const position = HFS.getPosition(state); // This checks if there’s an open trade position by looking at the state object.
// This checks if a position exists.
if (position) {
// This sends a notification with the closing price.
sendNotification(`Position closed at price: ${update.price}`);
}
return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}
/**
- This defines an asynchronous function that takes two inputs: `state` and `update`.
- `state` is the memory box for your strategy.
- `update` contains the latest market data.
**/
async (state = {}, update = {}) => {
console.log("Position is closing. Resetting state..."); // Log a message
return {}; // This resets the state object to an empty object, clearing all stored data.
}
state
object to an empty object, clearing all stored data.