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