[PYTHON] Operation of virtual currency automatic trading script

Be in trouble

The automatic trading script currently in operation is not stable enough to be executed stably and permanently with a while statement. I'm on a business trip from tomorrow, but I want to keep the script running even while I'm on a business trip. But I don't want to carry a PC with me. I want to complete it by operating on the iphone.

while True:

Reasons for unstable operation

  1. REST API error
  2. Realtime API error
  3. For some reason, as a result of the market order, the amount of open interest of 0.01 BTC or less remains, and we continue to wait for the contract. → Regarding this, if you are waiting for the execution of a market order for 100 seconds or more for the time being, an exception will be thrown and it will end abnormally.

temporary solution

As for the fundamental countermeasures, we will consider so-called "operational coverage". (not good)

  1. If an exception is detected in the try statement, a LINE notification will be sent. For the implementation of LINE notification in python, refer to the following. Send notifications to LINE in Python https://qiita.com/analytics-hiro/items/e42f857bd6b40bc178a3
  2. The following LINE notification will come.
[Automatic trading completion notification] 2020-01-19 17:20:20.630975 The start process has terminated abnormally.
The number of transactions was three.
The details of the error are as follows.
{'parent_order_acceptance_id': 'JRF20200119-082028-389427'}None
  1. Log in to the exchange to settle open positions and cancel orders.
  2. SSH to the server running the script on Termius. (Embarrassingly, we learned for the first time that we can SSH from an iphone.) Regarding Termius, I referred to the following. How to ssh from iPhone using public key with terminal app https://itneko.com/iphone-ssh/
  3. Execute the shell for script execution on the SSH server.

rerun.sh


#!/bin/sh
cd ~/bf/code_bf/
source ~/bf/bin/activate
nohup python IFDOCOBOTv3.1.py &
  1. Log in to the exchange and confirm that automatic trading has resumed.

Future tasks

--Make a script that doesn't fall in the first place. (this) ――Since we have sent you a LINE notification, put the URL in the notification content so that you can re-execute with one click. (Can you do it with API Gateway?) --If the server on which the script is executed goes down, the above flow cannot handle it, so consider a countermeasure. (I first came up with the idea of using Lambda, but I can't run Lambda for a long time, so it's NG. Can I implement it with AWS Batch? --Since the name of the automatic trading script (IFDOCOBOTv3.1.py in the above sample) is changed frequently, the re-execution script will be modified each time. (I'm thinking of solving it around CodeDeploy and CodeBuild. I hope it can be re-executed from the URL in the first place) --I'm using an EC2 instance, but the Public IP changes due to a reboot, so if I accidentally reboot, I have to connect to the AWS Management Console when connecting from the iphone, which is troublesome (I can solve it by using Elastic IP, but I want to make it a habit to shut down the server when not in use, so NG * Elastic IP is subject to billing when the OS is stopped) ――I wrote various things, but there is no script that doesn't fall. You should create a mechanism that automatically re-executes on the assumption that it will fall. Initialization (Settlement of open interest and cancellation of order) Naturally, it is done automatically.

Recommended Posts

Operation of virtual currency automatic trading script
Operation memo of Conda virtual environment
Good exception handling learned from the automatic monitoring program of virtual currency
Automatic operation of Chrome with Python + Selenium + pandas
Performance evaluation of investment automatic trading bot using backtesting
[Python] Operation of enumerate
Basic operation of pandas
Basic operation of Pandas
Get the trading price of virtual currency and create a chart with API of Zaif exchange