Example 4: Stop-Loss Implementation

This example demonstrates how to implement a stop-loss that works across all sell steps. The key is that every sell step must check the stop-loss condition first before checking its own logic.

class Strategy {
  constructor() {
    this.strategy = {
      data: {
        token: {
          address: "",
          creationTime: 0
        },
        priceTicks: []
      },
      steps: [
        {
          run: (entry, current) => this.buyOnDip(entry, current),
          label: "Buy on 10% dip"
        },
        {
          run: (entry, current) => this.firstTakeProfit(entry, current),
          label: "Sell 50% at 3x"
        },
        {
          run: (entry, current) => this.secondTakeProfit(entry, current),
          label: "Sell remaining at 5x"
        }
      ],
      context: {}
    }
  }

  /**
   * Stop-loss helper function
   * Checks if price has dropped below stop-loss threshold
   * MUST be called in every sell step (not in buy step)
   *
   * @param {number} entryPrice - Buy price
   * @param {number} currentPrice - Current price
   * @returns {object|null} - Sell action if stop-loss hit, null otherwise
   */
  checkStopLoss(entryPrice, currentPrice) {
    const stopLossPercentage = 0.20; // 20% stop-loss
    const lossPercent = (entryPrice - currentPrice) / entryPrice;

    if (lossPercent >= stopLossPercentage) {
      // Price has dropped 20% or more from entry - exit everything
      return {
        sell: true,
        partToSellInPercentage: 100
      };
    }

    return null; // Stop-loss not triggered
  }

  /**
   * Step 1: Buy on 10% dip
   * No stop-loss check here (we haven't bought yet)
   */
  buyOnDip(signalPrice, currentPrice) {
    if (currentPrice <= signalPrice * 0.9) {
      return {
        buy: true,
        partToBuyInPercentage: 100
      };
    }
    return null;
  }

  /**
   * Step 2: First take profit at 3x
   * MUST check stop-loss first before checking take-profit
   */
  firstTakeProfit(entryPrice, currentPrice) {
    // CRITICAL: Check stop-loss FIRST in every sell step
    const stopLossResult = this.checkStopLoss(entryPrice, currentPrice);
    if (stopLossResult) {
      return stopLossResult; // Exit immediately if stop-loss triggered
    }

    // Normal take-profit logic
    const multiplier = currentPrice / entryPrice;
    if (multiplier >= 3.0) {
      return {
        sell: true,
        partToSellInPercentage: 50
      };
    }

    return null;
  }

  /**
   * Step 3: Second take profit at 5x
   * MUST check stop-loss first before checking take-profit
   */
  secondTakeProfit(entryPrice, currentPrice) {
    // CRITICAL: Check stop-loss FIRST in every sell step
    const stopLossResult = this.checkStopLoss(entryPrice, currentPrice);
    if (stopLossResult) {
      return stopLossResult; // Exit immediately if stop-loss triggered
    }

    // Normal take-profit logic
    const multiplier = currentPrice / entryPrice;
    if (multiplier >= 5.0) {
      return {
        sell: true,
        partToSellInPercentage: 100
      };
    }

    return null;
  }
}

Why Check Stop-Loss in Every Sell Step?

Since you can only be on one step at a time, the stop-loss must be checked in whichever step is currently active:

  1. After buying: You're on Step 2 (first take profit)

    • Step 2 must check stop-loss every tick

    • If stop-loss triggers → sell everything and advance

    • If 3x target hits → sell 50% and advance to Step 3

  2. After first sell: You're on Step 3 (second take profit)

    • Step 3 must check stop-loss every tick

    • If stop-loss triggers → sell remaining 50%

    • If 5x target hits → sell remaining 50%

Key Pattern:

Last updated