zwp
Pine Script Scholar
Pine Script Scholar
Posts: 17
Joined: September 18th, 2021
Contact: TradingView Profile

Training Script Review - what I can do better

Hi Guys,
I was practicing coding skills on one script and I would like to ask you to review it and tell me what (and how) I can make better/different.
I am interested here in the coding, so discussion if this would work or not is not the main point. Though every comment is welcomed :fist:
Thanks upfront!

Code: Select all


//@version=5
strategy('Cross of Moving Averages - learning script  [zwp]', overlay=true)

//get user input
fastLenght = input(title='Fast Lenght', defval=50)
slowLenght = input(title='Slow Lenght', defval=200)
lookBack   = input.int(title='Look Back for Exit', defval=20)
profitMult = input.float(title='Profit Multiplier', defval=1, step=0.1)
atrLenght  = input.int(title='Atr Lenght', defval=14, step=1)
yearStart  = input.int(title='Year Start', defval=2010)
monthStart = input.int(title='Month Start', defval=1)
dayStart   = input.int(title='Day Start', defval=1)
yearEnd    = input.int(title='Year End', defval=2020)
monthEnd   = input.int(title='Month End', defval=12)
dayEnd     = input.int(title='Day End', defval=31)
fibLevel   = input.float(title='Fibonacci Level', defval = 0.35, step=0.05)

//declare variables
fastMA       = ta.vwma(close, fastLenght)
slowMA       = ta.vwma(close, slowLenght)
bullFib      = high - ((high-low)*fibLevel) //variables for entry signal hammer and star
bearFib      = low + ((high-low)*fibLevel)//variables for entry signal hammer and star
atr          = ta.atr(atrLenght)
strPermLong  = ta.lowest(close,lookBack) > fastMA-atr //condition to enter trade
strPermShort = ta.highest(open,lookBack) < fastMA+atr //condition to enter trade

//entry patterns
bullEG = close[1] < open[1] and close > open[1] //bullish engulfing pattern
bearEG = close[1] > open[1] and close < open[1] //bearish engulfing pattern
hammer = open >= bullFib and close >= bullFib
star   = open <= bearFib and close <= bearFib

//entry rules
entryLong  = (fastMA > slowMA and (bullEG or hammer) and strPermLong)
entryShort = (fastMA < slowMA and (bearEG or star) and strPermShort)

//define stop loss
exitLong  = fastMA - atr
exitShort = fastMA + atr

//calculate entry price
buy_price   =0.0
in_position = (strategy.position_size != 0)
buy_price := in_position and in_position[1] ? buy_price[1] : open

//set profit
profitLongA  = buy_price + (fastMA  - slowMA)
profitShortA = buy_price - (slowMA  - fastMA)
profitLongB  = buy_price + profitMult*(fastMA  - slowMA)
profitShortB = buy_price - profitMult*(slowMA  - fastMA)


//plot MAs to graph
plot(fastMA, color=color.new(color.red, 0))
plot(slowMA, color=color.new(color.blue, 0))

//strategy time fram
timeStart = timestamp(yearStart,monthStart,dayStart)
timeEnd   = timestamp(yearEnd,monthEnd,dayEnd)

if time > timeStart and time < timeEnd
//strategy entry
    if entryLong
        strategy.entry(id='Entry Long', direction=strategy.long)

    if entryShort
        strategy.entry(id='Entry Short', direction=strategy.short)
    
//strategy exit
    if strategy.position_size > 0
        strategy.exit(id='Exit LongA', stop=exitLong, limit=profitLongA, qty_percent=50)
        strategy.exit(id='Exit LongB', stop=exitLong, limit=profitLongB, qty_percent=100)
    if strategy.position_size < 0
        strategy.exit(id='Exit Short', stop=exitShort, limit=profitShortA, qty_percent=50)
        strategy.exit(id='Exit Short', stop=exitShort, limit=profitShortB, qty_percent=100)
        

dusktrader
Pine Script Scholar
Pine Script Scholar
Posts: 32
Joined: February 24th, 2021
Contact: TradingView Profile

Re: Training Script Review - what I can do better

Hey looks like you're making good progress! One thing I learned is that you can simplify date inputs with this new parameter they added in Dec 2020: https://www.tradingview.com/blog/en/new ... ine-21812/
(that allows you to get the entire date/time in one input, instead of multiple inputs)

zwp
Pine Script Scholar
Pine Script Scholar
Posts: 17
Joined: September 18th, 2021
Contact: TradingView Profile

Re: Training Script Review - what I can do better

Thanks @dusktrader . great input! 👍

Return to “Share Your Scripts”