Closed lleewwiiss closed 1 year 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:
get_trades
method, with the argument trade_status = 'closed'
. This will return all closed trades taken for the instrument/s specified, ordered by time closed (it is basically indexing broker.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 include INCLUDE_BROKER: True
in your strategy configuration - see the docs here for an example.v0.6.1
. To check if it is, try increasing the broker_verbosity
to 1 via the configure
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 the margin_call_fraction
. However, I am assuming you will not have changed the margin_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: kemackle98@gmail.com
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.
Describe the bug When using the key
STOCHASTIC
in the indicators dictionary an error is thrown.To Reproduce Steps to reproduce the behaviour:
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