随机指标策略(TB版)_第1页
随机指标策略(TB版)_第2页
随机指标策略(TB版)_第3页
随机指标策略(TB版)_第4页
随机指标策略(TB版)_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

随机指标策略(TB平台)该交易策略主要基于随机指标(StochasticOscillator)和简单移动平均线(SimpleMovingAverage,SMA)进行交易决策。概述:1.随机指标(K值和D值):-使用最高价和最低价以及收盘价来计算随机指标的K值和D值。K值表示当前周期的收盘价与过去N周期内的最低价相比,相对于同期内最高价与最低价的范围所处的位置(以百分比表示)。-D值是K值的移动平均值,用于平滑K值的波动。2.简单移动平均线:-使用收盘价计算一个3周期的简单移动平均线,作为价格趋势的参考。3.交易信号:-买入信号:当市场没有多头仓位,且前一周期的K值大于前一周期的D值,并且前一周期的收盘价高于3周期简单移动平均线时,执行买入操作。-卖出信号:当市场持有多头仓位,且前一周期的收盘价低于3周期简单移动平均线时,执行卖出操作。4.集合竞价阶段处理:-在策略开始执行交易逻辑之前,先检查是否处于集合竞价阶段。如果是集合竞价阶段,则不执行任何交易操作。原版代码解释:VarsNumericMinPoint;//声明变量MinPoint,一个最小变动单位,也就是一跳。NumericMyEntryPrice;//声明变量MyEntryPrice,英文好的,看英文意思都知道的,开仓价格,本例是开仓均价,也可根据需要设置为某次入场的价格。NumericTrailingStart1(50);//声明变量TrailingStart1,初始值为50,这是止盈启动设置1。NumericTrailingStart2(80);//声明变量TrailingStart2,初始值为80,这是止盈启动设置2。NumericTrailingStop1(30);//声明变量TrailingStop1,初始值为30,这是真正跟踪止盈设置1。NumericTrailingStop2(20);//声明变量TrailingStop2,初始值为20,这是真正跟踪止盈设置2。NumericStopLossSet(50);//声明变量StopLossSet,初始值为50,这是止损设置。NumericMyExitPrice;//声明变量MyExitPrice,平仓价格。NumericSeriesHighestAfterEntry;//声明序列变量HighestAfterEntry,开仓后出现的最高价。NumericSeriesLowestAfterEntry;//声明序列变量LowestAfterEntry,开仓后出现的最低价。Begin...If(BarsSinceentry==0)//获得当前持仓的第一个建仓位置到当前位置的Bar计数,这里就是开仓的那根k线。{HighestAfterEntry=Close;//开仓后的最高价为收盘价。LowestAfterEntry=Close;//开仓后的最低价为收盘价。If(MarketPosition<>0)//假如有持仓的情况下。{HighestAfterEntry=Max(HighestAfterEntry,AvgEntryPrice);//开仓的Bar(通常说的k线),将开仓价和当时的收盘价的较大值保留到HighestAfterEntry。LowestAfterEntry=Min(LowestAfterEntry,AvgEntryPrice);//开仓的Bar,将开仓价和当时的收盘价的较小值保留到LowestAfterEntry。}}else//这个对应着不是开仓k线之后的情况。{HighestAfterEntry=Max(HighestAfterEntry,High);//这个是逐个判断的,即开仓那条K线,跟下一根k线最高价对比,记录当前的最高点;再接着继续对比,直到符合止盈启动设置条件,这样好用于下一个Bar的跟踪止盈判断。LowestAfterEntry=Min(LowestAfterEntry,Low);//简略说就是,记录下当前k线的最低点,用于下一个K线的跟踪止盈判断。}Commentary("HighestAfterEntry="+Text(HighestAfterEntry));//在超级图表当前Bar添加一行注释信息,解读意思就是在K线图上显示最高价HighestAfterEntry等于多少Commentary("LowestAfterEntry="+Text(LowestAfterEntry));//解读同上,显示最低价。MinPoint=MinMove*PriceScale;//固定的最小跳动价公式。MyEntryPrice=AvgEntryPrice;//把开仓均价赋值给MyEntryPrice。If(MarketPosition==1)//有多仓的情况下{If(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart2*MinPoint)//假如最高价大于等于开仓均价加上固定的止盈启动设置2乘以最小跳动价{If(Low<=HighestAfterEntry[1]-TrailingStop2*MinPoint)//再接着假如低价小于等于最高价减去真正止盈设置2系数乘以最小跳动价。{MyExitPrice=HighestAfterEntry[1]-TrailingStop2*MinPoint;//平仓价呢等于最高价减去真正止盈设置2系数乘以最小跳动价。If(Open<MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。Sell(0,MyExitPrice);//以上面计算得的平仓价,平仓。}}elseif(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart1*MinPoint)//第一级跟踪止盈的条件表达式。解读同上,这里把设置2变成设置1{If(Low<=HighestAfterEntry[1]-TrailingStop1*MinPoint)//解读同上。{MyExitPrice=HighestAfterEntry[1]-TrailingStop1*MinPoint;//平仓价,主要计算就是那个止盈系数设置1,解读同上。If(Open<MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。Sell(0,MyExitPrice);//依据算得的平仓价,平掉。}}elseif(Low<=MyEntryPrice-StopLossSet*MinPoint)//这里写上初始的固定止损处理,即为低价小于等于开仓均价减去固定止损系数乘以最小跳动价。{MyExitPrice=MyEntryPrice-StopLossSet*MinPoint;//平仓价就等于开仓价减去止损系数乘以一跳。If(Open<MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。Sell(0,MyExitPrice);//依据算得的平仓价,全平。}}elseif(MarketPosition==-1)//有空仓的情况{If(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart2*MinPoint)//第二级跟踪止盈的条件表达式,即最低价小于等于开仓价减去止盈启动设置2乘以最小跳动价。{If(High>=LowestAfterEntry[1]+TrailingStop2*MinPoint)//假如高价大于等于最低价加上真正止盈设置2乘以最小跳动价。{MyExitPrice=LowestAfterEntry[1]+TrailingStop2*MinPoint;//平仓价等于最低价加上真正止盈设置2乘以最小跳动价。If(Open>MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。BuyToCover(0,MyExitPrice);//平仓。}}elseif(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart1*MinPoint)//第一级跟踪止盈的条件表达式,基本就是把止盈设置2都改成设置,解读同上。{If(High>=LowestAfterEntry[1]+TrailingStop1*MinPoint)//解读同上。{MyExitPrice=LowestAfterEntry[1]+TrailingStop1*MinPoint;//计算平仓价。//If(Open>MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。BuyToCover(0,MyExitPrice);//平仓。}}elseIf(High>=MyEntryPrice+StopLossSet*MinPoint)//初始的止损处理,高价大于开仓均价加上固定止损系数乘以最小跳动价。{MyExitPrice=MyEntryPrice+StopLossSet*MinPoint;//平仓价等于开仓均价加上止损系数乘以一跳。If(Open>MyExitPrice)MyExitPrice=Open;//如果该Bar开盘价有跳空触发,则用开盘价代替。BuyToCover(0,MyExitPrice);//平仓。}}...End策略代码:(原版)VarsNumericMinPoint;NumericMyEntryPrice;NumericTrailingStart1(50);NumericTrailingStart2(80);NumericTrailingStop1(30);NumericTrailingStop2(20);NumericStopLossSet(50);NumericMyExitPrice;NumericSeriesHighestAfterEntry;NumericSeriesLowestAfterEntry;BeginIf(BarsSinceentry==0){HighestAfterEntry=Close;LowestAfterEntry=Close;If(MarketPosition<>0){HighestAfterEntry=Max(HighestAfterEntry,AvgEntryPrice);LowestAfterEntry=Min(LowestAfterEntry,AvgEntryPrice);}}else{HighestAfterEntry=Max(HighestAfterEntry,High);LowestAfterEntry=Min(LowestAfterEntry,Low);}Commentary("HighestAfterEntry="+Text(HighestAfterEntry));Commentary("LowestAfterEntry="+Text(LowestAfterEntry));MinPoint=MinMove*PriceScale;MyEntryPrice=AvgEntryPrice;If(MarketPosition==1){If(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart2*MinPoint){If(Low<=HighestAfterEntry[1]-TrailingStop2*MinPoint){MyExitPrice=HighestAfterEntry[1]-TrailingStop2*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart1*MinPoint){If(Low<=HighestAfterEntry[1]-TrailingStop1*MinPoint){MyExitPrice=HighestAfterEntry[1]-TrailingStop1*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(Low<=MyEntryPrice-StopLossSet*MinPoint){MyExitPrice=MyEntryPrice-StopLossSet*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(MarketPosition==-1){If(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart2*MinPoint){If(High>=LowestAfterEntry[1]+TrailingStop2*MinPoint){MyExitPrice=LowestAfterEntry[1]+TrailingStop2*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}elseif(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart1*MinPoint){If(High>=LowestAfterEntry[1]+TrailingStop1*MinPoint){MyExitPrice=LowestAfterEntry[1]+TrailingStop1*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}elseIf(High>=MyEntryPrice+StopLossSet*MinPoint){MyExitPrice=MyEntryPrice+StopLossSet*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}End修改一下,加点条件的逻辑代码:ParamsNumericLength(14);NumericSlowLength(3);NumericSmoothLength(3);NumericDslowLength(200);VarsNumericSeriesHighestValue;NumericSeriesLowestValue;NumericSeriesKValue;NumericSumHLValue;NumericSumCLValue;NumericSeriesDValue;NumericSeriesAvgValue3;NumericMinPoint;NumericMyEntryPrice;NumericTrailingStart1(50);NumericTrailingStart2(100);NumericTrailingStop1(30);NumericTrailingStop2(20);NumericStopLossSet(50);NumericMyExitPrice;NumericSeriesHighestAfterEntry;NumericSeriesLowestAfterEntry;BeginAvgValue3=AverageFC(Close,DslowLength);PlotNumeric("MA3",AvgValue3);HighestValue=HighestFC(High,Length);LowestValue=LowestFC(Low,Length);SumHLValue=SummationFC(HighestValue-LowestValue,SlowLength);SumCLValue=SummationFC(Close-LowestValue,SlowLength);If(SumHLValue<>0){KValue=SumCLValue/SumHLValue*100;}Else{KValue=0;}DValue=AverageFC(KValue,SmoothLength);If(!CallAuctionFilter())Return;If(MarketPosition<>1AndKValue[1]>DValue[1]AndClose[1]>AvgValue3){Buy(1,Open);}If(MarketPosition==1AndClose[1]<AvgValue3){Sell(1,Open);}If(MarketPosition<>-1AndKValue[1]<DValue[1]AndClose[1]<AvgValue3){SellShort(1,Open);}If(MarketPosition==-1AndClose[1]>AvgValue3){BuyToCover(1,Open);}If(BarsSinceentry==0){HighestAfterEntry=Close;LowestAfterEntry=Close;If(MarketPosition<>0){HighestAfterEntry=Max(HighestAfterEntry,AvgEntryPrice);LowestAfterEntry=Min(LowestAfterEntry,AvgEntryPrice);}}else{HighestAfterEntry=Max(HighestAfterEntry,High);LowestAfterEntry=Min(LowestAfterEntry,Low);}Commentary("HighestAfterEntry="+Text(HighestAfterEntry));Commentary("LowestAfterEntry="+Text(LowestAfterEntry));MinPoint=MinMove*PriceScale;MyEntryPrice=AvgEntryPrice;If(MarketPosition==1){If(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart2*MinPoint)}If(Low<=HighestAfterEntry[1]-TrailingStop2*MinPoint){MyExitPrice=HighestAfterEntry[1]-TrailingStop2*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart1*MinPoint){If(Low<=HighestAfterEntry[1]-TrailingStop1*MinPoint){MyExitPrice=HighestAfterEntry[1]-TrailingStop1*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(Low<=MyEntryPrice-StopLossSet*MinPoint){MyExitPrice=MyEntryPrice-StopLossSet*MinPoint;If(Open<MyExitPrice)MyExitPrice=Open;Sell(0,MyExitPrice);}}elseif(MarketPosition==-1){If(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart2*MinPoint){If(High>=LowestAfterEntry[1]+TrailingStop2*MinPoint){MyExitPrice=LowestAfterEntry[1]+TrailingStop2*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}elseif(LowestAfterEntry[1]<=MyEntryPrice-TrailingStart1*MinPoint){If(High>=LowestAfterEntry[1]+TrailingStop1*MinPoint){MyExitPrice=LowestAfterEntry[1]+TrailingStop1*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}elseIf(High>=MyEntryPrice+StopLossSet*MinPoint){MyExitPrice=MyEntryPrice+StopLossSet*MinPoint;If(Open>MyExitPrice)MyExitPrice=Open;BuyToCover(0,MyExitPrice);}}End修改后的代码注解Params//参数定义NumericLength(14);//长度参数NumericSlowLength(3);//慢速长度参数NumericSmoothLength(3);//平滑长度参数NumericDslowLength(200);//慢速平均周期参数Vars//变量声明NumericSeriesHighestValue;//最高值序列NumericSeriesLowestValue;//最低值序列NumericSeriesKValue;//K线值序列NumericSumHLValue;//最高值和最低值之差的累加NumericSumCLValue;//收盘价与最低值之差的累加NumericSeriesDValue;//D线值序列NumericSeriesAvgValue3;//3周期平均值序列NumericMinPoint;//最小变动单位NumericMyEntryPrice;//我的开仓价格NumericTrailingStart1(50);//追踪止损启动值1NumericTrailingStart2(100);//追踪止损启动值2NumericTrailingStop1(30);//追踪止损值1NumericTrailingStop2(20);//追踪止损值2NumericStopLossSet(50);//止损设置NumericMyExitPrice;//我的平仓价格NumericSeriesHighestAfterEntry;//开仓后的最高价序列NumericSeriesLowestAfterEntry;//开仓后的最低价序列Begin//开始策略逻辑AvgValue3=AverageFC(Close,DslowLength);//计算3周期平均值PlotNumeric("MA3",AvgValue3);//绘制3周期平均值//计算最高值和最低值序列HighestValue=HighestFC(High,Length);LowestValue=LowestFC(Low,Length);//计算K线值和D线值SumHLValue=SummationFC(HighestValue-LowestValue,SlowLength);SumCLValue=SummationFC(Close-LowestValue,SlowLength);If(SumHLValue<>0){KValue=SumCLValue/SumHLValue*100;//计算K值}Else{KValue=0;//如果分母为0,则K值设为0}DValue=AverageFC(KValue,SmoothLength);//计算D值//检查是否处于集合竞价阶段,如果是则返回If(!CallAuctionFilter())Return;//交易逻辑If(MarketPosition<>1AndKValue[1]>DValue[1]AndClose[1]>AvgValue3){Buy(1,Open);//如果当前无多仓,K值大于D值且收盘价高于3周期平均值,则买入}If(MarketPosition==1AndClose[1]<AvgValue3){Sell(1,Open);//如果当前持有多仓,且收盘价低于3周期平均值,则卖出}If(MarketPosition<>-1AndKValue[1]<DValue[1]AndClose[1]<AvgValue3){SellShort(1,Open);//如果当前无空仓,K值小于D值且收盘价低于3周期平均值,则卖出做空}If(MarketPosition==-1AndClose[1]>AvgValue3){BuyToCover(1,Open);//如果当前持有空仓,且收盘价高于3周期平均值,则平空仓}//处理开仓后的逻辑If(BarsSinceentry==0){HighestAfterEntry=Close;//开仓后的最高价设为当前收盘价LowestAfterEntry=Close;//开仓后的最低价设为当前收盘价If(MarketPosition<>0){HighestAfterEntry=Max(HighestAfterEntry,AvgEntryPrice);//更新开仓后的最高价LowestAfterEntry=Min(LowestAfterEntry,AvgEntryPrice);//更新开仓后的最低价}}else{HighestAfterEntry=Max(HighestAfterEntry,High);//更新最高价序列LowestAfterEntry=Min(LowestAfterEntry,Low);//更新最低价序列}//在图表上添加注释Commentary("HighestAfterEntry="+Text(HighestAfterEntry));Commentary("LowestAfterEntry="+Text(LowestAfterEntry));//计算最小变动单位和开仓价格MinPoint=MinMove*PriceScale;MyEntryPrice=AvgEntryPrice;//多仓情况下的退出逻辑If(MarketPosition==1){If(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart2*MinPoint){If(Low<=HighestAfterEntry[1]-TrailingStop2*MinPoint){MyExitPrice=HighestAfterEntry[1]-TrailingStop2*MinPoint;//计算平仓价格If(Open<MyExitPrice)MyExitPrice=Open;//如果有跳空,则使用开盘价Sell(0,MyExitPrice);//执行卖出}}elseif(HighestAfterEntry[1]>=MyEntryPrice+TrailingStart1*MinPoint){If(Low<=HighestAfterEntry[1]-TrailingStop1*MinPoint){MyExitPrice=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论