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

Timestamp of a trade



  • Hi there,
    I' ve got a little problem when it comes to show logs about timestamp of a trade, can You guys/gals can help me with it? Each time there is a 23:59:59.999989 time signature and not the one from the next bar close.

    Here's the code of my strategy:

    import backtrader as bt
    
    class Third_trade(bt.Strategy):
    
        def __init__(self):
            self.dataclose = self.datas[0].close
            # Order variable will contain ongoing order details/status
            self.order = None
            self.body = abs(self.datas[0].close - self.datas[0].open) == abs(self.datas[0].high - self.datas[0].low)
            self.body_second = abs(self.datas[-1].close - self.datas[-1].open) == abs(self.datas[-1].high - self.datas[-1].low)
    
        def log(self, txt, dt=None):
            dt = dt or self.datas[0].datetime.date(0)
            tm = self.datas[0].datetime.time()
            print('%s, %s, %s' % (dt, tm, txt))
    
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                #Active Buy/Sell order submitted/accepted - Nothing to do
                return
    
            #Check if an order has been completed
            #Attention: broker could reject order if not enough cash
            if order.status in [order.Completed]:
                if order.isbuy():
                    self.log('BUY EXECUTED, %.2f' % order.executed.price)
                elif order.issell():
                    self.log('SELL EXECUTED, %.2f' % order.executed.price)
                self.bar_executed = len(self)
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
            #Reset orders
            self.order = None
    
        def next(self):
            if self.order:
                return
            # Check if we are in the market
            if not self.position:
                #warunki do trejdu, jeśli nie jestem w pozycji
                if self.body and self.body_second:
                    print('OK')
                    if self.dataclose[0] > self.dataclose[-1] and self.dataclose[-1] < self.dataclose[-2]:
                        self.log('BUY CREATE, %.2f' % self.dataclose[0])
                        # Keep track of the created order to avoid a 2nd order
                        self.order = self.buy()
    
                    elif self.dataclose[0] < self.dataclose[-1] and self.dataclose[-1] < self.dataclose[-2]:
                        self.log('SELL CREATE, %.2f' % self.dataclose[0])
                        self.order = self.sell()
    
                else:
                    return
    
            else:
                # We are already in the market, look for a signal to CLOSE trades
                if len(self) >= (self.bar_executed + 1):
                    self.log('CLOSE CREATE, %.2f' % self.datas[0].open[0])
                    self.order = self.close()
    

    and below please find code of my cerebro launcher:

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    import backtrader.feeds as bf
    from third_buy_str import *
    import datetime
    
    cerebro = bt.Cerebro()
    
    cerebro.broker.set_cash(10000)
    
    #dodaje własny plik i opsuje co gdzie jest (kolumny)
    data = bf.GenericCSVData(
        dataname='CO2_res.csv',
        volume=-1, openinterest=-1,
    
    )
    #Dodaje dane z pliku do cerebro
    cerebro.adddata(data)
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    #Dodaje strategię do cerebro
    cerebro.addsizer(bt.sizers.SizerFix,stake=10)
    cerebro.addstrategy(Third_trade)
    
    
    cerebro.run()
    
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
    

    Here's a sample line from my csv file (date time, O,H,L,C):
    2019-12-31 10:50:00,25.43,25.48,25.43,25.45

    And finally here's a sample of my output prints:

    OK
    2020-03-04, 23:59:59.999989, SELL CREATE, 23.87
    2020-03-05, 23:59:59.999989, SELL EXECUTED, 23.83
    2020-03-05, 23:59:59.999989, CLOSE CREATE, 23.96
    2020-03-06, 23:59:59.999989, BUY EXECUTED, 23.51
    OK
    2020-03-06, 23:59:59.999989, BUY CREATE, 23.61
    2020-03-09, 23:59:59.999989, BUY EXECUTED, 23.01
    2020-03-09, 23:59:59.999989, CLOSE CREATE, 22.59
    2020-03-10, 23:59:59.999989, SELL EXECUTED, 23.44
    OK
    2020-03-10, 23:59:59.999989, BUY CREATE, 23.66
    2020-03-11, 23:59:59.999989, BUY EXECUTED, 24.10
    2020-03-11, 23:59:59.999989, CLOSE CREATE, 23.99
    2020-03-12, 23:59:59.999989, SELL EXECUTED, 23.65
    OK
    2020-03-12, 23:59:59.999989, SELL CREATE, 22.92
    2020-03-13, 23:59:59.999989, SELL EXECUTED, 22.58
    2020-03-13, 23:59:59.999989, CLOSE CREATE, 22.72
    2020-03-16, 23:59:59.999989, BUY EXECUTED, 21.50
    OK
    OK
    2020-03-18, 23:59:59.999989, SELL CREATE, 16.61
    2020-03-19, 23:59:59.999989, SELL EXECUTED, 15.55
    2020-03-19, 23:59:59.999989, CLOSE CREATE, 15.97
    2020-03-20, 23:59:59.999989, BUY EXECUTED, 16.66
    OK
    OK
    2020-03-20, 23:59:59.999989, SELL CREATE, 16.03
    2020-03-23, 23:59:59.999989, SELL EXECUTED, 15.21
    2020-03-23, 23:59:59.999989, CLOSE CREATE, 14.94
    2020-03-24, 23:59:59.999989, BUY EXECUTED, 15.96
    OK
    2020-03-24, 23:59:59.999989, SELL CREATE, 15.90
    2020-03-25, 23:59:59.999989, SELL EXECUTED, 17.00
    2020-03-25, 23:59:59.999989, CLOSE CREATE, 16.97
    2020-03-26, 23:59:59.999989, BUY EXECUTED, 17.30
    OK
    2020-03-26, 23:59:59.999989, BUY CREATE, 16.78
    2020-03-27, 23:59:59.999989, BUY EXECUTED, 17.17
    2020-03-27, 23:59:59.999989, CLOSE CREATE, 17.11
    2020-03-30, 23:59:59.999989, SELL EXECUTED, 16.35
    OK
    2020-03-30, 23:59:59.999989, SELL CREATE, 16.43
    2020-03-31, 23:59:59.999989, SELL EXECUTED, 17.25
    2020-03-31, 23:59:59.999989, CLOSE CREATE, 17.45
    Final Portfolio Value: 9987.80```


  • ok it was fast after I posted my request. Two lines in data field solved this problem:

        timeframe=bt.TimeFrame.Minutes,
        compression=15
    

    Sorry for bothering.


Log in to reply
 

});