If (close>=BuyPosition and close[1]<BuyPosition )//假如当前高价 >= 上轨。//
{
Buy(lots,close); //开仓买1手,价格为取开盘价与上轨对比的较大值,再加上设置的滑点数了。//
LowAfterEntry = EntryPrice;//保存多头开仓价格;
Return;//返回
}
If(close<=SellPosition and close[1]>SellPosition) //假如当前低价 <= 下轨。//
{
SellShort(lots,close);//开仓卖出1手,价格为取开盘价与下轨对比的较小值,再加上设置的滑点数了。//
HighAfterEntry = EntryPrice;//保存空头开仓价格;
Return;//返回
}
}else if(MarketPosition>0 and BarsSinceEntry>0){
middle = middle[1];
if (low<middle){
Sell(0,low);
Commentary("low<middle");
}Else if ((close - EntryPrice)> BuysellRange)
{
Sell(0,Close);
Commentary("close-entryprice,entryprice="+text(Entryprice));
}
}else if (MarketPosition<0 and BarsSinceEntry>0){
middle = middle[1];
if (high<middle){
BuyToCover(0,High);
}else if ((EntryPrice-close)>BuysellRange){
BuyToCover(0,close);
}
我仅仅是想重现dual thrust的指标,由于需要取前N天的最高最低价格,所以data0设为5分钟,data1设为日线,就只有在代码最前面需要调用data1的前几个K线来取最高最低价格
其余代码都是按照只在data0上操作的。
看了老师提示,想会不会是data1上也会执行这个代码,导致平仓了。
然后我尝试把整个onbar里面的代码都加入了range[0:0],发现问题依旧,我也不知道是哪里把仓位平掉了
Params
Numeric K1(0.1);
Numeric May_D(2);
Numeric lots(1);
Vars
Numeric BuysellRange(0);//声明数值变量BuyRange,初值为0,即上轨幅度。//
Numeric BuyTrig(0);//声明数值变量BuyTrig,初值为0.//
Numeric SellTrig(0);//声明数值变量SellTrig,初值为0.//
Numeric HH;//声明数值变量HH。//
Numeric LL;//声明数值变量LL。//
Numeric HC;//声明数值变量HC。//
Numeric LC;//声明数值变量LC。//
Numeric BuyPosition;//声明数值变量BuyPosition,即买入价格。//
Numeric SellPosition;//声明数值变量SellPosition,即卖出价格。//
series<Numeric> middle;
Series<Numeric> HighAfterEntry;
Series<Numeric> LowAfterEntry;
Series<Numeric> Day_N;
Events
onBar(ArrayRef<Integer> indexs)
{
range[0:0]
{
if(TrueDate(0)<>TrueDate(1))
{
Day_N=Day_N[1]+1;
}
if(Day_N<May_D)Return;
data1.HH=data1.highest(data1.high[1],May_D);
data1.LL=data1.lowest(data1.low[1],May_D);
data1.HC=data1.highest(data1.close[1],May_D);
data1.LC=data1.lowest(data1.close[1],May_D);
data0.HH= Data1.HH;
data0.LL = data1.LL;
data0.HC = data1.HC;
data0.LC = data1.LC;
/*PlotNumeric("HH",data0.HH);
PlotNumeric("LL",data0.LL);
PlotNumeric("HC",data0.HC);
PlotNumeric("LC",data0.LC);*/
Commentary("marketposition = "+text(MarketPosition));
if (MarketPosition==0){
If((HH - LC) >= (HC - LL))
{
BuysellRange = HH - LC;
}
Else
{
BuysellRange = HC - LL;
}
commentary("buysellrange="+text(BuysellRange));
BuyTrig = K1*BuysellRange;//计算幅度
Commentary("buytrig="+Text(BuyTrig));
SellTrig = K1*BuysellRange;//计算幅度
BuyPosition = data1.open+BuyTrig;//上轨,即开盘价 + BuyTrig。
SellPosition = data1.open-SellTrig;//下轨,即开盘价 - SellTrig。
PlotNumeric("BuyPosition",BuyPosition);//画线上轨。
PlotNumeric("SellPosition",SellPosition);//画线下轨。
middle = data1.open;
If (close>=BuyPosition and close[1]<BuyPosition )//假如当前高价 >= 上轨。//
{
Buy(lots,close); //开仓买1手,价格为取开盘价与上轨对比的较大值,再加上设置的滑点数了。//
LowAfterEntry = EntryPrice;//保存多头开仓价格;
Return;//返回
}
If(close<=SellPosition and close[1]>SellPosition) //假如当前低价 <= 下轨。//
{
SellShort(lots,close);//开仓卖出1手,价格为取开盘价与下轨对比的较小值,再加上设置的滑点数了。//
HighAfterEntry = EntryPrice;//保存空头开仓价格;
Return;//返回
}
}else if(MarketPosition>0 and BarsSinceEntry>0){
middle = middle[1];
if (low<middle){
Sell(0,low);
Commentary("low<middle");
}Else if ((close - EntryPrice)> BuysellRange)
{
Sell(0,Close);
Commentary("close-entryprice,entryprice="+text(Entryprice));
}
}else if (MarketPosition<0 and BarsSinceEntry>0){
middle = middle[1];
if (high<middle){
BuyToCover(0,High);
}else if ((EntryPrice-close)>BuysellRange){
BuyToCover(0,close);
}
}
Commentary("middle="+text(middle));
Commentary("entryprice="+text(Entryprice));
}
}
但也不是entryprice立刻就变成0,也有经过1~4根后再变成0 的,在1~4根内entryprice还是正常计算的。不知道这个是什么原因?