Creating logic in Python for auto-generating a tuple of futures spreads to query

Discussion in 'App Development' started by joederp, Feb 10, 2017.

  1. joederp

    joederp

    Looking for ideas here...

    If I have a Python tuple of contract expirations for, say, CME Eurodollar futures, such as:

    GEcontracts = (GEH7.CME, GEM7.CME, GEU7.CME, GEZ7.CME, GEH8.CME, [and so on...])

    Now I want to make some logic in a loop statement that will automatically construct all possible combinations that would make up spreads as calendars (+1/-1 or -1/+1), butterflies (+1/-2/+1 or -1/+2/-1) and condors (+1/-1/+1/-1), the resulting output of which I will use to query a datafeed and filter by other metrics.

    What kind of statement would be a step in the right direction to get these spreads generated from the tuple?
     
  2. O(1)

    O(1)

    my first thought is that it sounds like a waste going thru the loop at runtime. is there a reason why you don't want to just define the spreads?
     
    Zzzz1 and tommcginnis like this.
  3. O(1)

    O(1)

  4. tommcginnis

    tommcginnis

    I developed a spreadsheet like this (ergo: "spreadsheet thinking!!" Eeeek!!)

    At any rate, I would create the entire mess, and shop for patterns that I would like to test-for and play with, rather than write-one, test, record, write-second, test, record.....
     
    Zzzz1 likes this.
  5. O(1)

    O(1)

    Code:
    import itertools
    
    GEcontracts = ('GEH7.CME', 'GEM7.CME', 'GEU7.CME', 'GEZ7.CME', 'GEH8.CME')
    
    print(tuple(itertools.combinations(GEcontracts, 2)))
    
    '''
    output is:
    (('GEH7.CME', 'GEM7.CME'), ('GEH7.CME', 'GEU7.CME'), ('GEH7.CME', 'GEZ7.CME'), ('GEH7.CME', 'GEH8.CME'), ('GEM7.CME', 'GEU7.CME'), ('GEM7.CME', 'GEZ7.CME'), ('GEM7.CME', 'GEH8.CME'), ('GEU7.CME', 'GEZ7.CME'), ('GEU7.CME', 'GEH8.CME'), ('GEZ7.CME', 'GEH8.CME'))
    '''
     
    Last edited by a moderator: Feb 12, 2017
  6. ryker

    ryker

    I have done something similar except that I generate a list of 3, 6, 9 and 12 months spreads and 3, 6 and 12 months butterflies (you can go further but I don't need too as I don't trade them).
    This way you get a list of all the traded spread and won't have any weird combinations (like a 13 months spread).
     
  7. joederp

    joederp

    Thanks for the comments, everyone.

    It needn't be a loop per se, that was just my first notion. As for just explicitly defining the spreads, well, I'm looking at 40-50 markets, and spreading not only intramarket, but intermarket, so we're talking more than a few thousand possible combinations.

    Yes, I think the combinations() function with some extra tweaks will work. Thank you!
     
  8. O(1)

    O(1)

    guess you could place the contracts in a csv that you could setup in excel but read in and parse.. even using columns to specify months.. and use OS time to decide which months to pick out and then create the spreads and place them in containers.. by category.. then once you have your containers of spread prices, you could do the rest. sounds intensive. how often do you plan to make data requests?
     
  9. joederp

    joederp

    Once a week for the whole list of combinations. As for the handful of spreads chosen per the filtering metrics (say 10-15 spreads), 3-4x per day...i.e. just get the last price.
     
    O(1) likes this.
  10. O(1)

    O(1)

    sounds good. keep us updated
     
    Last edited: Feb 11, 2017
    #10     Feb 11, 2017