Created
November 8, 2019 10:17
-
-
Save yuyasugano/c224a2d1a3602d52700950d9ed8d8231 to your computer and use it in GitHub Desktop.
Elasticsearch bitbank.cc API data crawling every second and SMA
This file contains hidden or 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
#!/usr/bin/python | |
import json | |
import time | |
from datetime import datetime | |
from elasticsearch import Elasticsearch | |
size = 5 | |
def print_search_stats(results): | |
print("=" * 80) | |
print("Total %d found in %dms" % (results["hits"]["total"]["value"], results["took"])) | |
print("-" * 80) | |
def print_hits(results): | |
" Simple utility function to print results of a search query. " | |
print_search_stats(results) | |
for hit in results["hits"]["hits"]: | |
print("/%s/%s/%s close: %s, volume: %s" % (hit["_index"], hit["_type"], hit["_id"], hit["_source"]["close"], hit["_source"]["volume"])) | |
print("=" * 80) | |
print() | |
def print_sma(results): | |
close = [results["hits"]["hits"][i]["_source"]["close"] for i in range(0, size)] | |
print("%d seconds Simple Moving Avarage: %f" % (size, sum(close)/len(close))) | |
def main(): | |
# by default we connect to localhost:9200 | |
es = Elasticsearch() | |
result= es.search( | |
index="btcjpy", | |
body={ | |
"size": size, | |
"query": { "match_all": {} }, | |
"sort": { "timestamp": { "order": "desc" } } | |
} | |
) | |
print_hits(result) | |
print_sma(result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment