Pinescript 设置仓位大小

Pep*_*peW 5 trading pine-script

我的策略()完全有效,但现在我正在尝试管理资金投入交易的方式。

\n

这是我目前的情况:
\n我的止损设置为最后 10 个柱线的最低点,止盈设置为 1.5 倍止损。
\n我的策略.退出:

\n
strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP)\n
Run Code Online (Sandbox Code Playgroud)\n

到这里为止,一切都运行良好。

\n

问题:
\n即使我使用:

\n
strategy("TEST MACD DEFAULT", shorttitle="MACD", overlay=true, initial_capital=1000, default_qty_type=strategy.equity, default_qty_value=1, currency=currency.EUR, process_orders_on_close=true, pyramiding=0)\n
Run Code Online (Sandbox Code Playgroud)\n

钱没有按照我想要的方式投入交易。

\n

我想要什么:
\n我的资本为 1000\xe2\x82\xac。
\n我希望我的止损(已设置为最后 10 个柱中的最低点)为我资本的 1% = 10\xe2\x82\xac。
\n我的 TP 是 1.5xSL,所以它是 15\xe2\x82\xac。
\n这意味着对于我输掉的每笔交易,我会输掉 10\xe2\x82\xac,而对于我赢的每笔交易,我会赢取 15\xe2\x82\xac。
\n但这不是我所拥有的:\n在此输入图像描述

\n

问题: \n
\n我怎样才能做到这一点?

\n

这是我的代码(仅适用于多头头寸):

\n
//@version=4\nstrategy("TEST MACD DEFAULT", shorttitle="MACD", overlay=true, initial_capital=1000, default_qty_type=strategy.cash, default_qty_value=10, currency=currency.EUR, process_orders_on_close=true, pyramiding=0)\n\n// MACD\n[macdLine, signalLine, _] = macd(close, 12, 26, 9)\n\n// EMA 200\nema = ema(close, 200)\nplot(ema, title="EMA 200", color=color.yellow, linewidth=2)\n\n// LONG CONDITIONS\nlongCheckCondition = barssince(crossover(macdLine, signalLine))\nlongCondition1 = longCheckCondition <= 3 ? true : false\nlongCondition2 = macdLine < 0 and signalLine < 0\nlongCondition3 = close > ema\nlongCondition = longCondition1 and longCondition2 and longCondition3 and strategy.opentrades == 0\n\n// STOP LOSS\nfloat longSL = na\nlongSL := longCondition ? lowest(low, 11)[1] : longSL[1]\n\n// TAKE PROFIT\nlongEntryPrice = close\nlongDiffSL = abs(longEntryPrice - longSL)\nfloat longTP = na\nlongTP := longCondition ? close + (1.5 * longDiffSL) : longTP[1]\n\n// ENTRY/EXIT\nif longCondition\n  strategy.entry("LONG", strategy.long)\n  strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP)\n\n// PLOT STOP LOSS\nlongPlotSL = strategy.opentrades > 0 and strategy.position_size > 0 ? longSL : na\nplot(longPlotSL, title=\'LONG STOP LOSS\', linewidth=2, style=plot.style_linebr, color=color.red)\n\n// PLOT TAKE PROFIT\nlongPlotTP = strategy.opentrades > 0 and strategy.position_size > 0 ? longTP : na\nplot(longPlotTP, title=\'LONG TAKE PROFIT\', linewidth=2, style=plot.style_linebr, color=color.green)\n
Run Code Online (Sandbox Code Playgroud)\n

Pin*_*ucF 9

如果您确实想在整个脚本中使用起始资金来确定止损的大小,则需要使用以下方法将其值保存在第一个柱上:

var initialCapital = strategy.equity
Run Code Online (Sandbox Code Playgroud)

如果您想使用当前的权益,请使用strategy.equity

这里我们使用第一个选项。诀窍是使用止损值确定头寸规模,以便止损代表您愿意承担风险的目标资本百分比,然后使用strategy.entry()参数在调用中指定该规模qty=

您可以通过脚本的输入更改键值:

//@version=4
strategy("TEST MACD DEFAULT", shorttitle="MACD", overlay=true, precision = 4, initial_capital=1000, default_qty_type=strategy.cash, default_qty_value=10, currency=currency.EUR, process_orders_on_close=true, pyramiding=0)
i_pctStop = input(1., "% of Risk to Starting Equity Use to Size Positions") / 100
i_tpFactor = input(1.5, "Factor of stop determining target profit")

// Save the strat's equity on the first bar, which is equal to initial capital.
var initialCapital = strategy.equity

// MACD
[macdLine, signalLine, _] = macd(close, 12, 26, 9)

// EMA 200
ema = ema(close, 200)
plot(ema, title="EMA 200", color=color.yellow, linewidth=2)

// LONG CONDITIONS
longCheckCondition = barssince(crossover(macdLine, signalLine))
longCondition1 = longCheckCondition <= 3 ? true : false
longCondition2 = macdLine < 0 and signalLine < 0
longCondition3 = close > ema
longCondition = longCondition1 and longCondition2 and longCondition3 and strategy.opentrades == 0

// STOP LOSS
float longSL = na
longSL := longCondition ? lowest(low, 11)[1] : longSL[1]

// TAKE PROFIT
longEntryPrice = close
longDiffSL = abs(longEntryPrice - longSL)
float longTP = na
longTP := longCondition ? close + (i_tpFactor * longDiffSL) : longTP[1]

positionValue = initialCapital * i_pctStop / (longDiffSL / longEntryPrice)
positionSize = positionValue / longEntryPrice

// ENTRY/EXIT
if longCondition
    strategy.entry("LONG", strategy.long, qty=positionSize)
    strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP)

// PLOT STOP LOSS
longPlotSL = strategy.opentrades > 0 and strategy.position_size > 0 ? longSL : na
plot(longPlotSL, title='LONG STOP LOSS', linewidth=2, style=plot.style_linebr, color=color.red)

// PLOT TAKE PROFIT
longPlotTP = strategy.opentrades > 0 and strategy.position_size > 0 ? longTP : na
plot(longPlotTP, title='LONG TAKE PROFIT', linewidth=2, style=plot.style_linebr, color=color.green)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

管理风险的好方法。恭喜。


[2020.09.05 编辑]

strategy.initial_capital在编写这个解决方案时忽略了。

var initialCapital = strategy.initial_capital
Run Code Online (Sandbox Code Playgroud)

可以用来代替:

var initialCapital = strategy.equity
Run Code Online (Sandbox Code Playgroud)