define_Indicators


(I) => {
  const indicators = {
    macd: new I.MACD([10, 26, 9]) // initialize MACD indicator and set the variable name to be macd
  }

  return indicators // return indicators object, contain all of indicators defined above and will be used in other sections
}

on_Price_Update

({ HFS, HFU }) => async (state = {}, update = {}) => {
	/**
    - 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.
**/
  const whenLong = async (state = {}, update = {}) => {
	  // This extracts the price and market timestamp from the update object. It’s like saying, "Get the latest price from the market data."
    const { price, mts } = update
    
    // Access macd indicator values object from state
    const { macd } = HFS.indicatorValues(state)

		// Check if macd value is below signal line
    if (macd.macd < macd.signal) {
	    // This closes the position (sells the asset) at the market price.
      return HFS.closePositionMarket(state, { price, mtsCreate: mts })
    }

    return state // This returns the updated state object.
  }

  const lookForTrade = async (state = {}, update = {}) => {
	  // This extracts the price and market timestamp from the update object. It’s like saying, "Get the latest price from the market data."
    const { price, mts } = update
    
    // This access macd as indicator object, not values object
    const { macd } = HFS.indicators(state)
    
    // Get current macd value
    const current = macd.v()
    
    // Get previous macd value
    const previous = macd.prev()

		// Check if length of macd line is less than 2 points. Meaning there's not previous macd value
    if (macd.l() < 2) {
      return state
    }

		/**
		- This variable is presenting if the macd line is cross over the signal line of not
			- Check if current macd value is greater than or equal current signal value
			- Check if previous macd value is less than or equal previous signal value
			- If 2 above conditions met, crossOver value equal True, else equal False
		**/
    const crossedOver =
      current.macd >= current.signal && previous.macd <= previous.signal

		// If crossOver value equal True
    if (crossedOver) {
	    // This opens a long position (buys the asset) at the market price with amount is 1% of user allocation.
      return HFS.openLongPositionMarket(state, {
        mtsCreate: mts,
        amount: 1,
        price,
      })
    }

    return state
  }

  const position = HFS.getPosition(state) // This checks if there’s an open trade position by looking at the state object.

	// This checks if there is not any open positions
  if (!position) {
		  // Call lookForTrade function declared above
      return lookForTrade(state, update)
  }

	// This check if there's open long position
  if (HFS.isLong(state)) {
		  // Call whenLong function declared above
      return whenLong(state, update)
  }

  return state
}