can SAR() be more fully implemented?

Posted By: dusktrader

can SAR() be more fully implemented? - 03/27/14 09:54

I am working with SAR() and I noticed the manual says "Provisionally implemented; uses the current asset price series and supports a single asset only"

Is it difficult to adjust SAR() to work in a multi-asset loop? Otherwise, do you know of a workaround or similar indicator?

Thanks
Posted By: jcl

Re: can SAR() be more fully implemented? - 03/27/14 15:37

It can not be adjusted because it's sloppy implemented in the TA-Lib, using a static variable. Thus it can never support more than one asset. But of course, the SAR can be rewritten in lite-C and included in the indicators.c library. Maybe a user can do this?
Posted By: swingtraderkk

Re: can SAR() be more fully implemented? - 03/28/14 12:50

is this any better?

Code:
var PSAR(var AF, var Max)
{

static var	psar0;
static var	psar1;
static int	lng;
static var	af;
static var	ep;
static var	hp;
static var	lp;

if (is(INITRUN))
	{
		lng		= 1;						//assume long for initial conditions
		af 		= AF;						//init acelleration factor
		psar0		= 0;
		psar1		= 0;
		ep			= 0;
		hp			= 0;
		lp			= 0;
		
		return;								// do not run on bar 0
	}

if (Bar == 1)
	{
		psar0		= priceClose(0);		// initialise
		psar1		= priceClose(0);
		ep 		= priceHigh(0);		// init extreme point as assuming long
		hp 		= priceHigh(0);
		lp 		= priceLow(0);
		
		return psar0;	
	}

if (lng == 1)	psar0 = psar1 + af * (hp - psar1);
else				psar0 = psar1 + af * (lp - psar1);
  
int reverse =  0;     
     
if (lng == 1)								//check for reversal
     {
         if (priceLow(0) < psar0)
         	{
            	lng		= 0;
            	reverse	= 1;			//reverse position to short
            	psar0		= hp;			
            	lp			= priceLow(0);
            	af			= AF;
         	}
     }
else
     {
         if (priceHigh(0) > psar0)
         	{
            	lng		= 1;
            	reverse	= 1;        //reverse position to long
            	psar0		= lp;
            	hp			= priceHigh(0);
            	af			= AF;
         	}
     }

if (reverse == 0)
     {
         if (lng == 1)
         {
         	if (priceHigh(0) > hp)
            	{
             		hp  = priceHigh(0);
             		af += AF;
             		af  = min(af,Max); 
					}
            psar0 = min(psar0, min(priceLow(1),priceLow(2)));
         }
         else
         {
         	if (priceLow(0) < lp)
            	{
             		lp  = priceLow(0);
             		af += AF;
             		af  = min(af,Max); 
					}
            psar0 = max(psar0,max(priceHigh(1),priceHigh(2)));
         }
     	}
psar1	= psar0;
     	
return psar0;

}

Posted By: dusktrader

Re: can SAR() be more fully implemented? - 03/28/14 16:08

I'm thinking not, though I haven't had time to play with this yet. The issue, as I understand, is that the static variables need to be linked to the asset. When you use static vars inside the function, that would imply every time the function is called, it refers back to the same static history (regardless of the asset -- which is a problem because every iteration of Zorro would be flipping through multiple assets)

So to fix it, I think the function either needs to somehow be re-written not to use static vars (if even possible), or use static storage that is linked to the asset. AlgoVar is one potential option (those are statics linked to each asset) but since they are in limited supply (only 8 i think)... then it doesn't seem ideal.

Thanks for posting this code. It's a good starting point. Perhaps I can play around with this and see if I can come up with anything.
Posted By: jcl

Re: can SAR() be more fully implemented? - 03/28/14 16:43

You can either use AlgoVar, or a series. Both are asset linked. As far as I can see from the above code, most of those variables need not be static anyway.
Posted By: mustang

Re: can SAR() be more fully implemented? - 10/17/14 03:52

Thank you everyone for your suggestions and effort.
Zorro team, any news on the PSAR?
I think it's very important for any serious trading platform to have a proper implementation of the Parabolic SAR.

Alternatively, is there a way to use the PSAR from MT4 through the bridge if Zorro does not make it available natively?
Posted By: jcl

Re: can SAR() be more fully implemented? - 10/20/14 17:27

It's certainly easier to write a PSAR in lite-C than using the MT4 bridge for it. But for us there was no reason for implementing a PSAR in lite-C so far. We have not yet seen that it could be used for a profitable system.
Posted By: mustang

Re: can SAR() be more fully implemented? - 10/20/14 18:03

A succesfull trader wrote in a book that amongst his methods, was the use of conventional indicators in a non-conventional way.

Could we make the request that Zorro team implements PSAR in Lite-C and let traders use it the they want?
Posted By: jcl

Re: can SAR() be more fully implemented? - 10/20/14 18:20

Sure. Use the ta-lib SAR, do a profitable system with it, and we'll implement at once a lite-C SAR. If an indicator can be shown to be useful in any way, you'll normally get it.
© 2024 lite-C Forums