This handler runs periodically (e.g., every few seconds) to check on your strategy. Think of it as a regular "check-in" that happens automatically, allowing you to monitor and manage your strategy over time.
/**
- 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 no position is open. The ! means "not."
if (!position) {
// This logs a message to the console if no position is open.
console.log("No open positions.");
} else {
// This logs the type of position (e.g., "long" or "short") and the entry price if a position is open.
console.log(`Current position: ${position.type} at ${position.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 = {}) => {
const position = HFS.getPosition(state); // This checks if there’s an open trade position by looking at the state object.
// This checks if there's opened position.
if (position) {
const { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
const trailingStop = state.trailingStop || position.price * 0.95; // This sets the initial stop-loss at 5% below the entry price. If a stop-loss already exists, it uses that value.
// This checks if the price has moved above the current stop-loss level.
if (price > trailingStop) {
state.trailingStop = price * 0.95; // This updates the stop-loss to 5% below the new price.
}
// This checks if the price has fallen to or below the stop-loss level.
else if (price <= trailingStop) {
return await HFS.closePositionMarket(state, { price }); // This closes the position at the market price if the stop-loss is hit.
}
}
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 there's opened position.
if (position) {
const currentTime = Date.now(); // This gets the current time in milliseconds.
const entryTime = position.entryTime; // This gets the time the position was opened.
// This checks if 1 hour (3600000 milliseconds) has passed since the position was opened.
if (currentTime - entryTime > 3600000) {
// This closes the position at the market price after 1 hour.
return await HFS.closePositionMarket(state, { price: update.price });
}
}
return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}