Skip to content

Latest commit

 

History

History
92 lines (71 loc) · 2.75 KB

分享-赌徒策略-马丁策略.md

File metadata and controls

92 lines (71 loc) · 2.75 KB

Name

分享-赌徒策略-马丁策略

Author

盯盘狗 - 策略出租

Strategy Description

该策略使用了FMZ平台提供的Exchange API来进行交易。在主循环中,首先获取K线数据,然后获取当前价格。如果当前价格低于上一次买入价格的一定比例,则进行止损操作;如果当前价格高于上一次买入价格的一定比例,则进行止盈操作;如果当前没有持仓,则进行买入初始仓位操作;如果当前持仓数量小于设定的最大加仓次数,则进行加仓操作。最后等待下一次循环。需要注意的是,马丁策略存在一定的风险,需要谨慎操作。

Source (python)

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)

Detail

https://www.fmz.com/strategy/410114

Last Modified

2023-04-18 12:51:35