22. The MakeCSV subroutine generates MYcsv.csv file in C:\ i.e. Root of C Drive.
Sub MakeCSV()
Dim fs As Object, a As Object, i As Integer, s As String, t As String, CellValue As String
'Create a file object for writing
Set fs = CreateObject("Scripting.FileSystemObject")
'Set a as C:\Mycsv.csv, True means overwrite on old file. You can change the location and name of csv file by editing below line.
Set a = fs.CreateTextFile("c:\MyCSV.csv", True)
'Select Cell A1 in the sheet containing quotations i.e. NOW
Application.Goto Workbooks("NSE-NOW-RT2.xlsm").Sheets("Now").Range("A1:A1")
'Iterate through all cells/columns from Row 3 to Row 65536 in which quotations exist and write quotations in the csv file.
For r = 3 To Range("A65536").End(xlUp).Row
s = ""
c = 1
While Not IsEmpty(Cells(r, c))
If Cells(r, c).Value = "0" Then
CellValue = ""
Else
CellValue = Cells(r, c).Value '
End If
s = s & CellValue & "," 'Add contents of current cell to string 's' and a comma
c = c + 1
Wend
a.writeline s 'write contents of String S to the csv file.
Next r 'go to next row
End Sub
23. The InitialiseAB subroutine starts AmiBroker.
Sub InitialiseAB()
On Error Resume Next
Set AB = GetObject(, "Broker.Application")
If AB Is Nothing Then ' True if not running
'Uncomment the line below if AmiBroker is not starting automatically
'ActiveWorkbook.FollowHyperlink "C:\Amibroker\Broker.exe", NewWindow:=True
Set AB = CreateObject("Broker.Application")
End If
AB.Visible = True
'Get the path of database loaded by AmiBroker
ABPath = AB.DatabasePath
'Get the database path from C1 cell in the Sheet NOW.
DBPath = Workbooks("NSE-Now-RT2.xlsm").Sheets("Now").Cells(1, 3).Value
If ABPath <> DBPath Then
AB.LoadDatabase (DBPath)
End If
'Edit and uncomment the lines below if you want to start a specific scrip or layout.
'AB.ActiveDocument ("NIFTY")
'AB.LoadLayout ("Realtime")
End Sub
24. The CallAmiBroker subroutine calls AmiBroker to import quotations from text file named "C:\MyCSV.csv" (we created in subroutine MakeCSV) as per the order of columns stated in the NSENOW2.format file. AmiBroker looks at the $FORMAT line in NSENOW2.format file which at present is like this-
$FORMAT Date_MDY, Ticker, Time, Close, Volume, Appendticker, Appendticker
Sub CallAmiBroker()
FileName = "C:\MyCSV.csv"
Call AB.Import(0, FileName, "NSENOW2.format")
'This line tells AmiBroker to refresh all charts
Call AB.RefreshAll
End Sub