Nghiên cứu về Binance Futures Multi-currency Hedging Strategy Phần 4

Tác giả:Tốt, Tạo: 2020-05-14 15:18:56, Cập nhật: 2023-11-04 19:51:33

img

Binance tương lai chiến lược phòng hộ đa tiền tệs đánh giá gần đây và các kết quả kiểm tra lại K-line cấp phút

Ba báo cáo nghiên cứu về chiến lược phòng hộ đa tiền tệ của Binance đã được xuất bản, đây là báo cáo thứ tư.

Nghiên cứu về Binance Futures Chiến lược phòng hộ đa loại tiền tệ Phần 1:https://www.fmz.com/digest-topic/5584

Nghiên cứu về Binance Futures Chiến lược phòng hộ đa tiền tệ Phần 2:https://www.fmz.com/digest-topic/5588

Nghiên cứu về Binance Futures Chiến lược phòng hộ đa tiền tệ Phần 3:https://www.fmz.com/digest-topic/5605

Bài viết này là để xem xét tình hình thị trường thực tế trong tuần gần đây, và tóm tắt lợi nhuận và tổn thất. Kể từ khi thu thập dữ liệu đường K phút của Binance Futures trong hai tháng qua, kết quả backtest đường 1h K ban đầu có thể được cập nhật, có thể giải thích tốt hơn ý nghĩa của một số cài đặt tham số.

# Libraries to import
import pandas as pd
import requests
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
symbols = ['BTC','ETH', 'BCH', 'XRP', 'EOS', 'LTC', 'TRX', 'ETC', 'LINK', 'XLM', 'ADA', 'XMR', 'DASH', 'ZEC', 'XTZ', 'BNB', 'ATOM', 'ONT', 'IOTA', 'BAT', 'VET', 'NEO', 'QTUM', 'IOST']

Dữ liệu dòng K cấp phút

Dữ liệu từ ngày 21 tháng 2 đến ngày 15 tháng 4 lúc 2 giờ chiều, tổng cộng 77160 * 24, làm giảm đáng kể tốc độ backtest của chúng tôi, công cụ backtest không đủ hiệu quả, bạn có thể tự tối ưu hóa nó.

price_usdt = pd.read_csv('https://www.fmz.com/upload/asset/2b1fa7ab641385067ad.csv',index_col = 0)
price_usdt.shape
(77160, 24)
price_usdt.index = pd.to_datetime(price_usdt.index,unit='ms')
price_usdt_norm = price_usdt/price_usdt.fillna(method='bfill').iloc[0,]
price_usdt_btc = price_usdt.divide(price_usdt['BTC'],axis=0)
price_usdt_btc_norm = price_usdt_btc/price_usdt_btc.fillna(method='bfill').iloc[0,]
class Exchange:
    
    def __init__(self, trade_symbols, leverage=20, commission=0.00005,  initial_balance=10000, log=False):
        self.initial_balance = initial_balance # Initial asset
        self.commission = commission
        self.leverage = leverage
        self.trade_symbols = trade_symbols
        self.date = ''
        self.log = log
        self.df = pd.DataFrame(columns=['margin','total','leverage','realised_profit','unrealised_profit'])
        self.account = {'USDT':{'realised_profit':0, 'margin':0, 'unrealised_profit':0, 'total':initial_balance, 'leverage':0, 'fee':0}}
        for symbol in trade_symbols:
            self.account[symbol] = {'amount':0, 'hold_price':0, 'value':0, 'price':0, 'realised_profit':0, 'margin':0, 'unrealised_profit':0,'fee':0}
            
    def Trade(self, symbol, direction, price, amount, msg=''):
        if self.date and self.log:
            print('%-20s%-5s%-5s%-10.8s%-8.6s %s'%(str(self.date), symbol, 'buy' if direction == 1 else 'sell', price, amount, msg))
            
        cover_amount = 0 if direction*self.account[symbol]['amount'] >=0 else min(abs(self.account[symbol]['amount']), amount)
        open_amount = amount - cover_amount
        
        self.account['USDT']['realised_profit'] -= price*amount*self.commission # Minus handling fee
        self.account['USDT']['fee'] += price*amount*self.commission
        self.account[symbol]['fee'] += price*amount*self.commission
        
        if cover_amount > 0: # close position first
            self.account['USDT']['realised_profit'] += -direction*(price - self.account[symbol]['hold_price'])*cover_amount  # profit
            self.account['USDT']['margin'] -= cover_amount*self.account[symbol]['hold_price']/self.leverage # Free margin
            
            self.account[symbol]['realised_profit'] += -direction*(price - self.account[symbol]['hold_price'])*cover_amount
            self.account[symbol]['amount'] -= -direction*cover_amount
            self.account[symbol]['margin'] -=  cover_amount*self.account[symbol]['hold_price']/self.leverage
            self.account[symbol]['hold_price'] = 0 if self.account[symbol]['amount'] == 0 else self.account[symbol]['hold_price']
            
        if open_amount > 0:
            total_cost = self.account[symbol]['hold_price']*direction*self.account[symbol]['amount'] + price*open_amount
            total_amount = direction*self.account[symbol]['amount']+open_amount
            
            self.account['USDT']['margin'] +=  open_amount*price/self.leverage            
            self.account[symbol]['hold_price'] = total_cost/total_amount
            self.account[symbol]['amount'] += direction*open_amount
            self.account[symbol]['margin'] +=  open_amount*price/self.leverage
            
        self.account[symbol]['unrealised_profit'] = (price - self.account[symbol]['hold_price'])*self.account[symbol]['amount']
        self.account[symbol]['price'] = price
        self.account[symbol]['value'] = abs(self.account[symbol]['amount'])*price
        
        return True
    
    def Buy(self, symbol, price, amount, msg=''):
        self.Trade(symbol, 1, price, amount, msg)
        
    def Sell(self, symbol, price, amount, msg=''):
        self.Trade(symbol, -1, price, amount, msg)
        
    def Update(self, date, close_price): # Update assets
        self.date = date
        self.close = close_price
        self.account['USDT']['unrealised_profit'] = 0
        for symbol in self.trade_symbols:
            if np.isnan(close_price[symbol]):
                continue
            self.account[symbol]['unrealised_profit'] = (close_price[symbol] - self.account[symbol]['hold_price'])*self.account[symbol]['amount']
            self.account[symbol]['price'] = close_price[symbol]
            self.account[symbol]['value'] = abs(self.account[symbol]['amount'])*close_price[symbol]
            self.account['USDT']['unrealised_profit'] += self.account[symbol]['unrealised_profit']
        
        self.account['USDT']['total'] = round(self.account['USDT']['realised_profit'] + self.initial_balance + self.account['USDT']['unrealised_profit'],6)
        self.account['USDT']['leverage'] = round(self.account['USDT']['margin']/self.account['USDT']['total'],4)*self.leverage
        self.df.loc[self.date] = [self.account['USDT']['margin'],self.account['USDT']['total'],self.account['USDT']['leverage'],self.account['USDT']['realised_profit'],self.account['USDT']['unrealised_profit']]

Đánh giá tuần trước

Mã chiến lược được phát hành trong nhóm WeChat vào ngày 10 tháng Tư. Ban đầu, một nhóm người chạy chiến lược 2 ((nâng cao quá ngắn và giảm quá dài). Trong ba ngày đầu tiên, lợi nhuận rất tốt, và sự thoái lui rất thấp. trong những ngày tiếp theo, một số nhà giao dịch đã phóng đại đòn bẩy, một số thậm chí sử dụng toàn bộ số tiền của họ để hoạt động, và lợi nhuận đạt 10% trong một ngày. Strategy Square cũng phát hành rất nhiều chiến lược thị trường thực tế, nhiều người bắt đầu không hài lòng với các thông số khuyến nghị bảo thủ, và đã khuếch đại khối lượng giao dịch. Sau ngày 13 tháng Tư, do xu hướng độc lập của BNB, lợi nhuận bắt đầu lùi và lùi sang một bên. Nếu bạn nhìn vào các thông số trade_value mặc định 3%, nó có thể đã thoái lui 1%. Tuy nhiên, do các giá trị mở rộng, nhiều nhà giao dịch kiếm được và mất nhiều ít hơn.

img

Chúng ta hãy xem xét toàn bộ tiền tệ backtest của Chiến lược 2. Ở đây, vì nó là một cập nhật cấp phút, các thông số Alpha cần phải được điều chỉnh. Từ một thị trường thực sự quan điểm, xu hướng đường cong là phù hợp, cho thấy rằng backtest của chúng tôi có thể được sử dụng như một tham chiếu mạnh mẽ. Giá trị ròng đã đạt đến đỉnh của giá trị ròng từ 4.13 trở đi và đã ở trong giai đoạn thu hồi và bên.

Alpha = 0.001
#price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.rolling(20).mean() # Ordinary moving average
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = list(set(symbols))
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
trade_value = 300
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 0.5*trade_value:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < -0.5*trade_value:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2a = e
(stragey_2a.df['total']/stragey_2d.initial_balance).plot(figsize=(17,6),grid = True);

img

Chiến lược 1, chiến lược altcoin ngắn đạt được lợi nhuận tích cực

trade_symbols = list(set(symbols)-set(['LINK','BTC','XTZ','BCH', 'ETH'])) # Selling short currencies
e = Exchange(trade_symbols+['BTC'],initial_balance=10000,commission=0.00075,log=False)
trade_value = 2000
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    empty_value = 0
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        if e.account[symbol]['value'] - trade_value  < -120 :
            e.Sell(symbol, price, round((trade_value-e.account[symbol]['value'])/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if e.account[symbol]['value'] - trade_value > 120 :
            e.Buy(symbol, price, round((e.account[symbol]['value']-trade_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        empty_value += e.account[symbol]['value']
    price = row[1]['BTC']
    if e.account['BTC']['value'] - empty_value < -120:
        e.Buy('BTC', price, round((empty_value-e.account['BTC']['value'])/price,6),round(e.account['BTC']['realised_profit']+e.account['BTC']['unrealised_profit'],2))
    if e.account['BTC']['value'] - empty_value > 120:
        e.Sell('BTC', price, round((e.account['BTC']['value']-empty_value)/price,6),round(e.account['BTC']['realised_profit']+e.account['BTC']['unrealised_profit'],2))
stragey_1 = e
(stragey_1.df['total']/stragey_1.initial_balance).plot(figsize=(17,6),grid = True);

img

Chiến lược 2 mua quá mức giảm và bán quá mức tăng ngắn phân tích lợi nhuận

Việc in ra thông tin tài khoản cuối cùng cho thấy hầu hết các loại tiền tệ đều mang lại lợi nhuận, và BNB đã chịu thiệt hại nhiều nhất.

pd.DataFrame(stragey_2a.account).T.apply(lambda x:round(x,3)).sort_values(by='realised_profit')

img

# BNB deviation
(price_usdt_btc_norm2.iloc[-7500:].BNB-price_usdt_btc_norm_mean[-7500:]).plot(figsize=(17,6),grid = True);
#price_usdt_btc_norm_mean[-7500:].plot(figsize=(17,6),grid = True);

img

Nếu BNB và ATOM bị loại bỏ, kết quả sẽ tốt hơn, nhưng chiến lược sẽ vẫn ở giai đoạn khôi phục gần đây.

Alpha = 0.001
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = list(set(symbols)-set(['BNB','ATOM']))
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
trade_value = 300
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 0.5*trade_value:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < -0.5*trade_value:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2b = e
(stragey_2b.df['total']/stragey_2b.initial_balance).plot(figsize=(17,6),grid = True);

img

Trong hai ngày qua, nó đã trở nên phổ biến để chạy các chiến lược tiền tệ chính thống. Hãy kiểm tra lại chiến lược này. Do sự sụt giảm sự đa dạng tiền tệ, trade_value đã được tăng 4 lần để so sánh, và kết quả hoạt động tốt, đặc biệt là kể từ khi việc khôi phục gần đây là nhỏ.

Cần lưu ý rằng chỉ có đồng tiền chính không tốt như đồng tiền đầy đủ trong thời gian dài hơn, và có nhiều sự hồi phục. Bạn có thể làm backtest của riêng bạn trên đường hàng giờ bên dưới, chủ yếu là vì đồng tiền ít phân tán và biến động tăng thay vào đó.

Alpha = 0.001
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = ['ETH','LTC','EOS','XRP','BCH']
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
trade_value = 1200
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 0.5*trade_value:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < -0.5*trade_value:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2c = e
(stragey_2c.df['total']/e.initial_balance).plot(figsize=(17,6),grid = True);

img

Phân tích các thông số về phí xử lý và chiến lược

Vì vài báo cáo đầu tiên sử dụng đường k cấp giờ, và các tham số thực tế rất khác với tình hình thị trường thực tế, bây giờ với đường k cấp phút, bạn có thể xem cách thiết lập một số tham số.

  • Alpha = 0.03 Các tham số Alpha của đường trung bình động theo cấp số nhân. Càng lớn thiết lập, càng nhạy cảm theo dõi giá chuẩn và ít giao dịch hơn. Vị trí nắm giữ cuối cùng cũng sẽ thấp hơn, làm giảm đòn bẩy, nhưng cũng sẽ làm giảm lợi nhuận và khôi phục tối đa.

  • Update_base_price_time_interval = 30 * 60 Bao nhiêu lần để cập nhật giá cơ sở, trong giây, liên quan đến tham số Alpha, nhỏ hơn cài đặt Alpha, nhỏ hơn khoảng thời gian có thể được đặt

  • Trade_value: Mỗi 1% giá altcoin (được mệnh giá bằng BTC) lệch so với giá trị nắm giữ chỉ số, cần được xác định theo tổng số tiền đầu tư và ưu tiên rủi ro. Bạn có thể xem xét kích thước đòn bẩy thông qua kiểm tra hậu trường của môi trường nghiên cứu. Trade_value có thể nhỏ hơn Adjust_value, chẳng hạn như một nửa giá trị Adjust_value, tương đương với giá trị nắm giữ 2% từ chỉ số.

  • Adjust_value: Giá trị hợp đồng (giá trị USDT) điều chỉnh giá trị lệch. Khi chỉ số lệch từ * Trade_value-current position> Adjust_value, nghĩa là sự khác biệt giữa vị trí mục tiêu và vị trí hiện tại vượt quá giá trị này, giao dịch sẽ bắt đầu. Sự điều chỉnh quá lớn chậm, giao dịch quá nhỏ thường xuyên và không thể dưới 10, nếu không thì giao dịch tối thiểu sẽ không đạt được, nên đặt nó lên hơn 40% giá trị Trade_value.

Không cần phải nói, Trade_value có liên quan trực tiếp đến lợi nhuận và rủi ro của chúng tôi.

Vì Alpha có dữ liệu tần số cao hơn lần này, rõ ràng là hợp lý hơn để cập nhật nó mỗi 1 phút.

Adjust_value luôn được đề nghị trên 40% của Trade_value. Cài đặt đường 1h K ban đầu có ít hiệu quả. Một số người muốn điều chỉnh nó rất thấp, để nó có thể gần hơn với vị trí mục tiêu. Ở đây chúng tôi sẽ phân tích lý do tại sao không nên làm như vậy.

Đầu tiên phân tích vấn đề xử lý phí

Có thể thấy rằng dưới tỷ lệ mặc định là 0,00075, phí xử lý là 293 và lợi nhuận là 270, đó là một tỷ lệ rất cao.

stragey_2a.account['USDT']
{'fee': 293.85972778530453,
 'leverage': 0.45999999999999996,
 'margin': 236.23559736312995,
 'realised_profit': 281.77464608744435,
 'total': 10271.146238,
 'unrealised_profit': -10.628408369648495}
Alpha = 0.001
#price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.rolling(20).mean() # Ordinary moving average
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = list(set(symbols))
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0,log=False)
trade_value = 300
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 10:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < 10:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2d = e
(stragey_2d.df['total']/e.initial_balance).plot(figsize=(17,6),grid = True);

img

Kết quả là một đường thẳng lên, BNB chỉ mang lại một chút xoắn và xoắn, giá trị điều chỉnh thấp hơn nắm bắt mọi biến động.

Điều gì sẽ xảy ra nếu giá trị điều chỉnh là nhỏ nếu có một khoản phí xử lý nhỏ?

Alpha = 0.001
#price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.rolling(20).mean() # Ordinary moving average
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = list(set(symbols))
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
trade_value = 300
for row in price_usdt.iloc[-7500:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 10:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < 10:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2e = e
(stragey_2e.df['total']/e.initial_balance).plot(figsize=(17,6),grid = True);

img

Kết quả là, nó cũng ra khỏi đường cong thẳng xuống. nó dễ hiểu nếu bạn nghĩ về nó, điều chỉnh thường xuyên trong một chênh lệch nhỏ sẽ chỉ mất phí xử lý.

Nhìn chung, mức phí càng thấp, giá trị Adjust_value càng nhỏ, giao dịch càng thường xuyên và lợi nhuận càng cao.

Vấn đề với cài đặt Alpha

Vì có một dòng phút, giá chuẩn sẽ được cập nhật một lần mỗi phút, ở đây chúng tôi chỉ đơn giản là backtest để xác định kích thước của alpha.

for Alpha in [0.0001, 0.0003, 0.0006, 0.001, 0.0015, 0.002, 0.004, 0.01, 0.02]:
#price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.rolling(20).mean() # Ordinary moving average
    price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() #Here is consistent with the strategy, using EMA
    trade_symbols = list(set(symbols))
    price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
    e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
    trade_value = 300
    for row in price_usdt.iloc[-7500:].iterrows():
        e.Update(row[0], row[1])
        for symbol in trade_symbols:
            price = row[1][symbol]
            if np.isnan(price):
                continue
            diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
            aim_value = -trade_value*round(diff/0.01,1)
            now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
            if aim_value - now_value > 0.5*trade_value:
                e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
            if aim_value - now_value < -0.5*trade_value:
                e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
    print(Alpha, e.account['USDT']['unrealised_profit']+e.account['USDT']['realised_profit'])
0.0001 -77.80281760941007
0.0003 179.38803796199724
0.0006 218.12579924541367
0.001 271.1462377177959
0.0015 250.0014065973528
0.002 207.38692166891275
0.004 129.08021828803027
0.01 65.12410041648158
0.02 58.62356792410955

Kết quả kiểm tra ngược của đường phút trong hai tháng qua

Cuối cùng, hãy nhìn vào kết quả của một backtest thời gian dài. Ngay bây giờ, một sau một tăng, và giá trị ròng ngày hôm nay ở mức thấp mới. Hãy cho bạn sự tự tin sau đây. Bởi vì tần suất của dòng phút cao hơn, nó sẽ mở và đóng các vị trí trong vòng một giờ, vì vậy lợi nhuận sẽ cao hơn nhiều.

Một điểm khác, chúng tôi luôn luôn sử dụng một trade_value cố định, làm cho việc sử dụng các quỹ trong thời gian sau đó là không đủ, và tỷ lệ lợi nhuận thực tế vẫn có thể tăng rất nhiều.

Chúng ta đang ở đâu trong thời gian kiểm tra hai tháng?

img

Alpha = 0.001
#price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.rolling(20).mean() # Ordinary moving average
price_usdt_btc_norm2 = price_usdt_btc/price_usdt_btc.ewm(alpha=Alpha).mean() # Here is consistent with the strategy, using EMA
trade_symbols = list(set(symbols))
price_usdt_btc_norm_mean = price_usdt_btc_norm2[trade_symbols].mean(axis=1)
e = Exchange(trade_symbols,initial_balance=10000,commission=0.00075,log=False)
trade_value = 300
for row in price_usdt.iloc[:].iterrows():
    e.Update(row[0], row[1])
    for symbol in trade_symbols:
        price = row[1][symbol]
        if np.isnan(price):
            continue
        diff = price_usdt_btc_norm2.loc[row[0],symbol] - price_usdt_btc_norm_mean[row[0]]
        aim_value = -trade_value*round(diff/0.01,1)
        now_value = e.account[symbol]['value']*np.sign(e.account[symbol]['amount'])
        if aim_value - now_value > 0.5*trade_value:
            e.Buy(symbol, price, round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
        if aim_value - now_value < -0.5*trade_value:
            e.Sell(symbol, price, -round((aim_value - now_value)/price, 6),round(e.account[symbol]['realised_profit']+e.account[symbol]['unrealised_profit'],2))
stragey_2f = e
(stragey_2f.df['total']/stragey_2e.initial_balance).plot(figsize=(17,6),grid = True);

img

(stragey_2f.df['leverage']/stragey_2e.initial_balance).plot(figsize=(17,6),grid = True);

img


Có liên quan

Thêm nữa