While I cannot debug the script you provided I can provide a simplifed sample of how to execute code 30 seconds before the primary bar. I have attached a script that prints to the output window the times for the event. For example if you are using a 15 minute bar we may see a print like the following:
The print happens when the time of the secondary series is greater or equal to the projected time. It resets for each new primary bar. You would need to use logic like this if you wanted to do something before the end of a primary bar. Please note that you will see varying times for the secondary series depending on how much volume the instrument in question has, if there is little volume it may wait some time which is why the secondary series timestamps may not be exactly at the projected time. It would be best to use 1 second or 1 tick granularity for the secondary series if you use a instrument that has times of low volume.
Comment