以下程序是交易开拓中的程序代码,我试着自己改写过,但总存在大大小小的问题,求高人帮忙改写以下,感激不尽!
Params
Numeric PercentOfRange(0.3); //params是定义参数,相当于INPUT,NUMERIC表示数值型的//
Numeric MinRange(0.2);
Numeric StopLossSet(0.5);
Numeric TrailingStart(0.8);
Numeric TrailingStop(0.5);
Vars
NumericSeries DayOpen;
//Vars是申明变量,相当于Variable// Numeric MinPoint; Numeric preDayHigh;
Numeric preDayLow;
NumericSeries preDayRange;
Numeric UpperBand;
Numeric LowerBand;
NumericSeries HigherAfterEntry;
NumericSeries LowerAfterEntry;
BoolSeries bLongStoped; //boolseries是布尔型,返回值是false或者true,这里初值都是false//
BoolSeries bShortStoped;
Numeric StopLine;
Numeric MyPrice;
Begin //begin表示主程序开始//
preDayHigh = HighD(1); //HIGHD(1)表示前一天的最高价,LOWD(1)表示前一天的最低价,该程序一般用在分钟线上,这相当于跨周期取数据// preDayLow = LowD(1);
If(Date!=Date[1]) //date[1]相当于金字塔中的REF(DATE,1)//
{
DayOpen = Open;
preDayRange = preDayHigh - preDayLow;
If(preDayRange < Open*MinRange*0.01) preDayRange = Open*MinRange*0.01;
}Else
{
DayOpen = DayOpen[1];
preDayRange = preDayRange[1];
bLongStoped = bLongStoped[1];
bShortStoped = bShortStoped[1];
If(BarsSinceEntry == 1) //BarsSinceEntry相当于金字塔中的ENTERBARS//
{
HigherAfterEntry = AvgEntryPrice; //AvgEntryPrice相当于金字塔中的AVGENTERPRICE//
LowerAfterEntry = HigherAfterEntry;
}Else If(BarsSinceEntry > 1)
{
HigherAfterEntry = max(HigherAfterEntry[1],High[1]);
//HIGH[1]表示向前引用1个周期的数据,同REF//
LowerAfterEntry = min(LowerAfterEntry[1],Low[1]);
}Else
{
HigherAfterEntry = HigherAfterEntry[1];
LowerAfterEntry = LowerAfterEntry[1];
}
}
UpperBand = DayOpen + preDayRange*PercentOfRange;
LowerBand = DayOpen - preDayRange*PercentOfRange;
// 多头开仓
If(MarketPosition==0 && High>=UpperBand && Time < 140000 && bLongStoped==False)
//MarketPosition==0,表示当前持仓为0,可同于holding//
{
MyPrice = UpperBand;
If(Open > MyPrice) MyPrice = Open;
Buy(1,MyPrice); //buy开仓,以价格myprice开一手//
bLongStoped = False;
Return;
}
// 空头开仓
If(MarketPosition==0 && Low<=LowerBand && Time < 140000&& bShortStoped==False)
{
MyPrice = LowerBand;
If(Open < MyPrice) MyPrice = Open;
SellShort(1,MyPrice); //sellshort开空头,相当于金子塔中的buyshort//
bShortStoped = False;
Return;
}
// 止损平仓
If(MarketPosition==1) //相当于holding>0//
{
If(HigherAfterEntry >= AvgEntryPrice + DayOpen*TrailingStart*0.01)//跟踪
{
StopLine = HigherAfterEntry - DayOpen*TrailingStop*0.01;
}Else // 止损
{
StopLine = UpperBand-DayOpen*StopLossSet*0.01;
}
If(Low <= StopLine)
{
MyPrice = StopLine;
If(Open < MyPrice) MyPrice = Open;
Sell(1,MyPrice);
bLongStoped = True;
bShortStoped = False;
}
}Else If(MarketPosition==-1) //相当于holding<0//
{
If(LowerAfterEntry <= AvgEntryPrice - DayOpen*TrailingStart*0.01)//跟踪
{
StopLine = LowerAfterEntry + DayOpen*TrailingStop*0.01;
}Else // 止损
{
StopLine = LowerBand+DayOpen*StopLossSet*0.01;
}
If(High >= StopLine)
{
MyPrice = StopLine;
If(Open > MyPrice) MyPrice = Open;
BuyToCover(1,MyPrice); // BuyToCover平空头,相当于sellshort//
bLongStoped = False;
bShortStoped = True;
}
}
// 收盘平仓
If(Time >=145000)
{
Sell(1,Open);
BuyToCover(1,Open); //以开盘价全部平仓//
}
End END表示程序结束
带跟踪止损的区间突破系统
runmode:0;
input:percentOfRange(0.3,0.1,1,0.1);
input:minRange(0.2,0.1,1,0.1);
input:stopLossSet(0.5,0.1,1,0.1);
input:trailingStart(0.8,0.1,1,0.1);
input:trailingStop(0.5,0.1,1,0.1);
variable:longStopped=false;
variable:shortStopped=false;
variable:stopLine=0;
preDayHigh:=callstock(stklabel,vthigh,6,-1);
preDayLow:=callstock(stklabel,vtlow,6,-1);
dayOpen:=valuewhen(date<>ref(date,1),open);
preDayRange:=preDayHigh-preDayLow;
if preDayRange<dayOpen*minRange*0.01 then
preDayRange:=dayOpen*minRange*0.01;
upperBand:=trimprice(dayOpen+preDayRange*percentOfRange);
lowerBand:=trimprice(dayOpen-preDayRange*percentOfRange);
enterTime:=time>=09100 and time<=140000;
exitTime:=time>=145000;
longCond:=enterTime and high>=upperBand and not(longStopped);
longPrice:=max(upperBand,open);
shortCond:=enterTime and low<=lowerBand and not(shortStopped);
shortPrice:=min(lowerBand,open);
if holding=0 then begin
if longCond then begin
buy(1,1,limitr,longPrice);
longStopped:=true;
end
end
if holding=0 then begin
if shortCond then begin
buyshort(1,1,limitr,shortPrice);
shortStopped:=true;
end
end
higherAfterEntry:=ref(hhv(high,enterbars),1);
lowerAfterEntry:=ref(llv(low,enterbars),1);
if holding>0 then begin
if higherAfterEntry>=avgenterprice+dayOpen*trailingStart*0.01 then
stopline:=higherAfterEntry-dayOpen*trailingStop*0.01;
else
stopline:=upperBand-dayOpen*stopLossSet*0.01;
if low<=stopLine then begin
sell(1,holding,limitr,min(stopLine,open));
longStopped:=true;
shortStopped:=false;
end
if time>=exitTime then
sell(1,holding,limitr,open);
end
if holding<0 then begin
if lowerAfterEntry<=avgenterprice-dayOpen*trailingStart*0.01 then
stopline:=lowerAfterEntry+dayOpen*trailingStop*0.01;
else
stopline:=lowerBand+dayOpen*stopLossSet*0.01;
if high>=stopLine then begin
sellshort(1,holding,limitr,max(stopLine,open));
shortStopped:=true;
longStopped:=false;
end
if time>=exitTime then
sellshort(1,holding,limitr,open);
end