K-HandChannel_2
Download scriptThis script is designed for the MetaTrader platform to automatically draw price channels on a trading chart based on three specific historical data points. By defining these anchor points, the tool constructs a visual geometric representation of price movement.
Core Logic and Functionality
1. Anchor Point Construction The logic relies on a "three-point" system to build each channel:
- Two Pivot Points: These are identified as the "Left" and "Right" points. They sit on one side of the trend.
- One Opposing Point: This is identified as the "Middle" point, which sits on the opposite side relative to the pivot points. The script automatically determines whether the channel represents a ceiling (upper boundary) or a floor (lower boundary) by comparing the high and low prices of these three specific timestamps.
2. Automated Drawing Mechanics Once the three points are identified, the script performs the following mathematical operations:
- Base Line: It draws a dashed line connecting the two pivot points.
- Extended Line: It calculates the trajectory (slope) between the points to project a third line that aligns with the middle point.
- Median Line: Optionally, it can draw a center line that represents the midpoint between the base and the extended lines.
3. Data Input Methods The script offers two distinct ways to provide information to the chart:
- Manual Entry: Users can input the specific dates and times for the three points directly into the settings panel. This is ideal for creating a single, specific channel.
- File Import: The script can read data from an external text file (named after the currency pair). This allows for "bulk" creation where multiple channels can be loaded at once. Each entry in the file defines a unique set of three points and a specific color.
4. Visual Customization and Management
- Dynamic Coloring: The script supports a wide range of standard colors (e.g., Gold, Lime, Red, Blue) to help distinguish between different channels.
- Point Markers: There is an option to visually display dots at the exact anchor points to make it clear where the data originates.
- Clean Slate Option: The script includes a command to automatically delete all previously drawn channels when new data is loaded or when the indicator is reset, ensuring the chart stays organized.
Summary for the User
Essentially, this tool acts as an automated drafting assistant. Instead of you having to manually click and drag lines to form perfectly symmetrical channels based on historical peaks and valleys, you simply provide the "coordinates" (the times), and the script calculates the geometry and draws the professional-grade channel for you.
//+------------------------------------------------------------------+
//| HandChannel.mq4 |
//| Ðó÷íîå ïîñòðîåíèå êàíàëîâ |
//| |
//| Âåðñèÿ 2. |
//| |
//| Êàíàë ñòðîèòñÿ ïî òð¸ì òî÷êàì: äâå îïîðíûå ñ îäíîé ñòîðîíû è |
//| îäíà ñ äðóãîé. Òåðìèíîëîãèÿ: îïîðíûå òî÷êè - ëåâàÿ è ïðàâàÿ, |
//| ïðîòèâîïîëîæíàÿ òî÷êà - ñðåäíÿÿ. Êîîðäèíàòû êàæäîé òî÷êè - |
//| âðåìÿ. Ïðèâÿçêà ê êîíêðåòíîìó áàðó ïî âðåìåíè íåîáõîäèìà, èíà÷å |
//| ïîëó÷èòå îøèáêó çàäàíèÿ äàííûõ. Öåíû áåðóòñÿ îò óêàçàííûõ áàðîâ. |
//| |
//| Ïàðàìåòðû: |
//| ExtTimeLeft = äàòà ëåâîé òî÷êè |
//| ExtTimeMiddle = äàòà ñðåäíåé òî÷êè |
//| ExtTimeRight = äàòà ïðàâîé òî÷êè |
//| ExtColor = öâåò êàíàëà |
//| ExtGetFromFile = ôëàã ïîëó÷åíèÿ äàííûõ èç ôàéëà |
//| ExtRegetFromFile = ôëàã ïåðå÷èòûâàíèÿ äàííûõ èç ôàéëà |
//| ExtDrawPoints = ðèñîâàòü òî÷êè ïðèâÿçêè |
//| ExtDrawMiddleLine = ðèñîâàòü ñðåäíþþ ëèíèþ êàíàëà |
//| ExtDeleteObject = óäàëÿòü ëèíèè êàíàëà ïðè äåèíèöèàëèçàöèè |
//| |
//| Ïðè ââîäå äàííûõ â ïàíåëè íàñòðîéêè ñòðîèòñÿ îäèí êàíàë. |
//| Ïðè ââîäå äàííûõ èç ôàéëà ìîæíî çàäàòü íåñêîëüêî êàíàëîâ. |
//| |
//| Âñå äàòû çàäàþòñÿ â âèäå: YYYY.MM.DD HH:MM |
//| Íàïðèìåð: 2006.08.12 12:00 |
//| |
//| Äàííûå â ôàéëå: |
//| Èìÿ ôàéëà äîëæíî ñîîòâåòñòâîâàòü âàëþòíîé ïàðå, íàïðèìåð: |
//| USDJPY.TXT, EURUSD.TXT. Ðàñøèðåíèå .TXT îáÿçàòåëüíî. |
//| Ôàéë ðàñïîëàãàåòñÿ â ïîäêàòàëîãå \Experts\Files. |
//| Äàííûå â ôàéëå ðàñïîëàãàþòñÿ ñëåäóþùèì îáðàçîì: |
//| Êàíàë 1 - ëþáîé òåêñò (èñêëþ÷èòåëüíî äëÿ óäîáñòâà) |
//| 2006.08.12 12:00 - äàòà ëåâîé òî÷êè |
//| 2006.08.13 16:00 - äàòà ñðåäíåé òî÷êè |
//| 2006.08.14 08:00 - äàòà ïðàâîé òî÷êè |
//| Blue - öâåò êàíàëà |
//| Äàëåå ìîæåò ñëåäîâàòü åù¸ íåñêîëüêî òàêèõ æå áëîêîâ äëÿ äðóãèõ |
//| êàíàëîâ |
//| Ïîääåðæèâàþòñÿ ñëåäóþùèå öâåòà: |
//| Black, Green, Purple, Maroon, Brown, Orange, Gold, Yellow, Lime, |
//| Aqua, Blue, Magenta, Red, Gray, Silver, Pink, Beige, White. |
//| |
//| Ñîîáùåíèÿ îá îøèáêàõ ÷òåíèÿ äàííûõ âûâîäÿòñÿ â Òåðìèíàëå |
//| â çàêëàäêå Ýêñïåðòû. |
//| |
//| Ïàðàìåòð ExtRegetFromFile ââåä¸í äëÿ òîãî, ÷òîáû äàííûå èç ôàéëà |
//| íå ÷èòàëèñü ïðè êàæäîì òèêå. Ïðè äîáàâëåíèè â ôàéë íîâûõ äàííûõ |
//| ïî íîâîìó êàíàëó èëè óäàëåíèÿ ñòàðûõ íóæíî ïîñòàâèòü äàííîìó |
//| ïàðàìåòðó çíà÷åíèå True, à ïîñëå îáíîâëåíèÿ ñíîâà ïîñòàâèòü |
//| False. Âëàäåëüöû ìîùíûõ êîìïüþòåðîâ ìîãóò ñðàçó ïîñòàâèòü True |
//| è áîëåå íå áåñïîêîèòüñÿ. :-) (õîòÿ ïîñòîÿííîå îáðàùåíèå ê äèñêó |
//| íå åñòü õîðîøî). Ïðè ïåðå÷èòûâàíèè äàííûõ ëèíèè ïîñòðîåííûõ |
//| êàíàëîâ óäàëÿþòñÿ ïðèíóäèòåëüíî è ñòðîÿòñÿ çàíîâî. |
//| |
//| Èçìåíåíèÿ âåðñèè 2: |
//| Öåíû íå çàäàþòñÿ, à áåðóòñÿ îò óêàçàííûõ áàðîâ. |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Karakurt"
#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Yellow
extern datetime ExtTimeLeft = D'2006.08.01 00:00';
extern datetime ExtTimeMiddle = D'2006.08.01 00:00';
extern datetime ExtTimeRight = D'2006.08.01 00:00';
extern color ExtColor = Lime;
extern bool ExtGetFromFile = True;
extern bool ExtRegetFromFile = False;
extern bool ExtDrawPoints = False;
extern bool ExtDrawMiddleLine = False;
extern bool ExtDeleteObject = False;
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle( 0, DRAW_ARROW, EMPTY, 1, indicator_color1 );
SetIndexArrow( 0, 251 );
SetIndexBuffer( 0, ExtMapBuffer1 );
SetIndexEmptyValue( 0, 0.0 );
return ( 0 );
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int deinit()
{
if ( ExtDeleteObject )
DelObj();
return ( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
static bool bGettingFromFile = False;
string sFileName;
string sIdentChannel; // èäåíòèôèêàòîð êàíàëà - äëÿ óäîáñòâà - íè íà ÷òî íå âëèÿåò
int iFileHandle;
int i;
if ( ExtGetFromFile ) { // äàííûå áåðóòñÿ èç ôàéëà
if ( ExtRegetFromFile ) // íàäî ïåðå÷èòûâàòü äàííûå
DelObj(); // ïðèíóäèòåëüíîå óäàëåíèå âñåõ èìåþùèõñÿ êàíàëîâ
else { // íå íàäî ïåðå÷èòûâàòü äàííûå
if ( bGettingFromFile ) // äàííûå óæå áûëè ñ÷èòàíû
return ( 0 );
} // íå íàäî ïåðå÷èòûâàòü äàííûå
sFileName = Symbol() + ".txt";
iFileHandle = FileOpen( sFileName, FILE_READ );
if ( iFileHandle < 1 ) {
Print( "Îøèáêà îòêðûòèÿ ôàéëà " + sFileName + " ", GetLastError() );
return ( -1 );
} // if
for ( i = 1; i < 10; i++ ) {
if ( FileIsEnding( iFileHandle ) ) {
Print( "Êîíåö ôàéëà" );
break;
} // if
//=== Ñ÷èòûâàíèå äàííûõ òåêóùåãî êàíàëà
sIdentChannel = FileReadString( iFileHandle );
ExtTimeLeft = StrToTime( FileReadString( iFileHandle ) );
ExtTimeMiddle = StrToTime( FileReadString( iFileHandle ) );
ExtTimeRight = StrToTime( FileReadString( iFileHandle ) );
ExtColor = StrToColor( FileReadString( iFileHandle ) );
Print( sIdentChannel );
/*Print( "ExtTimeLeft= ", TimeToStr( ExtTimeLeft, TIME_DATE|TIME_MINUTES ) );
Print( "ExtTimeMiddle= ", TimeToStr( ExtTimeMiddle, TIME_DATE|TIME_MINUTES ) );
Print( "ExtTimeRight= ", TimeToStr( ExtTimeRight, TIME_DATE|TIME_MINUTES ) );*/
Print( "Ñ÷èòàí ", i, "-é êàíàë" );
DrawLines(); // ðèñóåòñÿ òåêóùèé êàíàë
} // for
Print( "Ñ÷èòàíî êàíàëîâ: ", i-1 );
bGettingFromFile = True;
FileClose( iFileHandle );
} // if ( äàííûå áåðóòñÿ èç ôàéëà
else { // äàííûå áåðóòñÿ èç íàñòðîéêè
DrawLines(); // êàíàë ðèñóåòñÿ îäèí ðàç
} // else
return ( 0 );
} // int start()
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int DrawLines()
{
int iNumBarLeft; // íîìåð ëåâîãî áàðà
int iNumBarMiddle; // íîìåð ñðåäíåãî áàðà
int iNumBarRight; // íîìåð ïðàâîãî áàðà
double dPriceLeft; // öåíà ëåâîãî áàðà
double dPriceMiddle; // öåíà ñðåäíåãî áàðà
double dPriceRight; // öåíà ïðàâîãî áàðà
double dDrawPriceRight; // öåíû íà îïîðíûõ áàðàõ
double dDrawPriceLeft; // äëÿ ëèíèè ÷åðåç 3-þ òî÷êó
double dPriceDelta; // øàã öåíû îäíîãî áàðà ìåæäó ëåâûì è ïðàâûì áàðàìè
string sNameObject;
//=== Âû÷èñëÿåì íîìåðà áàðîâ
iNumBarLeft = iBarShift( NULL, 0, ExtTimeLeft , True );
iNumBarMiddle = iBarShift( NULL, 0, ExtTimeMiddle, True );
iNumBarRight = iBarShift( NULL, 0, ExtTimeRight , True );
if ( iNumBarLeft < 0 ) { Print( "Îøèáêà çàäàíèÿ ëåâîé äàòû" , ExtTimeLeft ); return ( -1 ); }
if ( iNumBarMiddle < 0 ) { Print( "Îøèáêà çàäàíèÿ ñðåäíåé äàòû", ExtTimeMiddle ); return ( -1 ); }
if ( iNumBarRight < 0 ) { Print( "Îøèáêà çàäàíèÿ ïðàâîé äàòû" , ExtTimeRight ); return ( -1 ); }
if ( High[ iNumBarLeft ] > Low[ iNumBarMiddle ] &&
High[ iNumBarRight ] > Low[ iNumBarMiddle ] ) { // îïîðíàÿ ëèíèÿ ñâåðõó
dPriceLeft = High[ iNumBarLeft ];
dPriceMiddle = Low[ iNumBarMiddle ];
dPriceRight = High[ iNumBarRight ];
} // if ( îïîðíàÿ ëèíèÿ ñâåðõó
if ( Low[ iNumBarLeft ] < High[ iNumBarMiddle ] &&
Low[ iNumBarRight ] < High[ iNumBarMiddle ] ) { // îïîðíàÿ ëèíèÿ ñíèçó
dPriceLeft = Low[ iNumBarLeft ];
dPriceMiddle = High[ iNumBarMiddle ];
dPriceRight = Low[ iNumBarRight ];
} // if ( îïîðíàÿ ëèíèÿ ñíèçó
if ( ExtDrawPoints ) { // ðèñîâàòü òî÷êè ïðèâÿçêè
if ( dPriceLeft > dPriceMiddle ) { // áàçîâàÿ ëèíèÿ ñâåðõó
ExtMapBuffer1[ iNumBarLeft ] = dPriceLeft + 5 * Point;
ExtMapBuffer1[ iNumBarMiddle ] = dPriceMiddle - 5 * Point;
ExtMapBuffer1[ iNumBarRight ] = dPriceRight + 5 * Point;
} // áàçîâàÿ ëèíèÿ ñâåðõó
else { // áàçîâàÿ ëèíèÿ ñíèçó
ExtMapBuffer1[ iNumBarLeft ] = dPriceLeft - 5 * Point;
ExtMapBuffer1[ iNumBarMiddle ] = dPriceMiddle + 5 * Point;
ExtMapBuffer1[ iNumBarRight ] = dPriceRight - 5 * Point;
} // áàçîâàÿ ëèíèÿ ñíèçó
} // if ( ðèñîâàòü òî÷êè ïðèâÿçêè
//=== Âû÷èñëÿåì øàã (åñëè îí ïîëîæèòåëüíûé, òî êàíàë íèñõîäÿùèé)
dPriceDelta = ( dPriceLeft - dPriceRight ) / ( iNumBarLeft - iNumBarRight );
// Êîîðäèíàòû ïî øêàëå öåíû äëÿ ëèíèè ÷åðåç 3-þ òî÷êó
dDrawPriceRight = dPriceMiddle - dPriceDelta * ( iNumBarMiddle - iNumBarRight );
dDrawPriceLeft = dPriceMiddle + dPriceDelta * ( iNumBarLeft - iNumBarMiddle );
//=== Ðèñóåì ëèíèè êàíàëà
// Îïîðíàÿ ëèíèÿ
sNameObject = "BaseLine" + ExtTimeLeft;
ObjectCreate( sNameObject, OBJ_TREND, 0,
ExtTimeLeft , dPriceLeft,
ExtTimeRight, dPriceRight );
ObjectSet( sNameObject, OBJPROP_COLOR, ExtColor );
ObjectSet( sNameObject, OBJPROP_WIDTH, 0 );
ObjectSet( sNameObject, OBJPROP_STYLE, STYLE_DASH );
// Ëèíèÿ ÷åðåç 3-þ òî÷êó
sNameObject = "Line3" + ExtTimeLeft;
ObjectCreate( sNameObject, OBJ_TREND, 0,
ExtTimeLeft , dDrawPriceLeft,
ExtTimeRight, dDrawPriceRight );
ObjectSet( sNameObject, OBJPROP_COLOR, ExtColor );
ObjectSet( sNameObject, OBJPROP_WIDTH, 0 );
ObjectSet( sNameObject, OBJPROP_STYLE, STYLE_DASH );
// Ñðåäíÿÿ ëèíèÿ
if ( ExtDrawMiddleLine ) {
sNameObject = "MidLine" + ExtTimeLeft;
ObjectCreate( sNameObject, OBJ_TREND, 0,
ExtTimeLeft , ( dPriceLeft + dDrawPriceLeft )/2,
ExtTimeRight, ( dPriceRight + dDrawPriceRight )/2 );
ObjectSet( sNameObject, OBJPROP_COLOR, ExtColor );
ObjectSet( sNameObject, OBJPROP_WIDTH, 0 );
ObjectSet( sNameObject, OBJPROP_STYLE, STYLE_DASHDOTDOT );
}
return ( 0 );
} // int DrawLines()
//+------------------------------------------------------------------+
//| Óäàëåíèå îáúåêòîâ.
//+------------------------------------------------------------------+
void DelObj()
{
string sNameObject;
int iiObject;
for ( iiObject = ObjectsTotal(); iiObject >= 0; iiObject-- ) {
sNameObject = ObjectName( iiObject );
if ( StringFind( sNameObject, "BaseLine" ) > -1 ) ObjectDelete ( sNameObject );
if ( StringFind( sNameObject, "Line3" ) > -1 ) ObjectDelete ( sNameObject );
if ( StringFind( sNameObject, "MidLine" ) > -1 ) ObjectDelete ( sNameObject );
} // for
} // void DelObj()
//+------------------------------------------------------------------+
//| Öâåòà
//+------------------------------------------------------------------+
color StrToColor( string sColor )
{
if ( sColor == "Black" ) return ( Black );
else if ( sColor == "Green" ) return ( Green );
else if ( sColor == "Purple" ) return ( Purple );
else if ( sColor == "Maroon" ) return ( Maroon );
else if ( sColor == "Brown" ) return ( Brown );
else if ( sColor == "Orange" ) return ( Orange );
else if ( sColor == "Gold" ) return ( Gold );
else if ( sColor == "Yellow" ) return ( Yellow );
else if ( sColor == "Lime" ) return ( Lime );
else if ( sColor == "Aqua" ) return ( Aqua );
else if ( sColor == "Blue" ) return ( Blue );
else if ( sColor == "Magenta" ) return ( Magenta );
else if ( sColor == "Red" ) return ( Red );
else if ( sColor == "Gray" ) return ( Gray );
else if ( sColor == "Silver" ) return ( Silver );
else if ( sColor == "Pink" ) return ( Pink );
else if ( sColor == "Beige" ) return ( Beige );
else if ( sColor == "White" ) return ( White );
else
return ( Lime );
} // color StrToColor()
Comments