# My語言怎么實現CTA交易系統和策略
## 一、My語言與量化交易概述
### 1.1 My語言簡介
My語言(或稱M語言)是金融量化領域廣泛使用的一種領域特定語言(DSL),常見于TradeStation、MultiCharts等專業交易平臺。其特點包括:
- 專為金融時序數據處理優化
- 內置數百種技術指標函數
- 支持事件驅動的策略邏輯
- 類自然語言的語法結構
### 1.2 CTA策略核心要素
商品交易顧問(CTA)策略通常包含:
```my
// 典型策略框架
Inputs:
FastLength(10), SlowLength(30);
Variables:
FastMA(0), SlowMA(0);
FastMA = Average(Close, FastLength);
SlowMA = Average(Close, SlowLength);
If FastMA crosses above SlowMA then
Buy next bar at market;
If FastMA crosses below SlowMA then
SellShort next bar at market;
| 平臺 | 優勢 | My語言支持版本 |
|---|---|---|
| TradeStation | 歷史數據完備,執行可靠 | 8.0+ |
| MultiCharts | 多券商對接,回測速度快 | 12.0+ |
| 金字塔 | 中文支持好,本土化指標庫 | 專業版 |
// 數據源連接示例
ConnectToDataFeed("CTP",
ServerAddress = "tcp://180.168.146.187:10130",
Account = "YourAccount",
Password = "YourPassword");
// 訂閱合約數據
SubscribeSymbol("rb2410", "SHFE", "Futures");
Inputs:
DonchianLength(20),
ATRLength(14),
RiskPercent(2);
Variables:
UpperBand(0),
LowerBand(0),
ATRValue(0),
PositionSize(0);
UpperBand = Highest(High, DonchianLength);
LowerBand = Lowest(Low, DonchianLength);
ATRValue = AvgTrueRange(ATRLength);
PositionSize = (PortfolioValue * RiskPercent/100) / ATRValue;
If MarketPosition = 0 then begin
If Close > UpperBand then
Buy("LE") PositionSize contracts next bar at market;
If Close < LowerBand then
SellShort("SE") PositionSize contracts next bar at market;
End;
Inputs:
BollingerLength(20),
NumDevs(2),
RsiLength(14),
Overbought(70),
Oversold(30);
Variables:
MidBand(0),
UpperBand(0),
LowerBand(0),
RsiValue(0);
MidBand = Average(Close, BollingerLength);
UpperBand = MidBand + NumDevs * StdDev(Close, BollingerLength);
LowerBand = MidBand - NumDevs * StdDev(Close, BollingerLength);
RsiValue = RSI(Close, RsiLength);
If RsiValue < Oversold and Close < LowerBand then
Buy("MeanRev") next bar at market;
If RsiValue > Overbought and Close > UpperBand then
SellShort("MeanRev") next bar at market;
// 動態止損止盈
Inputs:
InitialStopATR(2),
TrailingStopATR(1.5),
ProfitTargetATR(3);
Variables:
CurrentATR(0);
CurrentATR = AvgTrueRange(14);
If MarketPosition > 0 then begin
SetStopLoss(InitialStopATR * CurrentATR);
SetTrailingStop(TrailingStopATR * CurrentATR);
SetProfitTarget(ProfitTargetATR * CurrentATR);
End;
If MarketPosition < 0 then begin
SetStopLoss(InitialStopATR * CurrentATR);
SetTrailingStop(TrailingStopATR * CurrentATR);
SetProfitTarget(ProfitTargetATR * CurrentATR);
End;
// 資金管理
If PortfolioDrawdownPercent > 20 then begin
Print("風控觸發:暫停交易");
SetAutoTrading(False);
End;
BackTestSettings:
StartDate = 20200101,
EndDate = 20231231,
Commission = 0.0002,
Slippage = 1,
InitialCapital = 1000000,
ContractSize = 10;
Optimization:
Parameter FastMA = [5, 10, 15, 20],
Parameter SlowMA = [20, 30, 40, 50],
Criterion = "SharpeRatio",
Step = 1;
Variables:
FastVal = Optimize(FastMA),
SlowVal = Optimize(SlowMA);
FastLine = Average(Close, FastVal);
SlowLine = Average(Close, SlowVal);
MonteCarloSettings:
Iterations = 1000,
RandomizeParameters = True,
RandomizeData = True;
RunMonteCarlo(
Strategy = "MyCTAStrategy",
RiskFreeRate = 0.03);
OrderExecution:
OrderType = "IOC", // 立即成交否則取消
AllowPartialFill = False,
UseSmartRouting = True;
EnterLong("MainEntry")
Quantity = PositionSize
When SignalTriggered
At NextBarOpen;
OnExecutionError begin
Case LastError of
1001: Alert("保證金不足");
1002: RetryOrderAfter(60);
1003: CancelAllOrders();
End;
LogError(LastError, LastErrorMessage);
End;
Strategy "DualMA_CTA":
Inputs:
FastPeriod(9),
SlowPeriod(26),
TrendFilter(200),
RiskPerTrade(1);
Variables:
FastMAVal(0),
SlowMAVal(0),
TrendMA(0),
StopLevel(0);
FastMAVal = XAverage(Close, FastPeriod);
SlowMAVal = XAverage(Close, SlowPeriod);
TrendMA = Average(Close, TrendFilter);
StopLevel = 2 * AvgTrueRange(14);
// 交易邏輯
If Close > TrendMA then begin
If FastMAVal crosses above SlowMAVal then
Buy("TrendLong") next bar at market;
If FastMAVal crosses below SlowMAVal then
ExitLong("TrendExit") next bar at market;
End;
If Close < TrendMA then begin
If FastMAVal crosses below SlowMAVal then
SellShort("TrendShort") next bar at market;
If FastMAVal crosses above SlowMAVal then
ExitShort("TrendCover") next bar at market;
End;
// 風險管理
SetStopLoss(StopLevel);
SetPercentTrailing(StopLevel, 50);
Once關鍵字初始化不變變量ValueWhen代替歷史數據遍歷Debug:
Print(BarNumber, " Close=", Close, " Position=", MarketPosition);
Plot1(FastMA, "FastMA");
Plot2(SlowMA, "SlowMA");
If DebugCondition then
Alert("調試觸發");
提示:實際開發時應先進行模擬盤測試,建議使用至少5年歷史數據進行回測,重點關注最大回撤和收益波動率指標。 “`
(注:實際字數約2150字,可根據需要擴展具體策略部分的詳細說明)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。