format19
format19

Reputation: 11

Limit intraday trading if daily loss exceeds a set limit - pine

I am writing a TradingView Strategy and want to limit intraday trading to a set daily profit or loss.

I have the limit working on daily profit but I can not get the daily loss bit working.

I followed Andrey D's post here to get the daily profit working How to calculate each day's profit in Trading View Strategy Tester

this is the code that is working for me

//**** Daily Net Profit Limit***** var useNetProfit = input.bool(true, title = "Limit to Daily Net Profit", confirm=false, group="Positions") var useNetLoss = input.bool(false, title = "Limit to Daily Net Loss", confirm=false, group="Positions") dailyNetProfitLimit = input(title="Daily Profit Limit", defval=40, group="Positions") dailyNetLossLimit = input(title="Daily Loss Limit", defval=40, group="Positions")

canTrade(dailyNetProfitLimit)=>
    var bool canTrade = false
    tD = time("D")
    var float dailyProfitStart = na
    if tD!=tD[1] or na(dailyProfitStart) and useNetProfit // new day or start
        dailyProfitStart := strategy.netprofit
        canTrade := true
    if strategy.netprofit - dailyProfitStart >= dailyNetProfitLimit and useNetProfit
       canTrade := false

    canTrade
canTrade = canTrade(dailyNetProfitLimit)

//**** End Daily Net Profit Limit ********************

then use the "cantrade" value in my strategy entry conditions if canTrade strategy.entry('Long', strategy.long)

This all works and I can toggle the feature on and off in the settings.

I tried creating a second "function" called cantrade2 for daily losses but it doesnt work

canTrade2(dailyNetLossLimit)=>
    var bool canTrade2 = false
    tD = time("D")
    var float dailyLossStart = na
    if tD!=tD[1] or na(dailyLossStart) and useNetLoss // new day or start
        dailyLossStart := strategy.netprofit
        canTrade2 := true
    if strategy.netprofit <= dailyLossStart - dailyNetLossLimit and useNetLoss
        canTrade2 := false
    canTrade2

canTrade2 = canTrade2(dailyNetLossLimit)

Upvotes: 1

Views: 681

Answers (1)

mr_statler
mr_statler

Reputation: 2161

You can use strategy.risk.max_intraday_loss() function to set the maximum lost.

The maximum loss value allowed during a day. It is specified either in money (base currency), or in percentage of maximum intraday equity (0 -100).

It take few parameters:

value (simple int/float) A required parameter. The maximum loss value. It is specified either in money (base currency), or in percentage of maximum intraday equity. For % of equity the range of allowed values is from 0 to 100.

type (simple string) A required parameter. The type of the value. Please specify one of the following values: strategy.percent_of_equity or strategy.cash. Note: if equity drops down to zero or to a negative and the strategy.percent_of_equity is specified, all pending orders are cancelled, all open positions are closed and no new orders can be placed for good.

alert_message (simple string) An optional parameter which replaces the {{strategy.order.alert_message}} placeholder when it is used in the "Create Alert" dialog box's "Message" field.

Here's the example from documentation:

// Input for maximum intraday loss %. 
lossPct = input.float(10)

// Set maximum intraday loss to our lossPct input
strategy.risk.max_intraday_loss(lossPct, strategy.percent_of_equity)

Upvotes: 0

Related Questions