How to calculate based on previous entry/exit

rottentrade

Member
I would like to enter or exit a trade only when the recent signal is x distance (both horizontally and vertically) from the previous signal. For example, exit XYZ when price is at least x bars from the entry (horizontally) and at least $1 above the entry (vertically). I'm having trouble including the previous signal in the formula.
How would I write that in the code ("sell" below)?

def buy = Average(close, 10) crosses above Average(close, 20);
def sell = if close is greater than 5 days from "buy" AND above $1 from "buy";

Thanks for the help.
 
I would like to enter or exit a trade only when the recent signal is x distance (both horizontally and vertically) from the previous signal. For example, exit XYZ when price is at least x bars from the entry (horizontally) and at least $1 above the entry (vertically). I'm having trouble including the previous signal in the formula.
How would I write that in the code ("sell" below)?

def buy = Average(close, 10) crosses above Average(close, 20);
def sell = if close is greater than 5 days from "buy" AND above $1 from "buy";

Thanks for the help.

This is the reverse of which you requested in a strategy as almost everything was down that I looked over 5 days.. Just reverse the buy and sell terminology for the opposite method. Made to use on a daily chart.

Screenshot-2022-10-11-151923.png
Ruby:
def bn     = BarNumber();
def sell   = if Average(close, 10) crosses above Average(close, 20) then 1 else 0;
def sellbn = if sell[1] == 0 and  sell then bn else sellbn[1];

def buy    = bn > (sellbn + 5) and close < EntryPrice() - 1;
def buybn  = if buy then bn else buybn[1];

def initialsellbn = if IsNaN(buy) and sell then bn else if buy then 0 else initialsellbn[1];
def days = buybn - initialsellbn[1];

AddOrder(OrderType.SELL_TO_OPEN, bn==initialsellbn);
AddOrder(OrderType.BUY_TO_CLOSE, bn == buybn);

AddChartBubble(bn == initialsellbn, high, "Sellbn: " + initialsellbn + "\nEP: " + open[-1], Color.light_red);
AddChartBubble(bn==buybn, low,  "Buybn: " + Buybn + "\nDays: " + days + "\nXP: " + close, Color.light_green, no);
 

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

This is the reverse of which you requested in a strategy as almost everything was down that I looked over 5 days.. Just reverse the buy and sell terminology for the opposite method. Made to use on a daily chart.
Did I ask this in the past? I apologize if I did, but I don't recall seeing your code before. I haven't been around this place for some time. Anyway, I'll play with it for a while and see if my memory comes back. In case it doesn't, I hope you don't mind if I trouble you with more questions. As always, I appreciate your help.
 
Last edited:
Did I ask this in the past? I apologize if I did, but I don't recall seeing your code before. I haven't been around this place for some time. Anyway, I'll play with it for a while and see if my memory comes back. In case it doesn't, I hope you don't mind if I trouble you with more questions. As always, I appreciate your help.
I do not remember this being asked before. Let us know if you have further questions.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
605 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