This handler runs when a long position (a buy trade) is updated. It allows you to manage and adjust your buy trades based on the latest market data.
/**
- 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 the open position is a long position (buy trade).
if (position && position.type === "long") {
const { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
// This checks if the price is 10% above the entry price.
if (price >= position.price * 1.1) {
// This closes the position (sells the asset) at the market price.
return await HFS.closePositionMarket(state, { 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 the open position is a long position (buy trade).
if (position && position.type === "long") {
const { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
const stopLoss = state.stopLoss || 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 > stopLoss) {
state.stopLoss = 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 <= stopLoss) {
// This closes the position (sells the asset) at the market price.
return await HFS.closePositionMarket(state, { 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 the open position is a long position (buy trade).
if (position && position.type === "long") {
const { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
// This checks if the price is 5% below the entry price.
if (price <= position.price * 0.95) {
// This opens a long position (buys the asset) at the market price.
return await HFS.openLongPositionMarket(state, { price });
}
}
return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}