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

How am i sure buy and sell order are executed immediately ?



  • Hi, I am a new newbie to backtrader
    I`m trying to run backtrader with many datas
    Here is my strategy.
    Make a list of stocks to buy and run a loop so that buy it (open price) and sell it (close price)
    But I am not sure that logic is executed immediately and successfully

    Thing is my notify_order function prints only buy traction logs for a while, not also sell logs orderly
    Like below

    My dataset is consist of only 10 record but buy logs are more than that in a one day

    BUY @price: 391000.00
    BUY @price: 49400.00
    BUY @price: 49400.00
    BUY @price: 254500.00
    BUY @price: 257000.00
    BUY @price: 404000.00
    BUY @price: 411000.00
    BUY @price: 174447.00
    BUY @price: 77300.00
    BUY @price: 78100.00
    BUY @price: 52540.00
    BUY @price: 228900.00
    BUY @price: 177251.00
    BUY @price: 51380.00
    BUY @price: 391000.00
    BUY @price: 391500.00
    

    My Expectation is like this

    BUY @price: XXXXX
    SELL @price: XXXXX
    BUY @price: XXXXX
    SELL @price: XXXXX
    BUY @price: XXXXX
    SELL @price: XXXXX
    BUY @price: XXXXX
    SELL @price: XXXXX
    BUY @price: XXXXX
    SELL @price: XXXXX
    BUY @price: XXXXX
    SELL @price: XXXXX
    

    Here is my code

    import backtrader as bt
    class lowCloseStrategy(bt.Strategy):
        params = (
            ('toBuyList', {}),
            ('sma1', 5),
            ('sma2', 10),
            ('oneplot', True),
        )
        buy_order = None  # default value for a potential buy_order
    
        def __init__(self):
            self.order = None
    
        def next(self):
            for i, d in enumerate(self.datas):
                if d._name not in self.p.toBuyList[self.datetime.date(ago=-1).strftime('%Y-%m-%d')]:
                    continue
                dt, dn, dc, dv, do = self.datetime.date(), d._name, d.close[0], d.volume[0],d.open[0]
    
                pos = self.getposition(d).size
                self.order = self.buy(exectype=bt.Order.Limit,price=do,data=d,size=1) # Is it work immediately?
                self.order = self.sell(exectype=bt.Order.Limit,price=dc,data=d,size=1) # Is it work immediately?
    
    
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
    
        def notify_order(self, order):
            if not order.status == order.Completed:
                return  # discard any other notification
    
            if not self.position:  # we left the market
                print('SELL@price: {:.2f}'.format(order.executed.price))
                return
    
            # We have entered the market
            print('BUY @price: {:.2f}'.format(order.executed.price))
    

    I hope you guys understand my purpose and appreciate to yours reply in advance
    Thanks



  • According to your code:

    • you print BUY @price... notification for any order, not only for buy order
    • notification SELL@price... can only be printed if you have no position in the data0, also this notification will be printed for any order, not sell only

    I would recommend you to go thru Docs - Quickstart Guide and then read about orders Docs - Orders - General and Docs - Orders - Creation/Execution. Most of you questions will be answered.



  • @ab_trader Thanks i read all of you give me and I understood enough about what i asked before


Log in to reply
 

});