Back to Strategies

Self-Improving Momentum

Code Strategy by 0x7s0lt1·2026-08-10

// Momentum strategy for BTCUSDT
// Entry: price up 1% from the last checkpoint
// Exit: +2% take profit or -1% stop loss
// Also triggers a review every 3 days via a Data Webhook

const REVIEW_INTERVAL_MS = 3 * 24 * 60 * 60 * 1000; // 3 days
const REVIEW_WEBHOOK = "https://hook.sigrex.io/data/xxxx--your-hook-hash-xxxx";

const state = await $.Storage.get() || { checkpoint: null, lastReviewAt: 0 };

if (state.checkpoint === null) {
  // First run — just set the initial checkpoint, no trade yet
  state.checkpoint = $.Price.price;
  await $.Storage.set(state);
} else if ($.Strategy.lastTrigger.action === $.Action.LONG) {
  // We're in a position — check exit conditions
  const roi = $.Strategy.roi(true);

  if (roi !== null && roi <= -1) {
    await $.Strategy.action($.Action.EXIT);
    state.checkpoint = $.Price.price;
    await $.Storage.set(state);
  } else if (roi !== null && roi >= 2) {
    await $.Strategy.action($.Action.EXIT);
    state.checkpoint = $.Price.price;
    await $.Storage.set(state);
  }
} else {
  // No position open — check entry condition
  const changePercent = (($.Price.price - state.checkpoint) / state.checkpoint) * 100;

  if (changePercent >= 1) {
    await $.Strategy.action($.Action.LONG);
    state.checkpoint = $.Price.price;
    await $.Storage.set(state);
  }
}

// Self-trigger a review: every 3 days, ping the Data Webhook that wakes
// up the supervising LLM reaction. This runs independently of the trade
// logic above, so it fires on schedule even on runs with no trade at all.
const now = Date.now();
if (now - state.lastReviewAt >= REVIEW_INTERVAL_MS) {
  await $.Http.post(REVIEW_WEBHOOK, {
    request: "self_check",
    strategyId: $.Strategy.id,
    strategyType: $.Strategy.type,
    lastAction: $.Strategy.lastTrigger.action,
    lastActionAt: $.Strategy.lastTrigger.at,
    checkedAt: now
  });

  state.lastReviewAt = now;
  await $.Storage.set(state);
}