maximum net profit for a variable range

20 Views Asked by At

I am trying to display maximum strategy.netprofit for a range of a variable (l1). It is not displaying maximum value of the strategy netprofit but is changing with max value of l1 (l1MaxIn). Is there a simpler way to achieve this? Thank you, George

I` want to display max value of netprofit for a strategy when a variable varies in a domain (l1). Instead, the expected max value changes with l1 max value, that is l1MaxIn. Is there a simpler way to achieve this (i.e. for loops, etc). Thank you, George

`// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © george10001000

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)
l1In=input.int(2)
l2In=input.int(20)
l1MaxIn=input.int(10)
var l1=int(l1In)
var l2=int(l2In)
var profit1=float(-1000000)
var l1_best=int(0)

backTestStart=input.time(timestamp("1 Jan 2023 12:00"), title="Start backtest")



longCondition = ta.crossover(ta.sma(close, l1), ta.sma(close, l2)) and time>backTestStart and time>backTestStart
if (longCondition)
    strategy.entry("Buy", strategy.long)

shortCondition = ta.crossunder(ta.sma(close, l1), ta.sma(close, l2))
if (shortCondition)
    strategy.close_all()
    if strategy.netprofit>profit1
        profit1:=strategy.netprofit
        l1_best:=l1


if (barstate.islast)
    label.new(bar_index,high+2,str.tostring(profit1,".###"),color=color.white)
    label.new(bar_index,high+3,str.tostring(l1_best,"##"),color=color.yellow)


if l1<l1MaxIn
    l1:=l1+1``
0

There are 0 best solutions below