/////////////////////////////////////////////////////////////////////
// ScottyBFib.mq4
// Version 1.0
// Copyright © 2008 Scotty B Guru (ScottyB)
// lifesaved@gmail.com
// http://www.metaquotes.net
/////////////////////////////////////////////////////////////////////
#property copyright "Copyright © 2008, Scotty B (ScottyB)"
#property link      "http://www.metaquotes.net"
#property show_inputs

/////////////////////////////////////////////////////////////////////
// Description
/////////////////////////////////////////////////////////////////////
/*

This script provides the most practical fib ratios for trading.

*/ 

/////////////////////////////////////////////////////////////////////
// User Inputs
/////////////////////////////////////////////////////////////////////
/*


ShowAsRay - Flag to indicate whether the "Ray" setting is set on the fib set. The Ray setting if on makes the fibs extend all the way to the right side of the screen. This can be toggled later via the fib properties.

ShowPivot - Flag to indicate whether to show the pivot (50% level).

FibWidth - Specifies the width of the fib lines.  Must be from 1 to 4.

Fibo_Name - String that describes this fib set. This will be displayed on each level of the fib. It is also used to make the unique name of the fib set in the objects list. Fib object names are derived from Fibo_Num, Fibo_Name, and TimeFrame. 

ShowName - Flag to indicate whether the name given to the fib should be shown on each level.

Fibo_color - The color to draw the fib set.


Cust_Time - This variable indicates the date/time that should be used as a starting point for the fib set. If not changed, then the current date/time is used. This setting is useful for testing in the strategy tester to put the fibs on a particular date.

The format of this field is: "yyyy.mm.dd hh:mm"

For example, to set the starting date to Sep 24, 2007 at 00:00 server time, then set this field to:

2007.09.24 00:00
                
*/                


// User inputs
extern bool ShowAsRay = true;  // Set the ray flag on the fibos
extern bool ShowPivot = true; // Show the pivot level (50%)
extern int FibWidth = 1; // Width of the fib levels (has to be at least 1, max 4)
extern string Fibo_Name = "SB FIB";
extern bool ShowName = true; // Shows name of fib on each level
extern color Fibo_color = DodgerBlue;
extern datetime Cust_Time = 0;

double HiPrice, LoPrice;
datetime StartTime;
int Tfmin = PERIOD_H1;
string Object_Name = "";


/////////////////////////////////////////////////////////////////////
// Script initialization function
/////////////////////////////////////////////////////////////////////

int init() {
    Object_Name = "SB_PULL_FIBO_" + Fibo_Name;
    Tfmin = Period();

    if ( FibWidth < 1 || FibWidth > 4 ) {
        FibWidth = 1;
    }
    
    return( 0 );
}

/////////////////////////////////////////////////////////////////////
// Custom indicator deinitialization function
/////////////////////////////////////////////////////////////////////

int deinit() {
    return( 0 );
}

/////////////////////////////////////////////////////////////////////
// Indicator Logic run on every tick
/////////////////////////////////////////////////////////////////////

int start() {
    if ( ObjectFind( Object_Name ) != -1 ) {
        Print( "Fibos already drawn with that name/unique number." );
        return( -1 );
    }
    
	if ( Cust_Time == 0 ) {
        StartTime = Time[0];
    }
    else {
        StartTime = Cust_Time;
    }
	int shift	= iBarShift( NULL, Tfmin, StartTime ) + 1;	// yesterday
	HiPrice		= iHigh(NULL, Tfmin, shift);
	LoPrice		= iLow (NULL, Tfmin, shift);

	if( TimeDayOfWeek( StartTime ) == 0/*Sunday*/)
	{
		HiPrice = MathMax( HiPrice, iHigh( NULL, Tfmin, shift + 1 ) );
		LoPrice = MathMin( LoPrice, iLow( NULL, Tfmin, shift + 1 ) );
	}
    
	DrawFibo();
	
    return( -1 );
}

/////////////////////////////////////////////////////////////////////
// DrawFibo()
/////////////////////////////////////////////////////////////////////

int DrawFibo()
{
	if ( ObjectFind( Object_Name ) == -1 ) {
		ObjectCreate( Object_Name, OBJ_FIBO, 0, StartTime, HiPrice, StartTime, LoPrice );

        ObjectSet( Object_Name, OBJPROP_LEVELCOLOR, Fibo_color );
        ObjectSet( Object_Name, OBJPROP_LEVELWIDTH, FibWidth );
        
        int fiblevels = 10;
        if ( !ShowPivot ) {
            fiblevels--;
        }
        int fibctr = 0;
        
        string TheName = Fibo_Name + " ";
        if ( !ShowName ) {
            TheName = "";
        }
        
        ObjectSet( Object_Name, OBJPROP_FIBOLEVELS, fiblevels );

        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, -0.382 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "T1 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.0 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "0/100 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.236 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "23.6 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.382 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "38.2 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.5 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "50.0 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.618);
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "61.8 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 0.764 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "76.4 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 1.0 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "0/100 - %$");
        fibctr++;
        
        ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 1.382 );
        ObjectSetFiboDescription( Object_Name, fibctr, TheName + "T1 - %$");
        fibctr++;
        
          
        if ( ShowPivot ) {
            ObjectSet( Object_Name, OBJPROP_FIRSTLEVEL + fibctr, 1.0 );
            ObjectSetFiboDescription( Object_Name, fibctr, TheName + "0/100 - %$");
            fibctr++;
        
        }

        ObjectSet( Object_Name, OBJPROP_RAY, ShowAsRay );
        ObjectSet( Object_Name, OBJPROP_BACK, true);
    }
}