Eduardo
Eduardo

Reputation: 307

How to get the default_qty_value in PineScript?

I'm coding a strategy and I want to perform calculations based on the default_qty_value, which is set in the strategy settings. Yes, I know I can use the strategy.position_avg_price to get the position size, but these calculations must be done before the strategy is open.

Is there a way to retrieve this value, or is it necessary for the user to make an input?

This is the code.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EduardoMattje
//@version=4

strategy("Reversal closing price", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000)

// Settings

order_direction = input("Both", "Order direction", options=["Both", "Long", "Short"])
reward_risk_ratio = input(2.0, "Reward to risk ratio", minval=1.0, step=0.1)
stop_lookback = input(3, "Stoploss candle lookback", minval=1)
allow_retracing = input(true, "Allow price retracing")
disregard_trend = input(false, "Allow trades to take place regardless of the trend")
ma_cross_stop = input(false, "Close if MA crosses in oposite direction")
src = input(hl2, "Price source")

// MA calculation and plot

ma_type = input("EMA", "Moving average type", options=["EMA", "HMA", "SMA", "WMA"])
ma_long_period = input(80, "MA long period")
ma_short_period = input(8, "MA short period")
ma_long = ma_type == "EMA" ? ema(src, ma_long_period) : ma_type == "HMA" ? hma(src, ma_long_period) : ma_type == "SMA" ? sma(src, ma_long_period) : wma(src, ma_long_period)
ma_short = ma_type == "EMA" ? ema(src, ma_short_period) : ma_type == "HMA" ? hma(src, ma_short_period) : ma_type == "SMA" ? sma(src, ma_short_period) : wma(src, ma_short_period)
ma_bull = disregard_trend == true? true : ma_short > ma_long
ma_bear = disregard_trend == true? true : ma_short < ma_long
plot(ma_long, "MA long", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)
plot(ma_short, "MA short", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)

// RCP calculation

rcp_bull = low[0] < low[1] and low[0] < low[2] and close[0] > close[1]
rcp_bear = high[0] > high[1] and high[0] > high[2] and close[0] < close[1]

// Order placement

in_market = strategy.position_size != 0

bought = strategy.position_size[0] > strategy.position_size[1] and strategy.position_size[1] == 0
sold = strategy.position_size[0] < strategy.position_size[1] and strategy.position_size[1] == 0
closed = not in_market and in_market[1]
long_position = strategy.position_size > 0
short_position = strategy.position_size < 0

long_condition = rcp_bull and not in_market and order_direction != "Short" and ma_bull
short_condition = rcp_bear and not in_market and order_direction != "Long" and ma_bear 

buy_price = high + syminfo.mintick
sell_price = low - syminfo.mintick

// Stop loss orders

stop_price = long_position ? valuewhen(bought, lowest(stop_lookback)[1] - syminfo.mintick, 0) : short_position ? valuewhen(sold, highest(3)[1] + syminfo.mintick, 0) : na
stop_comment = "Stop loss triggered"
strategy.close("Long", low <= stop_price, stop_comment)
strategy.close("Short", high >= stop_price, stop_comment)
plot(stop_price, "Stop price", color.red, 2, plot.style_linebr)

// MA cross close orders

if ma_cross_stop
    if long_position and ma_bear
        strategy.close("Long", comment=stop_comment)
    if short_position and ma_bull
        strategy.close("Short", comment=stop_comment)

// Take profit orders

stop_ticks = abs(strategy.position_avg_price - stop_price)
target_ticks = stop_ticks * reward_risk_ratio
take_profit_price = long_position ? valuewhen(bought, strategy.position_avg_price + target_ticks, 0) : short_position ? valuewhen(sold, strategy.position_avg_price - target_ticks, 0) : na
target_comment = "Take profit"
strategy.close("Long", high >= take_profit_price, target_comment)
strategy.close("Short", low <= take_profit_price, target_comment)
plot(take_profit_price, "Target price", color.green, 2, plot.style_linebr)

//

if long_condition
    strategy.entry("Long", true, qty= order_size, stop=buy_price)
if short_condition
    strategy.entry("Short", false, stop=sell_price)

// Price retracing orders

if allow_retracing
    better_price_long = barssince(closed) > barssince(long_condition) and barssince(long_condition) >= 1 and not in_market and ma_bull and buy_price < valuewhen(long_condition, buy_price, 0) and buy_price[0] < buy_price[1]
    if better_price_long
        strategy.cancel("Long")
        strategy.entry("Long", true, stop=buy_price)
    
    better_price_short = barssince(closed) > barssince(short_condition) and barssince(short_condition) >= 1 and not in_market and ma_bear and sell_price > valuewhen(short_condition, sell_price, 0) and sell_price[0] > sell_price[1]
    if better_price_short
        strategy.cancel("Short")
        strategy.entry("Short", false, stop=sell_price)

Upvotes: 7

Views: 2720

Answers (1)

Eduardo
Eduardo

Reputation: 307

After a few trials, I managed to do what we brazilians call a "gambiarra".

In the very first bar, I open a position and I save the size of the position in a var called risk_size. This is the amount of money that was put in that first order, so that is the amount of money that the next orders are going to use as risk.

You can alter it to be contracts, money, or whatever. Until Trading View implements a built-in variable, this will have to do.

// Get risk size

var start_price = 0.0
var risk_size = 0.0

if barstate.isfirst
    strategy.entry("Get order size", true)
    start_price := close

if barssince(barstate.isfirst) >= 2 and strategy.position_entry_name == "Get order size"
    risk_size := strategy.position_size * start_price
    strategy.close("Get order size")

Upvotes: 3

Related Questions