HistoryBarsEditor

Author: Navoykov Alexey aka meat
Miscellaneous
It issuies visual alerts to the screenIt opens Message Boxes to the user
0 Views
0 Downloads
0 Favorites
HistoryBarsEditor
//+------------------------------------------------------------------+
//|                                            HistoryBarsEditor.mq4 | v.1.0
//+------------------------------------------------------------------+
#property copyright "Navoykov Alexey aka meat"
#property link      "nalx@yandex.ru"

#import "user32.dll"
  int  SendMessageA(int hWnd,int Msg,int wParam,int lParam);
  int  PostMessageA(int hWnd,int Msg,int wParam,int lParam);
  bool PeekMessageA(int lpMsg[],int hWnd,int wMsgFilterMin,int wMsgFilterMax,int wRemoveMsg);
  int  RegisterWindowMessageA(string lpString);
  int  FindWindowExA(int hwndParent, int hwndChildAfter, string lpClassName, string lpWindowName);
  bool IsWindow(int hWnd);
  int  GetParent(int hWnd);
  int  GetAncestor(int hwnd, int gaFlags);
  int  GetLastActivePopup(int hWnd);
  int  GetDlgItem(int hDlg, int nIDDlgItem);
  int  GetForegroundWindow();
  int  SetForegroundWindow(int hWnd);
  int  GetClassNameA(int hWnd,string lpString,int nMaxCount);
  bool GetWindowRect(int hWnd,int lpRect[]);
  bool GetCursorPos(int lpPoint[]);
  int  GetAsyncKeyState(int vKey);
  int  CreateWindowExA(int dwExStyle,string lpClassName,string lpWindowName,int dwStyle,int x,int y,int nWidth,int nHeight,
                       int hWndParent,int hMenu,int hInstance,int lpParam);
  int  CreatePopupMenu();
  bool DestroyMenu(int hMenu);
  bool AppendMenuA(int hMenu,int uFlags,int uIDNewItem,string lpNewItem);
  bool CheckMenuItem(int hmenu,int uIDCheckItem,int uCheck);
  int  TrackPopupMenuEx(int hMenu,int fuFlags,int x,int y,int hWnd,int lptpm);
  
  int  CharPrevA(string lpszStart,string lpszCurrent); // èñïîëüçóåì äëÿ ïîëó÷åíèÿ àäðåñà ñòðîêè
  int  CharPrevW(int lpszStart[],int lpszCurrent[]);   // èñïîëüçóåì äëÿ ïîëó÷åíèÿ àäðåñà ìàññèâà öåëûõ ÷èñåë
#import

#include <WinUser32.mqh>

#define TVM_GETCOUNT       4357
#define TVM_GETNEXTITEM    4362
#define TVM_SELECTITEM     4363
#define TVM_GETITEM        4364
#define TVGN_ROOT          0
#define TVGN_NEXT          1
#define TVGN_CHILD         4 	
#define TVGN_CARET         9
#define TVIF_TEXT          1	
  
#define LVM_GETITEMCOUNT   4100
#define LVM_GETITEMTEXT    4141
#define LVM_ENSUREVISIBLE  4115
#define LVM_SETITEMSTATE   4139
#define LVIS_SELECTED      2
#define LVIS_FOCUSED       1

#define DTM_SETSYSTEMTIME  4098

#define TPM_NONOTIFY       0x080
#define TPM_RETURNCMD      0x100

#define BM_CLICK           245
#define VK_LBUTTON         0x01
#define VK_LSHIFT          0xA0
#define VK_RSHIFT          0xA1

#define MF_SEPARATOR 0x00000800
#define MF_GRAYED    0x00000001
#define MF_POPUP     0x00000010
#define MF_CHECKED   0x00000008

#define WS_VISIBLE   0x10000000
#define WS_CHILD     0x40000000

//---- Ìîè êîíñòàíòû ---------
#define Key_PressDown    32768
#define Key_Clicked      1

#define ID_DELETE        1
#define ID_EDIT          2
#define ID_EDITVISUAL    3
#define ID_INSERT        4
#define ID_CANCEL        5
#define ID_LBUTTON      20
#define ID_LBUTTONSHIFT 21


string textbuffer="Àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ";

string BarParameterNames[5]={ "Open:   ","High:    ","Low:     ","Close:   ","Volume: " };
int    BarParameterEditId[5]={ 0x4BF,     0x485,      0x4A2,      0x411,      0x568 };

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{ 
  if (!IsDllsAllowed()) { Alert("Íåîáõîäèìî ðàçðåøèòü DLL â íàñòðîéêàõ!"); return(0); }
  int    hChart=WindowHandle(Symbol(),Period());
  int    hTerminal=GetAncestor(hChart,3);
  string DeletedBarParametersString[5];
  int    DeletedBarTime=0;
  string BarParamStr[5];
  int    UsingButton_ID=ID_LBUTTON;
  int    Time_Dropped=WindowTimeOnDropped();

  while(!IsStopped())
  { 
    Sleep(10);
    RefreshRates();
    Comment(WindowExpertName()," ðàáîòàåò");  
    int CursorPoint[2];
    GetCursorPos(CursorPoint);
    int x=CursorPoint[0];
    int y=CursorPoint[1];
   
    int bar;
    if (Time_Dropped>0)
    {
      bar=iBarShift(NULL,0,Time_Dropped);
      if (bar<0) { Alert("Íåâåðíûé áàð"); return(0); }
    }
    else
    {
      if (CheckForPressedKey(VK_LBUTTON)!=Key_Clicked|Key_PressDown) continue;
      if (UsingButton_ID==ID_LBUTTONSHIFT)
        if (CheckForPressedKey(VK_LSHIFT)!=Key_PressDown)
          if (CheckForPressedKey(VK_RSHIFT)!=Key_PressDown)
            continue;
      bar=GetBarFromCursor(x,y);
      if (bar<0) continue;
    }
    datetime bartime=Time[bar];
    BarParamStr[0]=DoubleToStr(Open[bar],Digits);
    BarParamStr[1]=DoubleToStr(High[bar],Digits);
    BarParamStr[2]=DoubleToStr(Low[bar],Digits);
    BarParamStr[3]=DoubleToStr(Close[bar],Digits);
    BarParamStr[4]=DoubleToStr(Volume[bar],0);
    
    int hMyWnd=CreateWindowExA(0,"Static","MyWindow",0, 1,1, 5,5, hTerminal,0,0, 0);
    if (hMyWnd==0) { Print("Íå óäàëîñü ñîçäàòü îêíî"); continue; }
    
    int hMenu=CreatePopupMenu();
    if (hMenu==0) { DestroyWindow(hMyWnd); continue; }
    AppendMenuA(hMenu,0,ID_EDIT,"Ðåäàêòèðîâàòü áàð");
    AppendMenuA(hMenu,0,ID_EDITVISUAL,"Ðåäàêòèðîâàòü âèçóàëüíî");
    AppendMenuA(hMenu,0,ID_DELETE,"Óäàëèòü áàð");
    int insertbar_style= 0 | MF_GRAYED*(Time[bar]-Time[bar+1]==Period()*60);   // åñëè ìåæäó áàðàìè íåò äûðû, òî äåàêòèâèðóåì ôóíêöèþ âñòàâêè íîâîãî áàðà
    if (DeletedBarTime>0)
    AppendMenuA(hMenu,0,ID_CANCEL,"Îòìåíèòü óäàëåíèå");
    AppendMenuA(hMenu,insertbar_style,ID_INSERT,"Âñòàâèòü áàð");
    AppendMenuA(hMenu,MF_SEPARATOR,6,"");
    AppendMenuA(hMenu,MF_GRAYED,7,"Date:     "+TimeToStr(Time[bar],TIME_DATE));
    AppendMenuA(hMenu,MF_GRAYED,8,"Time:     "+TimeToStr(Time[bar],TIME_MINUTES)); 
    for (int p=0; p<5; p++)
      AppendMenuA(hMenu,MF_GRAYED,9+p,BarParameterNames[p]+" "+BarParamStr[p]);
    AppendMenuA(hMenu,MF_SEPARATOR,15,"");
   
    int hMenuKeys=CreatePopupMenu();
    AppendMenuA(hMenu,MF_POPUP,hMenuKeys,"Íàñòðîéêà êëàâèøè");
    AppendMenuA(hMenuKeys,0,ID_LBUTTON,"LeftMouseButton");
    AppendMenuA(hMenuKeys,0,ID_LBUTTONSHIFT,"Shift+LeftMouseButton");
    CheckMenuItem(hMenuKeys,UsingButton_ID,MF_CHECKED);
    
    SetForegroundWindow(hMyWnd);
    EnableWindow(hChart,0);  // áëîêèðóåì îêíî ãðàôèêà
    
    int select=TrackPopupMenuEx(hMenu,TPM_NONOTIFY|TPM_RETURNCMD,x,y,hMyWnd,0);
    
    if (select==0) Sleep(150);  // æä¸ì ïàóçó, ÷òîáû íîâûå ñîáûòèÿ ìûøè íå óñïåëè ïåðåäàòüñÿ ãðàôèêó
    EnableWindow(hChart,1); // ðàçáëîêèðóåì îêíî ãðàôèêà
    SendMessageA(hChart,WM_LBUTTONUP,0,0);  // îòïóñêàåì êíîïêó ìûøè, åñëè îíà áûëà íàæàòà
    
    SetForegroundWindow(hTerminal);
    
    DestroyMenu(hMenu);
    DestroyMenu(hMenuKeys);
    DestroyWindow(hMyWnd);
    
    bool autoedit=0;
    
    switch (select)
    {
      case ID_LBUTTON:
      case ID_LBUTTONSHIFT:  UsingButton_ID=select; continue;
      
      case ID_INSERT:        bartime-=Period()*60; break;
      
      case ID_CANCEL:        select=ID_INSERT;
                             bartime=DeletedBarTime;
                             ArrayCopy(BarParamStr,DeletedBarParametersString);
                             autoedit=true;
                             break;
                             
      case ID_EDITVISUAL:    if (VisualEditBar(bar,BarParamStr)==true)
                               { select=ID_EDIT; autoedit=true; }
                               else select=0;
                             SendMessageA(hChart,WM_LBUTTONUP,0,0);
    }
    if (IsStopped()) break;   
    
    if (select==0)
      if (!Time_Dropped) continue;
       else break;
       
    int result=ModifyHistory(bartime,BarParamStr,select,autoedit);
    SetForegroundWindow(hTerminal);
    if (result==false)
      if (!Time_Dropped) continue;
       else break;
    string txt=TimeToStr(bartime)+"  "; 
    if (autoedit || select==ID_DELETE)
      for (int i=0; i<5; i++) 
        txt=txt+StringTrimRight(BarParameterNames[i])+" "+BarParamStr[i]+"  ";
    DeletedBarTime=0;

    if (select==ID_DELETE)
    {
      Sleep(300); RefreshRates();
      if (iBarShift(NULL,0,bartime,true)>=0 && !Time_Dropped) continue;
      Print("Óäàë¸í áàð:  ",txt);
      ArrayCopy(DeletedBarParametersString,BarParamStr);
      DeletedBarTime=bartime;
    }
    if (select==ID_EDIT)
      if (DoubleToStr(Open[bar],Digits)!=BarParamStr[0] || DoubleToStr(High[bar],Digits)!=BarParamStr[1]
       || DoubleToStr(Low[bar],Digits)!=BarParamStr[2] || DoubleToStr(Close[bar],Digits)!=BarParamStr[3] || DoubleToStr(Volume[bar],0)!=BarParamStr[4])
        Print("Èçìåí¸í áàð:  ",txt);
    if (select==ID_INSERT)
      Print("Âñòàâëåí áàð:  ",txt);
    
    if (Time_Dropped>0) break;
  }

  Comment("");
}
  
//------- Ïîëó÷åíèå áàðà ïîä êóðñîðîì -----------------------

int GetBarFromCursor(int x, int y)
{
  int hChart=WindowHandle(Symbol(),Period());
  int WindowRect[4];
  GetWindowRect(hChart,WindowRect);
  int WindowSizeX=WindowRect[2]-WindowRect[0];
  int WindowSizeY=WindowRect[3]-WindowRect[1];
  int ChartX[2], ChartY[2];
  ChartX[0]=WindowRect[0]+5;
  ChartX[1]=WindowRect[2]-43;
  ChartY[0]=WindowRect[1]+5;
  ChartY[1]=WindowRect[3]-21;
  if (x<ChartX[0] || x>ChartX[1]) return(-1);
  if (y<ChartY[0] || y>ChartY[1]) return(-1);
  int ChartSizeX=ChartX[1]-ChartX[0];
  int ChartSizeY=ChartY[1]-ChartY[0];
  int bars=WindowBarsPerChart();
  if (bars==0) return(-1);
  int barsize=ChartSizeX/bars;
  barsize=MathPow(2,MathFloor(MathLog(barsize)/MathLog(2)));  // ïðèâîäèì ê ðåàëüíîìó ðàçìåðó áàðà:  áëèæàéøåìó ìåíüøåìó, êðàòíîìó 2  (1, 2, 4, 8, 16, 32)
  int firstbar=WindowFirstVisibleBar();
  int bar=firstbar-MathRound(1.0*(x-ChartX[0])/barsize);  // äîáàâëÿåì 0.5, ò.ê. firstbar (ëåâûé áàð) âñåãäà âèäåí íàïîëîâèíó
  if (WindowsTotal()==1 && bar>=0)
  {
    double WindowPriceSize=WindowPriceMax(0)-WindowPriceMin(0);
    int BarY_high=ChartY[1]-(High[bar]-WindowPriceMin(0))/WindowPriceSize*ChartSizeY;
    int BarY_low=ChartY[1]-(Low[bar]-WindowPriceMin(0))/WindowPriceSize*ChartSizeY;
    if (y<BarY_high || y>BarY_low) return(-1);
  }
  return(bar);
}

//------- Ïðîâåðêà íàæàòèÿ êëàâèøè ----------------------------------------------------

int CheckForPressedKey(int VK_code)
{      
  static bool IsWindowActive=0;
  if (!IsDllsAllowed()) return(0);
  int h=GetParent(WindowHandle(Symbol(),Period()));
  if (GetParent(GetParent(h))!=GetForegroundWindow())  { IsWindowActive=false; return(0); }
  if (GetWindow(h,GW_HWNDFIRST)!=h) { IsWindowActive=false; return(0); }
  if (!IsWindowActive) GetAsyncKeyState(VK_code);
  IsWindowActive=true;
  int status=GetAsyncKeyState(VK_code)&0xFFFF;  // îáíóëÿåì ñòàðøèå áàéòû, ò.ê. ìû ïîëó÷èëè 16-áèòíîå ÷èñëî, êîòîðîå ìîãëî áûòü îòðèöàòåëüíûì è ïðåîáðàçîâàííûì â 32 áèòà ñ çàïîëíåíèåì ñòàðøèõ áàéòîâ
  if (status!=0) { IsWindowActive=0; return(status); }
  return(0);
}

//-------------- Èçìåíåíèå èñòîðèè êîòèðîâîê ----------------------------------------
  
int ModifyHistory(int time, string BarParamStrings[], int action,  bool autoedit=0)
{  
  string TimeString=TimeToStr(time);

  int hChart=WindowHandle(Symbol(),Period());
  int hTerminal=GetAncestor(hChart,3);
  int hArchiveWindow=OpenArchiveWindow(hTerminal);
  if (hArchiveWindow==0) { Alert("Íå íàéäåíî îêíî àðõèâà êîòèðîâîê"); return(0); }
  //CloseWindow(hArchiveWindow);  // ñâ¸ðòûâàåì îêíî â çíà÷îê
  SetWindowPos(hArchiveWindow,0,0,0,0,0,0x80|0x10|0x02|0x01); // SWP_HIDEWINDOW 0x80 | SWP_NOACTIVATE 0x10 | SWP_NOMOVE 0x02 | SWP_NOSIZE 0x01
  
  int hTreeView=GetDlgItem(hArchiveWindow,0x546);
  
  int hSymbol=SelectSymbol(hTreeView,Symbol());
  if (hSymbol==0) { Alert("Òåêóùèé èíñòðóìåíò íå íàéäåí â àðõèâå êîòèðîâîê"); return(0); }
  
  if (!SelectTimeframe(hTreeView,hSymbol,Period()))
    { Alert("Òåêóùèé òàéìôðåéì íå íàéäåí â àðõèâå êîòèðîâîê"); return(0); } 
  
  int hListView=GetDlgItem(hArchiveWindow,0x4F4);
  
  string FindBarParameterStrings[5];
  if (action!=ID_INSERT)
    if (!FindBar(hListView,TimeString,FindBarParameterStrings))
      { Alert("Âûáðàííûé áàð íå íàéäåí â àðõèâå êîòèðîâîê!"); return(0); }

  if (action==ID_DELETE)
  { //---Ïðîâåðêà ñîîòâåòñòâèÿ ïàðàìåòðîâ íàéäåííîãî áàðà ïàðàìåòðàì èñêîìîãî áàðà 
    string txt="";
    bool ok=true;
    for (int i=0; i<5; i++)
    {
      txt=txt+"\n"+BarParameterNames[i]+":\t"+BarParamStrings[i]+"\t"+FindBarParameterStrings[i];
      if (FindBarParameterStrings[i]!=BarParamStrings[i]) ok=false;
    }   
    if (!ok)
     if (MessageBox("Ïàðàìåòðû áàðà îòëè÷àþòñÿ!\n\tÃðàôèê\tÀðõèâ"+txt+"\n\nÂñ¸ ðàâíî óäàëèòü?",0,MB_YESNO)!=IDYES)
       action=0;
  }
  int IdButton;
  if (action==ID_EDIT)   IdButton=0x459; // ïðàâêà
  if (action==ID_DELETE) IdButton=0x44C; // óäàëèòü
  if (action==ID_INSERT) IdButton=0x3EB; // âñòàâèòü
  int hButton=GetDlgItem(hArchiveWindow,IdButton);
  if (action>0)
    if (hButton>0) PostMessageA(hButton,BM_CLICK,0,0);
     else { action=0; Alert("Íå íàéäåíà êíîïêà Id=",IdButton);  }
    
  PostMessageA(hArchiveWindow,WM_CLOSE,0,0); // çàêðûòèå îêíà àðõèâà êîòèðîâîê  
  if (action==0) return(0);
  //EnableWindow(hTerminal,true);
  
  if (action==ID_INSERT || action==ID_EDIT)
  {
    Sleep(200);
    int hPopup=GetLastActivePopup(hTerminal);
    GetWindowTextA(hPopup,textbuffer,255);
    int hDatetime=GetDlgItem(hPopup,0x445);
    GetClassNameA(hDatetime,textbuffer,255);
    if (textbuffer!="SysDateTimePick32") { Alert("Íå íàéäåíî îêíî ðåäàêòèðîâàíèÿ áàðà"); return(0); }
  }  
  if (action==ID_INSERT || (action==ID_EDIT && autoedit))
  {  
    int SystemTimeStruct[4];  // ñòðóêòóðà ñîñòîèò èç 8 ïàðàìåòðîâ òèïà WORD(2 áàéòà), ìû ïîäìåíÿåì å¸ ÷åòûðüìÿ 4-áàéòîâûìè ïàðàìåòðàìè 
    SystemTimeStruct[0]=TimeYear(time)+TimeMonth(time)<<16;
    SystemTimeStruct[1]=TimeDayOfWeek(time)+TimeDay(time)<<16;
    SystemTimeStruct[2]=TimeHour(time)+TimeMinute(time)<<16;
    SystemTimeStruct[3]=0; // Seconds | Milliseconds<<16
    int SystemTimeStruct_addr=CharPrevW(SystemTimeStruct,SystemTimeStruct);
    if (SendMessageA(hDatetime, DTM_SETSYSTEMTIME,0,SystemTimeStruct_addr)==0)
      { Alert("Íå óäàëîñü çàäàòü âðåìÿ áàðà"); return(0); }
      
    for (int p=0; p<5; p++)
    {
      int hEdit=GetDlgItem(hPopup,BarParameterEditId[p]);
      string textaddr=CharPrevA(BarParamStrings[p],BarParamStrings[p]);
      SendMessageA(hEdit,WM_SETTEXT,0,textaddr);
    }
    if (autoedit)
    {
      int hOk=GetDlgItem(hPopup,0x01);
      SendMessageA(hOk,BM_CLICK,0,0);
    }
  }
  if (action==ID_EDIT || action==ID_INSERT)
    while (IsWindow(hPopup))
      if (IsStopped()) { SendMessageA(hPopup,WM_CLOSE,0,0); return(0); }
        else Sleep(50);
  
  Sleep(100);
  int MT4InternalMsg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
  PostMessageA(hChart,MT4InternalMsg, 2, 1);  //ýìóëÿöèÿ òèêà
  
  UpdateWindow(hTerminal);
  
  return(true);
}

//---- Îòêðûòèå îêíà àðõèâà êîòèðîâîê ----

int OpenArchiveWindow(int hTerminal)
{
  string null_string;
  bool postmessage=0;
  for (int startticks=GetTickCount(); GetTickCount()-startticks<1000; )
  { 
    int h=FindWindowExA(0,h,"#32770",null_string);
    if (h==0 && !postmessage)
    { 
      PostMessageA(hTerminal,WM_COMMAND,33262,0); Sleep(100);
      postmessage=true; continue;
    }
    if (h==0) Sleep(10);
    if (GetAncestor(h,3)!=hTerminal) continue;
    if (FindWindowExA(h,0,"SysTreeView32","Tree1")>0) return(h);
  }
  return(0);
}

//---- Ïîèñê è âûáîð çàäàííîãî èíñòðóìåíòà â äðåâîâèäíîì ñïèñêå ---- 

int SelectSymbol(int hTreeView,string symbol)
{ 
  int count=SendMessageA(hTreeView,TVM_GETCOUNT,0,0);
  int TVITEM[10];
  int TVITEM_address=CharPrevW(TVITEM,TVITEM);  // ïîëó÷àåì àäðåñ ìàññèâà    // Dll_GetAddressOfInteger(TVITEM);
  int textbuffer_address=CharPrevA(textbuffer,textbuffer); // ïîëó÷àåì àäðåñ òåêñòîâîãî áóôåðà  // Dll_GetAddressOfString(textbuffer);
  int hRoot=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_ROOT,0);
  int hGroup=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_CHILD,hRoot);
  int hSymbol=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_CHILD,hGroup);
  int Count=0;
  
  while (hGroup>0)
  {
    if (hSymbol==0)
    {
      hGroup=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_NEXT,hGroup);
      hSymbol=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_CHILD,hGroup);
      continue;
    }
    TVITEM[0]=TVIF_TEXT;
    TVITEM[1]=hSymbol;
    TVITEM[4]=textbuffer_address;
    TVITEM[5]=255;
    bool result=SendMessageA(hTreeView,TVM_GETITEM,0,TVITEM_address);
    if (result>0) Count++;
    if (textbuffer==symbol) break;
    hSymbol=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_NEXT,hSymbol);
  }
  if (hGroup==0) return(0);
  
  SendMessageA(hTreeView,TVM_SELECTITEM,TVGN_CARET,hSymbol);
  SendMessageA(hTreeView,WM_LBUTTONDBLCLK,0,0);
  return(hSymbol);
}
  
//---- Âûáîð íóæíîãî òàéìôðåéìà ----

bool SelectTimeframe(int hTreeView,int hSymbol,int period)
{
  int hPeriod=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_CHILD,hSymbol);

  int Periods[]={ PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1 };
  string PeriodNames[]={ "M1",  "M5",  "M15",  "M30",  "H1",  "H4",  "Daily",  "Weekly",  "Monthly" };
  for (int i=0; i<ArraySize(Periods); i++)
  {
    if (period==Periods[i]) break;
    hPeriod=SendMessageA(hTreeView,TVM_GETNEXTITEM,TVGN_NEXT,hPeriod);
  }
  if (hPeriod==0) return(0);
  
  SendMessageA(hTreeView,TVM_SELECTITEM,TVGN_CARET,hPeriod);
  SendMessageA(hTreeView,WM_LBUTTONDBLCLK,0,0);
  GetWindowTextA(GetParent(hTreeView),textbuffer,255);
  if (StringFind(textbuffer,PeriodNames[i])<0) return(0);
  return(true);
}
  
//---- Ïîèñê è âûäåëåíèå áàðà ñ òðåáóåìûì âðåìåíåì â ñïèñêå ----

bool FindBar(int hListView, string timestring, string& valuestring[])
{
  int LVITEM[10];
  int LVITEM_address=CharPrevW(LVITEM,LVITEM); // ïîëó÷àåì àäðåñ ìàññèâà 
  LVITEM[2]=0; // subitem
  LVITEM[5]=CharPrevA(textbuffer,textbuffer); // ïîëó÷àåì àäðåñ òåêñòîâîãî áóôåðà 
  LVITEM[6]=255; // textmask

  int ItemsCount=SendMessageA(hListView,LVM_GETITEMCOUNT,0,0);
  
  for (int i=0; i<ItemsCount; i++)
  {
    if (SendMessageA(hListView,LVM_GETITEMTEXT,i,LVITEM_address)==0) continue;
    if (textbuffer==timestring) break;
  }
  if (i==ItemsCount) return(0);
  
  //---- Ïîëó÷åíèå çíà÷åíèé öåí è îáú¸ìà âûáðàííîãî áàðà ----
  for (int j=0; j<ArraySize(valuestring); j++)
  {
    LVITEM[2]=j+1; // subitem
    if (SendMessageA(hListView,LVM_GETITEMTEXT,i,LVITEM_address)>0)
      valuestring[j]=textbuffer+"";
    else valuestring[j]="";
  }
  // âûäåëÿåì íàéäåííûé ýëåìåíò è äåëàåì åãî âèäèìûì
  LVITEM[3]=LVIS_FOCUSED|LVIS_SELECTED; // state
  LVITEM[4]=LVIS_FOCUSED|LVIS_SELECTED; // statemask
  SendMessageA(hListView,LVM_SETITEMSTATE,i,LVITEM_address);
  SendMessageA(hListView,LVM_ENSUREVISIBLE,i,0);
  
  return(true);
}

//------- Âèçóàëüíîå ðåäàêòèðîâàíèå áàðà --------------------------------

bool VisualEditBar(int bar, string& ParamStrings[])
{
  int time=Time[bar];
  string vline="BarVLine";
  string arrowopen="BarArrowOpen";
  string arrowclose="BarArrowClose";
  string texthigh="BarTextHigh";
  string textlow="BarTextLow";
  ObjectDelete(vline);
  ObjectDelete(arrowopen);
  ObjectDelete(arrowclose);
  ObjectCreate(vline,OBJ_TREND,0,time,High[bar],time,Low[bar]);
  ObjectSet(vline,OBJPROP_RAY,0);
  ObjectSet(vline,OBJPROP_WIDTH,2);
  ObjectCreate(arrowopen,OBJ_ARROW,0,time,Open[bar]);
  ObjectSet(arrowopen,OBJPROP_ARROWCODE,5);
  ObjectCreate(arrowclose,OBJ_ARROW,0,time,Close[bar]);
  ObjectSet(arrowclose,OBJPROP_ARROWCODE,6);
  ObjectCreate(texthigh,OBJ_TEXT,0,0,0);
  ObjectCreate(textlow,OBJ_TEXT,0,0,0);
  double lastpriceopen, lastpriceclose, lastpricehigh, lastpricelow;
  int    lasttimeopen,  lasttimeclose,  lasttimehigh,  lasttimelow;
  
  int hChart=WindowHandle(Symbol(),Period());
  int hButtonOK=CreateWindowExA(0,"Button","Ñîõðàíèòü",WS_VISIBLE|WS_CHILD, 5,30,100,30, hChart,0,0, 0);
  int hButtonCancel=CreateWindowExA(0,"Button","Îòìåíèòü",WS_VISIBLE|WS_CHILD, 5,70,100,30, hChart,0,0, 0);
  bool result=0;
  while (!IsStopped())
  {
    if (ObjectFind(arrowopen)<0 || ObjectFind(arrowclose)<0 ||  ObjectFind(vline)<0) break;
    double priceopen=NormalizeDouble(ObjectGet(arrowopen,OBJPROP_PRICE1),Digits);
    double priceclose=NormalizeDouble(ObjectGet(arrowclose,OBJPROP_PRICE1),Digits);
    double vlineprice1=NormalizeDouble(ObjectGet(vline,OBJPROP_PRICE1),Digits);
    double vlineprice2=NormalizeDouble(ObjectGet(vline,OBJPROP_PRICE2),Digits);
    double pricehigh=MathMax(vlineprice1,vlineprice2);
    double pricelow=MathMin(vlineprice1,vlineprice2);
    
    if (ObjectSet(arrowopen,OBJPROP_TIME1,time)==true)   // çíà÷îê íå ïåðåòàñêèâàåòñÿ
      priceopen=MathMax(pricelow,MathMin(pricehigh,priceopen));
    if (ObjectSet(arrowclose,OBJPROP_TIME1,time)==true)  // çíà÷îê íå ïåðåòàñêèâàåòñÿ
      priceclose=MathMax(pricelow,MathMin(pricehigh,priceclose));
    if (ObjectSet(vline,OBJPROP_TIME1,time)==true)       // ëèíèÿ íå ïåðåòàñêèâàåòñÿ
    {
      pricehigh=MathMax(pricehigh,MathMax(priceopen,priceclose));
      pricelow=MathMin(pricelow,MathMin(priceopen,priceclose));
    }
    else
    if (NormalizeDouble(pricehigh-pricelow-(lastpricehigh-lastpricelow),Digits)==0)  // ëèíèÿ ïåðåòàñêèâàåòñÿ
    {
      priceopen=lastpriceopen+ pricehigh-lastpricehigh;
      priceclose=lastpriceclose+ pricehigh-lastpricehigh;
    } 
    double windowpricesize=WindowPriceMax()-WindowPriceMin();
    ObjectMove(arrowopen,0,time,priceopen);
    ObjectMove(arrowclose,0,time,priceclose);
    ObjectMove(vline,0,time,pricehigh);
    ObjectMove(vline,1,time,pricelow);
    ObjectMove(texthigh,0,time,pricehigh+windowpricesize*0.04);
    ObjectMove(textlow,0,time,pricelow-windowpricesize*0.02);
    ObjectSetText(texthigh,DoubleToStr(pricehigh,Digits),8,"Tahoma",Red);
    ObjectSetText(textlow,DoubleToStr(pricelow,Digits),8,"Tahoma",Red);
    
    int timeopen=ObjectGet(arrowopen,OBJPROP_TIME1);
    int timeclose=ObjectGet(arrowclose,OBJPROP_TIME1);
    int timehigh=ObjectGet(vline,OBJPROP_TIME1);
    int timelow=ObjectGet(vline,OBJPROP_TIME2);
    
    int MSG[7];
    if (PeekMessageA(MSG,hButtonOK,WM_LBUTTONDOWN,WM_LBUTTONUP,1)!=0)
     { result=true; break; }
    if (PeekMessageA(MSG,hButtonCancel,WM_LBUTTONDOWN,WM_LBUTTONUP,1)!=0)
      break; 
      
    if (priceopen!=lastpriceopen || priceclose!=lastpriceclose || pricehigh!=lastpricehigh || pricelow!=lastpricelow)
      WindowRedraw();
    else
    if (timeopen!=lasttimeopen || timeclose!=lasttimeclose || timehigh!=lasttimehigh || timelow!=lasttimelow)
      WindowRedraw();
      
    UpdateWindow(hButtonOK);
    UpdateWindow(hButtonCancel);
    
    lastpriceopen=priceopen;   lasttimeopen=timeopen;
    lastpriceclose=priceclose; lasttimeclose=timeclose;
    lastpricehigh=pricehigh;   lasttimehigh=timehigh;
    lastpricelow=pricelow;     lasttimelow=timelow;
    Sleep(10);  
  }
  
  ObjectDelete(vline);
  ObjectDelete(arrowopen);
  ObjectDelete(arrowclose);
  ObjectDelete(texthigh);
  ObjectDelete(textlow);
  WindowRedraw();
  DestroyWindow(hButtonOK);
  DestroyWindow(hButtonCancel);
  
  ParamStrings[0]=priceopen;
  ParamStrings[1]=pricehigh;
  ParamStrings[2]=pricelow;
  ParamStrings[3]=priceclose;
  int minvolume=4;
  for (int i=0; i<3; i++)
    for (int j=i; j<4; j++)
      if (ParamStrings[i]==ParamStrings[j])
        { minvolume--; break; }
  if (StrToInteger(ParamStrings[4])<minvolume)
    ParamStrings[4]=minvolume;
    
  if (pricehigh-pricelow==0)
    ParamStrings[4]=1;
    
  return(result);
}

  

Comments