Python code for calling 123 API

P123 staff -

I am attempting to run the Portfolio123 API using Python running in Google Colaboratory. I have successfully imported the P123 code, but now I am stuck because there is no meaningful example or documentation provided. The sample code provided is:

Sample code:

import p123api
    client = p123api.Client(api_id='your api id', api_key='your api key')
    print(client.screen_run({'screen': {'type': 'stock', 'universe': 'nasdaq100'}, 'asOfDt': '2020-05-12'}))
except p123api.ClientException as e:

Can you provide a REAL example for running a ranking system on a specific date for specific stocks (or stock universe). I have gone as far as I can guessing as to what the parameters might be. You guys have gone to the effort of creating this. So can you please go the last mile?

Thank you

The API documentation (supported endpoints and parameters) can be found here:

As for the Python client you can get the supported operations via code completion on the “client” object in any decent IDE. They generally follow the naming convention of the endpoints:

  • /data →
  • /data/universe → client.data_universe
  • /rank → client.rank_update
  • /rank/performance → client.rank_perf
  • /rank/ranks → client.rank_ranks
  • /screen/run → client.screen_run
  • /screen/backtest → client.screen_run
  • /screen/rolling-backtest → client.screen_rolling_backtest
  • /universe → client.universe_update

valmarv - OK - please point me to a “decent IDE” for Google Colaboratory and how to perform “code completion” using such IDE. As far as I can tell there isn’t enough information in the P123 documentation to make use of the client rank_ranks. A simple example call using the python client would very likely suffice to get me going. However, it seems that P123 is either unwilling to make the effort or is only interested in catering to specialized programmers with experience the this area. I am not the first to make this complaint.

Steve I agree . We need examples in the documentation that actually work with all the possible values for each parameter. Did you download the samples files for the DataMiner ? they should give you better idea and what to put for the parameters . Parameter names are not exactly the same but should be obvious. I’ve been pushing the programmers to be more supportive of non programmers.

For now see the sample folder in

I have looked at Dataminer, and I have gone through the python-importable file and and can’t find anywhere the parameter definitions that I need to make a simple ranking system output using python and the client provided. It would probably take 30 seconds from someone at P123 to generate a simple working example that I could use and modify for my project. The alternative is for me to flounder until I give up.


Here’s a simple rank ranks call:

import p123api
client = p123api.Client(api_id='your api id', api_key='your api key')
print(client.ranks({'rankingSystem': 264499, 'asOfDt': '2015-04-21', 'universe': 'nasdaq100'}))
except p123api.ClientException as e:

Full list of supported parameters can be found here (under Request Body > Schema):

Steve see image for parameter definition

Thank you Valmarv! When I looked through the python module I realized that you are an exceptional programmer. Just keep in mind that you are working with hacks like myself that need to shown the obvious.

We will be adding sample code too for each API operation

Steve, what you are trying to do is supported in the DataMiner. Just curious, why not use the DataMiner?

Marco - Thanks for asking. I understand this function is supported in Dataminer. But I am working on a bigger project using Python in Colaboratory. Ultimately, it may involve doing multiple ranking dumps on a weekly basis. So my preference is to automate it using Python instead of doing part of the job manually in Dataminer and having to screw around with running and saving files. It eliminates human errors and a lot of time.

Interesting. Perhaps we should load our sample code in Colaboratory.Thanks

Will you be using the machine learning features of colaboratory ?

Yes - tensorflow and xgboost.

So for anyone following this thread:

I had to change Valmarv’s code
print(client.ranks({‘rankingSystem’: 264499, ‘asOfDt’: ‘2015-04-21’, ‘universe’: ‘nasdaq100’}))

print(client.rank_ranks({‘rankingSystem’: 264499, ‘asOfDt’: ‘2015-04-21’, ‘universe’: ‘nasdaq100’}))

Then it worked. It returned UIDs, followed by Tickers, then ranking values.

For anyone wanting to use Colab with the P123 API.

For this code to work, you have to place a copy of on Google Drive. It has to be the original text file from GitHub. Don’t try to save it to Google Drive using Colab. It won’t work! You may need to adjust the path in sys.path.append(‘/content/gdrive/My Drive/Colab Notebooks’) to match your Drive setup.

Interesting work Steve. Perhaps this is the ticket for us to distinguish ourselves from the rest.

But how much ML stuff can you do w/o a data license? Right now all you can download is ranks and future performance data. Is that what you are using? Perhaps it is enough though and don’t really need the data point itself

We need to setup examples for people to tinker around with ML

Marco - how much historical rank data am I allowed to download?

no limits . only limits now is # api requests because we’re still in beta and finalizing the product entitlements. But with a decent p123 membership should be plenty. Can you store the data in colaboratory?


I think Steve will confirm that I have been working with him on this. I have been using TensorFlow and Boosting for a long time and they are incredible tools.

Colab is a free tool that I introduced to Steve. It saves the hassle of using terminal and brew (on Macs) to be able to use multiple processors and load XGBoost, download Anaconda, Python, libraries and modules and keeping these all compatible.

No one has asked P123 to make machine learning available on its platform for a long time. Colab makes that largely unnecessary.

BTW, Colab is free but can be sped up an upgraded for $9.99 a month. I doubt anyone would need this but I think Colab would be adequate for even the most demanding needs.

Recently I installed a library (with Anaconda) which required an update of TensorFlow to use. My program no longer ran. Turns out that only some of the TensorFlow and Keras interactions and coding had been changed. But I was lost for couple days. I thought I would have to start from scratch and download everything.

Okay too much of a long, sad story but Colab would have avoided all of that and I think Steve would even agree that Colab is a lot more user friendly (even with his programming skills). If Colab does not work just close the window and start again—importing the correct libraries this time.

Steve’s posts DO NOT even hint at his programing abilities. Steve is an incredible programmer.

It is nice that P123 is helping someone with this. But I will say that considering Steve’s programming skill this is a long thread.

If it took this long for Steve to get this to work, I can guarantee that P123 is missing an opportunity that the average programmer presents.

Maybe P123 will not lose me as Steve has been very kind to share is programming code with me while I share my experience with TensorFlow and XGBoost. But honestly, P123 could be better.

There are easier ways to do this and depending on whom you ask possibly better ways—with no PIT issues. P123 will have to do more to achieve its full potential, IMHO. If it were me, I would not stop here.

Did I mention Steve is an incredible programmer? With one of the better performing Designer Models? I can say with 100% confidence that anyone reading this would be a fool not to look for Deep learning or Boosting models that Steve may develop.

While Steve has already made this work, P123 could make this a little more user-friendly and attract more than elite programmers like Steve to P123 with this.