flyingfox icon indicating copy to clipboard operation
flyingfox copied to clipboard

security list

Open markxie opened this issue 7 years ago • 1 comments
trafficstars

Hi ,

This is not a bug. but I don't know where to put my question. i tried to assign a security list to the context, like:

python: context.security_list = [sid(24), sid(5061), sid(39840), sid(21435)]

flyingfox:

fly_initialize <- function(context) {
context$i <- 0L
sList <- c("ARNC","AAPL","ABT","ADSK","TAP","ADBE","ADI","ADM")
context$asset <-  sList
}

fly_handle_data <- function(context, data) {
  
  # Increment day
  context$i <- context$i + 1L
  
  # While < 300 days of data, return
  if(context$i < 21L) {
    return()
  }

  price_hist <- fly_data_history(data, context$asset, "price", bar_count = 21L, frequency = "1d")

}

Error:

debugSource('~/ShortTermMeanReversion.R') Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Evaluation error: ValueError: invalid literal for int() with base 10: 'ARNC'.

Can you help? Or do you have some example that I can follow?

thanks

Mark

markxie avatar Jul 02 '18 02:07 markxie

Try something like this:

fly_initialize <- function(context) {
  context$i <- 0L
  
  # Use fly_symbols() with a list
  context$asset <-  fly_symbols(list("ARNC","AAPL","ABT","ADSK","TAP","ADBE","ADI","ADM"))
}

fly_handle_data <- function(context, data) {
  
  # Increment day
  context$i <- context$i + 1L
  
  # While < 300 days of data, return
  if(context$i < 21L) {
    return()
  }
  
  # To access each asset in the list, use an apply style function
  nms <- vapply(context$asset, function(x) {x$symbol}, FUN.VALUE = character(1))
  
  # Get the data history for each asset
  price_hist_list <- lapply(context$asset, fly_data_history, data = data, fields = "price", bar_count = 21L, frequency = "1d")
  
  names(price_hist_list) <- nms
  
  print(price_hist_list)
}

fly_run_algorithm(fly_initialize, fly_handle_data, start = as.Date("2017-01-01"), end = as.Date("2017-02-05"))

DavisVaughan avatar Jul 14 '18 11:07 DavisVaughan