【问题标题】:PineScript doesnt calculate a SL, TP and quantity size properlyPineScript 无法正确计算 SL、TP 和数量大小
【发布时间】:2022-11-20 18:25:50
【问题描述】:

我需要一些关于我编写的简单策略代码的帮助。这是我的第一个代码。 我对用 PineScript 编写的 mon 代码有疑问。

PineScript 没有正确计算我的 SL、TP 和数量大小,我不知道为什么。

我的策略是: 当 1 根红色蜡烛后连续 4 根绿色蜡烛时,在第 5 根蜡烛(无论是红色还是绿色)上进入多头头寸。

止损价格 =(红色蜡烛中第 4 根绿色蜡烛的收盘价 + 红色蜡烛中第 2 根绿色蜡烛的开盘价)/2

Diff = 入场价 - 止损价之间的差异。 该价格值将用于计算 TP 价格

目标价 = 入场价 + (2*差异) “2”表示我有 2 的风险回报,我冒 1 的风险赢得 2。

另外,我想在我账户余额的每笔交易中承担 1% 的风险。 例如,如果我的账户余额是 200 000 美元,我想冒 2000 美元的风险 因此,如果差异(入场价格与止损价格之间的差异)为 2 美元,我想以 2000 美元/2 美元的价格购买 1000 个单位的 ETH 份额。 根据我的风险回报,我所有的亏损交易应始终为我账户余额的 1%,而我所有的盈利交易应始终为 2%。但是当我查看交易时,百分比并没有遵循任何东西。

但是 PineScript 并没有正确地做到这一点。 它确实检测到我想要交易的模式并在第 5 个蜡烛中进入,但退出点无法正常工作,无论是 SL 或 TP 和数量。

我不知道我对 SL 的说明是否有误或其他原因。 你有什么主意吗 ?

这是我当前的代码,请参见下面的一些交易图片:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5

strategy("4 Green Candle Strategy", overlay=true,shorttitle = "4GCS")

// 1. User Input //

i_rewardmulti = input.float(2.0, "Risk Reward", minval = 1.0, step = 0.5, group = "4 Green Candle Strategy Settings")
i_risk = input.float(1.0, "Percentage to risk per trade", minval = 0.1, step = 0.05, group = "4 Green Candle Strategy Settings")
ibtstarttime = input.time(title="Start Backtest", defval=timestamp("01 Jan 2022 00:00 +0000"), group="Backtest Period")
ibtendtime = input.time(title="End Backtest", defval=timestamp("01 Jan 2099"), group="Backtest Period")

// ---------------------------------------------------- Strategy Settings -----------------------------------------------//

// 2. Conditions of a valid setup //
ValidSetup = close[4] < open[4] and close[3] > close[4] and close[2] > close[3] and close[1] > close[2] and close > close[1] and barstate.isconfirmed

// 3. Confirmation of a valid setup //
ValidLong = ValidSetup and strategy.position_size==0 and barstate.isconfirmed

// 4. Calculation of TP, SL, balance risked and position size risked //

EntryPrice = close
long_SLprice = (close + open[2])*0.5
long_diff_EntryPrice_and_StopLossExitPrice = close - long_SLprice
long_TPprice = EntryPrice + (i_rewardmulti * long_diff_EntryPrice_and_StopLossExitPrice)
balance = (strategy.initial_capital + strategy.netprofit)
balance_limited = (balance > 0 ? balance : 0)
balance_risked = (i_risk/100) * balance
position_size_risked = (balance_risked/long_diff_EntryPrice_and_StopLossExitPrice)

// 5. Save of SL, TP and position size if a valid setup is detected //
var trade_entry = 0.0
var trade_SL = 0.0
var trade_TP = 0.0
var trade_direction = 0

// 6. Detection of a valid long and trigger alerts //
trade_entry := EntryPrice
trade_SL := long_SLprice
trade_TP := long_TPprice
trade_direction := 1

// 7. Enter a trade whenever a valid setup is detected //
if ValidLong
    strategy.entry("Long", strategy.long, qty=position_size_risked)

// 8. Exit a trade whenever a TP or SL is hit //
if strategy.position_size > 0
    strategy.exit("Long Exit", from_entry = "Long", limit= trade_TP, stop = trade_SL)

// 9. Draw trade data and Price action setup arrow //
plot (series = strategy.position_size !=0 and ValidLong ? trade_SL : na, title = "Trade Stop Price", color=color.red, style = plot.style_linebr)
plot (series = strategy.position_size !=0 and ValidLong ? trade_TP : na, title = "Trade TP Price", color=color.green, style = plot.style_linebr)
plotshape(series = ValidLong ? 1 : na, style =shape.triangleup, location = location.belowbar, color=color.green, title = "Bullish Setup")


// ------------------------------------------------------ End of Code -------------------------------------------------//

enter image description here

通常在 22.00(晚上 10 点)开始的交易中,在检测到 1 根红色蜡烛后有 4 根绿色蜡烛后,根据我的 TP 策略,它应该在 TP 值为 1.84 美元后以 1212.84 美元退出,但它以更高的价格退出它应该做的。 利润百分比为 1.57%,而它应该始终为 2%,您可以在下面的亏损交易中看到,当它应该始终为 1% 时,损失百分比为 0.12%。

你知道它为什么能正常工作吗? 我的代码有错误吗?

谢谢, 乌尔里希

我试图将蜡烛的参考数字更改为蜡烛 [2] 到蜡烛 [1],检测出错。

【问题讨论】:

    标签: pine-script pinescript-v5


    【解决方案1】:

    你写 :
    ' 例如,如果我的账户余额是 200 000 美元,我想冒 2000 美元的风险所以如果 Diff(入场价格 - SL 价格之间的差异)是 2 美元,我想购买 1000 个单位的 ETH 份额为 2000 美元/ 2 美元。

    但是要以 2000 美元的价格购买 1000 件,您将需要 2 000 000 美元。
    Pinescript 知道你有 200 000 美元,它会购买 100 个单位。
    您的盈利交易的 2% 计算是基于 1000 个单位,而不是 100 个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-27
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      相关资源
      最近更新 更多