What It Does

This handler runs whenever there’s a new price update, like a new candle (price chart) or trade. Think of it as a trigger that activates your strategy every time the market moves.


State and Update Breakdown


Examples

Example 1: Simple Price Alert

/**
    - 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 { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
  
  if (price > 100) { // This checks if the price is greater than 100. If true, the code inside the {} runs.
  
    console.log("Price is above 100!"); // This prints a message to the console. It’s useful for debugging or setting up alerts.
  }
  
  return state; // This returns the updated state object. It’s like saying, "Save the current state of the strategy."
}


Example 2: Moving Average Crossover

/**
    - 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 { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
  
  const candles = HFS.getCandles(state); // This gets historical candle data from the state object. Candles are used to analyze past price movements.
  
  if (candles.length < 20) return state; // This checks if there are at least 20 candles. If not, the function exits early.

  const shortMA = calculateMA(candles, 10); // This calculates the 10-period moving average using the calculateMA function.
  const longMA = calculateMA(candles, 20); // This calculates the 20-period moving average using the calculateMA function.

	// This checks if the short moving average is above the long moving average and no trade is open.
  if (shortMA > longMA && !HFS.getPosition(state)) {
    // 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."
}


Example 3: RSI Overbought/Oversold

/**
    - 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 { price } = update; // This extracts the price from the update object. It’s like saying, "Get the latest price from the market data."
  
  const candles = HFS.getCandles(state); // This gets historical candle data from the state object. Candles are used to analyze past price movements.
  
  if (candles.length < 14) return state; // This checks if there are at least 14 candles. If not, the function exits early.

  const rsi = calculateRSI(candles, 14); // This calculates the 14-period RSI using the calculateRSI function.
  
  // This checks if the RSI is above 70 (overbought) and a trade is open.
  if (rsi > 70 && HFS.getPosition(state)) {
    // This closes the position (sells the asset) at the market price.
    return await HFS.closePositionMarket(state, { price });
  }
  // If RSI is below 30 (oversold) and no position is open, buy
  else if (rsi < 30 && !HFS.getPosition(state)) {
    // 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."
}