BIBOT
BIBOT
BIBOT
전체 방문자
오늘
어제
  • 분류 전체보기 (79)
    • 경제뉴스 일기 (11)
    • 바이낸스 (17)
    • 코딩 (15)
    • 기술적 트레이딩 (27)
    • 기타 (1)
    • 경제상식 (6)
    • 일상 (1)

블로그 메뉴

  • 홈
  • 선물거래 계산기
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 파이썬
  • 우분투
  • 서버
  • 파이썬 바이낸스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
BIBOT

BIBOT

바이낸스 선물 거래에서 사용할 수 있는 페어 트레이딩(Pair Trading) 전략의 예제
코딩

바이낸스 선물 거래에서 사용할 수 있는 페어 트레이딩(Pair Trading) 전략의 예제

2023. 3. 16. 00:29

 

바이낸스 선물거래에서 사용할 수 있는 페어트레이딩 전략(Pair Trading)의 예제입니다 ! 

 

import ccxt
import numpy as np
import pandas as pd

api_key = "your_api_key"
secret_key = "your_secret_key"

exchange = ccxt.binance({
    'apiKey': api_key,
    'secret': secret_key,
    'enableRateLimit': True,
})

def get_historical_data(pair, timeframe, since):
    bars = exchange.fetch_ohlcv(pair, timeframe, since)
    data = pd.DataFrame(bars, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
    return data

def calculate_spread(asset1, asset2):
    spread = asset1['close'] / asset2['close']
    return spread

def pair_trading(asset1_data, asset2_data, entry_threshold, exit_threshold, stop_loss, take_profit):
    spread = calculate_spread(asset1_data, asset2_data)
    spread_mean = spread.mean()
    spread_std = spread.std()

    long_entry = spread_mean + entry_threshold * spread_std
    short_entry = spread_mean - entry_threshold * spread_std

    long_exit = spread_mean + exit_threshold * spread_std
    short_exit = spread_mean - exit_threshold * spread_std

    position = 0
    entry_price = 0

    for i in range(len(asset1_data)):
        if position == 0:
            if spread[i] > long_entry:
                position = 1
                entry_price = spread[i]
                print("Long position opened at", entry_price)
            elif spread[i] < short_entry:
                position = -1
                entry_price = spread[i]
                print("Short position opened at", entry_price)

        elif position == 1:
            if spread[i] < long_exit or spread[i] < entry_price * (1 - stop_loss) or spread[i] > entry_price * (1 + take_profit):
                position = 0
                print("Long position closed at", spread[i])

        elif position == -1:
            if spread[i] > short_exit or spread[i] > entry_price * (1 + stop_loss) or spread[i] < entry_price * (1 - take_profit):
                position = 0
                print("Short position closed at", spread[i])

asset1 = "BTC/USDT"
asset2 = "ETH/USDT"
timeframe = "1h"
since = exchange.parse8601("2022-01-01T00:00:00Z")

asset1_data = get_historical_data(asset1, timeframe, since)
asset2_data = get_historical_data(asset2, timeframe, since)

entry_threshold = 1
exit_threshold = 0.5
stop_loss = 0.03
take_profit = 0.03

pair_trading(asset1_data, asset2_data, entry_threshold, exit_threshold, stop_loss, take_profit)

 

반응형
저작자표시 비영리 변경금지 (새창열림)
    '코딩' 카테고리의 다른 글
    • 텐서플로우로 1시간 뒤의 바이낸스 BTCUSDT 시가를 예측하는 프로그램 샘플 만들기
    • 파이썬으로 바이낸스 선물거래소에 있는 심볼 중 거래량과 가격 변동이 비슷한 심볼 5개를 찾는 방법
    • 24시간 작동하는 이동평균(Moving Average) 전략 바이낸스 선물거래 알고리즘 예시
    • 파이썬 - 엘리어트 파동이론(Elliot Wave Theory) 마지막 파동 출력하기
    BIBOT
    BIBOT

    티스토리툴바