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

Incorrect plotting, but signal is correct



  • Hi,
    I have following code :

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    from datetime import datetime # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    
    # Import the backtrader platform
    import backtrader as bt
    import backtrader.indicators as btind
    
    
    
    # Create a Strategy
    class MyStrategy(bt.Strategy):
        params = (('fast_period', 10),
                  ('slow_period', 20))
    
        def __init__(self):
    
            self.fast_sma = btind.MovingAverageSimple(self.data.close, period=self.params.fast_period)
            self.slow_sma = btind.MovingAverageSimple(self.data.close, period=self.params.slow_period)
    
            self.signal = btind.CrossOver(self.fast_sma, self.slow_sma)
    
    
        def next(self):
            if self.position.size:
                if self.signal < 0:
                    self.sell()
            elif self.signal > 0:
                self.buy()
    
    
    
    
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro(optreturn=False)
    
        # Add a strategy
        # cerebro.optstrategy(MyStrategy,fast_period=range(10, 14), slow_period=range(20, 22))
        cerebro.addstrategy(MyStrategy)
        modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, 'sampledata.csv')
    
        # Create a Data Feed
        data = bt.feeds.GenericCSVData(
            dataname=datapath,
            datetime=0,
            open=1,
            high=2,
            low=3,
            close=4,
            volume=5,
            openinterest=-1,
            fromdate=datetime(2017,9,9),
            todate=datetime(2017,9,12),
            separator=',',
            dtformat=('%m/%d/%Y %H:%M')
    
        )
    
        # Add the Data Feed to Cerebro
        cerebro.adddata(data)
    
        # Set our desired cash start
        cerebro.broker.setcash(1000000.0)
    
        # Add a FixedSize sizer according to the stake
        cerebro.addsizer(bt.sizers.FixedSize, stake=1)
    
        # Set the commission
        cerebro.broker.setcommission(commission=0.0)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        result = cerebro.run()
    
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Plot the result
        cerebro.plot(style='candle',barup='green',bardown='red',fillalpha=0.1)
    
    

    Following is the sample data...

    Timestamp,Open,High,Low,Close,Volume,Volume (Currency),Weighted Price
    9/9/2017 0:00,4327.41,4336.42,4263.07,4286.29,347.93,1496712.8,4301.76
    9/9/2017 1:00,4281.69,4322.5,4200,4230.43,749.14,3178547.34,4242.95
    9/9/2017 2:00,4216.02,4252.12,4164,4244.94,692.24,2912213.17,4206.93
    9/9/2017 3:00,4252.12,4293.48,4242.1,4266.99,295.59,1263220,4273.54
    9/9/2017 4:00,4267,4279.17,4208.93,4265.68,158.98,673508.9,4236.42
    9/9/2017 5:00,4270.73,4377,4260.61,4376.81,521,2253379.85,4325.12
    9/9/2017 6:00,4376.84,4376.84,4320.87,4343.87,327.49,1423095.32,4345.46
    9/9/2017 7:00,4343.8,4379.78,4301.46,4328.02,462.01,2005931.49,4341.77
    9/9/2017 8:00,4334.6,4347.32,4293.48,4302,204.54,882141.74,4312.89
    9/9/2017 9:00,4302,4335.82,4272.4,4323.36,358,1539389.96,4299.95
    9/9/2017 10:00,4323.34,4336.4,4290.16,4300.31,225.53,974019.88,4318.75
    9/9/2017 11:00,4310,4364.99,4309.99,4356.01,377.62,1638429.91,4338.79
    9/9/2017 12:00,4359.48,4378.71,4327.15,4347.5,341.83,1486091.65,4347.47
    9/9/2017 13:00,4347.5,4353.06,4264.7,4285,707.4,3041771.33,4299.96
    9/9/2017 14:00,4284.99,4300,4253.77,4282.12,508.52,2173782.36,4274.75
    9/9/2017 15:00,4289.79,4300,4272.02,4285,221.07,948237.38,4289.33
    9/9/2017 16:00,4285,4310,4272.65,4291.36,286.97,1231659.2,4291.97
    9/9/2017 17:00,4291.35,4291.36,4215.77,4254.14,606.61,2573693.38,4242.75
    9/9/2017 18:00,4254.14,4258.98,4188,4204.53,645.81,2720696.69,4212.83
    9/9/2017 19:00,4190.61,4308.91,4176,4276.3,832.65,3532696.93,4242.69
    9/9/2017 20:00,4276.38,4303.98,4270.01,4270.01,395.76,1696164.38,4285.82
    9/9/2017 21:00,4270.07,4288.21,4259.08,4275.75,119.38,510400.62,4275.38
    9/9/2017 22:00,4275.06,4330,4259.33,4329.29,452.19,1945277.01,4301.94
    9/9/2017 23:00,4329.3,4334.38,4299.17,4308.72,136.13,588273.34,4321.31
    9/10/2017 0:00,4322.44,4322.44,4270,4285.74,217.96,933401.46,4282.4
    9/10/2017 1:00,4290.46,4291.27,4261.87,4280,107.34,459244.66,4278.58
    9/10/2017 2:00,4281.99,4282,4185.67,4188.46,715.98,3031956.86,4234.7
    9/10/2017 3:00,4190,4227.58,4186.81,4204,171.72,723012.1,4210.34
    9/10/2017 4:00,4192.51,4227,4158,4164.79,501.99,2102723.23,4188.76
    9/10/2017 5:00,4164.73,4164.84,4102.17,4142,1388.04,5734525,4131.38
    9/10/2017 6:00,4143,4146,4010,4023.01,1950.99,7955408.48,4077.63
    9/10/2017 7:00,4023,4123.5,4010,4096.01,1419.37,5791218.29,4080.14
    9/10/2017 8:00,4096.21,4144.1,4062.26,4100.73,1000.61,4103610.28,4101.12
    9/10/2017 9:00,4100.43,4119.98,4039.57,4072.14,810.11,3293755,4065.79
    9/10/2017 10:00,4053.55,4082.57,4039.46,4072.51,733.52,2979871.46,4062.41
    9/10/2017 11:00,4062.54,4123.5,4044.88,4121.52,502.12,2052660.61,4087.99
    9/10/2017 12:00,4118.41,4123.5,4082.57,4092,222.85,912113.44,4092.99
    9/10/2017 13:00,4104.22,4144.12,4092,4144.11,720.21,2968274.79,4121.41
    9/10/2017 14:00,4144.12,4164.84,4125,4162.72,590.92,2448181.17,4142.98
    9/10/2017 15:00,4164.82,4171.62,4124.52,4128.78,281.07,1166655.83,4150.81
    9/10/2017 16:00,4138.55,4206.59,4123.5,4206.59,542.94,2262688.69,4167.47
    9/10/2017 17:00,4206.58,4312.82,4195.17,4268.01,1085.47,4618947.39,4255.27
    9/10/2017 18:00,4282.26,4291.36,4241.01,4287.99,452.87,1932672.51,4267.62
    9/10/2017 19:00,4280.42,4295,4259.9,4293.55,287.4,1229093.37,4276.56
    9/10/2017 20:00,4293.55,4297.33,4233.99,4266.29,401.94,1711968.41,4259.27
    9/10/2017 21:00,4266.2,4266.2,4227.63,4238.99,179.98,763744.95,4243.49
    9/10/2017 22:00,4238.99,4290.02,4230,4250.15,204.67,873001.34,4265.39
    9/10/2017 23:00,4250.4,4269.97,4212.92,4226.22,234.13,991722.73,4235.69
    9/11/2017 0:00,4226.21,4242.58,4185.67,4201,305.84,1286094.41,4205.09
    9/11/2017 1:00,4204.53,4291.36,4204.26,4291.35,629.74,2677090.46,4251.09
    9/11/2017 2:00,4291.36,4300,4270.01,4290.4,198.87,852387.94,4286.09
    9/11/2017 3:00,4290.2,4353.49,4280.1,4315.95,602.32,2601258.8,4318.71
    9/11/2017 4:00,4316.3,4316.3,4151,4197.92,898.17,3796245.49,4226.67
    9/11/2017 5:00,4186,4206.51,4092.4,4144,701.24,2905795,4143.8
    9/11/2017 6:00,4140.07,4216.96,4115,4169.15,606.25,2529398.7,4172.18
    9/11/2017 7:00,4168.59,4206.45,4142,4160.01,551.65,2301208.01,4171.46
    9/11/2017 8:00,4153.77,4164.84,4105,4160,500.42,2071506.51,4139.5
    9/11/2017 9:00,4159.99,4206.59,4149.62,4169,524.01,2189954.79,4179.2
    9/11/2017 10:00,4169.44,4206.59,4156,4178,447.17,1868140.84,4177.68
    9/11/2017 11:00,4178,4196.04,4144.12,4178.49,381.69,1591024.62,4168.38
    9/11/2017 12:00,4178.01,4195,4162.17,4194.98,306.96,1283224.64,4180.37
    9/11/2017 13:00,4194.84,4291.36,4172.68,4246.95,1167.51,4948043.71,4238.1
    9/11/2017 14:00,4256,4270.01,4232.56,4244,554.9,2359608.07,4252.34
    9/11/2017 15:00,4244,4250,4212,4240,372.41,1576131.35,4232.3
    9/11/2017 16:00,4240,4267.22,4230.05,4261.82,569.82,2421744.29,4250
    9/11/2017 17:00,4261.82,4271.38,4208.77,4223.71,489.57,2079237.12,4247.09
    9/11/2017 18:00,4223.59,4229,4192.64,4222,332.8,1402907.95,4215.53
    9/11/2017 19:00,4222,4225,4170.99,4197.67,286.42,1201726.92,4195.66
    9/11/2017 20:00,4197.69,4204.19,4172.05,4188.36,196.51,822639.1,4186.16
    9/11/2017 21:00,4188.07,4202.06,4178.3,4192.17,216.83,908887.25,4191.77
    9/11/2017 22:00,4192.18,4227.4,4186.41,4219,235.83,992828.83,4209.98
    9/11/2017 23:00,4219.99,4220.01,4191,4207.31,114.65,481895.18,4203.24
    9/12/2017 0:00,4198.89,4246.97,4198.89,4226.32,323.56,1367621.69,4226.75
    9/12/2017 1:00,4239.36,4248.75,4216.43,4230,149.08,632129.94,4240.11
    9/12/2017 2:00,4230,4248.76,4212.67,4248,186.71,791152.95,4237.25
    9/12/2017 3:00,4239.48,4283.59,4239.02,4270.87,281.25,1198722.23,4262.13
    9/12/2017 4:00,4281.99,4306.4,4256.16,4276.31,404.98,1734825.45,4283.77
    9/12/2017 5:00,4285.57,4304,4271.19,4300.95,291.6,1251580.53,4292.05
    9/12/2017 6:00,4300.93,4356.02,4289.54,4352.09,482.83,2091157.67,4331.04
    9/12/2017 7:00,4355.62,4377.65,4317.06,4356.87,456.72,1988009.09,4352.83
    9/12/2017 8:00,4359.41,4361.67,4264.02,4307.55,923.26,3970194.13,4300.17
    9/12/2017 9:00,4294.48,4332,4291.37,4312.61,372.58,1606044.22,4310.56
    9/12/2017 10:00,4312.63,4351.82,4279.05,4350,715.92,3084509.43,4308.48
    9/12/2017 11:00,4350,4364,4334.38,4339.98,355.14,1544069.65,4347.83
    9/12/2017 12:00,4339.98,4355,4294.83,4299.98,453.83,1963247.42,4325.95
    9/12/2017 13:00,4299.98,4299.99,4240,4262.62,881.03,3760163.69,4267.93
    9/12/2017 14:00,4257,4291.36,4248.76,4274.34,702.19,2998308.25,4269.96
    9/12/2017 15:00,4270.97,4291,4236.01,4237.56,333.93,1424502.94,4265.82
    9/12/2017 16:00,4237.84,4265.28,4218.29,4250.69,516.51,2190441.65,4240.85
    9/12/2017 17:00,4262.98,4264.72,4185.67,4187,648.67,2740948.13,4225.48
    9/12/2017 18:00,4186.04,4190.31,4080,4115.07,2353.13,9701826.55,4122.94
    9/12/2017 19:00,4122.11,4186.88,4090.3,4164.85,979.54,4049551.41,4134.14
    9/12/2017 20:00,4164.88,4178,4123.5,4174.5,478.95,1989892.32,4154.73
    9/12/2017 21:00,4177.99,4204.99,4156.24,4200,373.65,1563858.84,4185.35
    9/12/2017 22:00,4199.99,4199.99,4173.1,4182.28,344.24,1439697.78,4182.3
    9/12/2017 23:00,4179,4181.2,4144.12,4172.56,285.13,1185583.83,4157.99
    

    I can see that correct CrossOver signal is being generated and strategy Buys, when I debug it.

    But when the plot is displayed why fast MA is not shown as Crossing Over Slow MA above the green buy arrow in the image ?

    0_1518715067827_graphimage.png

    Please advise, what I am doing wrong.
    PS: I am relatively new to platform.

    Thank You


  • administrators

    You are not telling the platform what your data is, i.e.: what timeframe and compression has. And it uses the default: 1-day.

    And the platform executes the order when the day changes.

    See: Community - FAQ



  • @backtrader

    Thank you. It's not very clear or maybe I missed reading introductory tutorials, that this parameter would be required.
    I have tried and have following queries :

    1. I have an hourly data, there is no timeframe provided for hourly data
    class TimeFrame(object):
        (Ticks, MicroSeconds, Seconds, Minutes,
         Days, Weeks, Months, Years, NoTimeFrame
    

    Why is that ?

    1. Are you suggesting to use TimeFrame.Minutes and compression = 60 ?
      If not, how can I process hourly data when each candle represents hourly data?

    I also tried to read about TimeFrames and compression and couldn't find an example that tries to use hourly data, please advise.

    Thank You


  • administrators

    @sunnyalgotrader said in Incorrect plotting, but signal is correct:

    Are you suggesting to use TimeFrame.Minutes and compression = 60 ?

    Indeed. Until Trump takes it away from us, mere mortals, 60 minutes will still be 1 hour.

    @sunnyalgotrader said in Incorrect plotting, but signal is correct:

    maybe I missed reading introductory tutorials

    The tutorials uses 1-Day data feeds, nothing to miss there.



  • @backtrader

    Hahaha trump and your humorous spirit rather than getting annoyed. Thanks for your answer. I had actually tried it. But wanted to be sure, as I can see I don't know few fundamentals of backtrader yet.

    Also, because the buy signal appeared below the next bar on plot I wanted to double confirm. I realize now is the right thing, as the buy would happen on the next bar.

    Any particular reason why TimeFrame.Hours is missing except due to the fact Hour can be easily represented as 60 minutes ?

    Thanks


  • administrators

    Probably the fact that you see people compressing to 1, 2, 5, 10, 15, 20, 30, 45, 60, 120, 180, 240 and 360 minutes (haven't seen anything else). But even if I have seen people talking about 1, 2 hours ... never have I seen anyone talking about 6 hours (360 minutes)

    Indeed nothing special


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.