macd combo

Stel hier uw vragen over TA-script, of help anderen met het oplossen van hun probleem
Eric
Berichten: 2880
Lid geworden op: za sep 10, 2005 2:41 am
Locatie: Den Haag

Re: macd combo

Bericht door Eric » zo apr 15, 2018 10:37 pm

Dat wel, maar het is altijd een flink klusje om van allerlei systemen in elkaar te schuiven.

Code: Selecteer alles

{- Filename: Tesjoeva's MACD-PetDev system -}

function HullMA(Prices: TSeries; Period: integer): TSeries;
begin
// Integer (Square Root (Period)) WMA [2 x Integer (Period/2) WMA (Price) - Period WMA (Price)]
//  waverage(2 * waverage(close, period/2) - waverage(close,period), SquareRoot(Period))
  Result := MA(SubtractSeries(MultiplySeriesBy(MA(Prices, maWeighted, Period div 2), 2),
                              MA(Prices, maWeighted, Period)),
               maWeighted, trunc(sqrt(Period)));
end;

{- Filename: PetDev's system -}

function RSI2(Price: TSeries; Periode: integer): TSeries;
var
  Count, i, n: integer;
  Diff: TSeries;
  Up, Down: real;
begin
  Count := GetArrayLength(Price);
  Result := CreateSeries(Count);
  Diff := Momentum(Price, 1);
  Up := 0;
  Down := 0;
  n := 0;
  for i:=FirstValidIndex(Diff) to Count-1 do
  begin
    if Diff[i] > 0 then Up := Up + Diff[i] else Down := Down - Diff[i];
    if n <= Periode then
    begin
      n := n+1;
    end else
    begin
      if Diff[i-Periode-1] > 0 then Up := Up - Diff[i-Periode-1] else Down := Down + Diff[i-Periode-1];
    end;
    if Down <> 0 then Result[i] := 100 * (Up/Down) / (1+Up/Down);
  end;
end;

type
  TState = (stNeutral, stSetupLong, stSetupShort);

function PetDev(Close: TSeries;
                nRSI: integer; bRSIExp: boolean; nRSILo, nRSIHi: integer;
                nMACD1, nMACD2, nMACD3, nEMA1, nEMA2, nMA: integer): TSeries;
var
  i, iFirst, Count: integer;
  sRSI, sMACD1, sMACD, sEMA1, sEMA2, sMA: TSeries;
  BuyScore, SellScore, LongTrigger, ShortTrigger, valResult: real;
  State: TState;
begin
  Count := GetArrayLength(Close);
  Result := FillSeries(CreateSeries(BarCount), 0);

{ Indicatorberekening }
  if bRSIExp then
    sRSI := RSI(Close, nRSI)
  else
    sRSI := RSI2(Close, nRSI);
  sMACD1 := MACD(Close, nMACD1, nMACD2);
  sMACD := MA(sMACD1, maExponential, nMACD3);
  sEMA1 := MA(Close, maExponential, nEMA1);
  sEMA2 := MA(Close, maExponential, nEMA2);
  sMA := MA(Close, maSimple, nMA);

{ signalen bepalen }
  iFirst := FirstValidIndex(sRSI);
  iFirst := MaxInt(iFirst, FirstValidIndex(sMACD));
  iFirst := MaxInt(iFirst, FirstValidIndex(sEMA1));
  iFirst := MaxInt(iFirst, FirstValidIndex(sEMA2));
  iFirst := MaxInt(iFirst, FirstValidIndex(sMA));
  valResult := 0;
  for i:=iFirst to Count-1 do
  begin
    BuyScore := 0;
    SellScore := 0;
    if sRSI[i] > nRSIHi then BuyScore := BuyScore+1 else
      if sRSI[i] < nRSILo then SellScore := SellScore-1;

    if sMACD1[i] > sMACD[i] then BuyScore := BuyScore+1 else
      if sMACD1[i] < sMACD[i] then SellScore := SellScore-1;

    if sEMA1[i] > sEMA2[i] then BuyScore := BuyScore+1 else
      if sEMA1[i] < sEMA2[i] then SellScore := SellScore-1;

    if (State = stSetupLong) and (Close[i] > LongTrigger) then
    begin
      valResult := 1;
      State := stNeutral;
    end else
    if (State = stSetupShort) and (Close[i] < ShortTrigger) then
    begin
      valResult := -1;
      State := stNeutral;
    end else
    if BuyScore >= 3 then
    begin
      if Close[i] > sMA[i] then
      begin
        if State = stSetupLong then
          LongTrigger := Min(LongTrigger, High[i])
        else
          LongTrigger := High[i];
        State := stSetupLong;
      end;
    end else
    if BuyScore <= 0 then
    begin
      if Close[i] < sMA[i] then
      begin
        if State = stSetupShort then
          ShortTrigger := Max(ShortTrigger, Low[i])
        else
          ShortTrigger := Low[i];
        State := stSetupShort;
      end;
    end else
    begin
      valResult := 0;
      State := stNeutral;
    end;
    Result[i] := valResult;
  end;
end;

type
  TState2 = (stNone, stLongTrigger, stShortTrigger);
  
var
  nHullMA1, nHullMA2, nMA1, nMA2, i, iFirst, Period, Displace, Trigger, MP: integer;
  TPLevel, TP: real;
  sHullMACD, sMACD, sHullDiff, sDiff, sZero: TSeries;
  sLR, sLRSlope, sForecast, sMAFc, sPetDev: TSeries;
  nRSI, nRSILo, nRSIHi, nMACD1, nMACD2, nMACD3, nEMA1, nEMA2, nMA: integer;
  bRSIExp: boolean;
  State: TState2;
  TriggerLevel: real;
begin
  nHullMA1 := CreateParameterInteger('Hull''s MA periode 1', 1, 999, 13, true);
  nHullMA2 := CreateParameterInteger('Hull''s MA periode 2', 1, 999, 26, true);
  nMA1 := CreateParameterInteger('MA periode 1', 1, 999, 13, true);
  nMA2 := CreateParameterInteger('MA periode 2', 1, 999, 26, true);
  Period   := CreateParameterInteger('T3 Period', 1, 999, 20, true);
  Displace := CreateParameterInteger('T3 Displace LRF', 0, 999, 0, false);
  Trigger  := CreateParameterInteger('T3 EMA over LRF', 1, 999, 5, true);
  TP  := CreateParameterReal('Take profit (pt)', 0, 9999, 10, true);

{ petdev }
  nRSI := CreateParameterInteger('RSI', 1, 999, 14, true);
  bRSIExp := CreateParameterBoolean('Exponentiele berekening', true, true);
  nRSILo := CreateParameterInteger('RSI low level', 0, 100, 50, true);
  nRSIHi := CreateParameterInteger('RSI high level', 0, 100, 50, true);
  nMACD1 := CreateParameterInteger('MACD 1', 1, 999, 12, true);
  nMACD2 := CreateParameterInteger('MACD 2', 1, 999, 26, true);
  nMACD3 := CreateParameterInteger('MACD 3', 1, 999, 9, true);
  nEMA1 := CreateParameterInteger('EMA 1', 1, 999, 5, true);
  nEMA2 := CreateParameterInteger('EMA 2', 1, 999, 20, true);
  nMA := CreateParameterInteger('MA', 1, 999, 30, true);

  with Indicator do
  begin
    RequiredBars := MaxInt(MaxInt(5*(nMA1+nMA2), nHullMA1+nHullMA2), 5*Period);
    RequiredBars := MaxInt(RequiredBars, 5*MaxInt(MaxInt(MaxInt(nRSI, nEMA1+nEMA2), nMACD1+nMACD2+nMACD3), nMA));
  end;
  
  sHullMACD := SubtractSeries(HullMA(Close, nHullMA1), HullMA(Close, nHullMA2));
  sHullDiff := Momentum(sHullMACD, 1);

  sMACD := MACD(Close, nMA1, nMA2);
  sDiff := Momentum(sMACD, 1);
  sZero := FillSeries(CreateSeries(BarCount), 0);

  sLR       := MovingRegression(Close,Period);
  sLRSlope  := MovingRegressionSlope(Close,Period);
  sForecast := ShiftSeries(AddSeries(sLR,sLRSlope),Displace);
  sMAFc     := MA(sForecast,MaExponential,Trigger);

  sPetDev := PetDev(Close, nRSI, bRSIExp, nRSILo, nRSIHi, nMACD1, nMACD2, nMACD3, nEMA1, nEMA2, nMA);

  iFirst := MaxInt(MaxInt(MaxInt(FirstvalidIndex(sHullDiff), FirstValidIndex(sDiff)), FirstValidIndex(sMAFc)), FirstValidIndex(sPetDev));
  for i:=iFirst+1 to BarCount-1 do
  begin
    if (MP > 0) and (Close[i] >= TPLevel) then
    begin
      ExitLong(i);
      MP := 0;
    end else
    if (MP < 0) and (Close[i] <= TPLevel) then
    begin
      ExitShort(i);
      MP := 0;
    end else
    if (State = stLongTrigger) and (Close[i] > TriggerLevel) then
    begin
      if EnterLong(i) then
      begin
        MP := 1;
        TPLevel := Close[i] + TP;
      end;
      State := stNone;
    end else
    if (State = stShortTrigger) and (Close[i] < TriggerLevel) then
    begin
      if EnterShort(i) then
      begin
        MP := -1;
        TPLevel := Close[i] - TP;
      end;
      State := stNone;
    end else
    if (MP <= 0) and (sHullDiff[i] > 0) and (sDiff[i] > 0) and (sDiff[i] > sDiff[i-1]) and (sMAFc[i] < sForecast[i]) and (sPetDev[i] > 0) then
    begin
      State := stLongTrigger;
      TriggerLevel := High[i];
    end else
    if (MP >= 0) and (sHullDiff[i] < 0) and (sDiff[i] < 0) and (sDiff[i] < sDiff[i-1]) and (sMAFc[i] >= sForecast[i]) and (sPetDev[i] < 0) then
    begin
      State := stShortTrigger;
      TriggerLevel := Low[i];
    end else
    if (sMAFc[i] >= sForecast[i]) then
    begin
      ExitLong(i);
      MP := 0;
    end else
    if (sMAFc[i] < sForecast[i]) then
    begin
      ExitShort(i);
      MP := 0;
    end;
  end;
  
  with CreateLine(sDiff) do
  begin
    Color := clLime;
  end;
  with CreateLine(sHullDiff) do
  begin
    Color := clYellow;
  end;
  with CreateLine(sZero) do
  begin
    Color := clSilver;
    LineContent := lcConstant;
  end;
  with CreateLine(sPetDev) do
  begin
    Name := 'PetDev';
    Color := clYellow;
    Visible := false;
  end;
end.
---
Eric

tesjoeva
Berichten: 84
Lid geworden op: vr jul 07, 2017 3:10 pm

Re: macd combo

Bericht door tesjoeva » ma apr 16, 2018 8:46 am

Hallo Eric,

Syntax error, script bevat fouten.

Kopieeren is dus helaas niet gelukt.

Wil je zo vriendelijk zijn....?

vr.gr.

Eric
Berichten: 2880
Lid geworden op: za sep 10, 2005 2:41 am
Locatie: Den Haag

Re: macd combo

Bericht door Eric » ma apr 16, 2018 9:43 am

Kennelijk iets misgegaan met kopieren en plakken, heb het hierboven verbeterd.

---
Eric

tesjoeva
Berichten: 84
Lid geworden op: vr jul 07, 2017 3:10 pm

Re: macd combo

Bericht door tesjoeva » ma apr 16, 2018 10:17 am

Hllo Eric,

Helemaal super, dank je wel.

vr.gr.

tesjoeva
Berichten: 84
Lid geworden op: vr jul 07, 2017 3:10 pm

Re: macd combo

Bericht door tesjoeva » ma apr 16, 2018 5:16 pm

Code: Selecteer alles

{- Filename: Coen's momentum -}

var
  i, Periode1, Periode2, Periode3: integer;
  Mom1, Mom2, Mom3, s0, s1, s2: TSeries;
begin
{ Parameters }
  Periode1 := CreateParameterInteger('Periode 1', 1, 999, 1, true);
  Periode2 := CreateParameterInteger('Periode 2', 1, 999, 5, true);
  Periode3 := CreateParameterInteger('Periode 3', 1, 999, 10, true);

{ Indicator eigenschappen }
  with Indicator do 
  begin
    RequiredBars := Periode1+Periode2+Periode3;    // Aantal benodigde koersen om eerste indicatorwaarde te berekenen
    NewBand := true;            // indicator standaard in nieuwe sectie plaatsen
    ScaleRange := srAuto;       // indicatorschaal automatisch
  end;

{ Berekening }
  Mom1 := Momentum(Close, Periode1);
  Mom2 := Momentum(Close, Periode2);
  Mom3 := Momentum(Close, Periode3);
  
  for i:=0 to BarCount-1 do
  begin
    if IsValid(Mom1[i]) and IsValid(Mom2[i]) then
    begin
      if (Mom1[i] > 0) and (Mom2[i] > 0) then
        SetBarColor(0, i, clGreen)
      else
      if (Mom1[i] < 0) and (Mom2[i] < 0) then
        SetBarColor(0, i, clRed)
      else
        SetBarColor(0, i, clYellow);
    end;

    if IsValid(Mom2[i]) and IsValid(Mom3[i]) then
    begin
      if (Mom2[i] > 0) and (Mom3[i] > 0) then
        SetBarColor(1, i, clGreen)
      else
      if (Mom2[i] < 0) and (Mom3[i] < 0) then
        SetBarColor(1, i, clRed)
      else
        SetBarColor(1, i, clYellow);
    end;
  end;
  
  s0 := FillSeries(CreateSeries(BarCount), 0);
  s1 := FillSeries(CreateSeries(BarCount), 1);
  s2 := FillSeries(CreateSeries(BarCount), 2);

{ Weergave }
  with CreateLineOHLC(s1, s1, s0, s0) do
  begin
    Name := 'Bar1';
    Color := clSilver;
    LineType := ltCandleStick;
  end;
  with CreateLineOHLC(s2, s2, s1, s1) do
  begin
    Name := 'Bar2';
    Color := clSilver;
    LineType := ltCandleStick;
  end;
end. [code/]

Hallo Eric,

Zou je het volgende voor me willen doen:

Long: Tesjoeva Macd system geeft koop EN Coen's Momentum is volledig groen.
Entry meteen de volgende bar, dus niet wachten op hoger slot dan de breakout bar EN
de Triggerlines (die je deze week eerder hebt gebruikt)zijn/is positief.

Exit/neutraal: Triggerlines gaan contrair; dus geen exits op Coen's kleurverandering of een 
tegengesteld signaal uit de Tesjoeva Macd systeem.

Short alles andersom, dat spreekt voor zich, sorry!

Wat je maakt is en blijft MAGIC, fantastisch!

vriendelijke groet.

Eric
Berichten: 2880
Lid geworden op: za sep 10, 2005 2:41 am
Locatie: Den Haag

Re: macd combo

Bericht door Eric » vr apr 20, 2018 7:39 pm

Ik miste de vraag omdat deze in het code-blok staat. Dat komt omdat je het codeblock afsluit met [code/ ] maar dat moet zijn [/code].

Code: Selecteer alles

{- Filename: Tesjoeva's MACD Momentum system -}

function HullMA(Prices: TSeries; Period: integer): TSeries;
begin
// Integer (Square Root (Period)) WMA [2 x Integer (Period/2) WMA (Price) - Period WMA (Price)]
//  waverage(2 * waverage(close, period/2) - waverage(close,period), SquareRoot(Period))
  Result := MA(SubtractSeries(MultiplySeriesBy(MA(Prices, maWeighted, Period div 2), 2),
                              MA(Prices, maWeighted, Period)),
               maWeighted, trunc(sqrt(Period)));
end;

var
  nHullMA1, nHullMA2, nMA1, nMA2, i, iFirst, Period, Displace, Trigger, Mom1, Mom2, Mom3, MP: integer;
  TPLevel, TP: real;
  sHullMACD, sMACD, sHullDiff, sDiff, sZero, sMom1, sMom2, sMom3: TSeries;
  sLR, sLRSlope, sForecast, sMAFc: TSeries;
begin
  nHullMA1 := CreateParameterInteger('Hull''s MA periode 1', 1, 999, 13, true);
  nHullMA2 := CreateParameterInteger('Hull''s MA periode 2', 1, 999, 26, true);
  nMA1 := CreateParameterInteger('MA periode 1', 1, 999, 13, true);
  nMA2 := CreateParameterInteger('MA periode 2', 1, 999, 26, true);
  Period   := CreateParameterInteger('T3 Period', 1, 999, 20, true);
  Displace := CreateParameterInteger('T3 Displace LRF', 0, 999, 0, false);
  Trigger  := CreateParameterInteger('T3 EMA over LRF', 1, 999, 5, true);
  TP  := CreateParameterReal('Take profit (pt)', 0, 9999, 10, true);
  Mom1 := CreateParameterInteger('Mom 1', 1, 999, 1, true);
  Mom2 := CreateParameterInteger('Mom 2', 1, 999, 5, true);
  Mom3 := CreateParameterInteger('Mom 3', 1, 999, 10, true);

  with Indicator do
  begin
    RequiredBars := MaxInt(MaxInt(5*(nMA1+nMA2), nHullMA1+nHullMA2), 5*Period)+Mom1+Mom2+MOm3;
  end;
  
  sHullMACD := SubtractSeries(HullMA(Close, nHullMA1), HullMA(Close, nHullMA2));
  sHullDiff := Momentum(sHullMACD, 1);

  sMACD := MACD(Close, nMA1, nMA2);
  sDiff := Momentum(sMACD, 1);
  sZero := FillSeries(CreateSeries(BarCount), 0);

  sLR       := MovingRegression(Close,Period);
  sLRSlope  := MovingRegressionSlope(Close,Period);
  sForecast := ShiftSeries(AddSeries(sLR,sLRSlope),Displace);
  sMAFc     := MA(sForecast,MaExponential,Trigger);

  sMom1 := Momentum(Close, Mom1);
  sMom2 := Momentum(Close, Mom2);
  sMom3 := Momentum(Close, Mom3);

  iFirst := MaxInt(MaxInt(MaxInt(MaxInt(MaxInt(FirstvalidIndex(sHullDiff), FirstValidIndex(sDiff)),
    FirstValidIndex(sMAFc)), FirstValidIndex(sMom1)), FirstValidIndex(sMom2)), FirstValidIndex(sMom3));
  for i:=iFirst+1 to BarCount-1 do
  begin
    if (MP > 0) and (Close[i] >= TPLevel) then
    begin
      ExitLong(i);
      MP := 0;
    end else
    if (MP < 0) and (Close[i] <= TPLevel) then
    begin
      ExitShort(i);
      MP := 0;
    end else
    if (sHullDiff[i] > 0) and (sDiff[i] > 0) and (sDiff[i] > sDiff[i-1]) and (sMAFc[i] < sForecast[i]) and
       (sMom1[i] > 0) and (sMom2[i] > 0) and (sMom3[i] > 0) then
    begin
      if EnterLong(i) then
      begin
        MP := 1;
        TPLevel := Close[i] + TP;
      end;
    end else
    if (sHullDiff[i] < 0) and (sDiff[i] < 0) and (sDiff[i] < sDiff[i-1]) and (sMAFc[i] >= sForecast[i]) and
       (sMom1[i] < 0) and (sMom2[i] < 0) and (sMom3[i] < 0) then
    begin
      if EnterShort(i) then
      begin
        MP := -1;
        TPLevel := Close[i] - TP;
      end;
    end else
    if (sMAFc[i] >= sForecast[i]) then
    begin
      ExitLong(i);
      MP := 0;
    end else
    if (sMAFc[i] < sForecast[i]) then
    begin
      ExitShort(i);
      MP := 0;
    end;
  end;
  
  with CreateLine(sDiff) do
  begin
    Color := clLime;
  end;
  with CreateLine(sHullDiff) do
  begin
    Color := clYellow;
  end;
  with CreateLine(sZero) do
  begin
    Color := clSilver;
    LineContent := lcConstant;
  end;
end.
---
Eric

tesjoeva
Berichten: 84
Lid geworden op: vr jul 07, 2017 3:10 pm

Re: macd combo

Bericht door tesjoeva » za apr 21, 2018 11:56 am

Hallo Eric,

Hartelijk dank voor de combo; ik zal het in het vervolg goed aanleveren.

Goed weekend en groet,

Plaats reactie