WL4 стратегия Williams Trader

7/28/2011 10:31:22 PM
Gravatar
Total Posts 2

WL4 стратегия Williams Trader

Есть стратегия для Wealth-Lab 4. Хочу протестировать ее под WLD .NET, подскажите как ее модифицировать

---текст

[CODE]

(*Description...

This script uses Williams %R for intraday and short-term trading, and gets lots of trades. To change fr om intraday to short-term change 'IntradayTrades' to false. The days to expiry and the profit target can be set separately for long and short positions.

Credit for this script goes to a piece of code fr om the Wealthlab Developer help file, and jrbyrd who wrote "A MultiPosition Buy-At-Limit System using Adaptive BBands".

*)

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 wh ere 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 );

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;

mt := EMASeries(#Close, period);

ut := EMASeries(mt, period);

x1 := ((2 * period) / (period + 1));

dt := MultiplySeriesValue(ut, x1);

dt := SubtractSeries(dt, ut);

dt := DivideSeriesValue(dt, ((period - 1) / (period + 1)) );

xseries := SubtractSeries(#Close, dt);

for Bar := period + 1 to BarCount - 1 do

begin

x := abs(GetSeriesValue(Bar, xseries));

SetSeriesValue(Bar, mt2, x);

end;

mt2 := EMASeries(mt2, period);

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);

PlotSeries(BBLower, 0, #Green, 0);

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

LowFactor := #OptVar2 / 100;

HighFactor := #OptVar3/ 100;

{ Implement Trading Rules }

{BuyLimitPrice := 0.0; }

InstallStopLoss( 5 );

{InstallProfitTarget ( ProfitTarget );}

for Bar := Start + 1 to nLastVarBar do

begin

ApplyAutoStops( Bar );

if GetSeriesValue( Bar, WilliamsRSeries( 14 )) < WilliamsLow then

if not LastActivePosition >= 0 then

ShortAtLimit(Bar + 1, (GetSeriesValue(Bar, BBUpper) * 1.05), '' );

if GetSeriesValue( Bar, WilliamsRSeries( 14 )) > WilliamsHigh then

if not LastActivePosition >= 0 then

BuyAtLim it(Bar + 1, (GetSeriesValue(Bar, BBLower) * 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

if bar + 1 - positionEntryBar( p ) >= LongDays then

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

begin

SellAtMarket( Bar + 1, P, 'LongProfitTarget' );

end;

if not PositionLong( P )then

if bar + 1 - positionEntryBar( p ) > 0 then

if bar + 1 - positionEntryBar( p ) >= ShortDays then

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

begin;

CoverAtMarket( Bar + 1, P, 'ShortProfitTarget' );

end;

if PositionLong( P ) then

if (IntradayTrades) then

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;

[/CODE]

---конец

7/28/2011 11:19:41 PM
Gravatar
Total Posts 151

RE:WL4 стратегия Williams Trader

Задача не тривиальная, предлагаем вам решение в первом приближении.

[CODE]using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using WealthLab;

using WealthLab.Indicators;

namespace WealthLab.Strategies

{

public class MyStrategy : WealthScript

{

StrategyParameter OptVar5;

StrategyParameter OptVar4;

StrategyParameter OptVar3;

StrategyParameter OptVar2;

StrategyParameter OptVar1;

public MyStrategy()

{

OptVar5 = CreateParameter("optVar5", 3, 0, 100, 1);

OptVar4 = CreateParameter("optVar4", 27, 0, 100, 1);

OptVar3 = CreateParameter("optVar3", 115, 0, 100, 1);

OptVar2 = CreateParameter("optVar2", 85, 0, 100, 1);

OptVar1 = CreateParameter("optVar1", 15, 0, 100, 1);

}

protected override void Execute()

{

int ShortDays = 2;

int LongDays = 2;

double LongProfitTarget = 10;

double ShortProfitTarget = 5;

double WilliamsHigh = 75;

double WilliamsLow = 25;

bool IntradayTrades = false;

int EMAPeriod = 9;

int ATRPeriod = 30;

int Start = EMAPeriod + ATRPeriod;

int nLastVarBar = Bars.Count - 1;

DataSeries williams14 = WilliamsR.Series(Bars, 14);

ChartPane PctRPane = CreatePane(100, true, false);

PlotSeries(PctRPane, williams14, Color.Black, WealthLab.LineStyle.Solid, 1);

DataSeries SmoothR = WilderMA.Series( williams14, 4 );

PlotSeries( PctRPane, SmoothR, Color.Black, WealthLab.LineStyle.Dots, 2 );

for (int bar = 20 ;bar<Bars.Count - 1;bar++ )

{

double x = SmoothR[bar];

if (x < 20 ) SetBackgroundColor( bar, Color.Red );

else if (x > 80 ) SetBackgroundColor( bar, Color.Blue);

}

DataSeries mt2 = new DataSeries(Bars, "mt2");

double de = OptVar1.Value / 10.0; // {Nr Std Dev's}

int period = OptVar5.ValueInt;

DataSeries mt = EMA.Series(Close, period, WealthLab.Indicators.EMACalculation.Modern);

DataSeries ut = EMA.Series(mt, period, WealthLab.Indicators.EMACalculation.Modern);

double x1 = ((2.0 * period) / (period + 1.0));

DataSeries dt = x1*ut;

dt = dt - ut;

dt = dt / ((period - 1.0) / (period + 1.0));

DataSeries xseries = Close - dt;

for(int bar = 1; bar<Bars.Count - 1;bar++)

{

mt2[bar] = bar < period+1 ? 0: Math.Abs(xseries[bar]);

}

mt2 = EMA.Series(mt2, period, WealthLab.Indicators.EMACalculation.Legacy);

DataSeries ut2 = EMA.Series(mt2, period, WealthLab.Indicators.EMACalculation.Modern);

DataSeries dt2 = ut2 * x1;

dt2 = dt2 - ut2;

dt2 = dt2 / ((period - 1.0) / (period + 1.0));

xseries = dt2* de;

DataSeries BBUpper = dt+ xseries;

DataSeries BBLower = dt- xseries;

//PlotSeries(PricePane, dt, Color.Blue, WealthLab.LineStyle.Solid, 2);

//PlotSeries(PricePane, xseries, Color.Green, WealthLab.LineStyle.Solid, 2);

PlotSeries(PricePane, BBUpper, Color.Blue, WealthLab.LineStyle.Solid, 2);

PlotSeries(PricePane, BBLower, Color.Green, WealthLab.LineStyle.Solid, 2);

PlotSeries(PricePane, dt, Color.Red, WealthLab.LineStyle.Solid, 1);

double LowFactor = OptVar2.Value / 100;

double HighFactor = OptVar3.Value / 100;

//{ Implement Trading Rules }

for ( int bar = Start + 1 ; bar<nLastVarBar;bar++ )

{

if(LastActivePosition == null)

{

if (williams14[bar] < WilliamsLow )

{

//ShortAtMarket(bar + 1);

ShortAtLimit(bar + 1, BBUpper[bar] * 1.005);//параметр для изменения

}

if (williams14[bar] > WilliamsHigh )

{

//BuyAtMarket(bar + 1);

BuyAtLimit(bar + 1, BBLower[bar] * 0.995);//параметр для изменения

}

}

else

{

for(int p = ActivePositions.Count-1;p>=0;p--)

{

Position pos = ActivePositions[p];

if(pos.PositionType == PositionType.Long)

{

if(bar + 1 - pos.EntryBar >= LongDays )

{

SellAtMarket( bar + 1, pos, "LongDaysExpired" );

}else

if(pos.NetProfit/Close[bar] >= LongProfitTarget )

{

SellAtMarket( bar + 1, pos, "LongProfitTarget" );

}else

if (IntradayTrades)

if(pos.NetProfit/Close[bar] >= LongProfitTarget )

{

SellAtMarket( bar + 1, pos, "LongProfitTarget" );

}

}

else//if not PositionLong( P )then

{

if(bar + 1 - pos.EntryBar >= ShortDays )

{

CoverAtMarket( bar + 1, pos, "ShortDaysExpired" );

}else

if(pos.NetProfit/Close[bar] >= ShortProfitTarget )

{

CoverAtMarket( bar + 1, pos, "ShortProfitTarget" );

}else

if (IntradayTrades)

if (pos.NetProfit/Close[bar] >= ShortProfitTarget )

{

CoverAtMarket( bar + 1, pos, "ShortProfitTarget" );

}

}

}

}

}

}

}

}

[/CODE]

7/29/2011 2:14:29 PM
Gravatar
Total Posts 142

RE:WL4 стратегия Williams Trader

Здравствуйте!

А как сделать так , чтобы сигналы отрисовывались в офлайне, нужно для оптимизации, в велсе 4. То есть сигналы на всей истории.

С уважением, Василий.

7/29/2011 5:02:12 PM
Gravatar
Total Posts 151

RE:WL4 стратегия Williams Trader

К счастью, мы работаем с WLD 6.2. Вам советуем приобрести у нас или получить триальную версию WLD 6