CodeRe: Already Converted TradingView Indicators to MT4 Indicators

381
Good evening guys, how are you? I don't know if you've seen this Luxalgo gem, but in my opinion it makes backtesting incredibly easy.

it takes your base indicator and imputes two targets of your choice in ticks, % or ATR.

It makes it much easier to configure the best parameters within the indicators.

I'm putting the code + indicator here just for information, but I believe it's worth studying.

Code: Select all

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Targets For Overlay Indicators [LuxAlgo]", shorttitle = "LuxAlgo - Targets For Overlay Indicators", overlay = true, max_lines_count = 500, max_labels_count = 500)
//-----------------------------------------------------------------------------}
//Target 1
//-----------------------------------------------------------------------------{
source = input.source(close)
showLabels = input(true, 'Show Target Labels')
candleColoring = input(true, 'Candle Coloring')

//Crossover Target
enableTarget1 = input(true, 'Show Crossover Target'
  , group     = 'Crossover Source')

isLong1       = input(true, 'Above Price Target'
  , inline    = 'enable'
  , group     = 'Crossover Source')

//Target 1 Logic
waitTarget1   = input(false, 'Wait Until Reached'
  , group     = 'Crossover Source')

newTarget1    = input(false, 'New Target When Reached'
  , group     = 'Crossover Source')

useWicks1     = input(true, 'Evaluate Wicks'
  , group     = 'Crossover Source')

distTarget1   = input.float(3, 'Target Distance From Price'
  , inline    = 'dist1'
  , group     = 'Crossover Source')

distOptions1  = input.string('ATR', ''
  , options   = ['Currencies', '%', 'ATR', 'Ticks']
  , inline    = 'dist1'
  , group     = 'Crossover Source')

target1Css    = input(#089981, 'Target Color '
  , inline    = 'style'
  , group     = 'Crossover Source')

target1Style  = input.string('- - -', '    Levels Style'
  , options   = ['──','- - -','· · ·']
  , inline    = 'style'
  , group     = 'Crossover Source')

//-----------------------------------------------------------------------------}
//Target 2
//-----------------------------------------------------------------------------{
//Crossunder Target
enableTarget2 = input(true, 'Show Crossunder Target'
  , group     = 'Crossunder Source')

isLong2       = input(false, 'Above Price Target'
  , inline    = 'enable'
  , group     = 'Crossunder Source')

//Target 1 Logic
waitTarget2   = input(false, 'Wait Until Reached'
  , group     = 'Crossunder Source')

newTarget2    = input(false, 'New Target When Reached'
  , group     = 'Crossunder Source')

useWicks2     = input(true, 'Evaluate Wicks'
  , group     = 'Crossunder Source')

distTarget2   = input.float(3, 'Target Distance From Price'
  , inline    = 'dist1'
  , group     = 'Crossunder Source')

distOptions2  = input.string('ATR', ''
  , options   = ['Currencies', '%', 'ATR', 'Ticks']
  , inline    = 'dist1'
  , group     = 'Crossunder Source')

target2Css    = input(#f23645, 'Target Color '
  , inline    = 'style'
  , group     = 'Crossunder Source')

target2Style  = input.string('- - -', '    Levels Style'
  , options   = ['──','- - -','· · ·']
  , inline    = 'style'
  , group     = 'Crossunder Source')

//-----------------------------------------------------------------------------}
//Dashboard
//-----------------------------------------------------------------------------{
showDash      = input.bool     (    true      , 'Show Dashboard'                                                     , group = 'Dashboard')
dashLoc       = input.string   (  'Top Right' , 'Location'  , options = ['Top Right', 'Bottom Right', 'Bottom Left'] , group = 'Dashboard')
textSize      = input.string   (   'Normal'   , 'Size'      , options =          ['Tiny', 'Small', 'Normal']         , group = 'Dashboard')

//-----------------------------------------------------------------------------}
//UDT
//-----------------------------------------------------------------------------{
type lshape
    line v
    line h

type target
    float  value
    int    loc
    bool   reached
    bool   islong
    bool   active
    lshape lines
    label  lbl

//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
n = bar_index

method set_target(target id, css, lstyle)=>
    style = switch lstyle
        '- - -' => line.style_dashed
        '· · ·' => line.style_dotted
        =>         line.style_solid
    
    id.lines := lshape.new(line.new(n, close, n, id.value, color = css, style = style),
      line.new(n, id.value, n, id.value, color = css, style = style))

method delete(target id)=>
    id.lines.v.delete()
    id.lines.h.delete()

//-----------------------------------------------------------------------------}
//Set crossover target
//-----------------------------------------------------------------------------{
var color css            = na
bool      isNewTarget1   = false
bool      isTgReached1   = false

var int countTargets1   = 0
var int countTgReached1 = 0

var target1_object      = target.new(reached = true, active = false)

target1_condition = ta.crossover(close, source)

//Distance
dist1 = switch distOptions1
    'Currencies' => distTarget1
    '%' => close + distTarget1 / 100 * close
    'ATR' => nz(ta.atr(50)) * distTarget1
    'Ticks' => syminfo.mintick * distTarget1

if target1_object.active and target1_object.reached == false
    target1_object.lines.h.set_x2(n)
    target1_object.lbl.set_x(n)

if (isLong1 ? (useWicks1 ? high : close) > target1_object.value : (useWicks1 ? low : close) < target1_object.value) and target1_object.active 
    target1_object.reached := true
    target1_object.active  := false 
    isTgReached1           := true
    countTgReached1        += 1
    css := na
    target1_object.lbl.set_color(target1Css)

if enableTarget1 and 
 (
  (target1_condition and (waitTarget1 ? target1_object.reached : true)) 
  or 
  (newTarget1 and target1_object.reached)
 ) 
    target_value = close + (isLong1 ? dist1 : -dist1)

    //Delete label if reached and creating new target
    if newTarget1 and target1_object.reached and showLabels
        target1_object.lbl.delete()

    //Create new target
    target1_object := target.new(target_value, n, false, isLong1, active = true)

    if showLabels
        target1_object.lbl := label.new(n, target_value, 'Target'
          , color = color.new(target1Css, 50)
          , textcolor = color.white
          , size = size.tiny
          , style = label.style_label_left
          , tooltip = str.tostring(target_value, format.mintick))

    css := target1Css

    target1_object.set_target(target1Css, target1Style)

    isNewTarget1  := true 
    countTargets1 += 1

//-----------------------------------------------------------------------------}
//Set crossunder target
//-----------------------------------------------------------------------------{
bool isNewTarget2   = false
bool isTgReached2   = false

var int countTargets2   = 0
var int countTgReached2 = 0

var target2_object = target.new(reached = true, active = false)

target2_condition = ta.crossunder(close, source)

//Distance
dist2 = switch distOptions2
    'Currencies' => distTarget2
    '%' => close + distTarget2 / 100 * close
    'ATR' => nz(ta.atr(50)) * distTarget2
    'Ticks' => syminfo.mintick * distTarget2

if target2_object.active and target2_object.reached == false
    target2_object.lines.h.set_x2(n)
    target2_object.lbl.set_x(n)

if (isLong2 ? (useWicks2 ? high : close) > target2_object.value : (useWicks2 ? low : close) < target2_object.value) and target2_object.active 
    target2_object.reached := true
    target2_object.active  := false 
    isTgReached2           := true
    countTgReached2        += 1
    css := na
    target2_object.lbl.set_color(target2Css)

if enableTarget2     and
 (
  (target2_condition and (waitTarget2 ? target2_object.reached : true)) 
  or 
  (newTarget2 and target2_object.reached)
 ) 
    target_value = close + (isLong2 ? dist2 : -dist2)

    //Delete label if reached and creating new target
    if newTarget2 and target2_object.reached and showLabels
        target1_object.lbl.delete()

    //Create new target
    target2_object := target.new(target_value, n, false, isLong2, active = true)

    if showLabels
        target2_object.lbl := label.new(n, target_value, 'Target'
          , color = color.new(target2Css, 50)
          , textcolor = color.white
          , size = size.tiny
          , style = label.style_label_left
          , tooltip = str.tostring(target_value, format.mintick))
    
    css := target2Css

    target2_object.set_target(target2Css, target2Style)

    isNewTarget2  := true 
    countTargets2 += 1

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
barcolor(candleColoring ? css : na, title = 'Candle Coloring')

//-----------------------------------------------------------------------------}
//Dashboard
//-----------------------------------------------------------------------------{
var table_position = dashLoc == 'Bottom Left' ? position.bottom_left 
  : dashLoc == 'Top Right' ? position.top_right 
  : position.bottom_right

var table_size = textSize == 'Tiny' ? size.tiny 
  : textSize == 'Small' ? size.small 
  : size.normal

var tb = table.new(table_position, 3, 4
  , bgcolor      = #1e222d
  , border_color = #373a46
  , border_width = 1
  , frame_color  = #373a46
  , frame_width  = 1)

if showDash

    if barstate.isfirst
        if enableTarget1 or enableTarget2
            tb.cell(0, 0,             ''           , text_color = color.white, text_size = table_size)
            tb.cell(1, 0,          'Count'         , text_color = color.white, text_size = table_size)
            tb.cell(2, 0,    'Target\nReached'     , text_color = color.white, text_size = table_size)
            tb.cell(0, 3,         'Total'          , text_color = color.white, text_size = table_size)

        if enableTarget1
            tb.cell(0, 1,        'Target 1'        , text_color = color.white, text_size = table_size)
        if enableTarget2
            tb.cell(0, 2,        'Target 2'        , text_color = color.white, text_size = table_size)

    if barstate.islast
        totT = countTargets1   + countTargets2        
        totR = countTgReached1 + countTgReached2
 
        if totT == 0
            tb.clear(0, 0, 2, 3)
            tb.cell(0, 0, 'Please Select another input source from the "Source" setting.', text_color = color.white, text_size = table_size)
        else
            if enableTarget1
                tb.cell(1, 1, str.tostring(countTargets1)                                                                       , text_color = color.white, text_size = table_size)
                tb.cell(2, 1, str.format  ('{0} ({1}%)', countTgReached1 , math.round(100 / countTargets1 * countTgReached1, 1)), text_color = color.white, text_size = table_size)
            if enableTarget2
                tb.cell(1, 2, str.tostring(countTargets2)                                                                       , text_color = color.white, text_size = table_size)
                tb.cell(2, 2, str.format  ('{0} ({1}%)', countTgReached2 , math.round(100 / countTargets2 * countTgReached2, 1)), text_color = color.white, text_size = table_size)
            if enableTarget1 or enableTarget2
                tb.cell(1, 3, str.tostring(     totT    )                                                                       , text_color = color.white, text_size = table_size)
                tb.cell(2, 3, str.format  ('{0} ({1}%)',       totR      , math.round(100 /      totT     *       totR     , 1)), text_color = color.white, text_size = table_size)


//-----------------------------------------------------------------------------}
//Alerts
//-----------------------------------------------------------------------------{
alertcondition(isNewTarget1, "Target 1 New"    , "Target 1 New"    )
alertcondition(isTgReached1, 'Target 1 Reached', 'Target 1 Reached')

alertcondition(isNewTarget2, "Target 2 New"    , "Target 2 New"    )
alertcondition(isTgReached2, 'Target 2 Reached', 'Target 2 Reached')

//-----------------------------------------------------------------------------}
These users thanked the author RodrigoRT7 for the post:
galaxy


Re: Already Converted TradingView Indicators to MT4 Indicators

382
RodrigoRT7 wrote: Fri Jan 26, 2024 9:53 am it takes your base indicator and imputes two targets of your choice in ticks, % or ATR.

It makes it much easier to configure the best parameters within the indicators.

I'm putting the code + indicator here just for information, but I believe it's worth studying.
These users thanked the author galaxy for the post:
RodrigoRT7
Yeshua is coming back soon
Only the truth can make free

Watch out for the poison thoughts

Re: Already Converted TradingView Indicators to MT4 Indicators

384
RodrigoRT7 wrote: Fri Jan 26, 2024 11:39 am I think you forgot to include the message. anything just edit and include :)
kvak wrote: Sun Aug 13, 2023 5:25 am Motion To Attraction Channel (MTA Channel) with Entry Arrows & Alerts for "Slope" or "Band Cross"

Hello, I modified my version, have separate menu for arrows/alerts for slope or band cross, test it...

PS: For a full description on the MTA channel indicator, please see here.
These users thanked the author galaxy for the post:
RodrigoRT7
Yeshua is coming back soon
Only the truth can make free

Watch out for the poison thoughts


Re: Already Converted TradingView Indicators to MT4 Indicators

387
This is a really good indicator. Is it possible to implement in MT4?

https://www.tradingview.com/script/hb6J ... 0analysis.

Code: Select all

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

//@version=5
indicator("Trendline Breakouts With Targets [ Chartprime ]",shorttitle = "TBT [ Chartprime ]",overlay = true,max_bars_back = 500,max_lines_count = 500)


bool ChartTime              = time > chart.left_visible_bar_time and time < chart.right_visible_bar_time
   
string CORE                 =  "➞ Core Settings 🔸"
var bool TradeisON          = false
var bool LongTrade          = false
var bool ShortTrade         = false
var float TP                = 0.0
var float SL                = 0.0
int BarTIME                 = time - time[1]
var line tpLine             = na
var label LAB               = na
var int UpdatedX            = 0
var float UpdatedY          = 0.0
var float UpdatedSLP        = 0.0
var int UpdatedXLow         = 0
var float UpdatedYLow       = 0.0
var float UpdatedSLPLow     = 0.0


int Period          = input.int(10, title='     Period     ➞',
     group = CORE,
     inline = "001")

bool Trendtype      = input.string(title = "     Type        ➞",
      defval='Wicks',
      options=['Wicks', 'Body'],
      group = CORE,
      inline = "001")
       == 'Wicks'

string Extensions   = input.string(title='     Extend    ➞', 
     defval='  25',
     options=['  25', '  50', '  75'],
     group = CORE,
     inline = "001")


color LineCol1 = input.color(color.rgb(109, 111, 111, 19),"",group = CORE,inline = "001")
bool ShowTargets = input.bool(true,"Show Targets",group = CORE,inline = "002")
     
ExtenSwitcher(ex) =>
    switch ex 
        '  25' => 1 ,
        '  50' => 2 ,
        => 3


WidthSwitcher(ex) =>
    switch ex 
        '1' => 1 ,
        '2' => 2 ,
        => 3 

StyleSwitcher(style) =>
    switch style 
        'Dashed' => line.style_dashed ,
        'Dotted' => line.style_dotted ,
        => line.style_solid 




method volAdj(int len)=>
    math.min(ta.atr(len) * 0.3, close * (0.3/100)) [20] /2

Zband = volAdj(30)




method Trendlines(float src, int timeIndex,bool dir) =>
    
    var int Start = 1 , var int End = 0 , var int TIME = 1
    var float YEnd = 0, var float YStart = 0 , var float Slope = 0
    var line Line1 = line.new(na,na,na,na)
    var line Line2 = line.new(na,na,na,na)
    var line Line3 = line.new(na,na,na,na)
    
    SCR = fixnan(src)
    if ta.change(SCR) != 0
        TIME := time[timeIndex]
        YStart := SCR[1]
        Start := TIME[1]
        Slope := (SCR - YStart) / (TIME - Start)
        Slope

    EXTime = ExtenSwitcher(Extensions) * BarTIME * 25
    End := TIME + EXTime
    YEnd := SCR + EXTime * Slope
    
    if ta.change(SCR) != 0 and not TradeisON[1]
        LineCond = Slope * time < 0 ? dir ? na : color.rgb(11, 139, 7, 53) : dir ?  color.rgb(212, 46, 0, 54) : na
        if not na(LineCond) //and ChartTime
            Line1 := line.new(Start,
                 YStart,
                 End,
                 YEnd,
                 xloc.bar_time,
                 extend.none,
                 color=color.new(color.white,100)
                 )
    
            Line2:=line.new(Start,
                 YStart - (Zband * 2),
                 End,
                 YEnd - (Zband * 2),
                 xloc.bar_time,
                 extend.none,
                 color=color.new(color.black,100)
                 )
                 
            Line3:=line.new(Start,
                 YStart - (Zband * 1),
                 End,
                 YEnd - (Zband * 1),
                 xloc.bar_time,
                 extend.none,
                 color=color.new(color.black,100)
                 )

            linefill.new(Line3,Line2,color= LineCol1)
            linefill.new(Line3,Line1,color= LineCond)
            // linefill.new(Line,Line2,color= color.rgb(28, 15, 2, 76))
           
    [Start, YStart, Slope]



PH = ta.pivothigh(Trendtype ? high : close > open ? close : open, Period, Period / 2)
PL = ta.pivotlow(Trendtype ? low : close > open ? open : close, Period, Period / 2)




method GetlinePrice(int TIME, float Price, float SLOP, int LookB) =>
    var float Current = 0.0
    EsTime = time - TIME
    Current := Price + (EsTime - LookB * BarTIME) * SLOP
    Current
    

method CheckCross(float Price, int StartTime, float StartPrice, float SLP) =>
    var float Current = 0.0
    var float Previous = 0.0
    if StartPrice[Period] != StartPrice
        Current := GetlinePrice(StartTime, StartPrice, SLP, 0) 
        Previous := GetlinePrice(StartTime, StartPrice, SLP, 1)
        Crossover =  Price[1] < Previous and Price > Current ? 1 : Price[1] > Previous - (Zband*0.1) and Price < Current - (Zband*0.1) ? -1 : 0
        Crossover



[Xx, XZ, SLPXZ] = Trendlines(PH, Period / 2,false)
[XxL, XZL, SLPXZL] = Trendlines(PL, Period / 2, true)




if ta.change(fixnan(PH)) != 0
    UpdatedX := Xx
    UpdatedY := XZ
    UpdatedSLP := SLPXZ
    UpdatedSLP
    
if ta.change(fixnan(PL)) != 0
    UpdatedXLow := XxL
    UpdatedYLow := XZL
    UpdatedSLPLow := SLPXZL
    UpdatedSLPLow

Long = not (UpdatedSLP * time > 0) 
     and CheckCross(close, UpdatedX, UpdatedY, UpdatedSLP)== 1
     and not TradeisON
Short = not (UpdatedSLPLow * time < 0)
     and CheckCross(close, UpdatedXLow, UpdatedYLow, UpdatedSLPLow)==-1
     and not TradeisON


TradeFire = Long or Short

if Long and not TradeisON
    LongTrade:= true
    ShortTrade:= false

if Short and not TradeisON
    LongTrade:= false
    ShortTrade:= true


if true 
    if TradeFire and not TradeisON
        TP := switch
            Long  => high + (Zband *20)
            Short => low - (Zband *20)

        SL := switch
            Long  => low - (Zband *20)
            Short => high + (Zband *20)

        TradeisON:= true
        if ShowTargets
            line.new(bar_index,
                 Long ? high : low,
                 bar_index,
                 TP,
                 width=2,
                 color = color.rgb(154, 103, 20),
                 style= line.style_dashed)

            tpLine:= line.new(bar_index,
                 TP,
                 bar_index+2,
                 TP,
                 style= line.style_dashed,
                 color = color.rgb(154, 103, 20)
                 )
            LAB:=label.new(bar_index,
                 TP,
                 "Target",
                 color = color.rgb(154, 103, 20),
                 style= label.style_label_left,
                 size=size.small,
                 textcolor = color.white
                 )
    if TradeisON
        line.set_x2(tpLine,bar_index)
        label.set_x(LAB,bar_index+1)

    if LongTrade and TradeisON
        if high >= TP
            label.set_color(LAB,color.rgb(6, 128, 10, 37))
            TradeisON:=false
        if close <= SL
            label.set_color(LAB,color.new(color.rgb(246, 7, 7),70))
            TradeisON:=false

    else if ShortTrade and TradeisON

        if low <= TP 
            label.set_color(LAB,color.rgb(6, 128, 10, 37))
            TradeisON:=false
            
        if close >= SL 
            label.set_color(LAB,color.new(color.rgb(246, 7, 7),70))   
            TradeisON:=false



plotshape(Long and not TradeisON[1],
     size = size.small,
     color = color.rgb(46, 192, 6, 11),
     location = location.belowbar,
     style = shape.labelup ,
     text = "",
     textcolor = color.white)

plotshape(Short and not TradeisON[1],
     size = size.small,
     color = color.rgb(241, 2, 2, 11),
     location = location.abovebar,
     style = shape.labeldown ,
     text = "",
     textcolor = color.white)

alertcondition(Long and not TradeisON, "BUY!", "BUY!")
alertcondition(Short and not TradeisON, "SELL!", "SELL!")

// -- END -- .

Re: Already Converted TradingView Indicators to MT4 Indicators

388
mrtools wrote: Tue Jan 23, 2024 12:09 pm Added arrows and alerts to the mtf version.
Hello Mr Tools, once again!!

Is there a possibility of you doing the same update of averages + Reg Ma in this indicator?

I've been exploring a lot of similar ones like Half Trend, Turtle Traders (donchian), Std Error bands.

However, this one seems to be different from the ones I mentioned above. Seems like a great trend follower to me




obs: In advance, I wanted to apologize for asking for a second indicator in less than 24 hours. Definitely not in a rush! but I believe it can be useful for members and friends!

A hug on your s2, I will never be able to thank you enough for everything you did for me and everyone on the forum.

Re: Already Converted TradingView Indicators to MT4 Indicators

389
mrtools wrote: Tue Jan 23, 2024 12:09 pm Added arrows and alerts to the mtf version.
Hello Mr Tools, how are you? so a complement to my suggestion on Follow The Lines. If the RMAs is not compatible with this indicator, there is no problem. Just adding the new averages, it's already excellent :D

This indicator is really very interesting for trend following.

Here's another photo + Template for our friends on the forum.
These users thanked the author RodrigoRT7 for the post (total 6):
ixion700, Tur005, Akela, Jimmy, boytoy, BeatlemaniaSA

CodeRe: Already Converted TradingView Indicators to MT4 Indicators

390
RodrigoRT7 wrote: Mon Feb 26, 2024 2:33 am Hello Mr Tools, how are you? so a complement to my suggestion on Follow The Lines. If the RMAs is not compatible with this indicator, there is no problem. Just adding the new averages, it's already excellent :D

This indicator is really very interesting for trend following.

Here's another photo + Template for our friends on the forum.
Follow The Line with Regularized MA's + All Averages (Filters)

All the averages updated with Regularized Moving Averages too.

PS: For more information on this code, please see: Follow The Line + Angle Of Attack indicators.
These users thanked the author mrtools for the post (total 9):
RodrigoRT7, Jimmy, josi, 太虚一毫, thomdel, Krunal Gajjar, Akela, boytoy, A_5


Who is online

Users browsing this forum: AddyLogger, Amazon [Bot], Applebot [Crawler], kvak, Ruby [Bot], thomdel, WhatsApp [Bot], xard777, YeungKwan and 96 guests