I have created a small study which basically looks to see if the current close of a stock is higher by some amount than another calculated value. For example, if the calculated value is $190, the study would return a value of 1 if the current close is greater than $196. The study works fine and I can put it on a chart and use a watchlist to switch charts with no problem. however, when I try to set up a scan which scans a small watchlist using this study and finds the stocks with a value of 1, ThinkorSwim actually completely crashes(which I've never had it do before with any scan I've set up). The filter I'm trying to add is: ECTEST1() is equal to 1. I also tried ECTEST1()."Reversed" is equal to 1. I am completely stumped. Can anyone help? I used AI to generate the code, but I can't see anything wrong with the algorithm. As I said, I can put the study onto charts and it runs with no problems. The crash does not occur when I try to run the scan, because I never get that far. It actually occurs when I try to save the new filter. I add a new filter, select the study ECTEST1, choose "Reversed", "Is Equal to",value =1. I go to save it and thinkorswim crashes.
Here is the code:
# P&F Traditional Box Logic Study
# Define the Traditional Box Size Scale
def price = close;
def boxSize = if price < 0.25 then 0.0625
else if price < 1.00 then 0.125
else if price < 5.00 then 0.25
else if price < 20.00 then 0.50
else if price < 100.00 then 1.00
else if price < 200.00 then 2.00
else if price < 500.00 then 4.00
else if price < 1000 then 5.00
else 50.00;
# Calculate the "Current Box" (Rounding down to the nearest box)
# Logic: Price divided by box size, floored to integer, then multiplied back
def currentBox = Floor(price / boxSize) * boxSize;
# Track the Lowest Box reached (to find the reversal point)
# We use a recursive variable to track the 'Floor' of the current move
def lowestBoxInMove = if SecondsTillTime(0930) == 0 then currentBox
else Min(currentBox, lowestBoxInMove[1]);
# Define the 3-Box Reversal Logic
# Target = Lowest Box + (3 * BoxSize)
def reversalTarget = lowestBoxInMove + (3 * boxSize);
# The Condition: Is the current price at or above the 3-box reversal target?
def IsReversed = IF (price >= reversalTarget) THEN 1 else 0;
plot Reversed = isreversed;
Reversed.hide();
# Optional: Display the current Box Size and Level for debugging
#AddLabel(yes, "Box Size: " + boxSize + " | Current Box: " + currentBox, Color.WHITe);
Here is the code:
# P&F Traditional Box Logic Study
# Define the Traditional Box Size Scale
def price = close;
def boxSize = if price < 0.25 then 0.0625
else if price < 1.00 then 0.125
else if price < 5.00 then 0.25
else if price < 20.00 then 0.50
else if price < 100.00 then 1.00
else if price < 200.00 then 2.00
else if price < 500.00 then 4.00
else if price < 1000 then 5.00
else 50.00;
# Calculate the "Current Box" (Rounding down to the nearest box)
# Logic: Price divided by box size, floored to integer, then multiplied back
def currentBox = Floor(price / boxSize) * boxSize;
# Track the Lowest Box reached (to find the reversal point)
# We use a recursive variable to track the 'Floor' of the current move
def lowestBoxInMove = if SecondsTillTime(0930) == 0 then currentBox
else Min(currentBox, lowestBoxInMove[1]);
# Define the 3-Box Reversal Logic
# Target = Lowest Box + (3 * BoxSize)
def reversalTarget = lowestBoxInMove + (3 * boxSize);
# The Condition: Is the current price at or above the 3-box reversal target?
def IsReversed = IF (price >= reversalTarget) THEN 1 else 0;
plot Reversed = isreversed;
Reversed.hide();
# Optional: Display the current Box Size and Level for debugging
#AddLabel(yes, "Box Size: " + boxSize + " | Current Box: " + currentBox, Color.WHITe);
Attachments
Last edited: