kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
945 stars 217 forks source link

STOCHASTIC Plotting doesn't work #31

Closed lleewwiiss closed 1 year ago

lleewwiiss commented 2 years ago

Describe the bug When using the key STOCHASTIC in the indicators dictionary an error is thrown.

To Reproduce Steps to reproduce the behaviour:

  1. Create a strategy and create STOCHK and STOCHD
        self.stoch_rsi = TA.STOCHRSI(self.data, rsi_period=6, stoch_period=27)
        self.stoch_rsi_k = TA.STOCH(self.data, period=3)
        self.stoch_rsi_d = TA.STOCHD(self.data, period=3, stoch_period=27)
  2. Create an indicator dictionary something like:
        self.indicators = {
            "RSI": {"type": "RSI", "data": self.stoch_rsi},
            "STOCHASTIC": {
                "K": self.stoch_rsi_k,
                "D": self.stoch_rsi_d,
            },
        }
  3. Run a backtest
  4. See error:

Traceback (most recent call last): File "/home/lewis/Projects/trading_bot/runfile.py", line 65, in main() File "/home/lewis/Projects/trading_bot/runfile.py", line 58, in main at.run() File "/home/lewis/.pyenv/versions/3.8.9/envs/maxwell/lib/python3.8/site-packages/autotrader/autotrader.py", line 711, in run self._main() File "/home/lewis/.pyenv/versions/3.8.9/envs/maxwell/lib/python3.8/site-packages/autotrader/autotrader.py", line 1120, in _main self.plot_backtest() File "/home/lewis/.pyenv/versions/3.8.9/envs/maxwell/lib/python3.8/site-packages/autotrader/autotrader.py", line 971, in plot_backtest ap.plot(backtest_dict=bot.backtest_results) File "/home/lewis/.pyenv/versions/3.8.9/envs/maxwell/lib/python3.8/site-packages/autotrader/autoplot.py", line 293, in plot bottom_figs = self._plot_indicators(indicators, main_plot) File "/home/lewis/.pyenv/versions/3.8.9/envs/maxwell/lib/python3.8/site-packages/autotrader/autoplot.py", line 736, in _plot_indicators indi_type = indicators[indicator]['type'] KeyError: 'type'

Process finished with exit code 1



**Expected behaviour**
Plot the two stochastic lines below the chart in one section.

**Screenshots**
n/a

**Version of AutoTrader being used**
`autotrader==0.6.2`

**Additional context**
Few other questions:
- Is there any functionality during backtesting to access previous trades? e.g. last 4 trades were losses so use 0.5*RR
- If the strategy has a lot of triggers it seems to cancel all trades, any idea why this might happen? My thought would be the whole account balance is in use so can't make a new trade without cancelling a previous one, when really it should just not create the new trade and let the old ones run.
kieran-mackle commented 2 years ago

Hey Lewis, the KeyError above is popping up because you haven't specified the indicator type as STOCHASTIC (the key you have named 'STOCHASTIC' is just the string used to label the indicator on the chart), but you have made me realise that I have actually deprecated the stochastic key, so apologies for the stale docs/code.

The preferred way to plot this would be using type: multi, as per the example below.

self.indicators = {"RSI": {"type": "RSI", "data": self.stoch_rsi},
                   "STOCHASTIC": {"type": "multi",
                                  "K": {"data": self.stoch_rsi_k, "color": "blue"},
                                  "D": {"data": self.stoch_rsi_d, "color": "red"},
                                  },
                   }

I think I decided to remove the STOCHASTIC type as the multi type is much more general and customisable, though I probably should have made the change clearer...

As per your other questions:

lleewwiiss commented 2 years ago

When using 1 leverage I get Insufficient margin to fill order. printed for every trade, I am testing it on ETH-USD is this because for a 10,000 account it cannot trade partial amounts with a 1% risk e.g. 1ETH is more than 1%*10000 and with 1 leverage it can't make the trade, therefore, it cancels it

kieran-mackle commented 2 years ago

I can't seem to replicate the issue (tried the macd demo strategy active in the demo runfile), and partial trades are allowed by default.

Looking into the code a bit deeper, I think this is coming from the get_size method of the broker utilites, and likely the get_pip_ratio method. The latter method is currently focused towards forex pairs, so might produce unexpected results for non FX instruments. I will need to fix this up to make it more general.

Basically what it looks like is happening, is that your stop losses are too tight (whether true, or not true due to an incorrect pip_value) forcing the trade size required to risk 1% up too high. This size then gets cancelled when the order reaches the broker, because although the trade would only lose 1% if the stop was hit, the margin required to open such a large trade is greater than the account margin available.

Sorry about the bug, but I can suggest a work around in the meantime: copy the get_size method into your strategy module, and provide the correct pip_value (line 82). Then just call this method to calculate the size and provide it when creating your orders.

lleewwiiss commented 2 years ago

Thanks, I will give it a try. I can give you the exact data/strategy if you would like to use it for debugging, its only a simple one

kieran-mackle commented 2 years ago

No problem, let me know how you go. And sure, feel free to email it to me: kemackle98@gmail.com

lleewwiiss commented 2 years ago

It seems to be a problem with RISK_PC using the macd strategy from the demo (latest version) with leverage set to 1 and RISK_PC set to 3 it gets all cancelled trades on EURAUD and mostly cancelled on ETH-USD. Might help with debugging.