Pinescript设置头寸大小

时间:2020-07-19 12:05:01

标签: pine-script trading

我的strategy()完全正常运行,但现在我正在尝试管理交易中的资金投放方式。

这是我当前的情况:
我将SL设置为最后10个柱的最低低点,将TP设置为1.5xSL。
我的strategy.exit:

strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP)

直到这里,一切正常。

问题:
即使我使用:

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)

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

我想要的内容:
我有1000欧元的资本。
我希望我的SL(已经设置为最近10个柱的最低低点)为我的本金的1%= 10€。
我的目标价格是1.5xSL,所以应该是15€。
这意味着我每损失一笔交易,我都会损失10欧元,而我每赢得一笔交易,我就会赢得15欧元。 但这不是我所拥有的: enter image description here

问题:
我该如何实现?

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

//@version=4
strategy("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)

// 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 + (1.5 * longDiffSL) : longTP[1]

// ENTRY/EXIT
if longCondition
  strategy.entry("LONG", strategy.long)
  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)

1 个答案:

答案 0 :(得分:3)

如果您真的想在整个脚本中使用起始资本来设定止损点的大小,则需要使用以下命令将其值保存在第一个小节上:

var initialCapital = strategy.equity

如果您想使用当前资产,请使用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)

enter image description here

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


[2020.09.05编辑]

在编写此解决方案时,我忽略了strategy.initial_capital

var initialCapital = strategy.initial_capital

可以代替:

var initialCapital = strategy.equity