Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might be relieved to know that there is already a python library that is optimized for rolling financial calculations... it is called <a href="http://pandas.pydata.org/" rel="nofollow noreferrer"><code>pandas</code></a>.</p> <p>I don't think <a href="https://stackoverflow.com/questions/10065051/python-pandas-and-databases-like-mysql">pandas will read from SQL yet</a>; however, <a href="http://pandas.pydata.org/" rel="nofollow noreferrer"><code>pandas</code></a> will read from a csv... I took the liberty of using csv data (which it seems you have stored in <code>ma.csv</code>)... once you have done that, getting a rolling 7-day average on your CLOSE is as simple as...</p> <pre><code>&gt;&gt;&gt; import pandas as pn &gt;&gt;&gt; from datetime import date &gt;&gt;&gt; df = pn.read_csv('fut.csv', index_col=2, parse_dates=[2]) &gt;&gt;&gt; pn.rolling_mean(df['CLOSE'], window=7) yyyymmdd 2005-03-29 NaN 2005-03-30 NaN 2005-03-31 NaN 2005-04-01 NaN 2005-04-04 NaN 2005-04-05 NaN 2005-04-06 0.121429 2005-04-07 0.120429 2005-04-08 0.119429 2005-04-11 0.118571 2005-04-12 0.117857 2005-04-13 0.117429 2005-04-14 0.117000 2005-04-15 0.116571 2005-04-18 0.116714 2005-04-19 0.117286 2005-04-20 0.117571 2005-04-21 0.117857 2005-04-22 0.118143 2005-04-25 0.118429 2005-04-26 0.118714 &gt;&gt;&gt; &gt;&gt;&gt; pn.rolling_mean(df['CLOSE'], window=7)[date(2005,4,26)] 0.11871428571428572 &gt;&gt;&gt; </code></pre> <p><code>df</code> above is a <a href="http://pandas.pydata.org/" rel="nofollow noreferrer"><code>pandas</code></a> <a href="http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe" rel="nofollow noreferrer"><code>DataFrame</code></a>, which is a specialized structure for holding tables of time-indexed values associated with an object... in this case, the <code>DataFrame</code> holds your HIGH, LOW, CLOSE, etc...</p> <p>Besides making your job much easier, <a href="http://pandas.pydata.org/" rel="nofollow noreferrer"><code>pandas</code></a> also offloads most of the heaving lifting to Cython, which makes running thousands of these calculations rather fast.</p> <p><hr></p> <h2>fut.csv</h2> <pre><code>SYMBOL,DESCRIPTION,yyyymmdd,OPEN,HIGH,LOW,CLOSE,tmp1,tmp2,tmp3,tmp4 AC-057,Ethanol CBOT (Pit) Liq Cont,20050329,0.121,0.123,0.121,0.123,47,233,32,219 AC-057,Ethanol CBOT (Pit) Liq Cont,20050330,0.124,0.124,0.122,0.122,68,233,0,219 AC-057,Ethanol CBOT (Pit) Liq Cont,20050331,0.123,0.123,0.123,0.123,68,246,57,226 AC-057,Ethanol CBOT (Pit) Liq Cont,20050401,0.122,0.122,0.122,0.122,5,241,5,221 AC-057,Ethanol CBOT (Pit) Liq Cont,20050404,0.12,0.12,0.12,0.12,1,240,0,220 AC-057,Ethanol CBOT (Pit) Liq Cont,20050405,0.12,0.12,0.12,0.12,5,241,0,220 AC-057,Ethanol CBOT (Pit) Liq Cont,20050406,0.12,0.12,0.12,0.12,4,241,2,220 AC-057,Ethanol CBOT (Pit) Liq Cont,20050407,0.119,0.119,0.116,0.116,30,233,23,209 AC-057,Ethanol CBOT (Pit) Liq Cont,20050408,0.115,0.115,0.115,0.115,35,217,34,194 AC-057,Ethanol CBOT (Pit) Liq Cont,20050411,0.117,0.117,0.117,0.117,5,217,0,194 AC-057,Ethanol CBOT (Pit) Liq Cont,20050412,0.117,0.117,0.117,0.117,5,217,2,194 AC-057,Ethanol CBOT (Pit) Liq Cont,20050413,0.117,0.117,0.117,0.117,9,217,0,194 AC-057,Ethanol CBOT (Pit) Liq Cont,20050414,0.117,0.117,0.117,0.117,9,217,0,194 AC-057,Ethanol CBOT (Pit) Liq Cont,20050415,0.117,0.117,0.117,0.117,9,218,4,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050418,0.117,0.117,0.117,0.117,5,218,0,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050419,0.119,0.119,0.119,0.119,5,218,5,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050420,0.119,0.119,0.119,0.119,0,218,0,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050421,0.119,0.119,0.119,0.119,5,218,0,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050422,0.119,0.119,0.119,0.119,5,223,0,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050425,0.119,0.119,0.119,0.119,0,223,0,190 AC-057,Ethanol CBOT (Pit) Liq Cont,20050426,0.119,0.119,0.119,0.119,0,223,0,190 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload