Expanded Time Functions?

Hey, first post here and am really loving the QuantScript!

When tinkering around I was strugging to find an easy way to use Time variables in the way I want as it seems to define time as Monday Tuesday etc and was hoping it might be able to be expanded to actual “Today” “This Hour” etc.

Reason why is most of the strategies I’ve tried so far are basically “big winners” whereby if it hits it wins big, but if it doesnt it hits its stop loss pretty almost as soon as it begins (with the stop loss set as low as it will go) so a way of adding a line which says “if a position has already been opened today… in this case leading to it then hitting the stop and the opening conditions still being valid at the stop level - dont open another one again” would be brilliant.

Unless there is currently a way to do this please do let me know? Will save me a lot of small losses and increase profitablity immensely

Essentially you’re asking for QS to have access to closed/open positions of the strategy, yes?

If so, we are thinking of ways to do this without messing up strategy following, as every follower has different positions with different results to the manager, but the script would only run for the manager.


Essentially, yeah. Appreciate it’s not an easy thing without possibly interfering with other people’s strategies, so thankyou for answering!

For what I need I wouldn’t need direct access to the times of every open/close, literally just a flag to say “a position was opened/closed” and then an ability to determine was that within a set timeframe ie. within the past hour, past 24hours etc … could even be done using ‘bars’ since open if it’s easier than time?

You could use series to build a counter for how many bars have passed since the last time a condition was true. Then feed your entry rule to that series/counter.

Something like https://docs.proquant.com/docs/Consecutive however this one counts consecutive “true” values of a condition.

Learn about creating custom series here: https://docs.proquant.com/docs/series

I’m not currently on my laptop so I can’t write up an example, I’ll try to do that a bit later. We might even create such a function as a built-in, sounds useful.

Would that kind of logic work for you, actually? Keep in mind that the presence of a signal does not necessarily mean an opened/closed position as there may have been an error on the broker side or some other issue which caused the signal to not execute.

A counter like that will work reasonably well, even if it doesn’t excute for some reason as I’m in a situation where there are sometimes 3 trades opened in the same day… so if for whatever reason the first two don’t execute I could still have the third?

A built in function would be cool but you’ve given me a good bit of inspiration so as is so will give it a go :blush:

1 Like

Great, let me know if you need any help! Make sure to use the Debug mode, it helps to validate your code is doing what you want it to.

Hey bud, so I’ve been messing around and I’ve got this, which doesnt work for my data (gives unknown) but works for AlwaysTrue and AlwaysFalse variables I made, returning values of 0 and 500 respectively:

function CountToLastOpen(Bool):
series count:
count = 0
then: count[1] + 1 if not Bool else 0

And the following comments I made below to try and explain my thoughts:
#Upon buy Signal set count = 0 then count = count + 1 until BuySignal = true again

On inital inspection i think my problem is my Bool of “Buy Signal” is literally just a static true/false driven by the opening price and not a series iself… either that or the setup of the custom series has thrown me?

For reference my Bool is as follows:

BuySignal = true if open[0] < BuyWindow and BuyAllowed and CurrentlyRising else false

enter long when BuySignal

Any ideas to try and help my poor coding out?? haha

*Edit: BuyAllowed is a Bool that is true/false based on whether certain technical conditions are met and CurrentlyRising is again another Bool that returns true/false depending if the long term moving average is rising or not

AlwaysTrue = open[0] > 0
AlwaysFalse = not AlwaysTrue*

I suspect you’re not handling initial unknown values for your Bool parameter. If an unknown value sneaks in, it ruins the whole series, everything will be unknown from then on.

Truth be told, this is an issue with the current Consecutive implementation as well.

Try the following:

once: 0 if isknown Bool else unknown

That way you only initialise the series once the parameter has a value itself.

Again, I’m not on my laptop so this is pure speculation, let me know if it’s of any help.

P.S. your BuySignal is indeed a series. You also don’t need a ternary if there, “and/or” operators return a Boolean.

1 Like

Thats brilliant thankyou! Solved it immediately.

Also had to note that since when the BuySignal hits it resets count to 0, I had to enter BuySignal[1] into CountToLast. :slight_smile:

Thankyou so much for your help again Nikola, really really appreciated!

1 Like

Can we perform time arithmetic and narrow the time precision to a specific HH:mm?

@Riggy77 Could you please provide an example?

1 Like

I want to input a schedule of future dateTimes based on the economic announcements calendar in order to catch the spike direction on the 1m chart and open positions (as i understand it, on the next bar only so some time and pips lost but i think the strategy will perform).

I also want to set behaviour based on US, Asian and European market opening and closing times in order to proactively identify trending currencies.

I also want to be able to set time stop limits on positions so that they expire. Will the feature become available @Nikola ?

I have tried this code out to keep a track of the entry price, one strange thing I notice is that when the “count” is above 500 it starts to return unknowns. I assume this is because the Time travel has a limit of [500] . Is there any way around this ?

no matter, I figured out a work around by having a separate series for the entry price.