SAR指标解说注释版by刘风
//------------------------------------------------------------------------ // 简称: ParabolicSAR // 名称: 求抛物线转向 // 类别: 用户函数 // 类型: 内建函数 // 输出: 布尔型 //本公式为函数,请勿创建为公式 //------------------------------------------------------------------------ Params Numeric AfStep(0.02); //加速因子 Numeric AfLimit(0.2); //加速因子限量 NumericRef oParClose; //当前Bar停损值 NumericRef oParOpen; //下一Bar停损值 NumericRef oTbPosition; //持仓状态,1 - 多头,-1 - 空头 NumericRef oTransition; //是否反转,1 或 -1 反转,0 保持不变 Vars Series<Numeric> Af(0); Series<Numeric> ParOpen(0); Series<Numeric> TbPosition(0); Series<Numeric> HHValue(0); Series<Numeric> LLValue(0); Begin //Commentary("oParClose="+text(oParClose)); //Commentary("currentbar="+text(CurrentBar)); If (CurrentBar == 0) //初始化相关变量的值 { TbPosition = 1 ; //临时持仓状态设为1 oTransition = 1 ; //是否反转设为1 Af = AfStep ; //加速因子初始为0.02,注意这里用af变量来接受加速因子时因为af可能变化 HHValue = High ; //上轨记录值为最高价 LLValue = Low ; //下轨记录纸为最低价 oParClose = LLValue ; //当前bar停损值先默认设置成下轨 ParOpen = oParClose + Af * ( HHValue - oParClose) ;//下根k线的临时停损值为 当前k线停损值 + 加速因子 * (上轨记录 - 当前停损) If (ParOpen > Low) //判断下根临时停损值如果大于最低价 { ParOpen = Low ; //下根临时停损值等于最低价 } }Else //现在不是第一根bar的计算 { oTransition = 0 ; //是否反转先 记录为0 不观测 待后面计算 If (High > HHValue[1]) //如果最高价突破上轨 { HHValue = High; //将当前bar的最高价记录为HHValue }Else //如果最高价没突破上轨 { HHValue = HHValue[1];//直接传递上一根的值 } If (Low < LLValue[1]) //如果最低价突破下轨 { LLValue = Low; //将当前bar的最低价记录为LLValue }Else //如果最低价没突破下轨 { LLValue = LLValue[1];//直接传递上一根的值 } If ( TbPosition[1] == 1) //如果上一根bar的持仓状态为1 { If ( Low <= ParOpen[1]) //如果最低价小于上一根的预测停损值 { TbPosition = -1 ; //持仓状态设为-1 oTransition = -1 ; //转向状态变为-1,说明此时发生了从多持仓转向空持仓的状态 oParClose = HHValue ;//当前停损值设置位hhvalue,注意 这个上轨是从上一根传递过来的值,也就是实际上取上一根的上轨,而不是当前根 HHValue = High ; //当前根的上轨设置为当前bar的high LLValue = Low ; //当前根的下轨设置为当前bar的low Af = AfStep ; //设定步进长度 ParOpen = oParClose + Af * ( LLValue - oParClose ) ;//计算下一根预测值 If (ParOpen < High) { ParOpen = High ; } //修正预测值 If (ParOpen < High[1]) { ParOpen = High[1] ; } }Else { //如果最低价没有小于上一根的预测停损值,说明没有发生反转 TbPosition = TbPosition[1];//延续之前状态 oParClose = ParOpen[1] ; //延续之前状态 If (HHValue > HHValue[1] And Af[1] < AfLimit ) { If(Af[1]+AfStep > AfLimit) { Af = AfLimit ;//判断加速值是否超限, }Else { Af = Af[1]+AfStep; } //判断加速值是否超限,最多在aflimint }Else { Af = Af[1]; } ParOpen = oParClose + Af * ( HHValue - oParClose ) ; //计算预测值 If (ParOpen > Low) { ParOpen = Low ; } If (ParOpen > Low[1]) { ParOpen = Low[1]; } //修正预测值 } }Else //如果上一根状态不是1 { If (High >= ParOpen[1]) { TbPosition = 1 ;//发生反转,以下代码类似状态为1时的判断 oTransition = 1 ; oParClose = LLValue ; HHValue = High ; LLValue = Low ; Af = AfStep ; ParOpen = oParClose + Af * ( HHValue - oParClose) ; If (ParOpen > Low) { ParOpen = Low ; } If (ParOpen > Low[1]) { ParOpen = Low[1]; } }Else { TbPosition = TbPosition[1]; oParClose = ParOpen[1]; If (LLValue < LLValue[1] And Af[1] < AfLimit ) { If(Af[1]+AfStep > AfLimit) { Af = AfLimit ; }Else { Af = Af[1]+AfStep; } }Else { Af = Af[1]; } ParOpen = oParClose + Af * ( LLValue - oParClose ) ; If (ParOpen < High) { ParOpen = High ; } If (ParOpen < High[1]) { ParOpen = High[1] ; } } } } oParOpen = ParOpen; oTbPosition = TbPosition; Commentary("当前停损值="+text(oParClose)); Commentary("下根停损值="+text(oParOpen)); Commentary("持仓状态="+text(oTbPosition)); Commentary("是否反转="+text(oTransition)); PlotNumeric("HHValue",HHValue); PlotNumeric("LLValue",LLValue); Return True; End //------------------------------------------------------------------------ // 编译版本 GS2010.12.08 // 版权所有 TradeBlazer Software 2003-2025 // 更改声明 TradeBlazer Software保留对TradeBlazer平 // 台每一版本的TradeBlazer公式修改和重写的权利 //------------------------------------------------------------------------