Ranking Daily Price Changes
<p>I am writing an algo to rank the changes in price of a given list of stocks each day. Right now I'm working with two stocks: Apple and Amex. I would like to rank them from greatest change in price to least change in price, daily.</p>

<p>For example the data I have looks like this:</p>

<p>Day 1:</p>

<p>AAPL = 60.40</p>

<p>AMX = 15.50</p>

<p>Day 2:</p>

<p>AAPL = 61.00</p>

<p>AMX = 15.60</p>

<p>And I would like the result to look like this, with the highest positive change to be ranked fist. Is this possible?</p>

<p>Change:</p>

<p>AAPLchg = .60 Rank 1</p>

<p>AMXchg = .10  Rank 2</p>

<p>This is what I have so far:</p>

<pre><code>def handle_data(context, data):
price_history = data.history(context.security_list, "price", bar_count=2, frequency="1d")
for s in context.security_list:
    PrevAMX = price_history[sid(679)][-2]
    CurrAMX = price_history[sid(679)][1]
    PrevAAPL = price_history[sid(24)][-2]
    CurrAAPL = price_history[sid(24)][1]
    AMXchg = CurrAMX - PrevAMX
    AAPLchg = CurrAAPL - PrevAAPL
    if AMXchg &lt; AAPLchg:
        order(sid(679), 20)
    if AAPLchg &lt; AMXchg:
        order(sid(24), 20)


price_history = data.history(context.security_list, "price", 20, "1d")
print price_history.mean()
print AMXchg
print AAPLchg
print log.info(str(context.chg_list.sort('adv_rank', descending=True).head()))
</code></pre>

<p>Say you have the prices in dictionaries, then you could do this:</p>

<pre><code>yesterday = {
    'AAPL': 60.40,
    'AMX': 15.50,
}

today = {
    'AAPL': 61.00,
    'AMX': 15.60,
}

change = {k: today[k] - yesterday[k] for k in today}

sorted_stocks = sorted(change, key=change.get, reverse=True)

for ss in sorted_stocks:
    print('%s: %.2f' % (ss, change[ss]))
</code></pre>

python sorting order ranking rank