• Australian (ASX) Stock Market Forum

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

Discussion in 'Software and Data' started by NickF, May 4, 2014.

  1. NickF

    NickF Well-Known Member

    Posts:
    180
    Likes Received:
    0
    Joined:
    Feb 20, 2014
    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
     
  2. Wysiwyg

    Wysiwyg Everyone wants money

    Posts:
    7,091
    Likes Received:
    7
    Joined:
    Aug 8, 2006
    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
     
  3. trash

    trash Well-Known Member

    Posts:
    388
    Likes Received:
    4
    Joined:
    Mar 31, 2012
    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
    // http://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 );
    
    [​IMG]
     
  4. trash

    trash Well-Known Member

    Posts:
    388
    Likes Received:
    4
    Joined:
    Mar 31, 2012
    Just a small follow up to extract only the required info

    Code:
    // three consecutive weeks of falling prices
    // for NickF at aussiestockforums.com, 2014
    // http://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 );
    [​IMG]
     
  5. NickF

    NickF Well-Known Member

    Posts:
    180
    Likes Received:
    0
    Joined:
    Feb 20, 2014
    Thank you so much, Trash. I will try using the code you kindly provided.

    Nick

     
Loading...

Share This Page