공유 - -마틴 전략

저자: 개 - 전략적 임대, 날짜: 2023-04-18 12:51:35
태그:

이 전략은 FMZ 플랫폼에서 제공하는 교환 API를 사용하여 거래를 수행합니다. 주주 라운드에서 먼저 K 라인 데이터를 얻고 현재 가격을 얻습니다. 현재 가격이 마지막 구매 가격의 특정 비율보다 낮으면 중단 손실 작전을 수행합니다. 현재 가격이 마지막 구매 가격의 특정 비율보다 높으면 중단 손실 작전을 수행합니다. 현재 보유가 없으면 구매 초기 포지션 작전을 수행합니다. 현재 보유가 최대 설정된 포지션 수보다 작다면 추가 포지션 작전을 수행합니다. 마지막으로 다음 라운드를 기다립니다. 주의해야 할 것은 마틴 전략이 특정 위험이 있으며 신중하게 작동해야한다는 것입니다.


import time

# 初始化策略参数
symbol = 'huobip/btc_usdt'
period = '1m'
amount = 0.01
martingale_factor = 2
max_martingale_times = 5
stop_loss = 0.05
stop_profit = 0.1
last_buy_price = 0
martingale_times = 0

# 连接API
exchange = Exchange()
exchange.SetContractType(symbol)
exchange.SetPeriod(period)

# 主循环
while True:
    # 获取K线数据
    klines = exchange.GetRecords()
    if not klines:
        continue

    # 获取当前价格
    current_price = float(klines[-1]['Close'])

    # 判断是否需要加仓
    if last_buy_price != 0 and current_price < last_buy_price * (1 - stop_loss):
        # 止损,卖出所有持仓
        sell_price = current_price
        sell_amount = exchange.GetPosition()['Amount']
        exchange.Sell(sell_price, sell_amount)
        last_buy_price = 0
        martingale_times = 0
        print('止损,卖出所有持仓,价格', sell_price)
    elif last_buy_price != 0 and current_price > last_buy_price * (1 + stop_profit):
        # 止盈,卖出所有持仓
        sell_price = current_price
        sell_amount = exchange.GetPosition()['Amount']
        exchange.Sell(sell_price, sell_amount)
        last_buy_price = 0
        martingale_times = 0
        print('止盈,卖出所有持仓,价格', sell_price)
    elif last_buy_price == 0:
        # 买入一份初始仓位
        buy_price = current_price
        buy_amount = amount / buy_price
        exchange.Buy(buy_price, buy_amount)
        last_buy_price = buy_price
        martingale_times = 0
        print('买入初始仓位,价格', buy_price)
    elif martingale_times < max_martingale_times:
        # 加仓
        buy_price = current_price * martingale_factor
        buy_amount = amount / buy_price
        exchange.Buy(buy_price, buy_amount)
        last_buy_price = (last_buy_price * martingale_times + buy_price) / (martingale_times + 1)
        martingale_times += 1
        print('加仓,价格', buy_price)

    # 等待下一次循环
    time.sleep(60)


더 많은