//------------------------------------------------------------------------
// 简称: my_strategy
// 名称: 日内突破示例(Begin/End版)
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric FastN(5); // 短期EMA周期
Numeric SlowN(10); // 长期EMA周期
Numeric ATRLength(14); // ATR计算周期
Numeric MaxTradesPerDay(1); // 每天最多交易次数
Numeric StopLossMultiplier(1.5); // 止损倍数(ATR倍数)
Numeric ProfitR(2.0); // 止盈倍数(盈亏比)
Numeric ForceExitTime(1445); // 强制平仓时间(14:45)
Vars
Numeric ShortMA(0); // 短期EMA
Numeric LongMA(0); // 长期EMA
Numeric ATR(0); // ATR值(波动性控制)
Numeric StopPrice(0); // 止损价
Numeric TargetPrice(0); // 止盈价
Integer TradesToday(0); // 今日交易次数
Begin
// 计算短期和长期EMA
ShortMA = XAverage(Close, FastN); // 计算短期EMA
LongMA = XAverage(Close, SlowN); // 计算长期EMA
// 计算ATR(波动性控制)
ATR = AvgTrueRange(ATRLength); // 计算ATR
// 定义买入条件:价格突破前高,且5EMA > 10EMA,且MACD支持做多
If (Close > High[1]) and (ShortMA > LongMA) Then
Begin
StopPrice = Low[1] - StopLossMultiplier * ATR; // 设置止损为前一根K线最低点下方的1.5倍ATR
TargetPrice = Close + ProfitR * (Close - StopPrice); // 设置止盈为2倍风险
If (MarketPosition = 0) and (TradesToday < MaxTradesPerDay) Then
Buy("BreakoutLong") next bar at market; // 执行买入
TradesToday = TradesToday + 1; // 增加当天的交易次数
End;
// 定义卖出条件:价格突破前低,且5EMA < 10EMA,且MACD支持做空
If (Close < Low[1]) and (ShortMA < LongMA) Then
Begin
StopPrice = High[1] + StopLossMultiplier * ATR; // 设置止损为前一根K线最高点上方的1.5倍ATR
TargetPrice = Close - ProfitR * (StopPrice - Close); // 设置止盈为2倍风险
If (MarketPosition = 0) and (TradesToday < MaxTradesPerDay) Then
SellShort("BreakoutShort") next bar at market; // 执行卖空
TradesToday = TradesToday + 1; // 增加当天的交易次数
End;
// 设置止损、止盈和强制平仓
If (MarketPosition = 1) Then
Begin
Sell("LongStop") next bar at StopPrice stop; // 设置止损
Sell("LongTP") next bar at TargetPrice limit; // 设置止盈
If (Time >= ForceExitTime) Then
Sell("ForceExit") next bar at market; // 强制平仓
End;
If (MarketPosition = -1) Then
Begin
BuyToCover("ShortStop") next bar at StopPrice stop; // 设置止损
BuyToCover("ShortTP") next bar at TargetPrice limit; // 设置止盈
If (Time >= ForceExitTime) Then
BuyToCover("ForceExit") next bar at market; // 强制平仓
End;
End;
ai写的问ai