菲阿里四价tbquant源码

Params
    	Numeric M(35); //计算的周期数 
    	Numeric SS(1); //手数
    	Numeric TR(5);//跟踪止损比例
Vars
	Series<Numeric> Nop;
	Series<Numeric> HH1;
	Series<Numeric> LL1;
	Series<Numeric> CC1;
	Series<Numeric> P;
	Series<Numeric> R1;
	Series<Numeric> R2;
	Series<Numeric> R3;
	Series<Numeric> S1;
	Series<Numeric> S2;
	Series<Numeric> S3;
	
	Series<Numeric> RM1;
	Series<Numeric> RM2;
	Series<Numeric> RM3;
	Series<Numeric> SM1;
	Series<Numeric> SM2;
	Series<Numeric> SM3;
	Series<Numeric> SY;
	Series<Numeric> XY;
	Series<Numeric> LowerAfterEntry;
	Series<Numeric> HigherAfterEntry;
	Series<Numeric> Myprice;
	Series<Numeric> Myprice2;
	
	
	Series<Bool> bartoday;
	Series<Bool> bartoday2;
	Series<Bool> Condition1;
	Series<Bool> Condition2;
	Series<Bool> Condition3;
	Series<Bool> Condition4;
	Series<Bool> Condition5;
	Series<Bool> Condition6;	
	Series<Bool> T1;	
	Series<Bool> T2;

Events
	OnBar(ArrayRef<Integer> indexs)
	{
	
	bartoday=(barssinceToday==0);//今日第一根BAR线
	bartoday2=(barssincetoday>0);//大于今日第一根BAR线
	
	If(bartoday)
	{
		Nop=Open;//获得今日开盘价
	}

	HH1=HighD(M);//M交易日内的最高价
	LL1=LowD(M);//M交易日内的最低价
	CC1=CloseD(M);//M交易日内的收盘价
	P=(HH1+LL1+CC1)/3;//以上三个价格的均价,中轴线;
	
	R1=2*P-LL1;//做第一个初始阻力价;
	S1=2*P-HH1;//做第一个初始支撑价;
	R2= P+(R1-S1);//第二个初始阻力价;
	S2= P-(R1-S1);//第二个初始支撑价;
	R3= HH1+2*(P-LL1);//第三个初始阻力价;
	S3= LL1-2*(HH1-P);//第三个初始支撑价;
	
	SM1=(P+S1)/2;//第一个区间的支撑;
	SM2=(S1+S2)/2;//第二份区间的支撑;
	SM3=(S2+S3)/2;//第三个区间的支撑;
	
	RM1=(P+R1)/2;//第一个区间的阻力;
	RM2=(R1+R2)/2;//第二个区间的阻力;
	RM3=(R2+R3)/2;//第三个区间的阻力;
	
	
	//设置6个区间,根据日开盘价所在区间所决定使用哪一个区间的上沿下沿作为突破开仓的条件;
	Condition1 = Nop>P and Nop<RM1;//下沿是P,上沿是RM1
	Condition2 = Nop>SM2 and Nop<SM1;//下沿是SM2,上沿是SM1
	Condition3 = Nop>SM3 and Nop<SM2;//下沿是SM3,上沿是SM2
	Condition4 = Nop>RM1 and Nop<RM2;//下沿是RM1,上沿是RM2
	Condition5 = Nop>RM2 and Nop<RM3;//下沿是RM2,上沿是RM3
	Condition6 = Nop<P and Nop>SM1; //下沿SM1,上沿就是P
	
	If(Condition1)
	{
		SY=RM1;//上沿赋值
		XY=P;//下沿赋值
	}
	If(Condition2)
	{
		SY=SM1;
		XY=SM2;
	}
	if(Condition3)
	{
		SY=SM2;
		XY=SM3;
	}
	If(Condition4)
	{
		SY=RM2;
		XY=RM1;
	}
	if(Condition5)
	{
		SY=RM3;
		XY=RM2;		
	}
	if(Condition6)
	{
		SY=P;
		XY=SM1;
	}
	
	//最新价格金叉SY且大于SY,并且SY不能是空值,SY与上一根 BAR线相等防止出现假连线
	T1 = (CrossOver(C,SY) or C>SY )and SY>0 and SY[1]==SY;
	
	//最新价格死叉XY且小于XY,并且XI不能是空值,XY与上一根BAR线相等防止出现假连线
	T2 = (CrossUnder(C,XY) or C<XY )and XY>0 and XY[1]==XY;
	
	If((T1[1] and Not(T2[1])) and MarketPosition<>1 and bartoday2)
	{
		   Buy(SS,Open);
		   LowerAfterEntry=EntryPrice;

	}
	If((T2[1] and Not(T1[1])) and MarketPosition<>-1 and bartoday2)
	{
	
			SellShort(SS,Open);
			HigherAfterEntry=EntryPrice;
	}
	
	//记录多头最低价和空头最高价格
	
	//有多头持仓,刚进场时的本根BAR线的序号
	if(MarketPosition==1 and BarsSinceEntry==0)
	{
		HigherAfterEntry=HigherafterEntry[1];
		LowerAfterEntry=Max(LowerAfterEntry,Low);
	}
	
	//有空头持仓,刚进场时的本根bar线的序号
	if(MarketPosition==-1 and BarsSinceEntry==0)
	{
		HigherAfterEntry=Min(HigherafterEntry,High);
		LowerAfterEntry=LowerAfterEntry[1];
	}
	if(MarketPosition<>0 and BarsSinceEntry>=1)
	{
		HigherAfterEntry=Min(HigherafterEntry,High);
		LowerAfterEntry=Max(LowerAfterEntry,Low);
	}
	
	//跟踪出场
	
	//多头出场线
	Myprice=LowerAfterEntry -Open*TR/100;
	
	//当持有多头持仓,最新价格跌破出场线吗,同时平仓信号和开仓信号不再同一根BAR线;
	{
	if(MarketPosition==1 and Low<=Myprice and BarsSinceEntry>0) 
	//如果跳空击破,取值最新的bar线的开盘价,若不是则使用出场线价格;
		Myprice = Min(Myprice,Open);
		
		//多头平仓
		Sell(0,Myprice);
	}
	Myprice2=HigherAfterEntry + Open*TR/100;
	//空头出场线
	if(MarketPosition==-1 and High>=Myprice2 and BarsSinceEntry>0)
	{
		Myprice2 = Max(Myprice2,Open);
		BuyToCover(0,Myprice2);
	}

}

  

posted on 2021-09-17 09:32  alantop  阅读(737)  评论(0编辑  收藏  举报