안녕하세요

현재 주식차트 그래프의 추세선을 이용해서 코인 예약 매매 시스템을 만들고 있습니다.
그래프 라이브러리 중에 plotly 를 찾아서 해보고 있는데 제가 원하는 그래프는 아닙니다..
더 좋은 그래프 라이브러리를 찾는다면 바꿔서 진행 할 예정입니다.

python plotly 와 dash 를 이용해서 아래 이미지 처럼 candlestick(stockchart) 나타나게 만들어 보았습니다.

python plotly

import plotly.graph_objects as go
import dash
import pymysql
import dash_core_components as dcc
import dash_html_components as html
# DB connect
def db_con():
db = pymysql.connect(
host='localhost',
user='id',
password='pw',
charset='utf8mb4',
database='db_name',
)
return db
# call data function
def graph(sbl, rod):
data = dict()
data['Open'] = list()
data['Close'] = list()
data['High'] = list()
data['Low'] = list()
name_list = list()
db = db_con()
sql = "select open, close, high, low, dt from chart where symbol = %s and rods = %s"
curs = db.cursor()
curs.execute(sql, (sbl, rod))
rows = curs.fetchall()
# +--------+------+---------------------+----------+----------+----------+----------+
# | symbol | rods | dt | open | close | high | low |
# +--------+------+---------------------+----------+----------+----------+----------+
# | ETHBTC | day1 | 2020-05-25 00:00:00 | 0.022568 | 0.022682 | 0.022755 | 0.02249 |
# | ETHBTC | day1 | 2020-05-26 00:00:00 | 0.022691 | 0.023111 | 0.023236 | 0.022631 |
# | ETHBTC | day1 | 2020-05-27 00:00:00 | 0.023111 | 0.022818 | 0.023136 | 0.02265 |
# | ETHBTC | day1 | 2020-05-28 00:00:00 | 0.022814 | 0.022463 | 0.023018 | 0.02246 |
# | ETHBTC | day1 | 2020-05-29 00:00:00 | 0.022466 | 0.02239 | 0.022675 | 0.022137 |
# | ETHBTC | day1 | 2020-05-30 00:00:00 | 0.022414 | 0.023276 | 0.023428 | 0.022414 |
# | ETHBTC | day1 | 2020-05-31 00:00:00 | 0.02328 | 0.024915 | 0.024955 | 0.02327 |
# | ETHBTC | day1 | 2020-06-01 00:00:00 | 0.024915 | 0.024686 | 0.025576 | 0.024597 |
# | ETHBTC | day1 | 2020-06-02 00:00:00 | 0.024689 | 0.025044 | 0.02525 | 0.024462 |
# | ETHBTC | day1 | 2020-06-03 00:00:00 | 0.025049 | 0.024446 | 0.025219 | 0.024 |
# +--------+------+---------------------+----------+----------+----------+----------+
for row in rows:
# y축의 값이지만 ochl 의 값을 표시 해야 하기 때문에 아래와 같이 만들어 준다
data['Open'].append(row[0])
data['Close'].append(row[1])
data['High'].append(row[2])
data['Low'].append(row[3])
# x축
name_list.append(row[4])
db.close()
return data, name_list
# call data
data, name_list = graph('ETHBTC', 'min5')
# make graph
fig = go.Figure(data=[go.Candlestick(x=name_list,
open=data['Open'], high=data['High'],
low=data['Low'], close=data['Close'],
name = 'ETHBTC')])
# css import
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# dash
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# layout
app.layout = html.Div([
# dcc.Graph(figure=fig),
html.Div(
[
# 그래프 표시되기 전 로딩 표시
dcc.Loading(
id="loading-1",
# figure 의 값에 위에 만들어 놓은 그래프 fig
children=[html.Div(id='figure-container', children=[dcc.Graph(figure=fig)])],
type="circle"
),
],
),
])
# web server init
if __name__ == '__main__':
app.run_server(
port=8050,
host='0.0.0.0',
debug=False
)

 

sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install python3-pip

위 순서대로 해보시면 설치 되실 겁니다

ubuntu 18 버전과python 3.6 버전 pipenv 가상환경에서 진행 하였습니다.
아래 deribit 공식 git 과 api 문서 사이트를 참고 하여 만들었습니다.

https://github.com/deribit/deribit-api-python

 

deribit/deribit-api-python

Deribit API python client library https://www.deribit.com/docs/api/ - deribit/deribit-api-python

github.com

https://docs.deribit.com/#deribit-api-v2-0-0

 

Deribit API

 

docs.deribit.com

필요한 라이브러리
pip3 install websocket-client

import websocket
import json
import time
from datetime import datetime
def main():
# 거래소에서 발급 받은 publick, secret key
client_id = 'key'
client_secret = 'secret_key'
def on_message(ws, message):
# 아래 ws.send() 에서 보낸 return message 받는 func
msg = json.loads(message)
try:
price = msg['params']['data']['close']
# 현재 가격을 불러와 5000$ 이하일 경우 40$ 만큼 시장가로 주문
if price < 5000:
data = {
"method": "/private/buy",
"params": {
"amount": 40,
"instrument_name": "BTC-PERPETUAL",
"label": "market0000234"
"type": "market"
},
"jsonrpc": "2.0",
"id": 34
}
ws.send(json.dumps(data))
except:
pass
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
data = {
"id" : 27,
"method": "public/auth",
"params":{
"grant_type": "client_credentials",
"scope": "session:apiconsole-aiczudeyx0n",
"client_id": client_id,
"client_secret": client_secret,
},
"jsonrpc": "2.0"
}
# access token 값 받기
ws.send(json.dumps(data))
data = {
"method": "/private/subscribe",
"params": {
"channels": ["chart.trades.BTC-PERPETUAL.60"],
}
}
# 위에서 받아온 access token 값으로 private 값 불러 오기
ws.send(json.dumps(data))
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://www.deribit.com/ws/api/v2/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
main()

 

websocket 참고 사이트
https://pypi.org/project/websocket_client/

 

websocket_client

WebSocket client for Python. hybi13 is supported.

pypi.org

잘 못 되었거나 잘 모르시는 부분 있으시면 댓글 부탁 드립니다.

API 를 호출 하기 위해 request 를 하다 보면 언젠가 python requests.exceptions.ConnectionError 오류가 발생 할 수 있습니다.

이를 막고자 계속 실행하기 위해서는 대책이 필요한데요

저는 Max retries exceeded with url 오류 발생 시 urlopen 을 활용 하여 끊김 없이 데이터를 불러 오도록 만들어 보았습니다.

import json
import requests
from urllib.request import urlopen
# 저는 deribit 을 활용하고 있어서 아래 사이트로 예시를 들었습니다.
url = 'https://www.deribit.com/api/v2/public/get_last_trades_by_currency?currency=BTC'
while(True):
# 기존 request 로 가져오는 방식에서 아래 except 를 추가해서 urlopen 으로 크롤링 하여 가져오는 방법을 추가
try:
res = requests.get(url)
# requests 오류 시 아래 urlopen 을 사용 하여 data 불러 오는데 끊김이 없도록 실행
except:
page = urlopen(url)
# bytes to string
doc = page.read().decode('utf-8')
# string to dictionary
dic = json.loads(doc)
result_dict = dic['result']['trades']
# 오류가 없을 경우
else:
if res.status_code == 200:
result_dict = json.loads(res.text)['result']['trades']
else:
print(res.status_code)
continue
for i in result_dict:
...
view raw urlopen.py hosted with ❤ by GitHub

잘못되었거나 궁금하신 것 있으면 답글 부탁 드립니다.

감사합니다.

파이썬과 html 을 활용해서 간단하게 만들어 보았습니다.

집에 남는 PC를 삼바 서버로 활용하고 있어 외부에서도 삼바서버에 업로드 할 수 있게 만들어 보았습니다.

안되거나 궁금한 사항 있으시면 댓글 부탁 드립니다. 

 

1. python

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/')
def render_file():
return render_template('upload.html')
@app.route('/fileUpload', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
files = request.files
print(type(files))
print('----------')
for f in files.to_dict(flat=False)['filename[]']:
f.save('/opt/folder/aaa/'+secure_filename(f.filename))
print(f.filename)
return render_template('success.html')
if __name__ == '__main__':
app.run(host='192.168.0.112', port=8000, debug = True)
view raw file_upload.py hosted with ❤ by GitHub

2. html

<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
</head>
<body>
<div style="width:80%;text-align:center;margin:30px auto;">
<form action = "http://192.168.0.112:8000/fileUpload" method = "POST" enctype = "multipart/form-data">
<input style="width:80%;padding-bottom:20px;height:22%" multiple="multiple" type = "file" name = "filename[]" />
<input style="width:80%;height:10%" type = "submit" />
</form>
</div>
</body>
</html>
view raw upload.html hosted with ❤ by GitHub

코인 거래소 API 를 websocket 으로 받오는 중에 프로세스가 자꾸 죽어서 아래와 같은 해결 방법을 아래와 같이 찾았습니다. 내용을 더 찾아보고 싶으신 분은 https://www.programcreek.com/python/example/7545/os.execl 여기서 확인 하시면 됩니다 !

사용법은 간단하게 작성 하였습니다. 메인 함수가 실행 되는 위치에 아래 소스를 넣으시면 됩니다.

os.execl(sys.executable, sys.executable, *sys.argv)

예제)

import os
def mais():
...
if __name__ == "__main__":
main()
os.execl(sys.executable, sys.executable, *sys.argv)
view raw os.execl hosted with ❤ by GitHub

 

Jupyter notebook 을 사용 할 때 for 문이 얼마나 진행 되었는지 눈으로 확인 하기쥬피터 노트북을 사용 할 때 for 문이 얼마나 진행 되었는지 눈으로 확인이
필요 할 때가 많죠? 그럴 때 tqdm 을 쓰시면 됩니다.

 

# 1. tqdm 선언 (없으면 설치)
from tqdm import tqdm_notebook, tqdm

# 기본 형
list_a = ['a', 'b', 'c', 'd', 'e']
for li in tqdm_notebook(list_a):
	print(li)
    
# 함수 안에서 tqdm 사용하기
def def_a(items, tqdm=None, **tqdm_params):
    
    list_data = list()
    if tqdm:
        items = tqdm(items, **tqdm_params)
    
    for item in items:
    	list_data.append(item)
    
	return list_data        

list_return = def_a(list_a, tqdm = tqdm_notebook)

python 실행 시 ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets'

위와 같이 에러 메시지가 나온다면

pip3 install PyQtWebEngine 혹은 pip install PyQtWebEnginePyQtWebEngine 을 install 해주시면 됩니다.

안되시면 답글 부탁 드려요

command 창을 띄우신 다음 (windows key + r => cmd)

python -m site --user-site

를 입력 하시면 아래 이미지 처럼 경로를 찾을 수 있습니다.

시드 머니를 1000불로 계획하고 룰렛을 마틴(게일)베팅법으로 베팅 했을 때 ,

초기 베팅 금액 별 이길 확률과 평균 베팅 횟수를 python 으로 구현 해 보았습니다.

(룰렛에 0은 포함 00은 미포함)

결론은 1000불을 가지고 룰렛 number, color 베팅을 할 경우

7불, 15불을 초기 배팅금으로 잡고 시작 하는게 가장 승률이 높았습니다.

베팅 횟 수까지 고려 한다면 15불로 시작 하는게 가장 이상적인 것 같습니다(저의 의견).

!!! 절대 베팅을 권고하는 글은 아닙니다. 심심풀이로 만들어 보았을 뿐입니다

base_bet_money: $, win_rate: 승률, betting_count: 베팅 횟수

!! 제가 코드를 잘 못 짰을 수도 있어서 코드도 마지막에 첨부 하였습니다.

for bet_origin in tqdm_notebook(range(1,51)):
    idx = 0
    win = 0
    lose = 0
    total = 0
    
    while True:
        money = 1000
        bet = bet_origin
        i = 0
        hole = 0
        zzac = 0
        while True:
            a = random.randint(0,36)

            if a == 0:
                money -= bet
                bet = bet*2
            elif a%2 == 0:
                money += bet
                bet = bet_origin
                zzac += 1
            else:
                money -= bet
                bet = bet*2
                hole += 1

            i += 1
            if money <= bet:
                lose += 1
                break

            if money >= 1100:
                total += i
                win += 1
                break

        idx += 1

        if idx >= 100000:
            break

    print(bet_origin, win/1000, total/100000)

+ Recent posts