Code Example for a High Low Price Range Stock Indicator using Thinkorswim thinkScript
Stock Indicator
Money management is an important aspect for any stock or equity trader, as is the use of stock indicator tools. Once an investment vehicle is selected to trade, minimizing risk is critical. Another important component for trading in the markets is to understand that the markets can quickly change on a whim, and that there is no guarantee that prices will go up or down, but can simply go sideways over time. With this, and specifically when trading for short term gains (opposed to investing for the longer term), it is helpful to use a "take what you can get" mindset rather than holding too long and losing small gains. This is where a one-to-one (1:1) risk/reward ratio methodology can help.
Here is an example stock indicator script that I have created using a one-to-one risk/reward ratio methodology to visually show the price ranges (buy, sell, and stop) from the current close price of a stock or equity. The close price is assumed to be the entry price line for a trade, the lower line is the stop-loss price, and the upper line is the sell-stop target price. This indicator is displayed in the upper price charting panel and was initially designed for bull, upward trending, or other long trade strategies. It should be fairly easy for a trader to use this for short selling in bear or downward trending markets by simply changing the calculations for the upper line to be the stop price, and the lower line for the sell stop price (you could also change the shaded area colors to correlate with their color coded meanings).
The stock indicator has a customizable lookback period that is used to determine how far back the lowest price of the equity is searched for. The default is five (5) periods, but can be set to whatever timeframe you are interested in using. You can adjust the lookback period to fit your particular situation. It is important to note that the lowest price within the lookback period is used to with the current close price to calculate the inital price range:
Range = Close Price - Lowest Price in the Defined Period
This range is used to determine the profit target as well, by adding the range to the current close price, giving a one to one risk/reward profit ratio.
Sell Price = Close Price + Range
Stop Price = Close Price - Range
This indicator also provides text boxes in the upper left of the chart area showing the prices for the Entry, Sell-Stop, and Stop-Loss orders that meet a one-to-one risk/reward profit ratio.
Get the Indicator Here
Screenshots
thinkScript Code
# THINKSCRIPT - DUANE MURRAY, 2022Get the Indicator Here
# DEFINE THE LOOKBACK PERIOD TIMEFRAME
input LookbackPeriod = 5;
# GET THE LOWEST PRICE IN THE GIVEN TIMEFRAME
def recent_low = Lowest(low, LookbackPeriod);
# SET A 1:1 RISK/REWARD RATIO FROM THE LOWEST PRICE TO SET A TARGET PRICE
def target_price = close + (close - recent_low);
# PLOT HORIZONTAL CURRENT PRICE LINE
vEntry = close;
def entryValue = HighestAll(if IsNaN(vEntry[-1]) then vEntry else Double.NaN);
plot entryLine = entryValue;
entryLine.SetPaintingStrategy(PaintingStrategy.LINE);
entryLine.SetDefaultColor(Color.GRAY);# PLOT HORIZONTAL LINE FOR TARGET 1:1 RISK/REWARD PRICE
def vTarget = target_price;
def highestProfit = HighestAll(if IsNaN(vTarget[-1]) then vTarget else Double.NaN);
plot profitLine = highestProfit;
profitLine.SetPaintingStrategy(PaintingStrategy.LINE);
profitLine.SetDefaultColor(Color.DARK_GREEN);# PLOT HORIZONTAL LINE FOR STOP LOSS ORDER PRICE
def vStop = recent_low;
def lowestStop = LowestAll(if IsNaN(vStop[-1]) then vStop else Double.NaN);
plot stopLine = lowestStop;
stopLine.SetPaintingStrategy(PaintingStrategy.LINE);
stopLine.SetDefaultColor(Color.DARK_RED);# SHOW STOP, ENTRY, AND SELL LIMIT CLOUD AREAS
AddCloud(entryValue, lowestStop, Color.PINK, Color.LIGHT_GREEN);
AddCloud(entryValue, highestProfit, Color.PINK, Color.LIGHT_GREEN);# ADD TEXT LABEL ON CHART TO SHOW ENTRY PRICE
AddLabel(1, " Entry: " + AsText(Round(close, 2), NumberFormat.TWO_DECIMAL_PLACES), Color.DARK_GRAY);# SHOW SELL STOP PROFIT PRICE
AddLabel(1, " Sell-Stop: " + AsText(Round(highestProfit, 2), NumberFormat.TWO_DECIMAL_PLACES), Color.DARK_GREEN);# SHOW STOP LOSS PRICE
AddLabel(1, " Stop-Loss: " + AsText(Round(lowestStop, 2), NumberFormat.TWO_DECIMAL_PLACES), Color.DARK_RED);

