supply demand formula from NINJACATOR

#51
hI

A friend of mine told me -just be careful -avoid these selected value based things.I asked him why so? he replied,"I lost money by not changing the selection during a live futures trade and I dont want that to happen to you"
.
whether anybody criyticise or insult me,I stick to my view-If I dont understand(becoz the novice that I am),my decision is to say "I dont understand this". Otherwise I never learn.
It is better to face an insult and learn the truth than feel shy and hide
being a novice.
anyways,thanks for the clarity.
cheers
ford
PS No need to offer spects to me. I already have them, and thick ones too.



SelectedValue is the same as LastValue only with the addition that you can also view historical data behaviour. If no bar is selected then SelectedValue == LastValue!

There is no "this or that is better" whatsoever. Both (LastValue and SelectedValue) behave the same in real-time (at last value) with new data arriving. It is pretty easy to understand.


PS: I'm not Casoni.
try these

 

trash

Well-Known Member
#52
Final Version 1.4

- added Multi Timeframe support (with switch ON/OFF, 1 minute to 1 day), default 60 minutes


- fixed blinking text display
- fixed min/max val recognition
- removed two linearrays and replaced with simpler method
- few other minor adjustments

Code:
// by trash, vers.1.4

RequestTimedRefresh( 1 );

procedure SayNotTooOften( text, Minperiod )
{   // by Tomasz Janeczko
    elapsed = GetPerformanceCounter() / 1000;
    Lastelapsed = Nz( StaticVarGet( "lastsaytime" ) );

    if ( elapsed - Lastelapsed > Minperiod )
    {
        StaticVarSet( "lastsaytime" , elapsed );
        Say( text );
    }
}

procedure GFXBlinkingMsg( Msg, x1, y1, x2, y2, TextColor, FontSize, FontName )
{
    // originally by Herman van den Bergen, edited by trash 
    global FontName;
    local BlinkState, textcolor, fontSize, msg;

    BlinkState = GetPerformanceCounter() - Nz( StaticVarGet( "~BlinkState" ) );

    if ( BlinkState / 1000 > 2 )
    {
        StaticVarSet( "~BlinkState", GetPerformanceCounter( 1 ) ) ;
    }
    else
    {
        GfxSetBkMode( 1 );
        GfxSelectFont( FontName, FontSize, 700 );
        GfxSetTextColor( TextColor );
        GfxDrawText( Msg, x1, y1, x2, y2, 37 );
    }
}

SDswitch    = ParamToggle( "Supply/Demand Zones", "OFF|ON", 1 );
MTFswitch   = ParamToggle( "Multi TF Support", "OFF|ON", 1 );
tmfrm       = Param( "Input TF of S/D Zones (minutes)", 60, 1, 1440, 1 ) * 60;
SDmode      = ParamList( "Supply/Demand Mode", "Small|Large|Extra Large", 0 );
NumZones    = Param( "Number of Zones", 5, 1, 30, 1 );
Sensitivity = Param( "Sensitivity", 0.4, 0.1, 10, 0.1 );

style1      = ParamStyle( "Style 1", styleNoLabel | styleNoRescale ) | styleCloud;
style2      = ParamStyle( "Style 2", styleNoLabel | styleNoRescale | styleThick );

ColorDem    = ParamColor( "Blend Color - Demand Zones", colorBlueGrey );
ColFacDem   = Param( "Blend Color Factor - Demand Zones", 0.65, 0, 2, 0.01 );
ColorSup    = ParamColor( "Blend Color - Supply Zones", colorDarkRed );
ColFacSup   = Param( "Blend Color Factor - Supply Zones", 0.65, 0, 2, 0.01 );
Colorline   = ParamColor( "Color - Zone Borders", colorGrey50 );


if ( MTFswitch )
{
    expandmode  = expandFirst;
    Close_      = TimeFrameGetPrice( "C", tmfrm, 0, expandmode );
    Open_       = TimeFrameGetPrice( "O", tmfrm, 0, expandmode );
    High_       = TimeFrameGetPrice( "H", tmfrm, 0, expandmode );
    Low_        = TimeFrameGetPrice( "L", tmfrm, 0, expandmode );
}
else
{
    Close_      = C;
    Open_       = O;
    High_       = H;
    Low_        = L;
}

bi          = BarIndex();
selectedBI  = SelectedValue( bi );
selectedC   = SelectedValue( Close_ );
MinVal      = Min( Open_, Close_ );
MaxVal      = Max( Open_, Close_ );


if ( selectedC != LastValue( C ) )
{
    SayNotTooOften( "Viewing historical Supply Demand zones", 30 );
    pxcw    = Status( "pxchartwidth" );
    pxcb    = Status( "pxchartbottom" );
    x1      = pxcw / 2;
    y1      = pxcb;
    GFXBlinkingMsg( "Currently viewing historical Supply/Demand zones!", x1 - 300, y1 - 10, x1 + 300, y1 - 30, colorRed, 12, "Helvetica" );
}


if( SDmode == "Small")
{
    ValSup  = MaxVal;
    ValDem  = MinVal;
}
else 
if( SDmode == "Large" )
{
    ValSup  = MinVal;
    ValDem  = MaxVal;
}
else
{
    ValSup  = Low_;
    ValDem  = High_;
}


for ( i = 1; i <= NumZones; i++ )
{
    x1      = selectedBI - SelectedValue( PeakBars( High_, Sensitivity, i ) );

    y1      = SelectedValue( Peak( High_, Sensitivity, i ) );
    y2      = ValueWhen( bi == x1, High_ - Valsup );

    Line1   = LineArray( x1 + 1, y1, selectedBI, y1, 1 );
    Line2   = Line1 - y2;

    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    x2      = selectedBI - SelectedValue( TroughBars( Low_, Sensitivity, i ) );

    y3      = SelectedValue( Trough( Low_, Sensitivity, i ) );
    y4      = ValueWhen( bi == x2, ValDem - Low_ );

    Line3   = LineArray( x2 + 1, y3, selectedBI, y3, 1 );
    Line4   = Line3 + y4;

    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    i_1     = 20 * i;
    i_2     = 25 + ( i - 1 ) * 10;
    Rcolor  = ColorBlend( ColorSup, ColorRGB( 180, i_1, i_1 ), ColFacSup ); // ParamColor( "Supply Color", colorBrown  );
    Scolor  = ColorBlend( ColorDem, ColorRGB( i_2, i_2, 80 + i_1 ), ColFacDem ); // ParamColor( "Demand Color", colorGreen  );
    RColor  = IIf( selectedC > y1, Scolor, Rcolor );
    SColor  = IIf( selectedC > y4, Scolor, Rcolor );

    if ( SDSwitch )
    {
        zorder  = -i - 9;
        zorder2 = zorder + 1;

        PlotOHLC( Line1, Line1, Line2, Line2, "", Rcolor, style1, 0, 1, 0, zorder );
        Plot( Line1, "", Colorline, style2, 0, 1, 0, zorder2 );
        Plot( Line2, "", Colorline, style2, 0, 1, 0, zorder2 );

        PlotOHLC( Line3, Line3, Line4, Line4, "", Scolor, style1, 0, 1, 0, zorder );
        Plot( Line3, "", Colorline, style2, 0, 1, 0, zorder2 );
        Plot( Line4, "", Colorline, style2, 0, 1, 0, zorder2 );
    }
}
 
Last edited:
#53
Hi Trash

Thanks for this code which is really advanced and beyond my novice zone.
Just saw it and yet to figure out how to utilise this useful tool.
Once again appreciate your serious efforts to give us this gift.
At the moment I just understod that it draws default sd zones using hourly as default timeframe whether I use 5min chart or 15 min chart thus giving access to big picture view.
regards
ford
 

trash

Well-Known Member
#54
Final Version 1.4

- added Multi Timeframe support (with switch ON/OFF, 1 minute to 1 day), default 60 minutes


- fixed blinking text display
- fixed min/max val recognition
- removed two linearrays and replaced with simpler method
- few other minor adjustments

Just found another issue being solved

changed from

Code:
y2 = ValueWhen( High_ == y1, High_ - Valsup );

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

y4 = ValueWhen( Low_ == y3, ValDem - Low_  );
to

Code:
y2 =ValueWhen( bi == x1, High_ - Valsup ); 

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

y4 = ValueWhen( bi == x2, ValDem - Low_ );
Why? Because if a new high/low appeared that was exactly the same as the one of peak or through then it took Valsup or Valdem of that new high so instead I choose barindex.

So copy new code of post #52 if you haven't yet.


At the moment I just understod that it draws default sd zones using hourly as default timeframe whether I use 5min chart or 15 min chart thus giving access to big picture view.
regards
ford
You can turn MTF ON/OFF and if MTF is turned on then you can change the higher timeframe from range 1 min to 1440. If you are on 5 min TF and wanna display zones of 15 min TF then input 15 instead of 60 etc.

If MTF is turned OFF and i.e. you are on 5 min TF then it displays zones of 5 min TF.
 
Last edited:
#55
Hi Trash
some guys have sent pm to me saying the improved code v 1.4 is not working.
with your permission,posting it here again.
regards
ford
HTML:
// by trash, vers.1.4- improved

RequestTimedRefresh( 1 );

procedure SayNotTooOften( text, Minperiod )
{   // by Tomasz Janeczko
    elapsed = GetPerformanceCounter() / 1000;
    Lastelapsed = Nz( StaticVarGet( "lastsaytime" ) );

    if ( elapsed - Lastelapsed > Minperiod )
    {
        StaticVarSet( "lastsaytime" , elapsed );
        Say( text );
    }
}

procedure GFXBlinkingMsg( Msg, x1, y1, x2, y2, TextColor, FontSize, FontName )
{
    // originally by Herman van den Bergen, edited by trash 
    global FontName;
    local BlinkState, textcolor, fontSize, msg;

    BlinkState = GetPerformanceCounter() - Nz( StaticVarGet( "~BlinkState" ) );

    if ( BlinkState / 1000 > 2 )
    {
        StaticVarSet( "~BlinkState", GetPerformanceCounter( 1 ) ) ;
    }
    else
    {
        GfxSetBkMode( 1 );
        GfxSelectFont( FontName, FontSize, 700 );
        GfxSetTextColor( TextColor );
        GfxDrawText( Msg, x1, y1, x2, y2, 37 );
    }
}

SDswitch    = ParamToggle( "Supply/Demand Zones", "OFF|ON", 1 );
MTFswitch   = ParamToggle( "Multi TF Support", "OFF|ON", 1 );
tmfrm       = Param( "Input TF of S/D Zones (minutes)", 60, 1, 1440, 1 ) * 60;
SDmode      = ParamList( "Supply/Demand Mode", "Small|Large|Extra Large", 0 );
NumZones    = Param( "Number of Zones", 5, 1, 30, 1 );
Sensitivity = Param( "Sensitivity", 0.4, 0.1, 10, 0.1 );

style1      = ParamStyle( "Style 1", styleNoLabel | styleNoRescale ) | styleCloud;
style2      = ParamStyle( "Style 2", styleNoLabel | styleNoRescale | styleThick );

ColorDem    = ParamColor( "Blend Color - Demand Zones", colorBlueGrey );
ColFacDem   = Param( "Blend Color Factor - Demand Zones", 0.65, 0, 2, 0.01 );
ColorSup    = ParamColor( "Blend Color - Supply Zones", colorDarkRed );
ColFacSup   = Param( "Blend Color Factor - Supply Zones", 0.65, 0, 2, 0.01 );
Colorline   = ParamColor( "Color - Zone Borders", colorGrey50 );


if ( MTFswitch )
{
    expandmode  = expandFirst;
    Close_      = TimeFrameGetPrice( "C", tmfrm, 0, expandmode );
    Open_       = TimeFrameGetPrice( "O", tmfrm, 0, expandmode );
    High_       = TimeFrameGetPrice( "H", tmfrm, 0, expandmode );
    Low_        = TimeFrameGetPrice( "L", tmfrm, 0, expandmode );
}
else
{
    Close_      = C;
    Open_       = O;
    High_       = H;
    Low_        = L;
}

bi          = BarIndex();
selectedBI  = SelectedValue( bi );
selectedC   = SelectedValue( Close_ );
MinVal      = Min( Open_, Close_ );
MaxVal      = Max( Open_, Close_ );


if ( selectedC != LastValue( C ) )
{
    SayNotTooOften( "Viewing historical Supply Demand zones", 30 );
    pxcw    = Status( "pxchartwidth" );
    pxcb    = Status( "pxchartbottom" );
    x1      = pxcw / 2;
    y1      = pxcb;
    GFXBlinkingMsg( "Currently viewing historical Supply/Demand zones!", x1 - 300, y1 - 10, x1 + 300, y1 - 30, colorRed, 12, "Helvetica" );
}


if( SDmode == "Small")
{
    ValSup  = MaxVal;
    ValDem  = MinVal;
}
else 
if( SDmode == "Large" )
{
    ValSup  = MinVal;
    ValDem  = MaxVal;
}
else
{
    ValSup  = Low_;
    ValDem  = High_;
}


for ( i = 1; i <= NumZones; i++ )
{
    x1      = selectedBI - SelectedValue( PeakBars( High_, Sensitivity, i ) );

    y1      = SelectedValue( Peak( High_, Sensitivity, i ) );
    //y2      = ValueWhen( bi == x1, High_ - Valsup );
   y2 =ValueWhen( bi == x1, High_ - Valsup ); 

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

   
    Line1   = LineArray( x1 + 1, y1, selectedBI, y1, 1 );
    Line2   = Line1 - y2;

    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    x2      = selectedBI - SelectedValue( TroughBars( Low_, Sensitivity, i ) );

    y3      = SelectedValue( Trough( Low_, Sensitivity, i ) );

    //y4      = ValueWhen( bi == x2, ValDem - Low_ );
    y4 = ValueWhen( bi == x2, ValDem - Low_ );
    Line3   = LineArray( x2 + 1, y3, selectedBI, y3, 1 );
    Line4   = Line3 + y4;

    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    i_1     = 20 * i;
    i_2     = 25 + ( i - 1 ) * 10;
    Rcolor  = ColorBlend( ColorSup, ColorRGB( 180, i_1, i_1 ), ColFacSup ); // ParamColor( "Supply Color", colorBrown  );
    Scolor  = ColorBlend( ColorDem, ColorRGB( i_2, i_2, 80 + i_1 ), ColFacDem ); // ParamColor( "Demand Color", colorGreen  );
    RColor  = IIf( selectedC > y1, Scolor, Rcolor );
    SColor  = IIf( selectedC > y4, Scolor, Rcolor );

    if ( SDSwitch )
    {
        zorder  = -i - 9;
        zorder2 = zorder + 1;

        PlotOHLC( Line1, Line1, Line2, Line2, "", Rcolor, style1, 0, 1, 0, zorder );
        Plot( Line1, "", Colorline, style2, 0, 1, 0, zorder2 );
        Plot( Line2, "", Colorline, style2, 0, 1, 0, zorder2 );

        PlotOHLC( Line3, Line3, Line4, Line4, "", Scolor, style1, 0, 1, 0, zorder );
        Plot( Line3, "", Colorline, style2, 0, 1, 0, zorder2 );
        Plot( Line4, "", Colorline, style2, 0, 1, 0, zorder2 );
    }
}
 
Last edited:
#56
hi friends

If you want to use trash,s new v 1.4 version,
the least you can do is
say thanks and be grateful to him for his serious efforts.


And for god,s sake dont be that ungrateful copycat,who will post Trash,s code on wisestocktrader site.
Let him do it by himself when he finds time.

regards
ford
 

trash

Well-Known Member
#57
Hi Trash
some guys have sent pm to me saying the improved code v 1.4 is not working.
with your permission,posting it here again.
regards
ford

I hope your so called "improved" code version is just an attempt to be funny because there is no improvement at all. You just commented two lines and have rewritten them below.

EDIT: You don't have to add the lines of post #54 to the code of post #52. They are already part of the code in post #52! It was just an info in post #54.




So delete the code from your post and keep my code as it is because it works just fine.

The one who has problems should tell what exactly is the problem instead of just saying "not working". What is not working? Just saying "Not working" are the most useless words in the world of programming (not just there).
 
Last edited:
#58
hello trash ,

Thanx you for the modification in above code given by Casoni.


Query - Is it possible to add Buy-Sell signals and exploration to this code.
(I guess not possible then too wt u say)



Also explain how to use it effectively??

FORDK if u could put some input would be glad.

Thanx you
 

mastermind007

Well-Known Member
#59
....

The one who has problems should tell what exactly is the problem instead of just saying "not working". What is not working? Just saying "Not working" are the most useless words in the world of programming (not just there).
:lol: LOL!!!! One of the American fellow I had worked with used to reply "Drop a Quarter and it will" every time he heard someone complain "not working" ...
 

trash

Well-Known Member
#60
Yeah, it can be really frustrating. :D


Anyway here is another small fix that takes care of correct color change

Replace the four lines of version 1.4

Code:
    Rcolor  = ColorBlend( ColorSup, ColorRGB( 180, i_1, i_1 ), ColFacSup ); // ParamColor( "Supply Color", colorBrown  );
    Scolor  = ColorBlend( ColorDem, ColorRGB( i_2, i_2, 80 + i_1 ), ColFacDem ); // ParamColor( "Demand Color", colorGreen  );
    RColor  = IIf( selectedC > y1, Scolor, Rcolor );
    SColor  = IIf( selectedC > y4, Scolor, Rcolor );
with following updated 4 lines

Code:
    Rcolor_ = ColorBlend( ColorSup, ColorRGB( 180, i_1, i_1 ), ColFacSup ); // ParamColor( "Supply Color", colorBrown  );
    Scolor_ = ColorBlend( ColorDem, ColorRGB( i_2, i_2, 80 + i_1 ), ColFacDem ); // ParamColor( "Demand Color", colorGreen  );
    RColor  = IIf( selectedC > Line1, Scolor_, Rcolor_ );
    SColor  = IIf( selectedC > Line4, Scolor_, Rcolor_ );
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?