Pinescript 设置位置大小 [英] Pinescript set a position size
问题描述
我的策略() 完全有效,但现在我正在努力管理如何在交易中投入资金.
My strategy() is fully working but now I'm trying to manage how money is put in the trade.
这是我目前的情况:
我将 SL 设置在最后 10 根柱线的最低点,将 TP 设置为 1.5xSL.
我的 strategy.exit :
HERE'S MY CURRENT SITUATION :
I have a SL set at the lowest low of the last 10 bars and a TP set at 1.5xSL.
My strategy.exit :
strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP)
到这里为止,一切正常.
Until here, everything is working fine.
问题:
即使我使用:
THE PROBLEM :
Even though I use :
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)
资金并没有按照我想要的方式进行交易.
The money is not put in the trade the way I want.
我想要什么:
我有 1000 欧元的资本.
我希望我的 SL(已经设置在最近 10 根柱线的最低点)是我资本的 1% = 10 欧元.
我的 TP 是 1.5xSL,所以它是 15 欧元.
这意味着对于我输掉的每笔交易,我会损失 10 欧元,对于我赢的每笔交易,我会赢取 15 欧元.
但这不是我所拥有的:
WHAT I WANT :
I have a capital of 1000€.
I want my SL (which is already set at the lowest low of the last 10 bars) to be 1% of my capital = 10€.
My TP being 1.5xSL, so it would be 15€.
Meaning that for each trade I lose, I lose 10€ and for each trade I win, I win 15€.
But this is not what I have :
问题:
我怎样才能做到这一点?
QUESTION :
How can I achieve this ?
这是我的代码(仅适用于多头头寸):
HERE'S MY CODE (only for long positions) :
//@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)
推荐答案
如果您真的想在整个脚本中使用起始资金来调整止损的大小,则需要使用以下方法将其值保存在第一个柱上:
If you really want to use your starting capital throughout your script to size your stops, you will need to save its value on the first bar using:
var initialCapital = strategy.equity
如果您想使用当前的资产,请使用 strategy.equity
.
If instead you want to use your current equity, then use strategy.equity
.
这里我们使用第一个选项.诀窍是使用止损的值来调整仓位的大小,以便止损代表您愿意承担风险的目标资本百分比,然后在 strategy.entry()
调用中使用qty=
参数.
Here we use the first option. The trick is to size the position using the stop's value so that the stop represents the target % of capital that you are willing to risk, and then specify that size in the strategy.entry()
call using the qty=
parameter.
您可以通过脚本的输入更改键值:
You can change the key values through your script's Inputs:
//@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)
管理风险的好方法.恭喜.
Great way to manage your risk. Congrats.
[2020.09.05 编辑]
[2020.09.05 EDIT]
在编写此解决方案时,我忽略了 strategy.initial_capital
.
I overlooked strategy.initial_capital
when writing this solution.
var initialCapital = strategy.initial_capital
可以代替:
var initialCapital = strategy.equity
这篇关于Pinescript 设置位置大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!