Old Code

 avatar
s7s
java
5 months ago
9.9 kB
15
Indexable

// |---------------------------------------------------------------------|
// |------------------------------------ B e g i n ----------------------|
// |---------------------------------------------------------------------|
// ===========================================   
// ------  ( Swing ) with Filter Range  ------
// ===========================================
//  1# InPut : 
gr_Swing = "------|  Swing For Signal BY / SELL  |------" 
rng_src  = input(High,'Source','1',gr_Swing)                 //Range Source
rng_per  = input.int(20,'Period',1,30,1,'','1', gr_Swing)    //Range Period
rng_qty  = input.float(defval=3.5,title='Mult',minval=0.0000001,inline = '2',group = gr_Swing)//Range Size 
// function :
rng_filt(x, rng_, n) =>
    r = rng_
    var rfilt = array.new_float(2, x)
    array.set(rfilt, 1, array.get(rfilt, 0))
    if x - r > array.get(rfilt, 1)
        array.set(rfilt, 0, x - r)
    if x + r < array.get(rfilt, 1)
        array.set(rfilt, 0, x + r)
    rng_filt1 = array.get(rfilt, 0)

    hi_band = rng_filt1 + r
    lo_band = rng_filt1 - r
    rng_filt = rng_filt1
    [hi_band, lo_band, rng_filt]
rng_size(x, qty, n) =>
    wper = n * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), n)
    AC = ta.ema(avrng, wper) * qty
    rng_size = AC
    rng_size
//
//Range Filter Values
[h_band, l_band, filt] = rng_filt(rng_src, rng_size(rng_src, rng_qty, rng_per), rng_per)
// 
// ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
// 
// ===========================================   
// ---------   Direction Conditions    -------
// ===========================================                            
//
var fdir = 0.0
fdir        := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir
upward      = fdir == 1 ? 1 : 0
downward    = fdir == -1 ? 1 : 0
filt_color  = upward ? #05ff9b : downward ? #ff0583 : #cccccc       //Colors
bar_color   = upward and rng_src > filt ? rng_src > rng_src[1] ? #05ff9b : #00b36b : downward and rng_src < filt ? rng_src < rng_src[1] ? #ff0583 : #b8005d : #cccccc

// ===========================================   
// ---------    Trading Condition      -------
// ===========================================
// 
longCond  = rng_src > filt and rng_src > rng_src[1] and upward > 0 or rng_src > filt and rng_src < rng_src[1] and upward > 0
shortCond = rng_src < filt and rng_src < rng_src[1] and downward > 0 or rng_src < filt and rng_src > rng_src[1] and downward > 0

CondIni = 0
CondIni         := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition   = longCond and CondIni[1] == -1
shortCondition  = shortCond and CondIni[1] == 1

//Plot Buy and Sell Labels
plotshape(longCondition , title='Buy Signal' , text='BUY' , textcolor=color.white, style=shape.labelup  , size=size.normal, location=location.belowbar, color=color.new(color.green, 0))
plotshape(shortCondition, title='Sell Signal', text='SELL', textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.new(color.red  , 0))
//Alerts
alertcondition(longCondition , title='Buy Alert' , message='BUY' )
alertcondition(shortCondition, title='Sell Alert', message='SELL')
// DIMAK - TimeFrame request.security - Repaint-Non Repaint
f_security(_sym, _res, _src, _rep) =>
    request.security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]
// DIMAK - TimeFrame request.security - Repaint-Non Repaint

// ===========================================   
// ---------    SL / TP   Long entry   -------
// ===========================================
Longin_TP             = input.float(3 ,'Take Profit (%)', group=GRUPO_statSLTP, minval=0.0, step=0.5)
Longin_SL             = input.float(1 ,'Stop Loss (%)'  , group=GRUPO_statSLTP, minval=0.0, step=0.5)
Long_TP = Longin_TP / 100
Long_SL = Longin_SL / 100
Long_SL := USE_SignSL ? Long_TP : Long_SL
Long_TP_EX  = strategy.position_avg_price * (1 + Long_TP )
Long_SL_EX  = strategy.position_avg_price * (1 - Long_SL )
// 
displTPSL = not USEdisplTPSL ? display.none : display.all
// visual TP SL
avg_position_price_plot = plot(series=strategy.position_size != 0 ? strategy.position_avg_price : na, color=color.new(#c2bfbf, 25), style=plot.style_linebr, linewidth=1, title="Precio Entrada", display = displTPSL)
LONG_tp_plot= plot(strategy.position_size > 0 and Long_TP_EX > 0.0 ? Long_TP_EX : na, color=color.new(color.lime, 60), style=plot.style_linebr, linewidth=1, title="LONG Take Profit", display = displTPSL)
LONG_sl_plot= plot(strategy.position_size > 0 and Long_SL_EX > 0.0? Long_SL_EX : na, color=color.new(color.red, 60), style=plot.style_linebr, linewidth=1, title="Long Stop Loss", display = displTPSL)
fill(avg_position_price_plot, LONG_tp_plot, color=color.new(color.olive, 90), display = displTPSL)
fill(avg_position_price_plot, LONG_sl_plot, color=color.new(color.maroon, 90), display = displTPSL)

SHORT_tp_plot = plot(strategy.position_size < 0 and short_TP_EX > 0.0 ? short_TP_EX : na, color=color.new(color.lime, 60), style=plot.style_linebr, linewidth=1, title="SHORT Take Profit", display = displTPSL)
SHORT_sl_plot = plot(strategy.position_size < 0 and short_SL_EX > 0.0 ? short_SL_EX : na, color=color.new(color.red, 60), style=plot.style_linebr, linewidth=1, title="Short Stop Loss", display = displTPSL)
fill(avg_position_price_plot, SHORT_tp_plot, color=color.new(color.olive, 90), display = displTPSL)
fill(avg_position_price_plot, SHORT_sl_plot, color=color.new(color.maroon, 90), display = displTPSL)



// 3. MATRICES / ARRAYS ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//—————————— FIRST VARIABLES
var int[]          row          = array.from(0) 
var int[]          col          = array.from(0)
var matrix<float>  matrixFloat  = na
col_float = array.new_float()
var matrix<line>   matrixLine   = na
var int[]           colLine     = array.from(0)
var matrix<label>  matrixLabel  = na
var int[]           colLabel    = array.from(0)
//—————————— FUNCTIONS
row(row) => 
    if barstate.isfirst
        get = array.get(row,0)
        array.set(row,0,get + 1)
        get
col(col) => 
    if barstate.isfirst
        get = array.get(col, 0)
        array.set(col,0,get+ 1)
        get
//—————————— ROWS
var L0 = row(row), var L1 = row(row), var L2 = row(row), var L3 = row(row), var L4 = row(row)
var S5 = row(row), var S6 = row(row), var S7 = row(row), var S8 = row(row), var S9 = row(row)

//—————————— COLUMNS
// Prices
var Entry_Price     = col(col)
var Exit_Price      = col(col)
var Stop_Price      = col(col)
var TSL_Price       = col(col)
var TP1_Price       = col(col)
var TP2_Price       = col(col)
// Max & Min prices reached
var Max_Reached = col(col)
var Min_Reached = col(col)
// Event Counters
var Entry_Count         = col(col)
var In_Trade_Bar_Count  = col(col)
var TSL_Hit_Count       = col(col)
var TP1_Hit_Count       = col(col)
var TP2_Hit_Count       = col(col)
var Fail_Hit_Count      = col(col)
// Bar Counter per Event type
var TP1_Bars_Count  = col(col)
var TP2_Bars_Count  = col(col)
var TP3_Bars_Count  = col(col)
var TP4_Bars_Count  = col(col)
var TSL_Bars_Count  = col(col)
var Fail_Bars_Count = col(col)
// Event Booleans 
var Entry_is_Active         = col(col)
var TP1_Hit                 = col(col)
var TP2_Hit                 = col(col)
var TSL_Hit                 = col(col)
var Fail_Hit                = col(col)
// Trade tracking results and calculations
var Number_of_Wins          = col(col)     
var Number_of_Losses        = col(col)
var Win_Percent             = col(col)
var Loss_Percent            = col(col)
var Avg_Win_RR              = col(col)
var Avg_Loss_RR             = col(col)
var Avg_Risk_Reward         = col(col)

var RRT_Fail_Hit_Percentage = col(col)
var RRT_TP1_Hit_Percentage  = col(col)
var RRT_TP2_Hit_Percentage  = col(col)
var RRT_TSL_Hit_Percentage  = col(col)

var RRT_Avg_Fail_Bars       = col(col)
var RRT_Avg_TP1_Bars        = col(col)
var RRT_Avg_TP2_Bars        = col(col)


//—————————— LINES / LABELS / BACKTESTING BOXES 

var a_entryPriceLabel   = array.new_label()
var a_newLine           = array.new_line ()
var a_newLabel          = array.new_label()

var a_newBox          = array.new_box  ()


if array.size   (a_entryPriceLabel ) >  50
    label.delete(array.pop(a_entryPriceLabel))
if  array.size   (a_newLine ) > 100
    line.delete (array.pop(a_newLine))
if array.size   (a_newLabel     ) > 350
    label.delete(array.pop(a_newLabel))
cleanBox(bx) =>
    if array.size  (bx) > 450
        box.delete (array.pop(bx))
//
cleanBox(a_newBox)

//
var Entry_Line  = col(colLine)
var SL_Line     = col(colLine) 
var TP1_Line    = col(colLine)
var TP2_Line    = col(colLine)
var TSL_Line    = col(colLine)

var Entry_Label = col(colLabel)
var SL_Label    = col(colLabel) 
var TP1_Label   = col(colLabel)
var TP2_Label   = col(colLabel)
var TSL_Label   = col(colLabel)

//—————————— FILL MATRIX

if barstate.isfirst
    matrix      := matrix.new<float> (array.get(row, 0), array.get(col,0),0.)  
    matrixLine  := matrix.new<line>  (array.get(row, 0), array.get(colLine , 0), na)        
    matrixLabel := matrix.new<label> (array.get(row, 0), array.get(colLabel, 0), na)        

    for i = 0 to array.si(row, 0) - 1
        for j = 0 to array.get(col_float,0)- 1
            matrix.set(matrix,i,j,close)
//