Strategies KB.ZH
Strategies

八阶蜡烛模式序列马丁格尔策略-Eight-Candle-Pattern-Sequence-Martingale-Strategy

ianzeng123 · PineScript · Risk and Position Sizing · 2025-04-08 10:53:33

All strategies →

No English write-up in the source (no author description).

Read the Chinese version →

Source code (68 lines)
/*backtest
start: 2024-04-08 00:00:00
end: 2025-04-07 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/

//@version=6
strategy("SUPRA MTS1", overlay=true)

// Martingale Parameters
initial_bet = input.float(1, title="Initial Bet")         // Initial bet amount
multiplier = input.float(2, title="Martingale Multiplier") // Multiplier in case of loss
max_capital = input.float(100, title="Maximum Capital")    // Loss limit

// Define if the candle is bullish (green) or bearish (red)
is_green = close > open
is_red = close < open

// Create buffers to store the last 8 candles
sequence_green_red =  is_red[7] and is_red[6] and is_red[5] and is_red[4] and is_green[3] and is_red[2] and is_green[1] and is_red
sequence_red_green = is_red[7] and is_red[6] and is_red[5] and is_green[4] and is_red[3] and is_green[2] and is_red[1] and is_green

// Martingale control variables
var float bet = initial_bet
var float current_capital = max_capital

// Counters for buy and sell signals
var int total_buys = 0
var int total_sells = 0

// If the last trade was a loss, double the bet (within capital limit)
new_bet = bet * multiplier
if strategy.opentrades > 0 and strategy.position_size == 0
    bet := new_bet < current_capital ? new_bet : current_capital // Ensure bet doesn't exceed capital

// If won, reset to initial bet
if strategy.opentrades == 0
    bet := initial_bet

// Entry conditions
if sequence_green_red
    strategy.entry("Buy", strategy.long, bet)
    total_buys := total_buys + 1

if sequence_red_green
    strategy.entry("Sell", strategy.short, bet)
    total_sells := total_sells + 1

// Plot signals on the chart
plotshape(sequence_green_red, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy", text="BUY")
plotshape(sequence_red_green, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell", text="SELL")

// Create alert table
var table tbl = table.new(position=position.top_right, columns=2, rows=3, border_width=1)
if bar_index == 0
    table.cell(tbl, 0, 0, "Type", text_color=color.white, bgcolor=color.blue)
    table.cell(tbl, 1, 0, "Total", text_color=color.white, bgcolor=color.blue)
    table.cell(tbl, 0, 1, "Buys", text_color=color.white, bgcolor=color.green)
    table.cell(tbl, 1, 1, str.tostring(total_buys), text_color=color.white, bgcolor=color.green)
    table.cell(tbl, 0, 2, "Sells", text_color=color.white, bgcolor=color.red)
    table.cell(tbl, 1, 2, str.tostring(total_sells), text_color=color.white, bgcolor=color.red)

// Update table on every candle
if bar_index > 0
    table.cell(tbl, 1, 1, str.tostring(total_buys), text_color=color.white, bgcolor=color.green)
    table.cell(tbl, 1, 2, str.tostring(total_sells), text_color=color.white, bgcolor=color.red)