Session high for Conditional Order script


Category:
0
0

Hey Pete! I was having trouble posting this, so I’m not certain if this is the right section. From your videos, I know recursion is a no-no for Thinkorswim’s custom code order conditions. Is there any way to reference today’s high of the day without using recursion? It has to be done using the “1-minute” chart setting. You’re the best!

Marked as spam
Posted by (Questions: 5, Answers: 1)
Asked on March 26, 2024 12:33 pm
61 views
0
Private answer

I moved your question out of the "Watch Lists" topic and into the "Strategy Guide" topic because this is where we discuss items related to the Conditional Order tool on Thinkorswim. I also updated the title of your question to better describe the context of your request and the solution I am providing. This should make it easier for other visitors to locate this solution using the search function.

Yes, there is a way to write a script to read the highest high of the current session without using recursive variables. It's a pretty nifty trick, which I leaned from one of the Thinkorswim support staff.

Here it is:

def currentDay = GetDay() == GetLastDay();
plot highOfSession = HighestAll(if currentDay then high else Double.NaN);

You can replace the plot with def if you want to use that value elsewhere in your script. It only works for the current session. And this technique may also work for the session low. (In which case you would replace HighestAll with LowestAll and replace high with low).

With further modifications, it may even be used to capture the session open. But that would require the application of another trick to make that work.

def currentDay = GetDay() == GetLastDay();
def newDay = GetDay() <> GetDay()[1];
plot sessionOpen = HighestAll(if newDay and currentDay then open else Double.NaN);

So there we have solutions to read the current session's high, low and open. Each without using recursive variables.

Marked as spam
Posted by (Questions: 37, Answers: 4116)
Answered on March 26, 2024 12:48 pm
0
Wow!! That’s awesome Pete. Thanks for sharing!
( at March 26, 2024 10:03 pm)
0
Hey Pete, how can we use this code or a version of it in a conditional order? For example, when the current prices crosses above or is greater than the ORB high value, place a limit or market order. I have one setup, and just like your video states, when the offset is zero the error ”Secondary period cannot be less than primary”. So when the study offset is set to 1, I can see the limit order on the chart in the waiting condition, but it never executes. Please let me know if you need full code listed. If the issue is because there are recursive variables, is there a way to get the first 5 or 15 minutes high and low with out using recursive variables?
( at September 26, 2024 5:31 am)
0
First issue is "secondary period cannot be less than primary". This means your code is referencing a time frame other than the one you have selected for the conditional order. So that is a problem in your script and has nothing to do with the offset. Second issue, yes it may be possible to write a script that captures an Open Range High and Low without using recursive variables. That solution would require you to use two additional function calls "SecondsFromTime(930) >= 0" and "SecondsTillTime(1000) > 0". And you would use that as the filter criteria within the "HighestAll()" and "LowestAll() function calls to get your high and low of the opening range. It might work. But that would be the only feasible method you could try.
( at September 26, 2024 7:50 am)
0
This is what I currently have (separated into 2 different comments due to character limitation) # orb5_c def na = Double.NaN; def bn = BarNumber(); def timeOffsetFromESTForPST = -300; def firstORBStartTime = 0630 - timeOffsetFromESTForPST; input orb_time = AggregationPeriod.FIVE_MIN; input show_only_current_day = yes; def newDay = GetDay() GetDay()[1]; def current_day = (GetDay() == GetLastDay()); def firstOpen = if newDay then open(period = orb_time) else firstOpen[1]; def firstHigh = if newDay then high(period = orb_time) else firstHigh[1]; def firstLow = if newDay then low(period = orb_time) else firstLow[1]; def firstClose = if newDay then close(period = orb_time) else firstClose[1]; plot priceHigh; plot priceLow; plot midPoint;
( at September 26, 2024 8:31 am)
0
if show_only_current_day and !current_day then { priceHigh = na; priceLow = na; midPoint = na; } else { priceHigh = firstHigh; priceLow = firstLow; midPoint = (priceHigh + priceLow) / 2; } priceHigh.SetDefaultColor(GetColor(6)); priceHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL); priceLow.SetDefaultColor(GetColor(5)); priceLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL); midPoint.SetDefaultColor(GetColor(4)); midPoint.SetPaintingStrategy(PaintingStrategy.HORIZONTAL); #
( at September 26, 2024 8:31 am)
0
You cannot use that code for a conditional order. It is loaded with recursive variables and also references a secondary aggregation period. Just as I explained in my original response. Providing a solution to use an ORB trading setup using the conditional order tool is well beyond the scope of complexity for a free solution I can provide in this forum.
( at September 26, 2024 8:39 am)
0
Sorry. Just one more question. Is there any way to record a value at a specific time and store it without using a recursive variable? It seems that any thing that involves a if/then statement will be overwritten since else is required.
( at September 27, 2024 5:19 pm)
0
There is no blanket solution for this. If it fits within the same method I have described in the solution than the answer is yes. Here is the basic template for the method employed in this solution: def targetValue = HighestAll(if currentDay and condition then value else Double.NaN); This will grab the "value" at the point where "condition" is true. However if there are more than one bars in the current session for which "condition" is true, you will only pick the highest of all of those values.
( at September 27, 2024 5:53 pm)