Risk Reward Fade / Continuation For ThinkOrSwim

Ommni007

New member
Came across this skimming through old archives (2015) - An interesting Risk Reward Continuation/Fade script from the old ThinkScripter forum. Attribution is documented in the code.

It identifies bars with a X:1 trading opportunity either fading or continuing the existing price action. Stats labels also included. See hints for a thorough overview.

A scanner version might provide additional utility. I'm jammed for time at present, and it might be a while before I can get to it, so posting it here for anyone to play with or modify if so inclined.

Have fun.

Code:

#fl_rr_mapSTUDY.ts
#
#Fil Lorinc
#Creation date: 8/27/2015
#Edit Log (Date/Editor):
#
#
#
#hint: Identifies bars with a X:1 trading opportunity either fading or continuing the existing price action
#hint i_print_labels: Toggles chart labels
#hint i_print_bubbles: Toggles chart bubbles (display the RR above/below entry bar)
#hint i_rr: The Reward/Risk ratio threshold
#hint i_eval_bars: The number of bars forward for which to run the RR analysis
#hint i_lookback: The nubmer of bars in retrospect for which to anchor the "risk" component of the trade (i.e. a value of 1 looks at only the present bar, a value of 2 looks at the present bar and 1 bar prior. This is due to the functionality of the "Highest" and "Lowest" functions which require an (n + 1) value in the length parameter to be equivalent to standard thinkscript indexing references like [1])
#hint i_exc_price_long: The price at which to anchor the "reward" component of the trade. Intended selections are: (HIGH v. CLOSE) or (LOW v. CLOSE). When HIGH/LOW is selected, the RR ratio is measured to the HIGH/LOW of the subsequent number of eval bars whereas if CLOSE/CLOSE is selected, the RR ratio is measured to the closes of the subsequent number of eval bars.


#INPUTS
input i_print_labels = YES;
input i_print_bubbles = YES;
input i_rr = 3.0;
input i_eval_bars = 3;
input i_lookback = 10;
input i_exc_price_long = high;
input i_exc_price_short = low;

#VARIABLES & LOGIC
def v_long = if !HasContractChangeEvent()[-i_eval_bars] and close > Lowest(low, i_lookback) and (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close) >= i_rr*(close - Lowest(low, i_lookback)) and Lowest(low[-i_eval_bars], i_eval_bars) > Lowest(low, i_lookback) then 1 else 0;
def v_short = if !HasContractChangeEvent()[-i_eval_bars] and close < Highest(high, i_lookback) and (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars)) >= i_rr*(Highest(high, i_lookback) - close) and Highest(high[-i_eval_bars], i_eval_bars) < Highest(high, i_lookback) then 1 else 0;
def v_long_count = TotalSum(v_long);
def v_short_count = TotalSum(v_short);
def v_long_rr = (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close)/(close - Lowest(low, i_lookback));
def v_short_rr = (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars))/(Highest(high, i_lookback) - close);
def v_long_type_cont = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars])/v_long_count[i_eval_bars];
def v_long_type_fade = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars])/v_long_count[i_eval_bars];
def v_short_type_cont = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars])/v_short_count[i_eval_bars];
def v_short_type_fade = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars])/v_short_count[i_eval_bars];

#PLOTS
plot p_long_rr = if v_long then v_long_rr else 0;
plot p_short_rr = if v_short then v_short_rr else 0;

#PLOT STYLES & SETTINGS
p_long_rr.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
p_long_rr.SetDefaultColor(Color.MAGENTA);
p_short_rr.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
p_short_rr.SetDefaultColor(Color.CYAN);

#LABELS
AddLabel(i_print_labels, "ACTIONABLE BARS: " + AsPercent((v_long_count[i_eval_bars] + v_short_count[i_eval_bars]) / BarNumber()), Color.WHITE);
AddLabel(i_print_labels, "LONG: " + v_long_count[i_eval_bars] + " (c/f: " + AsPercent(v_long_type_cont) + " / " + AsPercent(v_long_type_fade) + ") | SHORT: " + v_short_count[i_eval_bars] + " (c/f: " + AsPercent(v_short_type_cont) + " / " + AsPercent(v_short_type_fade) + ")", Color.WHITE);
AddLabel(i_print_labels, "AGG LONG RR: " + Round(TotalSum(if v_long[i_eval_bars] then v_long_rr[i_eval_bars] else 0) / v_long_count[i_eval_bars], 2) + " | AGG SHORT RR: " + Round(TotalSum(if v_short[i_eval_bars] then v_short_rr[i_eval_bars] else 0) / v_short_count[i_eval_bars], 2) , Color.WHITE);

#BUBBLES
DefineGlobalColor("long_cont", CreateColor(0, 255, 0)); #GREEN
DefineGlobalColor("long_fade", CreateColor(144, 238, 114)); #LIGHT GREEN
DefineGlobalColor("short_cont", CreateColor(255, 0, 0)); #RED
DefineGlobalColor("short_fade", CreateColor(255, 175, 175)); #PINK

AddChartBubble(i_print_bubbles and v_long, low, Round(v_long_rr, 1), if close > open then GlobalColor("long_cont") else if close < open then GlobalColor("long_fade") else Color.BLACK, no);
AddChartBubble(i_print_bubbles and v_short, high, Round(v_short_rr, 1), if close < open then GlobalColor("short_cont") else if close > open then GlobalColor("short_fade") else Color.WHITE, yes);

#DEBUG
 
Has anyone come up with a scanner for this script? Spent better part of the day testing - the long plot not visible when the study loaded into the scanner. I'm probably missing something. Any input is welcome! Thanks.

input i_rr = 3.0;
input i_eval_bars = 3;
input i_lookback = 10;
input i_exc_price_long = close;
input i_exc_price_short = close;

def v_long = if -i_eval_bars and close > Lowest(low, i_lookback) and (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close) >= i_rr * (close - Lowest(low, i_lookback)) and Lowest(low[-i_eval_bars], i_eval_bars) > Lowest(low, i_lookback) then 1 else 0;

def v_short = if -i_eval_bars and close < Highest(high, i_lookback) and (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars)) >= i_rr * (Highest(high, i_lookback) - close) and Highest(high[-i_eval_bars], i_eval_bars) < Highest(high, i_lookback) then 1 else 0;

def v_long_count = TotalSum(v_long);
def v_short_count = TotalSum(v_short);

def v_long_rr = (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close) / (close - Lowest(low, i_lookback));
def v_short_rr = (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars)) / (Highest(high, i_lookback) - close);
def v_long_type_cont = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars]) / v_long_count[i_eval_bars];
def v_long_type_fade = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars]) / v_long_count[i_eval_bars];
def v_short_type_cont = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars]) / v_short_count[i_eval_bars];
def v_short_type_fade = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars]) / v_short_count[i_eval_bars];

plot p_long_rr = if v_long then v_long_rr else 0;
#plot p_short_rr = if v_short then v_short_rr else 0;
 
So, here's a basic scanner I put together for the Risk Reward Fade/Continuation script. A scan setting of within the last 4-5 bars seems to be the threshold for returning a list of stocks. Set to within last 3 bars returns nothing at this point. The within last 4-5 bar setting is not precise enough - 2 would be acceptable. I suspect i_eval_bars = 3 could be limiting the ability to get results at the last 1 and 2 bar scan setting. Not visualizing a highly probable root cause or solution just yet - any help this forum can offer would be much appreciated.

#Risk Reward F/C Scanner

def i_rr = 3.0;
def i_eval_bars = 3;
def i_lookback = 10;
def i_exc_price_long = High;
def i_exc_price_short = Low;

def v_long = if (-i_eval_bars) and close > Lowest(low, i_lookback) and (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close) >= i_rr * (close - Lowest(low, i_lookback)) and Lowest(low[-i_eval_bars], i_eval_bars) > Lowest(low, i_lookback) then 1 else 0;

def v_short = if (-i_eval_bars) and close < Highest(high, i_lookback) and (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars)) >= i_rr * (Highest(high, i_lookback) - close) and Highest(high[-i_eval_bars], i_eval_bars) < Highest(high, i_lookback) then 1 else 0;

def v_long_count = TotalSum(v_long);
def v_short_count = TotalSum(v_short);

def v_long_rr = (Highest(i_exc_price_long[-i_eval_bars], i_eval_bars) - close) / (close - Lowest(low, i_lookback));
def v_short_rr = (close - Lowest(i_exc_price_short[-i_eval_bars], i_eval_bars)) / (Highest(high, i_lookback) - close);
def v_long_type_cont = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars]) / v_long_count[i_eval_bars];
def v_long_type_fade = TotalSum(v_long[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars]) / v_long_count[i_eval_bars];
def v_short_type_cont = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] < open[i_eval_bars]) / v_short_count[i_eval_bars];
def v_short_type_fade = TotalSum(v_short[i_eval_bars] and close[i_eval_bars] > open[i_eval_bars]) / v_short_count[i_eval_bars];

def p_long_rr = if v_long then v_long_rr else 0;
def p_short_rr = if v_short then v_short_rr else 0;

def condition1 = if(p_long_rr is true) and (close > open) then v_long_type_cont else 0;
def condition2 = if (p_long_rr is true) and (close < open) then v_long_type_fade else 0;
def condition3 = if(p_short_rr is true) and (close < open) then v_short_type_cont else 0;
def condition4 = if (p_short_rr is true) and (close > open) then v_short_type_fade else 0;

# Plot arrows
plot UArrowC = if condition1 is true then 1 else 0;
UArrowC.SetDefaultColor(Color.dark_green);
UArrowC.SetPaintingStrategy(PaintingStrategy.Boolean_arrow_up);
UarrowC.setLineWeight(3);

plot UArrowF = if condition2 is true then 1 else 0;
UArrowF.SetDefaultColor(Color.light_green);
UArrowF.SetPaintingStrategy(PaintingStrategy.Boolean_arrow_up);
UArrowF.setLineWeight(3);

plot DArrowC = if condition3 is true then 1 else 0;
DArrowC.SetDefaultColor(Color.red);
DArrowC.SetPaintingStrategy(PaintingStrategy.Boolean_arrow_down);
DarrowC.setLineWeight(3);

plot DArrowF = if condition4 is true then 1 else 0;
DArrowF.SetDefaultColor(Color.pink);
DArrowF.SetPaintingStrategy(PaintingStrategy.Boolean_arrow_down);
DArrowF.setLineWeight(3);

#Debug
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
384 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top