Store sorted symbols to display in Log file after test is finished?

I am just trying to store some sorted symbols into a few different arrays/Lists so that I can display them in the Log file as seperate lists. For example, an array of stocks that have gone up 20%, then another array of stocks that have gone up 30%. Then display these lists in the log seperated by some text so I know which are > 30% vs 20%. Or I would like to take these arrays and sort them by highest percent to lowest percent. Is this possible?

Comments

  • ptunneyptunney Posts: 246
    edited August 2020

    Of course, anything is possible!
    You need to give more information though.
    1) You need to decide the size of your universe, that will steer how you attack the problem.
    2) You need to specify when (or how often) in the backtest you need to output this list, is it once just after the open, is it at the end of the day or is it every minute?

    But doing anything that is not symbol by symbol and event driven immediately steps outside the design of the system and so you have to think more about how to achieve this. ie if your symbol universe was only 5 symbols FB, AAPL, AMZN, NFLX, GOOG, and what you are wanting to output is them sorted in order by how much they have moved since the close, and you want to do it at the open, then that is quite complex. You have to a) make sure they have all opened before attempting the sort and b) decide on WHOSE callback you use to do the sort.. ie you will get 5 callbacks in on_minute, one for each symbol. If you check for an open on each symbol, and the first callback you get is GOOG, you have to check if the others are open yet before you can do your sort, how do you do that? and the answer is no they have not (because GOOG opened first) so you have to set a flag saying GOOG is ready or 'some alternative' and wait. Then do this for each symbol in turn until all 5 are open then you can sort. So that is one option. Or you can just say, I will sort at 9:31 in on_minute. Or you can say I will sort at 9:30:10 in on_trade... in each of those cases, if you have a larger symbol set, thinner stocks, some will not have opened. So what do you do then??

    So you can see how "what" you are doing and "when" you are doing it are vitally important pieces of information to help you decide how to attack your problem.

    Now, I will give you the answer that will fit most scenarios....

    You only run SPY..... it is the most traded symbol, it trades even when the market is quiet, it is one of the first to open....

    You add any other symbols you are interested in to backtesting_extra_symbols in the same way you would for is_symbol_qualified. This means you get the DATA for the symbols but you do not get any CALLBACKS.. You are only going to get CALLBACKS for events for SPY. You also put each qualified symbol into a list and that list must be a "CLASS variable". A CLASS variable is a variable that can be accessed by all symbols that are running. We define this CLASS variable right at the top of our script. In CALLBACKS that include CLS we can access the variable directly, in most others we use self._ class _.nameofvariable (note, there are two underscores together on each side of class, I cannot do that in this editor as it treats that as a signal to italicise the word!!).

    And now, all that is left is to decide WHEN to sort... put that in your code and do your sort.

    If you look in public scripts there are a couple of SORT scripts that you can play with to get an idea of how this works...

    Sorted_60m_move
    Sorted_symbols.

    PS There is a LOG in CloudQuant but I think when you say LOG you actually mean CONSOLE.

  • jg892461jg892461 Posts: 11

    Excellent Pt!
    I cant thank you enough for the response.

    And yes, I did mean CONSOLE. My apologies.

Sign In or Register to comment.