Pine Script Tutorial

//@version=3

strategy("MA_strategy" , shorttitle="MA_strategy", overlay=true, initial_capital=100000)

SMA =input(10, minval=1)

s=sma(close,SMA)

plot(s, color=yellow,linewidth=2) // Plots the MA

buy= close>s

sell= close<s

ordersize=floor(strategy.equity/close) // To dynamically calculate the order size as the account equity increases or decreases.

strategy.entry("long",strategy.long,ordersize,when=buy)

strategy.close("long", when = sell )

strategy.exit( "exit long", from_entry="long" , profit=p,loss=sl,trail_points=tl,trail_offset=os)

//@version=3

strategy("MAcross_strategy", shorttitle="MAcross_strategy", overlay=true, initial_capital=100000)

SMA_Fast =input(10, minval=1) // To input period for 1st sma, default period set as 10

SMA_Slow =input(20, minval=1)

s1=sma(close,SMA_Fast) // sma values stored in s1 and s2 variables

s2=sma(close,SMA_Slow)

plot(s1, color=yellow,linewidth=2) // Plots the MA

plot(s2, color=red,linewidth=2)

buy= crossover(s1,s2) // Define our buy/sell conditions, using pine inbuilt functions.

sell= crossunder(s1,s2)

ordersize=floor(strategy.equity/close) // To dynamically calculate the order size as the account equity increases or decreases.

strategy.entry("long",strategy.long,ordersize,when=buy) // Buys when buy condition met

strategy.close("long", when = sell ) // Closes position when sell condition met

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store