Created
March 18, 2013 17:14
-
-
Save JerzySpendel/5188930 to your computer and use it in GitHub Desktop.
Program sejcie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import os | |
import sqlite3 | |
from datetime import datetime | |
from PyQt4.QtGui import * | |
from PyQt4.QtCore import * | |
import os | |
class Show(QMainWindow): | |
pass | |
class Data(object): | |
where = os.path.expanduser('~') | |
def __init__(self): | |
r = requests.get('https://pln.bitcurex.com/data/orderbook.json').text | |
r = json.loads(r) | |
self.bids = r['bids'] | |
self.asks = r['asks'] | |
self.checkDirectory() | |
self.checkHistory() | |
def fillWithTables(self): | |
if os.path.getsize(Data.where+'/.stock/history.db') == 0: | |
c = self.conn.cursor() | |
c.execute('create table bids(id integer primary key asc,amount real,price real,data datetime);') | |
c.execute('create table asks(id integer primary key asc,amount real,price real,data datetime);') | |
self.conn.commit() | |
c.close() | |
def updateBA(self): | |
r = requests.get('https://pln.bitcurex.com/data/orderbook.json').text | |
r = json.loads(r) | |
self.bids = r['bids'] | |
self.asks = r['asks'] | |
def updateD(self): | |
self.updateBA() | |
data = datetime.now() | |
c = self.conn.cursor() | |
for bid in self.bids: | |
if float(bid[1]) >=0.5: | |
c.execute("insert into bids(amount,price,data) values (?,?,?)", (bid[1], bid[0], data)) | |
break | |
for ask in self.asks: | |
if float(ask[1])>=0.5: | |
c.execute("insert into asks(amount,price,data) values (?,?,?)", (ask[1], ask[0], data)) | |
break | |
self.conn.commit() | |
c.close() | |
def checkDirectory(self): | |
where = os.path.expanduser('~') | |
if not os.path.exists(where+'/.stock'): | |
os.mkdir(where+'/.stock') | |
def checkHistory(self): | |
where = os.path.expanduser('~') | |
if not os.path.exists(where+'/.stock/history.db'): | |
open(where+'/.stock/history.db','w+') | |
self.fillWithTables() | |
self.conn = sqlite3.connect(Data.where+'/.stock/history.db', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) | |
if os.path.getsize(where+'/.stock/history.db') == 0: | |
c = self.conn.cursor() | |
c.execute('create table bids(id integer primary key asc,amount real,price real,data datetime);') | |
c.execute('create table asks(id integer primary key asc,amount real,price real,data datetime);') | |
self.conn.commit() | |
c.close() | |
def calculatePanic(self): | |
c = self.conn.cursor() | |
c.execute('select * from bids where id = (select max(id) from bids)') | |
last = c.fetchone() | |
c.execute('select * from bids where id = (select max(id)-1 from bids)') | |
nlast = c.fetchone() | |
print(last) | |
print(nlast) | |
d1 = datetime.strptime(last[3],"%Y-%m-%d %H:%M:%S.%d") | |
d2 = datetime.strptime(nlast[3],"%Y-%m-%d %H:%M:%S.%d") | |
dt = d1-d2 | |
data = Data() | |
data.updateD() | |
data.calculatePanic() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment