// Constants - Reduced transparency to 60 for brighter colors
var color red_color = color.new(#ff0000, 0) // Brighter red
var color green_color = color.new(#00ff00, 0) // Brighter green (#00FF00 is more vibrant than #32CD32)
// Rest of the code remains the same...
method calc_ma(string type, float src, int length) =>
float sma = ta.sma(src, length)
float ema = ta.ema(src, length)
float rma = ta.rma(src, length)
float wma = ta.wma(src, length)
float vwma = ta.vwma(src, length)
code
float smma = na(sma[1]) ? sma : (sma[1] * (length - 1) + src) / length
float hullma = ta.wma(2 * ta.wma(src, length / 2) - ta.wma(src, length), math.round(math.sqrt(float(length))))
float tma = ta.sma(sma, length)
float dema_ema = ema
float dema = 2 * dema_ema - ta.ema(dema_ema, length)
float tema_ema1 = ema
float tema_ema2 = ta.ema(tema_ema1, length)
float tema_ema3 = ta.ema(tema_ema2, length)
float tema = 3 * (tema_ema1 - tema_ema2) + tema_ema3
result = switch type
"RMA" => rma
"SMA" => sma
"EMA" => ema
"WMA" => wma
"VWMA" => vwma
"SMMA" => smma
"HullMA" => hullma
"TMA" => tma
"DEMA" => dema
"TEMA" => tema
=> sma
result
// Color calculations
method get_color(float dist, float niv_prof, float src, float ma) =>
color col = na
if dist >= niv_prof
if src > ma
col := red_color
if src < ma
col := green_color
col
This is my tradingview indicator it shows green sign for buy and red sign for sell
make a version 6 trading strategy from this
BUY when 1) Indicator shows greenn line or box 2) you get a green candle closure .. then open long with a stoploss of recent low and take profit of RR3 and vice versa for shorts
give me options
to change how much $ i want to risk on trade
to change RR
Assistant
Below is the TradingView Pine Script version 6 strategy based on your requirements. The script includes options to adjust the risk amount and risk-reward ratio (RR). It also incorporates the logic for entering long and short trades based on the conditions you specified.
pinescript
//@version=6
strategy(title="Six AI Algo Strategy", shorttitle="Six AI Algo Strat", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Input variables for current timeframe
tf_c = input.bool(title="🔹 Current Timeframe 🔹", defval=true)
ma_type_c = input.string(title="Type of MA=", defval="SMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "HullMA", "TMA", "DEMA", "TEMA"])
ma_len_c = input.int(defval=200, title="MA Length=", minval=1)
ma_src_c = input.source(close, title="MA Source=")
ma_dsl_c = input.float(defval=0.89, title="Depth Search Level", minval=0.0, step=0.01)
nopbtf_c = input.int(defval=200, title="Number Of Previous Bars To Find Depth", minval=1)
// Input variables for timeframe 1
tf_1 = input.bool(title="🔹 Timeframe 1 🔹", defval=true)
ma_type_1 = input.string(title="Type of MA Timeframe 1", defval="SMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "HullMA", "TMA", "DEMA", "TEMA"])
ma_len_1 = input.int(defval=200, title="MA Length Timeframe 1", minval=1)
ma_src_1 = input.source(close, title="MA Source Timeframe 1")
res1 = input.timeframe("2", title="Timeframe 1")
ma_dsl_1 = input.float(defval=0.89, title="Depth Search Level", minval=0.0, step=0.01)
nopbtf_1 = input.int(defval=200, title="Number Of Previous Bars To Find Depth", minval=1)
// Risk and RR inputs
risk_amount = input.float(title="Risk Amount ($)", defval=100, minval=1)
risk_reward_ratio = input.float(title="Risk-Reward Ratio", defval=3.0, minval=1.0)
// Constants - Reduced transparency to 60 for brighter colors
var color red_color = color.new(#ff0000, 0) // Brighter red
var color green_color = color.new(#00ff00, 0) // Brighter green (#00FF00 is more vibrant than #32CD32)
// Method to calculate moving average
method calc_ma(string type, float src, int length) =>
float sma = ta.sma(src, length)
float ema = ta.ema(src, length)
float rma = ta.rma(src, length)
float wma = ta.wma(src, length)
float vwma = ta.vwma(src, length)
float smma = na(sma[1]) ? sma : (sma[1] * (length - 1) + src) / length
float hullma = ta.wma(2 * ta.wma(src, length / 2) - ta.wma(src, length), math.round(math.sqrt(float(length))))
float tma = ta.sma(sma, length)
float dema_ema = ema
float dema = 2 * dema_ema - ta.ema(dema_ema, length)
float tema_ema1 = ema
float tema_ema2 = ta.ema(tema_ema1, length)
float tema_ema3 = ta.ema(tema_ema2, length)
float tema = 3 * (tema_ema1 - tema_ema2) + tema_ema3
result = switch type
"RMA" => rma
"SMA" => sma
"EMA" => ema
"WMA" => wma
"VWMA" => vwma
"SMMA" => smma
"HullMA" => hullma
"TMA" => tma
"DEMA" => dema
"TEMA" => tema
=> sma
result
// Current timeframe calculations
ma_current = calc_ma(ma_type_c, ma_src_c, ma_len_c)
dist_current = math.abs(ma_src_c - ma_current)
niv_prof_current = ta.highest(dist_current, nopbtf_c) * ma_dsl_c
// Timeframe 1 calculations
ma_tf_1 = request.security(syminfo.tickerid, res1, calc_ma(ma_type_1, ma_src_1, ma_len_1))
src_tf_1 = request.security(syminfo.tickerid, res1, ma_src_1)
dist_tf_1 = math.abs(src_tf_1 - ma_tf_1)
niv_prof_tf_1 = ta.highest(dist_tf_1, nopbtf_1) * ma_dsl_1
// Color calculations
method get_color(float dist, float niv_prof, float src, float ma) =>
color col = na
if dist >= niv_prof
if src > ma
col := red_color
if src < ma
col := green_color
col
col_tf_1 = get_color(dist_tf_1, niv_prof_tf_1, src_tf_1, ma_tf_1)
bg_color = get_color(dist_current, niv_prof_current, ma_src_c, ma_current)
// Entry and exit conditions
long_condition = bg_color == green_color and close > open
short_condition = bg_color == red_color and close < open
// Stoploss and take profit calculation
stop_loss_long = ta.lowest(low, 5) // Recent low as stop loss
take_profit_long = close + (close - stop_loss_long) * risk_reward_ratio
stop_loss_short = ta.highest(high, 5) // Recent high as stop loss
take_profit_short = close - (stop_loss_short - close) * risk_reward_ratio
// Position size calculation
risk_per_trade = risk_amount / syminfo.pointvalue
position_size_long = math.min(risk_per_trade / (close - stop_loss_long), 100) // Ensure position size does not exceed 100%
position_size_short = math.min(risk_per_trade / (stop_loss_short - close), 100)
// Execute trades
if (long_condition)
strategy.entry("Long", strategy.long, qty=position_size_long)
strategy.exit("Long Exit", from_entry="Long", stop=stop_loss_long, limit=take_profit_long)
if (short_condition)
strategy.entry("Short", strategy.short, qty=position_size_short)
strategy.exit("Short Exit", from_entry="Short", stop=stop_loss_short, limit=take_profit_short)
// Plotting
bgcolor(bg_color, transp=70)
plotshape(series=long_condition, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=short_condition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
Key Features:
Inputs for Customization:
Adjust the Risk Amount in dollars.
Modify the Risk-Reward Ratio (default is 3.0).
Entry Conditions:
Go long when the indicator shows a green box and a green candle closes.
Go short when the indicator shows a red box and a red candle closes.
Stop Loss and Take Profit:
Stop loss is set at the recent low (for longs) or recent high (for shorts).
Take profit is calculated based on the specified risk-reward ratio.
Position Sizing:
Automatically calculates position size based on the risk amount and stop loss distance.
Plotting:
Green triangles below the bars indicate long entries.
Red triangles above the bars indicate short entries.
This strategy should work well for backtesting and live trading. Let me know if you need further adjustments!