stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
12.87k stars 4.11k forks source link

AttributeError: 'TradingAlgorithm' object has no attribute 'factor_data' #228

Closed kth2 closed 2 years ago

kth2 commented 2 years ago

Hi Stefan, When I run 04_single_factor_zipline with my own customised bundles, I get the following error, I don't know where is factor_data:

~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2404 with self.builtin_trap: 2405 args = (magic_arg_s, cell) -> 2406 result = fn(*args, **kwargs) 2407 return result 2408

~/opt/anaconda3/lib/python3.8/site-packages/zipline/main.py in zipline_magic(line, cell) 333 ) 334 try: --> 335 return run.main( 336 # put our overrides at the start of the parameter list so that 337 # users may pass values with higher precedence

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in main(self, args, prog_name, complete_var, standalone_mode, windows_expand_args, extra) 1051 try: 1052 with self.make_context(prog_name, args, extra) as ctx: -> 1053 rv = self.invoke(ctx) 1054 if not standalone_mode: 1055 return rv

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in invoke(self, ctx) 1393 1394 if self.callback is not None: -> 1395 return ctx.invoke(self.callback, **ctx.params) 1396 1397 def shell_complete(self, ctx: Context, incomplete: str) -> t.List["CompletionItem"]:

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in invoke(_Context__self, _Contextcallback, *args, **kwargs) 752 with augment_usage_errors(self): 753 with ctx: --> 754 return __callback(*args, **kwargs) 755 756 def forward(

~/opt/anaconda3/lib/python3.8/site-packages/click/decorators.py in new_func(*args, kwargs) 24 25 def new_func(*args, *kwargs): # type: ignore ---> 26 return f(get_current_context(), args, kwargs) 27 28 return update_wrapper(t.cast(F, new_func), f)

~/opt/anaconda3/lib/python3.8/site-packages/zipline/main.py in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, benchmark_file, benchmark_symbol, benchmark_sid, no_benchmark, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter) 298 ) 299 --> 300 return _run( 301 initialize=None, 302 handle_data=None,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, custom_loader, benchmark_spec) 198 199 try: --> 200 perf = TradingAlgorithm( 201 namespace=namespace, 202 data_portal=data,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/algorithm.py in run(self, data_portal) 621 try: 622 perfs = [] --> 623 for perf in self.get_generator(): 624 perfs.append(perf) 625

~/opt/anaconda3/lib/python3.8/site-packages/zipline/gens/tradesimulation.py in transform(self) 226 for dt, action in self.clock: 227 if action == BAR: --> 228 for capital_change_packet in every_bar(dt): 229 yield capital_change_packet 230 elif action == SESSION_START:

~/opt/anaconda3/lib/python3.8/site-packages/zipline/gens/tradesimulation.py in every_bar(dt_to_use, current_data, handle_data) 141 metrics_tracker.process_commission(commission) 142 --> 143 handle_data(algo, current_data, dt_to_use) 144 145 # grab any new orders from the blotter, then clear the list.

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt) 204 with self._create_context(data): 205 for event in self._events: --> 206 event.handle_data( 207 context, 208 data,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt) 227 """ 228 if self.rule.should_trigger(dt): --> 229 self.callback(context, data) 230 231

in rebalance(context, data) AttributeError: 'TradingAlgorithm' object has no attribute 'factor_data'
kth2 commented 2 years ago

@stefan-jansen can you advise?

ghostMike666 commented 2 years ago

El dom., 27 de febrero de 2022 1:02 a. m., kth2 @.***> escribió:

Hi Stefan, When I run 04_single_factor_zipline with my own customised bundles, I get the following error, I don't know where is factor_data:

~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2404 with self.builtin_trap: 2405 args = (magic_arg_s, cell) -> 2406 result = fn(*args, **kwargs) 2407 return result 2408

~/opt/anaconda3/lib/python3.8/site-packages/zipline/main.py in zipline_magic(line, cell) 333 ) 334 try: --> 335 return run.main( 336 # put our overrides at the start of the parameter list so that 337 # users may pass values with higher precedence

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in main(self, args, prog_name, complete_var, standalone_mode, windows_expand_args, extra) 1051 try: 1052 with self.make_context(prog_name, args, extra) as ctx: -> 1053 rv = self.invoke(ctx) 1054 if not standalone_mode: 1055 return rv

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in invoke(self, ctx) 1393 1394 if self.callback is not None: -> 1395 return ctx.invoke(self.callback, **ctx.params) 1396 1397 def shell_complete(self, ctx: Context, incomplete: str) -> t.List["CompletionItem"]:

~/opt/anaconda3/lib/python3.8/site-packages/click/core.py in invoke(_Context__self, _Contextcallback, *args, **kwargs) 752 with augment_usage_errors(self): 753 with ctx: --> 754 return __callback(*args, **kwargs) 755 756 def forward(

~/opt/anaconda3/lib/python3.8/site-packages/click/decorators.py in new_func(*args, kwargs) 24 25 def new_func(*args, *kwargs): # type: ignore ---> 26 return f(get_current_context(), args, kwargs) 27 28 return update_wrapper(t.cast(F, new_func), f)

~/opt/anaconda3/lib/python3.8/site-packages/zipline/main.py in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, benchmark_file, benchmark_symbol, benchmark_sid, no_benchmark, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, blotter) 298 ) 299 --> 300 return _run( 301 initialize=None, 302 handle_data=None,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, custom_loader, benchmark_spec) 198 199 try: --> 200 perf = TradingAlgorithm( 201 namespace=namespace, 202 data_portal=data,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/algorithm.py in run(self, data_portal) 621 try: 622 perfs = [] --> 623 for perf in self.get_generator(): 624 perfs.append(perf) 625

~/opt/anaconda3/lib/python3.8/site-packages/zipline/gens/tradesimulation.py in transform(self) 226 for dt, action in self.clock: 227 if action == BAR: --> 228 for capital_change_packet in every_bar(dt): 229 yield capital_change_packet 230 elif action == SESSION_START:

~/opt/anaconda3/lib/python3.8/site-packages/zipline/gens/tradesimulation.py in every_bar(dt_to_use, current_data, handle_data) 141 metrics_tracker.process_commission(commission) 142 --> 143 handle_data(algo, current_data, dt_to_use) 144 145 # grab any new orders from the blotter, then clear the list.

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt) 204 with self._create_context(data): 205 for event in self._events: --> 206 event.handle_data( 207 context, 208 data,

~/opt/anaconda3/lib/python3.8/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt) 227 """ 228 if self.rule.should_trigger(dt): --> 229 self.callback(context, data) 230 231

in rebalance(context, data)

AttributeError: 'TradingAlgorithm' object has no attribute 'factor_data'

— Reply to this email directly, view it on GitHub https://github.com/stefan-jansen/machine-learning-for-trading/issues/228, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATHPZIPYMEAZGMPPWWZ5QX3U5HSAXANCNFSM5POTUMSA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

stefan-jansen commented 2 years ago

factor_data is logged in rebalance():

def rebalance(context, data):
    """Compute long, short and obsolete holdings; place trade orders"""
    factor_data = context.factor_data
    record(factor_data=factor_data.ranking)

Did you make any changes to the code so this attribute no longer exists?

kth2 commented 2 years ago

factor_data is logged in rebalance():

def rebalance(context, data):
    """Compute long, short and obsolete holdings; place trade orders"""
    factor_data = context.factor_data
    record(factor_data=factor_data.ranking)

Did you make any changes to the code so this attribute no longer exists?

Hi @stefan-jansen , thanks for replying. No, I didn't make any changes to the code. i follow exactly coding.

def rebalance(context, data):
"""Compute long, short and obsolete holdings; place trade orders"""
  factor_data = context.factor_data
  record(factor_data=factor_data.ranking)
  assets = factor_data.index
  record(prices=data.current(assets, 'price'))

  longs = assets[factor_data.longs]
  shorts = assets[factor_data.shorts]
  divest = set(context.portfolio.positions.keys()) - set(longs.union(shorts))

  exec_trades(data, assets=divest, target_percent=0)
  exec_trades(data, assets=longs, target_percent=1 / N_LONGS)
  exec_trades(data, assets=shorts, target_percent=-1 / N_SHORTS)
stefan-jansen commented 2 years ago

Sorry, I hadn't noticed you were using custom bundles. Does the code above work for you with the Quandl bundle? If so, do any other Zipline examples work with the custom bundle?

stefan-jansen commented 2 years ago

Closing for now due to lack of activity, feel free to reopen if you have further questions.