今回は作業メモです。
BitMEXへの注文履歴をMySQLに保存する処理をDjangoで実装するための準備です。
Djangoのmodelを利用します。
Contents
DjangoのModelを作成する
models.pyに以下の記載を追加します。
class Bitmexmanage(models.Model):
status = models.CharField(max_length=20, null=True)
count_open_ordered = models.IntegerField(null=True)
count_close_ordered = models.IntegerField(null=True)
total_profit = models.DecimalField(max_digits=15, decimal_places=8, null=True)
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(auto_now=True)
class Bitmexorder(models.Model):
manage_id = models.IntegerField()
open_order_id = models.IntegerField(null=True)
account_id = models.IntegerField()
product_code = models.CharField(max_length=10, null=True)
order_type = models.CharField(max_length=20, null=True)
order_side = models.CharField(max_length=4, null=True)
amount = models.DecimalField(max_digits=12, decimal_places=8, null=True)
price = models.DecimalField(max_digits=15, decimal_places=8, null=True)
price_result = models.DecimalField(max_digits=15, decimal_places=8, null=True)
price_profit = models.DecimalField(max_digits=15, decimal_places=8, null=True)
price_loss = models.DecimalField(max_digits=15, decimal_places=8, null=True)
message = models.TextField(null=True)
status = models.CharField(max_length=20, null=True)
profit_ordered = models.SmallIntegerField(null=True)
logic_started = models.CharField(max_length=20, null=True)
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(auto_now=True)
モデルを有効にする(makemigrationsを実行する)
モデルを作成するために以下のコマンドを実行します。
python manage.py makemigrations trades
ここのtradesはアプリケーション名です。
以下、実行結果です。
Migrations for 'trades':
trades/migrations/0008_bitmexmanage_bitmexorder.py
- Create model Bitmexmanage
- Create model Bitmexorder
続いてテーブルを作成するSQLを確認するためにsqlmigrateを実行します。
最後の0008は、上記のmakemigrationsを行ったときの数字です。
sqlmigrateの実行ではまだテーブルは作成されません。
python manage.py sqlmigrate trades 0008
以下は実行結果です。
BEGIN; -- -- Create model Bitmexmanage -- CREATE TABLE `trades_bitmexmanage` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `status` varchar(20) NULL, `count_open_ordered` integer NOT NULL, `count_close_ordered` integer NOT NULL, `total_profit` numeric(15, 8) NULL, `created_at` datetime(6) NOT NULL, `updated_at` datetime(6) NOT NULL); -- -- Create model Bitmexorder -- CREATE TABLE `trades_bitmexorder` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `manage_id` integer NOT NULL, `open_order_id` integer NOT NULL, `account_id` integer NOT NULL, `product_code` varchar(10) NULL, `order_type` varchar(20) NULL, `order_side` varchar(4) NULL, `amount` numeric(12, 8) NULL, `price` numeric(15, 8) NULL, `price_result` numeric(15, 8) NULL, `price_profit` numeric(15, 8) NULL, `price_loss` numeric(15, 8) NULL, `message` longtext NULL, `status` varchar(20) NULL, `profit_ordered` smallint NULL, `logic_started` varchar(20) NULL, `created_at` datetime(6) NOT NULL, `updated_at` datetime(6) NOT NULL); COMMIT;
上記がOKであれば、テーブルを作成します。
migrateを実行します。
python manage.py migrate
実行結果です。
DBを確認したところ、trades_bitmexmanageとtrades_bitmexorderが作成されていたことを確認できました。
Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, trades Running migrations: Applying trades.0008_bitmexmanage_bitmexorder... OK


