Closed straj-granicy closed 2 years ago
I would strongly suggest to carefully read through your code from top to bottom, especially populate_indicators
around the area where ema_short
and ema_long
are generated.
The error message is quite clear - your dataframe does not contain ema_short_5
(it actually won't include any ema_short_*
key.
The error is quite easily spottable - but providing "per line help" is not something we're providing here, but we expect to a) do your own research b) investigate errors yourself.
@xmatthias , I strictly follow your instructions. in populate trends I have
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# Calculate all ema_short values
frames=[dataframe]
for val in self.buy_ema_short.range:
frames.append(pd.DataFrame ({
f'buy_ema_short_{val}':ta.EMA(dataframe, timeperiod=val)
}))
#Append columns to existing dataframe
merged_dataframe=pd.concat(frames)
# Calculate all ema_long values
frames = [dataframe]
for val in self.buy_ema_short.range:
frames.append(pd.DataFrame({
f'buy_ema_long_{val}': ta.EMA(dataframe, timeperiod = val)
}))
# Append columns to existing dataframe
merged_dataframe = pd.concat(frames)
return dataframe
In populate buy and sell indicators:
conditions.append(qtpylib.crossed_above(
dataframe[f'buy_ema_short_{self.buy_ema_short.value}'], dataframe[f'buy_ema_long_{self.buy_ema_long.value}']
))
I've checked everything and found no mistake. I still have this mistake
Traceback (most recent call last):
File "/root/freqtrade/freqtrade/main.py", line 37, in main
return_code = args['func'](args)
File "/root/freqtrade/freqtrade/commands/optimize_commands.py", line 54, in start_backtesting
backtesting.start()
File "/root/freqtrade/freqtrade/optimize/backtesting.py", line 672, in start
min_date, max_date = self.backtest_one_strategy(strat, data, timerange)
File "/root/freqtrade/freqtrade/optimize/backtesting.py", line 643, in backtest_one_strategy
results = self.backtest(
File "/root/freqtrade/freqtrade/optimize/backtesting.py", line 519, in backtest
data: Dict = self._get_ohlcv_as_lists(processed)
File "/root/freqtrade/freqtrade/optimize/backtesting.py", line 267, in _get_ohlcv_as_lists
self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair}).copy()
File "/root/freqtrade/freqtrade/strategy/interface.py", line 874, in advise_buy
return self.populate_buy_trend(dataframe, metadata)
File "/root/freqtrade/user_data/strategies/test1.py", line 178, in populate_buy_trend
dataframe[f'buy_ema_short_{self.buy_ema_short.value}'], dataframe[f'buy_ema_long_{self.buy_ema_long.value}']
File "/root/freqtrade/.env/lib/python3.8/site-packages/pandas/core/frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "/root/freqtrade/.env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 'buy_ema_short_5'
I'll really appreciate if you can give the workable solution. I think it would help the others too. Thank you in advance.
The mistakes made are really beginner mistakes - they got nothing to do with freqtrade itself, but with python / programming understanding per se.
That's not a problem - everyone is starting at some point but i'd suggest to keep your implementation simple (not using methods like frames.append()
and pd.concat()
- which you clearly don't understand yet) - but instead stick to the hyperopt documentation - which DOES have a working solution - one that is using EXACTLY the usecase you're trying to implement.
Your primary mistake is that you assign to merged_dataframe
- but then don't use that anymore.
As such, the dataframe you return from populate_indicators()
(dataframe
) will not have any of the generated columns - but will be the unmodified dataframe.
@xmatthias , thank you for your comments. yes, you are right, I'm beginner in Python. but as I'm using big number of pairs(about 50), I'd like to use this method, as when I start using hyperopt, the server I use crahes. It would really take me days to do all the calculations.. I was thinking that the merged_dataframe should be used in populate buy and sell trends, so it should be the arguments, which should be used Like this. def populate_buy_trend(self, dataframe: DataFrame, merged_dataframe : merged_dataframe, metadata: dict) -> DataFrame: and also I should change the indicator like this conditions.append(qtpylib.crossed_above( merged_dataframe[f'emashort{self.buy_ema_short.value}'], merged_dataframe[f'emalong{self.buy_ema_long.value}']
Also I think I'll use other indicators, so I need to return a dictionary in populate trends indicator like this return { 'dataframe': Dataframe, 'merged_dataframe': merged_dataframe }
Are my thoughts correct? Or I'm doing wrong again? Thank you.
Hyperopt crashing with 50 pairs has little to do with how you assign your indicators, but instead with the memory it consumes.
all populate_*_trend()
methods must return one dataframe. returning nested dicts and whatnot is not supported (and pointless at that).
please simply read through the documentation instead - which will show you the ways to define a strategy, and (as pointed out above) includes a sample on how to opitmize an indicator - therefore directly and explicitly covers how to approach your question.
@macsim26 your question is CLEARLY off-topic (nothing in this whole issue mentions dataframe fragmentations).
This issue was about wrong column naming - and has been solved over a year ago. if you have a question that's not related to an existing issue - then don't ask it there, but instead open a new one (if you think it's worth being answered, and is not already part of the documentation).
Dear @xmatthias , As you closed my previous topic here https://github.com/freqtrade/technical/issues/230 I'd like to re-open this issue for such reasons: I've done everything according to your guides, but I still have errors (don't know why, as I've checked everything several times)... Here's my code:
I'd really appreciate if you look it through and tell me what's wrong with it... Thank you in advance for all your advices.
I have this mistake, when I run hyperopt:
And I also have this mistake, when I try to run backtest: