Technical Analysis - Parabolic Stop and Reverse Indicator
Asked Answered
T

2

6

I'm trying to build a class to create SAR serie. But it seems I don't understand the steps well. I'm not sure about the initial values for the calculation.

Here is my first attempt:

public class SAR : IndicatorCalculatorBase
{
    public override List<Ohlc> OhlcList { get; set; }
    public double AccelerationFactor = 0.02;
    public double MaximumAccelerationFactor = 0.2;

    public override IIndicatorSerie Calculate()
    {
        SingleDoubleSerie sarSerie = new SingleDoubleSerie();

        bool trendUp = false, trendChanged = false;
        double instantFactor = AccelerationFactor;

        for (int i = 1; i < OhlcList.Count; i++)
        {
            if (OhlcList[i].Low > OhlcList[i - 1].Low) // uptrend
            {   
                if (!trendUp)
                {
                    trendUp = true;
                    trendChanged = true;
                    instantFactor = AccelerationFactor;
                }
                else
                {
                    if (instantFactor + AccelerationFactor <= MaximumAccelerationFactor)
                    {
                        instantFactor += AccelerationFactor;
                    }
                }
            }
            else if (OhlcList[i].Low < OhlcList[i - 1].Low) // downtrend
            {
                if (trendUp)
                {
                    trendUp = false;
                    trendChanged = true;
                    instantFactor = AccelerationFactor;
                }
                else
                {
                    if (instantFactor + AccelerationFactor <= MaximumAccelerationFactor)
                    {
                        instantFactor += AccelerationFactor;
                    }
                }
            }
            else
            {
                // ?
            }
        }

        return sarSerie;
    }
}

Is there anyone else tried to do that before?

Here is the csv file I use:

https://drive.google.com/file/d/0Bwv_-8Q17wGaRDVCa2FhMWlyRUk/view

Using the SAR function in TTR package for R:

table <- read.csv("table.csv", header = TRUE, sep = ",")
sar <- SAR(table[,c("High","Low")])
sar

I get the following results:

[1] 160.03441 160.17173 160.30629 160.43817 160.71224 161.26890 162.04099
[8] 162.75131 170.92000 170.92000 170.54080 170.17677 169.62316 168.76051
[15] 167.58446 166.21112 165.00259 163.93908 163.00319 156.14000 156.14000
[22] 156.60440 157.34394 158.03910 158.69255 159.54515 160.32954 161.05117
[29] 161.71508 162.62857 163.70634 171.61000 171.61000 171.16840 170.74446
[36] 170.11000 169.51360 168.95298 167.99674 166.85007 165.58046 164.46320
[43] 163.48002 162.27322 160.96710 159.14602 157.44882 156.69000 156.69000
[50] 155.45000 154.37200 153.53116 152.69088 152.33000 150.57720 148.81404
[57] 148.50000 147.15600 146.69000 142.54000 142.54000 142.77480 143.00021
[64] 143.45320 144.34254 145.16074 145.91348 146.60600 147.24312 147.82927
[71] 148.51634 154.70000 154.70000 154.32640 153.71582 152.72495 151.29746
[78] 149.67776 147.73347 146.06139 145.16000 135.79000 135.79000 136.17800
[85] 136.91732 137.88153 139.05938 140.36506 141.51405 142.52516 143.63464
[92] 144.74750 146.05295 147.79636 149.64516 151.08722 152.31909 157.25000
[99] 157.25000 157.01640 156.64342 155.92234 155.00811 154.18530 153.10826
[106] 151.91291 150.97000 150.97000 149.73000 141.55000 152.00000 152.00000
[113] 151.42600 150.87496 149.98426 148.75272 147.23345 145.41384 143.40570
[120] 141.21119 139.10317 137.37460 129.50000 129.50000 129.95600 130.39376
[127] 130.81401 131.21745 131.60475 131.97656 132.33350 132.67616 133.00511
[134] 140.90000 140.90000 140.51600 140.14736 131.30000 131.30000 131.88280
[141] 132.44229 132.97940 133.78463 134.54155 135.25306 146.40000 146.40000
[148] 145.91200 145.44352 144.99378 144.56203 144.30000 144.30000 134.20000
[155] 134.20000 134.77000 135.31720 136.19217 137.01464 137.78776 138.86234
[162] 140.21110 141.42499 142.75400 145.31544 147.51827 149.41272 151.04194
[169] 152.44307 153.97217 155.62238 166.71000 155.00000 155.00000 156.25640
[176] 157.46254 158.62044 159.73202 160.79914 161.82358 162.80703 163.75115
[183] 164.65751 165.52761 166.36290 167.16479 167.93460 168.67361 169.38307
[190] 170.06414 170.71798 171.34566 171.94823 172.52670 173.08204 186.41000
[197] 186.41000 185.76880 185.15325 184.17625 182.88215 181.33994 179.37915
[204] 176.40146 173.33403 170.37770 167.53616 164.83541 162.31171 160.39370
[211] 160.21000 158.79640 157.72206 154.32000 154.32000 158.19000 158.19000
[218] 157.90800 157.63728 157.15804 156.70756 156.08056 148.87000 148.87000
[225] 149.21000 159.94000 159.94000 159.45480 158.98901 158.54185 147.81000
[232] 147.81000 148.73760 149.62810 150.48297 151.30365 152.09151 152.84785
[239] 153.57393 154.86310 156.66845 158.87660 160.86394 162.65255 164.26229
[246] 178.75000 178.75000 178.12840 177.53166 176.95880 176.40885 175.88089
[253] 175.37406 174.46361 173.60780 172.80333 172.04713 171.33630 170.66812
[260] 170.04004 169.44963 168.89466 168.37298 160.20000 160.20000 160.55840
[267] 161.20470 161.81221 162.38328 163.16062 163.87577 164.85119 165.72907
[274] 166.98558 168.45060 170.01851 171.70358 173.08533 174.75427 175.32000
[281] 181.43000 181.43000 181.01280 180.30463 179.40186 178.33568 177.62000
[288] 165.30000 165.30000 165.83120 166.68733 167.94834 169.64251 171.16726
[295] 172.88719 174.93998 176.70538 178.45652 180.68915 183.27132 185.10000
[302] 186.80000 187.61000 193.60000 193.60000 193.24000 192.47860 191.12831
[309] 189.88605 188.74316 187.69171 186.72437 185.83442 175.60000 175.60000
[316] 176.09600 176.57216 177.02927 188.00000 188.00000 187.49840 186.63810
[323] 185.82941 184.57346 182.83211 180.69826 177.99090 175.04036 171.99829
[330] 169.50380 167.45832 166.25000 158.14000 158.14000 158.47080 158.78837
[337] 159.09323 166.41000 166.41000 166.07360 165.54118 165.49000 165.49000
[344] 164.99260 164.52504 157.20000 157.20000 157.74960 158.27722 158.78373
[351] 159.26998 159.73678 170.94000 170.94000 170.41840 169.91766 169.43696
[358] 168.97548 168.27735 167.37676 166.54822 165.44540 164.04915 162.17047
[365] 159.91599 157.24012 154.03209 151.14523 147.32558 146.97000 146.46000
[372] 135.23000 135.23000 135.81280 136.37229 136.90940 137.42502 137.60000
[379] 149.80000 149.80000 149.28840 148.79726 148.32577 147.87314 147.43862
[386] 137.01000 137.01000 137.52920 138.32485 139.07276 140.04054 140.74000
[393] 140.74000 141.57440 151.17000 151.17000 150.69480 150.23861 149.30089
[400] 148.41944 147.25948 146.19232 145.21054 144.30730 143.09657 141.39778
[407] 139.53049 138.96000 128.06000 128.06000 128.60000 129.11840 141.56000
[414] 141.56000 140.94400 139.99816 138.52631 136.65368 134.57604 132.74771
[421] 130.71243 128.65284 125.90373 123.64946 113.38000 113.38000 113.57000
[428] 114.21020 115.57498 131.27000 131.27000 130.51920 129.79843 129.10649
[435] 128.44223 126.97210 125.01273 122.55746 120.34771 118.35894 116.56905
[442] 115.65000 114.13100 111.84728 109.83761 108.06909 105.89642 104.02792
[449] 102.42101 100.46245  97.78361  93.64689  89.05617  85.70000  72.78000
[456]  72.78000  73.42240  74.42966  75.94568  77.75112  79.37600  79.79000
[463]  94.00000  94.00000  93.41080  92.84517  79.27000  79.27000  80.01840
[470]  80.73686  81.42659  82.46059  83.43256  83.45000  98.66000  98.66000
[477]  97.85640  97.08494  96.34435  95.63337  94.38157  92.65264  90.09938
[484]  87.15105  83.80491  79.85692  76.54061  59.13000  59.13000  59.92000
[491]  61.35040  63.39197  65.27021  67.52819  70.48881  73.09415  75.38685
[498]  76.31000  92.20000  92.20000  91.51320  90.85387  90.22092  89.30106
[505]  88.43640  87.62361  86.54493  84.74443  82.38830  80.31490  78.49032
[512]  77.87000  65.11000  78.53000  78.53000  77.98960  77.03742  75.80000
[519]  60.22000  60.22000  61.05880  81.19000  81.19000  79.83880  78.54165
[526]  77.29638  76.10093  74.95329  73.85156  73.64000  47.41000  47.41000
[533]  48.68240  50.67246  53.72266  57.82139  62.28283  66.89823  70.86748
[540]  75.72868  80.58532  82.22000  85.90820  89.72656  95.50672 101.43070
[547] 107.19992 123.80000 123.80000 123.00800 104.00000 128.00000 128.00000
[554] 125.84000 123.76640  74.00000  74.00000  76.14040  80.09198  83.80646
[561]  87.29807  90.58019  93.66537  96.56545  99.29153 101.85403 104.26279
[568] 106.52702 144.98000 144.98000 142.63320  86.31000  86.31000  89.32840
[575]  92.22606  95.00782  99.41015 105.25334 110.62907 115.57475 120.12477
[582] 124.31079 128.16192 131.70497 134.96457 137.96341 140.72233 143.26055
[589] 145.59570 147.74405 149.72052 151.53888 153.21177 154.75083 156.16676
[596] 157.89809 160.93432 163.60620 165.95745 168.02656 169.84737 171.44969
[603] 173.48673 175.50005 186.83000 186.83000 186.24600 172.23000 172.23000
[610] 172.94240 173.62630 174.28285 174.85000 190.04000 190.04000 188.52840
[617] 187.07726 185.68417 184.34681 183.06293 181.91000 152.25000 152.25000
[624] 153.46560 154.63258 155.75287 156.82836 157.86082 159.60977 161.25379
[631] 162.79916 164.25181 165.61730 166.90086 168.10741 169.24157 187.01000
[638] 187.01000 185.97800 184.98728 184.03619 183.12314 182.24662 181.40515
[645] 180.59734 179.82185 179.50000 179.50000 178.76840 178.06606 161.21000
[652] 161.21000 162.10800 163.47552 165.54308 167.94977 170.57180 173.64035
[659] 176.73789 179.33983 181.52546 183.36138 186.07553 188.71043 191.44053
[666] 203.39000 203.39000 202.84720 201.97137 201.14809 200.37420 198.69306
[673] 196.62376 194.76138 193.08524 191.14421 187.77802 184.27914 180.21130
[680] 176.87566 174.14044 161.68000 161.68000 162.47280 163.62943 164.71667
[687] 165.73867 166.69935 181.75000 181.75000 180.99400 180.26824 162.85000
[694] 162.85000 163.71680 164.54893 184.52000 184.52000 182.75000 181.05080
[701] 179.41957 177.85359 176.35024 174.90703 173.52155 172.19149 170.91463
[708] 169.68884 140.27000 140.27000 141.87040 144.23498 146.45768 148.54702
[715] 150.51100 152.35714 154.09251 156.27471 158.28233 160.12934 162.28941
[722] 165.16468 168.64163 172.16137 176.05092 179.24035 182.85428 188.38634
[729] 193.28082 193.40000 193.40000 208.78000 208.78000 208.02520 207.30059
[736] 205.93796 203.49652 201.25040 175.42000 175.42000 176.47440 178.01914
[743] 179.47119 180.83612 182.11915 183.32520 184.45889 186.02218 188.32396
[750] 191.53108 195.06733 198.10851 201.25915 203.90568 205.02000 217.80000
[757] 217.80000 217.11360 215.90078 215.20000 215.20000 196.90000 196.90000
[764] 198.12200 199.29512 200.42132 201.50246 202.54036 203.53675 205.08554
[771] 207.41630 209.56060 234.22000 234.22000 233.06800 231.96208 230.90040
[778] 229.88118 229.57000 205.42000 205.42000 206.80520 208.13499 240.05000
[785] 240.05000 238.51080 237.03317 235.61464 201.57000 201.57000 203.28360
[792] 206.12858 208.80287 211.31670 213.67970 215.80000 215.80000 250.70000
[799] 250.70000 249.19200 247.74432 246.35455 245.02037 243.73955 242.50997
[806] 241.32957 240.19639 239.70000 238.63200 237.60672 236.62245 235.67755
[813] 234.77045 233.89963 233.06365 232.26110 230.97344 229.20036 227.05132
[820] 224.22877 221.31834 218.38740 212.81147 207.51718 202.69320 198.13963
[827] 193.86533 190.70234 187.28569 183.59998 181.01999 175.00000 183.70000
[834] 183.70000 183.14360 182.23018 181.37157 180.56448 167.92000 167.92000
[841] 168.43840 168.93606 169.41382 180.88000 180.88000 179.94000 157.38000
[848] 157.38000 158.57480 160.49931 163.53937 166.33622 168.90932 171.27657
[855] 173.45445 175.45809 177.81128 179.92916 181.83524 184.00181 186.76656
[862] 189.61351 192.90148 196.71518 201.28784 205.42356 209.76463 213.59654
[869] 216.35551 223.45000 223.45000 214.65000 214.65000 215.09480 215.52181
[876] 215.93174 225.77000 225.77000 225.28680 224.52319 223.80540 212.56000
[883] 212.56000 213.13360 214.16538 215.13526 216.04695 216.90393 217.70949
[890] 218.98393 220.15642 233.64000 233.64000 233.04440 232.47262 231.92372
[897] 218.75000 218.75000 219.35880 219.94325 220.50432 221.04295 221.56003
[904] 222.05643 222.53297 222.99045 223.42963 223.85125 224.25600 224.26000
[911] 233.97000 233.97000 233.52920 233.10603 232.69979 232.30980 231.65701
[918] 230.60845 229.29261 227.61949 226.14715 215.35000 215.35000 215.80040
[925] 216.23278 216.64787 226.61000 226.61000 226.10440 225.35054 223.97209
[932] 222.13588 220.12638 218.35801 216.80185 215.43243 214.22734 213.16686
[939] 212.23363 210.98153 209.90471 208.97865 203.29000 203.29000 203.64720
[946] 203.99011 204.16000 212.22000 212.22000 211.74880 210.94007 210.17987
[953] 209.16868 208.56000 207.67840 206.86733 206.12114 205.05903 203.69594
[960] 201.75751 189.85000 189.85000 190.41000 191.60940 193.75025 196.35522
[967] 199.52260 202.30988 204.76270 206.92117 208.82063 210.47000 222.75000
[974] 222.75000 222.23480 221.48931 220.78855 220.12984 219.51065 218.92861
[981] 218.19272 217.10245 216.12120 215.23808 207.29000 207.29000 207.81880
[988] 208.32645 208.81379 208.84000 209.10000 209.55640 209.99454 220.51000
[995] 220.51000 219.74960 219.01962 217.76444 216.17528 214.71326

Here is the source code that does the calculation:

https://github.com/joshuaulrich/TTR/blob/master/R/SAR.R https://github.com/joshuaulrich/TTR/blob/master/src/sar.c

Anyone can help me translating the calculation to C#?

Thanks in advance,

Terisateriyaki answered 28/4, 2015 at 14:8 Comment(2)
you could call the R code from c# using r.netHanuman
@Hanuman I want to implement the calculation in c# to learn the algorithm.Terisateriyaki
T
7

Here is my translation:

public class SAR : IndicatorCalculatorBase
{
    protected override List<Ohlc> OhlcList { get; set; }
    public double AccelerationFactor = 0.02;
    public double MaximumAccelerationFactor = 0.2;

    public SAR()
    { 

    }

    public SAR(double accelerationFactor, double maximumAccelerationFactor)
    {
        this.AccelerationFactor = accelerationFactor;
        this.MaximumAccelerationFactor = maximumAccelerationFactor;
    }

    public override IIndicatorSerie Calculate()
    {
        SingleDoubleSerie sarSerie = new SingleDoubleSerie();

        // Difference of High and Low
        List<double> differences = new List<double>();
        for (int i = 0; i < OhlcList.Count; i++)
        {
            double difference = OhlcList[i].High - OhlcList[i].Low;
            differences.Add(difference);
        }

        // STDEV of differences
        double stDev = Statistics.StandardDeviation(differences);

        double?[] sarArr = new double?[OhlcList.Count];

        double[] highList = OhlcList.Select(x => x.High).ToArray();
        double[] lowList = OhlcList.Select(x => x.Low).ToArray();

        /* Find first non-NA value */
        int beg = 1;
        for (int i = 0; i < OhlcList.Count; i++)
        {
            if (OhlcList[i].High == 0 || OhlcList[i].Low == 0)
            {
                sarArr[i] = 0;
                beg++;
            }
            else
            {
                break;
            }
        }

        /* Initialize values needed by the routine */
        int sig0 = 1, sig1 = 0;
        double xpt0 = highList[beg - 1], xpt1 = 0;
        double af0 = AccelerationFactor, af1 = 0;
        double lmin, lmax;
        sarArr[beg - 1] = lowList[beg - 1] - stDev;

        for (int i = beg; i < OhlcList.Count; i++)
        {
            /* Increment signal, extreme point, and acceleration factor */
            sig1 = sig0;
            xpt1 = xpt0;
            af1 = af0;

            /* Local extrema */
            lmin = (lowList[i - 1] > lowList[i]) ? lowList[i] : lowList[i - 1];
            lmax = (highList[i - 1] > highList[i]) ? highList[i - 1] : highList[i];
            /* Create signal and extreme price vectors */
            if (sig1 == 1)
            {  /* Previous buy signal */
                sig0 = (lowList[i] > sarArr[i - 1]) ? 1 : -1;  /* New signal */
                xpt0 = (lmax > xpt1) ? lmax : xpt1;             /* New extreme price */
            }
            else
            {           /* Previous sell signal */
                sig0 = (highList[i] < sarArr[i - 1]) ? -1 : 1;  /* New signal */
                xpt0 = (lmin > xpt1) ? xpt1 : lmin;             /* New extreme price */
            }

            /*
                * Calculate acceleration factor (af)
                * and stop-and-reverse (sar) vector
            */

            /* No signal change */
            if (sig0 == sig1)
            {
                /* Initial calculations */
                sarArr[i] = sarArr[i - 1] + (xpt1 - sarArr[i - 1]) * af1;
                af0 = (af1 == MaximumAccelerationFactor) ? MaximumAccelerationFactor : (AccelerationFactor + af1);
                /* Current buy signal */
                if (sig0 == 1)
                {
                    af0 = (xpt0 > xpt1) ? af0 : af1;  /* Update acceleration factor */
                    sarArr[i] = (sarArr[i] > lmin) ? lmin : sarArr[i];  /* Determine sar value */
                }
                /* Current sell signal */
                else
                {
                    af0 = (xpt0 < xpt1) ? af0 : af1;  /* Update acceleration factor */
                    sarArr[i] = (sarArr[i] > lmax) ? sarArr[i] : lmax;   /* Determine sar value */
                }
            }
            else /* New signal */
            {
                af0 = AccelerationFactor;    /* reset acceleration factor */
                sarArr[i] = xpt0;  /* set sar value */
            }
        }

        sarSerie.Values = sarArr.ToList();

        return sarSerie;
    }
}
Terisateriyaki answered 5/5, 2015 at 10:39 Comment(0)
C
2

I can't vouch for the accuracy of the implementation as I've not run it but try http://www.smartquant.com/forums/viewtopic.php?t=7498

Carburet answered 30/4, 2015 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.