AutoTrader
AutoTrader copied to clipboard
STOCHASTIC Plotting doesn't work
Describe the bug
When using the key STOCHASTIC
in the indicators dictionary an error is thrown.
To Reproduce Steps to reproduce the behaviour:
- 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)
- 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,
},
}
- Run a backtest
- See error:
Traceback (most recent call last):
File "/home/lewis/Projects/trading_bot/runfile.py", line 65, in <module>
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.
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:
- Yes, you can do this using the
get_trades
method, with the argumenttrade_status = 'closed'
. This will return all closed trades taken for the instrument/s specified, ordered by time closed (it is basically indexingbroker.trades
. Some notes: this functionality (specific retrieving closed trades) is currently only supported in the virtual broker, ie. in backtesting. To call this method from your strategy, you will need to includeINCLUDE_BROKER: True
in your strategy configuration - see the docs here for an example. - I have a feeling this is due to a margin call occurring, a feature implemented in
v0.6.1
. To check if it is, try increasing thebroker_verbosity
to 1 via theconfigure
method - this will print when a margin call occurs. This should only happen if you are trading on margin (ie. leverage is set greater than 1), and if your margin fraction (available margin/NAV) drops below themargin_call_fraction
. However, I am assuming you will not have changed themargin_call_fraction
from the default of 0, so the only way for this to occur is if your margin available or NAV goes negative. So another check would be to print the NAV each update. If these suggestions don't help, or don't seem relevant, let me know and we can figure out what's going on.
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
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.
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
No problem, let me know how you go. And sure, feel free to email it to me: [email protected]
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.