Skip to content

Add indicators #74

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

Closed
getamu opened this issue May 22, 2020 · 5 comments
Closed

Add indicators #74

getamu opened this issue May 22, 2020 · 5 comments
Labels
invalid This is not a (valid) bug report

Comments

@getamu
Copy link

getamu commented May 22, 2020

Expected Behavior

I want to add indicators apart from SMA. I tried changing the init.py and added my function there but was unable to import it. I think I can import that function from a different file, make a strategy based on those indicators and backseat. I was just wondering if it was possible to add inside your files.

Actual Behavior

Steps to Reproduce

Additional info

  • Backtesting version:
@kernc
Copy link
Owner

kernc commented May 22, 2020

... or you could put the indicators wherever you're calling backtesting from?

from backtesting import Strategy, Backtest

...  # put indicators here?

bt = Backtest(...)

Or, if you want, Python by default looks for modules in the current working directory so you could use:

# my_indicator_lib.py:

... # indicators here

And in Python just:

import my_indicator_lib

HTH.

@getamu
Copy link
Author

getamu commented May 22, 2020

Also, how are we supposed to use the stop loss in self.buy()? I have tried:
self.buy(sl=0.01*self.data.Close) meaning the stop loss when buying is 1% below the closing price. But the statistics are not different at all!

@kernc
Copy link
Owner

kernc commented May 22, 2020

Exactly. Except: .99 * self.data.Close is the price that is 1% below the current closing price. 😉

@getamu
Copy link
Author

getamu commented May 23, 2020

Thank you for your reply.
It was stupid of me. However, when I am trying to print the stats based on GOOGL data from 2009 to 2013, the maximum drawdown is shown to be -20%, which happened in 2010. However, based on the strategy, the SMA cross shouldn't allow the trading account to have any position opened. Instead, it should have opened a short position and started to make money.

from backtesting import Backtest, Strategy, Position
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG


class SmaCross(Strategy):
    def init(self):
        Close = self.data.Close
        self.ma1 = self.I(SMA, Close, 1)
        self.ma2 = self.I(SMA, Close, 50)

    def next(self):
        if crossover(self.ma1, self.ma2):
            self.buy(sl=.99 * self.data.Close)
        elif crossover(self.ma2, self.ma1):
            self.sell(sl=1.01 * self.data.Close)


G = GOOG['2009-03-01':]
bt = Backtest(G, SmaCross,
              cash=10000, commission=.002)
print(bt.run())

Also, it would be great if I can see detailed report on every positions entered? [Maybe good to provide an example.]

Thanks again

@kernc
Copy link
Owner

kernc commented Jul 15, 2020

Trade-entering logic was overhauled in #47 (released in 0.2.0), rendering above issue hopefully rather obsolete.

Additionally, the new way to access individual trades data is:

stats = bt.run()

stats._trades        # trade data

@kernc kernc closed this as completed Jul 15, 2020
@kernc kernc added the invalid This is not a (valid) bug report label Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This is not a (valid) bug report
Projects
None yet
Development

No branches or pull requests

2 participants