Re: Free RealTime Data NOW Nest ODIN Trade Tiger Google Yahoo to AmiBroker, Fcharts M
Similarly, I have edited NestIndexBackFill_0.3.au3 to provide for additional delay when no data is found in VWAP Statistics table.... Lines 231-236
Similarly, I have edited NestIndexBackFill_0.3.au3 to provide for additional delay when no data is found in VWAP Statistics table.... Lines 231-236
Code:
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=1404401545_282477.ico
#AutoIt3Wrapper_Outfile_x64=NestIndexBackfill_0.3_x64.exe
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
Developed by Josh1
Purpose - to backfill quotes automatically from Nest/Zerodha Trader
from Market watch window and Hourly Statistics
7th April, 2014
#ce
#Region ............ Included Scripts
#include-once
#include <GUIConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <Date.au3>
#include <DTC.au3>
#include <file.au3>
#EndRegion ............ Included Scripts
#Region ............ Options settings
Opt('WinWaitDelay', 100)
Opt('WinDetectHiddenText', 1)
Opt('MouseCoordMode', 0)
Opt("SendKeyDelay", 15)
AutoItSetOption("WinTitleMatchMode", 4)
#EndRegion ............ Options settings
#Region ### START Koda GUI section ### Form=
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
$hGUI = GUICreate("Select Symbols to backfill", 460, 500, 80, 90, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
GUISetFont(10, 400, 0, "MS Sans Serif")
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
$Label1 = GUICtrlCreateLabel("Nest Trader Index Window Title", 20, 12, 180, 20)
$Input1 = GUICtrlCreateInput(IniRead(@WorkingDir & "\NestVwap.ini", "NestTrader", "TitleNdx", "Index Value Bar"), 200, 10, 235, 24)
$Label2 = GUICtrlCreateLabel("Temporary CSV Folder Path", 20, 42, 180, 20)
$Input2 = GUICtrlCreateInput(IniRead(@WorkingDir & "\NestVwap.ini", "NestTrader", "CSVFolder", "=R:\"), 200, 42, 235, 24)
$Label3 = GUICtrlCreateLabel("Data Delay - Minutes", 20, 65, 180, 40)
$Input3 = GUICtrlCreateInput(IniRead(@WorkingDir & "\NestVwap.ini", "NestTrader", "Delay", "0.05"), 200, 72, 235, 20)
;$Label4 = GUICtrlCreateLabel("Options and Equity will remain as Trading Symbol",20, 102, 400, 20)
$Label5 = GUICtrlCreateLabel("Backfill Gap from", 20, 132, 120, 20)
$Label6 = GUICtrlCreateLabel(" Up To", 220, 132, 100, 20)
$T1 = GUICtrlCreateDate(_NowCalcDate() & " 09:15:00", 130, 132, 80, 20, $DTS_SHORTDATEFORMAT)
$T2 = GUICtrlCreateDate(_NowCalc(), 330, 132, 80, 20, $DTS_SHORTDATEFORMAT) ;$DTS_TIMEFORMAT
; to select a specific default format
$DTM_SETFORMAT_ = 0x1032 ; $DTM_SETFORMATW
$style = "HH:mm:ss"
GUICtrlSendMsg($T1, $DTM_SETFORMAT_, 0, $style)
GUICtrlSendMsg($T2, $DTM_SETFORMAT_, 0, $style)
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
$iListView = GUICtrlCreateListView("", 20, 162, 415, 300)
$hiListView = GUICtrlGetHandle($iListView)
_GUICtrlListView_SetExtendedListViewStyle($hiListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES), $LVSCW_AUTOSIZE)
;$iSelectionState = GUICtrlCreateButton("Do Backfill", 400 - 90, 275, 85, 22.5)
GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)
;$Edit1 = GUICtrlCreateListView("",20,162,415,300)
$Button1 = GUICtrlCreateButton("Do Backfill", 168, 465, 105, 25)
GUICtrlSetOnEvent($Button1, "On_DoBackfill_Click")
GUISetState()
WinSetOnTop($hGUI, "", 1)
#Region .............. Set Title for Nest main window and get its handle if open
Global $Title = GUICtrlRead($Input1)
Global $DataDir = GUICtrlRead($Input2)
Global $Delay = GUICtrlRead($Input3)
If Not WinExists($Title) Then
MsgBox(64, "Nest Error", "Please Start Nest Trader .... Exiting " & $Title & " " & $Input2)
Exit
Else
WinActivate($Title)
Global $hNdxBar = WinGetHandle($Title) ; Get handle for IndexValueBar
Global $hNdxWatch = ControlGetHandle($hNdxBar, "", "[CLASS:SysListView32; INSTANCE:1]") ;Get handle of Mxarket Watch Control
Global $hStat = 9999
EndIf
#EndRegion .............. Set Title for Nest main window and get its handle if open
$NdxScrips = _Get_Scrips_from_IndexValueBar($hNdxWatch)
;_ArrayDisplay($NdxScrips,"Scrips found")
$iRows = UBound($NdxScrips, 1)
$iCols = UBound($NdxScrips, 2)
_GUICtrlListView_InsertColumn($hiListView, 0, "Symbol ", 200)
For $i = 0 To $iRows - 1 ; Fill Listview with Marketwatch scrips
GUICtrlCreateListViewItem($NdxScrips[$i][0], $iListView)
Next
_GUICtrlListView_InsertColumn($hiListView, 1, "2 ", 100)
_GUICtrlListView_InsertColumn($hiListView, 2, "3 ", 100)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(100)
WEnd
Func On_DoBackfill_Click()
$Title = GUICtrlRead($Input1)
$DataDir = GUICtrlRead($Input2)
$Delay = GUICtrlRead($Input3)
;Write defaults to ini file
IniWrite(@WorkingDir & "\NestVwap.ini", "NestTrader", "TitleNdx", $Title)
IniWrite(@WorkingDir & "\NestVwap.ini", "NestTrader", "CSVFolder", $DataDir)
IniWrite(@WorkingDir & "\NestVwap.ini", "NestTrader", "Delay", $Delay)
$Delay = GUICtrlRead($Input3) * 60000
; Get Indexes selected for backup from IndexValueBar
$Ndx = _Select_Ndx_from_IndexValueBar($hNdxWatch)
_Get_Text_from_VWAP_Statistics($Ndx)
WinClose($hStat)
EndFunc ;==>On_DoBackfill_Click
Func _Select_Ndx_from_IndexValueBar($hListView)
Local $aReturn = 0
Local $aReturn[$iRows]
;Copy selected scrips indices instead of deleting unselected scrips
Local $size = 0
For $j = 0 To ($iRows - 1) Step +1
If _GUICtrlListView_GetItemChecked($hiListView, $j) = True Then
$aReturn[$size] = $j
$size = $size + 1;
EndIf
Next
ReDim $aReturn[$size]
Return SetError(Number($aReturn[0] = 0), 0, $aReturn)
EndFunc ;==>_Select_Ndx_from_IndexValueBar
Func CLOSEButton()
Exit
EndFunc ;==>CLOSEButton
Func _Get_Scrips_from_IndexValueBar($hListView)
Local $iItemCount = _GUICtrlListView_GetItemCount($hListView), $i, $j
Local $iColumnCount = _GUICtrlListView_GetColumnCount($hListView)
Local $aReturn[$iItemCount][$iColumnCount] ;= [[$iItemCount, $iColumnCount]]
For $i = 0 To $iItemCount - 1
For $j = 0 To $iColumnCount - 1
$aReturn[$i][$j] = _GUICtrlListView_GetItemText($hListView, $i, $j)
Next
Next
; _ArrayDisplay($aReturn,"$areturn from IndexValueBar")
Return SetError(Number($aReturn[0][0] = 0), 0, $aReturn)
EndFunc ;==>_Get_Scrips_from_IndexValueBar
Func _Get_Text_from_VWAP_Statistics(ByRef $Scrips)
; Find total rows in IndexValueBar
Local $iItemCount = ControlListView($hNdxBar, "", "[CLASS:SysListView32; INSTANCE:1]", "GetItemCount")
; Iterate selections for each selected scrip
For $i = 0 To UBound($Scrips, 1) - 1
ControlListView($hNdxBar, "", "[CLASS:SysListView32; INSTANCE:1]", "SelectClear")
Sleep(1000)
For $iRows = 0 To $iItemCount - 1 ;search scrip through all rows of Mktwatch
; Get Index from each row
$TempScrip = ControlListView($hNdxBar, "", "[CLASS:SysListView32; INSTANCE:1]", "GetText", $iRows, 0)
Local $index = $Scrips[$i];
If $NdxScrips[$index][0] = $TempScrip Then
_GUICtrlListView_SetItemText($hiListView, $iRows, "Opening Vwap", 1)
For $tr = 1 To 5
_GUICtrlListView_SetItemText($hiListView, $iRows, "Try-" & $tr, 1)
_GUICtrlListView_SetItemFocused($hNdxWatch, $iRows)
;Sleep(2000)
;ControlClick($hNdxBar,"","[CLASS:SysListView32; INSTANCE:1]","left",1,30)
Sleep(2000)
_GUICtrlListView_ClickItem($hNdxWatch, $iRows, "right")
ControlSend($hNdxBar, "", "[CLASS:SysListView32; INSTANCE:1]", "v")
Sleep(1000)
$hStat = WinWaitActive("[Title:Vwap Statistics; Class:#32770]", "", 3) ;Wait to open Vwap Statistitics and get handle
If ($hStat <> 9999 Or $hStat <> 0) Then
If WinExists($hStat, "") Then
WinActivate($hStat, "")
WinSetOnTop($hStat, "", 1)
_GUICtrlListView_SetItemText($hiListView, $iRows, "Got it", 1)
ExitLoop
Else
$hStat = 9999
EndIf
ElseIf $tr = 5 Then
MsgBox(64, "NestIndex Backfill", "Vwap window not opening ... Exiting" & @CRLF & @CRLF _
& "Start again or try other Index")
Exit
EndIf
Next
ControlSetText($hStat, "", "Edit1", "1")
Sleep(1000)
; WinSetState($hStat, "", @SW_MINIMIZE) ;minimise Hourly statistics window
ControlFocus($hStat, "", "[CLASS:Button; INSTANCE:1]")
ControlClick($hStat, "", "[CLASS:Button; INSTANCE:1]", "Left", 1) ;Click on 'Get Statistics' Button
Sleep($Delay)
; Get handle of ListView /Data and check column order
Local $hStLV = ControlGetHandle($hStat, "", "[Class:SysListView32; INSTANCE:1]")
#cs
Local $hStLVColumns [6] = ["Start Time","Open Rate","High Rate","Low Rate","Close Rate","Differential Vol"]
Local $aColumns = ""
For $iCols = 0 To 5
$aColumns = _GUICtrlListView_GetColumn($hStLV, $iCols)
If $aColumns[5] <> $hStLVColumns[$iCols] Then
MsgBox(262144,"VWAP Statistics Columns Order","Column - " & $i+1 & " is- " & $aColumns[5] & @LF _
& " It should be - " & $hStLVColumns[$iCols])
Exit
EndIf
Next
#ce
Local $Sname = $NdxScrips[$index][0]
_GUICtrlListView_SetItemText($hiListView, $iRows, "GettingData", 1)
Global $pos = $iRows
ControlFocus($hStat, "", "[CLASS:SysListView32; INSTANCE:1]")
;cs - Aditya -- Additional delay when the data takes a while to populate in VWAP Statistics
Local $STLVitemcount = _GUICtrlListView_GetItemCount($hStLV)
If $STLVitemcount = 0 Then ; If there is no data in Hourly Stat then provide additional delay
_GUICtrlListView_SetItemText($hiListView, $iRows, "Further delay...", 1)
Sleep(15000)
;ce - Aditya
Local $STLVitemcount = _GUICtrlListView_GetItemCount($hStLV)
If $STLVitemcount = 0 Then ; If there is no data in Hourly Stat then exit
_GUICtrlListView_SetItemText($hiListView, $iRows, "No Data found", 1)
WinClose($hStat)
EndIf
EndIf
; Start Collecting Data from Hourly Statistics
If $i = 0 Then
MsgBox(262144, "Set Backfill Time", "Set Time Gap to be filled and Press OK")
Global $DayDeleteFrom = _Date_Time_Convert(_NowCalcDate() & GUICtrlRead($T1), "yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
Global $DayDeleteUpto = _Date_Time_Convert(_NowCalcDate() & GUICtrlRead($T2), "yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
; MsgBox(0,"Date Time Convert", "From - " & $DayDeleteFrom & " To - " & $DayDeleteUpto )
EndIf
Local $T = ""
For $j = 0 To $STLVitemcount - 1
$sText = ""
For $k = 0 To 5
$sText &= _GUICtrlListView_GetItemText($hStLV, $j, $k)
If $k = 0 Then ;Convert 12 Hour to 24 Hour format
Local $Hour = Int(StringLeft($sText, "2"))
If ($Hour <> 12 And StringRight($sText, 2) = "PM") Then
$Hour = $Hour + 12
$sText = $Hour & StringMid($sText, 3, 6)
Else
$sText = StringLeft($sText, 8)
EndIf
$Checktime = @YEAR & @MON & @MDAY & StringReplace(StringLeft($sText, 8), ":", "")
If Not (($Checktime) >= ($DayDeleteFrom) And ($Checktime) <= ($DayDeleteUpto)) Then
_GUICtrlListView_SetItemText($hiListView, $pos, $DayDeleteFrom, 1)
_GUICtrlListView_SetItemText($hiListView, $pos, $DayDeleteUpto, 2)
ContinueLoop 2
EndIf
EndIf
$sText &= ","
Next
; TODO - remove hardcode - allow manual alias in search
$T &= $Sname & "," & @YEAR & @MON & @MDAY & "," & $sText & @CRLF ;Format CSV data
;$T &= "NIFTY" & "," & @YEAR & @MON & @MDAY & "," & $sText & @CRLF ;Format CSV data
Next
EndIf
Next
_GUICtrlListView_SetItemText($hiListView, $pos, "Data retrieved", 1)
FileDelete($DataDir & $Sname & ".txt")
$Filename = FileOpen($DataDir & $Sname & ".txt", 1)
FileWrite($Filename, $T)
FileClose($Filename)
_DeleteTics($Sname)
WinClose($hStat)
$hStat = 9999
Next
EndFunc ;==>_Get_Text_from_VWAP_Statistics
Func _Get_Item_from_MarketWatch($Scrip)
Local $sText = ""
Local $T = ""
$Qty = _GUICtrlListView_GetItemCount($hNdxWatch)
;MsgBox(64,"No. of items in Market Watch", "NSE = " & $qty, 1000)
Local $arr[40]
For $i = 0 To $Qty - 1
; $sText = _GUICtrlListView_GetItemTextString ( $hNdxWatch ,$i)
; $T = $T & $sText & @LF
$arr[$i] = _GUICtrlListView_GetItemTextArray($hNdxWatch, $i)
MsgBox(0, "Item No." & $i, _ArrayToString($arr[$i], ""","))
Next
;MsgBox(0,"Market Watch NSE", _ArrayToString ( $arr, "|") )
;MsgBox(0,"Market Watch NSE", $T)
EndFunc ;==>_Get_Item_from_MarketWatch
Func _DeleteTics($Scrip)
Local $Filename = $DataDir & $Scrip & ".txt"
Local $oAB = ObjGet("Broker.Application") ; Get existing instance of AmiBroker
If @error Then
Local $oAB = ObjCreate("Broker.Application")
EndIf
$oAB.Visible = 1
If StringRight($DayDeleteFrom, 6) <> 000000 Then
$oStocks = $oAB.Stocks
Local $Qty = $oStocks.Count
Local $i = 0
Local $j = 0
Local $k = 0
For $i = 0 To ($Qty - 1)
$Stock = $oStocks($i)
; $k = 0
Local $ScripName = $Stock.Ticker
$QCount = $Stock.Quotations.Count
$Quotes = $Stock.Quotations
If $ScripName = $Scrip Then
_GUICtrlListView_SetItemText($hiListView, $pos, "Deleting pr.quotes", 1)
$ts = TimerInit()
; MsgBox(0,"","Removing quotes " & $Scrip)
For $j = ($QCount - 1) To 0 Step -1
$Quote = $Quotes($j)
$tmpDateNum = $Quote.Date
If (($tmpDateNum >= $DayDeleteFrom) And ($tmpDateNum <= $DayDeleteUpto)) Then
; $k+=1
_GUICtrlListView_SetItemText($hiListView, $pos, $tmpDateNum, 1)
_GUICtrlListView_SetItemText($hiListView, $pos, $DayDeleteFrom, 2)
$Quotes.Remove($j)
ElseIf ($tmpDateNum < $DayDeleteFrom) Then
ExitLoop
EndIf
Next
$td = Int(TimerDiff($ts)) / 1000
$td &= " seconds"
_GUICtrlListView_SetItemText($hiListView, $pos, $td, 2)
EndIf
Next
With $oAB
.Import(0, $Filename, "rtd.format")
.RefreshAll
.SaveDatabase
EndWith
Else
Local $sString = FileReadToArray($Filename)
Local $aString = StringSplit($sString, @CRLF, 2)
$aRet = _ArraySortInOrder($sString)
; If Not @error Then _ArrayDisplay($aRet, "Sorted Array")
_FileWriteFromArray($Filename, $aRet, @CRLF)
With $oAB
.Import(0, $Filename, "rtd2.format")
.RefreshAll
.SaveDatabase
EndWith
EndIf
_GUICtrlListView_SetItemText($hiListView, $pos, "Done", 1)
EndFunc ;==>_DeleteTics
; Description - Sorts an array or a space separated string in the order of the characters listed in a given string.
; Parameters:
; $aStr - The array, or the space separated string to be sorted.
; $sAlphabetOrder - A string containing the sorting order of the characters which appear in the array or string.
; Requirement: #include <Array.au3>
; Returns: A sorted array.
Func _ArraySortInOrder($aStr)
Local $sAlphabetOrder = "0123456789abcdefghijklmnopqrstuwxyz"
$sAlphabetOrder = StringStripWS($sAlphabetOrder, 8) ; Cannot sort on a space.
Local $aAlpha = StringSplit($sAlphabetOrder, "", 2)
;--- Convert the array ($aStr) to a string or convert the string ($aStr) to an array.----
Local $sToSort
If IsArray($aStr) Then
For $i = 0 To UBound($aStr) - 1
$sToSort &= $aStr[$i] & " "
Next
$sToSort = StringTrimRight($sToSort, 1) ; Remove trailing space.
ElseIf IsString($aStr) Then
$sToSort = StringStripWS($aStr, 7)
$aStr = StringSplit($sToSort, " ", 2)
Else
Return SetError(0, 0, 1)
EndIf
;--->End of Convert array_string.----
; Convert all characters in $sToSort to $sAlphabetOrder characters.
; Note; "#>" was randomly chosen to prevent previous converted characters to be converted again.
For $i = 0 To UBound($aAlpha) - 1
$sToSort = StringRegExpReplace($sToSort, $aAlpha[$i] & "(?!\d{0,2}#>)", StringRight("00" & $i, 3) & "#>")
Next
;Create 2D array with $sAlphabetOrder character words in column 0, and given words in the next column.
Local $aToSort = StringSplit($sToSort, " ", 2)
Local $aArr[UBound($aStr)][2]
For $i = 0 To UBound($aStr) - 1
$aArr[$i][0] = ($aToSort[$i])
$aArr[$i][1] = $aStr[$i]
Next
;--->End of Create 2D array -------------------------------------------------------------
_ArraySort($aArr) ; Sort on column 0
; Copy the 2nd column of the 2D array into a 1D array.
For $i = 0 To UBound($aStr) - 1
$aStr[$i] = $aArr[$i][1]
Next
Return $aStr
EndFunc ;==>_ArraySortInOrder