@klearner1 Did you find the way out?
For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Latest posts made by Federico Bld
-
RE: opening range breakout strategy for multiple stocks
@abhishek.anand @krispy have you tried to specify which asset should be sold / closed? In the code i see:
self.buy()
insteadself.buy(data=d)
self.close()
insteadself.close(data=d)
Let me know if works better.
-
RE: Discrepancy with PnL in the Stop function vs TradeAnalyzer Net PnL
Errata corr.:
@federico-bld said in Discrepancy with PnL in the Stop function vs TradeAnalyzer Net PnL:
def stop(self): # if we're backtesting and out of data, close open positions so they get counted properly. # must be done on the 2nd to last, as market orders wait to get another candle, to execute based on open price. for data in self.datas: if self.getposition(data) and len(data) == data.buflen() - 1 and "live" not in self.environment: logger.debug("Closing open position to conclude backtesting") self.close(data=data)
-
RE: Discrepancy with PnL in the Stop function vs TradeAnalyzer Net PnL
def stop(self): # if we're backtesting and out of data, close open positions so they get counted properly. # must be done on the 2nd to last, as market orders wait to get another candle, to execute based on open price. for data in self.datas: if self.position and len(self.data) == self.data.buflen() - 1 and "live" not in self.environment: logger.debug("Closing open position to conclude backtesting") self.close(data=d)
-
RE: Discrepancy with PnL in the Stop function vs TradeAnalyzer Net PnL
@manos would it make a better sense to put it in the stop() method?
-
Get params trought superClass
Developing several strategies i have to combine them. So I wrote superclasses, i.e:
class BaseStrategy(bt.Strategy): params = ( ('loShBo', 2), ('isBacktesting', False), ("sizerType", None), # Must be specified otherwise will be rised an Exception ("sizerDictParameters", {"amount": '', "retint": ""}), # Must be specified otherwise will be rised an Exception )
class OverBaseStrategy(BaseStrategy): params = ( ('sma',26), ('roc',50), ('smaRoc',50), ('ema',26), )
class MyStrategy_01(OverBaseStrategy): params = dict( stop_loss=0.05, trail=0.07, )
When I call:
cerebro = bt.Cerebro() params = { 'trail': 0.02, 'smaRoc': 20, 'isBacktesting': True} cerebro.addstrategy(strategy=MyStrategy_01, **params) pprint(cerebro.strats[0][0][0].params.__dict__)
The output is:
mappingproxy({'__doc__': None, '__module__': 'backtrader.metabase', '_getpairs': <classmethod object at 0x0000024E510751F0>, '_getpairsbase': <classmethod object at 0x0000024E51075190>, '_getrecurse': <classmethod object at 0x0000024E51075250>, 'stop_loss': 0.05, 'trail': 0.07})
What can I do to retrive all params with superclasses included?
i.e. :mappingproxy({ '__doc__': None, '__module__': 'backtrader.metabase', '_getpairs': <classmethod object at 0x0000024E510751F0>, '_getpairsbase': <classmethod object at 0x0000024E51075190>, '_getrecurse': <classmethod object at 0x0000024E51075250>, 'loShBo': 2, 'isBacktesting': True, # <== NOT False as default 'sizerType': None, 'sizerDictParameters': {"amount": '', "retint": ""}, 'sma': 26, 'roc': 20, 'smaRoc': 20, # <== NOT 50 as default 'ema': 26, 'stop_loss': 0.05, 'trail': 0.02, # <== NOT 0.07 as default })
-
Get historical data dynamically before cerebro.run()
I have to lunch strategies with serveral feeds with different timeframes.
I didn't find the way to get "minpediod" to calculate "hist_start_date" variable.# My formula shuld be look like: minutes_of_one_candle = get_compression_in_minutes_from_timeframe(assetTimeframe,assetCompression) minperiod = # I don't know how to retrieve the "minperiod" from the strategy with custom timeAgoInMinutes = minperiod * minutes_of_one_candle
Follows the complete code
strategies_list = [sma_200minperiod_strategy, ema_5minperiod_strategy] assets = [ ['ticker1', 1, 'minutes',] ['ticker2', 30, 'minutes',] ['ticker3', 1, 'hours',] ['ticker4', 1, 'days',] ] for strategy in strategies_list: cerebro.addstrategy(strategy=strategy, **get_strategyParams(strategy)) for assetName in assets: dataname =assetName[0] assetTimeframe = get_bt_TimeFrame(assetName[2]) assetCompression = get_bt_compression(assetName[1]) assetCompression_in_Minutes = get_compression_in_minutes(assetTimeframe , assetCompression) minutes_of_one_candle = get_compression_in_minutes_from_timeframe(assetTimeframe,assetCompression) minperiod = # I don't know how to retrieve the "minperiod" from the strategy with custom parameters timeAgoInMinutes = minperiod * minutes_of_one_candle hist_start_date = datetime.utcnow() - timedelta(minutes=timeAgoInMinutes) data = store.getdata( dataname=dataname, timeframe=assetTimeframe, compression=assetCompression, fromdate=hist_start_date, # <======== ohlcv_limit=250, drop_newest=True) cerebro.adddata(data)
How can I get
minperiod
to setfromdate = hist_start_date
before cerebro.run()?