0% found this document useful (0 votes)
241 views

RTD 1-6 Source Code

This document contains the code for an AutoIt script to backfill stock market quotes from various sources. It includes: 1. Multiple versions and updates by different developers to add new features like index backfill, improved settings, and bug fixes. 2. Global variables and included library files to support the GUI, data retrieval, and other functions. 3. Two GUI functions - one for a main window and one for a backfill settings window, including buttons and controls. 4. Functions for settings, help, selecting scripts, starting/stopping RTD feed, and deleting ticks.

Uploaded by

MD DN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views

RTD 1-6 Source Code

This document contains the code for an AutoIt script to backfill stock market quotes from various sources. It includes: 1. Multiple versions and updates by different developers to add new features like index backfill, improved settings, and bug fixes. 2. Global variables and included library files to support the GUI, data retrieval, and other functions. 3. Two GUI functions - one for a main window and one for a backfill settings window, including buttons and controls. 4. Functions for settings, help, selecting scripts, starting/stopping RTD feed, and deleting ticks.

Uploaded by

MD DN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 39

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Icon=icons\1404401545_282477.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#cs
Developed by Josh1
Script RTD, Background backfill, Improved settings Update By Shankar4kv
Purpose - to backfill quotes automatically from NOW/Nest/Zerodha Trader
from Market watch window and Hourly Statistics
16th April, 2016
1. Added support for RTDMan2.0 with Ninja
2. Support for right+click on Scrip in marketwatch due to changes in Shift+D
by brokers
30th June, 2014
Modified by Josh1 on 21-JAN-2015
1. Removed Function Select_Scrips_from_Market_Watch
It was getting index of scrips and program crashed when user did not
select any scrip\
2. We now use Array of scrips $MarketWatchScrips that we collect in function
_Get_Scrips_from_Mwatch when we fill ListView
3. Added error catch when user did not select scrips
3. Added DeleteTics button and function to delete tics
4. $oMyret and $oMyError variable deleted - not used any where.
Version 1.20 by Josh1 on 22-JAN-2015
1. Added Index backfill functionality
2. Fixed issue of Last Scrip not backfilling with Nestplus
Version 1.3 by Josh1 on 18-AUG, 2015
1.Moved all global variables declared inside functions to top of code. That
should fix memory leak
2.Closing RTDMan.exe by processclose was creating big memory leak and
deleting preferences of Amibroker.
Fixed by using WinClose("RTDMan.exe")
3.RTD main window and Backfill window seperated.
4.Start RTD Added to backfill
5.Index Radio button was faded in Nest- Fixed
6.Hide button added to Main RTD window- by Shankar4kv
Version 1.4 by Josh1 on 18-AUG, 2015
1.Main GUI split into two.
2.GUI1 is a thin strip that can remain on top of AmiBroker
3.GUI2 for selecting Scrips backfill etc.
Version 1.5 by Josh1 on 06-MARCH, 2017
1. Index backfill from DataTable
2. Name of Backfill button in GUI1 changed to "Select"
3. Backfill now takes scrip alias from RTDMan.ini if available.
#ce

Global $Title = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Title",


"Welcome")
Global $DataDir = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "CSVFolder",
"R:\")
Global $FNO = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Future", "")
Global $TSymbol = -1, $Exchange = -1, $Series = -1, $Instrument = -1, $Symbol = -1,
$Optype = -1, $Strike = -1
Global $DayDeleteFrom = 0, $DayDeleteUpto = 0, $pos = 0 , $iRows =0, $iCols=0
Global $DataDelay = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Datatable",
"0") ; inserted by Shankar
Global $hTray_Show_Item = TrayCreateItem ; inserted by Shankar
Global $hNdxBar = 0, $hNdxWatch =0 , $NdxScrips[10][10]=[[0],[0]],$Npi,
$Npn,$Npy
Global $Input6 = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Terminal", "")
Global $LotQty = -1, $RequestRefresh,$BellWaitTime, $DivideVolume
Global $hgui1, $hWatch, $hWnd, $hStat = 1, $hgui, $MwatchScrips, $Plusbackfill,
$Vwapbackfill, $Ndxbackfill, $settingsgui, $iListView, $hiListView, $hgui,
$hgui1,$Client,$RefreshPeriod
Global $Button1, $Button2, $Button3, $Button7, $Button14, $Button4, $D1, $D2, $T1,
$T2, $BidAskRates, $BidAskQty
Global $Input1, $Input2, $Input3, $Input4, $Input5, $Input6, $Input7, $Input8,
$Input9, $Input10, $Input11, $Input12, $Input13, $Input14, $Input15, $Archive
Global $Input16, $Input17, $Input18, $Input19, $Input20, $Input21, $oAB, $Input23

#Region ............ Included Scripts


#include-once
#include <GUIConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <Date.au3>
#include <DTC.au3>
#include <file.au3>
#include <windowsconstants.au3>
#include <guiconstantsex.au3>
#EndRegion ............ Included Scripts

#Region ............ Options settings


Opt('WinWaitDelay', 100)
Opt('WinDetectHiddenText', 1)
Opt('MouseCoordMode', 0)
Opt("SendKeyDelay", 15)
AutoItSetOption("WinTitleMatchMode", 2)
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
#EndRegion ............ Options settings

_Gui1()

_START_STOP() ; inserted by Shankar

Func _Gui1()
; Create GUI for Main Program
#Region ### START Koda GUI section ### Form=
If Not $hgui1 Then
$hgui1 = GUICreate("RTD V 1.60", 345, 30,900,1, BitOR($WS_MINIMIZEBOX,
$WS_SYSMENU, $WS_DLGFRAME, $WS_POPUP, $WS_GROUP, $WS_CLIPSIBLINGS))
$hLabel = GUICtrlCreateLabel("RTD V 1.60", 0, 0,365, 13, -1,
$GUI_WS_EX_PARENTDRAG)
GUICtrlSetBkColor(-1, 0x00FF00)
Local $h = 0 ;horizontal space
Local $v = 13 ; vertical space
Local $vh = 18 ; height of charachters

GUISetFont(8, 400, 0, "MS Sans Serif")


; GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
$Button1 = GUICtrlCreateButton("Settings", $h, $v , 44, $vh)
$Button2 = GUICtrlCreateButton("Help", $h + 46, $v, 30, $vh) ;

$Button6 = GUICtrlCreateButton("SET INI", $h + 78, $v, 25, $vh,


$BS_ICON) ; inserted by Shankar
GuiCtrlSetImage(-1, @WorkingDir & "\icons\ini.ico", -1)
GUICtrlSetTip ( -1, "Set/Modify INI")
$Button7 = GUICtrlCreateButton("DeleteTics", $h + 105, $v, 57, $vh,
$BS_ICON) ; inserted by Josh on 19-1-2015
GuiCtrlSetImage(-1, @WorkingDir & "\icons\Trash.ico", -1)
GUICtrlSetTip ( -1, "Delete Tics")
$Button3 = GUICtrlCreateButton("Select", $h + 164, $v , 46, $vh)
$Button4 = GUICtrlCreateButton("START RTD", $h + 212, $v, 70, $vh) ;
inserted by Shankar
GUICtrlSetTip ( -1, "Start/Stop Realtime Feed")
$Button8 = GUICtrlCreateButton("Exit", $h + 283, $v, 30, $vh, $BS_ICON)
; inserted by Shankar
$Button9 = GUICtrlCreateButton("Hide", $h + 315, $v, 30, $vh, $BS_ICON)
; inserted by Shankar 16.08.2015

GUICtrlSetOnEvent($Button1, "_OnSettings")
GUICtrlSetOnEvent($Button2, "_OnHelp") ; inserted by Shankar
GUICtrlSetOnEvent($Button3, "_Gui2")
GUICtrlSetOnEvent($Button4, "_On_Rtd_Click") ; inserted by Shankar
GUICtrlSetOnEvent($Button6, "_Set_ini") ; inserted by Shankar
GUICtrlSetOnEvent($Button7, "_On_DeleteTics_Click") ; inserted by Josh1
GUICtrlSetOnEvent($Button8, "CLOSEButton") ; inserted by Josh1
GUICtrlSetOnEvent($Button9, "RTD_HIDE") ; inserted by shankar
16.08.2015

GUISetState()
EndIf

#EndRegion ### END Koda GUI section ###

#Region .............. Set Title for Nest main window and get its handle if
open
If Not WinExists($Title) Then
MsgBox(262144, "NOW/Nest Error", "Is NOW/Nest Trader Open? " & @CRLF &
"If yes- Please Copy title of NOW/Nest Trader Window Title" & @CRLF & "it should
match exact upto version number ")
_OnSettings()
Else
$hWnd = WinGetHandle($Title) ; Get handle for NOW main window
$hWatch = ControlGetHandle($hWnd, "", 1003) ;Get handle of Market Watch
Control
if $hStat = 1 Then
WinActivate($hWnd)
Send("+P")
Local $sMessage = "If Plus Login Opens - login and wait for
Market Monitor " & @CRLF & @CRLF & _
"If chart opens - continue"
SplashTextOn("Invoking NestPlus", $sMessage, 500, 100, -1, -1,
$DLG_TEXTVCENTER, "")
Sleep(4000)
SplashOff()

If WinExists("[CLASS:#32770]","NestPlus ") Then


WinActivate("[CLASS:#32770]","NestPlus ")
Send("{Enter}")
EndIf
EndIf
$hStat = 9999

EndIf
#EndRegion .............. Set Title for Nest main window and get its handle
if open

Global $start = _DateAdd('n', 5, _NowCalc())

EndFunc ;==>_Gui1

Func _Gui2()
; Create GUI for Backfill

If Not WinExists ("Backfill Quotes","") Then


;to prevent double instance
$hgui = GUICreate("Backfill Quotes", 365, 440, 800, 90,
BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))

Local $h = 10 ;horizontal space


Local $v = 5 ; vertical space
Local $vh = 18 ; height of charachters

GUISetFont(8, 400, 0, "MS Sans Serif")


GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSE_Gui2")

$Label5 = GUICtrlCreateLabel("Backfill Gap from", $h, $v + 5, 80, 18)


$T1 = GUICtrlCreateDate(_NowCalcDate() & " 09:15:00", $h + 85, $v + 5,
70, 18, $DTS_SHORTDATEFORMAT)
$Label6 = GUICtrlCreateLabel(" Up To", $h + 160, $v + 5, 50, 18)
$T2 = GUICtrlCreateDate(_NowCalc(), $h + 210, $v + 5, 70, 18,
$DTS_SHORTDATEFORMAT) ;$DTS_TIMEFORMAT

GUICtrlCreateGroup("Source -", $h, $v + 25, 300, 20)


$Npy = GUICtrlCreateRadio("NEST Plus", 80, $v + 25, 70, 18)
$Npn = GUICtrlCreateRadio("VWAP/Stat", 160, $v + 25, 80, 18)
$Npi = GUICtrlCreateRadio("Index", 250, $v + 25, 80, 18)

; GUICtrlSetState($Npn, $GUI_CHECKED)
GUICtrlSetOnEvent($Npy, "_OnPlusbackfill")
GUICtrlSetOnEvent($Npn, "_OnPlusbackfill")
GUICtrlSetOnEvent($Npi, "_OnIndexbackfill")

; 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)

GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE +


$GUI_DOCKBOTTOM)

$Button1 = GUICtrlCreateButton("Select All", $h, $v + 380, 25, $vh,


$BS_ICON)
GuiCtrlSetImage(-1, @WorkingDir & "\icons\Sec.ico", -1)
GUICtrlSetTip ( -1, "Select All")
$Button2 = GUICtrlCreateButton("DeSelect All", $h + 25, $v + 380, 25,
$vh, $BS_ICON)
GuiCtrlSetImage(-1, @WorkingDir & "\icons\Desec.ico", -1)
GUICtrlSetTip ( -1, "DeSelect All")
$Button3 = GUICtrlCreateButton("Do Backfill", $h + 140, $v + 380, 70,
$vh)

$Button14 = GUICtrlCreateButton("START RTD", $h + 212, $v+380, 70, $vh)


; inserted by Shankar
GUICtrlSetTip ( -1, "Start/Stop Realtime Feed")

$Button5 = GUICtrlCreateButton("RST", $h + 100, $v + 380, 25, $vh,


$BS_ICON) ; inserted by Shankar
GuiCtrlSetImage(-1, @WorkingDir & "\icons\Restart.ico", -1)
GUICtrlSetTip ( -1, "Restart Realtime Feed")

$Button7 = GUICtrlCreateButton("DeleteTics", $h + 75, $v + 380, 25,


$vh, $BS_ICON) ; inserted by Josh on 19-1-2015
GuiCtrlSetImage(-1, @WorkingDir & "\icons\Trash.ico", -1)
GUICtrlSetTip ( -1, "Delete Tics")

GUICtrlSetOnEvent($Button1, "_SelectAll")
GUICtrlSetOnEvent($Button2, "_DeselectAll")
GUICtrlSetOnEvent($Button3, "_On_DoBackfill_Click")
GUICtrlSetOnEvent($Button14, "_On_Rtd_Click") ; inserted by Shankar
GUICtrlSetOnEvent($Button5, "_On_Rtd_Restart") ; inserted by Shankar
GUICtrlSetOnEvent($Button7, "_On_DeleteTics_Click") ; inserted by Josh1

$Label7 = GUICtrlCreateLabel("Date from", $h, $v + 45, 50, 20)


$D1 = GUICtrlCreateDate(_NowCalc(), $h + 55, $v + 45, 90, 18,
$DTS_SHORTDATEFORMAT)
$Label8 = GUICtrlCreateLabel("To", $h + 155, $v + 45, 50, 20)
$D2 = GUICtrlCreateDate(_NowCalc(), $h + 200, $v + 45, 90, 18,
$DTS_SHORTDATEFORMAT)
$style1 = "yyyy/MM/dd"
GUICtrlSendMsg($D1, $DTM_SETFORMAT_, 0, $style1)
GUICtrlSendMsg($D2, $DTM_SETFORMAT_, 0, $style1)
ControlDisable($hgui, "", $D1)
ControlDisable($hgui, "", $D2)
GUICtrlSetState($Button1, $GUI_DISABLE )
GUICtrlSetState($Button2, $GUI_DISABLE )
GUICtrlSetState($Button3, $GUI_DISABLE )
If ProcessExists("RTDMan.exe") Then
GUICtrlSetData($Button14, "STOP RTD") ; changed from disable to
set data by shankar 16.08.2015
EndIf

GUISetState()
If $Input6 <> "Nest" then GUICtrlSetState($Npi, $GUI_DISABLE )
EndIf
EndFunc ;==>_Gui2

While 1
; Wait for user instructions
$start2 = _NowCalc()

If $start = $start2 Then


ControlClick($hWnd,"",1625)
;MsgBox(64,"Time Machine","Start Time = " & $start & @CRLF & "Real Time
= " & $start2)
_Gui1()

EndIf
Sleep(100)
WEnd

Func _START_STOP()
; Start stop Realtime feed by RTDMan
; To Change title of Start RTD button if RTDMan process exists
If ProcessExists("RTDMan.exe") Then
GUICtrlSetData($Button4, "STOP RTD")
EndIf

$hTray_Show_Item = TrayCreateItem("Show") ;Creates show Menu item in tray


icon
TrayItemSetOnEvent(-1, "To_Tray") ;Goes to func to_Tray when click show
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit") ;Goes to func On_Exit when click Exit

EndFunc ;==>_START_STOP

Func _Delete_Rtd_Scripts()
; Delete scrips from list
; To delete Scrips from RTDMan.ini
Local $Pre = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Preserve",
"") ; Check - Preserve any scrips in Settings ?

If $Pre = 0 Then ; if not to be preserved


$RequestRefresh = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"RequestRefresh", 0)
$BellWaitTime = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"BellWaitTime", 30)
$Client = IniRead(@WorkingDir & "\RTDMan.ini",
"RTDMan", "Client", "AB")
$RefreshPeriod = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"RefreshPeriod", 250)
$Archive = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Archive", False)

IniDelete(@WorkingDir & "\RTDMan.ini", "RTDMan") ; Delete settings for


RTDMan

; Copy Server name, Server prog ID, Bar period, temp. CSV folder path
from Settings Nest or NOW
$RTDMANSERVER = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"Terminal", "")
$Folder = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"CSVFolder", "")
$BARP = IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "BarPeriod", "")
$Archive = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Archive", False)

IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RTDServerProgID",


$RTDMANSERVER & ".ScripRTD")
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "CSVFolderPath",
$Folder)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "BarPeriod", $BARP)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RequestRefresh",
$RequestRefresh)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "BellWaitTime",
$BellWaitTime)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "Client", $Client)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RefreshPeriod",
$RefreshPeriod)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "Archive", $Archive)

IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewTicData", "0")


IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewRawData", "0")
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewBarData", "0")
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewNTData", "0")

EndIf

EndFunc ;==>_Delete_Rtd_Scripts

Func _On_Rtd_Restart()
; Restart Realtime feed with changed scrip list --- to do hide part
Local $SSRTD = GUICtrlRead($Button4, 0)
; Read Button4 (Start /Stop ) text from GUI
Local $UpdRTD = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "UpdateRTD", "")
; Read Settings to check whether Scrips are to be updated
If $SSRTD = "STOP RTD" Then ;
If $UpdRTD = "Yes" Then
_Delete_Rtd_Scripts()
_RTD_Script_Update()
Else
MsgBox(48, "WARNING!", "(RTDMan) Realtime Feed will Restart with the same
scripts that was previously saved, Make sure you have right setting in update
script option")
EndIf

If WinExists("RTDMan.exe") Then ; If RTDMan started


WinClose("RTDMan.exe")
EndIf
Run(@WorkingDir & "\RTDMan.exe")

; Send RTDMan to System Tray and hide its window


TraySetIcon(@WorkingDir & "\icons\RTDMan.ico")
; Local $CH = WinWait("[Title : RTDMan.exe; CLASS:ConsoleWindowClass]",
"", 3) ; This is not working ---- to do
Local $CH = WinWait("RTDMan.exe", "", 3)
Sleep(3000)
WinSetState($CH, "", @SW_HIDE)
TrayTip("Real Time Feed ReStarted", "You May Close RTD Anytime", 5, 0)
Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default

If not ProcessExists("RTDMan.exe") Then ; If RTDMan not started


MsgBox(48, "WARNING!", "(RTDMan) Realtime Feed Failed to ReStart,
Click ok to see Errorlog for more details.")
ShellExecute(@WorkingDir & "\errorLog.txt")
GUICtrlSetData($Button4, "START RTD") ; Change Button Text to
STOP RTD
GUICtrlSetData($Button14, "START RTD") ; Change Button Text to
STOP RTD
EndIf
Else
MsgBox(48, "WARNING!", "Use Start RTD to Start Realtime feed!, After Realtime
Feed Started, You may Restart using this function!.")

EndIf
EndFunc

Func _On_Rtd_Click()
; Start Realtime feed
;To start and Stop RTDMan.exe as required
Local $SSRTD = GUICtrlRead($Button4, 0) ; Read Button4 (Start /Stop )
text from GUI

If $SSRTD = "START RTD" Then ;


Local $UpdRTD = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"UpdateRTD", "") ; Read Settings to check whether Scrips are to be updated

If $UpdRTD = "Yes" Then


_Delete_Rtd_Scripts() ; Delete all Scrips from RTDMan settings
_RTD_Script_Update() ; Write them into RTDMan Settings
EndIf

Run(@WorkingDir & "\RTDMan.exe") ; and start it

Sleep(500)

; Send RTDMan to System Tray and hide its window


TraySetIcon(@WorkingDir & "\icons\RTDMan.ico")
; Local $CH = WinWait("[CLASS:ConsoleWindowClass]", "", 3)
Local $CH = WinWait("RTDMan.exe", "", 3)
WinSetState($CH, "", @SW_HIDE)
TrayTip("Real Time Feed Started", "You May Close RTD Anytime", 5, 0)
Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default

If ProcessExists("RTDMan.exe") Then ; If RTDMan started


GUICtrlSetData($Button4, "STOP RTD") ; Change Button Text to STOP
RTD
If WinExists($hgui) Then
GUICtrlSetData($Button14, "STOP RTD") ; Change Button Text
to STOP RTD
EndIf
Else
MsgBox(48, "WARNING!", "(RTDMan) Realtime Feed Not Started, Click
ok to see ErrorLog for more details.")
ShellExecute(@WorkingDir & "\errorLog.txt")
TrayTip("Real Time Feed Not Started", "See ErrorLog for More
Details", 5, 2)
EndIf

Else
; Stop RTDMan process and Change Button4 Text to Start RTD
If WinExists("RTDMan.exe") Then ; If RTDMan not started
WinActivate("RTDMan.exe")
Send("^{BREAK}")
WinClose("RTDMan.exe")
EndIf
GUICtrlSetData($Button4, "START RTD")
If WinExists($hgui) Then
GUICtrlSetData($Button14, "START RTD")
EndIf
TrayTip("Real Time Feed Stopped", "You May START RTD Anytime", 5, 0)

EndIf

EndFunc ;==>_On_Rtd_Click

Func _FillList()
; Fill list view with scrips captured from Market watch

If Not WinExists($Title) Then


MsgBox(262144, "NOW/Nest Error", "Is NOW/Nest Trader Open? " & @CRLF &
"Open it before this application ... Exiting ")
_OnSettings()
EndIf

_Check_MarketWatchColumns() ;Check whether requisite columns are present in


MarketWatch
$MwatchScrips = _Get_Scrips_from_Mwatch() ;If yes, get all scrips from Market
watch into array
;_ArrayDisplay($MwatchScrips,"Scrips found")

$iRows = UBound($MwatchScrips, 1) ; find total rows in Mktwatch


$iCols = UBound($MwatchScrips, 2) ; find total columns in Mktwatch
If $iListView Then GUICtrlDelete($iListView) ; if Listview is already
present, delete it

$iListView = GUICtrlCreateListView("Select Symbols to backfill", 10, 75, 350,


300) ; Created Listview to display and select scrips in RTD Window
$hiListView = GUICtrlGetHandle($iListView) ; obtained handle for the listview
_GUICtrlListView_SetExtendedListViewStyle($hiListView,
BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES), $LVSCW_AUTOSIZE) ; Make listview
with Check box and auto size.

;_GUICtrlListView_InsertColumn($hiListView, 0, "Symbol ", 150)


For $i = 0 To $iRows - 1
GUICtrlCreateListViewItem($MwatchScrips[$i][$TSymbol], $iListView) ;
Fill Listview with Marketwatch scrips Trading Symbol
Next
_GUICtrlListView_InsertColumn($hiListView, 2, "Progress", 95) ; Insert
Progress Column in Listview
_GUICtrlListView_InsertColumn($hiListView, 3, "Time", 95) ; Insert Time
Column in Listview

EndFunc ;==>_FillList

Func _On_DoBackfill_Click()
; Start Backfill

$Plusbackfill = ControlCommand($hgui, "", $Npy, "IsChecked")


$Vwapbackfill = ControlCommand($hgui, "", $Npn, "IsChecked")
$Ndxbackfill = ControlCommand($hgui, "", $Npi, "IsChecked")
$Client = IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan", "Client",
"AB")

If $Client = "AB" Then


$oAB = ObjCreate("Broker.Application")
$oAB.Visible = 1
Else
MsgBox(64, "Ninja Trader", "You have selected NT Client" &
@CRLF & _
"Data files will be created in " &
$DataDir & @CRLF & _
" Import them manually" & @CRLF & @CRLF &
_
" in Historical Data Manager" , 5)
EndIf

If $Plusbackfill or $Vwapbackfill or $Ndxbackfill Then

If $Ndxbackfill Then
_Get_Index_Data_from_DataTable($NdxScrips)
Else

_RTD_PLUSCHART_Update()
_Get_Text_from_VWAP_Stat_Data_Table()
EndIf

WinClose($hStat)
Else
MsgBox(64, "Real Time Data", "Select Nestplus or Vwap or Index radio
button from Source to Do Backfill", 3)
EndIf

EndFunc ;==>_On_DoBackfill_Click

Func _Get_Scrips_from_Mwatch()
; Capture scrips from market watch
Local $iItemCount = _GUICtrlListView_GetItemCount($hWatch), $i, $j
Local $iColumnCount = _GUICtrlListView_GetColumnCount($hWatch)
Local $aReturn[$iItemCount][$iColumnCount] ;= [[$iItemCount, $iColumnCount]]

For $i = 0 To $iItemCount - 1
For $j = 0 To $iColumnCount - 1
$aReturn[$i][$j] = ControlListView($hWnd, "", 1003, "GetText",
$i, $j) ; Get text of all items from Mktwatch into 2D array $aReturn
Next
Next

; _ArrayDisplay($aReturn,"$areturn from MktWatch")

Return SetError(Number($aReturn[0][0] = 0), 0, $aReturn) ;Return $aReturn to


calling function.
EndFunc ;==>_Get_Scrips_from_Mwatch

Func _Check_MarketWatchColumns()
; Check order of Market watch columns (This is not working with NestTrader
3.13/14/15)
; To check whether requisite columns are available in Market watcha

Local $iColumnCount = _GUICtrlListView_GetColumnCount($hWatch)

Local $aColumns[] = [], $aReturn[9]


For $i = 0 To $iColumnCount - 1
$aColumns = _GUICtrlListView_GetColumn($hWatch, $i)
Local $Name = $aColumns[5] ;Get heading fro each column from Market
Watch

Switch $Name ; Store column number for required columns in to same


variables of same name
Case "Trading Symbol"
$TSymbol = $i
Case "Exchange"
$Exchange = $i
Case "Instrument Name"
$Instrument = $i
Case "Symbol"
$Symbol = $i
Case "Option Type"
$Optype = $i
Case "Strike Price"
$Strike = $i
Case "Series/Expiry"
$Series = $i
Case "Lot Qty"
$LotQty = $i
Case "Min Lot Qty"
$LotQty = $i
EndSwitch

Next

If $TSymbol < 0 Then


MsgBox(64, "Columns", "Trading Symbol not found - Backfill May not work
set Position to 1 ")
$TSymbol = 0

ElseIf $Symbol < 0 Then


MsgBox(64, "Columns", "Symbol not found - Set Position to 6")
$Symbol = 5

ElseIf $Exchange < 0 Then


MsgBox(64, "Columns", "Exchange not found - Position set to 9")
$Exchange = 8

ElseIf $Instrument < 0 Then


MsgBox(64, "Columns", "Instrument Name not found - Position set to 10")
$Instrument = 9

ElseIf $Optype < 0 Then


MsgBox(64, "Columns", "Option Type not found - Position set to 11")
$Optype = 10

ElseIf $Strike < 0 Then


MsgBox(64, "Columns", "Strike Price not found - Position set to 12")
$Strike = 11

ElseIf $Series < 0 Then


MsgBox(64, "Columns", "Series/Expiry not found - Position set to 13")
$Series = 12

ElseIf $LotQty < 0 Then


MsgBox(64, "Columns", "Lot Qty or Min Lot Qty not found - Position set
to 14")
$LotQty = 13
EndIf
; _ArrayDisplay($aReturn,"hahaha")

EndFunc ;==>_Check_MarketWatchColumns

Func _RTD_Script_Update()
; Update list of scrips in ini file with other options
; To update scrips in RTDMan Settings
Local $PlusState = ControlCommand($hgui, "", $Npi, "IsChecked")
Local $CheckedScrips = 0
Local $RTNOW = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Terminal",
"") ;Read RTNow settings as NEST or NOW in RTDMan.ini
Local $Pre = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "Preserve",
"") ;Reads the Preserve value in RTDMan.ini

For $i = 0 To $iRows - 1 ;search scrip through all rows of $Mktwatch


ControlListView($hWnd, "", "[CLASS:SysListView32;INSTANCE:3]",
"SelectClear")

If _GUICtrlListView_GetItemChecked($hiListView, $i) = True Then


; Is the scrip checked in RTD Window?
$CheckedScrips+=1

If Not $PlusState Then


; Get Exchange from each row
Switch $MwatchScrips[$i][$Exchange]
Case "MCX"
$TempExchange = "mcx_fo"
Case "NSE"
$TempExchange = "nse_cm"
Case "NFO"
$TempExchange = "nse_fo"
Case "NCDEX"
$TempExchange = "ncx_fo"
Case "MCXSX"
$TempExchange = "mcx_sx"
Case "BSE"
$TempExchange = "bse_cm"
Case "CDS"
$TempExchange = "cde_fo"
Case "BFO"
$TempExchange = "bse_fo"
EndSwitch

If $MwatchScrips[$i][$Instrument] = "" Then


;Change Scripname
$TempSymbol = $MwatchScrips[$i][$Symbol]
ElseIf StringInStr($MwatchScrips[$i][$Instrument], "FUT")
And ($FNO <> "") Then
$TempSymbol = $MwatchScrips[$i][$Symbol] & $FNO
Else
$TempSymbol = $MwatchScrips[$i][$TSymbol]
EndIf

If $RTNOW = "Nest" Then


If $BidAskRates = "Yes" Then
if $BidAskQty = "Yes" Then
IniWrite(@WorkingDir & "\RTDMan.ini",
"RTDMan", "Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i]
[$TSymbol] & ";" & $TempSymbol & _
";LTP;LTT;Volume Traded Today;Open
Interest;;Bid Rate;Ask Rate;Bid Qty;Ask Qty")
;Updates Scripts in RTDMan.ini with Row
NO as scripts number by adding one and preserve value number
Else

IniWrite(@WorkingDir & "\RTDMan.ini",


"RTDMan", "Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i]
[$TSymbol] & ";" & $TempSymbol & _
";LTP;LTT;Volume Traded Today;Open
Interest;;Bid Rate;Ask Rate")
;Updates Scripts in RTDMan.ini with Row
NO as scripts number by adding one and preserve value number
EndIf
Else

IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan",


"Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i][$TSymbol]
& ";" & $TempSymbol & _
";LTP;LTT;Volume Traded Today;Open Interest;")
;Updates Scripts in RTDMan.ini with Row NO as scripts
number by adding one and preserve value number
EndIf
_GUICtrlListView_SetItemText($hiListView, $i, "Scrip
" & $CheckedScrips + $Pre & " Updated", 1)
Else
If $BidAskRates = "Yes" Then
if $BidAskQty = "Yes" Then
IniWrite(@WorkingDir & "\RTDMan.ini",
"RTDMan", "Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i]
[$TSymbol] & ";" & $TempSymbol & _
";Last Traded Price;Last Trade
Time;Volume Traded Today;Open Interest;;Best Buy Rate;Best Sell Rate;Best Buy
Qty;Best Sell Qty")
;Updates Scripts in RTDMan.ini with Row
NO as scripts number by adding one and preserve value number
Else
IniWrite(@WorkingDir & "\RTDMan.ini",
"RTDMan", "Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i]
[$TSymbol] & ";" & $TempSymbol & _
";Last Traded Price;Last Trade
Time;Volume Traded Today;Open Interest;;Best Buy Rate;Best Sell Rate")
;Updates Scripts in RTDMan.ini with Row
NO as scripts number by adding one and preserve value number
EndIf
Else
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Scrip" & $CheckedScrips + $Pre, $TempExchange & "|" & $MwatchScrips[$i][$TSymbol]
& ";" & $TempSymbol & _
";Last Traded Price;Last Trade Time;Volume
Traded Today;Open Interest;")
EndIf
_GUICtrlListView_SetItemText($hiListView, $i, "Scrip
" & $CheckedScrips + $Pre & " Updated", 1)

EndIf
Else
$TempSymbol = $NdxScrips[$i][0]
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Scrip" & $CheckedScrips + $Pre, $TempSymbol & ";" & $TempSymbol & ";Index
Value;;") ;Updates indexes in RTDMan.ini with Row NO as scripts number by adding
one and preserve value number
_GUICtrlListView_SetItemText($hiListView, $i, "Scrip
" & $CheckedScrips + $Pre & " Updated", 1)

EndIf
EndIf

Next

If $CheckedScrips = 0 Then
If WinActive("Backfill","") And $hiListView Then
MsgBox(64,"Real Time Data","No Scrip selected to add to
RTDMan.ini ..." & $hiListView,2)
EndIf
_Gui1()
EndIf

EndFunc ;==>_RTD_Script_Update

Func _RTD_PLUSCHART_Update()
; To display Plus chart

; Find total rows in Mktwatch

Local $Pluschart = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",


"Pluschart", "")

If $Pluschart = "Yes" Then


; Iterate selections for each selected scrip
Local $CheckedScrips = 0

For $i = 0 To $iRows - 1 ;search scrip through all rows of $Mktwatch


ControlListView($hWnd, "", "[CLASS:SysListView32;INSTANCE:3]",
"SelectClear")

If _GUICtrlListView_GetItemChecked($hiListView, $i) = True Then


; Is the scrip checked in RTD Window?
$CheckedScrips+=1
; Get TradingSymbol from each row
$TempScrip = ControlListView($hWnd, "",
"[CLASS:SysListView32;INSTANCE:3]", "GetText", $i, $TSymbol)

If $MwatchScrips[$i][$TSymbol] = $TempScrip Then ; Find if


script selected is same in MarketWatch
WinActivate($Title)
_GUICtrlListView_ClickItem($hWatch, $i, "Left", True)
; ZT - This fails to launch menu on first try of first scrip everytime
Sleep(500)
ControlSend($hWnd, $TempScrip,
"[CLASS:SysListView32;INSTANCE:3]", "+p") ;send shortcut key shift+p to open plus
chart
_GUICtrlListView_SetItemText($hiListView,$iRows,"
Chart Done ",1)
EndIf
EndIf

Next

If $CheckedScrips = 0 Then
MsgBox(64,"Real Time Data","Select atleast one Scrip Dude ...",2)
_Gui1()
EndIf

Sleep(3000)

EndIf

EndFunc ;==>_RTD_PLUSCHART_Update

Func _Get_Text_from_VWAP_Stat_Data_Table()
; This captures backfill data from VWAP/Hourly Statistics or Data Table

Local $CH = WinWait("[CLASS:ConsoleWindowClass]", "", 1)


; To get the class of RTDMan Console window
Local $VT = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"VWAPTry", "") ;Get Number of Vwap tries from RTDMan.ini file.
Local $MD = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"MinData", "") ; Check whether RTD window to be minimised
Local $Hide = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"RTDHide", "") ;Read Hide settings from ini file
Local $RTNOWVwap = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"Terminal", "") ; What terminal is it?
Local $DivideVolume = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"DivideVolume", "No") ; Divide MCX volume by LotQty
Local $Version = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"Version", "") ;Read Version Settings from ini File

If $Version = "Above" Then


$instance = 3
Else
$instance = 5
EndIf

TraySetIcon(@WorkingDir & "\icons\RTDMan.ico")


TraySetIcon("RTD.exe")

; Hide RTD window if if $Hide is Yes


If $Hide = "Before" Then
;If hide option before then hide RTD and RTDMan
;WinSetState($CH, "", @SW_HIDE)
WinSetState("Backfill", "", @SW_HIDE)
TrayTip("REAL TIME DATA", "RTD Minimized to System Tray", 5, 0)
EndIf

Opt("TrayOnEventMode", 1)
; Use event trapping for tray menu
Opt("TrayMenuMode", 3)
; Default tray menu items will not be shown.

If Not TrayItemGetText($hTray_Show_Item) = "Hide" Then


;If Not hide present in tray item

Else

TrayItemSetText($hTray_Show_Item, "Show")

EndIf
; end - Hide RTD window if if $Hide is Yes

;Start searching checked scrips through all rows of $hilListView (Backfill Window)
Local $CheckedScrips = 0

For $i = 0 To $iRows - 1

ControlListView($hWnd, "", 1003, "SelectClear") ;Control ID of market watch -


1003

If _GUICtrlListView_GetItemChecked($hiListView, $i) = True Then


; Is the scrip checked in RTD Window?
$CheckedScrips+=1
; Get TradingSymbol from each row
$CheckedScrip = ControlListView($hWnd, "", 1003, "GetText", $i,
$TSymbol)
Local $Sname1 = $MwatchScrips[$i][$TSymbol]

;MsgBox("","I am here", "$Sname1 - " & $Sname1 & " $TempScrip -


" & $TempScrip )

If $MwatchScrips[$i][$TSymbol] = $CheckedScrip Then

If Not $Plusbackfill Then


; Write to Listview what is being opened
_GUICtrlListView_SetItemText($hiListView,
$i,"Opening Vwap", 1)
Else
_GUICtrlListView_SetItemText($hiListView, $i,"Opening
DataTable" , 1)
EndIf

For $tr = 1 To 3
;Try 3 times to open -I think its not needed now
; _GUICtrlListView_SetItemText($hiListView, $i, "Try-"
& $tr, 1)
_GUICtrlListView_SetItemFocused($hWatch, $i)
;focus on checked scrip in Marketwatch
;WinActivate($Title)

$Occur = ControlListView($hWnd, "", 1003, "FindItem",


$CheckedScrip) ;find position of checked scrip and
ControlListView($hWnd, "", 1003, "Select", $Occur)
; select the item

If Not $Plusbackfill Then


;Open VWAP Statistics

If $CheckedScrips = 1 Then
;Open Vwap if it is first scrip
If $RTNOWVwap = "Nest" Then
;open Hourly statistics accordingly

;If not WinExists ( "[Title:Vwap


Statistics; Class:#32770]", "" ) Then

;added these two lines since


VWAP was not opening with ^+H
$aPos =
_GUICtrlListView_GetItemPosition($hWatch, $i)
ControlClick($hWnd, $Sname1,
1003, "right",1,$apos[0]+ 4, $aPos[1]+4)
ControlSend($hWnd, $Sname1,
1003, "tv")

$hStat = WinWait("[Title:Vwap
Statistics; Class:#32770]", "", $DataDelay) ;Wait to open Vwap Statistitics and get
handle

If $MD = "Yes" Then


;if minimize data yes then
minimize hourly statistics read settings from ini file.
WinSetState("[Title:Vwap
Statistics; Class:#32770]", "", @SW_MINIMIZE)
EndIf

;EndIf
Else
; $RTNOWVwap = "NOW"

;If not WinExists ( "[Title:Hourly


Statistics; Class:#32770]", "" ) Then
;_GUICtrlListView_ClickItem($
hWatch,$i,"left")
;WinMenuSelectItem ( $hWnd,
"$Sname", "&Market" , "&Hourly Statistics" )
ControlSend($hWnd, $Sname1,
1003, "^+h")
$hStat =
WinWait("[Title:Hourly Statistics; Class:#32770]", "", $DataDelay) ;Wait to open
Hourly Statistitics and get handle

If $MD = "Yes" Then


;minimize Hourly Statistics
after open

WinSetState("[Title:Hourly Statistics; Class:#32770]", "", @SW_MINIMIZE)


EndIf
;EndIf

EndIf

If ($hStat <> 9999 Or $hStat <> 0) Then


; If $hStat has changed then

If WinExists($hStat, "") Then


; See if Hourly Statiscs window opened
and
_GUICtrlListView_SetItemText($hiListView , $i, "Got it", 1); tell the user
ExitLoop
Else
$hStat = 9999
EndIf

ElseIf $tr = 3 Then


MsgBox(64, "Vwap Backfill", "Vwap
window refused to open ...trying next scrip" ,1)
ContinueLoop 2
EndIf

EndIf

Else
;Open Data Table

Opt("SendKeyDelay", 250)
; send keys to the tradingsymbol, Here it's
focusing but not actually selecting, so used down and up to get the actual text
selected.

$aPos =
_GUICtrlListView_GetItemPosition($hWatch, $i)
ControlClick($hWnd, $Sname1, $hWatch,
"right",1,$aPos[0]+4 ,$aPos[1]+4) ;First see if data table opens directly
ControlSend($hWnd, "", 1003, "pd")

; MsgBox("64", "Data Table status", "$Title = "


&$Title,2)
$hStat = WinWait("[Title:DataTable : " &
$Sname1 & "]", "", $DataDelay) ; Wait to open Data Table and get handle

If ($hStat = 9999 Or $hStat = 0) Then


; if it did not open then
_GUICtrlListView_SetItemText($hiListView,
$i,"Could not open" , 1)
_GUICtrlListView_SetItemText($hiListView,
$i,"Opening PlusChart" , 1)
ControlClick($hWnd, $Sname1, 1003,
"right",1,$apos[0]+ 4, $aPos[1]+4) ; Open Plus chart
ControlSend($hWnd, "", 1003, "p{ENTER}")
; $hChart = WinWait("[Title:Intraday
Chart : ]" & $Sname1, "", $DataDelay) ; Wait to chart window and get handle
Sleep(5000)
_GUICtrlListView_SetItemText($hiListView,
$i,"Openg Data Table again" , 1)
ControlClick($hWnd,"" , 1020,
"right",1,15,15) ; Open Plus chart
ControlSend($hWnd, "", 1020, "D")
$hStat = WinWait("[Title:DataTable : " &
$Sname1 & "]", "", $DataDelay) ; Wait to open Data Table and get handle
EndIf

If ($hStat <> 9999 Or $hStat <> 0) Then


; Check again

If WinExists($hStat, "") Then


; if data table opened then
WinActivate($hStat,"")
WinSetOnTop ( $hStat,"", 1 )

_GUICtrlListView_SetItemText($hiListView, $i, "Got it", 1) ; send


message to user
If $MD = "Yes" Then

WinSetState("[Title:DataTable; Class:#32770]", "", @SW_MINIMIZE) ; minimise


data table
EndIf
ExitLoop
Else
$hStat = 9999
EndIf

ElseIf $tr = 3 Then


Else
MsgBox(64, "NestPlus Backfill", "Data
Table refused to open ... Exiting" & @CRLF & @CRLF _
& "Check whether opens
manually, ...Start again or try other scrip")
ExitLoop 2
EndIf

EndIf
Next

If Not $Plusbackfill Then


;If VWAP then set combo box data and click on Get
Stistics
;WinActivate($hStat,"")
; Select Combo box options - ZT didnt select them
ControlCommand($hStat, "",1164, 'SelectString',
$MwatchScrips[$i][$Exchange]) ; Exchange ID=1164

; Series = Intrument for


NFO and = Series for NSE Equity

If ControlCommand($hStat, "", 1187, 'FindString',


$MwatchScrips[$i][$Instrument]) <> 0 Then
ControlCommand($hStat, "", 1187,
'SelectString', $MwatchScrips[$i][$Instrument]) ; Instrument ID=1187
Else
ControlCommand($hStat, "", 1187,
'SelectString', $MwatchScrips[$i][$Series]) ; Series Expiry
EndIf

ControlCommand($hStat, "", 1188, 'SelectString',


$MwatchScrips[$i][$Symbol]) ; Symbol ID=1188

If ControlCommand($hStat, "", 1192, 'IsEnabled', "")


<> 0 Then
ControlCommand($hStat, "", 1192,
'SelectString', $MwatchScrips[$i][$Series]) ; Expiry Date ID=1192
EndIf

If ControlCommand($hStat, "", 1039, 'IsEnabled', "")


<> 0 Then
ControlCommand($hStat, "", 1039,
'SelectString', $MwatchScrips[$i][$Optype]) ; Opt Type ID =1139
EndIf

If ControlCommand($hStat, "", 1189, 'IsEnabled', "")


<> 0 Then
ControlCommand($hStat, "", 1189,
'SelectString', $MwatchScrips[$i][$Strike]) ; Strike Price ID=1001
EndIf

ControlSetText($hStat, "", 1666, "1") ;Interval in


Min. ID=1666
Sleep(1000)
;WinActivate($hStat,"")
ControlFocus($hStat, "", 1025); Get Statistics Button
ID=1025
ControlSend($hStat, "", 1025, "{SPACE}") ;Click
presses on 'Get Statistics' Button
;ControlClick($hStat, "", 1025, "Left",1)
;Click on 'Get Statistics' Button

EndIf

Local $hStLV = ControlGetHandle($hStat, "",


"[Class:SysListView32; INSTANCE:1]") ; Get handle of Vwap or data table
Listview

Local $Sname
; First get scrip name from ini file

If $MwatchScrips[$i][$Instrument] = "" Then


;Change scrip name to Symbol (equity do not have Instrument name)
$Sname = $MwatchScrips[$i][$Symbol]
ElseIf StringInStr($MwatchScrips[$i][$Instrument],
"FUT") Then ; If it is future, get alias from ini file
Local $line = 1, $Sread, $Sread2

Do
$Sread = IniRead(@WorkingDir &
"\RTDMan.ini", "RTDMan", "Scrip" & $line , "F") ; Read Scrips from ini file
Local $Sread2 = StringSplit($Sread,"|;")
; MsgBox("64","Sread",$Sread2)
If $Sread = "F" then
_GUICtrlListView_SetItemText($hiListView,
$i,"New Scrip" , 1)
$Sname = $MwatchScrips[$i][$Symbol]
& $FNO
ExitLoop
ElseIf $MwatchScrips[$i][$TSymbol] =
$Sread2[2] Then
$Sname = $Sread2[3]
EndIf
$line += 1
Until $MwatchScrips[$i][$TSymbol] = $Sread2[2]

Else
;If option, keep name as is.
$Sname = $MwatchScrips[$i][$TSymbol]
EndIf
_GUICtrlListView_SetItemText($hiListView, $i, $Sname, 4)
_GUICtrlListView_SetItemText($hiListView, $i,
"GettingData", 1)
$pos = $i
ControlFocus($hStat, "", "[CLASS:SysListView32;
INSTANCE:1]")
; Sleep(2000)

; For $tr = 1 To $VT


;It tries for data avail or not, for user
defined tries.
Local $hTimer = TimerInit()
; Begin the timer and store the handle in a variable.
Do
; Wait till data filled in control - VWAP
or Data table
Sleep(2000) ; Sleep for 2 seconds.
Local $fDiff = TimerDiff($hTimer) ;
If $fDiff >= $DataDelay*1000 Then
; If there is no data in Hourly Stat/Datatable then goes
next try!
_GUICtrlListView_SetItemText($hiListView,
$i, "Try " & $tr & " No Data found", 1)
TrayTip($Sname,"Data not found",5,0)
Sleep(1000)
ExitLoop
EndIf
Local $STLVitemcount =
_GUICtrlListView_GetItemCount($hStLV)

Until $STLVitemcount > 0


; Next

$DayDeleteFrom =
_Date_Time_Convert(GUICtrlRead($D1) & GUICtrlRead($T1), "yyyy/MM/ddHH:mm:ss",
"yyyyMMddHHmmss")
$DayDeleteUpto =
_Date_Time_Convert(GUICtrlRead($D2) & GUICtrlRead($T2), "yyyy/MM/ddHH:mm:ss",
"yyyyMMddHHmmss")

Local $T = ""
_GUICtrlListView_SetItemText($hiListView, $pos,
"Processing", 2)
$ts = TimerInit()
_GUICtrlListView_SetItemText($hiListView,$pos,
$DayDeleteUpto,2)

For $j = 0 To $STLVitemcount - 1
; Start extracting data from listview
$sText = ""

If Not $Plusbackfill Then

For $k = 0 To 5

$sText1 = ControlListView($hStat, "",


1004, "GetText", $j, $k)
If $k = 0 Then
;Convert 12 Hour to 24 Hour format
Local $Hour =
Int(StringLeft($sText1, "2"))

If ($Hour <> 12 And


StringRight($sText1, 2) = "PM") Then
$Hour = $Hour + 12
$sText1 = $Hour &
StringMid($sText1, 3, 6)
Else
$sText1 = StringLeft($sText1,
8)
EndIf

$Checktime = @YEAR & @MON & @MDAY &


StringReplace(StringLeft($sText1, 8), ":", "")

;Skip if date and time is not


within selected
If Not (($Checktime) >=
($DayDeleteFrom) And ($Checktime) <= ($DayDeleteUpto)) Then
ContinueLoop 2
EndIf

_GUICtrlListView_SetItemText($hiListView,$pos,$DayDeleteFrom,1)
$sText1 =
StringReplace($sText1,":","")

EndIf

If $k = 5 And $MwatchScrips[$i]
[$Exchange] = "MCX" And $DivideVolume = "Yes" Then
$sText1 = $sText1/$MwatchScrips[$i]
[$LotQty] ;divide volume by lot qty
EndIf
$sText &= $sText1 & ","
Next

If $Client = "AB" Then


$T &= $Sname & "," & @YEAR & @MON & @MDAY
& "," & $sText & @CRLF ;Format CSV data for AB
Else
$T &= @YEAR & @MON & @MDAY & "," & $sText
& @CRLF ;Format CSV data for Ninja
EndIf

Else

For $k = 1 To 6
$sText1 = ControlListView($hStat, "",
"[CLASS:SysListView32; INSTANCE:1]", "GetText", $j, $k)

If $k = 1 Then ;Split Date Time into


separate fields and format them to yyyymmdd,HHmmss
$sText1 =
StringRegExpReplace($sText1, '(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2}):(\d{2})',
'$3$2$1,$4$5$6')
$Checktime = StringReplace($sText1,
",", "") ;

If Not (($Checktime) >=


($DayDeleteFrom) And ($Checktime) <= ($DayDeleteUpto)) Then
ContinueLoop 2
Else

_GUICtrlListView_SetItemText($hiListView,$pos,$Checktime,1)

_GUICtrlListView_SetItemText($hiListView,$pos,$DayDeleteUpto,2)
EndIf

EndIf

If $k = 6 And $MwatchScrips[$i]
[$Exchange] = "MCX" And $DivideVolume = "Yes" Then
$sText1 = $sText1/$MwatchScrips[$i]
[$LotQty] ; Divide volume by lot Qty
EndIf

$sText &= $sText1 & ","

Next

If $Client = "AB" Then


$T &= $Sname & "," & $sText & @CRLF
;Format CSV data for AB
Else
$T &= $sText & @CRLF
;Format CSV data for Ninja
EndIf

EndIf

Next

$td = Int(TimerDiff($ts)) / 1000


$td &= " seconds"
_GUICtrlListView_SetItemText($hiListView, $pos, $td, 2)
_GUICtrlListView_SetItemText($hiListView, $pos, "Data retrieved",
1)

FileDelete($DataDir & $Sname & ".txt")


If Not FileExists($DataDir) Then DirCreate($DataDir)
local $Filename = $DataDir & $Sname & ".txt"
;FileOpen($DataDir & $Sname & ".txt", 1)
FileOpen($Filename, 0)
FileWrite($Filename, $T)
FileClose($Filename)

Local $sString = FileReadToArray($Filename) ; read csv file into


1D array each line will be in one element.
;If Not @error Then _ArrayDisplay($sString, "Raw
Array")
Local $aRet = _ArraySortInOrder($sString) ; sort array
chronologically on start time
;If Not @error Then _ArrayDisplay($aRet, "Sorted
Array")

If $Client = "NT" Then


For $r = 0 To UBound($aRet,1) - 1
;Local $temp = $aRet[$r]
$aRet[$r] = StringReplace($aRet[$r],","," ",1)
$aRet[$r] = StringReplace($aRet[$r],",","; ")
;$aRet[$r] = $temp
Next
EndIf

_FileWriteFromArray($Filename, $aRet, @CRLF) ; write sorted array


into csv file
FileClose($Filename)

If $Client="AB" Then
_DeleteTics($Sname)
EndIf

If $Plusbackfill Then
WinClose($hStat)
$hStat = 9999
EndIf

TrayTip($Sname, "Backfill Completed", 5, 0)

EndIf

EndIf

Next

If Not $CheckedScrips Then


MsgBox(64,"Real Time Data","Select atleast one Scrip Dude .... ",2)
_Gui1()
EndIf
WinClose($hStat)
;Close Vwap Statistics window
$hStat = 9999

If $Hide = "After" Then


;hide to system after backfill completed.
GUIDelete($hgui)
EndIf

TrayTip("Real Time Data", "All Scrip Backfill Completed", 5, 0)

EndFunc ;==>_Get_Text_from_VWAP_Stat_Data_Table

Func To_Tray()
; Hide RTDman(console) and RTD window and send to tray
Local $CH = WinWait("[CLASS:ConsoleWindowClass]", "", 1)
If TrayItemGetText($hTray_Show_Item) = "Hide" Then
WinSetState($CH, "", @SW_HIDE)
WinSetState("RTD V", "", @SW_HIDE)
WinSetState("Backfill", "", @SW_HIDE)
TrayItemSetText($hTray_Show_Item, "Show")
Else
WinSetState($CH, "", @SW_SHOW)
WinSetState("RTD V", "", @SW_SHOW)
WinSetState("Backfill", "", @SW_SHOW)
TrayItemSetText($hTray_Show_Item, "Hide")
EndIf

EndFunc ;==>To_Tray

Func RTD_HIDE()
; Hide RTD window 16.08.2015

Local $CH = WinWait("[CLASS:ConsoleWindowClass]", "", 1)


WinSetState($CH, "", @SW_HIDE)
WinSetState("RTD V", "", @SW_HIDE)
;WinSetState("Backfill", "", @SW_HIDE)
TrayItemSetText($hTray_Show_Item, "Show")

EndFunc

Func On_Exit()
; Exit RTD
If $Client = "AB" Then
$oAB.SaveDatabase
$oAB = 0
EndIf

Exit
EndFunc ;==>On_Exit

Func _DeleteTics($Scrip)
; Delete existing tic data from Amibroker for selected period
Local $Filename = $DataDir & $Scrip & ".txt"

If StringRight($DayDeleteFrom, 6) <> 000000 Then ;Day delete from is 000000


means user wants to delete previous quotes.

Local $Stock, $ScripName, $QCount, $Quotes, $Quote


Local $oStocks = $oAB.Stocks
Local $Qty = $oStocks.Count
Local $i = 0
Local $j = 0
Local $k = 0
Local $DQ = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"DeleteQuote", "")

If $DQ = "Yes" Then ; If Delete Quotes is yes then start deleting


previous tick data
For $i = 0 To ($Qty - 1)
$Stock = $oStocks($i)
$ScripName = $Stock.Ticker

If $ScripName = $Scrip Then


$QCount = $Stock.Quotations.Count
$Quotes = $Stock.Quotations

_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
Else
EndIf

; import data from csv file into AB using rtd1 format, refresh chart
and save database
With $oAB
.Import(0, $Filename, "rtd1.format")
.RefreshAll
.SaveDatabase
EndWith

Else
#cs
; User wants to delete previous quotes with Tickmode 1
Local $sString = FileReadToArray($Filename) ; read csv file into 1D
array each line will be in one element.
;Local $aString = StringSplit($sString, @CRLF, 2);
;_ArrayDisplay($sString)
Local $aRet = _ArraySortInOrder($sString) ; sort array chronologically
on start time
If Not @error Then _ArrayDisplay($aRet, "Sorted Array")
_FileWriteFromArray($Filename, $aRet, @CRLF) ; write sorted array into
csv file

; import data from csv file into AB using rtd1 format, refresh chart
and save database
#ce
With $oAB
.Import(0, $Filename, "rtd2.format")
.RefreshAll
.SaveDatabase
EndWith

EndIf
_GUICtrlListView_SetItemText($hiListView, $pos, "Done", 1)

EndFunc ;==>_DeleteTics

Func _ArraySortInOrder($aStr)
; Sort backfill data ascending before import into Amibroker
; 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.
;

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

Func _SelectAll()
; Select all scrips in Listview
For $i = 0 To $iRows - 1

If Not $MwatchScrips[$i][$TSymbol] = 0 Then ;Inserted by Shankar


on 01-02-2015, If listview does not contain Tsymbol then it will not select it.
_GUICtrlListView_SetItemChecked($hiListView, $i)
EndIf

Next
EndFunc ;==>_SelectAll

Func _DeSelectAll()
; DeSelect all scrips in Listview
For $i = 0 To $iRows - 1
_GUICtrlListView_SetItemChecked($hiListView, $i, 0)
Next
EndFunc ;==>_DeSelectAll

Func _OnPlusbackfill()
; Manage Radio buttons in backfill window
Local $PlusState
If ControlCommand($hgui, "", $Npy, "IsChecked") Or ControlCommand($hgui, "",
$Npi, "IsChecked") Then
$PlusState = True
EndIf

If $PlusState Then
ControlEnable($hgui, "", $D1)
ControlEnable($hgui, "", $D2)
Else
GUICtrlSetData($D1, _NowCalc())
GUICtrlSetData($D2, _NowCalc())
ControlDisable($hgui, "", $D1)
ControlDisable($hgui, "", $D2)
EndIf

GUICtrlSetState($Button1, $GUI_ENABLE )
GUICtrlSetState($Button2, $GUI_ENABLE )
GUICtrlSetState($Button3, $GUI_ENABLE )
GUICtrlSetState($Button7, $GUI_ENABLE )
_FillList()

EndFunc ; ==>_OnPlusbackfill

Func _OnSettings()
; Open Settings window

If Not WinExists("Backfill Settings","") Then


$settingsgui = GUICreate("Backfill Settings", 365, 490, 800, 130,
BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX), "")
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSESettings", $settingsgui)
Local $h = 10
Local $v = 5
Local $vh = 18

GUISetFont(8, 400, 0, "MS Sans Serif")

$Label1 = GUICtrlCreateLabel("Nest/NOW Window Title", $h, $v, 140, $vh)


$Input1 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "Title", "NEST Trader 3"), $h + 170, $v, 150, $vh)
;NEST Trader 3.10.57.97.7
$Label2 = GUICtrlCreateLabel("Temporary CSV Folder Path", $h, $v + 20, 140,
$vh)
$Input2 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "CSVFolder", "R:\"), $h + 170, $v + 20, 150, $vh)

$Label3 = GUICtrlCreateLabel("Rename Future as" & @CRLF & "Symbol+ " & "-1/-
F1/_F1 etc.", $h, $v + 56, 140, 36)
$Input3 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "Future", ""), $h + 170, $v + 60, 150, 18)

$Label4 = GUICtrlCreateLabel("Options and Equity will remain as Trading


Symbol", $h, $v + 82, 400, $vh)

$Input4 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "Datatable", ""), $h + 170, $v + 40, 150, $vh)
$Label5 = GUICtrlCreateLabel("Plus Data Table Delay in Sec", $h, $v + 40,
140, 18)

$Input5 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "Pluschart", ""), $h + 170, $v + 95, 150, $vh)
GUICtrlSetData($Input5, "Yes|No", "")
$Label6 = GUICtrlCreateLabel("Plus Chart Open", $h, $v + 98, 140, $vh)

$Input6 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "Terminal", ""), $h + 170, $v + 118, 150, $vh)
GUICtrlSetData($Input6, "Nest|NOW", "")
$Label7 = GUICtrlCreateLabel("Terminal", $h, $v + 123, 140, $vh)
$Input7 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "DeleteQuote", ""), $h + 170, $v + 141, 150, $vh)
GUICtrlSetData($Input7, "Yes|No", "")
$Label8 = GUICtrlCreateLabel("Delete Quotes", $h, $v + 144, 140, $vh)

$Input8 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "VWAPTry", ""), $h + 170, $v + 164, 150, $vh)
$Label9 = GUICtrlCreateLabel("NO of VWAP Get Data Try", $h, $v + 167, 140,
$vh)

$Input9 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "MinData", ""), $h + 170, $v + 184, 150, $vh)
GUICtrlSetData($Input9, "Yes|No", "")
$Label10 = GUICtrlCreateLabel("Minimise Data When Backfill", $h, $v + 190,
140, $vh)

$Input10 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "BarPeriod", ""), $h + 170, $v + 207, 50, $vh)
GUICtrlSetData($Input10, "0|1000|60000", "")
$Label11 = GUICtrlCreateLabel("Candle Timeframe 0 | 1sec | 1min", $h, $v +
210, 168, $vh)

$Input11 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "RTDHide", ""), $h + 170, $v + 230, 150, $vh)
GUICtrlSetData($Input11, "After|Before|Don't Hide", "")
$Label12 = GUICtrlCreateLabel("Hide RTD Window", $h, $v + 230, 140, $vh)

$Label14 = GUICtrlCreateLabel("My Nest Version is", $h, $v + 253, 140, $vh)


$Label15 = GUICtrlCreateLabel("3.10.57.99", $h + 250, $v + 253, 140, $vh)
$Input14 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "Version", ""), $h + 170, $v + 253, 74, $vh)
GUICtrlSetData($Input14, "Above|Below", "")

$Input12 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "UpdateRTD", ""), $h + 170, $v + 276, 44, $vh)
GUICtrlSetData($Input12, "Yes|No", "")
$Label13 = GUICtrlCreateLabel("Update RTD Scrips / Preserve", $h, $v + 276,
160, $vh)

$Input13 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini",


"NestTrader", "Preserve", "1"), $h + 221, $v + 276, 44, $vh)

$Label16 = GUICtrlCreateLabel("RequestRefresh for AB 5.30 and below", $h, $v


+ 299, 200, $vh)
$Label17 = GUICtrlCreateLabel("BellWaitTime", $h, $v + 322, 140, $vh)
$Input16 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"RequestRefresh", "0"), $h + 200, $v + 299, 74, $vh)
$Input17 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"BellWaitTime", "15"), $h + 170, $v + 322, 74, $vh)

;added on 14-4-2016
$Label18 = GUICtrlCreateLabel("Client Amibroker|Ninja Trader", $h, $v + 345,
140, $vh)
$Input18 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Client", "AB"), $h + 170 , $v + 345, 50, $vh)
GUICtrlSetData($Input18, "AB|NT", "")

$Label19 = GUICtrlCreateLabel("Chart Refresh Period (50 to 1000)" , $h, $v +


368, 168, $vh)
$Input19 = GUICtrlCreateInput(IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"RefreshPeriod", "0"), $h + 170, $v + 368, 44, $vh)

$Label20 = GUICtrlCreateLabel("Bid|Ask Rates" , $h, $v + 391, 90, $vh)


$Input20 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "BidAskRates", "No"), $h + 120 , $v + 391,50, $vh)
GUICtrlSetData($Input20, "Yes|No", "")

$Label21 = GUICtrlCreateLabel("Bid|Ask Qty" , $h + 180, $v + 391, 100, $vh)


$Input21 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "BidAskQty", "No"), $h + 270 , $v + 391, 50, $vh)
GUICtrlSetData($Input21, "Yes|No", "")

$Label22 = GUICtrlCreateLabel("Archive" , $h , $v + 413, 100, $vh)


$Archive = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini", "RTDMan",
"Archive", False), $h + 201 , $v + 413, 50, $vh)
GUICtrlSetData($Archive, "true|false", "")

$Label23 = GUICtrlCreateLabel("Divide Volume by LotQty in backfill (MCX)" ,


$h , $v + 434, 200, $vh)
$Input23 = GUICtrlCreateCombo(IniRead(@WorkingDir & "\RTDMan.ini",
"NestTrader", "DivideVolume", "No"), $h + 270 , $v + 433, 50, $vh)
GUICtrlSetData($Input23, "Yes|No", "")

$Button1 = GUICtrlCreateButton("Save", $h, $v + 457, 90, 25)


GUICtrlSetOnEvent($Button1, "_OnSave")

$Button2 = GUICtrlCreateButton("Exit", $h + 250, $v + 457, 90, 25)


GUICtrlSetOnEvent($Button2, "CLOSEButton")

$Button3 = GUICtrlCreateButton("Set", $h + 295, $v + 270, 25, 25, $BS_ICON)


GuiCtrlSetImage(-1, @WorkingDir & "\icons\ini.ico", -1)
GUICtrlSetOnEvent($Button3, "_Set_ini")

GUISetState()

WinSetOnTop($settingsgui, "", 1)
EndIf
EndFunc ;==>_OnSettings

Func _OnSave()
; Save settings to ini file
Do

$Title = GUICtrlRead($Input1)
$DataDir = GUICtrlRead($Input2)
$FNO = GUICtrlRead($Input3)
$DataDelay = GUICtrlRead($Input4)
$Pluschart = GUICtrlRead($Input5)
$RTDMANSERVER = GUICtrlRead($Input6)
$DeleteQuote = GUICtrlRead($Input7)
$VWAPTry = GUICtrlRead($Input8)
$MinData = GUICtrlRead($Input9)
$BarPeriod = GUICtrlRead($Input10)
$RTDHide = GUICtrlRead($Input11)
$UpdateRTD = GUICtrlRead($Input12)
$Preserve = GUICtrlRead($Input13)
$Version = GUICtrlRead($Input14)
$RequestRefresh = GUICtrlRead($Input16)
$BellWaitTime = GUICtrlRead($Input17)
$Client = GUICtrlRead($Input18)
$RefreshPeriod = GUICtrlRead($Input19)
$BidAskRates = GUICtrlRead($Input20)
$BidAskQty = GUICtrlRead($Input21)
$Archive = GUICtrlRead($Archive)
$DivideVolume = GUICtrlRead($Input23)

If StringRight($DataDir, 1) <> "\" Then $DataDir &= "\" ; add backslash


to Temp CSV folder path if not present
Local $FileExists = FileOpen($DataDir & "MyFile.txt", $FO_CREATEPATH +
$FO_OVERWRITE) ; Open/create MyFile.txt in CSV folder path to check whether path is
correct.

;If cannot open/create MyFile.txt then


If $FileExists = -1 Then
MsgBox(64, "CSV folder Path incorrect", " Please give correct CSV
folder path and save")
ExitLoop
Else ; File exists so close it and delete.
FileClose($FileExists)
FileDelete($DataDir & "MyFile.txt")
EndIf

If $RefreshPeriod < 50 Then ;Rtd man does not accept lower value than
50, if lower than 50 show warining!
MsgBox(48, "Warning!", "RTDMan Refresh Period Interval Must not
be Lower Than 50 MilliSec (1000 = 1Sec)")
ExitLoop
EndIf

;Write defaults to ini file


IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Title", $Title)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "CSVFolder",
$DataDir)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "CSVFolderPath",
$DataDir)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Future", $FNO)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Datatable",
$DataDelay)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Pluschart",
$Pluschart)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Terminal",
$RTDMANSERVER)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RTDServerProgID",
$RTDMANSERVER & ".ScripRTD")
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "DeleteQuote",
$DeleteQuote)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "VWAPTry",
$VWAPTry)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "MinData",
$MinData)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "BarPeriod",
$BarPeriod)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "BarPeriod",
$BarPeriod)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "RTDHide",
$RTDHide)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "UpdateRTD",
$UpdateRTD)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Preserve",
$Preserve)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "Version",
$Version)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RequestRefresh",
$RequestRefresh)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "BellWaitTime",
$BellWaitTime)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "RefreshPeriod",
$RefreshPeriod)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "Client", $Client)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "BidAskRates",
$BidAskRates)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "BidAskQty",
$BidAskQty)
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "Archive", $Archive)
IniWrite(@WorkingDir & "\RTDMan.ini", "NestTrader", "DivideVolume",
$DivideVolume)

IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewTicData", "0")


IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewRawData", "0")
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewBarData", "0")
IniWrite(@WorkingDir & "\RTDMan.ini", "RTDMan", "ViewNTData", "0")

GUIDelete($settingsgui) ; If it was this GUI - we just delete the GUI


<<<<<<<<<<<<<<<
_Gui1()

Until 1
EndFunc ;==>_OnSave

Func CLOSEButton()
; Close Amibroker
If $oAB Then
$oAB.SaveDatabase
$oAB = 0
EndIf
Exit

EndFunc ;==>CLOSEButton

Func CLOSE_Gui2()
; Close backfill window
GUIDelete($hgui) ; If it was this GUI - we just delete the GUI
<<<<<<<<<<<<<<<
_Gui1()
EndFunc ;CLOSE_Gui2 ;ok

Func _OnInfo()
; Open Youtube site for help
ShellExecute("http://youtu.be/H-ZZHlZs6sM")
EndFunc ;==>_OnInfo

Func _Set_ini()
; Run shell program to write ini file
ShellExecute(@WorkingDir & "\RTDMan.ini")
EndFunc

Func _OnHelp()
; open help file
ShellExecute(@WorkingDir & "\Tutorial.chm")
EndFunc ;==>_OnHelp

Func CLOSESettings()
; close settings window
GUIDelete($settingsgui) ; If it was this GUI - we just delete the GUI
<<<<<<<<<<<<<<<
_Gui1()
EndFunc ;==>CLOSESettings

Func _On_DeleteTics_Click()
; Run program Delete_RT_Tics
$RunDeleteRT_Tics = ShellExecute ("DeleteRT_Tics.exe")
If not $RunDeleteRT_Tics Then
MsgBox(64, "DeleteTicsFailure", "Could not find the program
DeleteRT_Tics.exe in current directory")
EndIf

#cs
$DayDeleteFrom = _Date_Time_Convert(GUICtrlRead($D1) & GUICtrlRead($T1),
"yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
$DayDeleteUpto = _Date_Time_Convert(GUICtrlRead($D2) & GUICtrlRead($T2),
"yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
Local $DQ = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader", "DeleteQuote",
"")
Local $CheckedScrips = 0
Local $Ndxbackfill = ControlCommand($hgui, "", $Npi, "IsChecked")

If $DQ = "Yes" Then ; If Delete Quotes is yes then start deleting previous
tick data

For $i = 0 To $iRows - 1 ;search scrip through all rows of $Mktwatch

If _GUICtrlListView_GetItemChecked($hiListView, $i) = True Then


; Is the scrip checked in RTD Window?
$CheckedScrips+=1
$pos = $i

If $Ndxbackfill Then
$TempSymbol = $NdxScrips[$i][0]
Else

If $MwatchScrips[$i][$Instrument] = "" Then


;Change Scripname
$TempSymbol = $MwatchScrips[$i][$Symbol]
ElseIf StringInStr($MwatchScrips[$i][$Instrument],
"FUT") And ($FNO <> "") Then
$TempSymbol = $MwatchScrips[$i][$Symbol] & $FNO
Else
$TempSymbol = $MwatchScrips[$i][$TSymbol]
EndIf

EndIf

FileDelete($DataDir & $TempSymbol & ".txt")


_DeleteTics($TempSymbol)
EndIf

Next

If $CheckedScrips = 0 Then
MsgBox(64,"Real Time Data","Select atleast one Scrip Dude ...",2)
_Gui1()
EndIf

With $oAB
.RefreshAll
EndWith
Else

Msgbox(64, "Real Time Data", "Select Yes in the Delete Quote Option in
Settings and Try Again", 3)
EndIf
#ce
EndFunc ;==> _On_DeleteTics_Click

Func _OnIndexbackfill()
; backfill from index data table

ControlEnable($hgui, "", $D1)


ControlEnable($hgui, "", $D2)

;Get Handle of Index Window


$hNdxBar = WinGetHandle("Index Value Bar") ; Get handle for IndexValueBar
If not $hNdxBar then $hNdxBar = $hWnd ; If Index Value Bar not
present, then it is inside Nest Window
$hNdxWatch = ControlGetHandle($hNdxBar, "Index Value Bar", 1910) ;Get handle
of Index Bar Control

$NdxScrips = _Get_Scrips_from_IndexValueBar()

$iRows = UBound($NdxScrips, 1)
$iCols = UBound($NdxScrips, 2)
If $iListView Then GUICtrlDelete($iListView) ; if Listview is already
present, delete it

$iListView = GUICtrlCreateListView("Select Symbols to backfill", 10, 75, 350,


300) ; Created Listview to display and select scrips in RTD Window
$hiListView = GUICtrlGetHandle($iListView) ; obtained handle for the listview
_GUICtrlListView_SetExtendedListViewStyle($hiListView,
BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES), $LVSCW_AUTOSIZE) ; Make listview
with Check box and auto size.

_GUICtrlListView_InsertColumn($hiListView, 0, "Symbol ", 200)

For $i = 0 To $iRows - 1 ; Fill Listview with Index items


GUICtrlCreateListViewItem($NdxScrips[$i][0], $iListView)
Next

_GUICtrlListView_InsertColumn($hiListView, 2, "Progress", 95) ; Insert


Progress Column in Listview
_GUICtrlListView_InsertColumn($hiListView, 3, "Time", 95) ; Insert Time
Column in Listview

GUICtrlSetState($Button1, $GUI_ENABLE )
GUICtrlSetState($Button2, $GUI_ENABLE )
GUICtrlSetState($Button3, $GUI_ENABLE )
GUICtrlSetState($Button7, $GUI_ENABLE )

EndFunc

Func _Get_Scrips_from_IndexValueBar()
; capture indexes from index value bar
Local $iItemCount = _GUICtrlListView_GetItemCount($hNdxWatch), $i, $j
Local $iColumnCount = _GUICtrlListView_GetColumnCount($hNdxWatch)
Local $aReturn[$iItemCount][$iColumnCount] ;= [[$iItemCount, $iColumnCount]]

If Not $iItemCount Then ;if there is no index then show msg box, Added by
josh 24-01-2015
MsgBox(64, "No Index found", "Add atleast one index in Index Watch",2)
Return
EndIf
For $i = 0 To $iItemCount - 1
For $j = 0 To $iColumnCount - 1
$aReturn[$i][$j] = _GUICtrlListView_GetItemText($hNdxWatch,
$i, $j)
Next
Next
Return SetError(Number($aReturn[0][0] = 0), 0, $aReturn)
MsgBox(64, "Real Time Data", "Insert Altleast one Index in your
Nest Index value bar", 3)

EndFunc ;==>_Get_Scrips_from_IndexValueBar

Func _Get_Index_Data_from_DataTable(ByRef $NdxScrips) ; Backfill index


data from data table
; Iterate selections for each selected scrip
Local $CheckedScrips = 0
Local $T = ""

For $i = 0 To $iRows - 1 ;search scrip through all rows of $Mktwatch


ControlListView($hNdxBar,"",1910, "SelectClear")

If _GUICtrlListView_GetItemChecked($hiListView, $i) = True Then


; Is the scrip checked in RTD Window?
$CheckedScrips+=1
Sleep(1000)
Local $MD = IniRead(@WorkingDir & "\RTDMan.ini", "NestTrader",
"MinData", "") ; Check whether RTD window to be minimised
Local $j, $sText, $Sname

; Get Index from each row


$TempScrip = ControlListView($hNdxBar,"",1910, "GetText",$i,0)

If $NdxScrips[$i][0] = $TempScrip Then


_GUICtrlListView_SetItemText($hiListView,$i,"Opening
DataTable",1)
$Sname = $TempScrip

For $tr = 1 to 3
_GUICtrlListView_SetItemText($hiListView,$i,"Try-" &
$tr,1)
Sleep(1000)
_GUICtrlListView_ClickItem($hNdxWatch,$i,"right")
Sleep(1000)
ControlSend(("[CLASS:#32768]"), "",0, "p")
; Sleep(2000)
; $hChart = WinWait("[Title:Intraday Chart : ]" &
$Sname, "", $DataDelay) ; Wait to chart window and get handle
Sleep(5000)
; ControlSend($hChart,"",1020,"^D")
; Now open data table from Plus chart
ControlClick($hWnd,"Intraday
chart",1020,"right",1,15,15)
Sleep(1000)
ControlSend($hWnd, "Intraday chart",1020, "D")

$hStat = WinWait("[Title:DataTable : " & $Sname & ";


Class:#32770]", "", $DataDelay) ; Wait to open Data Table and get handle

If $MD = "Yes" Then


WinSetState("[Title:DataTable;
Class:#32770]", "", @SW_MINIMIZE)
EndIf

If ($hStat <> 9999 Or $hStat <> 0) Then

If WinExists($hStat, "") Then

_GUICtrlListView_SetItemText($hiListView, $i, "Got it", 1)


ExitLoop
Else
$hStat = 9999
EndIf

ElseIf $tr = 3 Then


MsgBox(64, "NestPlus Backfill", "Data
Table refused to open ... Exiting" & @CRLF & @CRLF _
& "Check whether opens
manually, ...Start again or try other scrip")
Exit
EndIf
Next

$DayDeleteFrom = _Date_Time_Convert(GUICtrlRead($D1) &


GUICtrlRead($T1), "yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
$DayDeleteUpto = _Date_Time_Convert(GUICtrlRead($D2) &
GUICtrlRead($T2), "yyyy/MM/ddHH:mm:ss", "yyyyMMddHHmmss")
; Get handle of ListView /Data and check column order
Local $hStLV = ControlGetHandle($hStat, "",
"[Class:SysListView32; INSTANCE:1]")
Local $STLVitemcount =
_GUICtrlListView_GetItemCount($hStLV)

If $STLVitemcount = 0 Then ; If there is no data in


Hourly Stat/Datatable then goes next try!
_GUICtrlListView_SetItemText($hiListView, $i,
"Try " & $tr & " No Data found", 1)
TrayTip($Sname,"Data not found",5,0)
Sleep(1000)
ExitLoop
EndIf

$pos = $i
_GUICtrlListView_SetItemText($hiListView, $pos,
"Processing", 2)
$ts = TimerInit()
_GUICtrlListView_SetItemText($hiListView,$pos,
$DayDeleteUpto,2)
For $j = 0 To $STLVitemcount - 1
$sText = ""

For $k = 1 To 6
$sText1 = ControlListView($hStat, "",
"[CLASS:SysListView32; INSTANCE:1]", "GetText", $j, $k)

If $k = 1 Then ;Split Date Time into


separate fields and format them to yyyymmdd,HHmmss
$sText1 =
StringRegExpReplace($sText1, '(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2}):(\d{2})',
'$3$2$1,$4$5$6')
$Checktime = StringReplace($sText1,
",", "") ;

If Not (($Checktime) >=


($DayDeleteFrom) And ($Checktime) <= ($DayDeleteUpto)) Then
ContinueLoop 2
Else

_GUICtrlListView_SetItemText($hiListView,$pos,$Checktime,1)
;_GUICtrlListView_SetItemText
($hiListView,$pos,$Checktime,2)
EndIf

EndIf

$sText &= $sText1 & ","

Next

; TODO - remove hardcode - allow manual alias in search


If $Client="AB" Then
; $T &= $Sname & "," & @YEAR & @MON & @MDAY & "," &
$sText & @CRLF ;Format CSV data
$T &= $Sname & "," & $sText & @CRLF ;Format CSV
data
Else
$T &= @YEAR & @MON & @MDAY & " " & $sText & @CRLF
;Format CSV data
EndIf
; MsgBox(64, "NestPlus Backfill",$T,3)

Next

_GUICtrlListView_SetItemText($hiListView,$pos,"Data
retrieved",1)
;FileDelete($DataDir & $Sname & ".txt")
If Not FileExists($DataDir) Then DirCreate($DataDir)

$Filename = FileOpen($DataDir & $Sname & ".txt", 1)


FileWrite($Filename, $T )
FileClose($Filename)

If $Client="AB" Then
_DeleteTics($Sname)
EndIf
_GUICtrlListView_SetItemText($hiListView, $pos, "Done", 1)
WinClose($hStat)
$hStat=9999
EndIf

EndIf

Next

If $CheckedScrips = 0 Then
MsgBox(64,"Real Time Data","Select atleast one Scrip Dude ...",2)
_Gui1()
EndIf

EndFunc ;==>_Get_Text_from_Hourly_Statistics

You might also like