Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Used of timer with cheat on open

    General Code/Help
    2
    4
    100
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sj last edited by

      Hi, For one of my strategy I am using timer for weekly rebalancing and cheat on open. I can see in the log that the order is executed the same day it is issued. Is it as expected? Attached is the code and log for your reference.

      class StrategyOptimization(bt.Strategy):
          params = dict(
              momentum=Momentum,
              mom_period=126,
              num_positions=20,
              when=bt.timer.SESSION_START,
              timer=True,
              cheat=True,
              weekdays = [1],
              weekcarry=True,
              history = 200,
              printlog = True,
              reserve = 0.01
          )
      ...
          def __init__(self):
              # Add a timer which will be called on the 1st trading day of the month
              if self.p.timer:
                  self.add_timer(
                      when=self.p.when,
                      weekdays=self.p.weekdays,
                      weekcarry=True
                  )
              if self.p.cheat:
                  self.add_timer(
                      when=self.p.when,
                      weekdays=self.p.weekdays,
                      weekcarry=True,
                      cheat=True,
                  )
      .....
      cerebro = bt.Cerebro(cheat_on_open=True)
      cerebro.broker.set_coo(True)
      .......
      

      Log

      2010-11-29T00:00:00, BUY Fresh Buy: MANAPPURAM, Open: 74.95, Close: 70.28
      2010-11-29T00:00:00, BUY Fresh Buy: UBL, Open: 400.00, Close: 389.65
      2010-11-29T00:00:00, BUY Fresh Buy: VGUARD, Open: 11.93, Close: 12.29
      2010-11-29T00:00:00, BUY Fresh Buy: JUBLFOOD, Open: 291.07, Close: 298.07
      2010-11-29T00:00:00, BUY Fresh Buy: CANBK, Open: 695.00, Close: 709.30
      2010-11-29T00:00:00, BUY Fresh Buy: NATCOPHARM, Open: 64.38, Close: 64.12
      2010-11-29T00:00:00, BUY Fresh Buy: M&MFIN, Open: 150.40, Close: 154.86
      2010-11-29T00:00:00, BUY Fresh Buy: BAJFINANCE, Open: 70.94, Close: 69.33
      2010-11-29T00:00:00, BUY Fresh Buy: TATAMOTORS, Open: 230.71, Close: 234.68
      2010-11-29T00:00:00, BUY Fresh Buy: TITAN, Open: 179.95, Close: 184.19
      2010-11-29T00:00:00, BUY Fresh Buy: NAUKRI, Open: 314.00, Close: 314.95
      2010-11-29T00:00:00, BUY Fresh Buy: HINDALCO, Open: 198.50, Close: 201.75
      2010-11-29T00:00:00, BUY Fresh Buy: INDUSINDBK, Open: 285.05, Close: 294.35
      2010-11-29T00:00:00, BUY Fresh Buy: PETRONET, Open: 60.25, Close: 58.70
      2010-11-29T00:00:00, BUY Fresh Buy: AARTIIND, Open: 30.17, Close: 31.46
      2010-11-29T00:00:00, BUY Fresh Buy: COROMANDEL, Open: 290.05, Close: 277.38
      2010-11-29T00:00:00, BUY Fresh Buy: AUROPHARMA, Open: 124.60, Close: 125.02
      2010-11-29T00:00:00, BUY Fresh Buy: EICHERMOT, Open: 114.69, Close: 114.77
      2010-11-29T00:00:00, BUY Fresh Buy: ICICIBANK, Open: 206.55, Close: 209.87
      2010-11-29T00:00:00, BUY Fresh Buy: SRTRANSFIN, Open: 782.40, Close: 771.05
      2010-11-29T00:00:00, BUY Fresh Buy: BANKINDIA, Open: 430.10, Close: 454.60
      2010-11-29T00:00:00, BUY Fresh Buy: RAJESHEXPO, Open: 112.00, Close: 113.65
      2010-11-29T00:00:00, BUY Fresh Buy: BANKBARODA, Open: 178.00, Close: 182.16
      2010-11-29T00:00:00, BUY Fresh Buy: CHOLAFIN, Open: 34.80, Close: 34.36
      2010-11-29T00:00:00, BUY Fresh Buy: BAJAJ-AUTO, Open: 1602.50, Close: 1592.70
      2010-11-29T00:00:00, BUY Fresh Buy: PAGEIND, Open: 1380.00, Close: 1380.85
      2010-11-29T00:00:00, BUY Fresh Buy: TVSMOTOR, Open: 82.50, Close: 80.15
      2010-11-29T00:00:00, BUY Fresh Buy: TCS, Open: 509.25, Close: 517.12
      2010-11-29T00:00:00, BUY Fresh Buy: CUB, Open: 44.05, Close: 43.01
      2010-11-29T00:00:00, BUY Fresh Buy: SBIN, Open: 287.21, Close: 287.98
      2010-11-29T00:00:00, BUY Fresh Buy: CUMMINSIND, Open: 542.86, Close: 548.54
      2010-11-29T00:00:00, BUY Fresh Buy: ADANIENT, Open: 65.37, Close: 69.21
      2010-11-29T00:00:00, BUY Fresh Buy: BIOCON, Open: 63.50, Close: 63.67
      2010-11-29T00:00:00, BUY Fresh Buy: BALKRISIND, Open: 74.46, Close: 74.03
      2010-11-29T00:00:00, BUY Fresh Buy: FEDERALBNK, Open: 44.50, Close: 43.98
      2010-11-29T00:00:00, BUY Fresh Buy: GUJGASLTD, Open: 382.00, Close: 384.85
      2010-11-29T00:00:00, BUY Fresh Buy: GRASIM, Open: 368.19, Close: 368.15
      2010-11-29T00:00:00, BUY Fresh Buy: VOLTAS, Open: 242.65, Close: 247.35
      2010-11-29T00:00:00, BUY Fresh Buy: APOLLOHOSP, Open: 464.15, Close: 475.75
      2010-11-29T00:00:00, BUY Fresh Buy: PNB, Open: 230.00, Close: 235.23
      2010-11-29T00:00:00, BUY EXECUTED, Price: 74.95, Cost: 3822.45, Comm 9.56
      2010-11-29T00:00:00, BUY EXECUTED, Price: 400.00, Cost: 35200.00, Comm 88.00
      2010-11-29T00:00:00, BUY EXECUTED, Price: 291.07, Cost: 21248.11, Comm 53.12
      2010-11-29T00:00:00, BUY EXECUTED, Price: 695.00, Cost: 4170.00, Comm 10.42
      2010-11-29T00:00:00, BUY EXECUTED, Price: 64.38, Cost: 34765.20, Comm 86.91
      2010-11-29T00:00:00, BUY EXECUTED, Price: 150.40, Cost: 33539.20, Comm 83.85
      2010-11-29T00:00:00, BUY EXECUTED, Price: 70.94, Cost: 35399.06, Comm 88.50
      2010-11-29T00:00:00, BUY EXECUTED, Price: 179.95, Cost: 33830.60, Comm 84.58
      2010-11-29T00:00:00, BUY EXECUTED, Price: 314.00, Cost: 3140.00, Comm 7.85
      2010-11-29T00:00:00, BUY EXECUTED, Price: 285.05, Cost: 5130.90, Comm 12.83
      2010-11-29T00:00:00, BUY EXECUTED, Price: 60.25, Cost: 4338.00, Comm 10.84
      2010-11-29T00:00:00, BUY EXECUTED, Price: 290.05, Cost: 35966.20, Comm 89.92
      2010-11-29T00:00:00, BUY EXECUTED, Price: 114.69, Cost: 34521.69, Comm 86.30
      2010-11-29T00:00:00, BUY EXECUTED, Price: 782.40, Cost: 8606.40, Comm 21.52
      2010-11-29T00:00:00, BUY EXECUTED, Price: 178.00, Cost: 23496.00, Comm 58.74
      2010-11-29T00:00:00, BUY EXECUTED, Price: 34.80, Cost: 16425.60, Comm 41.06
      2010-11-29T00:00:00, BUY EXECUTED, Price: 1602.50, Cost: 19230.00, Comm 48.07
      2010-11-29T00:00:00, BUY EXECUTED, Price: 1380.00, Cost: 34500.00, Comm 86.25
      2010-11-29T00:00:00, BUY EXECUTED, Price: 82.50, Cost: 19387.50, Comm 48.47
      2010-11-29T00:00:00, BUY EXECUTED, Price: 509.25, Cost: 22916.25, Comm 57.29
      2010-11-29T00:00:00, BUY EXECUTED, Price: 44.05, Cost: 13082.85, Comm 32.71
      2010-11-29T00:00:00, BUY EXECUTED, Price: 542.86, Cost: 34200.18, Comm 85.50
      2010-11-29T00:00:00, BUY EXECUTED, Price: 44.50, Cost: 8054.50, Comm 20.14
      2010-11-29T00:00:00, BUY EXECUTED, Price: 464.15, Cost: 7890.55, Comm 19.73
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @sj last edited by

        @sj From the docs:

        Trying cheat-on-open
        
        The sample below has a strategy with 2 different behaviors:
        
            If cheat-on-open is True, it will only operate from next_open
        
            If cheat-on-open is False, it will only operate from next
        
        In both cases the matching price must be the same
        
            If not cheating, the order is issued at the end of the previous day and will be matched with the next incoming price which is the open price
        
            If cheating, the order is issued on the same day it is executed. Because the order is issued before the broker has evaluated orders, it will also be matched with the next incoming price, the open price.
        
            This second scenario, allows calculation of exact stakes for all-in strategies, because one can directly access the current open price.
        
        

        RunBacktest.com

        S 1 Reply Last reply Reply Quote 0
        • S
          sj @run-out last edited by

          @run-out Thank you for the response. Yes, I saw this however let me give some more details, both with cheat-on-open as True
          Scenario 1 when no timer is used - The strategy is rebalanced every Monday and I have used dt.weeked() condition to make that happen. When I extract the transition using pyfolio integration, I see that buy/sell orders are issued on Tuesday and also executed on Tuesday open price.
          Scenario 2 when timer is used to trigger rebalancing - When I extract transitions, I see orders are issues on Monday and executed on Monday open price.
          Is is as expected and right behaviour? Appreciate any help!

          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @sj last edited by

            @sj I regret I've hardly ever used the timers. Perhaps someone else can help clear this up?

            RunBacktest.com

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors