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

cash not used entirely



  • Hello,
    I'm trying to do a basic project, with an all-in sizer. For some reason, there is always left-over cash which is way larger than the stock price. How can I set the trader to use all the cash up to its limits?

    Here's the code:

    import backtrader as bt
    import IlanData
    
    #####################################################################################
    class overIndicator(bt.Indicator):
        lines = ('over',)
    
        def __init__(self):
            self.l.over = self.data > 170
    
    #####################################################################################
    class underIndicator(bt.Indicator):
        lines = ('under',)
    
        def __init__(self):
            self.l.under = self.data < 150
    
    #####################################################################################
    # margin indicator
    
    class MarginIndicator(bt.Indicator):
        lines = ('margin',)
    
        def __init__(self):
            underInd = underIndicator()
            overInd = overIndicator()
            self.l.margin = overInd - underInd
    
    
    #####################################################################################
    # margin strategy
    
    class MarginStrategy(bt.SignalStrategy):
        def __init__(self):
            myind = MarginIndicator()
            # bt.LinePlotterIndicator(myind.over150, name='over 150', subplot=True)
            self.signal_add(bt.SIGNAL_LONG, myind)
    
    #####################################################################################
    
    data = IlanData.getData()
    cerebro = bt.Cerebro()
    cerebro.adddata(data)
    cerebro.broker.setcash(10000)
    cerebro.addstrategy(MarginStrategy)
    cerebro.broker.setcommission(commission = 0.0)
    cerebro.addsizer(bt.sizers.AllInSizer)
    cerebro.run()
    cerebro.plot()
    

    Here's the output. Note that after buying all-in, I'm left with almost 400 USD, while the cost of buying is 170.

    0_1544721079870_9aaf916b-d907-4bdf-aea2-61665faaa7a3-image.png

    Thanks!


  • administrators

    Since you seem to be using TSLA your code does this with a fresh download of TSLA data

    0_1544966859539_a00d94aa-8450-43a5-8559-a4fc31bc9dc3-image.png

    The best theory: the code you posted is not the code which was executed. A hint: your volume doesn't follow the same colour variation pattern.


  • administrators

    In any case, the alternative is that your data contains huge differences from the open to the close (which could also be the explanation for the colour of the volume bars)