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.
Thanks!
-
Since you seem to be using
TSLA
your code does this with a fresh download of TSLA dataThe 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.
-
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)