Back to Strategies

Funding Rate Contrarian + RSI Filter

Code Strategy by 0xsDp5JzBDn·2026-07-02


// Env: FUNDING_EXTREME, RSI_PERIOD, INTERVAL, TP_PERCENT, SL_PERCENT

async function getCloses(interval, limit) {
  const res = await $.Http.get("https://fapi.binance.com/fapi/v1/klines", {
    symbol: $.Price.symbol,
    interval,
    limit
  });
  if (res.status !== 200) return null;
  const raw = JSON.parse(res.text);
  return raw.map(c => parseFloat(c[4]));
}

async function getFundingRate() {
  const res = await $.Http.get("https://fapi.binance.com/fapi/v1/premiumIndex", {
    symbol: $.Price.symbol
  });
  if (res.status !== 200) return null;
  const data = JSON.parse(res.text);
  return parseFloat(data.lastFundingRate) * 100; // as percent
}

const FUNDING_EXTREME = Number($.Env.FUNDING_EXTREME || 0.05); // percent
const RSI_PERIOD = Number($.Env.RSI_PERIOD || 14);
const INTERVAL = $.Env.INTERVAL || "15m";
const TP_PERCENT = Number($.Env.TP_PERCENT || 1.5);
const SL_PERCENT = Number($.Env.SL_PERCENT || 1.0);

let S = (await $.Storage.get()) || { position: null, entryPrice: null };
let nextAction = null;

if (S.position) {
  const roi = $.Strategy.roi(true, S.entryPrice, $.Price.price);
  if (roi !== null) {
    const profit = S.position === "LONG" ? roi : -roi;
    if (profit >= TP_PERCENT || profit <= -SL_PERCENT) {
      nextAction = { type: $.Action.EXIT };
    }
  }
}

if (!nextAction && !S.position) {
  const funding = await getFundingRate();
  const closes = await getCloses(INTERVAL, RSI_PERIOD * 3);

  if (funding !== null && closes && closes.length > RSI_PERIOD) {
    const rsi = new $.Ta.RSI(RSI_PERIOD);
    for (const c of closes) rsi.update(c);

    if (rsi.isStable) {
      const rsiVal = rsi.getResult();

      if (funding >= FUNDING_EXTREME && rsiVal > 55) {
        nextAction = { type: $.Action.SHORT, side: "SHORT" };
      } else if (funding <= -FUNDING_EXTREME && rsiVal < 45) {
        nextAction = { type: $.Action.LONG, side: "LONG" };
      }
    }
  }
}

if (nextAction) {
  await $.Strategy.action(nextAction.type);
  if (nextAction.type === $.Action.EXIT) {
    S.position = null;
    S.entryPrice = null;
  } else {
    S.position = nextAction.side;
    S.entryPrice = $.Price.price;
  }
}

await $.Storage.set(S);