What It Does

This handler runs when a new trade position is opened. It allows you to perform actions like logging the entry price, setting a stop-loss, or initializing custom data when a trade starts.


State and Update Breakdown


Examples

Example 1: Log Entry Price

/**
    - 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 logs the entry price of the new position to the console.
  console.log(`New position opened at price: ${position.price}`);
  
  return state; // This returns the updated state object.
}


Example 2: Set 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.
  
  state.stopLoss = position.price * 0.95; // This sets a stop-loss at 5% below the entry price by multiplying the entry price by 0.95.
  
  return state; // This returns the updated state object.
}


Example 3: Initialize Custom 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 = {}) => {
	/**
	- This initializes or updates the `tradeCount` variable:
    - If `tradeCount` doesn’t exist, it starts at 0.
    - If it does exist, it increments by 1.
	**/
  state.tradeCount = (state.tradeCount || 0) + 1;
  
  return state; // This returns the updated state object.
}