3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

毎日自動でStackSatsしよう:Bybit編

Last updated at Posted at 2024-01-28

趣旨

とりあえず動く最も簡単な部類のbotを作ってみようという企画です。

まったく同じことをコードを書かなくても取引所の標準機能で可能なので、はっきり言って無駄なんですが、cronの備忘録にもなりますし、初心者はこういうところから始めた方が頭に入ってきやすいと思うので公開します。

以下のコードを実行する場合は全て自己責任でお願い致します。
本記事は教育目的での共有であり、投資を勧誘するものではありません。

Pybit

まずはディレクトリを作成します。

mkdir stacksats
cd stacksats

次に仮想環境を用意します。
用意しなくてもコードは実行できますが、念のためにやっておいた方が良いです。

python3 -m venv myenv
source myenv/bin/activate

次にpybitというライブラリをインストールします。

pip install pybit

pybitを使って成行注文するコードを準備します。

market_order.py
from pybit.unified_trading import HTTP
import config

session = HTTP(
    testnet=False,
    api_key=config.bybit_spot_api_key,
    api_secret=config.bybit_spot_secret_key,
)
response = session.place_order(
    category="spot",
    symbol="BTCUSDC",
    side="Buy",
    orderType="Market",
    qty="", #イコールの右側のダブルクオーテーションの中にDCAしたいBTC量に書きます
    )
print(response)

config.pyに直接API情報を書くのは推奨しませんが、ここでは例なのでそのまま書きます。
実際にコードを動かすときは暗号化した方が良いです。

config.py
bybit_api_key = ''
bybit_secret_key = ''

Cron設定

定時に特定のプログラムを自動で起動させる場合に使える機能です。
以下、使い方を記述します。

crontabを開きます。

crontab -e

起動させる条件を記載します。
ログも残すようにセットします。
以下の設定だと毎日0時に起動されます。

* 0 * * * /home/user/stacksats/market_order.py >> /home/user/stacksats/cronjob_bot.log 2>&1

アスタリスクはそれぞれ以下の時間を表します。
[minute] [hour] [day of the month] [month] [day of the week]

以下のコマンドでファイルの編集権限を変更します。

* chmod +x /home/user/market_order.py

chmodはchange modeを表し、ファイルやディレクトリのアクセス権限を変更するコマンドです。
+xで execute権限を付与します。

cronを起動させます。

sudo systemctl status cron
sudo systemctl start cron
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?