Skip to content

Instantly share code, notes, and snippets.

@vtanathip
Last active November 21, 2024 14:14
Show Gist options
  • Save vtanathip/d9e0a4801fd0bfbf16ab1019e96d4912 to your computer and use it in GitHub Desktop.
Save vtanathip/d9e0a4801fd0bfbf16ab1019e96d4912 to your computer and use it in GitHub Desktop.
Example Basic Pine Script
//@version=5
indicator("Dynamic Table Example with Colors", overlay=true)
// Inputs
smaLength = input.int(20, title="SMA Length", minval=1)
upColor = input.color(color.new(color.green, 0), title="Bullish Color")
downColor = input.color(color.new(color.red, 0), title="Bearish Color")
neutralColor = input.color(color.new(color.gray, 0), title="Neutral Color")
// Function to calculate the trend for a specific timeframe
f_get_trend(syminfo_res, len) =>
sma = request.security(syminfo.tickerid, syminfo_res, ta.sma(close, len))
slope = sma - sma[1]
trend = slope > 0 ? "Bullish" : slope < 0 ? "Bearish" : "Neutral"
trend
// Function to determine the background color for a trend
bgColor(trend) =>
trend == "Bullish" ? upColor : trend == "Bearish" ? downColor : neutralColor
// Define timeframes and calculate trends
timeframes = array.new_string(4)
array.set(timeframes, 0, "15m")
array.set(timeframes, 1, "1h")
array.set(timeframes, 2, "4h")
array.set(timeframes, 3, "1d")
trends = array.new_string(4)
array.set(trends, 0, f_get_trend("15", smaLength))
array.set(trends, 1, f_get_trend("60", smaLength))
array.set(trends, 2, f_get_trend("240", smaLength))
array.set(trends, 3, f_get_trend("D", smaLength))
// Create a table
var table dynamicTable = table.new(position.top_right, array.size(timeframes) + 1, 2, bgcolor=color.new(color.white, 0))
// Add headers
if bar_index == 0
table.cell(dynamicTable, 0, 0, "Timeframe", text_color=color.black, bgcolor=color.gray, text_size=size.auto)
table.cell(dynamicTable, 0, 1, "Trend", text_color=color.black, bgcolor=color.gray, text_size=size.auto)
// Populate the table dynamically
for i = 0 to array.size(timeframes) - 1
table.cell(dynamicTable, i + 1, 0, array.get(timeframes, i), text_color=color.black, bgcolor=color.new(color.white, 0), text_size=size.auto)
table.cell(dynamicTable, i + 1, 1, array.get(trends, i), text_color=color.black, bgcolor=bgColor(array.get(trends, i)), text_size=size.auto)
//@version=5
strategy("Moving Average Crossover Strategy", overlay=true, initial_capital=10000)
// Inputs
fastLength = input(9, title="Fast MA Length")
slowLength = input(21, title="Slow MA Length")
// Calculate Moving Averages
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
// Plot MAs
plot(fastMA, color=color.blue, title="Fast MA")
plot(slowMA, color=color.red, title="Slow MA")
// Calculate Position Size
positionSize = strategy.equity / close // Quantity of shares/contracts based on capital
// Entry and Exit Conditions
if ta.crossover(fastMA, slowMA)
strategy.entry("Buy", strategy.long, qty=positionSize)
if ta.crossunder(fastMA, slowMA)
strategy.close("Buy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment