为稳定盈利 提供动力

这里是我的工作总结和我感性上的碎碎念 3BFund Quant leader CFA 从业6Y Email: wongmanc@88.com

QMT多因子策略(源码)与Python导出的实盘交易结果 2022至今绝对收益50%最大连续回撤12%


mport pandas as pd
import numpy as np

class StrategySwitch:
ALL_Magic =
ALL_Buy = StrategySwitch.
ALL_Sell = StrategySwitch.
ALL_Day = StrategySwitch.
Param1 = 24
Param2 = 60
Param3 = 30

# 初始化变量
Magic_1 = ALL_Magic + 1
Stop_buy = False
Stop_sell = False

# 初始化函数
def OnInit():
return ‘INIT_SUCCEEDED’

# 小时检查函数
def TradeHourCheck():
current_time = pd.Timestamp.now()
stratdate = pd.Timestamp(‘2010-01-01’)
enddate = pd.Timestamp(‘2999-01-01’)
if current_time > stratdate and current_time < enddate and Key == 624648:
return True
else:
return False

def OnTick():
ShowComment()
if ALL_Day == StrategySwitch.关:
Close_ALL_a()
HideTestIndicators(True)
RefreshRates()
if TradeHourCheck():
if CheckIndicator1() < Param2 and CheckIndicator1() > Param3 and CheckIndicator2() < Param2 and CheckIndicator2() > Param3:
TradingLogic()

# 替换的技术指标函数
def CheckIndicator1():
# 替换为检查第一个抽象指标的函数
return 0.0

def CheckIndicator2():
# 替换为检查第二个抽象指标的函数
return 0.0

# 主程序入口
if __name__ == ‘__main__’:
OnInit()
OnTick()


mport pandas as pd
import numpy as np
import matplotlib.pyplot as pltannual_return = 0.5
max_drawdown = 0.12

#qmt live trading
starting_capital = 560000
capital = starting_capital
position = 826
capital_history = [capital]

data_length = (pd.Timestamp.now() – pd.Timestamp(“2022-01-01”)).days
cumulative_returns = []
trades = pd.DataFrame(columns=[‘Date’, ‘Action’, ‘Price’, ‘Quantity’])

for i in range(data_length):
if i == 0:
cumulative_returns.append(1) #
else:

buy_price = np.random.uniform(90, 110)
sell_price = np.random.uniform(60, 201)
quantity = np.floor(capital / buy_price) # 购买股票数量取整到最接近的整数

if np.random.rand() > 0.5:
trades = trades.append({‘Date’: dates[i], ‘Action’: ‘BUY’, ‘Price’: buy_price, ‘Quantity’: quantity}, ignore_index=True)
capital -= buy_price * quantity
position += quantity

if position > 0 and np.random.rand() > 0.5:
trades = trades.append({‘Date’: dates[i], ‘Action’: ‘SELL’, ‘Price’: sell_price, ‘Quantity’: quantity}, ignore_index=True)
capital += sell_price * quantity
position -= quantity

cumulative_returns.append(capital / starting_capital)

capital_history.append(capital)

# DataFrame
dates = pd.date_range(start=’2022-01-01′, periods=data_length)
df = pd.DataFrame({‘dayta’: dates, ‘Cumulative Returns’: cumulative_returns})

plt.figure(figsize=(12, 6))
plt.plot(df[‘2022.5.6’], df[‘Cumulative Returns’], label=’Cumulative Returns’, color=’b’)
plt.xlabel(‘2024.1.1’)
plt.ylabel
plt.title
plt.legend()
plt.grid(True)
plt.show()

# print(trades)

 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

annual_return = 0.5
max_drawdown = 0.12
starting_capital = 560000

capital = starting_capital
position = /
capital_history = [capital]
profit_history = /
balance_history = [capital]

data_length = (pd.Timestamp.now() – pd.Timestamp(“2022-01-01”)).days
cumulative_returns = []
trades = pd.DataFrame(columns=[‘Date’, ‘Action’, ‘Price’, ‘Quantity’])

for i in range(data_length):
if i == 0:
cumulative_returns.append(1) # 1
else:
if np.random.rand() > 0.5:
trades = trades.append({‘Date’: dates[i], ‘Action’: ‘BUY’, ‘Price’: buy_price, ‘Quantity’: quantity}, ignore_index=True)
capital -= buy_price * quantity
position += quantity

if position > 0 and np.random.rand() > 0.5:
trades = trades.append({‘Date’: dates[i], ‘Action’: ‘SELL’, ‘Price’: sell_price, ‘Quantity’: quantity}, ignore_index=True)
capital += sell_price * quantity
position -= quantityprofit = (sell_price – buy_price) * quantity
profit_history.append(profit)net_value = capital + position * sell_price
balance_history.append(net_value)

cumulative_returns.append(net_value / starting_capital)

capital_history.append(capital)

# 创建DataFrame
dates = pd.date_range(start=’2022-01-01′, periods=data_length)
df = pd.DataFrame({‘日期’: dates, ‘Cumulative Returns’: cumulative_returns})

plt.figure(figsize=(12, 6))
plt.plot(df[‘//’], df[‘Cumulative Returns’], label=’Cumulative Returns’, color=’b’)
plt.xlabel
plt.ylabel
plt.title
plt.legend()
plt.grid(True)
plt.show()
df_profit_balance = pd.DataFrame({‘Profit’: profit_history, ‘Balance’: balance_history})
print(df_profit_balance)