老师好!
用 A_SendOrder 开仓后也不行,BarsSinceEntry 和 AvgEntryPrice 函数依然没有返回值,并且还有一个问题:用if ( A_SellPosition == 0 and A_BuyPosition() == 0) 来限制开仓次数好像也有问题,A_SendOrder 里 的开仓数量是1手,但成交了两手。
Params
//此处添加参数
Numeric Length(5);
Numeric Length1(10);
Numeric Length2(20);
Vars
String my_tb("tbf_wd118552");
series<Numeric> Ma1;
series<Numeric> Ma2;
series<Numeric> Ma3;
Integer Back;
Integer k1;
Integer a(0);
Integer a2(1);
Integer date1;
Integer date2;
Integer date3;
Numeric exitp;
String datetxt;
Bar mybar;
Bar mybar1;
Global Integer un;
Global map<String,Integer> ccmap;
Bool EntryPd;
Bool EntryPd1;
Bool EntryPd2;
Bool EntryPd3;
bool exitside7;
bool exitside;
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次
OnInit()
{
A_BindTradeAccount(my_tb);
A_SubscribeTradeByCreateId(Enum_Trade_Source_ALL);
//datetxt=GetTBProfileString2File("d:\\开拓交易者\\localdate.txt","mydate","date2");
//print("datetxt=="+datetxt);
//date3=Value(datetxt);
//print("date3=="+text(date3));
SubscribeBar("rb2301.SHFE","3m",20221101,0);
//SubscribeBar("i2301.DCE","1d",date3,0);
}
OnBar(ArrayRef<Integer> indexs)
{
range[0:0]
{
ma1=Average((open+close+high+low)/4,Length);
ma2=Average((open+close+high+low)/4,Length1);
ma3=Average((open+close+high+low)/4,Length2);
PlotNumeric("ma1",ma1);
PlotNumeric("ma2",ma2);
PlotNumeric("ma3",ma3);
if (BarStatus==2)
{
if (A_SellPosition()>0)
{
k1=BarsSinceEntry();
exitp=low[k1];
for k1 downto 0
{
if (exitp<low[k1-1])
{
exitp=exitp;
}
Else
{
exitp=low[k1-1];
}
}
exitside7=close>(AvgEntryPrice()+MinMove()*3*PriceScale);
exitside=(exitp<(AvgEntryPrice()-MinMove()*3*PriceScale)) and (close>=AvgEntryPrice()-MinMove()*3*PriceScale);
if (exitside7 or exitside)
{
A_SendOrder(Enum_Buy,Enum_Exit,A_SellPosition(),Q_AskPrice()+MinMove()*3*PriceScale);
}
}
if (A_BuyPosition()>0)//持有多头仓位
{
k1=BarsSinceEntry();
exitp=high[k1];
for k1 downto 0
{
if (exitp>high[k1-1])
{
exitp=exitp;
}
Else
{
exitp=high[k1-1];
}
}
exitside7=close<(AvgEntryPrice()-MinMove()*3*PriceScale);
exitside=(exitp>(AvgEntryPrice()+MinMove()*3*PriceScale)) and (close<=AvgEntryPrice()+MinMove()*3*PriceScale);
if (exitside7 or exitside)
{
A_SendOrder(Enum_Sell,Enum_Exit,A_buyPosition(),Q_BidPrice()-MinMove()*3*PriceScale);
}
}
if ( A_SellPosition == 0 and A_BuyPosition() == 0) //开仓
{
EntryPd= close>open and ma1[4]>ma1[3] and ma1[3]>ma1[2] and ma1[2]-ma1[1]>ma1[1]-ma1 and close[1]<ma1[1] and close>ma1;
EntryPd1= close>open and ma1[4]>ma1[3] and ma1[3]>ma1[2] and ma1>ma1[1] and close[1]<ma1[1] and close>ma1;
EntryPd2=close>open and ma1[3]>ma1[2] and ma1[2]>ma1[1] and ma1[2]-ma1[1]>ma1[1]-ma1 and close[1]<ma1[1] and close>ma1;
EntryPd3=close>open and ma1[3]>ma1[2] and ma1[2]>ma1[1] and ma1>ma1[1] and close[1]<ma1[1] and close>ma1;
if (EntryPd or EntryPd1 or EntryPd2 or EntryPd3)
{ //多头开仓
A_SendOrder(Enum_Buy,Enum_Entry,1,Q_AskPrice()+MinMove()*PriceScale);
}
EntryPd= close<open and ma1[4]<ma1[3] and ma1[3]<ma1[2] and ma1[1]-ma1[2]>ma1-ma1[1] and close[1]>ma1[1] and close<ma1;
EntryPd1=close<open and ma1[4]<ma1[3] and ma1[3]<ma1[2] and ma1<ma1[1] and close[1]>ma1[1] and close<ma1;
EntryPd2=close<open and ma1[3]<ma1[2] and ma1[2]<ma1[1] and ma1[1]-ma1[2]>ma1-ma1[1] and close[1]>ma1[1] and close<ma1;
EntryPd3=close<open and ma1[3]<ma1[2] and ma1[2]<ma1[1] and ma1<ma1[1] and close[1]>ma1[1] and close<ma1;
if (EntryPd or EntryPd1 or EntryPd2 or EntryPd3)
{ //空头开仓
A_SendOrder(Enum_sell,Enum_Entry,1,Q_BidPrice()-MinMove()*PriceScale);
}
}
print("BarsSinceEntry=="+text(BarsSinceEntry()));
print("low=="+text(exitp));
print("资金=="+text(A_FreeMargin()));//建仓后的资金变动
print("建仓价格=="+text(AvgEntryPrice()));
print("空头仓位=="+text(A_SellPosition() ));
print("多头仓位=="+text(A_BuyPosition()));
print("ccmap=="+textmap(ccmap));
}
}
}
OnOrder(OrderRef ord)
{
print("Onorder资金=="+text(A_FreeMargin()));
if (ord.combOffset==Enum_Entry and (ord.status==Enum_Filled or ord.status==Enum_FillPart))
{
if (MapContain(ccmap,ord.symbol))
{
MapErase(ccmap,ord.symbol);
}
ccmap[ord.symbol]=ord.orderId;
print("OPENccmap=="+textmap(ccmap));
}else if ((ord.combOffset==Enum_Exit or ord.combOffset==Enum_ExitToday) and ord.status==Enum_Filled)
{
if (MapContain(ccmap,ord.symbol))
{
MapErase(ccmap,ord.symbol);
}
print("EXIEccmap=="+textmap(ccmap));
}
}
OnFill(FillRef ordFill)
{
print("OnFill.symbol"+ordfill.symbol);
}
图表函数 对A_SendOrder也没有用 ,另外个帖子告诉你了