# Need help coding buy/sell conditions

##### 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 && Close > Close; <-- Error with "Short" as being undefined.
def short1 = Some condition;
def short2 = long && Close < Close;

How would I be able to define what "Short" is in this case (it would be like a circular argument)? Is there a possible (more simpler) workaround for this?

Solution
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) and <some condition for long1> {
long1 = 1;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = Double.NaN;

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

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

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

}else{
long1 = long1;
long2 = long2;
short1 = short1;
short2 =...``````

#### Svanoy

##### Well-known member
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) and <some condition for long1> {
long1 = 1;
long2 = Double.NaN;
short1 = Double.NaN;
short2 = Double.NaN;

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

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

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

}else{
long1 = long1;
long2 = long2;
short1 = short1;
short2 = short2;}``````

Last edited:

##### Member
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 && close > close; ## reverse to long if "sell1" trades higher

def sell = sell1 or sell2;

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:

#### Svanoy

##### Well-known member
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 then long2 is true.
Line #4 same thing just long side.
Line#5 just carries over the values to the next bar.

• ##### Member
Thanks for the clarification. I think I could really clean up a lot of useless codes in my strategy if I knew some of the things you write.  ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?