#property copyright "Your Author"#property link "Your Website"#property version "1.4"// 输入参数input int maShortPeriod = 5; // 短期均线周期input int maMediumPeriod = 10; // 中期均线周期input int maLongPeriod = 20; // 长期均线周期input bool useMaCross = true; // 是否使用均线交叉开仓开关input int kdjPeriod = 9; // KDJ 指标周期input int kdjSlowPeriod = 3; // KDJ 慢速周期input int kdjSmoothPeriod = 3; // KDJ 平滑周期input bool useKdjCross = false; // 是否使用 KDJ 交叉开仓开关input int rsiPeriod = 14; // RSI 指标周期input int rsiBuyLevel = 30; // RSI 上穿水平开多input int rsiSellLevel = 70; // RSI 下穿水平开空input bool useRsi = true; // 是否使用 RSI 开仓开关input double initialLotSize = 0.01; // 初始开仓手数input double incrementLotSize = 0.01; // 每次加仓手数递增量input double multiplierLotSize = 2.0; // 倍投手数乘数input bool useIncrement = true; // 使用递增加仓还是倍投加仓开关input double takeProfitPips = 50; // 止盈点数input double stopLossPips = 30; // 止损点数input double breakevenPips = 10; // 保本盈利点数开启保本移动止损input double trailingDistancePips = 10; // 价格距离多少移动止损多少input bool enableReverseClose = true; // 是否启用反方向信号平仓double lotSize = initialLotSize;int totalPositions = 0;int maDirection = 0; // 0 - 无方向,1 - 多头,-1 - 空头int kdjDirection = 0;int rsiDirection = 0;// 计算均线double maShortValue, maMediumValue, maLongValue;void CalculateMAs(){ maShortValue = iMA(Symbol(),0,maShortPeriod,0,MODE_SMA,PRICE_CLOSE); maMediumValue = iMA(Symbol(),0,maMediumPeriod,0,MODE_SMA,PRICE_CLOSE); maLongValue = iMA(Symbol(),0,maLongPeriod,0,MODE_SMA,PRICE_CLOSE);}// 计算 KDJint k,j,d;void CalculateKDJ(){ Stochastic(0,0,kdjPeriod,kdjSlowPeriod,kdjSmoothPeriod,MODE_SMA,0,k,j,d);}// 计算 RSIdouble rsiValue;void CalculateRSI(){ rsiValue = iRSI(Symbol(),0,rsiPeriod,PRICE_CLOSE);}void OnTick(){ CalculateMAs(); CalculateKDJ(); CalculateRSI(); // 判断均线交叉方向 if(maShortValue > maMediumValue && maMediumValue > maLongValue) { maDirection = 1; } else if(maShortValue < maMediumValue && maMediumValue < maLongValue) { maDirection = -1; } else { maDirection = 0; } // 判断 KDJ 交叉方向 if(k > d && j > d) { kdjDirection = 1; } else if(k < d && j < d) { kdjDirection = -1; } else { kdjDirection = 0; } // 判断 RSI 方向 if(rsiValue > rsiBuyLevel && rsiDirection!= 1) { rsiDirection = 1; } else if(rsiValue < rsiSellLevel && rsiDirection!= -1) { rsiDirection = -1; } if(totalPositions == 0) { // 开仓条件 if((useMaCross && maDirection > 0) || (useKdjCross && kdjDirection > 0) || (useRsi && rsiDirection > 0)) { int orderResult = OrderSend(Symbol(),OP_BUY,lotsize,Bid,3,0,0,"MyStrategy",0,0,clrNone); if(orderResult == ORDER_OK) { totalPositions++; } else { Print("开仓失败,错误代码:", orderResult); } } else if((useMaCross && maDirection < 0) || (useKdjCross && kdjDirection < 0) || (useRsi && rsiDirection < 0)) { int orderResult = OrderSend(Symbol(),OP_SELL,lotsize,Ask,3,0,0,"MyStrategy",0,0,clrNone); if(orderResult == ORDER_OK) { totalPositions--; } else { Print("开仓失败,错误代码:", orderResult); } } } else if(totalPositions > 0) { // 多头持仓处理 double currentPrice = Bid; double profit = (currentPrice - OrderOpenPrice()) * Point; if(profit >= takeProfitPips) { // 止盈平仓 CloseAllBuyPositions(); totalPositions = 0; } else if(profit <= -stopLossPips) { // 止损平仓 CloseAllBuyPositions(); totalPositions = 0; } else if(profit >= breakevenPips) { // 保本移动止损 ModifyStopLossToBreakeven(); } else { // 判断加仓条件 if((useMaCross && maDirection > 0) || (useKdjCross && kdjDirection > 0) || (useRsi && rsiDirection > 0)) { if(useIncrement) { lotSize += incrementLotSize; } else { lotSize *= multiplierLotSize; } int orderResult = OrderSend(Symbol(),OP_BUY,lotsize,Bid,3,0,0,"MyStrategy",0,0,clrNone); if(orderResult == ORDER_OK) { totalPositions++; } else { Print("加仓失败,错误代码:", orderResult); } } } // 判断反方向信号平仓 if(enableReverseClose && ((useMaCross && maDirection < 0) || (useKdjCross && kdjDirection < 0) || (useRsi && rsiDirection < 0))) { CloseAllBuyPositions(); totalPositions = 0; } } else if(totalPositions < 0) { // 空头持仓处理 double currentPrice = Ask; double profit = (OrderOpenPrice() - currentPrice) * Point; if(profit >= takeProfitPips) { // 止盈平仓 CloseAllSellPositions(); totalPositions = 0; } else if(profit <= -stopLossPips) { // 止损平仓 CloseAllSellPositions(); totalPositions = 0; } else if(profit >= breakevenPips) { // 保本移动止损 ModifyStopLossToBreakeven(); } else { // 判断加仓条件 if((useMaCross && maDirection < 0) || (useKdjCross && kdjDirection < 0) || (useRsi && rsiDirection < 0)) { if(useIncrement) { lotSize += incrementLotSize; } else { lotSize *= multiplierLotSize; } int orderResult = OrderSend(Symbol(),OP_SELL,lotsize,Ask,3,0,0,"MyStrategy",0,0,clrNone); if(orderResult == ORDER_OK) { totalPositions--; } else { Print("加仓失败,错误代码:", orderResult); } } } // 判断反方向信号平仓 if(enableReverseClose && ((useMaCross && maDirection > 0) || (useKdjCross && kdjDirection > 0) || (useRsi && rsiDirection > 0))) { CloseAllSellPositions(); totalPositions = 0; } } // 移动止损 if(totalPositions > 0) { double currentPrice = Bid; double trailingStop = OrderOpenPrice() + trailingDistancePips * Point; if(currentPrice > trailingStop) { ModifyStopLossToTrailing(currentPrice); } } else if(totalPositions < 0) { double currentPrice = Ask; double trailingStop = OrderOpenPrice() - trailingDistancePips * Point; if(currentPrice < trailingStop) { ModifyStopLossToTrailing(currentPrice); } }}void CloseAllBuyPositions(){ for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY) { int closeResult = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNone); if(closeResult!= ORDER_OK) { Print("平仓失败,错误代码:", closeResult); } } }}void CloseAllSellPositions(){ for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL) { int closeResult = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrNone); if(closeResult!= ORDER_OK) { Print("平仓失败,错误代码:", closeResult); } } }}void ModifyStopLossToBreakeven(){ for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY) { double breakevenPrice = OrderOpenPrice(); int modifyResult = OrderModify(OrderTicket(),OrderOpenPrice(),breakevenPrice,OrderTakeProfit(),0,clrNone); if(modifyResult!= ORDER_OK) { Print("修改止损为保本失败,错误代码:", modifyResult); } } else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL) { double breakevenPrice = OrderOpenPrice(); int modifyResult = OrderModify(OrderTicket(),OrderOpenPrice(),breakevenPrice,OrderTakeProfit(),0,clrNone); if(modifyResult!= ORDER_OK) { Print("修改止损为保本失败,错误代码:", modifyResult); } } }}void ModifyStopLossToTrailing(double currentPrice){ for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY) { double trailingStop = currentPrice - trailingDistancePips * Point; int modifyResult = OrderModify(OrderTicket(),OrderOpenPrice(),trailingStop,OrderTakeProfit(),0,clrNone); if(modifyResult!= ORDER_OK) { Print("修改移动止损失败,错误代码:", modifyResult); } } else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL) { double trailingStop = currentPrice + trailingDistancePips * Point; int modifyResult = OrderModify(OrderTicket(),OrderOpenPrice(),trailingStop,OrderTakeProfit(),0,clrNone); if(modifyResult!= ORDER_OK) { Print("修改移动止损失败,错误代码:", modifyResult); } } }} |