你好,我正在尝试创建一个松树策略,一旦价格上涨 3% 或下跌 1% 就会平仓,任何人都可以指导这就是我目前所拥有的
//@version=5
startDate = input(title="Start Date",defval=15)
startMonth = input(title="Start Month",defval=1)
startYear = input(title="Start Year",defval=2023)
afterStartDate = (time >= timestamp(syminfo.timezone,startYear, startMonth, startDate, 0, 0))
investment = input.int(defval=1000, title='INVESTMENT USDT')
comm=0.08
strategy('BS WAB with lines', shorttitle='WAB', overlay=true, default_qty_value = 1000, commission_type = strategy.commission.percent, commission_value = comm)
// Definitions: Price and Timeframes
src = input(close, title='Source')
resolution = timeframe.period
price = request.security(syminfo.tickerid, resolution, src)
// Defintions: Moving Average periods and types
ma1Period = input.int(defval=8, title='Period', inline='1', group='Moving Averages: Zone 1')
ma1Type = input.string(defval='EMA', title='Type', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='MA Smoothing', inline='1', group='Moving Averages: Zone 1')
ma2Period = input.int(defval=21, title='Period', inline='2', group='Moving Averages: Zone 1')
ma2Type = input.string(defval='EMA', title='Type', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='MA Smoothing', inline='2', group='Moving Averages: Zone 1')
// Moving Average Calculation
ma1 = ma1Type == 'EMA' ? ta.ema(price, ma1Period) : ma1Type == 'SMA' ? ta.sma(price, ma1Period) : ma1Type == 'WMA' ? ta.wma(price, ma1Period) : ma1Type == 'HMA' ? ta.hma(price, ma1Period) : ma1Type == 'LIN' ? ta.linreg(price, ma1Period, 0) : na
ma2 = ma2Type == 'EMA' ? ta.ema(price, ma2Period) : ma2Type == 'SMA' ? ta.sma(price, ma2Period) : ma2Type == 'WMA' ? ta.wma(price, ma2Period) : ma2Type == 'HMA' ? ta.hma(price, ma2Period) : ma2Type == 'LIN' ? ta.linreg(price, ma2Period, 0) : na
// Definitions: Trends
TrendUp1() =>
ma1 > ma2
TrendDown1() =>
ma1 < ma2
trendColor1 = TrendUp1() ? color.green : TrendDown1() ? color.red : color.blue
p_ma1 = plot(ma1)
p_ma2 = plot(ma2)
fill(p_ma1, p_ma2, color=trendColor1)
plotshape(ma1 > ma2 and ma1[1] < ma2[1], style=shape.triangleup, color= color.rgb(59, 255, 108), size=size.small, text="B", location=location.belowbar, title='Buy shades', offset=1)
plotshape(ma1 < ma2 and ma1[1] > ma2[1], style=shape.triangledown, color= color.rgb(255, 0, 0), size=size.small, text="S", location=location.abovebar, title='Sell shades', offset=1)
mainComment=str.tostring(high[1])
strategyAmount=investment/close
longCondition = ma1 > ma2 and ma1[1] < ma2[1]
if (longCondition and afterStartDate)
strategy.entry("BUY", strategy.long, qty=strategyAmount, comment = mainComment)
shortCondition = ma1 < ma2 and ma1[1] > ma2[1]
if (shortCondition and afterStartDate)
strategy.entry("SELL", strategy.short, qty=strategyAmount, comment = mainComment)
如果有人可以帮助我,在此示例中创建一个出口,将不胜感激
你基本上想要基于百分比的 TP 和 SL。
使用
strategy.position_avg_price
获得您的平均头寸规模,然后计算从那里上涨 3% 和下跌 1%。然后使用strategy.exit()
功能下达您的退出订单。
对于多头头寸,如下所示:
tp_per = 0.03
sl_per = 0.01
tp_price = strategy.position_avg_price * (1 + tp_per)
sl_price = strategy.position_avg_price * (1 - sl_per)
if (strategy.position_size > 0)
strategy.exit("LE", "BUY", stop=sl_price, limit=tp_price)
我的建议,复制你的代码,打开聊天 gpt,告诉聊天 gpt 优化代码以包含你需要的参数,在你的参数后面粘贴你的代码,按回车键。
这就是我最近在做的,而且效果很好。
希望有帮助。