Skip to content

trading is not work with tulipy #889

Open
@famsoo

Description

@famsoo

Expected Behavior

trading ((buy and sell)

Actual Behavior

not trading

Steps to Reproduce

  1. I success plot example
class SmaCross(Strategy):
    n1 = 10
    n2 = 20

    def init(self):
        
        
        close = self.data.Close
        self.sma1 = self.I(SMA, close, self.n1)
        self.sma2 = self.I(SMA, close, self.n2)

    def next(self):
        if crossover(self.sma1, self.sma2):
            self.buy()
        elif crossover(self.sma2, self.sma1):
            self.sell()


bt = Backtest(GOOG SmaCross,
              cash=100000000, commission=.002,
              exclusive_orders=True)

output = bt.run()
bt.plot()

image

  1. so I try same with using import tulipy
class SmaCross(Strategy):
    n1 = 7
    n2 = 21
    
    def init(self):
        
        self.sma7 = self.I(ti.sma,self.data.Close, self.n1)
        self.sma21 = self.I(ti.sma,self.data.Close, self.n2)
        
    def next(self):
        if crossover(self.sma7,self.sma21):
            self.buy()
        elif crossover(self.sma21, self.sma7):
            self.sell()
            
bt = Backtest(GOOG, SmaCross,
              cash=10000, commission=.002,
              exclusive_orders=True)

output = bt.run()
bt.plot()

but have problem

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtesting\backtesting.py", line 1139, in run
strategy.init()
File "", line 5, in init
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtesting\backtesting.py", line 143, in I
raise ValueError(
ValueError: Indicators must return (optionally a tuple of) numpy.arrays of same length as data (data shape: (2148,); indicator "sma(C,10)"shape: (2139,), returned value: [104.761 104.878 104.048 ... 793.88 795.714 797.551])

bt.plot()
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtesting\backtesting.py", line 1589, in plot
raise RuntimeError('First issue backtest.run() to obtain results.')
RuntimeError: First issue backtest.run() to obtain results.

  1. So I find some solution.
class SmaCross(Strategy):
    n1 = 10
    n2 = 20
    
    def init(self):
        def tulip_pad(func, *args, **kwargs):
            outputs = func(*args, **kwargs)
            if not isinstance(outputs, tuple):
                outputs = (outputs,)
            expect_size = len(args[0])
            padded = [np.r_[np.repeat(np.nan, expect_size - o.size), o] for o in outputs]
            return padded
        
        self.sma7 = self.I(tulip_pad, ti.sma,self.data.Close, self.n1)
        self.sma21 = self.I(tulip_pad, ti.sma,self.data.Close, self.n2)
        
    def next(self):
        if crossover(self.sma7,self.sma21):
            self.buy()
        elif crossover(self.sma21, self.sma7):
            self.sell()
            
bt = Backtest(GOOG, SmaCross,
              cash=10000, commission=.002,
              exclusive_orders=True)

output = bt.run()
bt.plot()

This code is work, but didn't trading

image

I don't know how to fix it!!!!!

Additional info

  • Backtesting version: 0.?.?
  • bokeh.__version__:
  • OS:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions