What It Does

This handler runs when a short position (a sell trade) is updated. It allows you to manage and adjust your sell trades based on the latest market data.


State and Update Breakdown


Examples

Example 1: Take Profit

/**
    - 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 short position (sell trade).
  if (position && position.type === "short") {
    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% below the entry price.
    if (price <= position.price * 0.9) {
	    // 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."
}


Example 2: Dynamic Stop-Loss

/**
    - 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 short position (sell trade).
  if (position && position.type === "short") {
    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 * 1.05; // This sets the initial stop-loss at 5% above the entry price. If a stop-loss already exists, it uses that value.
    
    // This checks if the price has moved below the current stop-loss level.
    if (price < stopLoss) {
      state.stopLoss = price * 1.05; // This updates the stop-loss to 5% above the new price.
    }
    // This checks if the price has risen to or above 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."
}


Example 3: Add to Position

/**
    - 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 short position (sell trade).
  if (position && position.type === "short") {
    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% above the entry price.
    if (price >= position.price * 1.05) {
	    // This sells more of the asset at the current price if the price rises by 5%.
      return await HFS.openShortPositionMarket(state, { price });
    }
  }
  
  return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}