如何用ChatGPT写一个OKX抢开盘脚本

本文所有代码全部都是chatgpt写的。
前两天留言问大家想要什么脚本,看到了有OKX开盘的,觉得这个不错

本文主要是教大家怎么描述需求让AI写代码~
代码块上加粗部分都是我的对话语句,代码文件全部chatgpt写的。

research this python sdk https://github.com/coinrising/okex-api-v5
Assist me in writing Python code that calls the OKX API and utilizes its SDK.

from okex.account_api import AccountAPI
from okex.market_data_api import MarketDataAPI
from okex.trade_api import TradeAPI
from okex.const import *

# Set up authentication credentials
api_key = 'YOUR_API_KEY'
secret_key = 'YOUR_SECRET_KEY'
passphrase = 'YOUR_PASSPHRASE'

# Create API clients
account_client = AccountAPI(api_key, secret_key, passphrase, True)
trade_client = TradeAPI(api_key, secret_key, passphrase, True)
market_data_client = MarketDataAPI()

# Get the latest market price of BTC
response = market_data_client.get_ticker('BTC-USDT')
latest_price = response['last']
print(f"The latest price of BTC is {latest_price} USDT")

首先让他学习了okx的文档,这里已经识别出来怎么调用okex的Api来进行市场操作,但是我们的需求是抢开盘,那么告诉他。

I want to monitor a token for its listing and buy it as soon as it is listed.

import schedule
import time
from okex.trade_api import TradeAPI
from okex.account_api import AccountAPI
from okex.const import *

# Set up authentication credentials
api_key = 'YOUR_API_KEY'
secret_key = 'YOUR_SECRET_KEY'
passphrase = 'YOUR_PASSPHRASE'

# Set up trade parameters
symbol = 'TOKEN-USDT'  # Replace with your desired token symbol
amount = 100  # Replace with your desired order amount in USDT

# Create API clients
account_client = AccountAPI(api_key, secret_key, passphrase, True)
trade_client = TradeAPI(api_key, secret_key, passphrase, True)

def buy_token():
    try:
        # Check if the token is listed
        response = trade_client.get_specific_public_symbol(symbol)
        if response['state'] == 'live':
            # If the token is listed, place a market order to buy it
            response = trade_client.place_market_order(symbol, SIDE_BUY, funds=amount)
            print(response)
    except Exception as e:
        print(f"An error occurred: {e}")

# Schedule the buy_token function to be called every minute
schedule.every(1).minutes.do(buy_token)

# Run the scheduler loop
while True:
    schedule.run_pending()
    time.sleep(1)

这里看到已经在监控代币是否上线了,但是购买的时候间隔太长,并且一直循环购买,我想要在过了某一时间段之后疯狂请求购买,并且都变成参数可调。

Modify to scan the token online at millisecond level after reaching a certain time, and purchase it immediately once it is available.Do not use schedule.every().day, directly start the program when judging that the time is greater than something.

import time
import argparse
from datetime import datetime
from okex.trade_api import TradeAPI
from okex.account_api import AccountAPI
from okex.const import *

# Set up authentication credentials
api_key = 'YOUR_API_KEY'
secret_key = 'YOUR_SECRET_KEY'
passphrase = 'YOUR_PASSPHRASE'

# Set up trade parameters
symbol = 'TOKEN-USDT'  # Replace with your desired token symbol
amount = 100  # Replace with your desired order amount in USDT

# Create API clients
account_client = AccountAPI(api_key, secret_key, passphrase, True)
trade_client = TradeAPI(api_key, secret_key, passphrase, True)

def buy_token():
    try:
        # Check if the token is listed
        response = trade_client.get_specific_public_symbol(symbol)
        if response['state'] == 'live':
            # If the token is listed, place a market order to buy it
            response = trade_client.place_market_order(symbol, SIDE_BUY, funds=amount)
            print(response)
            return True
    except Exception as e:
        print(f"An error occurred: {e}")
        return False

if __name__ == '__main__':
    # Parse command-line arguments
    parser = argparse.ArgumentParser(description='Monitor token listing and buy immediately')
    parser.add_argument('year', type=int, help='Listing year')
    parser.add_argument('month', type=int, help='Listing month')
    parser.add_argument('day', type=int, help='Listing day')
    parser.add_argument('hour', type=int, help='Listing hour')
    parser.add_argument('minute', type=int, help='Listing minute')
    parser.add_argument('second', type=int, help='Listing second')
    args = parser.parse_args()

    # Convert command-line arguments to datetime object
    listing_time = datetime(args.year, args.month, args.day, args.hour, args.minute, args.second)

    # Monitor the token listing
    while datetime.now() < listing_time:
        time.sleep(1)

    while not buy_token():
        time.sleep(0.1)

这里已经基本上可用了,能监控交易对,能配置什么时候开始,(咱们只需要配置在开盘前3分钟就疯狂监控就可以了)但是我想让他更快一点呢?多线程疯狂请求。

Can we enable multi-threading during purchase to make it faster?

import time
import argparse
from datetime import datetime
from threading import Thread
from okex.trade_api import TradeAPI
from okex.account_api import AccountAPI
from okex.const import *

# Set up authentication credentials
api_key = 'YOUR_API_KEY'
secret_key = 'YOUR_SECRET_KEY'
passphrase = 'YOUR_PASSPHRASE'

# Set up trade parameters
symbol = 'TOKEN-USDT'  # Replace with your desired token symbol
amount = 100  # Replace with your desired order amount in USDT
num_threads = 5  # Replace with the number of threads to use

# Create API clients
account_client = AccountAPI(api_key, secret_key, passphrase, True)
trade_client = TradeAPI(api_key, secret_key, passphrase, True)

def buy_token():
    try:
        # Check if the token is listed
        response = trade_client.get_specific_public_symbol(symbol)
        if response['state'] == 'live':
            # If the token is listed, place a market order to buy it
            response = trade_client.place_market_order(symbol, SIDE_BUY, funds=amount)
            print(response)
            return True
    except Exception as e:
        print(f"An error occurred: {e}")
        return False

if __name__ == '__main__':
    # Parse command-line arguments
    parser = argparse.ArgumentParser(description='Monitor token listing and buy immediately')
    parser.add_argument('year', type=int, help='Listing year')
    parser.add_argument('month', type=int, help='Listing month')
    parser.add_argument('day', type=int, help='Listing day')
    parser.add_argument('hour', type=int, help='Listing hour')
    parser.add_argument('minute', type=int, help='Listing minute')
    parser.add_argument('second', type=int, help='Listing second')
    args = parser.parse_args()

    # Convert command-line arguments to datetime object
    listing_time = datetime(args.year, args.month, args.day, args.hour, args.minute, args.second)

    # Wait until the listing time
    while datetime.now() < listing_time:
        time.sleep(1)

    # Buy the token using multiple threads
    threads = []
    for i in range(num_threads):
        thread = Thread(target=buy_token)
        thread.start()
        threads.append(thread)

    # Wait for all threads to complete
    for thread in threads:
        thread.join()

到这里基本上逻辑看起来完全可以用了,只需要稍微修改一下配置。
问题来了,是不是真的可用呢?
答案是否的,虽然逻辑都看起来对了,但是这个是调用okex的sdk,okex并没有官方sdk,而都是社区自己写的,这个时候就需要去找sdk的api是不是跟代码里面调用的一样的呢,不一样的话就需要修改成一样的,这个就需要稍微懂技术的人来修改了。

综上可得,Chatgpt确实可以写代码,并且写的逻辑完全没问题,在写模块化组件,CRUD,常规的工具类方面很强,但是触及到各种第三方调用,整体架构之类的还需要程序员自己修改。当然也可以直接给他模版,让他学习。

Subscribe to Daniel
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.