//------------------------------------------------------------------------ // 简称: VIP17_duo // 名称: VIP17_duo // 类别: 公式应用 // 类型: 用户应用 // 输出: Void //------------------------------------------------------------------------ Params Numeric Fund(20000); // 投入保证金 Vars Numeric Lots(0); // 交易手数 Numeric totalScore(5); // 开仓分数阈值,目前都是等权分配,总共6个指标及6分。 Numeric rpmPeriod(14); // RPM Period Numeric bboPeriod(16); // BBO Period Numeric macdFastPeriod(12); // MACD Fast Period Numeric macdSlowPeriod(24); // MACD Slow Period Numeric macdSignalPeriod(9); // MACD Signal Period Numeric rsiPeriod(14); // RSI Period Numeric cciPeriod(14); // CCI Period Numeric stochasticKLength(14); // Stochastic %K Length Numeric stochasticKSmoothing(1); // Stochastic %K Smoothing Numeric stochasticDSmoothing(3); // Stochastic %D Smoothing Numeric supertrendPeriod(10); // SUPERTREND Period Numeric supertrendFactor(2); // SUPERTREND Factor Series rpmValue; // RPM Value Series bboValue; // BBO Value Series macdValue; // MACD Value Series macdSignal; // MACD Signal Line Series macdHist; // MACD Histogram Series rsiValue; // RSI Value Series stochasticValue; // Stochastic Value Series cciValue; // CCI Value Series supertrendValue; // SUPERTREND Value Series netChgAvg ; Series totChgAvg ; Series score; Numeric EntryStrength(90); // 趋势强度的进场值 Numeric Length(5); // 强弱指标和通道计算的周期值 Series CloseChange; // 收盘价变动值 Numeric i; // 循环控制变量 Numeric UpCloses; // 收盘价上涨累计值 Numeric DnCloses; // 收盘价下跌累计值 Numeric SumChange; // 收盘价变动累计值 Series MarketStrength; // 市场强弱指标 Plot plt1; // 用于画图的对象 Events OnInit() { Range[0:DataCount-1] { //=========数据源相关设置============== AddDataFlag(Enum_Data_RolloverBackWard()); //设置后复权 AddDataFlag(Enum_Data_RolloverRealPrice()); //设置映射真实价格 AddDataFlag(Enum_Data_AutoSwapPosition()); //设置自动换仓 //AddDataFlag(Enum_Data_IgnoreSwapSignalCalc()); //设置忽略换仓信号计算 } // 创建第一个副图 plt1.figure(0); // 独立副图区域 plt1.setOption("Score", "width", Enum_7Pix); // 将指标“MA1”的输出线宽设置为3像素 plt1.setOption("Score","color",Blue); } OnBar(ArrayRef indexs) { // 计算交易手数 Lots = Max(1, Round(Fund/(O*ContractUnit*BigPointValue* MarginRatio/rollover), 0)); // 计算市场强弱指标 CloseChange = Close - Close[1]; UpCloses = 0; DnCloses = 0; For i = 0 To Length-1 { // 收盘价上涨计入涨幅累计 If(CloseChange[i] > 0) UpCloses = UpCloses + CloseChange[i]; // 否则计入跌幅累计 Else DnCloses = DnCloses + CloseChange[i]; } // 计算周期内涨跌 SumChange = Summation(CloseChange, Length); // 周期内上涨,计算上涨强度,0-100之间 If(SumChange >= 0) { MarketStrength = SumChange / UpCloses * 100; } // 周期内下跌,计算下跌强度,0-100之间 Else { MarketStrength = SumChange / Abs(DnCloses) * 100; } // 计算 MACD 值 Numeric emaFast = XAverage(Close, macdFastPeriod); Numeric emaSlow = XAverage(Close, macdSlowPeriod); macdValue = emaFast - emaSlow; macdSignal = XAverage(macdValue, macdSignalPeriod); macdHist = macdValue - macdSignal; // 计算 RSI 值 Numeric SF = 1 / rsiPeriod; Numeric change = Close - Close[1]; If(CurrentBar <= rsiPeriod - 1) { netChgAvg = (Close - Close[rsiPeriod]) / rsiPeriod; totChgAvg = Average(Abs(Close - Close[1]), rsiPeriod); } Else { netChgAvg = netChgAvg[1] + SF * (change - netChgAvg[1]); totChgAvg = totChgAvg[1] + SF * (Abs(change) - totChgAvg[1]); } If(totChgAvg <> 0) { rsiValue = 50 * (netChgAvg / totChgAvg + 1); } Else { rsiValue = 50; } // 计算 Stochastic 值 Numeric highestHigh = Highest(High, stochasticKLength); Numeric lowestLow = Lowest(Low, stochasticKLength); Numeric stochasticRaw = (Close - lowestLow) / (highestHigh - lowestLow) * 100; stochasticValue = Average(stochasticRaw, stochasticKSmoothing); // 计算 CCI 值 Numeric typicalPrice = (High + Low + Close) / 3; Numeric smaTypicalPrice = Average(typicalPrice, cciPeriod); Numeric meanDeviation = Summation(Abs(typicalPrice - smaTypicalPrice), cciPeriod) / cciPeriod; If(meanDeviation <> 0) { cciValue = (typicalPrice - smaTypicalPrice) / (0.015 * meanDeviation); } Else { cciValue = 0; } // 计算 SUPERTREND 值 Numeric atr = AvgTrueRange(supertrendPeriod); Numeric basicUpperBand = (High + Low) / 2 + supertrendFactor * atr; Numeric basicLowerBand = (High + Low) / 2 - supertrendFactor * atr; Numeric finalUpperBand = IIF(basicUpperBand < supertrendValue[1] || Close[1] > supertrendValue[1], basicUpperBand, supertrendValue[1]); Numeric finalLowerBand = IIF(basicLowerBand > supertrendValue[1] || Close[1] < supertrendValue[1], basicLowerBand, supertrendValue[1]); supertrendValue = IIF(Close > supertrendValue[1], finalLowerBand, finalUpperBand); // 设置多头条件 Bool macdCondition = macdValue > macdSignal; Bool rsiCondition = rsiValue >50; Bool stochasticCondition = stochasticValue > 50; Bool cciCondition = cciValue >0; Bool supertrendCondition = Close > supertrendValue; Bool Marketbool = MarketStrength >= EntryStrength; // 计算得分 score = 0; If(macdCondition) { score = score + 1; } If(rsiCondition) { score = score + 1; } If(stochasticCondition) { score = score + 1; } If(cciCondition) { score = score + 1; } If(supertrendCondition) { score = score + 1; } if(Marketbool) { score = score + 1; } // 在副图显示得分 Commentary("Score: " + Text(score)); plt1.barv("Score",score); // 判断是否达到条件数量 If(score == totalScore) { Commentary("All conditions met!"); } // 开多条件 If(score[1] >= totalScore And MarketPosition == 0 ) // { Buy(Lots, Open); } // 平仓条件 If(score[1] == 0 And MarketPosition == 1) { Sell(0, Open); } // 在副图显示条件成立和不成立 If(macdCondition) { PlotAuto("MACD_Condition",1,1,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("MACD_Condition",1,1,Red,Enum_Line,Enum_Solid,Enum_7Pix); } If(rsiCondition) { PlotAuto("RSI_Condition",2,2,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("RSI_Condition",2,2,Red,Enum_Line,Enum_Solid,Enum_7Pix); } If(stochasticCondition) { PlotAuto("Stochastic_Condition", 3,3,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("Stochastic_Condition", 3,3,Red,Enum_Line,Enum_Solid,Enum_7Pix); } If(cciCondition) { PlotAuto("CCI_Condition", 4,4,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("CCI_Condition", 4,4,Red,Enum_Line,Enum_Solid,Enum_7Pix); } If(supertrendCondition) { PlotAuto("Supertrend_Condition", 5,5,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("Supertrend_Condition", 5,5,Red,Enum_Line,Enum_Solid,Enum_7Pix); } If(Marketbool) { PlotAuto("Marketbool", 6,6,Green,Enum_Line,Enum_Solid,Enum_7Pix); } Else { PlotAuto("Marketbool", 6,6,Red,Enum_Line,Enum_Solid,Enum_7Pix); } } //------------------------------------------------------------------------ // 编译版本 2024/11/27 145803 // 版权所有 songshu123 // 更改声明 TradeBlazer Software保留对TradeBlazer平台 // 每一版本的TradeBlazer公式修改和重写的权利 //------------------------------------------------------------------------