Plotting stop loss on different aggregation periods

victorng

New member
Plus
Hello - for a certain long futures strategy I have developed, I plot the previous high, low, and halfback at 30 minute aggregation on a 1 minute chart and open/close the position based on certain number of 1m closes above or below those levels. (For clarity, I'm plotting within my strategy, not in a separate study)So far so good the strategy itself works as desired. In real live trading, I will open/close the position manually and want to plot my stops which are based on 30 minutes levels on the same 1m chart. I am not having success in plotting my stops as desired. Below is the code I'm working with:

input AG = AggregationPeriod.THIRTY_MIN;
input LowBufferTicks = 6;
def Half = (high(period = AG)[1] + low(period = AG)[1]) / 2;
input StopPoints = 5;
def EntryPrice = EntryPrice();
def TickSize = TickSize();
def LX1 = EntryPrice - StopPoints;
def LX2 = low(period = AG)[1] - (LowBufferTicks * TickSize);
def MyStop = Max(LX1, LX2);

plot MyNewStop = if MyStop > EntryPrice + 5 then Half else max(MyStop, MyStop[1]);

MyNewStop.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
MyNewStop.SetDefaultColor(Color.CYAN);

To articulate what I'm looking to do:
1. Until the close of the 30m timeframe of which I opened the position, I'm looking to plot the higher of either my entry price minus my stop loss, or the previous 30m low minus a buffer.
2. For the subsequent 30 minute periods for which the position is open, I want to plot the higher of either the new 30m period low minus a buffer or the previous plotted stop.
3. Looking to repeat step 2 until such time that step two is greater than my entry price + 5 points at which time, the stop plot should move to the halfback of the most recent previous 30m period.

Looking at the attached picture1, during the 4th 30m period the position was open, I wanted the cyan triangles to remain at the same level as the 3rd 30 minute period since that level is higher but the way I've currently coded and played with, does not net the desire result. Looking for any help and guidance and please forgive my coding if it's messy - I'm self taught and doing the best I can but appreciate pointers. Thank you!
 

Attachments

  • Picture1.png
    Picture1.png
    64.5 KB · Views: 67

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
293 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