-2

I am trying to figure out how to do the following:

If "some logic" occurs for more than 15 seconds: "execute some code"

Im not quite sure how to ask python to do this. I am working with stock prices and my index column is dtype='datetime64[ns].

I tried searching other threads for hints on how to do this but i wasn't even quite sure what to search for.

Sorry in advance if its a very elementary question.

3 Answers3

0

try takinng the current time and comparing it every tick

asdfs99
  • 21
  • 2
0

One way to accomplish this is to use threads (multiprocessing) as they would be 2 tasks "some logic" and the "scheduler".

This can be addressed easily with the following logic:

import threading
import time

lock = threading.Lock()
cond = threading.Condition(threading.Lock())

def waitLock(timeout):
    with cond:
        current_time = start_time = time.time()
        while current_time < start_time + timeout:
            if lock.acquire(False):
                return True
            else:
                cond.wait(timeout - current_time + start_time)
                current_time = time.time()
    return False

Source: How to implement a Lock with a timeout in Python 2.7

Rafael Aguilar
  • 3,084
  • 1
  • 25
  • 31
0
import time

def check():
    if not some_logic(): return False

    start_time = time.time()
    while time.time() - start_time <= 15:
        if not some_logic(): return False

    return True

if check():
    ... # do stuff

This runs some_logic as many times as possible to ensure that it returns True throughout the whole 15-second period.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I think this will work! thank you! If i run into a snag il reply again. – Sagittar1us19 Apr 16 '18 at 18:00
  • the issue i seem to be running into is that my program runs through each new line of price data like a loop and therefor makes "start_time" a different value every time the script progresses down the line of data. How do I make it so that start time remains the same every time the scrpit runs through a new line? – Sagittar1us19 Apr 18 '18 at 18:15
  • @Sagittar1us19, set `start_time = time.time()` first and then do the looping? Or use multithreading: one thread does the checking, the other one works with data. This really depends on what your code does. – ForceBru Apr 18 '18 at 18:36
  • The trading environment i am using is named backtrader. When putting in the trading strategy you do it in a method that goes through each row of data one by one. Each time the program goes to a new row, it makes that row into [0]. So the only way that the strategy could know if condition is true, is by going through the loop unfortunately. – Sagittar1us19 Apr 18 '18 at 19:19
  • I am going to try to store the time the condition is true into a list and then keep checking if the current time - the [0] value in the list is less than 5 . Then clear the list after I have made use of the start time. Hopefully that will work – Sagittar1us19 Apr 18 '18 at 19:50