Comments (2)
Please see changes to WebSocketAPI.py
to add ltc support
import asyncio
import hashlib
import json
import websocket
import websockets
class OKCoinWSPublic:
Ticker = None
def init(self, pair):
self.pair = pair
@asyncio.coroutine
def initialize(self):
TickerFirstRun = True
while True:
if TickerFirstRun or not ws.open:
TickerFirstRun = False
if self.pair == 'btc_cny':
sockpair = 'btccny'
url = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
elif self.pair == 'btc_usd':
sockpair = 'btcusd'
url = "wss://real.okcoin.com:10440/websocket/okcoinapi"
elif self.pair == 'ltc_cny':
sockpair = 'ltccny'
url = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
elif self.pair == 'ltc_usd':
sockpair = 'ltcusd'
url = "wss://real.okcoin.com:10440/websocket/okcoinapi"
print('Connecting to Public OKCoin WebSocket...')
try:
ws = yield from websockets.connect(url)
# Ticker
yield from ws.send("{'event':'addChannel','channel':'ok_" + sockpair + "_ticker'}")
except Exception:
TickerFirstRun = True
OKCoinWSPublic.Ticker = yield from ws.recv()
class OKCoinWSPrivate:
TradeOrderID = None
def init(self, pair, api_key='', secret=''):
self.pair = pair
self.api_key = api_key
self.secret = secret
if self.pair == 'btc_cny':
self.url = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
elif self.pair == 'btc_usd':
self.url = "wss://real.okcoin.com:10440/websocket/okcoinapi"
elif self.pair == 'ltc_cny':
self.url = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
elif self.pair == 'ltc_usd':
self.url = "wss://real.okcoin.com:10440/websocket/okcoinapi"
print('Connecting to Private OKCoin WebSocket...')
notconnected = True
while notconnected:
try:
self.ws = websocket.create_connection(self.url)
notconnected = False
except Exception:
pass
def buildMySign(self, params, secretKey):
sign = ''
for key in sorted(params.keys()):
sign += key + '=' + str(params[key]) + '&'
data = sign + 'secret_key=' + secretKey
return hashlib.md5(data.encode("utf8")).hexdigest().upper()
def userinfo(self):
params = {'api_key': self.api_key}
sign = self.buildMySign(params, self.secret)
try:
self.ws.send("{'event':'addChannel', 'channel':'ok_spot" + self.pair[-3:] + "_userinfo',
'parameters':{ 'api_key':'" + self.api_key + "', 'sign':'" + sign + "'} }")
info = self.ws.recv()
except (websocket._exceptions.WebSocketTimeoutException, websocket._exceptions.WebSocketConnectionClosedException):
self.ws = websocket.create_connection(self.url)
self.ws.send("{'event':'addChannel', 'channel':'ok_spot" + self.pair[-3:] + "_userinfo',
'parameters':{ 'api_key':'" + self.api_key + "', 'sign':'" + sign + "'} }")
info = self.ws.recv()
return info
def cancelorder(self, order_id):
params = {'api_key': self.api_key,
'symbol': self.pair, 'order_id': order_id}
sign = self.buildMySign(params, self.secret)
try:
self.ws.send("{'event':'addChannel', 'channel':'ok_spot" + self.pair[-3:]
+
"_cancel_order', 'parameters':{ 'api_key':'" + self.api_key
+ "', 'sign':'" + sign + "', 'symbol':'" + self.pair
+ "', 'order_id':'" + order_id + "'} }")
# Don't muck up userinfo with executed order_id
self.ws.recv()
except (websocket._exceptions.WebSocketTimeoutException, websocket._exceptions.WebSocketConnectionClosedException):
self.ws = websocket.create_connection(self.url)
self.ws.send("{'event':'addChannel', 'channel':'ok_spot" + self.pair[-3:]
+
"_cancel_order', 'parameters':{ 'api_key':'" + self.api_key
+ "', 'sign':'" + sign + "', 'symbol':'" + self.pair
+ "', 'order_id':'" + order_id + "'} }")
# Don't muck up userinfo with executed order_id
self.ws.recv()
def trade(self, order, rate, amount):
params = {'api_key': self.api_key, 'symbol': self.pair,
'type': order, 'price': rate, 'amount': amount}
sign = self.buildMySign(params, self.secret)
try:
self.ws.send("{'event':'addChannel','channel':'ok_spot" + self.pair[-3:]
+ "_trade','parameters':{'api_key':'" + self.api_key
+ "','sign':'" + sign + "','symbol':'" + self.pair
+ "','type':'" + order + "','price':'"
+ str(rate) + "','amount':'" + str(amount) + "'}}")
OKCoinWSPrivate.TradeOrderID = json.loads(
self.ws.recv())[-1]['data']['order_id']
except (websocket._exceptions.WebSocketTimeoutException, websocket._exceptions.WebSocketConnectionClosedException):
self.ws = websocket.create_connection(self.url)
self.ws.send("{'event':'addChannel','channel':'ok_spot" + self.pair[-3:]
+ "_trade','parameters':{'api_key':'" + self.api_key
+ "','sign':'" + sign + "','symbol':'" + self.pair
+ "','type':'" + order + "','price':'"
+ str(rate) + "','amount':'" + str(amount) + "'}}")
OKCoinWSPrivate.TradeOrderID = json.loads(
self.ws.recv())[-1]['data']['order_id']
Subscribes to channel, updates on new trade. Not in use since we store
the order_id from trade
def realtrades(self):
params = {'api_key': self.api_key}
sign = self.buildMySign(params, self.secret)
self.ws.send("{'event':'addChannel','channel':'okspot_" + self.pair[-3:]
+ "_cancel_order','parameters':{'api_key':'"
+ self.api_key + "','sign':'" + sign + "', 'symbol':'"
+ self.pair + "', 'order_id':'-1'} }")
trades = self.ws.recv()
return trades
from avarice.
Thank you! I can't believe this slipped past me.
from avarice.
Related Issues (20)
- XMPP module import error HOT 9
- robust websocket connectivity HOT 5
- Fails to update the price HOT 10
- Request HOT 3
- Staging branch DMI issue. HOT 7
- OKCoin FIX support HOT 2
- Willingness to contribute HOT 6
- Major Rewrite in Progress HOT 20
- 5.0.1a Bug HOT 6
- Question - No new issue. :) HOT 3
- Possible Input bug HOT 1
- error when clean the database? HOT 1
- false order status or messed up messages
- fake order, report order successful but there is no such order placed.
- [BUG] Exception on Database
- [Issue?] XMPP Dependacies HOT 3
- [BUG] Exception with advanced strategy HOT 3
- WebSocket exception HOT 1
- The bot doesn't notice the price change (v5.0a2) HOT 1
- No longer being developed or maintained. Hello Rapacity HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from avarice.