new event first connect to send previous data on first connect
This commit is contained in:
parent
3f09e9fea8
commit
02678fd8e6
@ -107,6 +107,17 @@ class SymbolStatus:
|
|||||||
def last_positions(self) -> List[PositionWrapper]:
|
def last_positions(self) -> List[PositionWrapper]:
|
||||||
return self.positions[self.current_tick]
|
return self.positions[self.current_tick]
|
||||||
|
|
||||||
|
def all_ticks(self) -> List[int]:
|
||||||
|
return [x for x in range(self.current_tick)]
|
||||||
|
|
||||||
|
def all_prices(self) -> List[float]:
|
||||||
|
res = []
|
||||||
|
|
||||||
|
for x in range(1, self.current_tick):
|
||||||
|
res.append(self.prices[x])
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
# Applies strategy and adds position to list
|
# Applies strategy and adds position to list
|
||||||
async def add_position(self, position: Position):
|
async def add_position(self, position: Position):
|
||||||
pw = PositionWrapper(position)
|
pw = PositionWrapper(position)
|
||||||
|
21
main.py
21
main.py
@ -489,26 +489,27 @@ import os
|
|||||||
import threading
|
import threading
|
||||||
|
|
||||||
import dotenv
|
import dotenv
|
||||||
|
from flask import Flask, render_template
|
||||||
|
from flask_socketio import SocketIO
|
||||||
|
|
||||||
from bfxbot import BfxBot
|
from bfxbot import BfxBot
|
||||||
from bfxbot.currency import Symbol
|
from bfxbot.currency import Symbol
|
||||||
from bfxbot.models import EventHandler, PositionState, SymbolStatus, Event, EventKind
|
from bfxbot.models import SymbolStatus, Event, EventKind
|
||||||
from strategy import TrailingStopStrategy
|
from strategy import TrailingStopStrategy
|
||||||
|
|
||||||
from flask import Flask, render_template
|
|
||||||
from flask_socketio import SocketIO
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
socketio = SocketIO(app, async_mode="threading")
|
socketio = SocketIO(app, async_mode="threading")
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
|
|
||||||
bot = None
|
bot: BfxBot = None
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def entry():
|
def entry():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
|
|
||||||
# dotenv.load_dotenv()
|
# dotenv.load_dotenv()
|
||||||
|
|
||||||
# API_KEY = os.getenv("API_KEY")
|
# API_KEY = os.getenv("API_KEY")
|
||||||
@ -529,7 +530,6 @@ def entry():
|
|||||||
|
|
||||||
# while True:
|
# while True:
|
||||||
# await bot.update()
|
# await bot.update()
|
||||||
from flask import request
|
|
||||||
|
|
||||||
@socketio.on("close")
|
@socketio.on("close")
|
||||||
def on_message(message: dict):
|
def on_message(message: dict):
|
||||||
@ -537,6 +537,7 @@ def on_message(message: dict):
|
|||||||
|
|
||||||
print("I would close position {}".format(position_id))
|
print("I would close position {}".format(position_id))
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('connect')
|
@socketio.on('connect')
|
||||||
def start_bot():
|
def start_bot():
|
||||||
global bot
|
global bot
|
||||||
@ -547,13 +548,18 @@ def start_bot():
|
|||||||
API_SECRET = os.getenv("API_SECRET")
|
API_SECRET = os.getenv("API_SECRET")
|
||||||
|
|
||||||
if not bot:
|
if not bot:
|
||||||
bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, symbols=[Symbol.BTC], tick_duration=20)
|
bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, symbols=[Symbol.BTC], tick_duration=1)
|
||||||
strategy = TrailingStopStrategy()
|
strategy = TrailingStopStrategy()
|
||||||
bot.set_strategy(Symbol.BTC, strategy)
|
bot.set_strategy(Symbol.BTC, strategy)
|
||||||
|
|
||||||
threading.Thread(target=lambda: asyncio.run(bot_loop())).start()
|
threading.Thread(target=lambda: asyncio.run(bot_loop())).start()
|
||||||
print("Bot started.")
|
print("Bot started.")
|
||||||
|
|
||||||
|
socketio.emit("first_connect",
|
||||||
|
{"ticks": bot.status[Symbol.BTC].all_ticks(),
|
||||||
|
"prices": bot.status[Symbol.BTC].all_prices()})
|
||||||
|
|
||||||
|
|
||||||
async def bot_loop():
|
async def bot_loop():
|
||||||
global bot
|
global bot
|
||||||
|
|
||||||
@ -568,5 +574,6 @@ async def bot_loop():
|
|||||||
while True:
|
while True:
|
||||||
await bot.update()
|
await bot.update()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
socketio.run(app, debug=True)
|
socketio.run(app, debug=True)
|
||||||
|
@ -5,6 +5,10 @@ import Plot from "react-plotly.js"
|
|||||||
import { NewTickData, socket } from '../';
|
import { NewTickData, socket } from '../';
|
||||||
|
|
||||||
|
|
||||||
|
type FirstConnectData = {
|
||||||
|
ticks: Array<number>,
|
||||||
|
prices: Array<number>
|
||||||
|
}
|
||||||
|
|
||||||
type PlotState = {
|
type PlotState = {
|
||||||
x: Array<number>,
|
x: Array<number>,
|
||||||
@ -22,6 +26,13 @@ class RPlot extends Component<{}, PlotState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
socket.on("first_connect", (data: FirstConnectData) => {
|
||||||
|
this.setState({
|
||||||
|
x: data.ticks,
|
||||||
|
y: data.prices
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
socket.on("new_tick", (data: NewTickData) => {
|
socket.on("new_tick", (data: NewTickData) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
x: [...this.state.x, data.tick],
|
x: [...this.state.x, data.tick],
|
||||||
|
Loading…
Reference in New Issue
Block a user