Normally we do not use SL for BTC/USD trading, but for Altcoins, we strongly advise to use SL function.
//@version=5
strategy("SpotMartingaleBacktester[capayam.com]", overlay=false, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, pyramiding=99, commission_type=strategy.commission.percent, commission_value=0.12)
// This backtester is to be used for testing before entering into Kucoin Spot Martingale setting
// In Kucoin setting, it does not include first buy in the max position;
// That means if u put the setting here as max Pos 8, in Kucoin the position is 7 [+1 to include the IB]
// Default setting is 100USDT capital with Multiplier 2x, the IB as follows:
// ...Max pos 5, IB 3.23usdt
// ...Max pos 6, IB 1.59usdt
// ...Max pos 7, IB 0.79usdt
//... Max pos 8, IB 0.37usdt
// Default setting is 100USDT capital with Multiplier 1.5x, the IB as follows:
// ...Max pos 5, IB 7.58usdt
// ...Max pos 6, IB 4.81usdt
// ...Max pos 7, IB 3.11usdt
// ...Max pos 8, IB 2.03usdt
// Inputs
percentageDrop = input.float(1.1, title="Percentage Drop(%)", step=0.1) / 100
maxPositions = input.int(8, title="Max Positions[in Kucoin setting,please -1]", minval=1)
multiplier = input.float(2, title="Multiplier", minval=1, step=0.1)
takeProfitPct = input.float(1.1, title="Take Profit Percentage (%)", step=0.1) / 100
initialCapital = input.float(0.37, title="Initial Buy(adjust according to MaxPositions)USDT", step=0.1)
stopLossPct = input.float(99, title="SL(%)", step=0.1) / 100
show_time = input(true, title="---For specific date backtesting---")
start_year = input(2024, title="Start Year")
start_month = input.int(1, title="Start Month (1-12)", minval=1, maxval=12)
start_day = input.int(1, title="Start Day (1-31)", minval=1, maxval=31)
stop_year = input(2026,title="Stop Year")
stop_month = input.int(12, title="Stop Month (1-12)", minval=1, maxval=12)
stop_day = input.int(31, title="Stop Day (1-31)", minval=1, maxval=31)
// Create timestamps from the input dates
start_trading_date = timestamp(start_year, start_month, start_day, 0, 0)
stop_trading_date = timestamp(stop_year, stop_month, stop_day, 23, 59)
// Variables
var float averageEntry = na
var float totalPositionSize = 0
var float totalCost = 0
var int positionCount = 0
var float lastPositionSize = na // Track the last added position size
// Price Levels
dropLevel = na(averageEntry) ? na : averageEntry * (1 - percentageDrop)
takeProfitLevel = na(averageEntry) ? na : averageEntry * (1 + takeProfitPct)
stopLossLevel = na(averageEntry) ? na : averageEntry * (1 - stopLossPct)
// Entry Condition
if na(averageEntry) and positionCount == 0 and (time >= start_trading_date) and (time <= stop_trading_date)
initialQty = initialCapital / close // Directly calculate without rounding
strategy.entry("Initial Buy", strategy.long, qty=initialQty)
averageEntry := close
totalPositionSize := initialQty
totalCost := initialQty * close
positionCount := 1
lastPositionSize := initialQty // Initialize the last position size
if not na(averageEntry) and close <= dropLevel and positionCount < maxPositions
newPositionSize = lastPositionSize * multiplier // Calculate based on the last position size
strategy.entry("Add Position", strategy.long, qty=newPositionSize)
totalCost := totalCost + newPositionSize * close
totalPositionSize := totalPositionSize + newPositionSize
averageEntry := totalCost / totalPositionSize
positionCount := positionCount + 1
lastPositionSize := newPositionSize // Update the last position size
// Take Profit Condition
if not na(averageEntry) and close >= takeProfitLevel
strategy.close("Initial Buy")
strategy.close("Add Position")
averageEntry := na
totalPositionSize := 0
totalCost := 0
positionCount := 0
lastPositionSize := na // Reset the last position size
// Stop-Loss Condition (Fixed)
if not na(averageEntry) and close <= averageEntry * (1 - stopLossPct)
strategy.close_all()
averageEntry := na
totalPositionSize := 0
totalCost := 0
positionCount := 0
lastPositionSize := na // Reset the last position size
Comments
Post a Comment