spirit 发表于 2005-9-26 07:22

WR交易系统

这是老外写的源码,
里面我略加注释了一下,就应该都能看懂了.
我认为其中为了界定主图K线的上下边界曲线构造的序列数组挺复杂的.
那么下面会给出美股AA的图形,让大家有形像的认识.
在建仓时有这两条曲线的值参与运算得出进场价位.
可以看出它是一种以短周期持仓来抓价格形成背离之后的反弹波幅的策略.



var INTRADAYTRADES: boolean;
var SHORTDAYS, LONGDAYS, LONGPROFITTARGET, SHORTPROFITTARGET, WILLIAMSHIGH, WILLIAMSLOW, EMAPERIOD, ATRPERIOD, START, NLASTVARBAR, MT2, DE, PERIOD, MT, UT, X1, DT, XSERIES, UT2, DT2, BBUPPER, BBLOWER, LOWFACTOR, HIGHFACTOR, P: integer;

{#OptVar5 Default: 3}
{#OptVar4 Default: 27}
{#OptVar3 Default: 115}
{#OptVar2 Default: 85}
{#OptVar1 Default: 15}
{This is where you can adjust most of the
variables to optimize the script }

ShortDays := 2;
LongDays := 2;
LongProfitTarget := 10;
ShortProfitTarget := 5;
WilliamsHigh := 75;
WilliamsLow:= 25;
IntradayTrades := false;

HideVolume;   {Can delete this line if you need volume}

{------------------}



EMAPeriod := 9;
ATRPeriod := 30;
Start := EMAPeriod + ATRPeriod;
nLastVarBar := BarCount() - 1;

{Williams %R}

var X: float;
var PCTRPANE, SMOOTHR, BAR: integer;
PctRPane := CreatePane( 75, true, true );
PlotSeries( WilliamsRSeries( 14 ), PctRPane, 511, #Thick );
SmoothR := WilderMASeries( WilliamsRSeries( 14 ), 4 );
DrawLabel( 'WilliamsR( 14 )', PctRPane );
PlotSeries( SmoothR, PctRPane, #Black, #Thin );

//以WR指标窗口内的WR的值做WMA后所得值,以大于80或小于20为条件,将K线背景改变颜色
for Bar := 20 to BarCount - 1 do
begin
x := GetSeriesValue( Bar, SmoothR );
if x < 20 then
    SetBackgroundColor( Bar, #RedBkg )
else if x > 80 then
    SetBackgroundColor( Bar, #BlueBkg );

end;



mt2 := CreateSeries();
de := #OptVar1 / 10;{Nr Std Dev's}
period := #OptVar5;   //3


mt := EMASeries(#Close, period);//收盘价3周期EMA
ut := EMASeries(mt, period);//再次EMA
x1 := ((2 * period) / (period + 1)); // 2*3/4
dt := MultiplySeriesValue(ut, x1); //以ut的值乘以x1,再取整数部分,形成新的数组
dt := SubtractSeries(dt, ut);//以每个dt减去ut后,形成新的数组
dt := DivideSeriesValue(dt, ((period - 1) / (period + 1)) );//以dt的每个值除以一个数,得到新的数组

xseries := SubtractSeries(#Close, dt);//以收盘价减去dt的值,得新数组

//用以下循环给mt2数组赋值,应该是没有现成的数组函数
for Bar := period + 1 to BarCount - 1 do
begin
x := abs(GetSeriesValue(Bar, xseries));//取xseries数组元素的绝对值
SetSeriesValue(Bar, mt2, x);
end;

mt2 := EMASeries(mt2, period);//mt2的3周期ema
ut2 := EMASeries(mt2, period);
dt2 := MultiplySeriesValue(ut2, x1);
dt2 := SubtractSeries(dt2, ut2);
dt2 := DivideSeriesValue(dt2, ((period - 1) / (period + 1)) );


xseries := MultiplySeriesValue(dt2, de);//相乘得数组
BBUpper := AddSeries(dt, xseries);//相加得数组
BBLower := SubtractSeries(dt, xseries);//相减得数组


PlotSeries(BBUpper, 0, #Blue, 0);//按BBUpper数组,画指标曲线
PlotSeries(BBLower, 0, #Green, 0);

PlotSeries(dt, 0, #Red, 0);

LowFactor := #OptVar2 / 100; // 85/100
HighFactor := #OptVar3/ 100; // 115/100




{ Implement Trading Rules }

{BuyLimitPrice := 0.0; }

InstallStopLoss( 5 );//设定止损为5%
{InstallProfitTarget ( ProfitTarget );}//可选设定止盈语句



//因为有两条均线,所以把开台时间定在了star+1,即两条均线都有效的时候.
for Bar := Start + 1 to nLastVarBar do
begin

ApplyAutoStops( Bar );//自动停损设置成有效状态


    if GetSeriesValue( Bar, WilliamsRSeries( 14 )) < WilliamsLow then
//如果WR指标值<low
    if not LastActivePosition >= 0 then
    ShortAtLimit(Bar + 1, (GetSeriesValue(Bar, BBUpper) * 1.05), '' );
//下根K线限价做空价位选择主图上边界的值与1.05的乘积

    if GetSeriesValue( Bar, WilliamsRSeries( 14 )) > WilliamsHigh then
    if not LastActivePosition >= 0 then
    BuyAtLimit(Bar + 1, (GetSeriesValue(Bar, BBLower) * 0.95), '' );
//下根K线限价做多,价位选择主图下边界的值与0.95的乘积

for P := 0 to PositionCount - 1 do
   begin
      if PositionActive( P ) then             //有仓位吗
      begin
         if PositionLong( P ) then            //是多仓吗
         if bar + 1 - positionEntryBar( p ) > 0 then    //持仓周期>0吗
         if bar + 1 - positionEntryBar( p ) >= LongDays then   //持仓周期>2吗
         begin
            SellAtMarket( Bar + 1, P, 'LongDaysExpired' );//下周期平仓,因持仓周期限制
         end;
         if PositionLong( P ) then
         if bar + 1 - positionEntryBar( p ) > 0 then
         if PositionProfit( P )/PriceClose(Bar) >= LongProfitTarget then//净利润(美元)与收盘价的比>10
         begin
            SellAtMarket( Bar + 1, P, 'LongProfitTarget' );//平仓卖出因利润达到
         end;
         if not PositionLong( P )then         //没有多仓,即有空仓
         if bar + 1 - positionEntryBar( p ) > 0 then   //持仓周期>0吗
         if bar + 1 - positionEntryBar( p ) >= ShortDays then    //持仓周期>2吗
         begin;
            CoverAtMarket( Bar + 1, P, 'ShortDaysExpired' );//下周期平仓,因持仓周期限制
         end;
         if not PositionLong( P )then
         if bar + 1 - positionEntryBar( p ) > 0 then
         if PositionProfit( P )/PriceClose(Bar) >= ShortProfitTarget then   //净利润(美元)与收盘价的比>5
         begin;
            CoverAtMarket( Bar + 1, P, 'ShortProfitTarget' );//下周期平仓,因做空利润
         end;
         
         if PositionLong( P ) then   //如果有多仓
         if (IntradayTrades) then      //这是开关,前面有赋值语句,默认是false,则下面语句忽略执行
         if PositionProfit( P )/PriceClose(Bar) >= LongProfitTarget then
         begin
            SellAtMarket( Bar + 1, P, 'LongProfitTarget' );
         end;

         if not PositionLong( P )then
         if (IntradayTrades) then
         if PositionProfit( P )/PriceClose(Bar) >= ShortProfitTarget then
         begin;
            CoverAtMarket( Bar + 1, P, 'ShortProfitTarget' );
         end;

      end;
   end;

end;

[ Last edited by spirit on 2005-9-26 at 08:06 ]

ls729 发表于 2005-9-26 09:03

太多了,看不懂了

牧羊 发表于 2005-9-26 12:17

看来的学学,这种编成还没学会

没开刃 发表于 2005-9-26 13:01

学习学习

bockju 发表于 2005-9-26 13:04

谢谢楼主,学习

doublemint 发表于 2005-9-26 13:32

谁能把公式转成飞狐的?

csc101 发表于 2006-2-26 20:29

okokok

hellomsl 发表于 2012-5-17 14:05

#*d1*# 这顶你,这样也可以

x2921 发表于 2012-5-17 16:02

没精力看这么长的源码,纯顶。
页: [1]
查看完整版本: WR交易系统