main code cleanup
This commit is contained in:
		
							parent
							
								
									7fafdc6293
								
							
						
					
					
						commit
						f930a849f0
					
				
							
								
								
									
										81
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										81
									
								
								main.py
									
									
									
									
									
								
							| @ -14,19 +14,45 @@ from bfxbot.currency import Symbol | |||||||
| from bfxbot.models import PositionWrapper, SymbolStatus, Event, EventKind | from bfxbot.models import PositionWrapper, SymbolStatus, Event, EventKind | ||||||
| from strategy import TrailingStopStrategy | from strategy import TrailingStopStrategy | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | async def bot_loop(): | ||||||
|  |     await bot.start() | ||||||
|  | 
 | ||||||
|  |     while True: | ||||||
|  |         await bot.update() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | asyncio.new_event_loop() | ||||||
|  | 
 | ||||||
|  | dotenv.load_dotenv() | ||||||
|  | 
 | ||||||
|  | API_KEY = os.getenv("API_KEY") | ||||||
|  | API_SECRET = os.getenv("API_SECRET") | ||||||
|  | 
 | ||||||
| app = Flask(__name__) | app = Flask(__name__) | ||||||
| socketio = SocketIO(app, async_mode="threading") | socketio = SocketIO(app, async_mode="threading") | ||||||
| dotenv.load_dotenv() | bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, | ||||||
| loop = asyncio.new_event_loop() |              symbols=[Symbol.BTC], tick_duration=20) | ||||||
|  | bot.set_strategy(Symbol.BTC, TrailingStopStrategy()) | ||||||
|  | btc_eh = bot.event_handler(Symbol.BTC) | ||||||
| 
 | 
 | ||||||
| bot: BfxBot = None | # initializing and starting bot on other thread | ||||||
|  | threading.Thread(target=lambda: asyncio.run(bot_loop())).start() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | ################################### | ||||||
|  | # Flask callbacks | ||||||
|  | ################################### | ||||||
|  | 
 | ||||||
| @app.route('/') | @app.route('/') | ||||||
| def entry(): | def entry(): | ||||||
|     return render_template('index.html') |     return render_template('index.html') | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | ################################### | ||||||
|  | # Socker.IO callbacks | ||||||
|  | ################################### | ||||||
|  | 
 | ||||||
| @socketio.on("close") | @socketio.on("close") | ||||||
| def on_message(message: dict): | def on_message(message: dict): | ||||||
|     position_id = message['id'] |     position_id = message['id'] | ||||||
| @ -35,52 +61,35 @@ def on_message(message: dict): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @socketio.on('connect') | @socketio.on('connect') | ||||||
| def start_bot(): | def on_connect(): | ||||||
|     global bot |  | ||||||
| 
 |  | ||||||
|     asyncio.set_event_loop(loop) |  | ||||||
| 
 |  | ||||||
|     API_KEY = os.getenv("API_KEY") |  | ||||||
|     API_SECRET = os.getenv("API_SECRET") |  | ||||||
| 
 |  | ||||||
|     if not bot: |  | ||||||
|         bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, |  | ||||||
|                      symbols=[Symbol.BTC], tick_duration=20) |  | ||||||
|         strategy = TrailingStopStrategy() |  | ||||||
|         bot.set_strategy(Symbol.BTC, strategy) |  | ||||||
| 
 |  | ||||||
|         threading.Thread(target=lambda: asyncio.run(bot_loop())).start() |  | ||||||
|         print("Bot started.") |  | ||||||
| 
 |  | ||||||
|     socketio.emit("first_connect", |     socketio.emit("first_connect", | ||||||
|                   {"ticks": bot.status[Symbol.BTC].all_ticks(), |                   {"ticks": bot.status[Symbol.BTC].all_ticks(), | ||||||
|                    "prices": bot.status[Symbol.BTC].all_prices()}) |                    "prices": bot.status[Symbol.BTC].all_prices()}) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| async def bot_loop(): | ################################### | ||||||
|     global bot | # Bot callbacks | ||||||
|  | ################################### | ||||||
| 
 | 
 | ||||||
|     eh = bot.event_handler(Symbol.BTC) | @btc_eh.on_event(EventKind.NEW_TICK) | ||||||
|  | def on_new_tick(event: Event, status: SymbolStatus): | ||||||
|  |     # TODO: finalize JSON structure | ||||||
|  |     def pos_to_json(pw: PositionWrapper): | ||||||
|  |         return { | ||||||
|  |             "id": pw.position.id, | ||||||
|  |             "symbol": pw.position.symbol, | ||||||
|  |             "profit_loss": pw.position.profit_loss, | ||||||
|  |             "profit_loss_percentage": pw.position.profit_loss_percentage | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|     @eh.on_event(EventKind.NEW_TICK) |  | ||||||
|     def on_new_tick(event: Event, status: SymbolStatus): |  | ||||||
|     tick = event.tick |     tick = event.tick | ||||||
|     price = status.prices[event.tick] |     price = status.prices[event.tick] | ||||||
|         positions: List[PositionWrapper] = status.positions[event.tick] if event.tick in status.positions else [] |  | ||||||
| 
 | 
 | ||||||
|         # wrapping into json |     positions: List[PositionWrapper] = status.positions[event.tick] if event.tick in status.positions else [] | ||||||
|         positions = list( |  | ||||||
|             map(lambda x: {"id": x.position.id, "symbol": x.position.symbol, "profit_loss": x.position.profit_loss, |  | ||||||
|                            "profit_loss_percentage": x.position.profit_loss_percentage}, positions)) |  | ||||||
| 
 | 
 | ||||||
|     socketio.emit("new_tick", {"tick": tick, |     socketio.emit("new_tick", {"tick": tick, | ||||||
|                                "price": price, |                                "price": price, | ||||||
|                                    "positions": positions}) |                                "positions": list(map(pos_to_json, positions))}) | ||||||
| 
 |  | ||||||
|     await bot.start() |  | ||||||
| 
 |  | ||||||
|     while True: |  | ||||||
|         await bot.update() |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user