Sell order based on study condition not triggering

InfamousCookie

New member
While doing some testing with live trading, I was having an issue with conditional sell orders not triggering. I'm using Accumulation Swing Index as my sell study. The code is posted below. I have it set up to sell the stock when asiplot is below smaplot but doesn't seem to always trigger. Seems like the trigger rate is about 30% or so. I'm currently looking at a stock that should have sold mid day yesterday using this conditional selling code but it's still sitting there despite the chart clearly showing that the asiplot is below the smaplot. I have checked to make sure the times on the chart and condition are the same as well as the input parameters are the same. Any idea what I'm missing? Thanks for the help.

Code:
#TOS Accumulation_Swing_Index
declare lower;

input smaLength = 10;
input averageType = AverageType.WILDERS;
def limit = 30;
def AbsHighClose = AbsValue(high - close[1]);
def AbsLowClose = AbsValue(low - close[1]);
def AbsCloseOpen = AbsValue(close[1] - open[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
        If(AbsHighClose >= (high - low), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen),
        If(AbsLowClose >= (high - low),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
            (50 * (((close - close[1]) + 0.50 * (close - open) + 0.25 * (close[1] - open[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
            0 + if !IsNaN(nRes[1]) then nRes[1] else 0);

def asi = nRes;
def sma = Movingaverage(averagetype, asi, smaLength);
def ASI_crossesbelow = asi crosses below sma ;
plot asiplot = asi;
plot smaplot = sma;
 
Solution
You have both a Wait Trg and Wait Cond on the Order above. Your issues have nothing to do with your conditional order.
The Wait Trg is taking precedence over a Wait Cond. Your conditional order is never being processed, I am assuming because of the hold of the Wait Trg/

Here is the Order of Precedence:
Screenshot (199).png




Do you see #2 in the description in the bottom of your order rules?
The particular OCO that you displayed in the above post, states that the conditional order must wait for the trigger that is on your group of orders.
As long as that wait trg is there, it will not execute your condition.

The bottom of your order should look like this. Nothing in the way of processing the OCO. No Wait...
Seems to be a pretty good exit indicator. I am assuming you are seeing the actual condition on the chart but the order not responding? I have heard and experienced myself issues with Conditional orders based on Studies not firing. It's an issue with TOS. Maybe try creating an alert based on the crossbelow and see what reliability of the alert is . And make sure you have set the timeframe ie 5 Min in the conditional order, it usually comes up Day.
I would use Plot instead of def for ASI_crossbelow then just say when that is true send alert, if that seems to be working then try again in conditional order but like I said I don't know that conditional orders work consistently.
 
Seems to be a pretty good exit indicator. I am assuming you are seeing the actual condition on the chart but the order not responding? I have heard and experienced myself issues with Conditional orders based on Studies not firing. It's an issue with TOS. Maybe try creating an alert based on the crossbelow and see what reliability of the alert is . And make sure you have set the timeframe ie 5 Min in the conditional order, it usually comes up Day.
I would use Plot instead of def for ASI_crossbelow then just say when that is true send alert, if that seems to be working then try again in conditional order but like I said I don't know that conditional orders work consistently.
You are correct; I see the conditions being met in the chart but the sell order not firing. Trying the plot instead of def for ASI_crossbelow is a good idea. I'm going to give that a try with the conditional order. I thought about doing alerts but would prefer to have it auto trigger due to having a full time job during the day, chances are good I couldn't get online to do the trade if the alert triggers. However, if the conditional order keeps giving me issues I might not have a choice. Thank you for the suggestion!
 
I used the ASI in conditional orders successfully in the past.

I am assuming that you chose to reference your custom study in your OCO.
That is allowed and theoretically should work if you wrote the OCO correctly.

You didn't provide the script for your conditional order so it isn't possible to comment on where the issue might be.

You could try referencing the ToS indicator in the conditional order and create the custom entry/exit condition. This is my preferred method and I have never had an issue with my OCOs.

What is OCO script? How are you creating your exit condition?

TDAmeritrade has several caveats listed as to the causes of OCOs not triggering. You could google their commentary and see if it applies to your situation.
 
Last edited:
I used the ASI in conditional orders successfully in the past.

I am assuming that you chose to reference your custom study in your OCO.
That is allowed and theoretically should work if you wrote the OCO correctly.

You didn't provide the script for your conditional order so it isn't possible to comment on where the issue might be.

You could try referencing the ToS indicator in the conditional order and create the custom entry/exit condition. This is my preferred method and I have never had an issue with my OCOs.

What is OCO script? How are you creating your exit condition?

TDAmeritrade has several caveats listed as to the causes of OCOs not triggering. You could google their commentary and see if it applies to your situation.
Hi MerryDay,

Here are several pictures showing what I have done. I did happen to look at TOS study condition page and the thing that stood out to me is that studies had to have one plot. So, I actually took the above code and marked out plots creating an ASI only and SMA only plot. Then I created the study based order using those two codes. Before, I was using the code above only. Of course, even creating separate plots didn't cause the sale to trigger on some trades.

JzrdqVG.png

CPeZltw.png

Rp3GNoC.png

grkN840.png


Code:
ASMASIONLY("sma length" = 5, "average type" = "EXPONENTIAL") is less than ASMSMAOnly("sma length" = 5, "average type" = "EXPONENTIAL")

Code:
#TOS Accumulation_Swing_Index ASI ONLY
declare lower;

input smaLength = 10;
input averageType = AverageType.WILDERS;
def limit = 30;
def AbsHighClose = AbsValue(high - close[1]);
def AbsLowClose = AbsValue(low - close[1]);
def AbsCloseOpen = AbsValue(close[1] - open[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
        If(AbsHighClose >= (high - low), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen),
        If(AbsLowClose >= (high - low),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
            (50 * (((close - close[1]) + 0.50 * (close - open) + 0.25 * (close[1] - open[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
            0 + if !IsNaN(nRes[1]) then nRes[1] else 0);

def asi = nRes;
def sma = Movingaverage(averagetype, asi, smaLength);
def ASI_crossesbelow = asi crosses below sma ;
plot asiplot = asi;
#plot smaplot = sma;

Code:
#TOS Accumulation_Swing_Index SMA ONLY
declare lower;

input smaLength = 10;
input averageType = AverageType.WILDERS;
def limit = 30;
def AbsHighClose = AbsValue(high - close[1]);
def AbsLowClose = AbsValue(low - close[1]);
def AbsCloseOpen = AbsValue(close[1] - open[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
        If(AbsHighClose >= (high - low), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen),
        If(AbsLowClose >= (high - low),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (high - low) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
            (50 * (((close - close[1]) + 0.50 * (close - open) + 0.25 * (close[1] - open[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
            0 + if !IsNaN(nRes[1]) then nRes[1] else 0);

def asi = nRes;
def sma = Movingaverage(averagetype, asi, smaLength);
def ASI_crossesbelow = asi crosses below sma ;
#plot asiplot = asi;
plot smaplot = sma;

Please let me know what you think or if I can provide any other additional details. I appreciate your help. Thank you.
 
You have both a Wait Trg and Wait Cond on the Order above. Your issues have nothing to do with your conditional order.
The Wait Trg is taking precedence over a Wait Cond. Your conditional order is never being processed, I am assuming because of the hold of the Wait Trg/

Here is the Order of Precedence:
Screenshot (199).png




Do you see #2 in the description in the bottom of your order rules?
The particular OCO that you displayed in the above post, states that the conditional order must wait for the trigger that is on your group of orders.
As long as that wait trg is there, it will not execute your condition.

The bottom of your order should look like this. Nothing in the way of processing the OCO. No Wait Trg.
yfzmrxt.png
 
Last edited:
Solution
You have both a Wait Trg and Wait Cond on the Order above. Your issues have nothing to do with your conditional order.
The Wait Trg is taking precedence over a Wait Cond. Your conditional order is never being processed, I am assuming because of the hold of the Wait Trg/

Here is the Order of Precedence:
View attachment 1105



Do you see #2 in the description in the bottom of your order rules?
The particular OCO that you displayed in the above post, states that the conditional order must wait for the trigger that is on your group of orders.
As long as that wait trg is there, it will not execute your condition.

The bottom of your order should look like this. Nothing in the way of processing the OCO. No Wait Trg.
yfzmrxt.png
That makes sense to me. This is probably why if I did a cancel then replace on the conditional study sell with the exact same parameters, it tends to go through since I probably inadvertently got rid of that trigger. The question I have would have is, what's causing that wait trigger then? Before the actual submission of the order above, I see the #2 wait trigger in the part associated with the study order. If I delete the buy portion of the OCO order before actually submitting the order, then that trigger goes away so I would assume that the trigger is associated with the buy order somehow. However, I am currently holding a stock where the buy order went through but the study is still waiting on that trigger you mentioned. To get around this issue, should I be buying the stock alone then setting the sell order separately? Or maybe there is some other setting that I'm missing?

I appreciate your help, MerryDay; you're awesome!
 
Last edited:
@InfamousCookie I haven't any idea what you are trying to do. So I can't tell you how to do it.

If your buy and sell orders are not dependent on one triggering the other. Then don't set them up w/ First Triggers.
View attachment 1107
https://tlc.thinkorswim.com/center/howToTos/thinkManual/Trade/Active-Trader/AT-Entering-Orders
In a nutshell, as soon as I buy a stock and the buy is filled, I want it to create an OCO with the conditional study order and the trailing stop loss with that stock immediately. But I will play with it. Thanks.
 
I just ran across this post. I as well have had study orders that I have saved as custom using the 'Save Custom Order Template'. After some playing around I do not believe that it is me (user error) and it has something to do with the custom order.

Just opening the custom order and hitting confirm and send the order will sit in a Wait Cond forever. However, if I go into the order then the study and hit edit. Change nothing hit then ok and then save it will execute using the correct condition.
 
Just opening the custom order and hitting confirm and send the order will sit in a Wait Cond forever. However, if I go into the order then the study and hit edit. Change nothing hit then ok and then save it will execute using the correct condition.
I was having the exact same issue, and your observation solved the problem. Thanks for sharing!
 
This work around is working out great but a real pain. I tested a few different scenarios and observed that the issue only comes up for custom built studies.
 
I have reported this issue to TOS and they confirmed it as a bug. The template does not load the conditional script, you have to load it manually by clicking edit or the script name in the gear dropdown.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
428 Online
Create Post

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