Pinescript set a position size

ぃ、小莉子 提交于 2020-12-01 12:56:00

问题


My strategy() is fully working but now I'm trying to manage how money is put in the trade.

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.

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)

回答1:


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

If instead you want to use your current equity, then use strategy.equity.

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 EDIT]

I overlooked strategy.initial_capital when writing this solution.

var initialCapital = strategy.initial_capital

can be used instead of:

var initialCapital = strategy.equity


来源:https://stackoverflow.com/questions/62980054/pinescript-set-a-position-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!