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

Unable to Figure out Leverage



  • import datetime
    
    import backtrader as bt
    
    
    class Strat(bt.Strategy):
    
        def __init__(self):
    
            self.ma_short = bt.indicators.MovingAverageSimple(period=6)
            self.ma_long = bt.indicators.MovingAverageSimple(period=24)
    
            self.buy_signal = bt.indicators.CrossUp(self.ma_short, self.ma_long)
            self.sell_signal = bt.indicators.CrossDown(self.ma_short, self.ma_long)
    
        def next(self):
    
            if self.position.size <= 0 and self.buy_signal[0]:
                self.order_target_percent(target=1)
            elif self.position.size >= 0 and self.sell_signal[0]:
                self.order_target_percent(target=-1)
    
    
    if __name__ == '__main__':
    
        cerebro = bt.Cerebro()
    
        datalist = (
            ('d0', data_0),
        )
    
        for name, df in datalist:
            data = bt.feeds.PandasData(dataname=any_data)
            cerebro.adddata(data, name=name)
    
        cerebro.broker.set_cash(100000)
    
        cerebro.broker.setcommission(commission=0.001, leverage=10)
    
        cerebro.addstrategy(Strat)
    
        cerebro.run(maxcpus=1)
    
        print(f'Final Portfolio Value: {cerebro.broker.getvalue():.2f}')
    
        cerebro.plot()
    
    

    The above code works well without leverage in setcommission. I assume that just adding leverage to the broker doesn't change anything as long as my trades remain within my bankroll. My orders should still commit 100% of my bankroll.? What seems to be happening is that it uses full leverage for all short trades and 100% of bankroll for all long trades. What am I missing here?

    Thanks in advance!



  • Can't figure how to edit OP. Should be:

        datalist = (
            ('d0', any_data),
        )
    
    data = bt.feeds.PandasData(dataname=df)
    


  • Seems to me there are several bugs in Backtrader. Bugs which show up when using leverage. One of which is related to target orders and another to plotting.



  • @Developing-Coder said in Unable to Figure out Leverage:

    Seems to me there are several bugs in Backtrader.

    You may want to elaborate it with some numbers and examples. Right now it is clear that your expectations are different from what bt is doing as a default. But it is hard to understand what is your problem/issues in particular.


  • administrators

    @Developing-Coder said in Unable to Figure out Leverage:

    Seems to me there are several bugs in Backtrader.

    1st post: NON-WORKING BROKEN CODE

    2nd post: CORRECTION WHICH STILL RENDERS THE CODE AS NON-WORKING

    3rd post: The bugs are in the platform. Proof: NONE.

    Sample data? No

    Logs? No



  • Code:

    import backtrader as bt
    import pandas as pd
    
    
    class Strat(bt.Strategy):
    
        def __init__(self):
    
            self.ma_short = bt.indicators.MovingAverageSimple(period=6)
            self.ma_long = bt.indicators.MovingAverageSimple(period=24)
    
            self.buy_signal = bt.indicators.CrossDown(self.ma_short, self.ma_long)
            self.sell_signal = bt.indicators.CrossUp(self.ma_short, self.ma_long)
    
        def log(self, txt):
    
            dt = self.datas[0].datetime.datetime(0)
            print(f'{dt.isoformat()}, {txt}')
    
        def notify_order(self, order):
    
            if order.status in [order.Submitted, order.Accepted]:
                return
    
            if order.status == order.Completed:
                if order.isbuy():
                    self.log(', '.join((
                        'BUY EXECUTED',
                        f'Price: {order.executed.price:.2f}',
                        f'Cost: {order.executed.value:.2f}',
                        f'Comm: {order.executed.comm:.2f}')))
    
                elif order.issell():
                    self.log(', '.join((
                        'SELL EXECUTED',
                        f'Price: {order.executed.price:.2f}',
                        f'Cost: {order.executed.value:.2f}',
                        f'Comm: {order.executed.comm:.2f}')))
    
            elif order.status == order.Canceled:
                self.log(f'Status: Canceled')
            elif order.status == order.Margin:
                self.log(f'Status: Margin')
            elif order.status == order.Rejected:
                self.log(f'Status: Rejected')
    
        def notify_trade(self, trade):
    
            if trade.isclosed:
                self.log(f'PNL: GROSS {trade.pnl:.2f}, NET {trade.pnlcomm:.2f}')
    
        def next(self):
    
            if self.position.size <= 0 and self.buy_signal[0]:
                self.order_target_percent(target=0.5)
            elif self.position.size >= 0 and self.sell_signal[0]:
                self.order_target_percent(target=-0.5)
    
    
    if __name__ == '__main__':
    
        cerebro = bt.Cerebro()
    
        df = pd.read_csv('A.csv', index_col='date', parse_dates=True)
        data = bt.feeds.PandasData(dataname=df)
        cerebro.adddata(data, name='A-2018')
    
        cerebro.broker.set_cash(100000)
    
        # Only change between two tests is leverage value here  ↓
        cerebro.broker.setcommission(commission=0.001, leverage=1)
    
        cerebro.addstrategy(Strat)
    
        cerebro.run(maxcpus=1)
    
        print(f'Final Portfolio Value: {cerebro.broker.getvalue():.2f}')
    
        cerebro.plot()
    

    Data:

    https://pastebin.com/gBhJV8B5
    

    First test:

    2018-01-02T17:10:00, BUY EXECUTED, Price: 67.58, Cost: 49941.62, Comm: 49.94
    2018-01-03T14:35:00, SELL EXECUTED, Price: 67.72, Cost: -35.74, Comm: 100.02
    2018-01-03T14:35:00, PNL: GROSS 103.46, NET 3.47
    2018-01-03T17:25:00, BUY EXECUTED, Price: 68.78, Cost: -455.76, Comm: 100.28
    2018-01-03T17:25:00, PNL: GROSS -782.28, NET -883.02
    2018-01-03T17:40:00, SELL EXECUTED, Price: 69.00, Cost: -89.40, Comm: 99.29
    2018-01-03T17:40:00, PNL: GROSS 158.40, NET 59.20
    2018-01-04T16:20:00, BUY EXECUTED, Price: 69.46, Cost: -224.94, Comm: 99.33
    2018-01-04T16:20:00, PNL: GROSS -330.74, NET -430.29
    2018-01-04T17:15:00, SELL EXECUTED, Price: 69.60, Cost: 39.66, Comm: 98.83
    2018-01-04T17:15:00, PNL: GROSS 99.54, NET 0.67
    2018-01-04T17:45:00, BUY EXECUTED, Price: 69.39, Cost: 59.28, Comm: 98.60
    2018-01-04T17:45:00, PNL: GROSS 148.89, NET 50.35
    2018-01-05T14:40:00, SELL EXECUTED, Price: 69.08, Cost: 220.72, Comm: 98.37
    2018-01-05T14:40:00, PNL: GROSS -220.72, NET -319.31
    2018-01-05T17:30:00, BUY EXECUTED, Price: 69.59, Cost: -263.19, Comm: 98.47
    2018-01-05T17:30:00, PNL: GROSS -363.12, NET -461.85
    2018-01-05T19:05:00, SELL EXECUTED, Price: 69.72, Cost: -91.39, Comm: 98.03
    2018-01-05T19:05:00, PNL: GROSS 91.39, NET -6.54
    2018-01-05T20:45:00, BUY EXECUTED, Price: 69.86, Cost: -41.30, Comm: 98.08
    2018-01-05T20:45:00, PNL: GROSS -98.42, NET -196.54
    2018-01-08T15:30:00, SELL EXECUTED, Price: 69.94, Cost: 87.29, Comm: 97.91
    2018-01-08T15:30:00, PNL: GROSS 52.58, NET -45.42
    2018-01-08T16:20:00, BUY EXECUTED, Price: 69.85, Cost: 10.43, Comm: 97.72
    2018-01-08T16:20:00, PNL: GROSS 59.42, NET -38.29
    2018-01-08T16:25:00, SELL EXECUTED, Price: 69.85, Cost: 69.85, Comm: 97.72
    2018-01-08T16:25:00, PNL: GROSS 0.00, NET -97.79
    2018-01-08T17:20:00, BUY EXECUTED, Price: 69.87, Cost: -55.89, Comm: 97.61
    2018-01-08T17:20:00, PNL: GROSS -13.98, NET -111.64
    2018-01-08T17:30:00, SELL EXECUTED, Price: 69.85, Cost: 83.81, Comm: 97.44
    2018-01-08T17:30:00, PNL: GROSS -13.96, NET -111.48
    2018-01-08T17:40:00, BUY EXECUTED, Price: 69.84, Cost: -6.97, Comm: 97.36
    2018-01-08T17:40:00, PNL: GROSS 6.97, NET -90.39
    2018-01-08T17:50:00, SELL EXECUTED, Price: 70.01, Cost: 21.53, Comm: 97.45
    2018-01-08T17:50:00, PNL: GROSS 118.49, NET 21.01
    2018-01-08T20:15:00, BUY EXECUTED, Price: 70.16, Cost: -106.23, Comm: 97.31
    2018-01-08T20:15:00, PNL: GROSS -104.25, NET -201.67
    2018-01-09T14:40:00, SELL EXECUTED, Price: 70.55, Cost: -128.78, Comm: 97.50
    2018-01-09T14:40:00, PNL: GROSS 269.88, NET 172.51
    2018-01-09T20:15:00, BUY EXECUTED, Price: 72.03, Cost: -632.15, Comm: 97.75
    2018-01-09T20:15:00, PNL: GROSS -1024.65, NET -1123.03
    2018-01-10T16:15:00, SELL EXECUTED, Price: 70.98, Cost: 348.78, Comm: 95.04
    2018-01-10T16:15:00, PNL: GROSS -703.68, NET -799.08
    2018-01-10T17:15:00, BUY EXECUTED, Price: 70.69, Cost: 17.19, Comm: 95.22
    2018-01-10T17:15:00, PNL: GROSS 194.88, NET 99.68
    2018-01-10T19:45:00, SELL EXECUTED, Price: 70.77, Cost: 20.14, Comm: 95.46
    2018-01-10T19:45:00, PNL: GROSS 50.63, NET -44.86
    2018-01-11T15:35:00, BUY EXECUTED, Price: 70.67, Cost: 6.64, Comm: 95.33
    2018-01-11T15:35:00, PNL: GROSS 64.03, NET -31.30
    2018-01-11T17:20:00, SELL EXECUTED, Price: 70.48, Cost: 124.88, Comm: 95.15
    2018-01-11T17:20:00, PNL: GROSS -124.88, NET -220.15
    2018-01-11T19:00:00, BUY EXECUTED, Price: 70.58, Cost: -77.04, Comm: 95.14
    2018-01-11T19:00:00, PNL: GROSS -64.12, NET -159.34
    2018-01-11T20:20:00, SELL EXECUTED, Price: 70.77, Cost: -57.10, Comm: 95.19
    2018-01-11T20:20:00, PNL: GROSS 127.87, NET 32.74
    2018-01-12T17:50:00, BUY EXECUTED, Price: 71.64, Cost: -346.68, Comm: 95.35
    2018-01-12T17:50:00, PNL: GROSS -584.64, NET -680.34
    2018-01-12T19:20:00, SELL EXECUTED, Price: 71.69, Cost: 38.74, Comm: 94.42
    2018-01-12T19:20:00, PNL: GROSS 32.95, NET -61.50
    2018-01-12T19:55:00, BUY EXECUTED, Price: 71.61, Cost: -52.64, Comm: 94.24
    2018-01-12T19:55:00, PNL: GROSS 52.64, NET -41.65
    2018-01-12T20:25:00, SELL EXECUTED, Price: 71.75, Cost: -20.37, Comm: 94.35
    2018-01-12T20:25:00, PNL: GROSS 92.12, NET -2.21
    2018-01-15T15:40:00, BUY EXECUTED, Price: 71.73, Cost: -84.87, Comm: 94.18
    2018-01-15T15:40:00, PNL: GROSS 13.14, NET -81.13
    2018-01-16T14:35:00, SELL EXECUTED, Price: 72.18, Cost: -150.84, Comm: 94.56
    2018-01-16T14:35:00, PNL: GROSS 295.20, NET 200.80
    2018-01-16T15:45:00, BUY EXECUTED, Price: 71.62, Cost: 135.10, Comm: 94.18
    2018-01-16T15:45:00, PNL: GROSS 366.24, NET 272.19
    2018-01-16T17:30:00, SELL EXECUTED, Price: 71.61, Cost: 78.22, Comm: 94.60
    2018-01-16T17:30:00, PNL: GROSS -6.61, NET -101.29
    2018-01-16T17:40:00, BUY EXECUTED, Price: 71.65, Cost: -45.25, Comm: 94.51
    2018-01-16T17:40:00, PNL: GROSS -26.40, NET -120.95
    2018-01-17T14:40:00, SELL EXECUTED, Price: 72.05, Cost: -119.50, Comm: 94.82
    2018-01-17T14:40:00, PNL: GROSS 263.60, NET 168.90
    2018-01-17T16:10:00, BUY EXECUTED, Price: 71.74, Cost: 11.55, Comm: 94.48
    2018-01-17T16:10:00, PNL: GROSS 203.67, NET 109.20
    2018-01-17T16:40:00, SELL EXECUTED, Price: 71.99, Cost: -21.02, Comm: 94.88
    2018-01-17T16:40:00, PNL: GROSS 165.00, NET 70.14
    2018-01-17T16:55:00, BUY EXECUTED, Price: 71.91, Cost: 19.27, Comm: 94.71
    2018-01-17T16:55:00, PNL: GROSS 52.64, NET -42.05
    2018-01-17T17:55:00, SELL EXECUTED, Price: 71.95, Cost: 45.59, Comm: 94.76
    2018-01-17T17:55:00, PNL: GROSS 26.36, NET -68.44
    2018-01-17T18:40:00, BUY EXECUTED, Price: 71.90, Cost: -32.90, Comm: 94.62
    2018-01-17T18:40:00, PNL: GROSS 32.90, NET -61.75
    2018-01-17T19:00:00, SELL EXECUTED, Price: 71.96, Cost: 32.48, Comm: 94.63
    2018-01-17T19:00:00, PNL: GROSS 39.48, NET -55.18
    2018-01-17T21:00:00, BUY EXECUTED, Price: 72.03, Cost: -94.79, Comm: 94.51
    2018-01-17T21:00:00, PNL: GROSS -49.28, NET -143.88
    2018-01-18T15:30:00, SELL EXECUTED, Price: 72.07, Cost: 49.14, Comm: 94.34
    2018-01-18T15:30:00, PNL: GROSS 22.92, NET -71.46
    2018-01-18T16:00:00, BUY EXECUTED, Price: 71.91, Cost: 39.18, Comm: 94.20
    2018-01-18T16:00:00, PNL: GROSS 104.64, NET 10.48
    2018-01-18T17:30:00, SELL EXECUTED, Price: 72.03, Cost: -9.96, Comm: 94.44
    2018-01-18T17:30:00, PNL: GROSS 82.00, NET -12.43
    2018-01-18T20:35:00, BUY EXECUTED, Price: 72.13, Cost: -82.03, Comm: 94.35
    2018-01-18T20:35:00, PNL: GROSS -62.22, NET -156.65
    2018-01-19T14:45:00, SELL EXECUTED, Price: 73.09, Cost: -261.43, Comm: 95.09
    2018-01-19T14:45:00, PNL: GROSS 626.88, NET 532.05
    2018-01-19T16:30:00, BUY EXECUTED, Price: 72.91, Cost: 29.18, Comm: 94.64
    2018-01-19T16:30:00, PNL: GROSS 116.64, NET 22.03
    2018-01-19T17:45:00, SELL EXECUTED, Price: 72.92, Cost: 66.42, Comm: 94.72
    2018-01-19T17:45:00, PNL: GROSS 6.50, NET -88.29
    2018-01-19T18:10:00, BUY EXECUTED, Price: 72.86, Cost: -42.18, Comm: 94.57
    2018-01-19T18:10:00, PNL: GROSS 42.18, NET -52.42
    2018-01-19T19:45:00, SELL EXECUTED, Price: 72.88, Cost: 56.66, Comm: 94.53
    2018-01-19T19:45:00, PNL: GROSS 16.22, NET -78.36
    2018-01-22T14:50:00, BUY EXECUTED, Price: 72.89, Cost: 3.24, Comm: 94.46
    2018-01-22T14:50:00, PNL: GROSS -3.24, NET -97.70
    2018-01-22T15:45:00, SELL EXECUTED, Price: 73.11, Cost: 0.42, Comm: 94.60
    2018-01-22T15:45:00, PNL: GROSS 145.80, NET 51.20
    2018-01-22T16:25:00, BUY EXECUTED, Price: 72.95, Cost: -27.17, Comm: 94.33
    2018-01-22T16:25:00, PNL: GROSS 100.13, NET 5.77
    2018-01-22T17:25:00, SELL EXECUTED, Price: 73.04, Cost: 18.04, Comm: 94.44
    2018-01-22T17:25:00, PNL: GROSS 55.00, NET -39.46
    2018-01-23T16:20:00, BUY EXECUTED, Price: 73.58, Cost: -169.42, Comm: 94.54
    2018-01-23T16:20:00, PNL: GROSS -345.61, NET -440.32
    2018-01-23T19:05:00, SELL EXECUTED, Price: 73.38, Cost: 124.61, Comm: 93.78
    2018-01-23T19:05:00, PNL: GROSS -124.61, NET -218.51
    2018-01-23T19:30:00, BUY EXECUTED, Price: 73.36, Cost: -82.95, Comm: 93.69
    2018-01-23T19:30:00, PNL: GROSS 9.59, NET -84.19
    2018-01-23T20:55:00, SELL EXECUTED, Price: 73.44, Cost: 22.40, Comm: 93.64
    2018-01-23T20:55:00, PNL: GROSS 51.04, NET -42.62
    2018-01-24T14:50:00, BUY EXECUTED, Price: 73.19, Cost: 57.14, Comm: 93.46
    2018-01-24T14:50:00, PNL: GROSS 162.43, NET 69.03
    2018-01-24T15:10:00, SELL EXECUTED, Price: 73.77, Cost: -149.89, Comm: 94.20
    2018-01-24T15:10:00, PNL: GROSS 371.20, NET 277.15
    2018-01-24T16:40:00, BUY EXECUTED, Price: 73.48, Cost: 35.71, Comm: 93.83
    2018-01-24T16:40:00, PNL: GROSS 184.73, NET 90.93
    2018-01-24T18:10:00, SELL EXECUTED, Price: 73.57, Cost: 15.97, Comm: 94.10
    2018-01-24T18:10:00, PNL: GROSS 57.60, NET -36.51
    2018-01-24T18:25:00, BUY EXECUTED, Price: 73.31, Cost: 53.79, Comm: 93.91
    2018-01-24T18:25:00, PNL: GROSS 166.14, NET 72.28
    2018-01-24T18:40:00, SELL EXECUTED, Price: 73.81, Cost: -96.38, Comm: 94.54
    2018-01-24T18:40:00, PNL: GROSS 317.79, NET 223.34
    2018-01-24T20:30:00, BUY EXECUTED, Price: 73.67, Cost: -12.60, Comm: 94.22
    2018-01-24T20:30:00, PNL: GROSS 86.27, NET -7.97
    2018-01-25T14:40:00, SELL EXECUTED, Price: 74.42, Cost: -182.32, Comm: 94.96
    2018-01-25T14:40:00, PNL: GROSS 480.00, NET 385.22
    2018-01-25T17:00:00, BUY EXECUTED, Price: 73.89, Cost: 106.26, Comm: 94.43
    2018-01-25T17:00:00, PNL: GROSS 337.08, NET 242.75
    2018-01-25T18:30:00, SELL EXECUTED, Price: 74.08, Cost: -47.90, Comm: 95.04
    2018-01-25T18:30:00, PNL: GROSS 121.98, NET 26.98
    2018-01-25T19:40:00, BUY EXECUTED, Price: 73.93, Cost: -22.22, Comm: 94.85
    2018-01-25T19:40:00, PNL: GROSS 96.15, NET 1.28
    2018-01-26T14:40:00, SELL EXECUTED, Price: 74.16, Cost: -73.50, Comm: 95.15
    2018-01-26T14:40:00, PNL: GROSS 147.66, NET 52.59
    2018-01-26T17:20:00, BUY EXECUTED, Price: 74.20, Cost: -48.56, Comm: 95.05
    2018-01-26T17:20:00, PNL: GROSS -25.64, NET -120.74
    2018-01-26T17:45:00, SELL EXECUTED, Price: 74.47, Cost: -23.86, Comm: 95.17
    2018-01-26T17:45:00, PNL: GROSS 172.80, NET 77.65
    2018-01-26T19:25:00, BUY EXECUTED, Price: 74.38, Cost: -57.42, Comm: 94.91
    2018-01-26T19:25:00, PNL: GROSS 57.42, NET -37.55
    2018-01-26T20:00:00, SELL EXECUTED, Price: 74.46, Cost: 23.42, Comm: 94.94
    2018-01-26T20:00:00, PNL: GROSS 51.04, NET -43.92
    2018-01-29T16:00:00, BUY EXECUTED, Price: 74.64, Cost: -109.26, Comm: 94.87
    2018-01-29T16:00:00, PNL: GROSS -114.66, NET -209.64
    2018-01-29T17:35:00, SELL EXECUTED, Price: 74.63, Cost: 6.34, Comm: 94.63
    2018-01-29T17:35:00, PNL: GROSS -6.34, NET -100.98
    2018-01-29T18:40:00, BUY EXECUTED, Price: 74.44, Cost: 28.42, Comm: 94.54
    2018-01-29T18:40:00, PNL: GROSS 120.46, NET 25.95
    2018-01-29T19:50:00, SELL EXECUTED, Price: 74.47, Cost: 58.56, Comm: 94.65
    2018-01-29T19:50:00, PNL: GROSS 15.90, NET -78.80
    2018-01-29T20:45:00, BUY EXECUTED, Price: 74.41, Cost: -38.10, Comm: 94.49
    2018-01-29T20:45:00, PNL: GROSS 38.10, NET -56.43
    2018-01-30T18:20:00, SELL EXECUTED, Price: 73.32, Cost: 395.70, Comm: 93.41
    2018-01-30T18:20:00, PNL: GROSS -688.98, NET -782.78
    2018-01-30T19:25:00, BUY EXECUTED, Price: 73.12, Cost: 18.44, Comm: 93.59
    2018-01-30T19:25:00, PNL: GROSS 127.80, NET 34.22
    2018-01-30T20:55:00, SELL EXECUTED, Price: 73.17, Cost: 37.92, Comm: 93.74
    2018-01-30T20:55:00, PNL: GROSS 35.25, NET -58.52
    2018-01-31T16:30:00, BUY EXECUTED, Price: 73.73, Cost: -234.64, Comm: 93.78
    2018-01-31T16:30:00, PNL: GROSS -355.20, NET -449.22
    2018-01-31T18:35:00, SELL EXECUTED, Price: 73.71, Cost: 12.64, Comm: 93.17
    2018-01-31T18:35:00, PNL: GROSS -12.64, NET -105.82
    2018-01-31T19:25:00, BUY EXECUTED, Price: 73.53, Cost: -40.23, Comm: 93.02
    2018-01-31T19:25:00, PNL: GROSS 113.76, NET 20.70
    2018-02-01T14:30:00, SELL EXECUTED, Price: 72.94, Cost: 370.31, Comm: 92.35
    2018-02-01T14:30:00, PNL: GROSS -370.31, NET -463.02
    2018-02-01T14:35:00, BUY EXECUTED, Price: 73.12, Cost: 37.66, Comm: 92.50
    2018-02-01T14:35:00, PNL: GROSS -110.78, NET -203.23
    2018-02-01T14:40:00, SELL EXECUTED, Price: 73.65, Cost: -187.66, Comm: 92.95
    2018-02-01T14:40:00, PNL: GROSS 334.96, NET 242.20
    2018-02-01T14:50:00, BUY EXECUTED, Price: 73.07, Cost: -0.05, Comm: 92.43
    2018-02-01T14:50:00, PNL: GROSS 365.40, NET 272.97
    2018-02-01T17:05:00, SELL EXECUTED, Price: 72.89, Cost: 38.23, Comm: 92.65
    2018-02-01T17:05:00, PNL: GROSS -111.12, NET -203.81
    2018-02-01T17:25:00, BUY EXECUTED, Price: 72.83, Cost: 31.49, Comm: 92.71
    2018-02-01T17:25:00, PNL: GROSS 41.34, NET -51.34
    2018-02-01T18:15:00, SELL EXECUTED, Price: 73.02, Cost: 25.01, Comm: 92.88
    2018-02-01T18:15:00, PNL: GROSS 121.03, NET 28.12
    2018-02-01T18:55:00, BUY EXECUTED, Price: 72.78, Cost: -3.65, Comm: 92.58
    2018-02-01T18:55:00, PNL: GROSS 149.22, NET 56.64
    2018-02-01T20:00:00, SELL EXECUTED, Price: 72.94, Cost: -25.79, Comm: 92.85
    2018-02-01T20:00:00, PNL: GROSS 98.74, NET 5.91
    2018-02-01T20:30:00, BUY EXECUTED, Price: 72.68, Cost: -20.00, Comm: 92.59
    2018-02-01T20:30:00, PNL: GROSS 165.36, NET 72.75
    Final Portfolio Value: 92978.90
    

    First
    Everything looks pretty much as I'd expect it to. Green arrow for first entry is not visible on main plot but that's minor.

    Second test:

    Changed leverage from 1 to 4.

    cerebro.broker.setcommission(commission=0.001, leverage=4)
    
    2018-01-02T17:10:00, BUY EXECUTED, Price: 67.58, Cost: 199766.48, Comm: 199.77
    2018-01-03T14:35:00, SELL EXECUTED, Price: 67.72, Cost: -600954.80, Comm: 1000.90
    2018-01-03T14:35:00, PNL: GROSS 413.84, NET 13.89
    2018-01-03T17:25:00, Status: Margin
    2018-01-04T16:20:00, Status: Margin
    2018-01-04T17:45:00, Status: Margin
    2018-01-05T17:30:00, Status: Margin
    2018-01-05T20:45:00, Status: Margin
    2018-01-08T16:20:00, Status: Margin
    2018-01-08T17:20:00, Status: Margin
    2018-01-08T17:40:00, Status: Margin
    2018-01-08T20:15:00, Status: Margin
    2018-01-09T20:15:00, Status: Margin
    2018-01-10T17:15:00, Status: Margin
    2018-01-11T15:35:00, Status: Margin
    2018-01-11T19:00:00, Status: Margin
    2018-01-12T17:50:00, Status: Margin
    2018-01-12T19:55:00, Status: Margin
    2018-01-15T15:40:00, Status: Margin
    2018-01-16T15:45:00, Status: Margin
    2018-01-16T17:40:00, Status: Margin
    2018-01-17T16:10:00, Status: Margin
    2018-01-17T16:55:00, Status: Margin
    2018-01-17T18:40:00, Status: Margin
    2018-01-17T21:00:00, Status: Margin
    2018-01-18T16:00:00, Status: Margin
    2018-01-18T20:35:00, Status: Margin
    2018-01-19T16:30:00, Status: Margin
    2018-01-19T18:10:00, Status: Margin
    2018-01-22T14:50:00, Status: Margin
    2018-01-22T16:25:00, Status: Margin
    2018-01-23T16:20:00, Status: Margin
    2018-01-23T19:30:00, Status: Margin
    2018-01-24T14:50:00, Status: Margin
    2018-01-24T16:40:00, Status: Margin
    2018-01-24T18:25:00, Status: Margin
    2018-01-24T20:30:00, Status: Margin
    2018-01-25T17:00:00, Status: Margin
    2018-01-25T19:40:00, Status: Margin
    2018-01-26T17:20:00, Status: Margin
    2018-01-26T19:25:00, Status: Margin
    2018-01-29T16:00:00, Status: Margin
    2018-01-29T18:40:00, Status: Margin
    2018-01-29T20:45:00, Status: Margin
    2018-01-30T19:25:00, Status: Margin
    2018-01-31T16:30:00, Status: Margin
    2018-01-31T19:25:00, Status: Margin
    2018-02-01T14:35:00, Status: Margin
    2018-02-01T14:50:00, Status: Margin
    2018-02-01T17:25:00, Status: Margin
    2018-02-01T18:55:00, Status: Margin
    2018-02-01T20:30:00, Status: Margin
    Final Portfolio Value: 38910.77
    

    Second
    From target order documentation:
    "percent -> percentage (from current portfolio) value of the asset in the current portfolio"
    I wouldn't have expected any changes in outcome after making this change because I haven't changed my orders. However, observed non-symmetrical default usage of leverage is especially confusing.

    p.s. Where is the option to edit posts? I can't seem to locate it...


  • administrators

    @Developing-Coder said in Unable to Figure out Leverage:

    Everything looks pretty much as I'd expect it to. Green arrow for first entry is not visible on main plot but that's minor.

    Ok. So you think there is some artificial intelligence at work that will do as you please. You may also read the docs and understand how you can plot and get the arrow plotted. But hey, it is much better to claim that there is a "bug" that shows up when using leverage.

    With regards to leverage ... what amazes me is that you apparently want to run a leverage strategy using the standard shorting of stocks that pumps money into your account.

    What is your expectation here? To go to the broker sell 100 stocks of A and get for free 4 times the money they are worth? That would be fantastic. I pay interest on 100 stocks but may money on the value of 4 x 100 stocks.

    You could start by stating what type of asset that is, how it behaves ...

    One important thing about algotrading is knowing how to trade. Not so long ago you didn't even know that shorting pumps money in your account (when shorting stocks, the same doesn't happen when selling a future which is also going short) => https://community.backtrader.com/topic/1887/spikes-in-cash

    @Developing-Coder said in Unable to Figure out Leverage:

    2018-01-02T17:10:00, BUY EXECUTED, Price: 67.58, Cost: 199766.48, Comm: 199.77
    2018-01-03T14:35:00, SELL EXECUTED, Price: 67.72, Cost: -600954.80, Comm: 1000.90
    2018-01-03T14:35:00, PNL: GROSS 413.84, NET 13.89
    2018-01-03T17:25:00, Status: Margin
    

    What price is going to be bought or sold? What size? Not the results ... but the intent, plus how much cash is there, the account value, ...



  • You could start by stating what type of asset that is, how it behaves ...

    I am working with BitMEX perpetual contracts.

    What is your expectation here?

    I'm assuming that funding fees are balancing themselves out over the long run and hence should not be included in the calculation.

    I'm also assuming that if I use 4x leverage then I can use up to 4x the funds I have for both longs and shorts.

    I'm also assuming that by just allowing leverage, my bet sizes will not change.


  • administrators

    @Developing-Coder said in Unable to Figure out Leverage:

    I am working with BitMEX perpetual contracts.

    You are using futures. Configure the platform for FUTURES.

    @Developing-Coder said in Unable to Figure out Leverage:

    I'm assuming that funding fees are balancing themselves out over the long run and hence should not be included in the calculation.
    I'm also assuming that if I use 4x leverage then I can use up to 4x the funds I have for both longs and shorts.
    I'm also assuming that by just allowing leverage, my bet sizes will not change.

    Stop assuming and start by configuring things right, before claiming a BUG is there.

    If you have futures and you have the link above (and there links close to it about "Futures")

    • Margin? Where is that in your code? It's one of the vital things when operating with futures
    • Multiplier? Futures have NO leverage, they have a MULTIPLIER. No, it's not the same thing.

    Read the futures specification on Bitmex and find out about those details.

    Read how to work with FUTURES.

    You will now clearly see that the bug is in your code, which isn't properly configuring the platform for the trading scenario you are simulating.


  • administrators

    By the way ... in all cases it's a very bad idea to go All-In (no, All-In is not the new black)

    If you use futures and try to go All-In, the slightest move against your entry price will have your broker closing your position until you meet the margin again (read the documentation from your broker), which means you can no longer 100% recover.

    Use proper position sizing.


Log in to reply
 

});