Help with QS - if open price = x%

Hey, I’m not a coder so struggling a bit here. Please could someone help me write a bit of QS that is essentially:
If open price is within x% of the highest close price in past X bars then true /false if not. I think this should be a function? So i can use open long if function is true? Is that right?

Hi Jrbp,

Here’s my take if I understand your question correctly

you can make use of 'max and 'min function e.g
highest = max(close close[1] close[2] …) --> hint you can also use for loop function here
lowest = min( … )
then compare the open, trigger short when within 10% of highest or trigger long when within 10% of lowest.
somethig like this
enter long when open < 1.1lowest
enter short when open > 0.9
highest

regards ~Law

Hey, @Jrbp

For the highest/lowest price, you can use the provided built-in functions Highest or Lowest, which are explained in our documentation.

Getting the highest close price for the last 14 bars can look like this:

highestClose = Highest(value: close, period: 14)

Now if for your entry condition you want to check whether the current open price is less then X percent (75 in the example) of the highest close in a period you can write the whole thing like so:

highestClose = Highest(value: close, period: 14)
xPercent = 75

enter long when open < (xPercent / 100) * highestClose

As you see, the use of a function is not necessary for the entry condition.

You can check out more on using functions in the QuantScript guide https://docs.proquant.com/docs/functions/

A note on @lawlag’s answer:

The recommended and fastest way to get the highest price in a period is to use the Highest function. Using the Math.max function. , which only compares two values, will make the code unnecessarily slower and longer. The same applies to the usage of loops in this situation.

Hi Mihael,

Cool, I learned something new again. I have not explored yet all the built-in functions in the doc so I guessed it will be a good Idea to spend time with that.
Additional Question. can this or in general, can all function be preceded by indicating time frame? e.g.
highestClose = Instrument:1h Highest(value: close, period: 14) --> will this work ?
Thanks in advance

cheers ~Law

Yes, you can indicate the desired period and instrument, but as explained in the docs https://docs.proquant.com/docs/multiple-instruments-periods/
the correct syntax in your case would be

<Instrument name>:1h highestClose = Highest(value: close, period: 14)

Example:

Gold:1h highestClose = Highest(value: close, period: 14)
Tesla:15m highestClose = Highest(value: close, period: 14)

Lovely stuff, thanks guys! Let’s see if I can get where I want to with this help. I’ve already implemented bits of this into an existing strat which has reduced DD and increased return, so thank you

2 Likes