KeltnerChannel conditional order did not trigger


Category:
0
0

Keltner /atr conditional order code below working well before today.  See anything wrong?  Photo shows orders placed yesterday but not filled today.  ToS support had no idea why.

declare weak_volume_dependency;

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.exponential;
input trueRangeAverageType = AverageType.exponential;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

def Upper_Band = average[-displace] + shift[-displace];

#def Lower_Band = average[-displace] – shift[-displace];

plot buywhencloseisbelow = upper_band;

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on August 1, 2025 4:53 pm
55 views
1
Private answer

i finally came up with the right code, thanks largely to you, Pete, for which i again thank you.  the solution is in the last line.  bueno!

# conditional orders solution for Keltners

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.exponential;
input trueRangeAverageType = AverageType.exponential;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
def Upper_Band = average[-displace] + shift[-displace];

plot buytrigger = close[1] > upper_band and close < upper_band;

 

Marked as spam
Posted by (Questions: 6, Answers: 3)
Answered on August 5, 2025 11:40 am
0
Private answer

I moved your question over to the "Strategy Guide" topic because this is where we find solutions for questions related to chart strategies and conditional order scripts.

Check the attached screenshot. I have replicated one of your three conditional orders and I used the "Study Alert" tool on Thinkorswim to display the true/false signals being generated by your script. I used the same ticker symbol and time frame as in your example screenshot.

Note the cyan colored line on the lower sub-graph of the chart. It displays a value of 1 when the condition is true and a value of 0 when it is false. Note this is a daily time frame and it is only showing the state of the entry signal as of the close of the bar on each day.

From this view, there is no way to tell how that signal evolved during the course of the day. Did the close of today's daily candle ever trade below the value of the keltner line? There is no way to know for certain because your script is designed to chase a moving target. That keltner line is changing throughout the day. All we see at this point is where the value of that line was at the close of the day. We don't get to see where it was during the course of the day.

I spot-checked a few time stamps from 7/30/25 using the OnDemand feature of Thinkorswim. It looks to me like the close was always above the kletner band for the entire session.

Mystery solved. Hopefully I have provided you enough training to be able to replicate these troubleshooting steps on your own.

If you would like my opinion on this matter. The method you have employed here is not suitable for back-testing. And historical signals you examine in the past do not reflect how those signals would have been executed on a live chart.

Why? Because you have constructed a signal which is based on the current bar's close and the current bar's Keltner channel.This will cause signals which repaint. They change during the course of the trading session. And you only get to see the final state of the signal on each bar and not the condition of that signal during the course of each daily candle.

If you want to fix this, to make it a proper fit for back-testing, you will update the script as follows:

close is less than keltorders ("factor" = 1.5). "buywhencloseisbelow" from 1 bars ago

But you will find the back-test results are very different than what you are using now. Be aware of that and complete your research before implementing this.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4138)
Answered on August 1, 2025 5:18 pm
0
i am super-duper excited to try this using on-demand with yesterday’s data, which isn’t available to me yet. i did try it on the previous day’s prices and it worked just as i’d hoped. https://i.imgur.com/qNYXG3h.png. in the attached photo, if i’m reading things right, you can see that 1.5k is currently 41.46 and price is below it at 41.30 and the keltnerorders study signal is at 1 and firing. a few minutes later, when price rises above 1.5k, the signal flat lines again at 0, but by that time, the order should already have been filled. but that was 2 days ago, and i am impatiently waiting to see what’ll happen when yesterday’s prices show up. p.s. if it doesn’t work, doesn’t that also mean that no conditional order using price crossing a moving average will consistently work, since keltners are essentially a moving average? or maybe it means that it’ll only work on daily-closing-price data and not intraday, if i’m making any sense at all.
( at August 2, 2025 5:20 am)
0
Great work. That is exactly how you need to test this out and find out which conditions are working and when. Perhaps in this case the signal only lasted a few seconds, or not enough time for it to be recognized by the conditional order. But armed with his information, you can contact Thinkorswim again and maybe they can be convinced to dig a bit deeper in helping to resolve this. You can also try modifying your signal to capture the close crossing below the Keltner line. However this would not pick up a signal in which the daily candled opened below the Keltner line. Either way, there are tradeoffs and you may need to transfer this automated trade entry to a trading platform that supports fully automated trading. TradeStation would not work, because it would require you reference the previous daily bar's Keltner value instead of the current value. Perhaps TradingView or TrendSpider would allow you to build automated orders of this type. NinjaTrader may be another option.
( at August 2, 2025 2:48 pm)
0
well, i just went and proved to myself that not even a conditional order with a simple price / moving average cross will fire an order even tho the cross shows up in the study. https://i.imgur.com/kwuhVxs.png. in this case, i made the condition read close "crosses below" the 20MA. made no difference. only thing i can conclude is that close means daily closing price and not the price as it fluctuates during the day. i guess what i want is 'mark' instead of 'close' but 'mark' isn't an option. bummer. i am flummoxed.
( at August 2, 2025 2:57 pm)
0
It's definitely a challenge given the limitations of Thinkorswim. I am sure if you change to "Keltner from 1 bars ago" you will find it delivers more consistent results. But I know that is not what are shooting for. You can try this out on TradingView or TrendSpider in a simulated account without having to pay any fees. Might be worth a try.
( at August 2, 2025 3:02 pm)
0
thanks much. i continue to be baffled. i mean, i set up the original code as an alert in market watch and the alerts were triggered right on schedule. https://i.imgur.com/VZBtB65.png. why would it work there and not live? well, it does work sometimes but not others. gots to be a bug somewhere, imho, but what do i know>
( at August 2, 2025 3:06 pm)