let refresh_rate = 15;
let stocknum = 40;
let close_rate = 1.07;
let loss_rate = 0.93;
let rsi_rate = 1.0001;
let trade_rate = 0.5;
let xs = 4 / 500;
let base_index_level = 2000;
let base_cap_floor = 3;
let base_cap_ceiling = 28;

fn band_start(current_price, base_index_level, xs, base_cap_floor) {
  if current_price == base_index_level {
    base_cap_floor
  } else if current_price > 0 {
    round((current_price - base_index_level) * xs + base_cap_floor)
  } else {
    base_cap_floor
  }
}

fn band_end(current_price, base_index_level, xs, base_cap_ceiling) {
  if current_price == base_index_level {
    base_cap_ceiling
  } else if current_price > 0 {
    round((current_price - base_index_level) * xs + base_cap_ceiling)
  } else {
    base_cap_ceiling
  }
}

strategy("microcap_volume_trend_000852") {
  market("CN_A")
  benchmark("000852.SH")
  signal("000852.SH")

  rebalance.every_days(refresh_rate).at("10:18")

  universe.exclude("paused", "st", "kcb", "one_yuan", "new_listing")

  selection.limit(stocknum)
  selection.market_cap_band(
    field="market_cap",
    lower=band_start(signal_close, base_index_level, xs, base_cap_floor),
    upper=band_end(signal_close, base_index_level, xs, base_cap_ceiling)
  )

  risk.index_exposure(
    signal_ma5 > signal_ma10 * rsi_rate ? 1.0 : trade_rate
  )

  filter.stock_expr(
    stock_ma5 > stock_ma10 * rsi_rate &&
    stock_ma10 > stock_ma30 * rsi_rate &&
    rolling_mean("volume", 5) < rolling_mean("volume", 60)
  )

  risk.take_profit(close_rate)
  risk.stop_loss(loss_rate)
  allocation.buy_scale(touched_upper_limit ? 1.0 : trade_rate)

  ordering.rank_by("market_cap", "asc")
}
