AFL to plot line on next few bars

#11
As per your chart, my requirement is we to identify two bar before your red bar and draw line from there. So when you get red bar, is it possible to identify index of two bar before and it's high and draw the line? I hope that i am not confusing.. Thanks for looking into this
 

colion

Active Member
#13
As per your chart, my requirement is we to identify two bar before your red bar and draw line from there. So when you get red bar, is it possible to identify index of two bar before and it's high and draw the line? I hope that i am not confusing.. Thanks for looking into this
That was done in the first code. Just modify it if you do not want to use SelectedValue().
 
#15
Hi,
Want to know how to plot a line on 1st bar close.
I have this code plotting line on 1st bar open.
Plot( C, " ", colorWhite, styleCandle );
S = TimeFrameGetPrice("O", inDaily);
Plot(S, "",colorGreen,styleDots+styleLine);
 
#18
Hi Colion,

Could you please look into this. I was using vwap, vzo and supertrend together. To remove clutter from chart and plot only buy sell arrows. But, vwap vzo and supertrend will turn on buy signals on different bars (same for sell signals).

I have tried my best and have tried coding (where i have no idea of development and coding). May be i coded all wrong. Tried and failed with "for loops", "lastvalue" and now failed with "value when".

Could you please suggest how to combine different indicator signals that appear on different bars, into one.

I will be much obliged.
PHP:
Buy =   (ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)  ==0 AND ValueWhen(Sell2,Sell2) == 0 AND ValueWhen(Sell3,Sell3) ==0);
Sell =   (ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)  == 0 AND ValueWhen(Buy2,Buy2) ==0 AND  ValueWhen(Buy3,Buy3) ==0);

sideways = ((ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)==1 OR ValueWhen(Sell2,Sell2)==1 OR ValueWhen(Sell3,Sell3) ==1))
        OR    ((ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2)==1 OR  ValueWhen(Buy3,Buy3) ==1))
     OR    ((ValueWhen(Sell1,Sell1) ==1 OR ValueWhen(Sell2,Sell2)==1 OR  ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2) OR ValueWhen(Buy3,Buy3) ==1));
 
Last edited:

colion

Active Member
#19
Hi Colion,

Could you please look into this. I was using vwap, vzo and supertrend together. To remove clutter from chart and plot only buy sell arrows. But, vwap vzo and supertrend will turn on buy signals on different bars (same for sell signals).

I have tried my best and have tried coding (where i have no idea of development and coding). May be i coded all wrong. Tried and failed with "for loops", "lastvalue" and now failed with "value when".

Could you please suggest how to combine different indicator signals that appear on different bars, into one.

I will be much obliged.
PHP:
Buy =   (ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)  ==0 AND ValueWhen(Sell2,Sell2) == 0 AND ValueWhen(Sell3,Sell3) ==0);
Sell =   (ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)  == 0 AND ValueWhen(Buy2,Buy2) ==0 AND  ValueWhen(Buy3,Buy3) ==0);

sideways = ((ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)==1 OR ValueWhen(Sell2,Sell2)==1 OR ValueWhen(Sell3,Sell3) ==1))
        OR    ((ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2)==1 OR  ValueWhen(Buy3,Buy3) ==1))
     OR    ((ValueWhen(Sell1,Sell1) ==1 OR ValueWhen(Sell2,Sell2)==1 OR  ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2) OR ValueWhen(Buy3,Buy3) ==1));
Without seeing your complete code and a chart it is difficult to understand your problem.
 
#20
Without seeing your complete code and a chart it is difficult to understand your problem.
Please have a look at this url. It is the original thread of the issue.

http://www.traderji.com/amibroker/94505-my-trading-strategy-needs-coding.html

However, also including the graphic and code here as well. (code has been commented upon a lot by me. It might help to copy paste it in amibroker to understand it. )

please do help. looking forward to your guidance.
=================
PHP:
_SECTION_BEGIN( "Confluence" );
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////Volume Zone Oscillator/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////


//Volume Zone Oscillator 
C1=C;
V1=V;
period=Param("LookBack",14,2,999,1,0);
R= IIf (C1>Ref(C1,-1), V1,-V1); 
VP= EMA (R, period);
TV=EMA (V, period);
VZO= 100*(VP/TV);
//Plot(VZO,"",colorOrange,4);
//Plot(60,"",colorPaleBlue,styleLine);
//Plot(40,"",colorPaleBlue,styleLine);
//Plot(0,"",colorDefault,styleDashed);
//Plot(-60,"",colorPaleBlue,styleLine);
//Plot(-40,"",colorPaleBlue,styleLine);
//PlotOHLC(Null,60,40,Null,"",colorDarkGreen);//Removed stylecloud
//PlotOHLC(Null,-60,-40,Null,"",colorDarkRed); //Removed styleCloud

  
///////////////////////////////buy/sell///////////////////////
//zerochkbuy = IIf(Cross(0,Ref(VZO,-1)) AND Cross(VZO,0),True,False);
//zerochksell = IIf(Cross(Ref(VZO,-1),0)AND Cross(0,VZO),True,False);

//Buy=Cross(VZO,-40) OR zerochkbuy;Sell=Cross(40,VZO) OR zerochksell ;
Buy1=Cross(VZO,-40) OR Cross(VZO,0);
Sell1=Cross(40,VZO) OR Cross(0,VZO);
//Buy = ExRem( Buy, Sell );
//Sell = ExRem( Sell, Buy );
/////////////////////////shapes ///////////////////////////////
//shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
//PlotShapes( shape, IIf( Buy, colorBrightGreen, colorRed ),0, IIf( Buy,VZO,VZO) );
/////////////////////////exploration ///////////////////////////////
//Filter=Buy OR Sell; 
//AddColumn( IIf(Buy,1,IIf(Sell,-1,Null)) ,"B/S",1.0,colorWhite,IIf(Buy,colorGreen,IIf(Sell,colorRed,Null)),60);
  
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////Volume Zone Oscillator/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////SuperTrend ATR System///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
    Factor = Param( "Factor", 1, 1, 10, 0.1 );
    Pd = Param( "ATR Periods", 5, 1, 100, 1 );

    Up = ( H + L ) / 2 + ( Factor * ATR( Pd ) );
    Dn = ( H + L ) / 2 - ( Factor * ATR( Pd ) );
    iATR = ATR( Pd );
    TrendUp = TrendDown = Null;
    trend[0] = 1;
    changeOfTrend = 0;
    flag = flagh = 0;

    for ( i = 1; i < BarCount - 1; i++ )
    {
        TrendUp[i] = Null;
        TrendDown[i] = Null;

        trend[i] = 1;


        if ( Close[i] > Up[i-1] )
        {
            trend[i] = 1;

            if ( trend[i-1] == -1 )
                changeOfTrend = 1;

        }
        else
            if ( Close[i] < Dn[i-1] )
            {
                trend[i] = -1;

                if ( trend[i-1] == 1 )
                    changeOfTrend = 1;
            }
            else
                if ( trend[i-1] == 1 )
                {
                    trend[i] = 1;
                    changeOfTrend = 0;
                }
                else
                    if ( trend[i-1] == -1 )
                    {
                        trend[i] = -1;
                        changeOfTrend = 0;
                    }

        if ( trend[i] < 0 && trend[i-1] > 0 )
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }

        if ( trend[i] > 0 && trend[i-1] < 0 )
        {
            flagh = 1;
        }
        else
        {
            flagh = 0;
        }

        if ( trend[i] > 0 && Dn[i] < Dn[i-1] )
        {
            Dn[i] = Dn[i-1];
        }

        if ( trend[i] < 0 && Up[i] > Up[i-1] )
        {
            Up[i] = Up[i-1];
        }

        if ( flag == 1 )
        {
            Up[i] = ( H[i] + L[i] ) / 2 + ( Factor * iATR[i] );;
        }

        if ( flagh == 1 )
        {
            Dn[i] = ( H[i] + L[i] ) / 2 - ( Factor * iATR[i] );;
        }

        if ( trend[i] == 1 )
        {
            TrendUp[i] = Dn[i];

            if ( changeOfTrend == 1 )
            {
                TrendUp[i-1] = TrendDown[i-1];
                changeOfTrend = 0;
            }
        }
        else
            if ( trend[i] == -1 )
            {
                TrendDown[i] = Up[i];

                if ( changeOfTrend == 1 )
                {
                    TrendDown[i-1] = TrendUp[i-1];
                    changeOfTrend = 0;
                }
            }
    }

//    Plot( TrendUp, "Trend", colorBrightGreen , styleNoRescale);

//    Plot( TrendDown, "Down", colorRed , styleNoRescale);

    
    

//////////////////////////////SuperTrend BUY CONDITIONS///////////////////////////////////////////////////////////
Buy2 = IIf(trend == 1,1,0);
//////////////////////////////SuperTrend SELL CONDITIONS////////////////////////////////////
Sell2 = IIf(trend == -1,1,0);
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////VWAP CALCULATIONS////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////


starttime = 093000; 
endtime = 160000; 

timeWindow = TimeNum() >= starttime AND TimeNum() <= endtime;
firstBarOfDay = TimeNum() >= starttime ;
firstBarOfDay = firstBarOfDay - Ref(firstBarOfDay,-1);

P = (H + L)/2;

vv = V * P * timeWindow;

BarsInDay = (1 + BarsSince(firstBarOfDay)) * timeWindow;
CumVolumeInDay = Sum(V,BarsInDay);
VWAP = Sum(vv, BarsInDay ) / CumVolumeInDay;
SV = Sum((P - VWAP)^2,BarsInDay) / BarsInDay; // sample variance
SD = sqrt(SV); // standard deviation
period = ParamList( "What period?", "Daily|Weekly|Monthly", 0 );

switch ( period )
{

case "Daily":
    newPeriod = Day() != Ref( Day(), -1 );
    break;

case "Weekly":
    newPeriod = DayOfWeek() == 0 AND Ref( DayOfWeek(), -1 ) != 0;
    break;

case "Monthly":
    newPeriod = Month() != Ref( Month(), -1 );
    break;
}
//////////////////////////////VWAP BUY CONDITIONS///////////////////////////////////////////////////////////
Buy3 = Ref(C,-1) > VWAP ;    
//////////////////////////////VWAP SELL CONDITIONS///////////////////////////////////////////////////////////
Sell3 = Ref(C,-1) < VWAP ;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////Buy Sell Logic///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
Buy =   (ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)  ==0 AND ValueWhen(Sell2,Sell2) == 0 AND ValueWhen(Sell3,Sell3) ==0);
Sell =   (ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)  == 0 AND ValueWhen(Buy2,Buy2) ==0 AND  ValueWhen(Buy3,Buy3) ==0);

sideways = ((ValueWhen(Buy1,Buy1)==1 AND ValueWhen(Buy2,Buy2)==1 AND ValueWhen(Buy3,Buy3)==1) AND (ValueWhen(Sell1,Sell1)==1 OR ValueWhen(Sell2,Sell2)==1 OR ValueWhen(Sell3,Sell3) ==1))
        OR    ((ValueWhen(Sell1,Sell1)==1 AND ValueWhen(Sell2,Sell2)==1 AND ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2)==1 OR  ValueWhen(Buy3,Buy3) ==1))
     OR    ((ValueWhen(Sell1,Sell1) ==1 OR ValueWhen(Sell2,Sell2)==1 OR  ValueWhen(Sell3,Sell3)==1) AND (ValueWhen(Buy1,Buy1)==1 OR ValueWhen(Buy2,Buy2) OR ValueWhen(Buy3,Buy3) ==1));




//Buy =   (LastValue(Buy1)==1 AND LastValue(Buy2)==1 AND LastValue(Buy3)==1) AND (LastValue(Sell1)  ==0 AND LastValue(Sell2) == 0 AND LastValue(Sell3) ==0);
//Sell =   (LastValue(Sell1)==1 AND LastValue(Sell2)==1 AND LastValue(Sell3)==1) AND (LastValue(Buy1)  == 0 AND LastValue(Buy2) ==0 AND  LastValue(Buy3) ==0);

//sideways = ((LastValue(Buy1)==1 AND LastValue(Buy2)==1 AND LastValue(Buy3)==1) AND (LastValue(Sell1)==1 OR LastValue(Sell2)==1 OR LastValue(Sell3) ==1))
   //     OR    ((LastValue(Sell1)==1 AND LastValue(Sell2)==1 AND LastValue(Sell3)==1) AND (LastValue(Buy1)==1 OR LastValue(Buy2)==1 OR  LastValue(Buy3) ==1))
     //OR    ((LastValue(Sell1) ==1 OR LastValue(Sell2)==1 OR  LastValue(Sell3)==1) AND (LastValue(Buy1)==1 OR LastValue(Buy2) OR LastValue(Buy3) ==1));


//Buy =   (LastValue(VWAPbuyflag)==LastValue(vzobuyflag)==LastValue(SupTbuyflag)==1) AND (LastValue(VWAPsellflag)  == LastValue(vzosellflag) == LastValue(SupTsellflag) ==0);
//Sell =   (LastValue(VWAPsellflag)==LastValue(vzosellflag)==LastValue(SupTsellflag)==1) AND (LastValue(VWAPbuyflag)  == LastValue(vzobuyflag) == LastValue(SupTbuyflag) ==0);

//sideways = ((LastValue(VWAPbuyflag)==LastValue(vzobuyflag)==LastValue(SupTbuyflag)==1) AND (LastValue(VWAPsellflag)  | LastValue(vzosellflag) | LastValue(SupTsellflag) ==1))
   //        OR    ((LastValue(VWAPsellflag)==LastValue(vzosellflag)==LastValue(SupTsellflag)==1) AND (LastValue(VWAPbuyflag)  | LastValue(vzobuyflag) | LastValue(SupTbuyflag) ==1))
      //     OR    ((LastValue(VWAPsellflag) | LastValue(vzosellflag) | LastValue(SupTsellflag)==1) AND (LastValue(VWAPbuyflag)  | LastValue(vzobuyflag) | LastValue(SupTbuyflag) ==1));
      

///////////////////////////////////////////////////////////////////////////////////////////////////////
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
//Buy = Flip( Buy, Sell );
//Sell = Flip(Sell,Buy);
///////////////////////////////////////////////////////////////////////////////////////////////////////
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -12 );
        PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -12 );

AlertIf( Buy, "SOUND C:\\Program Files (x86)\\AmiBroker\\Sound Alerts\\alert1.wav", "Audio alert", 2 );
AlertIf( Sell, "SOUND C:\\Program Files (x86)\\AmiBroker\\Sound Alerts\\alert1.wav", "Audio alert", 2 );
Filter = Buy OR Sell;
    
//if(Candletype)
//{
//SetBarFillColor(  IIf ( (Sell) AND (!sideways),colorRed,colorBrightGreen));
//Plot(C,"Price",     IIf ( (Hlv == -1 &&  Hlv  !=1  &&  Hlv  !=0) AND NOT(Hlv == 1 &&  Hlv  !=-1  &&  Hlv  !=0),colorRed,colorBrightGreen),styleCandle);
//}
//else
//{
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
//}

_SECTION_END();
Above is my simple strategy. AFLs of the components are available on net and on Traderji.

I buy when Volume Zone Oscillator, VWAP and SuperTrend all are positively in Buy Mode. And do wait for reverse signals on all 3 to initiate a Short.

Tried to put all three conditions as a ribbon on my chart to remove all clutter. But i do not know how to "hold" a signal until reverse signal appears.

I have created a small Algorithm which if coded correctly can give desired results. Below, have a look.

===================================
**********************BEGIN*********************** ***
===================================


Initial Declare / Asssign values to flags
greenflagup = 0;
yellowflagup = 0;
redflagup =0;


if at end of most current bar ---> ((buy1 and buy2 and buy3) are all true) AND ((sell1 and sell2 and sell3) are all false);
greenflagup = 1;
(( ACTION -- color of all bars to remain GREEN from CURRENT BAR ONWARDS) uptill greenflagup = 1);


if at the end of current bar ---> (EITHER OF ( Sell1 or Sell2 or Sell3) are true (BUT NOT ALL TRUE TOGETHER, that is, (Sell1 and Sell2 and Sell3 ) != TRUE);
greenflagup = 0;
yellowflagup = 1;
(( ACTION -- color of all bars to remain GREY from CURRENT BAR ONWARDS) uptill yellowflagup = 1);


if at end of most current bar ---> ((Sell1 and Sell2 and Sell3 ) are all true) AND ((buy1 and buy2 and buy3) are all false);
redflagup = 1;
(( ACTION -- color of all bars to remain RED from CURRENT BAR ONWARDS) uptill REDflagup = 1);


if at the end of current bar ---> (EITHER OF ( buy1 OR buy2 OR buy3) are true (BUT NOT ALL TRUE TOGETHER, that is, ( buy1 AND buy2 AND buy3 ) != TRUE);
redflagup = 0;
yellowflagup = 1;
(( ACTION -- color of all bars to remain GREY from CURRENT BAR ONWARDS) uptill yellowflagup = 1);
 
Last edited:

Similar threads