三、编写常见问题
Setting
AddTimes:5;
Params
Numeric Length(3); // 周期
Vars
NumericSeries BarN; //当日K线根数
NumericSeries myflag; // 交易次数
Begin
// ------------------- 取当日K线根数及交易次数 -----------------
If(date!=date[1])
{
BarN= 1 ;
myflag =0;
}
else BarN= BarN[1]+1;
// ----------------------- 多头开仓--------------------
If (MarketPosition!=-1 And Vol > 0 )
{
If(Every(IsUp,Length) And BarN>=Length And CloseMinute>=5 And myflag<= 5) //连续三根收阳,则开多单,最多连续开5次。
{
Buy(0, Active_Order);
myflag = myflag+1;
}
}
// ------------------------ 空头开仓-----------------
If (MarketPosition!=1 And Vol > 0 )
{
If(Every(IsDown,Length) And BarN>=Length And CloseMinute>=5 And myflag<= 5) //连续三根收阴,则开空单,最多连续开5次。
{
SellShort(0,Active_Order);
myflag = myflag+1;
}
}
If(CloseMinute<=5 And BarsSinceEntry > 0 And Vol > 0)
{
// ------------------------- 尾盘多头平仓----------------
If(MarketPosition == 1 )
Sell(0, Active_Order);
// ---------------------- 尾盘空头平仓-------------
If(MarketPosition == -1)
BuyToCover(0, Active_Order);
}
End
Data
data0:"m1801";
Vars
Global_Numeric type;
Begin
If(data0.A_BuyProfitLoss()>5000&&type == 0) //如果该合约多头盈亏大于5000,加仓1手
{
data0.A_SendOrder(Enum_Buy,Enum_Entry,1,data0.price("Ask1"));
type = 1; //加仓后type重新赋值,避免重复执行
}
End
Data
data0:"m1801";
Begin
If(data0.A_BuyProfitLoss()>5000 && GetGlobalVar(0) == 0) //如果该合约多头盈亏大于5000,加仓1手
{
data0.A_SendOrder(Enum_Buy,Enum_Entry,1,data0.price("Ask1"));
SetGlobalVar(0,1); //加仓后全局变量第一个位置重新赋值,避免重复执行
}
End
Vars
NumericSeries Ma5; //5周期均线
NumericSeries Ma10; //10周期均线
Numeric Buytype(0);//是否启用按资金比例下单
Numeric lots;//下单手数
Begin
If(Buytype ==0 )
{
lots = 5;//固定5手开仓
}
Else If(Buytype == 1 )
{
lots = MoneyTot*0.2/(Close*ContractUnit*MarginRatio);//按资金的百分之20开仓
}
Ma5 = Ma(Close,5);
Ma10 = Ma(Close,10);
If(Ref( CrossUp(Ma5,Ma10) ,1) )
{
Buy(lots);
}
Else If(Ref( CrossDown(Ma5,Ma10) ,1) )
{
SellShort(lots);
}
End
Vars
NumericArray deep;
Numeric i;
Numeric j;
Numeric k;
Begin
For i =0 To 9
{
If( High>High[i])
{
deep[j]=Low[i];
j = j+1;//装入最近的10根K线内小于当根最高价的K线的最低价
}
}
For i =0 To j-1
{
If( Low<deep[i])
k = k+1;
}
PlotNumeric("根数",k);
End
来源http://www.cxh99.com/2018/02/10/50054.shtml