Hi All,
I use loop to execute my exit strategy which in fact heavily copying the idea of PSAR, except....
- use the BB channel to set the starting point of each parabolic line(BB bottom for buy signal, while BB top for short signal....)
- accelerate only when zscore of this bar is higher than previous bar.
Attached below is my full version of codes. It works ok, but it takes a very long time to optimize as no surprise while loop is being used. I want to use array or existing built-in function to achieve the same goal, but the problem I am having is there is no way, by far I know, to set the starting point of the current SAR() function for each signal, and also no way to make it only accelerate when the zscore ceritieria is met.
I think it is not the limitation of the software but only I am not clever enough. Really hope some people here could give me some hints......
English is not my first language but hope I could still manage to make my questions clear....thanks!!
Buy1 = cross( close, ema( close, 45 ) );
Short1 = cross( ema( close, 45 ), close );
exitBBtop = BBandTop( O, 100);
exitBBbot = BBandBot( O, 100);
zscore = ( Close - MA( Close, 100 ) ) / StDev( Close, 100 );
// use the BB channel to set the starting point of each parabolic line
// accelerate only when zscore of this bar is higher than periouvs bar
longpos = 0; // these two variable serves as a flag, to tell inside the loop what is the current position,
shortpos = 0;
IAF = 0.02;//
for( i = 2; i < BarCount; i++ )
{
///////////////////////////////D2.) Part one - postion entry///////////////////////////////////
// Check for new long position
if ( (longpos == 0) AND Buy1[i] AND shortpos == 0) // entry when not having a Long position, and when Buy1 is true
{
Buy[i] = 1; // Buy signal generate
Psar[i] = exitBBbot[i]; // use BB channel bottom to set the starting point of PSAR
zscoretemp = zscore[i]; // record down the zscore of the entry bar.
Hp = High[i]; //record down the entry high
af = IAF; //reset the af
longpos = 1; // set the flag to be true
}
// Check for new short position
if ( (shortpos == 0) AND Short1[i] AND longpos == 0)// entry when not having a short position, and when Short1 is true
{
Short[i] = 1; // Short signal generate
Psar[i] = exitBBtop[i]; // use another BB channel top to set the starting point of PSAR
zscoretemp = zscore[i];// the zscore of the entry bar
Lp = Low[i]; //record down the entry low
af = IAF; //reset the af
shortpos = 1; // set the flag to be true
}
///////////////////////////////A.) Part two - postion exit///////////////////////////////////
// Check for exit from long position
if ((longpos == 1) AND (Low[i] < Psar[i-1])) // use the PSAR as an exit condition
{
Sell[i] = 1; // generate the sell signal
SellPrice[i] = Psar[i-1];
longpos = 0; // reset the flag, to tell the loop it is under zero position.
}
// Check for exit from short position
if ((shortpos == 1) AND (High[i] > Psar[i-1])) // use the PSAR as an exit condition
{
Cover[i] = 1; // generate the cover signal
coverPrice[i] = Psar[i-1];
shortpos = 0;//reset the flag
}
///////////////////////////////B.) Part three - postion continuation///////////////////////////////////
// Continuation of long position -- adjust stop
if ( longpos == 1 )
{
if (High[i] > hp) // under the long position, to reset the hp if we have a higher high, for the calucation of next PSAR.
{
hp = High[i];
}
if (zscore[i] > zscoretemp) // zscoretemp is directly from your idea, to record down the zscore when enter the trade, and compare it on every bar
{
zscoretemp = zscore[i];
af = af + IAF; // to accelerate only when this bar has higher zscore then the entry bar
if (af > MaxAF) af = MaxAF;
}
psar [ i ] = psar[i-1] + af * ( hp - psar[i-1] ); //PSAR calculation
}
// Continuation of Short position -- adjust stop
if ( shortpos == 1 )
{
if (Low[i] < lp) // under the short position, to reset the lp if we have a lower low, for the calucation of next PSAR.
{
lp = Low[i];
}
if (zscore[i] > zscoretemp) // to compare the zscore of this bar againest the zscore of the entry bar
{
zscoretemp = zscore[i];
af = af + IAF; // to accelerate only when this bar has higher zscore then the entry bar
if (af > MaxAF) af = MaxAF;
}
psar [ i ] = psar[i-1] + af * ( lp - psar[i-1]); //PSAR calculation
}
///////////////////////////////C.) Part four - take care the variable when not holding position///////////////////////////////////
if (longpos == 0 AND shortpos == 0)
{
psar[i] = Null;
}
}