Supertrend in express code

Stel hier uw vragen over TA-script, of help anderen met het oplossen van hun probleem
Plaats reactie
Tibovwh
Berichten: 1
Lid geworden op: di apr 21, 2015 9:59 pm

Supertrend in express code

Bericht door Tibovwh » di apr 21, 2015 10:09 pm

Hallo,

Ik heb zelf niet zo heel veel kennis van programmeren en de handleidingen van de software helpt mij niet echt veel verder. Op zoek naar meer informatie kwam ik op dit forum terecht. Ik wil graag de WHS Supertrend indicator uit WHS Prostation platform overzetten naar de WHS Futurestation software. Deze laatste gebruikt 'express' als taal, wat me wel een vrij simpele taal lijkt. Ik heb het begin kunnen doen maar op het einde zit ik vast. Ik had gehoopt dat iemand hier goed kan werken met deze zaken en me verder op weg kan helpen.

De originele code:

Code: Selecteer alles

indicator SuperTrend;
input
  CalcPriceType = 1,    // type of price calculation over which average will be calculated
  TrendPriceType = 2,   // type of price calculation over which trend direction will be calculated
  VolatPeriod = 1,      // period to look for volatility
  VolatCoeff = 1.8,     // distance from average, multiply of volatility
  AvgToVolatRatio = 10, // period to calculate average, multiply of volatility period
  UpDnCalcType = 6;     // type of calculation for stops
draw 
  AvgPrice("Average Price"),
  Stop("Stop");
vars
  i(number),
  VolatUpCoeff(number),
  VolatDnCoeff(number),
  HH(series),
  LL(series),
  CalcPrice(series),
  TrendPrice(series),
  Volat(series),
  AvgVolat(series),
  AvgPeriod(number),
  Up(series),
  Dn(series),
  Trend(series);
begin
  VolatUpCoeff := abs(VolatCoeff);
  VolatDnCoeff := -abs(VolatCoeff);
  AvgPeriod := AvgToVolatRatio*VolatPeriod;
  
  HH := Highest(High, VolatPeriod);
  LL := Lowest(Low, VolatPeriod);
  
  if CalcPriceType = 1 then
    CalcPrice := Close; 
  if CalcPriceType = 2 then
    CalcPrice := (HH+LL)/2; 
  if CalcPriceType = 3 then 
    CalcPrice := (HH+LL+Close)/3; 
  if CalcPriceType = 4 then begin
    CalcPrice := Close;
    for i := front(Close)+VolatPeriod-1 to back(Close) do
      CalcPrice[i] := (HH[i]+LL[i]+Close[i]+Low[i-VolatPeriod+1])/4; 
  end;
  if CalcPriceType = 5 then begin
    CalcPrice := Close;
    for i := front(Close)+VolatPeriod to back(Close) do
      CalcPrice[i] := (HH[i]+LL[i]+Close[i-VolatPeriod])/3; 
  end;
  if CalcPriceType = 6 then begin
    CalcPrice := Close;
    for i := front(Close)+VolatPeriod to back(Close) do
      CalcPrice[i] := (HH[i]+LL[i]+Close[i-VolatPeriod]+Low[i-VolatPeriod+1])/4; 
  end;

  if TrendPriceType = 1 then 
    TrendPrice := Close;
  if TrendPriceType = 2 then 
    TrendPrice := (HH+LL)/2; 
  if TrendPriceType = 3 then 
    TrendPrice := (HH+LL+Close)/3; 
  if TrendPriceType = 4 then begin
    TrendPrice := Close;
    for i := front(Close)+VolatPeriod-1 to back(Close) do
      TrendPrice[i] := (HH[i]+LL[i]+Close[i]+Low[i-VolatPeriod+1])/4; 
  end;
  if TrendPriceType = 5 then begin
    TrendPrice := Close;
    for i := front(Close)+VolatPeriod to back(Close) do
      TrendPrice[i] := (HH[i]+LL[i]+Close[i-VolatPeriod])/3; 
  end;
  if TrendPriceType = 6 then begin
    TrendPrice := Close;
    for i := front(Close)+VolatPeriod to back(Close) do
      TrendPrice[i] := (HH[i]+LL[i]+Close[i-VolatPeriod]+Low[i-VolatPeriod+1])/4; 
  end;

  Volat := (HH - LL)/2;
  Up := CalcPrice;
  Dn := CalcPrice;

  if AvgToVolatRatio>1 then begin 
  
    if UpDnCalcType=1 or UpDnCalcType=2 then begin 
      AvgPrice := SMA(CalcPrice, AvgPeriod); 
      AvgVolat := SMA(Volat, AvgPeriod); 
      Up := AvgPrice+VolatUpCoeff*AvgVolat; 
      Dn := AvgPrice+VolatDnCoeff*AvgVolat; 
    end; 
    if UpDnCalcType=3 or UpDnCalcType=4 then begin 
      AvgPrice := EMA(CalcPrice, AvgPeriod); 
      AvgVolat := EMA(Volat, AvgPeriod); 
      Up := AvgPrice+VolatUpCoeff*AvgVolat; 
      Dn := AvgPrice+VolatDnCoeff*AvgVolat; 
    end; 
    if UpDnCalcType=5 or UpDnCalcType=6 then begin 
      AvgPrice := WMA(CalcPrice, AvgPeriod); 
      AvgVolat := WMA(Volat, AvgPeriod); 
      Up := AvgPrice+VolatUpCoeff*AvgVolat; 
      Dn := AvgPrice+VolatDnCoeff*AvgVolat; 
    end; 
    if UpDnCalcType=7 or UpDnCalcType=8 then begin 
      AvgPrice :=WMA(2*WMA(CalcPrice, int(AvgPeriod/2)) - WMA(CalcPrice, AvgPeriod), 
int(SQRT(AvgPeriod)));
      AvgVolat :=WMA(2*WMA(Volat, int(AvgPeriod/2)) - WMA(Volat, AvgPeriod), 
int(SQRT(AvgPeriod)));
      Up := AvgPrice+VolatUpCoeff*AvgVolat; 
      Dn := AvgPrice+VolatDnCoeff*AvgVolat; 
    end; 
  end else begin
    AvgPrice := CalcPrice; 
    Up := CalcPrice+VolatUpCoeff*Volat; 
    Dn := CalcPrice+VolatDnCoeff*Volat; 

  end;

  Trend := makeseries(front(Close), back(Close), 0);
  for i := front(Close)+AvgPeriod+1 to back(Close) do begin
    if (AvgToVolatRatio>1) and (UpDnCalcType=2 or UpDnCalcType=4 or UpDnCalcType=6 or 
UpDnCalcType=8) then begin
      if Trend[i-1]<0 then begin
        if Up[i]>Up[i-1] then
          Up[i] := Up[i-1];
      end;
      if Trend[i-1]>=0 then begin
        if Dn[i]<Dn[i-1] then
          Dn[i] := Dn[i-1];
      end;
    end;

    Trend[i] := Trend[i-1];
    if TrendPrice[i]>Up[i] then
      Trend[i] := 1;
    if TrendPrice[i]<Dn[i] then 
      Trend[i] := -1;
  end;

  for i := front(Close)+AvgPeriod to back(Close) do begin
    if Trend[i] >= 0 then
      Stop[i] := Dn[i]
    else
      Stop[i] := Up[i];
  end;
  
end.
de code die ik zelf al een deel aangepast heb voor 'express'. Ik heb een aantal van de opties wel weggelaten om het simpel te houden...

Code: Selecteer alles

Express OWN_Supertrend

Vars

series HH, LL, CalcPrice, TrendPrice, Volat, AvgPrice, AvgVolat, AvgPeriod, Up, Dn, Trend;
numeric i, VolatUpCoeff, VolatDnCoeff;
input $CalcPriceType(1,5,1);  		  // type of price calculation over which AvgPrice will be calculated (for information see Section 'CalcPrice')
input $TrendPriceType(1,2,1);   	  // type of price calculation over which Trend direction will be calculated (for information see Section 'TrendPrice')
input $UpDnCalcType(1,8,1);   		  // type of calculation for stops (for information see Section 'Up and Down')
input $VolatPeriod(1,3,1);    		  // period to look for volatility
input $VolatCoeff(1,4,2);   		  // distance from AvgPrice as a multiple of volatility
input $AvgToVolatRatio(1,15,10); 	  // period to calculate average, multiply of volatility period

Calculation

if IsFirstBar() then 
begin
  CalculateAtEveryTick(false);
  SetYscaleFormat(GetPriceFormat());
end

VolatUpCoeff = AbsValue($VolatCoeff);
VolatDnCoeff = -AbsValue($VolatCoeff);
AvgPeriod = $AvgToVolatRatio*$VolatPeriod;
HH = Highest(High, $VolatPeriod);
LL = Lowest(Low, $VolatPeriod);


/////////////////////////////////////////////////////
////////              CalcPrice           ///////////
////////  1 = Close
////////  2 = (HH + LL) / 2
////////  3 = (HH + LL + Close) /3
/////////////////////////////////////////////////////


if $CalcPriceType = 1 then
    CalcPrice = Close; 

if $CalcPriceType = 2 then
    CalcPrice = (HH+LL)/2; 

if $CalcPriceType = 3 then 
    CalcPrice = (HH+LL+Close)/3;



/////////////////////////////////////////////////////
////////    TrendPrice  /////////////
/////////////////////////////////////////////////////


if $TrendPriceType = 1 then 
    TrendPrice = Close;

if $TrendPriceType = 2 then 
    TrendPrice = (HH+LL)/2;

if $TrendPriceType = 3 then 
    TrendPrice = (HH+LL+Close)/3;


/////////////////////////////////////////////////////////


Volat = (HH - LL)/2;
Up = CalcPrice;
Dn = CalcPrice;


/////////////////////////////////////////////////////
//////////    Up and Dn          
//////////    1 or 2 = MA
//////////    3 or 4 = EME
//////////    5 or 6 = Weighted
/////////////////////////////////////////////////////



if $AvgToVolatRatio > 1 then
begin  
    if $UpDnCalcType = 1 or $UpDnCalcType = 2 then
      begin 
          MovingAverage(CalcPrice, AvgPrice, $AvgPeriod); 
          MovingAverage(Volat, AvgVolat, $AvgPeriod); 
          Up = AvgPrice+VolatUpCoeff*AvgVolat; 
          Dn = AvgPrice+VolatDnCoeff*AvgVolat; 
      end
else
begin
    AvgPrice = CalcPrice; 
    Up = CalcPrice+VolatUpCoeff*Volat; 
    Dn = CalcPrice+VolatDnCoeff*Volat; 

end

/////////////////////////////////////////////////////
////////    Trend 
/////////////////////////////////////////////////////


Trend = makeseries(front(Close), back(Close), 0);
  for i = front(Close)+AvgPeriod+1 to back(Close) do
  begin
        if (AvgToVolatRatio>1) and (UpDnCalcType=2 or UpDnCalcType=4 or UpDnCalcType=6 or UpDnCalcType=8) then 
          begin
            if Trend[i-1]<0 then 
              begin
                if Up[i]>Up[i-1] then
                  Up[i] = Up[i-1];
              end
            if Trend[i-1]>=0 then 
              begin
                if Dn[i]<Dn[i-1] then
                  Dn[i] = Dn[i-1];
              end
          end

   Trend[i] = Trend[i-1];
        if TrendPrice[i]>Up[i] then
            Trend[i] = 1;
        if TrendPrice[i]<Dn[i] then 
            Trend[i] = -1;
   end

  for i = front(Close)+AvgPeriod to back(Close) do
   begin
        if Trend[i] >= 0 then
            Stop[i] = Dn[i]
        else
            Stop[i] = Up[i];
    end

////////////////////////////////////////////////////
  
Interpretation

plot (Stop, Black, 2);
Het loopt fout vanaf de sectie 'Trend'.

Mvg,

Thibaut Van Weehaeghe

Plaats reactie