Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Now that you know a little about me, let me tell you about the issue I am
having:
In a monthly schedule function, I order 150 securities using order_target_value the target I set is just 100$. Then I got an OverflowError message.
OverflowError: Python int too large to convert to C long", call = py_call_impl(callable, dots$args, dots$keywords)
`
Here is how you can reproduce this issue on your machine:
I am looking into the source code. but does anyone know what is the limitations of the zipline
...
Sincerely,
Mo
This looks like you are calling into some R library to do some calculation. Do you get the same error if remove all logic except for a simple order?
Are you using a custom slippage or commission model?
Thanks! I just tried. After I removed slippage and commission I still got this issue.
I do call some R library. but it is just for feeding data, after I removed these R code it sill give me same errors.
Can you post the full traceback? The one given shows R and C++ code which zipline does not contain.
Sure. Currently, We are using the R calling zipline. We do re-balance every month. with ranked scores.
This code works well for now. But if I updat the n_top to 200. I got the issue above.
rebalance <-function(context, data){
tryCatch({
message(paste0("rebalance at:", context$i ))
n_top <- 100
target_value <- 10000L
tops<- universe[day==Days[context$i] & !is.na(COMPOSITE) & !startsWith(as.character(security),'@'), security][1:n_top]
if(context$lastrb>0){
last_tops<- universe[day==Days[context$lastrb] & !is.na(COMPOSITE) & !startsWith(as.character(security),'@'), security][1:n_top]
no_change <- intersect(tops, last_tops)
tops <- setdiff(tops, no_change)
sell_list <- setdiff(last_tops, no_change)
# sell not on the list
message('sell_list> ', sell_list)
for(sell in sell_list){
fly_order_target_value(asset = fly_symbol(sell), target = 0L)
}
}
context$lastrb = context$i
# buy on the list
message('buy_list> ', tops)
for(buy in tops){
if(data$can_trade(fly_symbol(buy)) ){
fly_order_target_value(fly_symbol(buy), target = target_value)
}
}
}, error = function(e){
message("error :", e)
})
message('context$lastrb=' , context$lastrb )
}
We don't support any R interface. The R interface you are using is a third party package, you may want to open an issue on that repo. What is likely happening is that some integer is growing larger than the size of a long on your system[1]. In Python, this wouldn't be an issue, but the code used to marshal Python data back to R is failing to account for arbitrarily sized Python ints. The code is helpful, but I would also need the full traceback of the error, though no guarantees that I can help.
gcc -x c <(printf '#include<stdio.h>\nint main(){printf("%%d\\n", sizeof(long));return 0;}') -o /tmp/sizeoflong && /tmp/sizeoflong && rm -f /tmp/sizeoflong
If that prints 4, this error seems basically unavoidable.
@llllllllll Hi Thank you very much for this info. I just tried on gcc code you sent to me. it give me back
8.
Here is the full traceback of the error, btw.
Error in py_call_impl(callable, dots$args, dots$keywords) : OverflowError: Python int too large to convert to C long
15.
stop(structure(list(message = "OverflowError: Python int too large to convert to C long", call = py_call_impl(callable, dots$args, dots$keywords), cppstack = structure(list(file = "", line = -1L, stack = c("/home/mz3/R/x86_64-pc-linux-gnu-library/3.4/reticulate/libs/reticulate.so(Rcpp::exception::exception(char const*, bool)+0x82) [0x7fcea59d7802]", "/home/mz3/R/x86_64-pc-linux-gnu-library/3.4/reticulate/libs/reticulate.so(Rcpp::stop(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)+0x27) [0x7fcea59d7937]", ...
14.
zipline at _protocol.pxd#14
13.
__setattr__ at position.py#68
12.
handle_split at position.py#120
11.
handle_splits at ledger.py#136
10.
process_splits at ledger.py#533
9.
handle_splits at tracker.py#180
8.
once_a_day at tradesimulation.py#174
7.
transform at tradesimulation.py#209
6.
run at algorithm.py#730
5.
_run at run_algo.py#208
4.
run_algorithm at run_algo.py#398
3.
py_run at <string>#50
2.
main$py_run(fly_initialize = initialize, fly_handle_data = handle_data, start = start, end = end, capital_base = capital_base, before_trading_start = before_trading_start, analyze = analyze, data_frequency = data_frequency, data = data, bundle = bundle, bundle_timestamp = bundle_timestamp, trading_calendar = trading_calendar, ...
1.
fly_run_algorithm(initialize = fly_initialize, before_trading_start = fly_before_trading_start, handle_data = fly_handle_data, analyze = fly_analyze, bundle = "mdo_csv4", capital_base = 1e+06, trading_calendar = calendar, data_frequency = "daily", start = start_date, end = end_date)
I will try to migrate our code to pure python later and check if it still an issue. Let me work on it .
just a small formatting comment, when you want to monospace format multiple lines, like the traceback you posted, you need to use three backtick characters, not just one. I edited your comment to do that.
Given this traceback it looks like the overflow is actually in Zipline's python->cython boundary. This appears to be in update the position size after a split. What is likely happening is that you are ordering a lot of shares of one of the assets and then there is a massive split causing the share count to grow very rapidly and exceed 2 ** 63 - 1 shares. Some things to check:
Thanks @llllllllll
very helpful information. let me try these 3 things you pointed out.
btw, I will formatting the content better for the next time.
Any updates @zhaimac?
@freddiev4 We stopped working on the zipline R integration for now, but we may follow up this problem in the future.!