Need Assistance! Tweaking iteration(Looping) through multiple columns in SQLITE3 PYTHON
I have the following code below: It iterates through three columns grabbing the date from the Date (Column), high value from Vix_High (Column), Low value from Vix_Low(column). When the next value is lower or higher it replaces them until the difference between the values is >0.16 then it saves these values and the associated dates in a new table. This part all works. And I am happy I have been able to get this far as I am new to Python.
I have one issue that needs tweaking and I feels stuck. The code iterates through about 5000 rows through these three columns starting at the latest date back in 2013. I need it so that the high value that is saved is atleast 1 day after the low value, it could be more but it can’t be before.
example: 2014-01-02 13.24 2014-01-15 18.69 – Correct
example: 2015-03-25 12.11 2015-03-08 17.55 – Incorrect
Right now my program has both. I need it so Once the local Low value is found – only then can the high value start to be found following that date. When the difference between the two is >0.16 it can deposit the data and then start where it left off – finding the low value and then finding the local high value. Any Suggestions?
import sqlite3 import datetime conn = sqlite3.connect('VIX.db') cur = conn.cursor() cur.execute('CREATE TABLE IF NOT EXISTS VIX_ANALYSIS (LLDate integer, Lower_Limit real, ULDATE integer, Upper_Limit real)') conn.commit() cur.execute("SELECT COUNT(Date) FROM VIX") row_count=cur.fetchone()[0] print (row_count) #lowhigh_list=[] def vix_analysis(): high=1.3 low=99.9 index=0 while index < row_count: cur.execute("SELECT Date FROM VIX") local_date=cur.fetchall()[index] cur.execute("SELECT Vix_Low FROM VIX") local_low=cur.fetchall()[index] cur.execute("SELECT Vix_High FROM VIX") local_high=cur.fetchall()[index+1] cur.execute("SELECT Date FROM VIX") local_hdate=cur.fetchall()[index+1] print (local_date, local_high, local_low) if local_low[0] <= low: low = local_low[0] date_low=local_date[0] else: low=low if local_high[0] >= high: high = local_high[0] date_high=local_hdate[0] else: high=high if 8 <= low <= 10 and (high - low)/low >= 0.31: cur.execute('INSERT INTO VIX_ANALYSIS(LLDate, Lower_Limit, ULDATE, Upper_Limit) Values(?,?,?,?)',(date_low, low, date_high, high)) conn.commit() high=1.3 low=99.9 elif 10 < low <= 12 and (high - low)/low >= 0.26: cur.execute('INSERT INTO VIX_ANALYSIS(LLDate, Lower_Limit, ULDATE, Upper_Limit) Values(?,?,?,?)',(date_low, low, date_high, high)) conn.commit() high=1.3 low=99.9 elif 12 < low <= 16 and (high - low)/low >= 0.23: cur.execute('INSERT INTO VIX_ANALYSIS(LLDate, Lower_Limit, ULDATE, Upper_Limit) Values(?,?,?,?)',(date_low, low, date_high, high)) conn.commit() high=1.3 low=99.9 elif 16 < low <= 19 and (high - low)/low >= 0.19: cur.execute('INSERT INTO VIX_ANALYSIS(LLDate, Lower_Limit, ULDATE, Upper_Limit) Values(?,?,?,?)',(date_low, low, date_high, high)) conn.commit() high=1.3 low=99.9 elif low > 19 and (high - low)/low >= 0.16: cur.execute('INSERT INTO VIX_ANALYSIS(LLDate, Lower_Limit, ULDATE, Upper_Limit) Values(?,?,?,?)',(low, date_low, high, date_high)) conn.commit() high=1.3 low=99.9 else: low=low high=high print (high, date_high, low, date_low) index+=1 vix_analysis()