该策略是一个基于网格的多头 Martingale 动态加仓网格交易策略。主要思想是在价格触及网格线时,根据已有仓位数量动态调整每次的加仓量,同时设置总的最大开仓数量。当价格上涨触及止盈价格时,平掉所有多头仓位。
通过这种方式,在行情下跌过程中逐步加大仓位,当行情回升触及止盈线时获利了结。
风险防范措施: 1. 结合风险承受能力,谨慎设置 “multifactor” 等参数。必要时可在代码中添加止损逻辑。 2. 仔细回测和模拟交易,选择合适的参数。 3. 评估策略在高波动行情下的表现。如果必要,可通过调整参数或限制使用场景等方式规避风险。
这些优化可以提高策略适应性,更好地把握行情,提高盈利潜力和稳健性。同时通过更精细的仓位控制和风险管理,降低回撤,提高风险收益比。
该基于网格的多头 Martingale 动态加仓网格交易策略,通过逐步加仓的方式试图在行情下跌过程中降低持仓均价,并在上涨时获利了结。策略通过参数设置具有较强的灵活性。但同时也潜在较大风险,需要仔细评估和控制风险。若能在此基础上加入趋势判断、动态止盈、仓位优化等,有望进一步提升策略性能。策略实现了价格触及网格线时自动开仓加仓,价格触及止盈线时自动全平仓的功能,整体逻辑较为清晰,但也有优化空间。策略适合在对行情和参数进行充分评估的基础上谨慎使用。
/*backtest
start: 2023-03-16 00:00:00
end: 2024-03-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lagerta13
//@version=4
strategy("Grid A.", shorttitle="Grid(A)", overlay=true, format=format.price, precision=4, pyramiding = 100)
input_tf=input("15", "Started TimeFrame",
options = ["1", "5", "15", "30", "1H", "4H", "1D", "1W", "1M"],
group="TimeFrame")
// avg_tf=input("5", "Average TimeFrame",
// options = ["1", "5", "15", "30", "1H", "4H", "1D", "1W", "1M"],
// group="TimeFrame")
slip_Hilo = input(3.0, "Slippage by open order", group="Strategy Settings")
start_lot = input(0.01, "Start lot", group="Strategy Settings")
start_avg = input(2, "Started average since Order #", group="Strategy Settings")
MaxTrades_Hilo = input(10, "Max Open Orders", group="Strategy Settings")
dropdown_selection = input("Only Long", "Direction", options=["Only Long", "Only Short", "Long & Short"],
group="Strategy Settings")
type_selection = input("By Close", "Type input", options=["By Close", "By grid line"],
group="Strategy Settings")
multifactor = input(1.5, "Multifactor", group="Strategy Settings")
precision_lot = input(2, "Number of precision", group="Strategy Settings")
take_profit = input(1, "TakeProfit", group="Strategy Settings")
// PipStep_S1 = input(30)
// PipStepX_S1 = input(1.2)
// dinamicStep = input(false, "Dinamic Step for AVG")
get_size_lot_order(number, multi, prec, avg_from, lot_from) =>
res = lot_from
for i = 1 to number
if i >= avg_from
res := round(res * multi, precision = prec)
res
var float[] entry_levels = array.new_float(MaxTrades_Hilo + 1)
for i = 0 to MaxTrades_Hilo
array.push(entry_levels, 0)
gridSize = input(0.5, title="Grid Size")
gridLevels = int(close / gridSize) * gridSize
var int num_open_orders = 0
var float sum_price_orders = 0
var float entry_lot = 0
buy_condition = num_open_orders < MaxTrades_Hilo and gridLevels[0]<gridLevels[1] and dropdown_selection != "Only Short"
if (buy_condition)
if num_open_orders == 0
lot = get_size_lot_order(num_open_orders, multifactor, precision_lot, start_avg, start_lot)
sum_price_orders := sum_price_orders + gridLevels[1] * lot
strategy.entry("buy" + tostring(num_open_orders), true, qty=lot, limit=gridLevels[1]+slip_Hilo)
// strategy.order("buy" + tostring(num_open_orders), true, qty=lot, limit=gridLevels[1])
array.set(entry_levels, num_open_orders, gridLevels[1])
entry_lot := entry_lot + lot
num_open_orders := num_open_orders + 1
else
if gridLevels[1] < (array.get(entry_levels, num_open_orders - 1))
lot = get_size_lot_order(num_open_orders, multifactor, precision_lot, start_avg, start_lot)
sum_price_orders := sum_price_orders + gridLevels[1] * lot
entry_lot := entry_lot + lot
strategy.entry("buy" + tostring(num_open_orders), true, qty=lot, limit=gridLevels[1]+slip_Hilo)
// +" S:" + tostring(sum_price_orders / (entry_lot)) + " Prev:" + tostring(array.get(entry_levels, num_open_orders - 1))
// strategy.order("buy" + tostring(num_open_orders), true, qty=lot, limit=gridLevels[1])
array.set(entry_levels, num_open_orders, gridLevels[1])
num_open_orders := num_open_orders + 1
take = sum_price_orders > 0 and take_profit + (sum_price_orders / entry_lot) < high ? high : na
plotshape(take, location = location.belowbar, color = color.white)
strategy.exit("tp", comment = "TP " + tostring(num_open_orders), qty = entry_lot, limit = take_profit + (sum_price_orders / entry_lot))
if sum_price_orders > 0 and take_profit + (sum_price_orders / entry_lot) <= high
num_open_orders := 0
sum_price_orders := 0
entry_lot := 0
for i = 0 to MaxTrades_Hilo
array.set(entry_levels, i, 0)
plot(gridLevels, color=color.blue, style=plot.style_circles, linewidth=2)