kernc / kernc/backtesting.py

trading is not work with tulipy

Open
#889 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

### Expected Behavior
trading ((buy and sell)

### Actual Behavior
not trading

### Steps to Reproduce

1. I success plot example

```python

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](https://user-images.githubusercontent.com/90539110/215303710-aff752fe-a5be-41ba-968c-a8758b470976.png)

2. so I try same with using import tulipy
```python
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.

3. So I find some solution.
```python
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](https://user-images.githubusercontent.com/90539110/215303726-fe525d8d-9030-4c12-98c0-652dbe5b4909.png)

I don't know how to fix it!!!!!
### Additional info

- Backtesting version: 0.?.?
- `bokeh.__version__`:
- OS:

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported SmaCross examples with backtesting.py and tulipy, focusing on Strategy.I and the traceback from backtesting.py line 1139. Compare the output shape from ti.sma with the data length and then inspect crossover behavior after the padding workaround. Done means the integration runs and the example produces the expected trades, with the Python and package versions recorded.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
fintech-quant
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.