How to check if two patterns (e.g peak and trough) exist or not?

How to check if two patterns exists in one condition or multiple conditions?

What I am trying to achieve is, if a peak (day 1) and trough (day 2) exist then on (day 3) buy at closing price of peak (day 1) whenever high & low gets HIT on (Day 3).

I am trying use the IIF Condition where I check if Peak and Trough are there or not. If condition is true then I Buy at (Day 1) closing price, when (Day 3) high crosses the (Day 1) closing price.

_SECTION_BEGIN("Buy Indicator");

X=1; 
Constant= 0.000; 

PeakRefO = Ref(Peak(C,X),-1);//refer to last peak 
TroughRefO = Ref(Trough(C,X),-1);//refer to last trough

PeakRef_H = Ref(Peak(H,X),0);

Peak_Ref = PeakRefO*(1 + Constant ); //converting values into decimals
Trough_Ref = TroughRefO*(1 - Constant ); //same

//BuySignal=Cross(H,Peak_Ref);

Entry_Signal = IIf(PeakRefO == True AND TroughRefO == True, Buy=Cross(C,Peak_Ref), Null);//Condition iif peak & trough exists
Sell = ApplyStop(stopTypeLoss,stopModePercent,2,1);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

_SECTION_END();

@mayankb3 I have looked at your post, but don’t know what your question is.

I took a quick look at your code, but am not in the mindset to try and figure it all out…

But here are my hints anyway.

Make an Exploration. If you don’t know how, it is WELL worth your time to figure it out. When you make the Exploration, you will be able to show your values as you are calculating them to see if your calculations are what you expect. Then modify your code as needed.

If or when you run into logic issues, post a well defined question, so we can know how to help.

Hello @snoopy.pa30, I’ll try to make the exploration and thanks for the reply. Also, can you please give me a link of a well defined question related to logic issues preferably.

Thanks for the help. I’ll comment further when I have something in hand. :slight_smile:

Hello again @snoopy.pa30, I have been working on my code and finally got some results. Now, my query is

"I want to check whether two conditions are True or not, if both of them are true then I want to BUY else NULL. For this I tried using IIF and ValueWhen functions but couldn't get results in the EXPLORATION window. Can you help me with the code?

Rules for buying are -

  1. Most recent peak and trough should be available.
  2. If the first rule is satisfied then we buy on the current day.
  3. If second rule is satisfied then we will buy at the closing price of most recent peak (1).

I have pasted the code & screenshot"

X=0.1;//for %change in peak/trough
Y=1;//Most recent peak/trough
Constant = 0.000;//for decimal

UpperO=Ref(Peak(C,X,Y),-1);
LowerO=Ref(Trough(C,X,Y),-1);

Upper=UpperO*(1+Constant);
Lower=LowerO*(1-Constant);

//EntrySignal = IIf(UpperO==True && LowerO==True, BuyNOW=True, BuyNOW=False);

EntrySignal = ValueWhen(UpperO==True AND LowerO==True,Upper,1);

Buy = Cross(H,EntrySignal);

Capture

1 Like

hi, i do not really understand the question as well, that’s probably because of the language.

but I would say that when playing around it is easiest to use the chart to see what is happening. Then you get visual feedback, see for instance code below

X = 5; //for %change in peak/trough
Y = 1; //Most recent peak/trough
Constant = 0.000;//for decimal

UpperO = Ref( Peak( C, X, Y ), -1 );
LowerO = Ref( Trough( C, X, Y ), -1 );

Buy = Cross( H, UpperO );

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

Plot( UpperO, "", colorRed, 1 );
Plot( LowerO, "", colorGreen, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 255, 0, 0 ), 0, L, -10 );
2 Likes

Hey @empottasch @snoopy.pa30 I'll make it easy for you.

"I want ENTER in a trade if I have a peak and a trough, check the below code. I am checking if a basic pattern of peak and trough exists then I should buy whenever HIGH PRICE breaks the CLOSING price of the UPPER.

Check Screenshot"

EntrySignal = IIf(UpperO==True && LowerO==True, Buy=Cross(H,Upper), NULL);

Did you understand?Screenshot

1 Like

you can do that as follow, see code below.

to explain the Buy rule:

Buy = Cross( H, UpperY1 ) AND UpperX1 < LowerX1 AND LowerY1 > LowerY2;
  • The first you know.

  • The UpperX1 < LowerX1 means the X value of the last peak is less than the X value of the last trough. This ensures that there is a trough between your last peak and the Buy signal.

  • Then LowerY1 > LowerY2 means that the Y value of the last trough is higher then the Y value of the trough before that. Or: you want your last trough to be a higher low.

Size = Param( "Percentage", 5, 0, 10, 0.1 );

x = BarIndex();
pk = Peak( H, Size ) == H AND Zig( H, Size ) == H;
tr = Trough( L, Size ) == L AND Zig( L, Size ) == L;

for( i = 0; i <= 3; i++ )
{
    VarSet( "UpperX" + i, ValueWhen( pk, x, i ) );
    VarSet( "UpperY" + i, ValueWhen( pk, H, i ) );
    VarSet( "LowerX" + i, ValueWhen( tr, x, i ) );
    VarSet( "LowerY" + i, ValueWhen( tr, L, i ) );
}

Buy = Cross( H, UpperY1 ) AND UpperX1 < LowerX1 AND LowerY1 > LowerY2;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

Plot( UpperY1, "", colorRed, 1 );
Plot( LowerY1, "", colorGreen, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), ColorRGB( 0, 255, 0 ), 0, L, -10 );
1 Like

You understood it right but I am still not getting the results with the code. What I want is explained in the image here Screenshot

When I am using your code buying is happening at the peak. What I require is it should BUY just before the peak is about to be made. Where the BUY arrow is pointing out. Consider the image,

Peak 1 then Trough 2 then Peak 2 and so on..

Now I want to BUY at the CLOSE price of Peak 1 when trough 1 takes place and price is going up to PEAK 2. I want to BUY before PEAK 2 is made. Did you understand?

P.S - Your BUY code is working for

Your Condition : Peak1&&Trough1==Exists then BUY at Peak 2.
My Condition : Peak1&&Trough1==Exists then BUY whenever HIGH hits the closing price of PEAK1.

Thanks for the help!

1 Like

this is not what I am seeing. What timeframe are you using and at what percentage did you set the parameter "Size".

when I use EOD data and Percentage = 5%, my chart looks as shown below. The blue arrows are at the buy locations. This is just like in your picture. Basically the code is correct (in my opinion) although it does not yet account for situations where the cross of UpperY1 coincides with a peak. This however does not happen often but should be accounted for still.

pktr

1 Like

as I said sometimes a buy signal coincides with a peak, see chart (3rd buy signal). I corrected the code for that.

pktr2

size = Param( "Percentage", 5, 0, 10, 0.1 );

x = BarIndex();
pk = Peak( H, size ) == H AND Zig( H, size ) == H;
tr = Trough( L, size ) == L AND Zig( L, size ) == L;

for( i = 0; i <= 3; i++ )
{
    VarSet( "UpperX" + i, ValueWhen( pk, x, i ) );
    VarSet( "UpperY" + i, ValueWhen( pk, H, i ) );
    VarSet( "LowerX" + i, ValueWhen( tr, x, i ) );
    VarSet( "LowerY" + i, ValueWhen( tr, L, i ) );
}

Buy = Cross( H, Ref( UpperY1, -1 ) ) AND Ref( UpperX1, -1 ) <= Ref( LowerX1, -1 ) AND Ref( LowerY1, -1 ) >= Ref( LowerY2, -1 );
Buy = ExRem( Buy, pk );
BuyPrice = Max( O, Ref( UpperY1, -1 ) );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

Plot( UpperY1, "", colorRed, 1 );
Plot( LowerY1, "", colorGreen, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, -10 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), ColorRGB( 255, 255, 255 ), 0, BuyPrice, 0 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), ColorRGB( 0, 255, 0 ), 0, L, -10 );
3 Likes

@empottasch

I get everything you said above and sorry I forget to mention one more thing that I am using line charts instead of candlestick charts. As line charts are giving better results rather than using Candlestick charts. I have checked it manually. I am trying to implement a high frequency trading system in terms of order execution (using algorithm to track prices & fluctuations on a time frame of 10 to 60 mins). Since, I am new to the trading strategies I am still learning how to back test and code the strategies. If I am not clear let me know then. Thanks for the help.

1 Like

you can also try fractal and ATR type pivots. They adjust when you change the time frame. I posted these lots of times. Below some code that uses ATR pivots. I just did a replay and seems that the signals are correct but you will have to double check

perBull = Param( "Bullish ATR Period", 20, 1, 150, 1 );
perBear = Param( "Bearish ATR Period", 20, 1, 150, 1 );
multBull = Param( "Bullish ATR Multiple", 2, 1, 4, 0.05 );
multBear = Param( "Bearish ATR Multiple", 2, 1, 4, 0.05 );
tfrm = in1Minute * Interval() / 60 * Param( "Chart Time Frame Factor", 2, 1, 10, 1 );
x = BarIndex();

function ATRtrail_func()
{
    tvHigh = C;
    tvLow = C;

    sup = tvHigh - multBull * ATR( perBull );
    res = tvLow + multBear * ATR( perBear );

    trailARRAY = Null;
    trailstop = 0;

    for( i = 1; i < BarCount; i++ )
    {
        if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
            trailstop = Max( trailstop, sup[ i ] );
        else
            if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
                trailstop = Min( trailstop, res[ i ] );
            else
                trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );

        trailARRAY[ i ] = trailstop;
    }

    return trailARRAY;
}

TimeFrameSet( tfrm );
trBull = multBull * ATR( perBull );
trBear = multBear * ATR( perBear );
trailArray = ATRtrail_func();
ts = IIf( trailArray > C, trailArray, Null ); // dntrend
tl = IIf( trailArray < C, trailArray, Null ); // uptrend
TimeFrameRestore();

ts = TimeFrameExpand( ts, tfrm, expandlast );
tl = TimeFrameExpand( tl, tfrm, expandlast );

lll = LLV( L, BarsSince( !IsEmpty( tl ) ) );
lll = IIf( ts, lll, Null );
trn = ts AND L == lll;

hhh = HHV( H, BarsSince( !IsEmpty( ts ) ) );
hhh = IIf( tl, hhh, Null );
pkn = tl AND H == hhh;

tr = ExRem( Reverse( trn ), Reverse( pkn ) );
pk = ExRem( Reverse( pkn ), Reverse( trn ) );

tr = Reverse( tr );
pk = Reverse( pk );

for( i = 0; i <= 3; i++ )
{
    VarSet( "UpperX" + i, ValueWhen( pk, x, i ) );
    VarSet( "UpperY" + i, ValueWhen( pk, H, i ) );
    VarSet( "LowerX" + i, ValueWhen( tr, x, i ) );
    VarSet( "LowerY" + i, ValueWhen( tr, L, i ) );
}

Buy = Cross( H, Ref( UpperY1, -1 ) ) AND Ref( UpperX1, -1 ) <= Ref( LowerX1, -1 ) AND Ref( LowerY1, -1 ) >= Ref( LowerY2, -1 );
Buy = ExRem( Buy, pk );
BuyPrice = Max( O, Ref( UpperY1, -1 ) );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

Plot( UpperY1, "", colorRed, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( LowerY1, "", colorGreen, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, -10 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), ColorRGB( 255, 255, 255 ), 0, BuyPrice, 0 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), ColorRGB( 0, 255, 0 ), 0, L, -10 );
3 Likes

@empottasch

I got it but how to identify all the possible peaks and troughs based on the LINE CHART (not candlestick) since candlestick chart doesn't support my strategy only line chart does. I want to identify all the possible peaks and troughs (means % change of 0.1) as the time and market moves forward.

P.S - Sorry for the late reply. I was busy with my other work.

Thank you very much for the help.

using tick data you would just use the last price, which is C (Close)

would look like this:

size = Param( "Percentage", 0.1, 0, 10, 0.05 );

x = BarIndex();
pk = Peak( C, size ) == C AND Zig( C, size ) == C;
tr = Trough( C, size ) == C AND Zig( C, size ) == C;

for( i = 0; i <= 3; i++ )
{
    VarSet( "UpperX" + i, ValueWhen( pk, x, i ) );
    VarSet( "UpperY" + i, ValueWhen( pk, C, i ) );
    VarSet( "LowerX" + i, ValueWhen( tr, x, i ) );
    VarSet( "LowerY" + i, ValueWhen( tr, C, i ) );
}

Buy = Cross( C, Ref( UpperY1, -1 ) ) AND Ref( UpperX1, -1 ) <= Ref( LowerX1, -1 ) AND Ref( LowerY1, -1 ) >= Ref( LowerY2, -1 );
Buy = ExRem( Buy, pk );
BuyPrice = C;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleLine, Null, Null, 0, 0, 1 );

Plot( UpperY1, "", colorRed, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( LowerY1, "", colorGreen, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), ColorRGB( 0, 255, 255 ), 0, L, -10 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), ColorRGB( 255, 255, 255 ), 0, BuyPrice, 0 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), ColorRGB( 255, 0, 0 ), 0, H, 10 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), ColorRGB( 0, 255, 0 ), 0, L, -10 );
2 Likes

Code is correct but 40% of the buy signals are missing from here. Please check image -

I have highlighted buys with yellow colour.

Capture

1 Like

starting from the first yellow square from the left

  1. there should be no buy there because the low pivot is below the low pivot before that. That is how you first wanted it. If you want to see that signal then just change the Buy rule to (comment out last condition):
Buy = Cross( C, Ref( UpperY1, -1 ) ) AND Ref( UpperX1, -1 ) <= Ref( LowerX1, -1 );// AND Ref( LowerY1, -1 ) >= Ref( LowerY2, -1 );
  1. same situation as 1)
  2. using Buy = ExRem( Buy, pk ); I removed all but the first Buy signal between a trough and a peak.
  3. same situation as 1)
1 Like