感觉文华程序化比不上金字塔,现想使用金字塔程序化,请老师帮助把文华模型给转成金字塔的。再就在此模型中增加个止损止盈的功能。
a.当开仓后,亏损达到30个点,自动止损;
b. 当开仓后,盈利达到40到100点时,回撤80%,平仓;
c.开仓后,盈利达到100点以上时,回撤30%,平仓。 谢谢!
文华模型:
VAR6:=(2*CLOSE+HIGH+OPEN+LOW)/8;
VAR7:=LLV(LOW,12);
VAR8:=HHV(HIGH,20);
VAR9:=EMA((VAR6-VAR7)/(VAR8-VAR7)*150,13);
VARA:=EMA(0.667*REF(VAR9,1)+0.333*VAR9,2);
SARLINE:=ABS(SAR(3,0.01,0.1));
TR:=SUM(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),6);
HD:=HIGH-REF(HIGH,1);
LD:=REF(LOW,1)-LOW;
DMP:=SUM(IFELSE(HD>0 && HD>LD,HD,0),6);
DMM:=SUM(IFELSE(LD>0 && LD>HD,LD,0),6);
PDI:=DMP*100/TR,COLORWHITE;
MDI:=DMM*100/TR,COLORYELLOW;
ADX:=MA(ABS(MDI-PDI)/(MDI+PDI)*150,20),COLORRED;
ADXR:=(ADX+REF(ADX,14))/2,COLORGREEN;
MA10:=EMA(CLOSE,11);
MA1:=MA(CLOSE,5);
VAR9>VARA&&CLOSE>=SARLINE&&MA10>REF(MA10,1)&&ADX>30&&CLOSE>REF(OPEN,1)&&CLOSE>REF(OPEN,2)&&CLOSE>REF(CLOSE,1)&&CLOSE>REF(CLOSE,2)&&CLOSE>MA1&&CLOSE>OPEN,BPK;
VAR9<=VARA&&CLOSE<SARLINE&&MA10<REF(MA10,1)&&ADX<80&&CLOSE<REF(OPEN,1)&&CLOSE<REF(OPEN,2)&&CLOSE<REF(CLOSE,1)&&CLOSE<REF(CLOSE,2)&&CLOSE<MA1&&CLOSE<OPEN,SPK;
问题正在解决中
仅把策略改写到金字塔里,以下代码即可实现.
VAR6:=(2*CLOSE+HIGH+OPEN+LOW)/8;
VAR7:=LLV(LOW,12);
VAR8:=HHV(HIGH,20);
VAR9:=EMA((VAR6-VAR7)/(VAR8-VAR7)*150,13);
VARA:=EMA(0.667*REF(VAR9,1)+0.333*VAR9,2);
SARLINE:=ABS(SAR(3,0.01,0.1));
T:=SUM(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),6);
HD:=HIGH-REF(HIGH,1);
LD:=REF(LOW,1)-LOW;
DMP:=SUM(IF(HD>0 and HD>LD,HD,0),6);
DMM:=SUM(IF(LD>0 and LD>HD,LD,0),6);
PDI:=DMP*100/T;
MDI:=DMM*100/T,;
ADX:=MA(ABS(MDI-PDI)/(MDI+PDI)*150,20),COLORRED;
ADXR:=(ADX+REF(ADX,14))/2,COLORGREEN;
MA10:=EMA(CLOSE,11);
MA1:=MA(CLOSE,5);
//平空开多
long:=MA10>REF(MA10,1) and CLOSE>REF(OPEN,1) and CLOSE>REF(OPEN,2) and CLOSE>REF(CLOSE,1) and CLOSE>REF(CLOSE,2) and CLOSE>MA1;//最新价高于前两个周期的开盘价和收盘价。
//平多开空
short:=MA10<REF(MA10,1) and CLOSE<REF(OPEN,1) and CLOSE<REF(OPEN,2) and CLOSE<REF(CLOSE,1) and CLOSE<REF(CLOSE,2) and CLOSE<MA1;//最新价低于前两个周期的开盘价和收盘价。
{开多}ENTERLONG:long and VAR9>VARA and CLOSE>=SARLINE and ADX>30 and CLOSE>OPEN,TFILTER;
{平多}EXITLONG:short and VAR9<=VARA and CLOSE<SARLINE and ADX<80 and CLOSE<OPEN,TFILTER;
{开空}ENTERSHORT:short and VAR9<=VARA and CLOSE<SARLINE and ADX<80 and CLOSE<OPEN ,TFILTER ;
{平空}EXITSHORT:long and VAR9>VARA and CLOSE>=SARLINE and ADX>30 and CLOSE>OPEN ,TFILTER;
如果是想实现止损止盈,那就需要标准版及其以上
以下以高级图表程序化交易-----标准版功能,为例说明
//K线走完模式,逐K线模式
variable:maxprofit=0;//有仓位时最大获利幅度
VAR6:=(2*CLOSE+HIGH+OPEN+LOW)/8;
VAR7:=LLV(LOW,12);
VAR8:=HHV(HIGH,20);
VAR9:=EMA((VAR6-VAR7)/(VAR8-VAR7)*150,13);
VARA:=EMA(0.667*REF(VAR9,1)+0.333*VAR9,2);
SARLINE:=ABS(SAR(3,0.01,0.1));
T:=SUM(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),6);
HD:=HIGH-REF(HIGH,1);
LD:=REF(LOW,1)-LOW;
DMP:=SUM(IF(HD>0 and HD>LD,HD,0),6);
DMM:=SUM(IF(LD>0 and LD>HD,LD,0),6);
PDI:=DMP*100/T;
MDI:=DMM*100/T,;
ADX:=MA(ABS(MDI-PDI)/(MDI+PDI)*150,20),COLORRED;
ADXR:=(ADX+REF(ADX,14))/2,COLORGREEN;
MA10:=EMA(CLOSE,11);
MA1:=MA(CLOSE,5);
//平空开多
long:=MA10>REF(MA10,1) and CLOSE>REF(OPEN,1) and CLOSE>REF(OPEN,2) and CLOSE>REF(CLOSE,1) and CLOSE>REF(CLOSE,2) and CLOSE>MA1;//最新价高于前两个周期的开盘价和收盘价。
//平多开空
short:=MA10<REF(MA10,1) and CLOSE<REF(OPEN,1) and CLOSE<REF(OPEN,2) and CLOSE<REF(CLOSE,1) and CLOSE<REF(CLOSE,2) and CLOSE<MA1;//最新价低于前两个周期的开盘价和收盘价。
if long and VAR9>VARA and CLOSE>=SARLINE and ADX>30 and CLOSE>OPEN then
begin
sellshort(holding<0,1,market);
buy(holding=0,1,market);
maxprofit:=0;
end
if short and VAR9<=VARA and CLOSE<SARLINE and ADX<80 and CLOSE<OPEN then
begin
sell(holding>0,1,market);
buyshort(holding=0,1,market);
maxprofit:=0;
end
//判断当前持仓状态下的最大盈利
win:=0;
win2:=0;
if holding > 0 and enterbars > 0 then
begin
win:=(c-enterprice)/enterprice*100; //记录最大盈利
if win > maxprofit then
maxprofit:=win;
win2:=(maxprofit-win)/maxprofit*100; //最大盈利后的回调幅度
end
if holding < 0 and enterbars > 0 then
begin
win:=(enterprice-c)/enterprice*100; //记录最大盈利
if win > maxprofit then
maxprofit:=win;
win2:=(maxprofit-win)/maxprofit*100; //最大盈利后的回调幅度
end
//亏损达到30个点---30个最小变动价位,自动止损;
SELL((ENTERPRICE-c)>=30*mindiff and holding>0,0,MARKET);
SELLSHORT((c-ENTERPRICE)>=30*mindiff and holding<0,0,MARKET);
//当开仓后,盈利达到40到100点时,回撤80%,平仓;
if c-enterprice>40*mindiff and c-enterprice<100*mindiff and holding>0 then
begin
SELL(win2 >= 80, 0,market);
end
if enterprice-c>40*mindiff and enterprice-c<100*mindiff and holding<0 then
begin
SELL(win2 >= 80, 0,market);
end
//开仓后,盈利达到100点以上时,回撤30%,平仓。
if c-enterprice>=100*mindiff and holding>0 then
begin
SELL(win2 >= 30, 0,market);
end
if enterprice-c>=100*mindiff and holding<0 then
begin
SELL(win2 >= 30, 0,market);
end
//不知对不对,加入了平仓参数p
input:p(1,1,100,1);
VAR6:=(2*CLOSE+HIGH+OPEN+LOW)/8;
VAR7:=LLV(LOW,12);
VAR8:=HHV(HIGH,20);
VAR9:=EMA((VAR6-VAR7)/(VAR8-VAR7)*150,13);
VARA:=EMA(0.667*REF(VAR9,1)+0.333*VAR9,2);
SARLINE:=ABS(SAR(3,0.01,0.1));
TR1:=SUM(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),6);
HD:=HIGH-REF(HIGH,1);
LD:=REF(LOW,1)-LOW;
DMP:=SUM(IFELSE(HD>0 && HD>LD,HD,0),6);
DMM:=SUM(IFELSE(LD>0 && LD>HD,LD,0),6);
PDI:=DMP*100/TR1,COLORWHITE;
MDI:=DMM*100/TR1,COLORYELLOW;
ADX:=MA(ABS(MDI-PDI)/(MDI+PDI)*150,20),COLORRED;
ADXR:=(ADX+REF(ADX,14))/2,COLORGREEN;
MA10:=EMA(CLOSE,11);
MA1:=MA(CLOSE,5);
if VAR9>VARA&&CLOSE>=SARLINE&&MA10>REF(MA10,1)&&ADX>30&&CLOSE>REF(OPEN,1)&&CLOSE>REF(OPEN,2)&&CLOSE>REF(CLOSE,1)&&CLOSE>REF(CLOSE,2)&&CLOSE>MA1&&CLOSE>OPEN then
begin
SELLSHORT(1 ,holding ,MARKET );
BUY( 1,holding ,MARKET);
end
if VAR9<=VARA&&CLOSE<SARLINE&&MA10<REF(MA10,1)&&ADX<80&&CLOSE<REF(OPEN,1)&&CLOSE<REF(OPEN,2)&&CLOSE<REF(CLOSE,1)&&CLOSE<REF(CLOSE,2)&&CLOSE<MA1&&CLOSE<OPEN then
begin
SELL(1 ,holding ,MARKET );
BUYSHORT(1 ,holding ,MARKET );
end
//判断是否亏损达到30个点,自动止损;
if enterprice-c>=30*mindiff then
begin
sell(1,holding,market);
end
if c-enterprice>=30*mindiff then
begin
sellshort(1,holding,market);
end
//当开仓后,盈利达到40到100点时,回撤80%,平仓
HV:=HHV(ASSET,0); //交易来最大资产
HB:=HV - ASSET; //当前的回撤
hui:HB/HV;
if c-enterprice>=40*mindiff and c-enterprice<=100*mindiff and hui>=0.8 then
begin
sell(1,p,market);
end
if enterprice-c>=40*mindiff and enterprice-c<=100*mindiff and hui=0.8 then
begin
sellshort(1,p,market);
end
//开仓后,盈利达到100点以上时,回撤30%,平仓
if c-enterprice>=100*mindiff and hui=0.3 then
begin
sell(1,p,market);
end
if enterprice-c>=100*mindiff and hui=0.3 then
begin
sellshort(1,p,market);
end
我说的回撤是指:开仓后,比如多头,价格涨到100 然后反相的掉到80 就是回撤了20%
再想请教个问题,就是模型在出现开平信号后,是马上开平仓,还是延迟发单呢,如马上发单,那有时信号只出一下就会变的,会造成频繁开平仓的。
问下上面回复的:平仓参数p 指的是什么呢
p是参数 ,手动可以设的 平仓手数。默认是1手。
上面的回撤 不是按照 “开仓后,比如多头,价格涨到100 然后反相的掉到80 就是回撤了20%”
而是总资产的回撤来编写的。