杨刚:
第一个测试策略
Params
Numeric Length1(60);
Numeric ATR_times(3);
Numeric ATR_period(14);
Vars
Numeric ema;
Series<Numeric> ATRup;
Series<Numeric> ATRdown;
Events
OnBar(ArrayRef<Integer> indexs)
{
Range[0:DataSourceSize() - 1]
{
ema = XAverage(Close, Length1);
PlotNumeric("EMA1", ema);
ATRup = ema + 3 * AvgTrueRange(ATR_period);
ATRdown = ema - 3 * AvgTrueRange(ATR_period);
PlotNumeric("ATRup", ATRup);
PlotNumeric("ATRdown", ATRdown);
if ( Close[1] > ATRup[1] && MarketPosition <>1 )
{
PlotBool("buy", true);
Buy(0,Open);
}
if ( Close[1] < ATRdown[1] && MarketPosition <>-1 )
{
PlotBool("sell", False);
SellShort(0,Open);
}
}
}
第二个
延迟反手策略
Params
Numeric FastLength(5);
Numeric SlowLength(120);
Numeric DelayTicks(5);
Vars
Series<Numeric> AvgValue1;
Series<Numeric> AvgValue2;
Numeric LastBarTime;
Numeric TickCounter;
Numeric dataIndex;
Events
OnBar(ArrayRef<Integer> indexs)
{
AvgValue1 = AverageFC(Close,FastLength);
AvgValue2 = AverageFC(Close,SlowLength);
LastBarTime = GetGlobalVar(0);
TickCounter = GetGlobalVar(1);
// 最新Bar第一次生成时,Tick重新开始计数
If(BarStatus == 2 && gValue[0]!= Time)
{
LastBarTime = Time;
TickCounter = 0;
}
If(MarketPosition <> 1 && AvgValue1[1] > AvgValue2[1])
{
If(MarketPosition == 0 || BarStatus != 2)
// 无持仓,直接买多仓
// 持空仓且Bar不是实时行情,平空仓,买多仓
{
Buy(1,Open);
}Else // 持空仓,Bar实时行情,平空仓,通过TickCounter计数,延迟反手
{
BuyToCover(1,Open);
If(TickCounter == 0)
{
TickCounter = 1;
}Else If(TickCounter < DelayTicks)
{
TickCounter = TickCounter + 1;
}Else
{
Buy(1,Open);
}
}
}
If(MarketPosition <> -1 && AvgValue1[1] < AvgValue2[1])
{
If(MarketPosition == 0 || BarStatus != 2)
{
SellShort(1,Open);
}Else // 持多仓且Bar为实时行情,平多,延迟反手
{
Sell(1,Open);
If(TickCounter == 0)
{
TickCounter = 1;
}Else If(TickCounter < DelayTicks)
{
TickCounter = TickCounter + 1;
}Else
{
SellShort(1,Open);
}
}
}
SetGlobalVar(0,LastBarTime);
SetGlobalVar(1,TickCounter);
}
把第二个延迟反手加到第一个里面只要延迟部分谢谢老师
Params
Numeric d1(10);
Numeric d2(20);
Numeric d3(40);
Numeric s(3);
Numeric y(10);
Vars
Numeric lot(0);
Numeric EMA1(0);
Numeric EMA2(0);
Numeric EMA3(0);
Numeric sd(0);
Events
onBar(ArrayRef<Integer> indexs)
{
EMA1=Average(Close[1],d1);
EMA2=Average(Close[1],d2);
EMA3=Average(Close[1],d3);
sd=(StandardDev((Close[1]-Close[2])/Close,100,2))*100;
lot=IntPart(y/sd);
If(MarketPosition==0)
{
If((EMA1>(EMA2+s))&&(EMA1>(EMA3+s)))
Buy(lot,Open);
If((EMA1<(EMA2-s))&&(EMA1<(EMA3-s)))
SellShort(lot,Open);
};
If(MarketPosition==1)
{
If((EMA1<(EMA2-s))&&(EMA1<(EMA3-s)))
{
SellShort(lot,Open);};
};
If(MarketPosition==-1)
{
If((EMA1>(EMA2+s))&&(EMA1>(EMA3+s)))
{
Buy(lot,Open);
}
}
}
老师加个macd谢谢
Params
Numeric FastLength(20);//声明数值参数FastLength,初值5.//
Numeric SlowLength(20);//声明数值参数SlowLength,初值20.//
Numeric DslowLength(40);//声明数值参数DslowLength,初值200.//
Numeric Lots(1); // 基本下单单位
Numeric MaxLots(10); // 最大下单单位
Vars
Series<Numeric> AvgValue1;
Series<Numeric> AvgValue2;
Series<Numeric> AvgValue3;//声明数值序列变量AvgValue3.//
Series<Numeric> myLots; // 每次下单的手数
Series<Numeric> myNetProfit; // 累计的最大净利润
Events
onBar(ArrayRef<Integer> indexs)
{
AvgValue1 = AverageFC(Close,FastLength);
AvgValue2 = AverageFC(Close,SlowLength);
AvgValue3 = AverageFC(Close,DslowLength);//求200日均线了。//
// PlotNumeric(\"MA1\",AvgValue1);
// PlotNumeric(\"MA2\",AvgValue2);
PlotNumeric(\"MA1\",AvgValue1);//画5日均线。//
PlotNumeric(\"MA2\",AvgValue2);//画20日均线。//
PlotNumeric(\"MA3\",AvgValue3);//画200日均线。//
If(TotalTrades == 0)
{
myLots = Lots;
myNetProfit = 0;
}
If(MarketPosition == 1 && AvgValue1[1] < AvgValue2[1])
{
Sell(0,Open);
If(NetProfit >= myNetProfit[1])
{
myLots = Lots;
}Else
{
myLots = Min((myLots[1] + Lots),MaxLots);
}
}
If(MarketPosition <>1 && AvgValue1[1] > AvgValue2[1]&& High >= AvgValue3[1])
{
Buy(myLots,Open);
}
myNetProfit = Max(myNetProfit[1],NetProfit);
}
这几个策略是楼主买来的吗?好几个都是偷价策略或是带未来函数的
Params
Numeric notbef(9.00);
Numeric notaft(14.55);
Numeric f1(0.01);
Numeric f2(100);
Numeric f3(0.01);
Numeric reverse(1.00);
Numeric rangemin(0.1);
Numeric xdiv(3);
Numeric offset(5);
Vars
Series<Numeric> ssetup(0);
Series<Numeric> bsetup(0);
Series<Numeric> senter(0);
Series<Numeric> benter(0);
Series<Numeric> bbreak(0);
Series<Numeric> sbreak(0);
Series<Numeric> ltoday(0);
Series<Numeric> hitoday(9999);
Series<Numeric> startnow(0);
Series<Numeric> div(0);
Series<Bool> rfilter(false);
Numeric i_reverse;
Numeric i_rangemin;
Numeric i_vB;
Numeric i_vS;
Numeric i_offset;
Numeric offset2;
Events
onBar(ArrayRef<Integer> indexs)
{
If(BarStatus==2 && Time==0.090000 && high==low ) return; // 集合竟价过滤信号
i_offset = offset*MinMove*PriceScale;
i_reverse = reverse*(OpenD(0)/100);
i_rangemin = rangemin*(OpenD(0)/100);
if(BarStatus==0)
{
startnow=0;
div=max(xdiv,1);
}
if(Date != Date[1])//如果当前公式应用商品在当前Bar的日期不等于前面公式应用商品在当前Bar的日期
{
SetGlobalVar(0,0);// 将第1个全局变量设置为0,将第2个全局变量设置为0
SetGlobalVar(1,0);
startnow=startnow+1;
ssetup=hitoday[1]+f1*(Close[1]-ltoday[1]);
senter=((1+f2)/2)*(hitoday[1]+Close[1])-(f2)*ltoday[1];
benter=((1+f2)/2)*(ltoday[1]+Close[1])-(f2)*hitoday[1];
bsetup=ltoday[1]-f1*(hitoday[1]-Close[1]);
bbreak=ssetup+f3*(ssetup-bsetup);
sbreak=bsetup-f3*(ssetup-bsetup);
hitoday=High;
ltoday=Low;
rfilter=(hitoday[1]-ltoday[1])>=i_rangemin;
}
if(High>hitoday)
{
hitoday=High;
}
if(Low<ltoday)
{
ltoday=Low;
}
if(Time*100>notbef and Time*100<notaft and startnow>=2 and rfilter)//当前公式应用商品在当前Bar的时间乘以100》=9点或者小于14.55分
{
if(Time != GetGlobalVar(1) and GetGlobalVar(1) != 0)
{
SetGlobalVar(1,10000);
}
if(hitoday>=ssetup and marketposition>-1 and GetGlobalVar(1)<1)
{
If(Low<=(senter+(hitoday-ssetup)/div))
{
SellShort(1,senter+(hitoday-ssetup)/div+i_offset);
SetGlobalVar(1,Time);
Return;
}
}
if(ltoday<=bsetup and marketposition<1 and GetGlobalVar(1)<1)
{
If(High>=(benter-(bsetup-ltoday)/div))
{
Buy(1,benter-(bsetup-ltoday)/div-i_offset);
SetGlobalVar(1,Time);
Return;
}
}
if(marketposition==-1)//-1 当前位置为持空仓
{
SetGlobalVar(0,1);
if(High-EntryPrice>=i_reverse)
{
BuyToCover(1,entryprice);
Return;
}
}
if(marketposition==1)//1 当前位置为持多仓
{
SetGlobalVar(0,1);
if(EntryPrice-Low>=i_reverse)
{
Sell(1,entryprice);
Return;
}
}
if(marketposition==0)//1 当前位置为没有持仓
{
if(High>=bbreak and GetGlobalVar(0) == 0)
{
Buy(1,bbreak-i_offset);
Return;
}
}
if(marketposition==0)//1 当前位置为没有持仓
{
if(low<=sbreak and GetGlobalVar(0) == 0)
{
SellShort(1,sbreak+i_offset);
Return;
}
}
}
if(Time*100>=notaft and Time<0.1600)
{
if(marketposition==-1)
{
BuyToCover(1,Open);
}
if(marketposition==1)
{
Sell(1,Open);
}
}
}
Params
Numeric FastLength(5);// 短期指数平均线参数
Numeric SlowLength(20);// 长期指数平均线参数
//Numeric Lots(1);
Numeric A(189);
Numeric B(198);
Numeric CC(29);
Numeric s(3);
Numeric sK(0.5);
Vars
Numeric lot(0);
Numeric FastLengthA(A);
Numeric SlowLengthA(B);
Numeric MACDLength(CC);
Series<Numeric> MACDValue;
Series<Numeric> AvgMACD;
Series<Numeric> MACDDiff;
Series<Numeric> MA5;
Series<Numeric> MA20;
Bool BuyCon(False);
Bool SellCon(False);
Bool MacdJcCon(False);
Bool MacdScCon(False);
Numeric sd(0);
Events
onBar(ArrayRef<Integer> indexs)
{
MACDValue = XAverage(Close, FastLength) - XAverage(Close, SlowLength);
AvgMACD = XAverage(MACDValue, MACDLength);
MACDDiff = MACDValue - AvgMACD;
MA5 = AverageFC(Close,FastLength);
MA20 = AverageFC(Close,SlowLength);
PlotNumeric(\"MA5\", Ma5);
PlotNumeric(\"MA20\", Ma20);
sd=(StandardDev((Close[1]-Close[2])/Close,100,2))*100;
lot=IntPart(sK/sd);
If(MarketPosition != 1 && MACDValue[1] > 0 && Ma5[1] > Ma20[1]) {
Buy(lot, Open);
} Else If(MarketPosition == -1 && (MACDValue[1] > 0 || Ma5[1] > Ma20[1])) {
BuyToCover(Lot, Open);
} Else If(MarketPosition != -1 && MACDValue[1] < 0 && Ma5[1] < Ma20[1]) {
SellShort(Lot, Open);
} Else If(MarketPosition == 1 && (MACDValue[1] < 0 || Ma5[1] < Ma20[1])) {
Sell(Lot, Open);
}
}
Params
Numeric notbef(9.00);
Numeric notaft(14.55);
Numeric f1(0.01);
Numeric f2(1);
Numeric f3(0.01);
Numeric reverse(1.00);
Numeric rangemin(0.1);
Numeric xdiv(3);
Numeric offset(5);
Vars
Series<Numeric> ssetup(0);
Series<Numeric> bsetup(0);
Series<Numeric> senter(0);
Series<Numeric> benter(0);
Series<Numeric> bbreak(0);
Series<Numeric> sbreak(0);
Series<Numeric> ltoday(0);
Series<Numeric> hitoday(9999);
Series<Numeric> startnow(0);
Series<Numeric> div(0);
Series<Bool> rfilter(false);
Numeric i_reverse;
Numeric i_rangemin;
Numeric i_vB;
Numeric i_vS;
Numeric i_offset;
Numeric offset2;
Events
onBar(ArrayRef<Integer> indexs)
{
If(BarStatus==2 && Time==0.090000 && high==low ) return; // 集合竟价过滤信号
i_offset = offset*MinMove*PriceScale;
i_reverse = reverse*(OpenD(0)/100);
i_rangemin = rangemin*(OpenD(0)/100);
if(BarStatus==0)
{
startnow=0;
div=max(xdiv,1);
}
if(Date != Date[1])//如果当前公式应用商品在当前Bar的日期不等于前面公式应用商品在当前Bar的日期
{
SetGlobalVar(0,0);// 将第1个全局变量设置为0,将第2个全局变量设置为0
SetGlobalVar(1,0);
startnow=startnow+1;
ssetup=hitoday[1]+f1*(Close[1]-ltoday[1]);
senter=((1+f2)/2)*(hitoday[1]+Close[1])-(f2)*ltoday[1];
benter=((1+f2)/2)*(ltoday[1]+Close[1])-(f2)*hitoday[1];
bsetup=ltoday[1]-f1*(hitoday[1]-Close[1]);
bbreak=ssetup+f3*(ssetup-bsetup);
sbreak=bsetup-f3*(ssetup-bsetup);
hitoday=High;
ltoday=Low;
rfilter=(hitoday[1]-ltoday[1])>=i_rangemin;
}
if(High>hitoday)
{
hitoday=High;
}
if(Low<ltoday)
{
ltoday=Low;
}
if(Time*100>notbef and Time*100<notaft and startnow>=2 and rfilter)//当前公式应用商品在当前Bar的时间乘以100》=9点或者小于14.55分
{
if(Time != GetGlobalVar(1) and GetGlobalVar(1) != 0)
{
SetGlobalVar(1,10000);
}
if(hitoday>=ssetup and marketposition>-1 and GetGlobalVar(1)<1)
{
If(Low<=(senter+(hitoday-ssetup)/div))
{
SellShort(1,senter+(hitoday-ssetup)/div+i_offset);
SetGlobalVar(1,Time);
Return;
}
}
if(ltoday<=bsetup and marketposition<1 and GetGlobalVar(1)<1)
{
If(High>=(benter-(bsetup-ltoday)/div))
{
Buy(1,benter-(bsetup-ltoday)/div-i_offset);
SetGlobalVar(1,Time);
Return;
}
}
if(marketposition==-1)//-1 当前位置为持空仓
{
SetGlobalVar(0,1);
if(High-EntryPrice>=i_reverse)
{
BuyToCover(1,entryprice);
Return;
}
}
if(marketposition==1)//1 当前位置为持多仓
{
SetGlobalVar(0,1);
if(EntryPrice-Low>=i_reverse)
{
Sell(1,entryprice);
Return;
}
}
if(marketposition==0)//1 当前位置为没有持仓
{
if(High>=bbreak and GetGlobalVar(0) == 0)
{
Buy(1,bbreak-i_offset);
Return;
}
}
if(marketposition==0)//1 当前位置为没有持仓
{
if(low<=sbreak and GetGlobalVar(0) == 0)
{
SellShort(1,sbreak+i_offset);
Return;
}
}
}
if(Time*100>=notaft and Time<0.1600)
{
if(marketposition==-1)
{
BuyToCover(1,Open);
}
if(marketposition==1)
{
Sell(1,Open);
}
}
}
Params
//此处添加参数
Numeric N(920);
Numeric M(5);
Numeric Fund(20000);
Numeric Lots(1); // 基本下单单位
Numeric MaxLots(10); // 最大下单单位
Numeric zhiying(30); //止盈
Numeric zhiying2(50); //止盈
Numeric zhiying3(50); //止盈
Numeric zhiying4(50); //止盈
Numeric zhiying5(50); //止盈
Numeric zhiying6(80); //止盈
Numeric zhiying7(120); //止盈
Numeric zhiying8(150); //止盈
Vars
//此处添加变量
//Numeric Lots( 1 );
Series<Numeric> C_O( 0 );
Series<Numeric> Band( 0 );
Series<Numeric> Price_BPK( 0 );
Series<Numeric> Price_SPK( 0 );
Series<Numeric> Price_BP( 0 );
Series<Numeric> Price_SP( 0 );
Series<Bool> B( False );
Series<Bool> S( False );
Series<Bool> BuyPK(false);
Series<Bool> SellPK(false);
Series<Bool> BuyS(false);
Series<Bool> SellS(false);
Series<Bool> BuyP(false);
Series<Bool> SellP(false);
Series<Numeric> myLots; // 每次下单的手数
Series<Numeric> myNetProfit; // 累计的最大净利润
Events
OnBarClose(ArrayRef<Integer> indexs)
{
//此处添加代码正文
//If(!CallAuctionFilter()) Return;
//Lots=max(1,intpart(Fund/(O*ContractUnit*BigPointValue*0.1)));
C_O=XAverage(C,N)-XAverage(O,N);
B=CrossOver(C_O,0);
S=CrossUnder(C_O,0);
Band=AvgTrueRange(N)*1.51*M;
If (B)
{
Price_BPK=H+Band;
Price_SP=L-Band;
}
If (S)
{
Price_SPK=L-Band;
Price_BP=H+Band;
}
BuyPK=C_O>0 AND C>=Price_BPK;
SellPK=C_O<0 AND C<=Price_SPK;
SellP=S;
BuyP=B;
SellS=C<=Price_SP;
BuyS=C>=Price_BP;
If(TotalTrades == 0)
{
myLots = Lots;
myNetProfit = 0;
}
If (MarketPosition== 1 and BarsSinceEntry>1 and SellPK[1])
{
Sell(0,Open);
//Commentary(\"SS\");
If(NetProfit >= myNetProfit[1])
{
myLots = Lots;
}Else
{
myLots = Min((myLots[1] + Lots),MaxLots);
}
}
If (MarketPosition== -1 and BarsSinceEntry>1 and BuyPK[1])
{
BuyToCover(0,Open);
//Commentary(\"BS\");
If(NetProfit >= myNetProfit[1])
{
myLots = Lots;
}Else
{
myLots = Min((myLots[1] + Lots),MaxLots);
}
}
If (MarketPosition!= 1 and CurrentBar>N and BuyPK[1])
{
Buy(myLots,Open);
//Commentary(\"BPK\");
}
If (MarketPosition!= -1 and CurrentBar>N and SellPK[1])
{
SellShort(myLots,Open);
//Commentary(\"SPK\");
}
myNetProfit = Max(myNetProfit[1],NetProfit);
If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)
{
sell(1,max(o,AvgEntryPrice+zhiying));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying2 and MarketPosition==1 and abs(CurrentContracts)==3)
{
sell(1,max(o,AvgEntryPrice+zhiying2));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying3 and MarketPosition==1 and abs(CurrentContracts)==4)
{
sell(1,max(o,AvgEntryPrice+zhiying3));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying4 and MarketPosition==1 and abs(CurrentContracts)==5)
{
sell(1,max(o,AvgEntryPrice+zhiying4));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying5 and MarketPosition==1 and abs(CurrentContracts)==6)
{
sell(1,max(o,AvgEntryPrice+zhiying5));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying6 and MarketPosition==1 and abs(CurrentContracts)==7)
{
sell(1,max(o,AvgEntryPrice+zhiying6));
Commentary(\"止盈\");
}
If(h-AvgEntryPrice>=zhiying7 and MarketPosition==1 and abs(CurrentContracts)==8)
{
sell(1,max(o,AvgEntryPrice+zhiying7));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying2 and MarketPosition==-1 and abs(CurrentContracts)==3 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying2));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying3 and MarketPosition==-1 and abs(CurrentContracts)==4 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying3));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying4 and MarketPosition==-1 and abs(CurrentContracts)==5 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying4));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying5 and MarketPosition==-1 and abs(CurrentContracts)==6 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying5));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying6 and MarketPosition==-1 and abs(CurrentContracts)==7 )
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying6));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying7 and MarketPosition==-1 and abs(CurrentContracts)==8)
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying7));
Commentary(\"止盈\");
}
}
Params
Numeric FastLength(2);//声明数值参数FastLength,初值5.//
Numeric SlowLength(200);//声明数值参数SlowLength,初值20.//
Numeric DslowLength(300);//声明数值参数DslowLength,初值200.//
Vars
Series<Numeric> AvgValue1; //声明数值序列变量AvgValue1.//
Series<Numeric> AvgValue2;//声明数值序列变量AvgValue2.//
Series<Numeric> AvgValue3;//声明数值序列变量AvgValue3.//
Events
onBar(ArrayRef<Integer> indexs)
{
AvgValue1 = AverageFC(Close,FastLength);//求5日均线了。//
AvgValue2 = AverageFC(Close,SlowLength);//求20日均线了。//
AvgValue3 = AverageFC(Close,DslowLength);//求200日均线了。//
PlotNumeric(\"MA1\",AvgValue1);//画5日均线。//
PlotNumeric(\"MA2\",AvgValue2);//画20日均线。//
PlotNumeric(\"MA3\",AvgValue3);//画200日均线。//
//If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//
If(MarketPosition <>1 && AvgValue1[1] > AvgValue2[1] && High >= AvgValue3[1]) //假如当前没用持多单,且前一5日均线大于前一20日均线,且当前高价大于或等于200日均线。//
{
Buy(1,Max(Open , AvgValue3 ));//开仓买入,这里的细节处理就是把200日均线与当前k线开盘价比较,取较大值。//
}
If(MarketPosition ==1 && AvgValue1[1] < AvgValue2[1]) //假如当前持有多仓,且前一5日均线小于前一20日均线。//
{
Sell(1,Open);//平仓。//
}
If(MarketPosition <>-1 && AvgValue1[1] < AvgValue2[1] && Low <= AvgValue3[1]) //假如当前没有持空单,且前一5日均线小于前一20日均线,且当前低价小于前一200日均线。//
{
SellShort(1,Min(Open , AvgValue3 ));//开仓卖出,细节处理就是把当前k线的开盘价与200日均线值比较,取较小值即可。//
}
If(MarketPosition ==-1 && AvgValue1[1] > AvgValue2[1])//当前持有空单,且前一5日均线大于前一20日均线的。//
{
BuyToCover(1,open);//平仓。//
}
}
Params
//此处添加参数
Numeric N(120);
Numeric M(15);
Numeric Fund(20000);
Numeric Lots(1); // 基本下单单位
Numeric MaxLots(10); // 最大下单单位
Vars
//此处添加变量
//Numeric Lots( 1 );
Series<Numeric> C_O( 0 );
Series<Numeric> Band( 0 );
Series<Numeric> Price_BPK( 0 );
Series<Numeric> Price_SPK( 0 );
Series<Numeric> Price_BP( 0 );
Series<Numeric> Price_SP( 0 );
Series<Bool> B( False );
Series<Bool> S( False );
Series<Bool> BuyPK(false);
Series<Bool> SellPK(false);
Series<Bool> BuyS(false);
Series<Bool> SellS(false);
Series<Bool> BuyP(false);
Series<Bool> SellP(false);
Series<Numeric> myLots; // 每次下单的手数
Series<Numeric> myNetProfit; // 累计的最大净利润
Events
onBar(ArrayRef<Integer> indexs)
{
//此处添加代码正文
//If(!CallAuctionFilter()) Return;
//Lots=max(1,intpart(Fund/(O*ContractUnit*BigPointValue*0.1)));
C_O=XAverage(C,N)-XAverage(O,N);
B=CrossOver(C_O,0);
S=CrossUnder(C_O,0);
Band=AvgTrueRange(N)*1.51*M;
If (B)
{
Price_BPK=H+Band;
Price_SP=L-Band;
}
If (S)
{
Price_SPK=L-Band;
Price_BP=H+Band;
}
BuyPK=C_O>0 AND C>=Price_BPK;
SellPK=C_O<0 AND C<=Price_SPK;
SellP=S;
BuyP=B;
SellS=C<=Price_SP;
BuyS=C>=Price_BP;
If(TotalTrades == 0)
{
myLots = Lots;
myNetProfit = 0;
}
If (MarketPosition== 1 and BarsSinceEntry>1 and SellPK[1])
{
Sell(0,Open);
//Commentary(\"SS\");
If(NetProfit >= myNetProfit[1])
{
myLots = Lots;
}Else
{
myLots = Min((myLots[1] + Lots),MaxLots);
}
}
If (MarketPosition== -1 and BarsSinceEntry>1 and BuyPK[1])
{
BuyToCover(0,Open);
//Commentary(\"BS\");
If(NetProfit >= myNetProfit[1])
{
myLots = Lots;
}Else
{
myLots = Min((myLots[1] + Lots),MaxLots);
}
}
If (MarketPosition!= 1 and CurrentBar>N and BuyPK[1])
{
Buy(myLots,Open);
//Commentary(\"BPK\");
}
If (MarketPosition!= -1 and CurrentBar>N and SellPK[1])
{
SellShort(myLots,Open);
//Commentary(\"SPK\");
}
myNetProfit = Max(myNetProfit[1],NetProfit);
}
Params
//此处添加参数
Numeric Length1(20);
Numeric s1(2);
Numeric s2(2);
Numeric lots(1);
Numeric stoploss(5);
Numeric zhiying(30);
Vars
//此处添加变量
Series<Numeric>ma1;
Bool Buyentry(false);
Bool sellentry(false);
Bool buyexit(false);
Bool sellexit(false);
Series<Numeric>Highe;
Series<Numeric> HighAfterEntry; // 开仓后出现的最高价
Series<Numeric> LowAfterEntry; // 开仓后出现的最低价
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
onBar(ArrayRef<Integer> indexs)
{
MA1=AverageFC(Close,Length1);
PlotNumeric(\"ma1\",ma1);
BuyEntry =CountIf(Close[1]>ma1[1],s1)==s1;
sellentry =CountIf(Close[1]<ma1[1],s1)==s1;
sellexit =CountIf(Close[1]>ma1[1],s2)==s2;
buyexit=CountIf(Close[1]<ma1[1],s2)==s2;
If(MarketPosition!=1 && BuyEntry)
{
Buy(lots,Open);
Commentary(\"开多\");
}
Else If(MarketPosition!=-1 && sellentry)
{
SellShort(lots,Open);
Commentary(\"开空\");
}
Else If(MarketPosition==-1 && sellexit)
{
BuyToCover(0,Open);
Commentary(\"平空\");
//PlotNumeric(\"mark\",\"平空\",high,Yellow);
}
Else If(MarketPosition==1 && buyexit)
{
sell(0,Open);
Commentary(\"平多\");
//PlotNumeric(\"mark\",\"平多\",high,Yellow);
}
If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)
{
sell(1,max(o,AvgEntryPrice+zhiying));
Commentary(\"止盈\");
}
If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2)
{
BuyToCover(1,Min(o,AvgEntryPrice-zhiying));
Commentary(\"止盈\");
}
}