[TOC]

Im Bereich des quantitativen Handels ist die Rolling-Position-Strategie ein attraktives, aber auch anspruchsvolles Thema. Der Kern dieser Strategie besteht darin, durch die Reinvestition realisierter Gewinne in Trendmärkten ein exponentielles Wachstum zu erzielen. Dieser Artikel erläutert Schritt für Schritt, wie diese Handelsidee in ausführbare Code-Logik umgesetzt werden kann, wobei der Fokus auf dem nötigen Denkansatz und nicht auf technischen Details liegt. Es ist wichtig zu beachten, dass die Rolling-Position-Strategie zwar die Rendite erhöht, aber auch die Risiken. Dieser Artikel dient ausschließlich Lern- und Diskussionszwecken.

Die Gewinnlogik der Rolling-Position-Strategie ist im Wesentlichen einezusammengesetztes WachstumsmodellLassen Sie uns dies anhand eines vereinfachten Beispiels verstehen:
Im gleichen Szenario, in dem der Markt dreimal hintereinander um 10 % steigt:
Bei drei aufeinanderfolgenden Steigerungen um jeweils 10 % betrug der Gewinn für einen einzelnen Trade 99,3 USDT und der Gewinn für das Rollen der Position 119,7 USDT.Dieser Unterschied ist die Macht des Zinseszinses.。
Ausgedrückt in einer mathematischen Formel:
// 传统交易:线性增长
最终资金 = 初始资金 × (1 + 杠杆 × 涨幅)
// 滚仓交易:指数增长
最终资金 = 初始资金 × (1 + 杠杆 × 单次涨幅) ^ 滚仓次数
Dies offenbart das Wesen des Überschlags:Umwandlung von linearem Wachstum in exponentielles WachstumDies legte jedoch auch Risiken offen:Ein einziger Stop-Loss-Auftrag könnte alle bis dahin erzielten Zinseszinsgewinne zunichtemachen.。
Bevor wir mit dem Programmieren beginnen, müssen wir aus strategischer Sicht drei grundlegende Fragen beantworten:
Frage 1: Wann beginnt es? (Erster Eintrag)
Es ist notwendig, das Startsignal eines Trends zu bestimmen.
Frage 2: Wann soll fortgefahren werden? (Zusätzliche Rollposition)
Das ist der Kern des Positionsrollovers: Wie lässt sich feststellen, ob sich der Trend nach der Gewinnmitnahme fortsetzt?
Frage 3: Wann aufhören? (Zurückziehen und beobachten)
Diese drei Fragen bilden den Rahmen der gesamten Strategie, und wir werden sie nun nacheinander in Code-Logik übersetzen.

Lassen Sie uns zunächst das ideale Anwendungsszenario für die Rolling-Position-Strategie verstehen.
Ideales Szenario:
Stellen Sie sich vor, Sie könnten in den SHIB-Markt einsteigen, sobald der Kurs von 0,000001 $ steigt, oder eine Position eröffnen, kurz bevor ein bestimmter Altcoin einen Kursanstieg erlebt. Durch kontinuierliches Rollover könnten aus 100 USDT potenziell 10.000 USDT oder sogar mehr werden. Das ist der ultimative Traum der Rollover-Strategie.Steigen Sie in den Markt ein, bevor der Kryptowährungsmarkt explodiert, und sichern Sie sich zehn- oder sogar hundertfache Renditen.。
Die bittere Realität:
Das Problem ist: Woher weiß man, welche Kryptowährung einen Kursanstieg erleben wird? Und wann wird dieser Kursanstieg erfolgen?
Für die meisten von uns ist es schwierig, diesen Wendepunkt genau zu erfassen…Um es ganz deutlich zu sagen: Es ist alles eine Frage des Glücks.Wir können die Zukunft nicht vorhersagen; wir können lediglich versuchen, die Wahrscheinlichkeit, den Jackpot zu knacken, mithilfe historischer Daten und technischer Indikatoren zu erhöhen.
Da wir nicht vorhersagen können, welche Kryptowährung einen Aufschwung erleben wird, bleibt uns nur Folgendes:Legen Sie einen durchführbaren Satz von Einstiegsregeln fest und verwenden Sie technische Indikatoren, um “Trendstart”-Signale zu simulieren.。
Es ist wie Fischen im weiten Ozean. Auch wenn wir nicht wissen, wo die großen Fische sind, können wir Folgendes tun:
Wenn mehrere Signale übereinstimmen, gehen wir davon aus, dass ein Trend bevorsteht und steigen in den Markt ein, um ihn zu testen. Liegen wir richtig, folgen wir dem Trend und verlängern unsere Positionen, um Gewinne zu erzielen; liegen wir falsch, begrenzen wir unsere Verluste und verlassen den Markt umgehend.
Auswahl technischer Hilfsmittel:
Wir verwenden das EMA-Dual-Moving-Average-System (EMA5 und EMA10) als Trendidentifizierungsinstrument. Der Grund für diese Wahl ist einfach:
Kernlogik:
Durch die Erkennung des „Goldenen Kreuzes“ (EMA5 kreuzt EMA10 von oben nach unten) und des „Todeskreuzes“ (EMA5 kreuzt EMA10 von unten nach unten) der gleitenden Durchschnitte können Trendumkehrpunkte erfasst werden:
Die Code-Idee:
// 计算EMA指标
var emaFast = TA.EMA(records, FastEMA); // EMA5
var emaSlow = TA.EMA(records, SlowEMA); // EMA10
// 获取当前和前一根K线的EMA值
var ema5_current = emaFast[emaFast.length - 1];
var ema5_prev = emaFast[emaFast.length - 2];
var ema10_current = emaSlow[emaSlow.length - 1];
var ema10_prev = emaSlow[emaSlow.length - 2];
// 检测金叉:前一根K线EMA5<=EMA10,当前K线EMA5>EMA10
var bullCross = ema5_prev <= ema10_prev && ema5_current > ema10_current;
// 检测死叉:前一根K线EMA5>=EMA10,当前K线EMA5<EMA10
var bearCross = ema5_prev >= ema10_prev && ema5_current < ema10_current;
// 空仓时等待信号入场
if (bullCross) {
Log("📈 金叉信号 - 做多");
openPosition("LONG", currentPrice);
} else if (bearCross) {
Log("📉 死叉信号 - 做空");
openPosition("SHORT", currentPrice);
}
Dieser Abschnitt befasst sich nicht mit den Details von Goldenen Kreuzen und Todeskreuzen; dies sind grundlegende Konzepte im Trading. Der Kernpunkt ist:Wir benötigen ein klares, quantifizierbares Einstiegssignal, um den Beginn des Rollovers auszulösen.。

Die Rollover-Strategie ist im WesentlichenEin rationales AbenteuerspielUm dies zu verstehen, betrachten wir ein vollständiges Szenario:
Spielregeln:
1. 你从交易所账户中拿出100 USDT作为冒险资金
2. 这100 USDT独立管理,与账户其他资金隔离
3. 用这100 USDT开始交易:
- 赚了 → 盈利加入资金池,继续用更大的资金交易(滚仓)
- 亏了 → 触发止损,回到空仓状态
4. 重复这个过程,直到:
- 要么把100 USDT亏完(游戏结束)
- 要么滚到一个满意的金额(主动退出)
Die Genialität dieses Spiels liegt darin:
Dies ist das Kernkonzept der Rolling-Position-Strategie.
Probleme mit traditionellen Praktiken:
Angenommen, Ihr Devisenkonto verfügt über 1000 USDT:
Fondspooling-Lösung:
// 创建一个虚拟的"策略资金池"
var strategyCapital = InitialCapital; // 初始100 USDT
// 第1次交易
// 开仓金额 = 100 USDT
// 止盈后盈利 = 30 USDT
strategyCapital = strategyCapital + 30; // 资金池变为130 USDT
// 第2次交易(滚仓)
var positionValue = strategyCapital * Leverage; // 130 × 3 = 390
var amount = positionValue / price / ctVal; // 计算开仓数量
// 自动使用了第1次的盈利,这就是复利的关键
// 止盈后盈利 = 39 USDT
strategyCapital = strategyCapital + 39; // 资金池变为169 USDT
// 第3次交易(滚仓)
// 开仓金额 = 169 USDT(继续利滚利)
Die Vorteile dieser Konstruktion:
Dies ist das Kernelement der Rolling-Position-Strategie:Nach Ausführung des Gewinnmitnahmeauftrags müssen wir eine wichtige Entscheidung treffen – weiterrollen oder stoppen?
Entscheidungsszenario:
假设我们做多BTC:
- 入场价:45000 USDT,用100 USDT开仓
- 止盈价:49500 USDT(涨10%)
- 止盈成交,盈利30 USDT
- 现在资金池:130 USDT
问题来了:
选项A:收手,带着130 USDT退出,回到空仓
选项B:继续,用130 USDT再次开多(滚仓)
Wie soll man sich entscheiden?
Diese Entscheidung darf nicht auf „Gefühlen“ beruhen; es müssen klare Kriterien gelten. Unsere Beurteilungslogik lautet:Setzt sich dieser Trend fort?
Beurteilungsmethode:
Im Moment der Ausführung des Take-Profit-Auftrags werden die neuesten technischen Indikatoren (EMA gleitender Durchschnitt) neu berechnet:
// 止盈单成交后,获取最新K线数据
var records = _C(exchange.GetRecords, PERIOD_M1);
var emaFast = TA.EMA(records, FastEMA);
var emaSlow = TA.EMA(records, SlowEMA);
var ema5_current = emaFast[emaFast.length - 1];
var ema10_current = emaSlow[emaSlow.length - 1];
var shouldRoll = false;
if (currentDirection == "LONG") {
// 多头止盈后,如果EMA5仍在EMA10上方,继续做多(滚仓)
if (ema5_current > ema10_current) {
shouldRoll = true;
Log("✅ EMA5 > EMA10,上升趋势未破坏");
Log("🔄 决策:继续做多(滚仓)");
} else {
Log("❌ EMA5 <= EMA10,趋势可能转弱");
Log("⏸️ 决策:不滚仓,等待新信号");
}
} else if (currentDirection == "SHORT") {
// 空头止盈后,如果EMA5仍在EMA10下方,继续做空(滚仓)
if (ema5_current < ema10_current) {
shouldRoll = true;
Log("✅ EMA5 < EMA10,下降趋势未破坏");
Log("🔄 决策:继续做空(滚仓)");
} else {
Log("❌ EMA5 >= EMA10,趋势可能转弱");
Log("⏸️ 决策:不滚仓,等待新信号");
}
}
Wenn die Entscheidung lautet: „Die Position weiterrollen“:
if (shouldRoll) {
// 1. 增加滚仓计数
currentRoundRolls++;
Log("🔄 执行滚仓操作... (本轮第", currentRoundRolls, "次滚仓)");
// 2. 获取最新价格
var ticker = _C(exchange.GetTicker);
var newPrice = ticker.Last;
// 3. 基于新资金池重新开仓
if (openPosition(currentDirection, newPrice)) {
Log("✅ 滚仓成功!");
// 4. 挂新的止盈单(在openPosition函数中完成)
// 5. 设置新的止损价(在checkStopLoss函数中监控)
} else {
Log("❌ 滚仓失败,等待新信号");
saveRollRecord(false);
resetPositionState();
}
}
Wenn die Entscheidung „Stopp“ lautet:
else {
// 1. 保存本轮统计
saveRollRecord(false); // false表示正常结束,非止损
// 2. 保留资金池金额
// strategyCapital 保持当前值,等待下次机会
// 3. 回到空仓状态
resetPositionState();
Log("⏳ 已平仓,等待新信号...");
}
Wichtigste Punkte dieses Prozesses:
Lassen Sie uns die Macht des Zinseszinses anhand einer vollständigen Fallstudie erleben:
Erfolgsgeschichten:
初始资金:100 USDT
止盈比例:10%
杠杆:3倍
第1次:100 USDT → 盈利30 → 资金池130
第2次:130 USDT → 盈利39 → 资金池169
第3次:169 USDT → 盈利50.7 → 资金池219.7
第4次:219.7 USDT → 盈利65.9 → 资金池285.6
第5次:285.6 USDT → 盈利85.7 → 资金池371.3
连续滚5次,100变成371.3,增长271%!
Fehlerfall:
第1次:100 USDT → 盈利30 → 资金池130
第2次:130 USDT → 盈利39 → 资金池169
第3次:169 USDT → 趋势反转 → 触发止损
止损比例5%,亏损:169 × 3 × 5% = 25.35 USDT
剩余资金:169 - 25.35 = 143.65 USDT
原本从100滚到169,一次止损后只剩143.65
Das ist die zweischneidige Seite des Rollover-Tradings:

Proaktiver Ausstieg: Abschwächung des Trends
Diese Situation wurde bereits in „Frage Zwei“ behandelt: Wenn nach Gewinnmitnahmen erkennbar ist, dass der Trend keine weiteren Kursgewinne zulässt, sollte man aktiv aussteigen. Dies ist die ideale Ausstiegsstrategie, um den Markt mit Gewinnen zu verlassen.
Passiver Ausstieg: Auslösung eines Stop-Loss-Aufrufs
Darauf werden wir uns jetzt konzentrieren – wenn sich der Markt gegen uns entwickelt und der Kurs die Stop-Loss-Linie erreicht, sind wir gezwungen, unsere Positionen zu schließen.
Viele Menschen lehnen Stop-Loss-Orders ab, weil:
Bei der rollierenden Positionsstrategie hingegenStop-Loss ist die Untergrenze für das Überleben.Denk mal darüber nach:
如果没有止损:
第1次:100 → 滚到 169
第2次:169 → 趋势反转,不止损
价格持续下跌:169 → 150 → 120 → 80 → 50...
最终可能全亏,甚至爆仓
如果有止损:
第1次:100 → 滚到 169
第2次:169 → 趋势反转,触发止损
止损5%:亏损 25.35
剩余:143.65
虽然亏了,但保留了大部分资金
可以等待下一个机会
Das Wesen einer Stop-Loss-Strategie:Setzen Sie auf kleine, sichere Verluste, um große, ungewisse Risiken zu vermeiden.
// 检查止损
function checkStopLoss(currentPrice, position) {
var totalDrawdown = 0;
// 计算当前回撤
if (currentDirection == "LONG") {
totalDrawdown = (currentPrice - entryPrice) / entryPrice;
} else {
totalDrawdown = (entryPrice - currentPrice) / entryPrice;
}
// 判断是否触发止损
if (totalDrawdown < -StopLossPercent) {
Log("❌ 触发止损!回撤:", (totalDrawdown * 100).toFixed(2), "%");
// 1. 取消止盈单
if (takeProfitOrderId) {
Log("取消止盈单:", takeProfitOrderId);
exchange.CancelOrder(takeProfitOrderId);
takeProfitOrderId = null;
Sleep(500);
}
// 2. 市价平仓(循环重试直到成功)
var profit = closePositionMarketWithRetry(currentPrice, position);
// 3. 更新策略资金池
strategyCapital += profit; // profit是负数
totalProfitRealized += profit;
Log("止损亏损:", profit.toFixed(2), "U");
Log("策略剩余资金:", strategyCapital.toFixed(2), "U");
// 4. 记录本轮止损亏损
currentRoundLoss = Math.abs(profit);
Log("本轮止损亏损:", currentRoundLoss.toFixed(2), "U");
// 5. 保存本轮滚仓记录(被止损中断)
saveRollRecord(true); // true表示止损结束
// 6. 重置状态
resetPositionState();
// 7. 检查资金是否充足
if (strategyCapital < 10) {
Log("💥 策略资金不足10U,停止运行");
throw "资金不足";
}
Log("⏳ 已止损,等待新信号...");
}
}
Erinnert ihr euch an das „Spiel des rationalen Abenteurers“, von dem wir gesprochen haben? Dieses Spiel hat eine eindeutige Endbedingung:
Bedingung 1: Der Kapitalpool ist auf Null reduziert.
if (strategyCapital <= 0) {
Log("💥 游戏结束:资金池已归零");
Log("本次冒险失败,100 USDT全部亏光");
throw "资金耗尽";
}
Bedingung 2: Freiwilliger Rücktritt
if (strategyCapital >= 目标金额) {
Log("🎉 达到目标金额,可以选择主动退出");
Log("锁定利润,开始新一轮100 USDT的游戏");
}
Bedingung 3: Die maximale Anzahl an Überschlägen erreichen
if (连续滚仓次数 >= 10次) {
Log("⚠️ 达到最大滚仓次数,主动退出");
Log("持续时间太长,风险累积,见好就收");
saveRollRecord(false);
resetPositionState();
}
Der Kern der gesamten Strategie für rollierende Positionen liegt inDas richtige Verhältnis zwischen Risiko und Rendite finden:
Umsatzseite:
Risikoseite:
TRUMP_USDTBacktesting-Analyse des ersten Handelstages von Binance Futures (20. Januar 2025 bis 21. Januar 2025):


Die Backtest-Ergebnisse zeigen Folgendes:
Highlights:
Risikoexposition:
Wichtige Daten:
Aus der obigen Analyse geht klar hervor, dass es sich bei dieser Strategie im Wesentlichen um eine Simulation handelt:
Das Handelsverhalten eines rationalen Abenteurers:
Die Kernlogik lautet:
Einschränkung 1: Abhängigkeit von Trendmärkten
Diese Strategie schneidet in volatilen Märkten schlecht ab, weil:
Einschränkung 2: Parameterempfindlichkeit
Parameter wie ein Gewinnziel von 10 % und ein Stop-Loss von 5 % sind nicht optimal:
Einschränkung 3: Unvorhersehbarer Detonationspunkt
Wie bereits erwähnt, ist der Markteintritt mithilfe technischer Indikatoren im Wesentlichen ein Glücksspiel:
Option 1: Währungen basierend auf dem Workflow filtern
Richtung 2: Parameter dynamisch anpassen
Richtung 3: Mehrere parallel operierende Fonds
Durch die Ableitung von drei Kernfragen haben wir vollständig aufgezeigt, wie sich die Handelsidee des Positionsrollings in Code-Logik umsetzen lässt. Der Kern dieses Prozesses ist:Die Denkweise eines rationalen Risikoträgers im Handel mithilfe präziser Regeln und Datenstrukturen zum Ausdruck bringen.
Wichtiger Hinweis:
Dies ist lediglich eine Simulation der Rolling-Position-Strategie. In der Realität ist Rolling Positions eine Handelsstrategie, die viel Markterfahrung erfordert. Diese Strategie ist nur ein Hilfsmittel. Später kann sie mit Workflows kombiniert werden, um populäre oder besonders aussichtsreiche Kryptowährungen zu identifizieren. Der Einsatz dieses Hilfsmittels wird uns noch einige Überraschungen bescheren.
Denken Sie daran:
Es gibt keine Strategie, die einen Gewinn garantiert.Das Überspringen von Positionen ist lediglich ein Hilfsmittel. Was wirklich über Erfolg oder Misserfolg entscheidet, ist Ihre Fähigkeit:
Möge euch allen auf eurer Reise im quantitativen Handel euer eigenes „großes Glück“ begegnen!
Vollständige Richtlinienadresse:**Strategie-Quellcode -> ** https://www.fmz.com/strategy/521864
Vollständiger Strategiecode:
”`js /*backtest start: 2025-01-20 00:00:00 end: 2025-01-21 00:00:00 period: 1m basePeriod: 1m exchanges: [{“eid”:“Futures_Binance”,“currency”:“TRUMP_USDT”,“balance”:5000}] */
// ============================================ // 滚仓策略 - EMA5/EMA10 简化版 // 使用 CreateOrder 统一下单 // 持续检测订单状态 // 止盈后根据EMA关系决定是否滚仓 // 新增:滚仓统计功能(三个两行表格) // 修复:方向记录、亏损记录、入场价格记录 // 优化:市价平仓循环重试直到成功 // 优化:滚仓统计表格新增开始/结束时间 // ============================================
// ========== 策略参数(可调整)========== var Symbol = “TRUMP_USDT.swap”; // 交易币种 var InitialCapital = 100; // 策略初始资金 100U var Leverage = 3; // 杠杆倍数 var RollProfitPercent = 0.10; // 滚仓盈利系数(10% = 0.10) var StopLossPercent = 0.05; // 止损系数(10% = 0.10)
// EMA参数 var FastEMA = 5; var SlowEMA = 10;
// 全局变量 var strategyCapital = InitialCapital; var entryPrice = 0; var lastRollPrice = 0; var rollCount = 0; var totalProfitRealized = 0; var currentDirection = “”; var takeProfitOrderId = null; // 止盈单ID var amountPrecision = 0; // 数量精度 var pricePrecision = 2; // 价格精度 var ctVal = 1; // 合约面值
// ========== 滚仓统计变量 ========== var currentRoundRolls = 0; // 本轮滚仓次数(连续滚仓) var currentRoundStartTime = 0; // 本轮开始时间 var currentRoundDirection = “”; // 本轮方向 var currentRoundTotalProfit = 0; // 本轮累计盈利(每次止盈累加) var currentRoundLoss = 0; // 本轮亏损(止损时记录) var currentRoundEntryPrice = 0; // 本轮入场价格 var rollHistory = []; // 滚仓历史记录 var maxHistoryRecords = 10; // 保留最近10次滚仓记录
function main() { Log(“=== EMA滚仓策略启动(CreateOrder模式 + 滚仓统计)===”); Log(“交易币种:”, Symbol); Log(“━━━━━━━━━━━━━━━━━━━━”);
// 获取市场信息
var markets = exchange.GetMarkets();
if (!markets || !markets[Symbol]) {
Log("❌ 错误:无法获取", Symbol, "的市场信息");
return;
}
var marketInfo = markets[Symbol];
amountPrecision = marketInfo.AmountPrecision;
pricePrecision = marketInfo.PricePrecision || 2;
ctVal = marketInfo.CtVal;
Log("市场信息:");
Log(" - 数量精度:", amountPrecision);
Log(" - 价格精度:", pricePrecision);
Log(" - 合约面值:", ctVal);
var account = _C(exchange.GetAccount);
Log("账户总资金:", account.Balance.toFixed(2), "U");
Log("策略使用资金:", InitialCapital, "U");
Log("杠杆倍数:", Leverage, "倍");
Log("滚仓系数:", (RollProfitPercent * 100), "%");
Log("止损系数:", (StopLossPercent * 100), "%");
Log("━━━━━━━━━━━━━━━━━━━━");
if (account.Balance < InitialCapital) {
Log("❌ 错误:账户余额不足");
return;
}
exchange.SetContractType("swap");
exchange.SetMarginLevel(Leverage);
var lastBarTime = 0;
while (true) {
var records = _C(exchange.GetRecords, PERIOD_M1);
if (records.length < SlowEMA + 5) {
Sleep(3000);
continue;
}
var currentBarTime = records[records.length - 1].Time;
if (currentBarTime == lastBarTime) {
Sleep(1000);
continue;
}
lastBarTime = currentBarTime;
var ticker = _C(exchange.GetTicker);
var currentPrice = ticker.Last;
var account = _C(exchange.GetAccount);
var position = _C(exchange.GetPositions);
// 计算EMA
var emaFast = TA.EMA(records, FastEMA);
var emaSlow = TA.EMA(records, SlowEMA);
if (emaFast.length < 3 || emaSlow.length < 3) {
Sleep(3000);
continue;
}
var ema5_current = emaFast[emaFast.length - 1];
var ema5_prev = emaFast[emaFast.length - 2];
var ema10_current = emaSlow[emaSlow.length - 1];
var ema10_prev = emaSlow[emaSlow.length - 2];
var isBullTrend = ema5_current > ema10_current;
var isBearTrend = ema5_current < ema10_current;
var bullCross = ema5_prev <= ema10_prev && ema5_current > ema10_current;
var bearCross = ema5_prev >= ema10_prev && ema5_current < ema10_current;
if(takeProfitOrderId){
checkTakeProfitOrder();
}
// ========== 持仓逻辑 ==========
if (position.length > 0) {
var pos = position[0];
currentDirection = pos.Type == PD_LONG ? "LONG" : "SHORT";
if (entryPrice == 0) {
entryPrice = pos.Price;
lastRollPrice = pos.Price;
}
// 检查止损
checkStopLoss(currentPrice, pos);
} else {
// ========== 空仓:等待信号 ==========
if (bullCross) {
Log("📈 金叉信号 - 做多");
openPosition("LONG", currentPrice);
} else if (bearCross) {
Log("📉 死叉信号 - 做空");
openPosition("SHORT", currentPrice);
}
}
showStatus(account, position, currentPrice, ema5_current, ema10_current, isBullTrend, currentBarTime);
Sleep(1000);
}
}
// 开仓(持续检测订单状态) function openPosition(direction, price) { Log(“🚀 开仓”, direction == “LONG” ? “做多” : “做空”); Log(“使用资金:”, strategyCapital.toFixed(2), “U”);
var positionValue = strategyCapital * Leverage;
var amount = _N(positionValue / price / ctVal, amountPrecision);
Log("计算数量:", amount, "| 持仓价值:", positionValue.toFixed(2), "U");
if (amount <= 0) {
Log("❌ 数量无效");
return false;
}
// 使用 CreateOrder 市价开仓
var orderId = exchange.CreateOrder(Symbol, direction == "LONG" ? "buy" : "sell", -1, amount);
if (!orderId) {
Log("❌ 下单失败");
return false;
}
Log("订单ID:", orderId, "开始持续检测...");
// 持续检测订单状态,直到成交或超时
var maxWaitTime = 30000; // 最多等待30秒
var startTime = Date.now();
var checkCount = 0;
while (Date.now() - startTime < maxWaitTime) {
Sleep(500);
checkCount++;
var order = exchange.GetOrder(orderId);
if (!order) {
Log("❌ 无法获取订单信息");
continue;
}
if (order.Status == 1) {
// 订单已成交
var avgPrice = order.AvgPrice;
entryPrice = avgPrice;
lastRollPrice = avgPrice;
currentDirection = direction;
// ========== 修改:无论是否第一次,都要初始化/更新统计数据 ==========
if (currentRoundRolls == 0) {
// 第一次开仓:初始化所有统计数据
currentRoundStartTime = Date.now();
currentRoundDirection = direction;
currentRoundTotalProfit = 0;
currentRoundLoss = 0;
currentRoundEntryPrice = avgPrice;
Log("🆕 开始新一轮交易统计");
Log(" - 开始时间:", _D(currentRoundStartTime));
Log(" - 方向:", direction == "LONG" ? "🟢 多头" : "🔴 空头");
Log(" - 入场价格:", avgPrice.toFixed(pricePrecision));
} else {
// 滚仓时:更新方向(理论上应该相同,但为了健壮性还是更新)
currentRoundDirection = direction;
Log("🔄 滚仓操作 (第", currentRoundRolls, "次)");
Log(" - 方向:", direction == "LONG" ? "🟢 多头" : "🔴 空头");
Log(" - 入场价格:", avgPrice.toFixed(pricePrecision));
}
Log("✅ 开仓成功!");
Log(" - 成交均价:", avgPrice.toFixed(pricePrecision));
Log(" - 成交数量:", order.DealAmount);
Log(" - 成交金额:", (order.DealAmount * avgPrice * ctVal).toFixed(2), "U");
// 挂止盈单
Sleep(1000);
placeTakeProfitOrder(direction, avgPrice, order.DealAmount);
return true;
} else if (order.Status == 2) {
// 订单已取消
Log("❌ 订单已取消");
return false;
}
// Status == 0 表示未成交,继续等待
}
// 超时未成交
Log("⚠️ 订单超时,尝试取消订单");
exchange.CancelOrder(orderId);
return false;
}
// 挂止盈单 function placeTakeProfitOrder(direction, entryPrice, amount) { var takeProfitPrice = 0;
if (direction == "LONG") {
takeProfitPrice = _N(entryPrice * 1.1, pricePrecision); // 多头止盈:+10%
} else {
takeProfitPrice = _N(entryPrice * 0.9, pricePrecision); // 空头止盈:-10%
}
Log("📌 挂止盈单");
Log(" - 入场价格:", entryPrice.toFixed(pricePrecision));
Log(" - 止盈价格:", takeProfitPrice);
Log(" - 数量:", amount);
// 使用 CreateOrder 挂限价止盈单
if (direction == "LONG") {
takeProfitOrderId = exchange.CreateOrder(Symbol, "closebuy", takeProfitPrice, amount);
} else {
takeProfitOrderId = exchange.CreateOrder(Symbol, "closesell", takeProfitPrice, amount);
}
if (takeProfitOrderId) {
Log("✅ 止盈单已挂,订单ID:", takeProfitOrderId);
} else {
Log("❌ 止盈单挂单失败");
}
}
// 检查止盈单状态 function checkTakeProfitOrder() {
if (!takeProfitOrderId) {
return;
}
var order = exchange.GetOrder(takeProfitOrderId);
if (!order) {
return;
}
if (order.Status == 1) {
// 止盈单成交
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Log("💰 止盈单成交!");
Log(" - 成交价格:", order.AvgPrice.toFixed(pricePrecision));
Log(" - 成交数量:", order.DealAmount);
// 使用订单数据精确计算盈利
var profit = 0;
if (currentDirection == "LONG") {
// 多头盈利 = (止盈价 - 入场价) * 数量 * 合约面值
profit = (order.AvgPrice - entryPrice) * order.DealAmount * ctVal;
} else {
// 空头盈利 = (入场价 - 止盈价) * 数量 * 合约面值
profit = (entryPrice - order.AvgPrice) * order.DealAmount * ctVal;
}
// 计算盈利率
var profitRate = profit / strategyCapital;
Log("📊 盈利统计:");
Log(" - 入场价格:", entryPrice.toFixed(pricePrecision));
Log(" - 止盈价格:", order.AvgPrice.toFixed(pricePrecision));
Log(" - 本次盈利:", profit.toFixed(2), "U");
Log(" - 盈利率:", (profitRate * 100).toFixed(2), "%");
Log(" - 策略资金(盈利前):", strategyCapital.toFixed(2), "U");
// 更新资金
strategyCapital += profit;
totalProfitRealized += profit;
rollCount++;
Log(" - 策略资金(盈利后):", strategyCapital.toFixed(2), "U");
Log(" - 累计盈利:", totalProfitRealized.toFixed(2), "U");
Log(" - 滚仓次数:", rollCount, "次");
// ========== 累加本轮盈利 ==========
currentRoundTotalProfit += profit;
Log(" - 本轮累计盈利:", currentRoundTotalProfit.toFixed(2), "U");
// 重置止盈单ID
takeProfitOrderId = null;
// 获取最新K线计算EMA
Sleep(1000);
var records = _C(exchange.GetRecords, PERIOD_M1);
var emaFast = TA.EMA(records, FastEMA);
var emaSlow = TA.EMA(records, SlowEMA);
if (emaFast.length < 2 || emaSlow.length < 2) {
Log("⚠️ EMA数据不足,无法判断是否滚仓");
// 记录本轮滚仓结束(正常结束,之前有盈利)
saveRollRecord(false);
resetPositionState();
Log("⏳ 等待新信号...");
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
return;
}
var ema5_current = emaFast[emaFast.length - 1];
var ema10_current = emaSlow[emaSlow.length - 1];
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Log("📈 EMA滚仓判断:");
Log(" - EMA5:", ema5_current.toFixed(pricePrecision));
Log(" - EMA10:", ema10_current.toFixed(pricePrecision));
Log(" - 原持仓方向:", currentDirection);
var shouldRoll = false;
if (currentDirection == "LONG") {
// 多头止盈后,如果EMA5仍在EMA10上方,继续做多(滚仓)
if (ema5_current > ema10_current) {
shouldRoll = true;
Log(" - 判断结果: ✅ EMA5 > EMA10,趋势延续");
Log(" - 决策: 🔄 继续做多(滚仓)");
} else {
Log(" - 判断结果: ❌ EMA5 <= EMA10,趋势转弱");
Log(" - 决策: ⏸️ 不滚仓,等待新信号");
}
} else if (currentDirection == "SHORT") {
// 空头止盈后,如果EMA5仍在EMA10下方,继续做空(滚仓)
if (ema5_current < ema10_current) {
shouldRoll = true;
Log(" - 判断结果: ✅ EMA5 < EMA10,趋势延续");
Log(" - 决策: 🔄 继续做空(滚仓)");
} else {
Log(" - 判断结果: ❌ EMA5 >= EMA10,趋势转弱");
Log(" - 决策: ⏸️ 不滚仓,等待新信号");
}
}
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
if (shouldRoll) {
// ========== 滚仓:增加本轮滚仓次数 ==========
currentRoundRolls++;
Log("🔄 执行滚仓操作... (本轮第", currentRoundRolls, "次滚仓)");
Sleep(1000);
var ticker = _C(exchange.GetTicker);
var newPrice = ticker.Last;
if (openPosition(currentDirection, newPrice)) {
Log("✅ 滚仓成功!");
} else {
Log("❌ 滚仓失败,等待新信号");
// 记录本轮滚仓结束(滚仓失败,但之前有盈利)
saveRollRecord(false);
resetPositionState();
}
} else {
// ========== 不滚仓:记录本轮滚仓结束 ==========
saveRollRecord(false);
resetPositionState();
Log("⏳ 已平仓,等待新信号...");
}
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
}
}
// ========== 保存滚仓记录 ========== function saveRollRecord(isStopLoss) { // 必须有开始时间才记录(防止异常情况) if (currentRoundStartTime == 0) { Log(“⚠️ 本轮未正确初始化,跳过记录”); currentRoundRolls = 0; currentRoundTotalProfit = 0; currentRoundLoss = 0; currentRoundDirection = “”; currentRoundEntryPrice = 0; return; }
var endTime = Date.now();
var duration = endTime - currentRoundStartTime;
// 计算总体盈利 = 累计盈利 - 亏损
var netProfit = currentRoundTotalProfit - currentRoundLoss;
var record = {
direction: currentRoundDirection, // 本轮方向
roundRolls: currentRoundRolls, // 本轮滚仓次数
totalProfit: currentRoundTotalProfit, // 累计盈利(止盈累加)
loss: currentRoundLoss, // 亏损金额(止损)
netProfit: netProfit, // 总体盈利
duration: duration, // 持续时间(毫秒)
isStopLoss: isStopLoss, // 是否止损结束
startTime: currentRoundStartTime, // 开始时间
endTime: endTime, // 结束时间
entryPrice: currentRoundEntryPrice // 入场价格
};
rollHistory.push(record);
// 只保留最近N条记录
if (rollHistory.length > maxHistoryRecords) {
rollHistory.shift();
}
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Log("📝 保存滚仓记录:");
Log(" - 方向:", currentRoundDirection == "LONG" ? "🟢 多头" : "🔴 空头");
Log(" - 入场价格:", currentRoundEntryPrice.toFixed(pricePrecision));
Log(" - 开始时间:", _D(currentRoundStartTime));
Log(" - 结束时间:", _D(endTime));
Log(" - 持续时间:", formatDuration(duration));
Log(" - 本轮滚仓次数:", currentRoundRolls);
Log(" - 累计盈利:", currentRoundTotalProfit.toFixed(2), "U");
Log(" - 亏损金额:", currentRoundLoss.toFixed(2), "U");
Log(" - 总体盈利:", netProfit.toFixed(2), "U");
Log(" - 结束方式:", isStopLoss ? "❌ 止损" : "✅ 正常");
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
// 重置本轮统计数据
currentRoundRolls = 0;
currentRoundStartTime = 0;
currentRoundDirection = "";
currentRoundTotalProfit = 0;
currentRoundLoss = 0;
currentRoundEntryPrice = 0;
}
// 格式化时长 function formatDuration(ms) { var seconds = Math.floor(ms / 1000); var minutes = Math.floor(seconds / 60); var hours = Math.floor(minutes / 60); var days = Math.floor(hours / 24);
if (days > 0) {
return days + "天" + (hours % 24) + "时" + (minutes % 60) + "分";
} else if (hours > 0) {
return hours + "时" + (minutes % 60) + "分";
} else if (minutes > 0) {
return minutes + "分" + (seconds % 60) + "秒";
} else {
return seconds + "秒";
}
}
// 重置持仓状态函数 function resetPositionState() { entryPrice = 0; lastRollPrice = 0; currentDirection = “”; // 注意:不重置 rollCount、strategyCapital 和 currentRoundRolls }
// 检查止损 function checkStopLoss(currentPrice, position) { var totalDrawdown = 0;
if (currentDirection == "LONG") {
totalDrawdown = (currentPrice - entryPrice) / entryPrice;
} else {
totalDrawdown = (entryPrice - currentPrice) / entryPrice;
}
if (totalDrawdown < -StopLossPercent) {
Log("❌ 触发止损!回撤:", (totalDrawdown * 100).toFixed(2), "%");
// 取消止盈单
if (takeProfitOrderId) {
Log("取消止盈单:", takeProfitOrderId);
exchange.CancelOrder(takeProfitOrderId);
takeProfitOrderId = null;
Sleep(500);
}
// ========== 市价平仓(循环重试直到成功) ==========
var profit = closePositionMarketWithRetry(currentPrice, position);
// 更新策略资金池
strategyCapital += profit;
totalProfitRealized += profit;
Log("止损亏损:", profit.toFixed(2), "U");
Log("策略剩余资金:", strategyCapital.toFixed(2), "U");
Log("累计盈利:", totalProfitRealized.toFixed(2), "U");
// ========== 记录本轮止损亏损 ==========
currentRoundLoss = Math.abs(profit); // 转为正数保存
Log("本轮止损亏损:", currentRoundLoss.toFixed(2), "U");
// ========== 记录本轮滚仓结束(被止损中断) ==========
saveRollRecord(true);
// 重置状态
resetPositionState();
if (strategyCapital < 10) {
Log("💥 策略资金不足10U,停止运行");
throw "资金不足";
}
Log("⏳ 已止损,等待新信号...");
}
}
// ========== 市价平仓(带重试机制,直到成功) ========== function closePositionMarketWithRetry(currentPrice, position) { Log(“🔴 市价平仓(循环重试模式)”);
var maxRetries = 10; // 最多重试10次
var retryCount = 0;
while (retryCount < maxRetries) {
retryCount++;
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Log("🔄 第", retryCount, "次平仓尝试");
var profit = closePositionMarket(currentPrice, position);
// 如果返回值不为0,说明平仓成功
if (profit !== 0) {
Log("✅ 平仓成功!");
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
return profit;
}
// 平仓失败,检查持仓是否还存在
Sleep(2000);
var newPosition = _C(exchange.GetPosition);
if (newPosition.length == 0) {
Log("⚠️ 持仓已不存在,可能已被其他途径平仓");
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
return 0;
}
// 更新position和currentPrice
position = newPosition[0];
var ticker = _C(exchange.GetTicker);
currentPrice = ticker.Last;
Log("⚠️ 平仓失败,", (maxRetries - retryCount), "次重试机会剩余");
Log("等待3秒后重试...");
Sleep(3000);
}
// 所有重试都失败
Log("❌ 平仓失败!已达到最大重试次数");
Log("⚠️ 请手动检查持仓状态!");
Log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
return 0;
}
// 市价平仓(持续检测订单状态) function closePositionMarket(currentPrice, position) { Log(“📤 发起市价平仓订单”);
var pos = position;
var amount = pos.Amount;
if (pos.Type == PD_LONG) {
exchange.SetDirection("closebuy");
} else {
exchange.SetDirection("closesell");
}
// 市价平仓
var orderType = pos.Type == PD_LONG ? "closebuy" : "closesell";
var orderId = exchange.CreateOrder(Symbol, orderType, -1, amount);
if (!orderId) {
Log("❌ 平仓下单失败");
return 0;
}
Log("平仓订单ID:", orderId, "开始持续检测...");
// 持续检测订单状态
var maxWaitTime = 30000; // 单次等待最多30秒
var startTime = Date.now();
var checkCount = 0;
while (Date.now() - startTime < maxWaitTime) {
Sleep(500);
checkCount++;
var order = exchange.GetOrder(orderId);
if (!order) {
Log("❌ 无法获取订单信息(检测", checkCount, "次)");
continue;
}
if (order.Status == 1) {
// 平仓成功
Log("✅ 订单成交,成交价:", order.AvgPrice.toFixed(pricePrecision));