//------------------------------------------------------------------------
// 简称: FUTURESDYNAMICSTOPLOSS
// 名称: 期货动态止损策略
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric BollStdDev(2); // 布林线标准差倍数
Numeric BollPeriod(20); // 布林线周期
Numeric Ma30Period(30); // 30日均线周期
Numeric DailyMaPeriod(1); // 当日均线周期
Numeric Ma5Period(5); // 5日均线周期
Numeric Percent1(0.01); // 布林线上方1%
Numeric Percent2(0.01); // 30日线上方1%
Numeric Percent3(0.01); // 当日均线下方1%
Numeric Percent4(0.015); // 当日均线下方1.5%
Numeric Percent5(0.02); // 当日均线下方2%
Numeric StopLossOffset(1); // 止损偏移点数
Numeric MaxTrade(3); // 每日最大开仓次数
Numeric MaxLoss(3); // 最大连续亏损次数
Vars
Series<Numeric> BollUpper; // 布林线上轨
Series<Numeric> BollLower; // 布林线下轨
Series<Numeric> Ma30; // 30日均线
Series<Numeric> DailyMa; // 当日均线
Series<Numeric> Ma5; // 5日均线
Series<Numeric> EntryPrice; // 开仓价格
Numeric TradeCount(0); // 当日开仓次数
Numeric LossCount(0); // 连续亏损次数
Bool EnableTrade(False); // 是否允许开仓
Series<Numeric> StopPrice; // 止损价格
Series<Numeric> TargetPrice; // 止盈价格
Integer TimerId; // 定时器标识符
Events
OnInit()
{
// 初始化变量
CreateTimer(TimerInterval=1000, &CheckDynamicStopLoss);
}
OnBar(ArrayRef<Integer> indexes)
{
// 计算技术指标
BollUpper = BollingerBand(Close, BollPeriod, BollStdDev);
BollLower = BollingerBand(Close, BollPeriod, -BollStdDev);
Ma30 = AverageFC(Close, Ma30Period);
DailyMa = AverageFC(Close, DailyMaPeriod);
Ma5 = AverageFC(Close, Ma5Period);
// 判断是否在允许的交易时间段内
EnableTrade = IsWithinTradingTime();
// 开仓条件
If (EnableTrade && MarketPosition == 0)
{
// 条件1:实时收盘价价格远离布林线上轨下方1%以上
If (Close < BollUpper * (1 - Percent1))
{
// 条件2:收盘价同时在30日线上方
If (Close > Ma30 * (1 + Percent2))
{
// 分批开仓逻辑
If (Close < DailyMa * (1 - Percent3))
{
// 做多分批开仓
For i = 1 To 3
{
If (i == 1 && Close < DailyMa * (1 - Percent3))
{
Buy(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 + Percent3);
TradeCount++;
StartMonitoringForStopLoss();
}
Else If (i == 2 && Close < DailyMa * (1 - Percent4))
{
Buy(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 + Percent4);
TradeCount++;
StartMonitoringForStopLoss();
}
Else If (i == 3 && Close < DailyMa * (1 - Percent5))
{
Buy(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 + Percent5);
TradeCount++;
StartMonitoringForStopLoss();
}
}
}
Else If (Close > DailyMa * (1 + Percent3))
{
// 做空分批开仓
For i = 1 To 3
{
If (i == 1 && Close > DailyMa * (1 + Percent3))
{
SellShort(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 - Percent3);
TradeCount++;
StartMonitoringForStopLoss();
}
Else If (i == 2 && Close > DailyMa * (1 + Percent4))
{
SellShort(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 - Percent4);
TradeCount++;
StartMonitoringForStopLoss();
}
Else If (i == 3 && Close > DailyMa * (1 + Percent5))
{
SellShort(1, Close);
EntryPrice = Close;
TargetPrice[i] = Ma5 * (1 - Percent5);
TradeCount++;
StartMonitoringForStopLoss();
}
}
}
}
}
}
// 平仓逻辑
If (MarketPosition == 1) // 多单
{
// 止盈条件
If (Close >= TargetPrice)
{
Sell(0, TargetPrice);
}
// 止损条件
If (Close < StopPrice)
{
Sell(0, StopPrice);
If (Close < EntryPrice) LossCount++;
}
}
Else If (MarketPosition == -1) // 空单
{
// 止盈条件
If (Close <= TargetPrice)
{
BuyToCover(0, TargetPrice);
}
// 止损条件
If (Close > StopPrice)
{
BuyToCover(0, StopPrice);
If (Close > EntryPrice) LossCount++;
}
}
// 动态止损逻辑
If (MarketPosition != 0 && HasProfit())
{
CheckDynamicStopLoss();
}
// 风险控制
If (TradeCount >= MaxTrade || LossCount >= MaxLoss)
{
EnableTrade = False;
}
// 每日收盘重置
If (Time >= 0.1500)
{
TradeCount = 0;
LossCount = 0;
}
}
// 辅助函数:判断是否在允许的交易时间段内
Bool IsWithinTradingTime()
{
If (
(Time >= 0.0900 && Time <= 0.0906) ||
(Time >= 0.0912 && Time <= 0.0929) ||
(Time >= 0.0935 && Time <= 0.0942) ||
(Time >= 0.0955 && Time <= 0.1005) ||
(Time >= 0.1128 && Time <= 0.1130)
)
{
Return True;
}
Return False;
}
// 辅助函数:判断是否有盈利
Bool HasProfit()
{
If (MarketPosition == 1 && Close > EntryPrice)
{
Return True;
}
Else If (MarketPosition == -1 && Close < EntryPrice)
{
Return True;
}
Return False;
}
// 辅助函数:启动动态止损监测
Void StartMonitoringForStopLoss()
{
CreateTimer(TimerInterval=60*1000, &CheckDynamicStopLoss); // 每分钟检查一次
}
// 辅助函数:检查动态止损条件
Void CheckDynamicStopLoss()
{
If (MarketPosition == 1) // 多单
{
ArrayRef<BarData> minuteBars = GetHistoryBars(TimeFrame='M', Count=2);
If (!IsEmpty(minuteBars))
{
BarData currentMinute = minuteBars[LastIndex];
BarData previousMinute = minuteBars[LastIndex - 1];
If ((currentMinute.Close / previousMinute.Open - 1) >= 0.0028)
{
StopPrice = currentMinute.Low - MinMove * StopLossOffset;
}
}
}
Else If (MarketPosition == -1) // 空单
{
ArrayRef<BarData> minuteBars = GetHistoryBars(TimeFrame='M', Count=2);
If (!IsEmpty(minuteBars))
{
BarData currentMinute = minuteBars[LastIndex];
BarData previousMinute = minuteBars[LastIndex - 1];
If ((currentMinute.Close / previousMinute.Open - 1) <= -0.0028)
{
StopPrice = currentMinute.High + MinMove * StopLossOffset;
}
}
}
}
点开来无语一分钟。
优化哪里?优化目标是什么?
什么描述都没有,啪几百行行代码扔脸上。
🤔
直接发付费模型代写板块吧。
还甩了100大洋到脸上撒
画的100大洋也算大洋吗