Help with Conditional Order closing when condition is fasle

grammates

New member
Hello all! This is my first post and I did try to use the search function but I was not able to locate exactly what I was searching for. I am looking for some help with a conditional order to close a trade.

Basically, Say I want to open a trade when rsi is between 40 - 50 and close when that condition is no longer active. I wasn't able to figure out the verbiage to have it ONLY close when the condition isn't true after the entry. It tends to continue entering positions the entire time the condition is false. If the source code is needed please let me know! It's not specific to any indicator I am more curious about how it would be worded.

Thank you for your time!
 
Solution
Hello all! This is my first post and I did try to use the search function but I was not able to locate exactly what I was searching for. I am looking for some help with a conditional order to close a trade.

Basically, Say I want to open a trade when rsi is between 40 - 50 and close when that condition is no longer active. I wasn't able to figure out the verbiage to have it ONLY close when the condition isn't true after the entry. It tends to continue entering positions the entire time the condition is false. If the source code is needed please let me know! It's not specific to any indicator I am more curious about how it would be worded.

Thank you for your time!


yes, always post the code that you are trying to develop...
Hello all! This is my first post and I did try to use the search function but I was not able to locate exactly what I was searching for. I am looking for some help with a conditional order to close a trade.

Basically, Say I want to open a trade when rsi is between 40 - 50 and close when that condition is no longer active. I wasn't able to figure out the verbiage to have it ONLY close when the condition isn't true after the entry. It tends to continue entering positions the entire time the condition is false. If the source code is needed please let me know! It's not specific to any indicator I am more curious about how it would be worded.

Thank you for your time!


yes, always post the code that you are trying to develop. don't be shy, everyone started out at the beginning. and many times, it is easier to fix something, instead of create it from scratch, or at least look at, point out what is wrong.


you didn't post code, so your sentence about continueing to enter postions when something is false, is meaningless.

--------------------------------
i am not that familiar with conditional order codes, so this may or may not help.

i'm not sure if you can reference portfolio functions in that type of code?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio


maybe using entryprice() will help?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/EntryPrice


posts about codes that do something once and ignores future signals
https://usethinkscript.com/threads/...iring-twice-in-thinkorswim.12310/#post-105468

---------------------------


this is an upper strategy for testing , save to 'strategies'

i set it to,
..buy when rsi crosses above the 40
..sell when rsi crosses out of the range, above 50 or below 40

it uses crosses above , to create 1 buy signal.
then it waits to see which level rsi crosses, to trigger a sell


observation, ( based on 1 stock , on a 15min chart)
when a long trend of price is dropping, this has losses.
when a long trend of price is rising, this has gains.

so other factors should be considered along with this.


Code:
# strat_rsi_rng_sell_when_out_00a

#https://usethinkscript.com/threads/help-with-conditional-order-closing-when-condition-is-fasle.14763/
#Help with Conditional Order closing when condition is fasle

#----------------------------

# buy - crosses above rng_lo level
# sell - when out of range , either above or below, the rsi range

def na = double.nan;
def bn = barnumber();

input rng_hi = 50;
input rng_lo = 40;
#def r = RSI().rsi;

#---------------------------------
# RSI
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
#-----------------------------------

def r = rsi;

# buy/sell rules
def buy1 = r crosses above rng_lo;
def sell1 = r crosses above rng_hi;
def sell2 = r crosses below rng_lo;


# variable is true during a trade
#   other buy signals during this period will be ignored
def trade = if sell1 or sell2 then 0
 else if buy1 then 1
 else trade[1];


#  use the initial transition, from 0 to 1 , as buy
#  use ending transition from 1 to 0 as sell
def buy = trade == 1 and trade[1] == 0;
def sell = trade == 0 and trade[1] == 1;


def Size = 1;

AddOrder(type = OrderType.BUY_TO_OPEN, condition = buy, price = open[-1], tradeSize = Size, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Buy");

AddOrder(type = OrderType.SELL_TO_CLOSE, condition = sell, price = open[-1], tradeSize = Size, tickcolor = Color.RED, arrowcolor = Color.RED, name = "Sell");

#

NEE 15min
strategy loaded in upper chart
normal RSI loaded - levels at 40 and 50 for reference
floatingPL loaded in lower
QwVWotK.jpg
 
Solution

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