三、編寫常見問題
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.tumamayizhan.com/2018/02/10/50054.shtml
?