Different formula inside one curve. Please, help with the code

Hi to all.
I make 2 pics to explain my problem for you. I think it will more understandable
(original size: http://ibb.co/mE5kqn)
target005

(original size: http://ibb.co/bKAJAn)
target006

Please help me with coding this curve.

And of course i need to be able to choose the start point of my curve.
Something like this:

dd = ParamDate("Date begin", "2012-01-10", 0);
tt = ParamTime("Time begin", "12:00:00", 0);

BarsStart = 1 + BarsSince(DateNum() == dd AND tt == TimeNum());
StartBar = ValueWhen(DateNum() == dd AND tt == TimeNum(), BarIndex());

If the start point ois the Low Extremum, so it will not HI the start point but LOW. And the same changes in other calculation

And here is the code for the red curve at my second pict:

dd = ParamDate("Дата начала", "2012-01-10", 0);
tt = ParamTime("Время начала", "12:00:00", 0);

BarsStart = 1 + BarsSince(DateNum() == dd AND tt == TimeNum());
StartBar = ValueWhen(DateNum() == dd AND tt == TimeNum(), BarIndex());
xi3 = L;
wil = L-Ref(L,-1);
wilsum = Sum (wil, BarsStart-1);
wilkr = xi3+wilsum;
wilkrplot2 = IIf (BarIndex() >= StartBar, wilkr, Null);
Plot (wilkrplot2,"wilkrplot2",colorRed, styleLine | styleNoRescale, Null, Null, 0, 0, 3);

hi,

i am not sure what the curve means but following the instructions on your first chart it would look like this:

dd = ParamDate( "Date begin", "7/6/2015", 0 );
tt = ParamTime( "Time begin", "01:00:00", 0 );
StartBar = LastValue( ValueWhen( DateNum() == dd AND tt == TimeNum(), BarIndex() ) );

sdiff = L - Ref( L, -1 );
ycurve = sdiff + L;
ycurve = IIf( BarIndex() >= StartBar, ycurve, Null );
ycurve[StartBar] = H[StartBar];
ycurve[StartBar + 1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );
Plot( ycurve, "ycurve", coloryellow, styleLine | styleNoRescale, Null, Null, 0, 0, 3 );

The first two values of curve are right as far as i see. But others are not.
We need cumulyative effect.

Let's look at the my formula:

xi3 = L;
wil = L-Ref(L,-1);
wilsum = Sum (wil, BarsStart-1);
wilkr = xi3+wilsum;

But in right way we should take into calculation the difference at our first bar.

target007

i am a bit confused but you can put all the values you like to sum up in the array. So

dd = ParamDate( "Date begin", "7/6/2015", 0 );
tt = ParamTime( "Time begin", "01:00:00", 0 );
StartBar = LastValue( ValueWhen( DateNum() == dd AND tt == TimeNum(), BarIndex() ) );
wil = L - Ref( L, -1 );
wil = IIf( BarIndex() >= StartBar, wil, Null );
wil[StartBar] = H[StartBar];
wil[StartBar + 1] =( L[StartBar + 1] - H[StartBar] );

and then simply do

cum(wil);

but I am confused when looking at your explanation on the first chart

So...
wil[StartBar] - this is our curve at the first bar
wil[StartBar + 1] - this is our curve at the second bar

But I don't understand how I can code the "wil" at the all other bars.

Simply...
wil = ....

maybe best to write down what the values need to be because I am just guessing

so how would you final curve (wilsum) look like? When you fill in the question marks it will be easier

wilsum[StartBar] = H[StartBar];
wilsum[StartBar+1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );
wilsum[StartBar+2] = ?
wilsum[StartBar+3] = ?
wilsum[StartBar+4] = ?
etc.

wilsum[StartBar] = H[StartBar];
wilsum[StartBar+1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );

//cumwil = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] )

FDiff = L[StartBar + 1] - H[StartBar]

wilsum[StartBar+2] = L[StartBar + 2] + ( (L[StartBar + 2] - (L[StartBar + 1] + FDiff )
wilsum[StartBar+3] = L[StartBar + 3] + ( L[StartBar + 3] - (L[StartBar + 2] + (L[StartBar + 2] - (L[StartBar + 1] + FDiff )
wilsum[StartBar+4] = L[StartBar + 4] + (L[StartBar + 4] - (L[StartBar + 3] + (L[StartBar + 2] (L[StartBar + 3] - (L[StartBar + 2] + (L[StartBar + 2] - (L[StartBar + 1] + FDiff ))

Something like this

wilsum[StartBar] = H[StartBar];
wilsum[StartBar+1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );

FDiff = L[StartBar + 1] - H[StartBar];
Diff = L - Ref (L,-1);
wil = FDiff + Sum( Diff, StartBar+2);

wilsum[StartBar+2] = L[StartBar + 2] + wil
wilsum[StartBar+3] = L[StartBar + 3] + wil
wilsum[StartBar+4] = L[StartBar + 4] + wil

I think this is more good looking

like this?

dd = ParamDate( "Date begin", "7/6/2015", 0 );
tt = ParamTime( "Time begin", "01:00:00", 0 );
StartBar = LastValue( ValueWhen( DateNum() == dd AND tt == TimeNum(), BarIndex() ) );

// loop version
fdiff = L[StartBar + 1] - H[StartBar];
diff = L - Ref( L, -1 );
wilsum = Null;
wilsum[StartBar] = H[StartBar];
wilsum[StartBar + 1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );
sumdiff = 0;

for( i = startbar + 2; i < BarCount; i++ )
{
    sumdiff += diff[i];
    wilsum[i] = L[i] + ( fdiff + sumdiff );
}
Plot( wilsum, "wilsum", coloryellow, styleLine | styleNoRescale, Null, Null, 0, 0, 3 );


// array version
fdiff = L[StartBar + 1] - H[StartBar];
diff = L - Ref( L, -1 );
sumdif = Sum( diff, BarsSince( BarIndex() == ( StartBar + 1 ) ) );
wilsum = IIf( BarIndex() > StartBar + 1, L + fdiff + sumdif, Null );
wilsum[StartBar] = H[StartBar];
wilsum[StartBar + 1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );
Plot( wilsum, "wilsum", colorBlue, styleLine | styleNoRescale, Null, Null, 0, 0, 3 );

Oh, yeah!
Tried array version - perfect!

Thanks a lot!

And if not difficult can you make version for low extremum?

For example first of all we get from the menu the value for the first bar of our curve - Hi or Low or "specific meaning" (will write value by hands)

And then if we get Hi then the calculate will be how in we see now.
If we get Low so all calculate will be not L[StartBar ] bur H[StartBar]

Is it possible?

I still have no clue what to use it for :slight_smile: but would the top & bottom look something like this? you can chose the top or bottom with the mouse. What do you use it for?

GraphXSpace = 5;
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 150, 0 ), IIf( C < O, ColorRGB( 150, 0, 0 ), ColorRGB( 150, 150, 150 ) ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ) , IIf( C < O, ColorRGB( 255, 0, 0 ), ColorRGB( 150, 150, 150 ) ) ), 64, Null, Null, 0, 0, 1 );

sv = SelectedValue( BarIndex() );
dd = ParamDate( "Date begin", "7/6/2015", 0 );
tt = ParamTime( "Time begin", "01:00:00", 0 );
StartBar = LastValue( ValueWhen( DateNum() == dd AND tt == TimeNum(), BarIndex() ) );

StartBar = sv;

if( H[StartBar] >= H[StartBar - 1] AND  H[StartBar] >= H[StartBar + 1] )
{
	//TOP
    fdiff = L[StartBar + 1] - H[StartBar];
    diff = L - Ref( L, -1 );
    sumdif = Sum( diff, BarsSince( BarIndex() == ( StartBar + 1 ) ) );
    wilsum = IIf( BarIndex() > StartBar + 1, L + (fdiff + sumdif), Null );
    wilsum[StartBar] = H[StartBar];
    wilsum[StartBar + 1] = L[StartBar + 1] + ( L[StartBar + 1] - H[StartBar] );
    Plot( wilsum, "wilsum", colorBlue, styleLine | styleNoRescale, Null, Null, 0, 0, 3 );
}
else
    if( L[StartBar] <= L[StartBar - 1] AND  L[StartBar] <= L[StartBar + 1] )
    {
		//BOTTOM
        fdiff = H[StartBar + 1] - L[StartBar];
        diff = H - Ref( H, -1 );
        sumdif = Sum( diff, BarsSince( BarIndex() == ( StartBar + 1 ) ) );
        wilsum = IIf( BarIndex() > StartBar + 1, H - (fdiff - sumdif), Null );
        wilsum[StartBar] = L[StartBar];
        wilsum[StartBar + 1] = H[StartBar + 1] - ( H[StartBar + 1] - L[StartBar] );
        Plot( wilsum, "wilsum", colorGreen, styleLine | styleNoRescale, Null, Null, 0, 0, 3 );
    }

I have a mistake with this code.
target010

And I guess it will be not VERY good. To find bottom or top automatikaly.
I think about such option:

xiname1 = ParamList("Основа расчета 1", "Х,Л", 0);
if (xiname1=="Х") {
xi3 = L;
} else
if (xiname1=="Л") {
xi3 = H;

Something like this.

And about using it. I will show you later, first I need to have a right calculation to draw the right curves, so it will be understand

ok yes, when using selected value you have to make sure Startbar < barcount - 1,

so replace this line, StarBar = sv, with

StartBar = Min( sv, BarCount - 2 );