2025-08-22 10:48:59 +00:00
|
|
|
|
import core.logger as logging
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
from core.biz.market_data import MarketData
|
|
|
|
|
|
from core.trade.ma_break_statistics import MaBreakStatistics
|
|
|
|
|
|
from core.db.db_market_data import DBMarketData
|
|
|
|
|
|
from core.biz.metrics_calculation import MetricsCalculation
|
|
|
|
|
|
from core.utils import (
|
|
|
|
|
|
datetime_to_timestamp,
|
|
|
|
|
|
timestamp_to_datetime,
|
|
|
|
|
|
transform_date_time_to_timestamp,
|
|
|
|
|
|
)
|
|
|
|
|
|
from trade_data_main import TradeDataMain
|
|
|
|
|
|
from config import (
|
|
|
|
|
|
API_KEY,
|
|
|
|
|
|
SECRET_KEY,
|
|
|
|
|
|
PASSPHRASE,
|
|
|
|
|
|
SANDBOX,
|
2025-08-31 03:20:59 +00:00
|
|
|
|
OKX_MONITOR_CONFIG,
|
2025-08-22 10:48:59 +00:00
|
|
|
|
BAR_THRESHOLD,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.logger
|
|
|
|
|
|
|
2025-09-17 11:33:37 +00:00
|
|
|
|
|
2025-08-22 10:48:59 +00:00
|
|
|
|
class TradeMaStrategyMain:
|
2025-09-17 11:33:37 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
is_us_stock: bool = False,
|
2025-09-25 04:28:43 +00:00
|
|
|
|
is_astock: bool = False,
|
|
|
|
|
|
is_aindex: bool = True,
|
2025-09-17 11:33:37 +00:00
|
|
|
|
is_binance: bool = False,
|
|
|
|
|
|
commission_per_share: float = 0,
|
2025-09-25 04:28:43 +00:00
|
|
|
|
buy_by_long_period: dict = {"by_week": False, "by_month": False},
|
|
|
|
|
|
long_period_condition: dict = {"ma5>ma10": False, "ma10>ma20": False, "macd_diff>0": False, "macd>0": False},
|
2025-09-17 11:33:37 +00:00
|
|
|
|
):
|
|
|
|
|
|
self.ma_break_statistics = MaBreakStatistics(
|
|
|
|
|
|
is_us_stock=is_us_stock,
|
2025-09-25 04:28:43 +00:00
|
|
|
|
is_astock=is_astock,
|
|
|
|
|
|
is_aindex=is_aindex,
|
2025-09-17 11:33:37 +00:00
|
|
|
|
is_binance=is_binance,
|
|
|
|
|
|
commission_per_share=commission_per_share,
|
2025-09-25 04:28:43 +00:00
|
|
|
|
buy_by_long_period=buy_by_long_period,
|
|
|
|
|
|
long_period_condition=long_period_condition,
|
2025-09-17 11:33:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-08-22 10:48:59 +00:00
|
|
|
|
def batch_ma_break_statistics(self):
|
|
|
|
|
|
"""
|
|
|
|
|
|
批量计算MA突破统计
|
|
|
|
|
|
"""
|
|
|
|
|
|
logger.info("开始批量计算MA突破统计")
|
|
|
|
|
|
strategy_dict = self.ma_break_statistics.main_strategy
|
2025-09-17 11:33:37 +00:00
|
|
|
|
account_value_chg_df_list = []
|
2025-08-22 10:48:59 +00:00
|
|
|
|
for strategy_name, strategy_info in strategy_dict.items():
|
2025-09-15 06:12:47 +00:00
|
|
|
|
if "macd" in strategy_name:
|
|
|
|
|
|
# 只计算macd策略
|
2025-09-17 11:33:37 +00:00
|
|
|
|
account_value_chg_df = self.ma_break_statistics.batch_statistics(
|
|
|
|
|
|
strategy_name=strategy_name
|
|
|
|
|
|
)
|
|
|
|
|
|
account_value_chg_df_list.append(account_value_chg_df)
|
|
|
|
|
|
|
|
|
|
|
|
total_account_value_chg_df = pd.concat(account_value_chg_df_list)
|
|
|
|
|
|
return total_account_value_chg_df
|
|
|
|
|
|
|
|
|
|
|
|
def statistics_account_value_chg(self, account_value_chg_df: pd.DataFrame):
|
|
|
|
|
|
logger.info("开始统计account_value_chg")
|
2025-08-23 17:44:33 +00:00
|
|
|
|
|
2025-08-22 10:48:59 +00:00
|
|
|
|
|
2025-09-25 04:28:43 +00:00
|
|
|
|
def test_single_symbol():
|
|
|
|
|
|
ma_break_statistics = MaBreakStatistics(
|
|
|
|
|
|
is_us_stock=False,
|
|
|
|
|
|
is_astock=True,
|
|
|
|
|
|
is_aindex=False,
|
|
|
|
|
|
is_binance=False,
|
|
|
|
|
|
commission_per_share=0,
|
|
|
|
|
|
)
|
|
|
|
|
|
symbol = "600111.SH"
|
|
|
|
|
|
bar = "1D"
|
|
|
|
|
|
ma_break_statistics.trade_simulate(symbol=symbol, bar=bar, strategy_name="均线macd结合策略2")
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-22 10:48:59 +00:00
|
|
|
|
if __name__ == "__main__":
|
2025-09-17 11:33:37 +00:00
|
|
|
|
commission_per_share_list = [0, 0.0008]
|
2025-09-25 04:28:43 +00:00
|
|
|
|
buy_by_long_period_list = [{"by_week": True, "by_month": True},
|
|
|
|
|
|
{"by_week": True, "by_month": False},
|
|
|
|
|
|
{"by_week": False, "by_month": True},
|
|
|
|
|
|
{"by_week": False, "by_month": False}]
|
|
|
|
|
|
long_period_condition_list = [{"ma5>ma10": True, "ma10>ma20": True, "macd_diff>0": True, "macd>0": True},
|
|
|
|
|
|
{"ma5>ma10": True, "ma10>ma20": False, "macd_diff>0": True, "macd>0": True},
|
|
|
|
|
|
{"ma5>ma10": False, "ma10>ma20": True, "macd_diff>0": True, "macd>0": True}]
|
|
|
|
|
|
|
2025-09-17 11:33:37 +00:00
|
|
|
|
for commission_per_share in commission_per_share_list:
|
2025-09-25 04:28:43 +00:00
|
|
|
|
for buy_by_long_period in buy_by_long_period_list:
|
|
|
|
|
|
for long_period_condition in long_period_condition_list:
|
|
|
|
|
|
logger.info(f"开始计算, 主要参数:commission_per_share: {commission_per_share}, buy_by_long_period: {buy_by_long_period}, long_period_condition: {long_period_condition}")
|
|
|
|
|
|
trade_ma_strategy_main = TradeMaStrategyMain(
|
|
|
|
|
|
is_us_stock=False,
|
|
|
|
|
|
is_astock=False,
|
|
|
|
|
|
is_aindex=True,
|
|
|
|
|
|
is_binance=False,
|
|
|
|
|
|
commission_per_share=commission_per_share,
|
|
|
|
|
|
buy_by_long_period=buy_by_long_period,
|
|
|
|
|
|
long_period_condition=long_period_condition,
|
|
|
|
|
|
)
|
|
|
|
|
|
trade_ma_strategy_main.batch_ma_break_statistics()
|
|
|
|
|
|
|
|
|
|
|
|
trade_ma_strategy_main = TradeMaStrategyMain(
|
|
|
|
|
|
is_us_stock=False,
|
|
|
|
|
|
is_astock=True,
|
|
|
|
|
|
is_aindex=False,
|
|
|
|
|
|
is_binance=False,
|
|
|
|
|
|
commission_per_share=commission_per_share,
|
|
|
|
|
|
buy_by_long_period=buy_by_long_period,
|
|
|
|
|
|
long_period_condition=long_period_condition,
|
|
|
|
|
|
)
|
|
|
|
|
|
trade_ma_strategy_main.batch_ma_break_statistics()
|