Need help coding buy/sell conditions

rottentrade

Member
I would like to reverse the trade at the next bar if it moves in the opposite direction of my entry, long or short.

###Long/Short###
def long1 = Some condition;
def long2 = short[1] && Close > Close[1]; <-- Error with "Short[1]" as being undefined.
def short1 = Some condition;
def short2 = long[1] && Close < Close[1];

How would I be able to define what "Short[1]" is in this case (it would be like a circular argument)? Is there a possible (more simpler) workaround for this?
 
Solution
@rottentrade
I don't know the context of your code, so I could not test, but have you tried:
Ruby:
###Long/Short###

def long1;
def long2;
def short1;
def short2;

if IsNaN(Long1[1]) and <some condition for long1> {
long1 = 1;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = Double.NaN;

}else if IsNaN(short1[1]) and <some condition for short1> {
long1 = Double.NaN;
long2 = Double.NaN;
short1 = 1;
short2 = Double.NaN;

}else if !IsNaN(short1[1]) && Close > Close[1] {
long1 = Double.NaN;
long2 = 1;
short1 = Double.NaN;
short2 = Double.NaN;

}else if !IsNaN(long1[1]) && Close < Close[1] {
long1 = Double.NaN;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = 1;

}else{
long1 = long1[1];
long2 = long2[1];
short1 = short1[1];
short2 =...
@rottentrade
I don't know the context of your code, so I could not test, but have you tried:
Ruby:
###Long/Short###

def long1;
def long2;
def short1;
def short2;

if IsNaN(Long1[1]) and <some condition for long1> {
long1 = 1;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = Double.NaN;

}else if IsNaN(short1[1]) and <some condition for short1> {
long1 = Double.NaN;
long2 = Double.NaN;
short1 = 1;
short2 = Double.NaN;

}else if !IsNaN(short1[1]) && Close > Close[1] {
long1 = Double.NaN;
long2 = 1;
short1 = Double.NaN;
short2 = Double.NaN;

}else if !IsNaN(long1[1]) && Close < Close[1] {
long1 = Double.NaN;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = 1;

}else{
long1 = long1[1];
long2 = long2[1];
short1 = short1[1];
short2 = short2[1];}
 
Last edited:
Solution
Hey, thanks for your reply. However, being a noob, I would appreciate if you can explain this in plain english for me.

For example, how am I to interpret this code:

if IsNaN(Long2) and IsNaN(short2) and <some condition for long1> {
long1 = 1;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = Double.NaN;

I kinda understand the first two lines, but I can't follow lines 3-5. What's the rationale behind the Double.NaN? I think the first and second lines go something like this:

1) IF "Long2" and "Short2" is not a number and <some condition for long1>
2) THEN, YES to "Long1"
3) But "Long2" is not a number, so ignore??
4) Ditto.
5) Ditto.

I don't know the context of your code, so I could not test

Here's the simplified version of the code. Hope it makes sense and let me know if you have any question. Thanks.

Ruby:
def long = close crosses above ExpAverage(close, 20) ;
def short = close crosses below ExpAverage(close, 20) ;

def buy1 = long; ## buy when the "long" condition is met
def sell1 = short; ## buy when the "short" condition is met

def buy2 = sell1[1] && close > close[1]; ## reverse to long if "sell1" trades higher
def sell2 = buy1[1] && close < close[1]; ## reverse to short if "buy1" trades lower

def buy = buy1 or buy2;
def sell = sell1 or sell2;

AddOrder(OrderType.BUY_AUTO, buy, close, 1, tickcolor = GetColor(1), arrowcolor = Color.DOWNTICK, name = “Buy”);
AddOrder(OrderType.SELL_AUTO, sell, close, 1, tickcolor = GetColor(2), arrowcolor = Color.UPTICK, name = “Sell”);

EDIT: After having re-read my own reply, I think I found the problem. The problem was with how the codes were arranged (or that's what I think for now, since there's a lot of nesting involved). Anyway, if you could respond about the usage of Double.NaN, I would appreciate it.
 
Last edited:
After seeing your simplified code snippet I corrected my previous post to match.

Double.NaN is used as a disqualifier here.
Depending upon the current trade state, certain condition lines are skipped.

Line #1 if long1 is not true and long1 condition occurs then long1 is true.
Line #2 same thing, just short side.
Line #3 if short1 is true and close > close[1] then long2 is true.
Line #4 same thing just long side.
Line#5 just carries over the values to the next bar.
 

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