Optimizing Pinscript Code for best variables to Run all possible combination

34 Views Asked by At

This Code is just an example we take 2 emas and their min, max and step value then i get the total posible combinations possible for example. indicator Name SMA Crossover Strategy Name Value Input Seconds 1

Input LONG SMA From 50 Input LONG SMA To 60 Input LONG SMA Step 1

Input Short SMA From 20 Input Short SMA To 30 Input Short SMA Step 1

i get the total combinations 121 now i can manually increment the value of "Input Seconds" from 1 to 121 and save results and then chose the best combination. right now i am using extremal extension for that.

if i could somehow run it with in the code that will be very efficient and time saving.

//@version=5

ema(inval)=>    
    shortSma = ta.sma(close, math.floor(inval))    

strategy("SMA Crossover Strategy", overlay=true)



var seconds_Total = 0
var minutes_Total = 0
var hours_Total = 0
var days_Total = 0


var sec_Val = 0
var min_Val = 0
var hr_Val = 0
var day_val = 0

var c_sec_Val = 0.00
var c_min_Val = 0.00
var c_hr_Val = 0.00
var c_day_val = 0.00

var tol_combs = 0
var new_inp = 0


nin = input.int(10, "Input Seconds")
new_inp := nin


mfrom = input.float(50, "Input LONG SMA From ", group ="Long SMA")
mto = input.float(60, "Input LONG SMA To ")
mstep = input.float(1, "Input LONG SMA Step ")


sfrom = input.float(20, "Input Short SMA From " , group = "Short SMA")
sto = input.float(30, "Input Short SMA To ")
sstep = input.float(1, "Input Short SMA Step ")


seconds_Total := math.round(((sto - sfrom)/sstep)+1)
minutes_Total := math.round(((mto - mfrom)/mstep)+1)

int minutes = nin / seconds_Total  
nin %= seconds_Total
int seconds = nin 


sec_Val := math.floor(seconds)
min_Val := math.floor(minutes)

c_sec_Val := ( sec_Val * sstep ) + sfrom
c_min_Val := ( min_Val * mstep ) + mfrom


tol_combs := seconds_Total * minutes_Total 


// Calculate the SMAs
shortSma = ema(c_sec_Val)
longSma = ema(c_min_Val)

// Generate buy and sell signals
buySignal = ta.crossover(shortSma, longSma)
sellSignal = ta.crossunder(shortSma, longSma)


if (time != time[1])
    message = 
     "\n\n shortSma = "                   + str.tostring(c_sec_Val) +
     "\n longSma = "                             + str.tostring(c_min_Val) +
     "\n total_max_combinations = "          + str.tostring(tol_combs) 

    l= label.new(x=bar_index, y=high+0.5, text=message, textcolor=color.new(color.green, 0), style=label.style_none)
    label.delete(l[1])    



// Plot the SMAs on the chart
plot(shortSma, color=color.blue, title="Short SMA")
plot(longSma, color=color.red, title="Long SMA")

// Plot buy and sell signals on the chart
plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Execute trades based on the signals
if (buySignal)
    strategy.entry("Long", strategy.long)

if (sellSignal)
    strategy.close("Long", "Long")

i have used external extensions so far but very time consuming

0

There are 0 best solutions below