Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trading is not work with tulipy #889

Open
famsoo opened this issue Jan 29, 2023 · 1 comment
Open

trading is not work with tulipy #889

famsoo opened this issue Jan 29, 2023 · 1 comment

Comments

@famsoo
Copy link

famsoo commented Jan 29, 2023

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:
@kernc
Copy link
Owner

kernc commented Jan 29, 2023

This code is work, but didn't trading

This makes no sense. I'm at loss as to what the issue could be. 😕 Is self.sma7, by chance, a list of one element?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants