Australian (ASX) Stock Market Forum

Amibroker - how to identify a week of falling close prices?

Joined
20 February 2014
Posts
180
Reactions
0
Hi All,

I try to create a stop loss - if XAO records three consecutive weeks of falling prices (XAO on Friday < XAO on Monday for the last three weeks), then I want to stop trading until there is at least one week of raising prices. I don't know if this will give good results, but at least I want to learn how to do it and run some simulations.

Thanks,
Nick
 
Try this. Not exactly what you want but worth a look. Close is based on end of week close.

Code:
SetOption("InitialEquity", 100000);
PosQty = 4; 
SetOption("MaxOpenPositions", PosQty );
PositionSize = -100/PosQty; 
SetOption ("UsePrevBarEquityForPosSizing", True);
PositionScore = MACD() > 0;
SetTradeDelays(1, 1, 1, 1);

TurnOver = MA(C*V, 50) > 250000; 

//// Index close is 3 consecutive weeks lower \\\\

SetForeign("^AORD");
ThisWeek = TimeFrameGetPrice("C", inWeekly, 0); 
LastWeek = TimeFrameGetPrice("C", inWeekly, -1);
TwoWeeksAgo = TimeFrameGetPrice("C", inWeekly, -2);
IndexDownWeekly = ThisWeek < LastWeek & LastWeek < TwoWeeksAgo;
RestorePriceArrays(); 


Buy =  C >= HHV(C, 26) & TurnOver; // Buy if a stock price closes above the 26 bar highest closing price 
Sell = IndexDownWeekly; // Sell if All Ordinaries Index weekly close is less 3 times consecutively
 
OK, here is the one that should come close in regards to counting consecutive weeks with falling prices.

Code:
// three consecutive weeks of falling prices
// for NickF at aussiestockforums.com, 2014
// https://www.aussiestockforums.com/forums/newreply.php?do=postreply&t=28392

dow = DayOfWeek();
Fri = dow == 5;
Mon = dow == 1;
FriC = Valuewhen( Fri, C );
MonC = Valuewhen( Mon, C );
Cond = FriC < MonC AND Fri;

friday = Null;
myvar = Null;
count = 3;
n = 0;
 
for ( i = 1; i < BarCount; i++ )
{
    if ( Fri[i] )
    { 
        friday[n] = Cond[i];
        n++;
    } 
 
    if  ( n > count /*&& Fri[i]*/ )
    {
        cumfridays = 0;
 
        for ( j = n - count; j < n; j++ )
        {           
            cumfridays += friday[j];
        }
        myvar[i] = cumfridays;
    }
}

// Plotting :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function PlotOwnScale( input, chartName, color, minPerc, maxPerc )
{// function by Marcin G.
    minVal = LowestVisibleValue( input );
    maxVal = HighestVisibleValue( input );

    onePerc = ( maxVal - minVal ) / ( maxPerc - minPerc );
    minScale = minVal - minPerc * onePerc;
    maxScale = maxVal + ( 100 - maxPerc ) * onePerc;

    Plot( input, chartName, color, styleHistogram | styleOwnScale, minScale, maxScale, 0, 0, -50 );
}

PlotOwnScale( myvar, "Sum", colorbrightgreen, 0, 40 );
PlotOwnScale( Cond, "Single Occurrence", colorRed, 40, 80 );

image.png
 

Attachments

  • image.png
    image.png
    201 KB · Views: 0
Just a small follow up to extract only the required info

Code:
// three consecutive weeks of falling prices
// for NickF at aussiestockforums.com, 2014
// https://www.aussiestockforums.com/forums/newreply.php?do=postreply&t=28392

dow = DayOfWeek();
Fri = dow == 5;
Mon = dow == 1;
FriC = Valuewhen( Fri, C );
MonC = Valuewhen( Mon, C );
Cond = FriC < MonC AND Fri;

count = 3; // number of consecutive occurrences

friday = Null;
myvar = Null;
n = 0;
 
for ( i = 1 ; i < BarCount ; i ++ )
{
    if ( Fri[i] )
    { 
        friday[n] = Cond[i];
        n++;
    } 
 
    if  ( n > count && Fri[i] )
    {
        cumfridays = 0;
 
        for ( j = n - count; j < n; j++ )
        {           
            cumfridays += friday[j];
        }        

        myvar[i] = cumfridays;
    }
}

myvar =  iif( myvar == count, myvar, 0 );

// Plotting :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
procedure PlotOwnScale( input, chartName, color, minPerc, maxPerc )
{// function by Marcin G.
    minVal = LowestVisibleValue( input );
    maxVal = HighestVisibleValue( input );

    onePerc = ( maxVal - minVal ) / ( maxPerc - minPerc );
    minScale = minVal - minPerc * onePerc;
    maxScale = maxVal + ( 100 - maxPerc ) * onePerc;

    Plot( input, chartName, color, styleHistogram | styleOwnScale, minScale, maxScale, 0, 0, -50 );
}

PlotOwnScale( Cond, "Single Occurrence", colorRed, 40, 80 );
PlotOwnScale( myvar, "Sum", colorbrightgreen, 0, 40 );

image.png
 

Attachments

  • image.png
    image.png
    129 KB · Views: 0
Thank you so much, Trash. I will try using the code you kindly provided.

Nick

Just a small follow up to extract only the required info

Code:
// three consecutive weeks of falling prices
// for NickF at aussiestockforums.com, 2014
// https://www.aussiestockforums.com/forums/newreply.php?do=postreply&t=28392

dow = DayOfWeek();
Fri = dow == 5;
Mon = dow == 1;
FriC = Valuewhen( Fri, C );
MonC = Valuewhen( Mon, C );
Cond = FriC < MonC AND Fri;

count = 3; // number of consecutive occurrences

friday = Null;
myvar = Null;
n = 0;
 
for ( i = 1 ; i < BarCount ; i ++ )
{
    if ( Fri[i] )
    { 
        friday[n] = Cond[i];
        n++;
    } 
 
    if  ( n > count && Fri[i] )
    {
        cumfridays = 0;
 
        for ( j = n - count; j < n; j++ )
        {           
            cumfridays += friday[j];
        }        

        myvar[i] = cumfridays;
    }
}

myvar =  iif( myvar == count, myvar, 0 );

// Plotting :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
procedure PlotOwnScale( input, chartName, color, minPerc, maxPerc )
{// function by Marcin G.
    minVal = LowestVisibleValue( input );
    maxVal = HighestVisibleValue( input );

    onePerc = ( maxVal - minVal ) / ( maxPerc - minPerc );
    minScale = minVal - minPerc * onePerc;
    maxScale = maxVal + ( 100 - maxPerc ) * onePerc;

    Plot( input, chartName, color, styleHistogram | styleOwnScale, minScale, maxScale, 0, 0, -50 );
}

PlotOwnScale( Cond, "Single Occurrence", colorRed, 40, 80 );
PlotOwnScale( myvar, "Sum", colorbrightgreen, 0, 40 );

image.png
 

Attachments

  • image.png
    image.png
    129 KB · Views: 0
Top