For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Orders placed with `valid=` never executed



  • When placing an order like so:

    self.buy_bracket(limitprice=1.1, price=1.0, stopprice=0.99, valid=self.datas[0].datetime.date(0) + timedelta(hours=8))
    

    No orders are ever placed. When I remove the valid code it works again. I've tried a few different versions of valid, including:

    valid=now() + timedelta(hours=8)

    valid=timedelta(hours=8)

    valid=datetime.timedelta(hours=8)


  • administrators

    If you work, for example, with a timeframe of Weeks, how are 8 hours supposed to make any sense?



  • @backtrader

    Python datetime objects do not care about timeframe.

    0_1530640932056_Screen Shot 2018-07-03 at 7.01.34 PM.png

    I don't think I'm setting a timeframe anywhere in backtrader that I know of.

    I don't understand your reply.


  • administrators

    You post a single line of code and say something is not happening.

    The answer points out that you could be working with a given timeframe (Weeks) for which your timedelta (8 hours), makes no sense because the order would be immediately invalidated.

    I.e.: a single line of code with no context means nothing. If you thing you have uncovered a problem (and not a problem of your own code), you can share a complete snippet which reproduces the problem.



  • @backtrader said in Orders placed with `valid=` never executed:

    The answer points out that you could be working with a given timeframe (Weeks) for which your timedelta (8 hours), makes no sense because the order would be immediately invalidated.

    So, my data is a 5M timeframe. With code like this valid=self.datas[0].datetime.date(0) + timedelta(minutes=960), all of my orders get canceled.

    I'm asking if the syntax above is right for what I'm saying: I want all orders which are not entered to expire after 16 hours (960) minutes.


  • administrators

    @tw00000 said in Orders placed with `valid=` never executed:

    I don't think I'm setting a timeframe anywhere in backtrader that I know of.

    @tw00000 said in Orders placed with `valid=` never executed:

    So, my data is a 5M timeframe

    The two statements contradict each other. Either you are not setting a timeframe or you are using a 5-minutes timeframe. A sample of some working code (and not an isolated line and then the isolated line again) could help.

    @tw00000 said in Orders placed with `valid=` never executed:

    I'm asking if the syntax above is right for what I'm saying: I want all orders which are not entered to expire after 16 hours (960) minutes.

    The syntax is right or else you would have gotten an error, or? But using the right syntax doesn't help if it's used in the wrong context.



  • @tw00000 said in Orders placed with `valid=` never executed:

    I don't think I'm setting a timeframe anywhere in backtrader that I know of.

    When you add data to the system, you may want to specify your data's timeframe and compression to notify the system.

    From the FAQ

    data = bt.feeds.MyChosenFeed(
        dataname='myname',
        timeframe=bt.TimeFrame.Minutes, compression=5,
        sessionstart=datetime.time(9, 0), sessionend=datetime.time(16, 0)
    )
    


  • @backtrader said in Orders placed with `valid=` never executed:

    @tw00000 said in Orders placed with `valid=` never executed:

    I don't think I'm setting a timeframe anywhere in backtrader that I know of.

    @tw00000 said in Orders placed with `valid=` never executed:

    So, my data is a 5M timeframe

    The two statements contradict each other. Either you are not setting a timeframe or you are using a 5-minutes timeframe. A sample of some working code (and not an isolated line and then the isolated line again) could help.

    Ok, so backtrader is automatically detecting the timeframe of my data then? I'm not setting a timeframe anywhere in my code.


    @ab_trader thank you for explaining this:

    When you add data to the system, you may want to specify your data's timeframe and compression to notify the system.
    From the FAQ

    data = bt.feeds.MyChosenFeed(
        dataname='myname',
        timeframe=bt.TimeFrame.Minutes, compression=5,
        sessionstart=datetime.time(9, 0), sessionend=datetime.time(16, 0)
    )
    

    This is interesting, my code appears to run without having set the timeframe, compression, or session start in my data feed, which is really simple:

    # Add the data
    df = pd.read_csv('data/{}_5m.csv'.format(instrument_name))
    df.columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
    df.index = pd.to_datetime(df['Date'], unit='ms')
    df['Date'] = pd.to_datetime(df['Date'], unit='ms')
    df = df.drop('Date', axis=1)
    data = bt.feeds.PandasData(dataname=df)
    
    cerebro.adddata(data)
    

    I will experiment with explicitly setting the timeframe and see if that changes how the valid = arg works!


  • administrators

    @tw00000 said in Orders placed with `valid=` never executed:

    Ok, so backtrader is automatically detecting the timeframe of my data then?

    No. How do you expect it do be done automatically?

    @tw00000 said in Orders placed with `valid=` never executed:

    This is interesting, my code appears to run without having set the timeframe, compression, or session start in my data feed, which is really simple:

    Of course your code will work. The platform will move through your prices. But things which depend on timeframe/timestamps will probably not match your expectations. The platform cannot know that the bars are not daily bars, which is the default.



  • @tw00000 remember I proposed you to read docs first? :) Read at least FAQ, can save you time in the future.