General Strategy Question


New member
Hey guys, trying to backtest some different strategies and wondering if you guys can help give me some direction because i am struggling...I'm using certain boolean conditions to define a buy/sell setup to open a trade. When the condition is true for a bar, how do I get and hold values from the bar right before the condition triggers? As an example, if X happens on bar 5, then I would like to use the low value of bar 4 as a stop, and then take the difference from entryprice to define a target. I know this is a vague example, so here is one I am working on for supertrend. Basically, I look for the price to flip color (above/below supertrend) and then if the following bar retraces a certain percentage of the "flip" bar's range (around 50%) initiate a long/short. I would then use the low/high of the bar before the "priceflip" as a stop and use that as a target multiple.

Below is an excerpt (not complete code) from the strategy i described above. Keep in mind, this script already has ST defined I just didn't think I needed to include in this post. Also, i have the bool conditions as plots instead of def's to help me while I debug

input fib = .25;   #retrace percentage for bar following flip
input mult = 3;  #target multiple
plot D1 = HL2 crosses above ST;  #priceflip (condition 1)

def range = high - low;
plot D2 = D1 and low[-1] <= high - (fib * (range));  # price flip day 1 and day 2 retrace below fib percentage to take long (condition 2)

AddOrder(OrderType.BUY_TO_OPEN, D2, price = high - (fib * range), name = "LE");

def entry = EntryPrice();
plot stopline=if entry!=0 then low[1] else 0; #the low of bar right before flip. This is what I can't figure out because it carries the low onto following bars instead of being static
def target= entry+(mult*(entry-stopline));

AddOrder(OrderType.SELL_TO_CLOSE, low crosses below stopline, price = stopline, name = "LES");
AddOrder(OrderType.SELL_TO_CLOSE, high crosses above target, price = target, name = "LEX");

##End Code

This is pretty simple but I am not a coding expert. Thanks in advance for any insight!

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
381 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.