Numeric MinPoint; // 一个最小变动单位,也就是一跳
Numeric MyEntryPrice; // 开仓价格,本例是开仓均价,也可根据需要设置为某次入场的价格
Numeric TrailingStart1(50); // 跟踪止损启动设置1
Numeric TrailingStart2(80); // 跟踪止损启动设置2
Numeric TrailingStop1(30); // 跟踪止损设置1
Numeric TrailingStop2(20); // 跟踪止损设置2
Numeric StopLossSet(50); // 止损设置,默认50
Numeric MyExitPrice; // 平仓价格
NumericSeries HighestAfterEntry; // 开仓后出现的最高价
NumericSeries LowestAfterEntry; // 开仓后出现的最低价
//跟踪止盈止损
If(BarsSinceentry == 0)
{
HighestAfterEntry = Close;
LowestAfterEntry = Close;
If(MarketPosition <> 0)
{
HighestAfterEntry = Max(HighestAfterEntry,AvgEntryPrice); // 开仓的Bar,将开仓价和当时的收盘价的较大值保留到HighestAfterEntry
LowestAfterEntry = Min(LowestAfterEntry,AvgEntryPrice); // 开仓的Bar,将开仓价和当时的收盘价的较小值保留到LowestAfterEntry
}
}else
{
HighestAfterEntry = Max(HighestAfterEntry,High); // 记录下当前Bar的最高点,用于下一个Bar的跟踪止损判断
LowestAfterEntry = Min(LowestAfterEntry,Low); // 记录下当前Bar的最低点,用于下一个Bar的跟踪止损判断
}
Commentary("HighestAfterEntry="+Text(HighestAfterEntry));
Commentary("LowestAfterEntry="+Text(LowestAfterEntry));
MinPoint = MinMove*PriceScale;
MyEntryPrice = AvgEntryPrice;
If(MarketPosition==1) // 有多仓的情况
{
If(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart2*MinPoint) // 第二级跟踪止损的条件表达式
{
If(Low <= HighestAfterEntry[1] - TrailingStop2*MinPoint)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop2*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
Sell(0,MyExitPrice);
}
}else if(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart1*MinPoint)// 第一级跟踪止损的条件表达式
{
If(Low <= HighestAfterEntry[1] - TrailingStop1*MinPoint)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop1*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
Sell(0,MyExitPrice);
}
}else if(Low <= MyEntryPrice - StopLossSet*MinPoint)//可以在这里写上初始的止损处理
{
MyExitPrice = MyEntryPrice - StopLossSet*MinPoint;
If(Open < MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
Sell(0,MyExitPrice);
}
}else if(MarketPosition==-1) // 有空仓的情况
{
If(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart2*MinPoint) // 第二级跟踪止损的条件表达式
{
If(High >= LowestAfterEntry[1] + TrailingStop2*MinPoint)
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop2*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
BuyToCover(0,MyExitPrice);
}
}else if(LowestAfterEntry[1] <= MyEntryPrice + TrailingStart1*MinPoint)// 第一级跟踪止损的条件表达式
{
If(High >= LowestAfterEntry[1] + TrailingStop1*MinPoint)
{
MyExitPrice = LowestAfterEntry[1] - TrailingStop1*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
BuyToCover(0,MyExitPrice);
}
}else If(High >= MyEntryPrice + StopLossSet*MinPoint)//可以在这里写上初始的止损处理
{
MyExitPrice = MyEntryPrice + StopLossSet*MinPoint;
If(Open > MyExitPrice) MyExitPrice = Open; // 如果该Bar开盘价有跳空触发,则用开盘价代替
BuyToCover(0,MyExitPrice);
}
}
一大推代码看着真累。
怎么连个开仓条件都没有啊?
还不如直接说出你的思路,我帮你写
看别人的代码比自己写代码难受多了
举个例子,突破hi20买入,lo20卖出 止损采用你的设置,代码如下:(30行不到的代码就搞定)
variable:zs=c,hl=c;
hi20:=ref(hhv(h,20),1);
lo20:=ref(llv(l,20),1);
if holding>0 and l<zs then sell(1,1,limitr,min(o,zs)-mindiff);//止损
if holding<0 and h>zs then sellshort(1,1,limitr,max(o,zs)+mindiff);//止损
if holding>0 and l<lo20 then sell(1,1,limitr,min(o,lo20)-mindiff);//离场
if holding<0 and h>hi20 then sellshort(1,1,limitr,max(o,hi20)+mindiff);//离场
if holding=0 and h>hi20 then begin//开多
buy(1,1,limitr,max(o,hi20)+mindiff);
hl:=h;//记录开仓后的最高点
zs:=enterprice-50*mindiff;//初始止损50个跳动点
end
if holding=0 and l<lo20 then begin//开空
buyshort(1,1,limitr,min(o,lo20)-mindiff);
hl:=l;//记录开仓后的最低点
zs:=enterprice+50*mindiff;//初始止损50个跳动点
end
if holding>0 and h>hl then begin//上移最高点
hl:=h;
if hl>enterprice+80*mindiff then zs:=hl-20*mindiff;//满80个点,回落20点为止损位
else if hl>enterprice+50*mindiff then zs:=hl-30*mindiff;//满50个点,回落30点为止损位
end
if holding<0 and l<hl then begin
hl:=l;
if hl<enterprice-80*mindiff then zs:=hl+20*mindiff;//满80个点,反弹20点为止损位
else if hl<enterprice-50*mindiff then zs:=hl+30*mindiff;//满50个点,反弹30点为止损位
end
leevolvo版主在你原来基础上再加上定额百分比的止损止盈的意思就是点数换算成比率可以适合不同的品种合约
按照开仓点亏千分之五止损
盈利百分之二减仓1手,盈利百分之2.5再减仓一手
那就是要量身定制了?下班有事先回家,改天写
期待中
期待中
leevolvo版主在你原来基础上再加上定额百分比的止损止盈的意思就是点数换算成比率可以适合不同的品种合约
按照开仓点亏千分之五止损
盈利百分之二减仓1手,盈利百分之2.5再减仓一手
一共开仓2手呗。亏千分之5,2手全平?
盈利2%减一手。盈利2.5%全平?