Created
February 10, 2022 05:00
-
-
Save j40903272/67a1beb6c5138fd44cafec5b5897f025 to your computer and use it in GitHub Desktop.
riusbot 近期績效整理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[59]: | |
import pdb | |
import time | |
import pickle | |
import ccxt | |
import requests | |
import logging | |
import functools | |
import datetime | |
import pandas as pd | |
import numpy as np | |
from tqdm import tqdm | |
from collections import defaultdict | |
import os | |
import concurrent.futures | |
import time | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
get_ipython().run_line_magic('matplotlib', 'inline') | |
# In[270]: | |
with open("/tmp/message.pkl", "rb") as f: | |
message = pickle.load(f) | |
# In[3]: | |
exchange = ccxt.binance({ | |
'enableRateLimit': True, | |
'options': { | |
'defaultType': 'spot', | |
}, | |
}) | |
tmp = exchange.loadMarkets(True) | |
# In[263]: | |
class Evaluation(): | |
def __init__(self, base, hold, leverage=1, timeframe="8h", sl=0.0, tp=0.0, move=99999): | |
self.leverage = leverage | |
self.base = base | |
self.hold = hold # minutes | |
self.total_profit = 0 | |
self.timeframe = timeframe | |
self.sl = sl | |
self.tp = tp | |
self.config = { | |
"sl": sl, | |
"tp": tp, | |
"leverage": leverage, | |
} | |
"klines : time 0, open 1, high 2, low 3, close 4, vol 5" | |
def evaluate(self, info: dict, symbol: str): | |
since = int(info["timestamp"] * 1000) | |
limit = self.hold | |
klines = exchange.fetch_ohlcv( | |
symbol, timeframe=self.timeframe, since=since, limit=limit | |
) | |
entry = klines[0][1] | |
action = info["action"] | |
entry_timestamp = float(klines[0][0]) / 1000 | |
liquidate = entry * (1 + 1 / self.leverage) | |
profit = 0 | |
result = None | |
for e, k in enumerate(klines): | |
low = float(k[3]) | |
high = float(k[2]) | |
close_timestamp = float(k[0])/1000 | |
# if high >= liquidate: | |
# profit = -self.base | |
# percentage = -1 | |
# close = liquidate | |
# result = "liquidate" | |
# break | |
if action == "SELL": | |
if self.sl: | |
sl = entry * (1+self.sl) if info["sl"] is None else info["sl"] | |
if high >= sl: | |
close = sl | |
if action == "SELL": | |
percentage = -(close / entry - 1) | |
else: | |
percentage = (close / entry - 1) | |
profit = percentage * self.base | |
result = "stop loss" | |
break | |
if self.tp: | |
tp = entry * (1-self.tp) if info["tp"] is None else info["tp"] | |
if low <= tp: | |
close = tp | |
if action == "SELL": | |
percentage = -(close / entry - 1) | |
else: | |
percentage = (close / entry - 1) | |
profit = percentage * self.base | |
result = "take profit" | |
break | |
else: | |
if self.sl: | |
sl = entry * (1-self.sl) if info["sl"] is None else info["sl"] | |
if low <= entry * (1-self.sl): | |
close = sl | |
if action == "SELL": | |
percentage = -(close / entry - 1) | |
else: | |
percentage = (close / entry - 1) | |
profit = percentage * self.base | |
result = "stop loss" | |
break | |
if self.tp: | |
tp = entry * (1+self.tp) if info["tp"] is None else info["tp"] | |
if high >= tp: | |
close = tp | |
if action == "SELL": | |
percentage = -(close / entry - 1) | |
else: | |
percentage = (close / entry - 1) | |
profit = percentage * self.base | |
result = "take profit" | |
break | |
if profit == 0: | |
close = float(k[4]) | |
if action == "SELL": | |
percentage = -(close / entry - 1) | |
else: | |
percentage = (close / entry - 1) | |
profit = percentage * self.base * self.leverage | |
result = "end" | |
if "action" not in info: | |
print(info) | |
return { | |
"symbol": symbol, | |
"date": info["date"], | |
"action": info["action"], | |
"entry_timestamp": int(entry_timestamp), | |
"close_timestamp": int(close_timestamp), | |
"entry": entry, | |
"close": close, | |
"liquidate": liquidate, | |
"profit": profit, | |
"percentage": percentage, | |
"result": result, | |
"klines": klines, | |
} | |
def simulate(self, trades): | |
detail = list() | |
tasks = list() | |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: | |
for order in trades: | |
# if order["action"] != "sell": | |
# continue | |
symbol = f'{order["symbol"]}/USDT' | |
try: | |
exchange.market(symbol) | |
except: | |
print(symbol, "not found") | |
continue | |
info = { | |
"date": order['date'], | |
"timestamp": order['date'].timestamp(), | |
"action": order["action"], | |
"sl": order["stop_loss"], | |
"tp": order["take_profit"], | |
} | |
task = executor.submit(self.evaluate, info, symbol) | |
tasks.append(task) | |
time.sleep(0.05) | |
for task in tqdm(concurrent.futures.as_completed(tasks), total=len(tasks)): | |
result = task.result() | |
if result: | |
detail.append(result) | |
self.total_profit += result["profit"] | |
self.detail = detail | |
daily_profit = np.array([i["percentage"] for i in s.detail]) | |
plt.hist(daily_profit, bins=50) | |
daily_profit *= self.leverage | |
self.config["profit"] = self.total_profit | |
self.config["mean"] = np.mean(daily_profit) | |
self.config["std"] = np.std(daily_profit) | |
self.config["sharpe"] = (np.mean(daily_profit) - 0.0005) / np.std(daily_profit) | |
self.save_records() | |
return self.config | |
def save_records(self, path="evaluation.csv"): | |
if os.path.exists(path): | |
df = pd.read_csv(path) | |
else: | |
df = pd.DataFrame(columns=self.config.keys()) | |
df = df.append(self.config, ignore_index=True) | |
df.to_csv(path, index=False) | |
def output_df(self): | |
columns = list(self.detail[0].keys()) | |
df = pd.DataFrame() | |
for col in columns: | |
df[col] = [i[col] for i in self.detail] | |
return df | |
# # ROSE | |
# In[197]: | |
rose_trade = [] | |
for i in message: | |
i["date"] = datetime.datetime.fromtimestamp(i["message_timestamp"]) | |
i["timestamp"] = int(i["message_timestamp"]) | |
if i["channel"] == "ROSE": | |
rose_trade.append(i) | |
rose_trade = rose_trade[:-40][::-1] | |
print(len(rose_trade)) | |
# In[219]: | |
s = Evaluation(base=100, hold=48, timeframe="1h", leverage=1, sl=0.15, tp=0.35) | |
result = s.simulate(rose_trade) | |
print(result) | |
df = s.output_df() | |
df["day"] = df.date.apply(lambda x: x.strftime("%Y%m%d")) | |
tmp = df.groupby("day").sum("profit").percentage.values | |
tmp *= 3 | |
print((np.mean(tmp) - 0.0004) / np.std(tmp)) | |
display(df.sort_values("date", ascending=True)) | |
# In[225]: | |
df = df.sort_values("date", ascending=True) | |
init_balance = 1000 | |
x = df.date | |
y = np.cumsum(df.profit.values.tolist()) + init_balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=10) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# In[221]: | |
init = 100 | |
balance = [] | |
for i in df.percentage.values: | |
init *= (i+1) | |
balance.append(init) | |
x = df.date | |
y = balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=10) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# In[222]: | |
df.to_csv("rose_20220209.csv", index=False) | |
# # Perpetual | |
# In[226]: | |
perp_trade = [] | |
for i in message: | |
i["date"] = datetime.datetime.fromtimestamp(i["message_timestamp"]) | |
i["timestamp"] = int(i["message_timestamp"]) | |
if i["symbol"] in "BTCUSDT_22": | |
i["symbol"] = "BTC" | |
if i["symbol"] in "ETHUSDT_22": | |
i["symbol"] = "ETH" | |
if i["channel"] == "PERPETUAL": | |
perp_trade.append(i) | |
print(len(perp_trade)) | |
# In[233]: | |
s = Evaluation(base=100, hold=8, timeframe="1h", leverage=1, sl=0.2, tp=0.2) | |
result = s.simulate(perp_trade) | |
print(result) | |
df = s.output_df() | |
df["day"] = df.date.apply(lambda x: x.strftime("%Y%m%d")) | |
tmp = df.groupby("day").sum("profit").percentage.values | |
tmp *= 3 | |
print((np.mean(tmp) - 0.0004) / np.std(tmp)) | |
display(df.sort_values("date", ascending=True)) | |
# In[234]: | |
df = df.sort_values("date", ascending=True) | |
df.to_csv("perpetual_20220209.csv", index=False) | |
init_balance = 1000 | |
x = df.date | |
y = np.cumsum(df.profit.values.tolist()) + init_balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=1) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# In[235]: | |
init = 100 | |
balance = [] | |
for i in df.percentage.values: | |
init *= (i+1) | |
balance.append(init) | |
x = df.date | |
y = balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=1) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# # Daily | |
# In[288]: | |
daily_trade = [] | |
for i in message: | |
i["date"] = datetime.datetime.fromtimestamp(i["message_timestamp"]) | |
i["timestamp"] = int(i["message_timestamp"]) | |
if i["symbol"] in "BTCUSDT_22": | |
i["symbol"] = "BTC" | |
if i["symbol"] in "ETHUSDT_22": | |
i["symbol"] = "ETH" | |
if "KSM" in i["symbol"]: | |
continue | |
# i["stop_loss"] = None | |
# i["take_profit"] = None | |
if i["channel"] == "DAILYSCALP": | |
daily_trade.append(i) | |
print(len(daily_trade)) | |
# In[293]: | |
s = Evaluation(base=100, hold=108, timeframe="1h", leverage=1, sl=0.2, tp=0.4) | |
result = s.simulate(daily_trade) | |
print(result) | |
df = s.output_df() | |
df["day"] = df.date.apply(lambda x: x.strftime("%Y%m%d")) | |
tmp = df.groupby("day").sum("profit").percentage.values | |
tmp *= 3 | |
print((np.mean(tmp) - 0.0004) / np.std(tmp)) | |
display(df.sort_values("date", ascending=True)) | |
# In[295]: | |
df = df.sort_values("date", ascending=True) | |
df.to_csv("daily_20220209.csv", index=False) | |
init_balance = 1000 | |
x = df.date | |
y = np.cumsum(df.profit.values.tolist()) + init_balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=1) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# # Justin | |
# In[297]: | |
just_trade = [] | |
for i in message: | |
i["date"] = datetime.datetime.fromtimestamp(i["message_timestamp"]) | |
i["timestamp"] = int(i["message_timestamp"]) | |
if i["symbol"] in "BTCUSDT_22": | |
i["symbol"] = "BTC" | |
if i["symbol"] in "ETHUSDT_22": | |
i["symbol"] = "ETH" | |
# i["stop_loss"] = None | |
# i["take_profit"] = None | |
if i["channel"] == "JUSTIN": | |
just_trade.append(i) | |
print(len(just_trade)) | |
# In[307]: | |
s = Evaluation(base=100, hold=120, timeframe="1h", leverage=1, sl=0.2, tp=0.2) | |
result = s.simulate(just_trade) | |
print(result) | |
df = s.output_df() | |
df["day"] = df.date.apply(lambda x: x.strftime("%Y%m%d")) | |
tmp = df.groupby("day").sum("profit").percentage.values | |
tmp *= 3 | |
print((np.mean(tmp) - 0.0004) / np.std(tmp)) | |
display(df.sort_values("date", ascending=True)) | |
# In[309]: | |
df = df.sort_values("date", ascending=True) | |
df.to_csv("justin_20220209.csv", index=False) | |
init_balance = 1000 | |
x = df.date | |
y = np.cumsum(df.profit.values.tolist()) + init_balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=10) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# # Vegas | |
# In[311]: | |
vegas_trade = [] | |
for i in message: | |
i["date"] = datetime.datetime.fromtimestamp(i["message_timestamp"]) | |
i["timestamp"] = int(i["message_timestamp"]) | |
if i["symbol"] in "BTCUSDT_22": | |
i["symbol"] = "BTC" | |
if i["symbol"] in "ETHUSDT_22": | |
i["symbol"] = "ETH" | |
# i["stop_loss"] = None | |
# i["take_profit"] = None | |
if i["channel"] == "VEGAS": | |
vegas_trade.append(i) | |
print(len(vegas_trade)) | |
# In[315]: | |
s = Evaluation(base=100, hold=48, timeframe="1h", leverage=1, sl=0.1, tp=0.3) | |
result = s.simulate(vegas_trade) | |
print(result) | |
df = s.output_df() | |
df["day"] = df.date.apply(lambda x: x.strftime("%Y%m%d")) | |
tmp = df.groupby("day").sum("profit").percentage.values | |
tmp *= 3 | |
print((np.mean(tmp) - 0.0004) / np.std(tmp)) | |
df = df.sort_values("date", ascending=True) | |
display(df) | |
# In[318]: | |
df = df.sort_values("date", ascending=True) | |
df.to_csv("vegas_20220209.csv", index=False) | |
init_balance = 1000 | |
x = df.date.values[2:] | |
y = np.cumsum(df.profit.values.tolist()[2:]) + init_balance | |
from scipy.ndimage.filters import gaussian_filter1d | |
y = gaussian_filter1d(y, sigma=1) | |
plt.plot(x, y) | |
plt.xticks(rotation=45) | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment