Skip to main content
Latest Crypto Fear & Greed Index


My sets of favourite pine codes (for easy revision)

This post is mainly for my own reference actually, so that i could easily come back grab the lines of pine scripts to be inserted into my pine script strategy. They are all version 5 pine script.

1. Top line 

Strategy with preset of initial capital, order size, and commission percentage:

// @version=5
strategy("Strategy Name [capayam.com]", overlay=false, initial_capital = 100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent , commission_value=0.1)


2. Input groups

They are usually for adjusting Take Profit and Stop Loss level. I use this to set Oversold level as well that determine my entry threshold.

// Input Groups
OverboughtLevel = input.int(50, title="OverboughtLevel")
OversoldLevel = input.int(-50, title="OversoldLevel")


3.  Timestamp

This is useful when backtesting a specified shorter period within a long period of time backtesting range (usually with TF 1hour and above) 

// Input Group
start_year = input(2024, title="Start Year")
start_month = input(1, title="Start Month")
start_day = input(1, title="Start Day")

stop_year = input(2024, title="Stop Year")
stop_month = input(12, title="Stop Month")
stop_day = input(31, title="Stop Day")

// Create timestamps from the input dates
start_trading_date = timestamp(start_year, start_month, start_day, 0, 0)
stop_trading_date = timestamp(stop_year, stop_month, stop_day, 23, 59)


// Define Buy Signal
BuySignal = oversold and (time >= start_trading_date) and (time <= stop_trading_date)


4. Calculate Take Profit or Stop Loss (by percentage)

// Input parameters
profit_target_percent = input(6, title="Profit Target (%)")
stop_loss_percent = input(3, title="Stop Loss (%)")


// Calculate profit target and stop loss levels
entry_price = strategy.position_avg_price
profit_target = entry_price * (1 + profit_target_percent / 100)
stop_loss = entry_price * (1 - stop_loss_percent / 100)

// Exit conditions
strategy.exit("Exit", limit=profit_target, stop=stop_loss)


5. Calculate TP and SL (by Recent swing low & Risk reward ratio)

// Input Groups
swingLowLookback = input.int(5, title="Swing Low Lookback Period")


// Define the stop loss at the recent swing low
recentSwingLow = ta.lowest(low, swingLowLookback)

// Calculate stop loss and take profit levels
entryPrice = close
stopLoss = recentSwingLow
takeProfit = entryPrice + 2 * (entryPrice - stopLoss)

// Strategy entry with stop loss and take profit
if (BuySignal)
    strategy.entry("Buy", strategy.long)
    strategy.exit("Exit", "Buy", stop=stopLoss, limit=takeProfit)


6. Strategy logic and execution

// Define Buy Exit condition
BuySignal = oversold
SellSignal = overbought


// Strategy entry and exit conditions
if (BuySignal)
    strategy.entry("Buy", strategy.long)
   
if (SellSignal)
    strategy.close("Buy")


7. Stop loss OR Close position with profit

// Strategy entry with Stop Loss OR Close position with Profit
if (BuySignal)
    strategy.entry("Buy", strategy.long)
    strategy.exit("StopLoss", "Buy", stop=stopLoss)

if (SellSignal)
    strategy.close("Buy")



Comments