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

Need help to get started with pre-made signal



  • Hi there
    I am super new to both bt and python. Maybe someone can help.
    I loaded btc price data (close only) and included a sell/buy indicator for each day via a csv.
    The csv is called 'output.csv'. Price data is in column 5 and the 'signal' in column 16.
    I get an output plot but the strategy is not being executed.
    Where do I go wrong?
    Sources I already looked into:
    https://community.backtrader.com/topic/812/using-my-own-pre-calculated-indicators
    https://www.backtrader.com/docu/extending-a-datafeed.html

    import datetime
    import backtrader as bt
    import backtrader.feeds as btfeeds
    from backtrader.feeds import GenericCSVData
    
    class firstStrategy(bt.Strategy):
        
        def next(self):
            if not self.position:
                if self.data.signal > 0:
                    self.buy(size=100)
            else:
                if self.data.signal < 1:
                    self.sell(size=100)
                    
    startcash = 10000
    
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(firstStrategy)
    
    
    class GenericCSV_PE(GenericCSVData):
    
        lines = ('signal',)
    
        params = (('signal', 16),)
    
        
    data = GenericCSV_PE(
        dataname='output.csv',
    
        fromdate=datetime.datetime(2018, 4, 25),
        todate=datetime.datetime(2018, 11, 12),
    
        nullvalue=0.0,
    
        dtformat=('%Y-%m-%d'),
    
        datetime=0,
        close=5,
        signal = 16,
    )
    
    
    cerebro.adddata(data)
    
    cerebro.broker.setcash(startcash)
    
    strategies = cerebro.run()
    firstStrat = strategies[0]
    
    cerebro.run()
    
    cerebro.plot(style = 'line')
    

    Thank you


  • administrators

    @valentin said in Need help to get started with pre-made signal:

    strategies = cerebro.run()
    firstStrat = strategies[0]
    
    cerebro.run()
    

    Why do you run twice? If the entire indication for you is: "the plot is wrong" (which we don't know because you haven't even shown that) ...

    • What about a data sample?
    • What about logging something of the things that happen?


  • @backtrader
    thanks a lot for your answer. I don't know why I had this extra line in there. I got rid of it now but still the strategy is not being executed as intended. It is supposed to buy on 1 and sell on 0 of the 'signal' value. Currently it looks like this 0_1544777145556_Screen Shot 2018-12-14 at 09.39.29.png

    this is how the data looks like

    date         ...   price(USD)    ...   signal
    2018-04-25   ...   9701.03       ...    0
    ...
    2018-04-28   ...   8939.27       ...    1
    ...
    

  • administrators

    @valentin said in Need help to get started with pre-made signal:

    close=5,
    

    It would seem like if you fail to provide anything but the close (we don't know it because your data sample is not a data sample)


  • administrators

    See also here:https://community.backtrader.com/topic/1507/order-execution-price-nan-single-column-pandas-data-input

    You seem to be the also a trader who is new to trading and you face the same issues, which one can quickly summarize as: "You cannot trade a price you have seen"



  • Thank you for your comments. Following the advice from the links I now provide a dataset which also includes OHLC. Still, the strategy of buying on 1 and selling on 0 of the signal column is not being executed. Any other ideas?

    This is the code:

    import datetime
    import backtrader as bt
    import backtrader.feeds as btfeeds
    from backtrader.feeds import GenericCSVData
    
    class firstStrategy(bt.Strategy):
        
        def next(self):
            if not self.position:
                if self.data.signal > 0:
                    self.buy(size=100)
            else:
                if self.data.signal < 1:
                    self.sell(size=100)
                    
    startcash = 10000
    
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(firstStrategy)
    
    
    class GenericCSV_PE(GenericCSVData):
    
        lines = ('signal',)
    
        params = (('signal', 1),)
    
        
    data = GenericCSV_PE(
        dataname='price_and_signals.csv',
    
        fromdate=datetime.datetime(2018, 4, 25),
        todate=datetime.datetime(2018, 8, 14),
    
        nullvalue=0.0,
    
        dtformat=('%Y-%m-%d'),
    
        datetime=0,
        high=3,
        low=4,
        open=5,
        close=2,
        openinterest=-1,
    )
    
    cerebro.adddata(data)
    
    cerebro.broker.setcash(startcash)
    
    cerebro.run()
    
    cerebro.plot(style = 'line')
    

    this is the data:
    0_1545065753637_Screenshot 2018-12-17 at 17.55.37.png

    and the result looks like this:
    0_1545065798456_Screenshot 2018-12-17 at 17.56.22.png



  • @backtrader I just tried the code without the "size = 100" and simply used

    self.buy()
    

    and

    self.sell()
    

    Now it works:
    0_1545228995951_Screenshot 2018-12-19 at 15.12.30.png

    Why does it not work when I use (size =100)?
    and:
    Unfortunately I can't adjust the position size. I assume it uses the default of 1.
    When I try to use the fixed size, it does not work (with not working I mean as explained above that the strategy is not executed. Hence the output looks as in the picture from my last comment):

    cerebro.addsizer(bt.sizers.FixedSize, stake=10)
    


  • Your prices are around 6000-10000, your cash is 10000. How can you expect to buy 100 of shares in this case? Only 1 fits.



  • @ab_trader wow.
    haha okay, well I think there is a long way head of me. Thank you so much for taking your time to answer


  • administrators

    @ab_trader said in Need help to get started with pre-made signal:

    Your prices are around 6000-10000, your cash is 10000. How can you expect to buy 100 of shares in this case? Only 1 fits.

    Top. I had this in mind and was completely taken off course by the chart.

    0_1545309427332_c893533a-bfab-47bc-a31b-1c5ab8ebd40f-image.png

    Because the markers and the data are located very close to each other and where the first operations would have taken place, which also raised the question: how were the buy and sell issued on the same day? (the code didn't allow that)


 

});