I will put all of the source code here instead of explaining it in plain english. Since the AutoIt is BASIC like language it easy to understand.
#include <ie.au3>
#include <string.au3>
#include <array.au3>
#Include <date.au3>
#include <sqlite.au3>
#include <sqlite.dll.au3>
#include "myFunctions.au3"
#include <winhttp.au3>
; Set variables (read from database)
$START_TIME = fnFetchConfigs("START_TIME") ; START_TIME
$TICKER_LIST = fnFetchTickerList() ; TICKER_LIST
$REFRESH_INTERVAL = fnFetchConfigs("REFRESH_INTERVAL") ; REFRESH_INTERVAL
$RUN_TIME = fnFetchConfigs("RUN_TIME") ; RUN_TIME
$myStartDateTime = fnFetchConfigs("STARTED_AT") ; STARTED_AT
;-------------------- @YEAR & "/" & @MON &
; "/" & @MDAY & " " & $START_TIME
If _DateTimeFormat( _NowCalc(), 5) > $START_TIME Then
$MinutesSinceStarted = _DateDiff ( 'n', $myStartDateTime, _NowCalc())
WriteToLogFile ("Started capturing quotes.")
Dim $iBasePrc, $chars, $QuoteFileName
Dim $AppEndDateTime, $QuoteString
while ($RUN_TIME - $MinutesSinceStarted) >= 0
;split the $TICKER_LIST and get quotes for all the tickers in the list
$SplitTICKER_LIST = StringSplit($TICKER_LIST, "")
for $i = 1 to UBound($SplitTICKER_LIST, 1)-1
$TICKER = $SplitTICKER_LIST[$i]
;msgbox (0, $i, $SplitTICKER_LIST[$i])
$QuoteString = fnHTTP_GetQuote($SplitTICKER_LIST[$i])
if StringLen($QuoteString) > 10 Then
$QuoteArray = _StringSplit($QuoteString, "")
$CurrentQuote = StringReplace ($QuoteArray[2], ",", "")
$myTime = $QuoteArray[0] & " " & $QuoteArray[1]
$iBasePrc = Number($iBasePrc)
$CurrentQuote = Number($CurrentQuote)
fnWriteToDB ("UPDATE ICICI_WatchList SET &_ CurrentQuote = "& $CurrentQuote &", &_
LocalDateTime = CURRENT_TIMESTAMP WHERE Ticker = '" & $TICKER & "';")
;ConsoleWrite ("Quotes updated.." & @CRLF)
$iBasePrc = ""
$QuoteString=""
$chars = ""
Else
WriteToLogFile ("HTTP Request might have failed!")
EndIf
NEXT ;$i
;interval
sleep ($REFRESH_INTERVAL) ; every 20 seconds..
$MinutesSinceStarted = _DateDiff ( 'n', $myStartDateTime, _NowCalc())
;ConsoleWrite($RUN_TIME - $MinutesSinceStarted & @CRLF)
wend
WriteToLogFile ("End of trading session.")
;Shutdown(32) ; put computer in Sleep mode
Else
WriteToLogFile ("Trading session not yet started.")
EndIf
Exit
;====================================================================================================
Func fnHTTP_GetQuote($Ticker)
Dim $TradeDate, $TradeTime, $LatestQuote
$hw_open = _WinHttpOpen()
$hw_connect = _WinHttpConnect($hw_open, "getquote.icicidirect.com")
$h_openRequest = _WinHttpOpenRequest($hw_connect, "GET", "/trading/equity/trading_stock_quote.asp?Symbol=" & $Ticker)
_WinHttpSendRequest($h_openRequest)
_WinHttpReceiveResponse($h_openRequest)
If _WinHttpQueryDataAvailable($h_openRequest) Then
$data = ""
While 1
$chunk = _WinHttpReadData($h_openRequest, 1)
If Not @extended Then ExitLoop
;ConsoleWrite($chunk & @CRLF)
$data &= $chunk
WEnd
;ConsoleWrite($data & @CRLF)
;msgbox (0, "StringLen($data)", StringLen($data))
;-- error stringLen 7808
;-- good stringLen 12995
If StringLen($data) > 10000 Then ; good request return
;=========== LAST TRDED DATE START ================
;First cut
$mySubString = "DATE "
$StartAt = StringInStr($data, $mySubString)
$TradeDate = StringMid($data, $StartAt, 200)
;Second cut
$mySubString = "" "
$StartAt = StringInStr($TradeDate, $mySubString)
$TradeDate = StringMid($TradeDate, $StartAt, 200)
;Third cut
$mySubString = ">"
$StartAt = StringInStr($TradeDate, $mySubString)
$TradeDate = StringMid($TradeDate, $StartAt+1, 200)
;Fourth cut
$mySubString = "
$EndAt = StringInStr($TradeDate, $mySubString)
$TradeDate = StringMid($TradeDate, 1, $EndAt-1)
;msgbox (0, "Last Trade Date", $TradeDate)
;=========== LAST TRDED DATE ENDS ================
;=========== LAST TRDED TIME START ================
;First cut
$mySubString = "LAST TRADED "
$StartAt = StringInStr($data, $mySubString)
$TradeTime = StringMid($data, $StartAt, 200)
;Second cut
$mySubString = "" "
$StartAt = StringInStr($TradeTime, $mySubString)
$TradeTime = StringMid($TradeTime, $StartAt, 200)
;Third cut
$mySubString = ">"
$StartAt = StringInStr($TradeTime, $mySubString)
$TradeTime = StringMid($TradeTime, $StartAt+1, 200)
;Fourth cut
$mySubString = "
$EndAt = StringInStr($TradeTime, $mySubString)
$TradeTime = StringMid($TradeTime, 1, $EndAt-1)
;msgbox(0, "Latest Trde Time", $TradeTime)
;=========== LAST TRDED TIME ENDS ================
;~ ;=========== LASTEST QUOTES STARTS ================
;First cut
$mySubString = "LAST TRADE "
$StartAt = StringInStr($data, $mySubString)
$LatestQuote = StringMid($data, $StartAt, 200)
;Second cut
$mySubString = "" "
$StartAt = StringInStr($LatestQuote, $mySubString)
$LatestQuote = StringMid($LatestQuote, $StartAt, 200)
;Third cut
$mySubString = ">"
$StartAt = StringInStr($LatestQuote, $mySubString)
$LatestQuote = StringMid($LatestQuote, $StartAt+1, 200)
;Fourth cut
$mySubString = "
$EndAt = StringInStr($LatestQuote, $mySubString)
$LatestQuote = StringMid($LatestQuote, 1, $EndAt-1)
;msgbox(0, "Latest Quoted", $LatestQuote)
;~ ;=========== LASTEST QUOTES ENDS ================
EndIf
EndIf
_WinHttpCloseHandle($h_openRequest)
_WinHttpCloseHandle($hw_connect)
_WinHttpCloseHandle($hw_open)
;ConsoleWrite ($TradeDate & "" & $TradeTime & "" & $LatestQuote & @CRLF)
Return($TradeDate & "" & $TradeTime & "" & $LatestQuote)
EndFunc
This above code is the one which captures the ticker/stock price and writes it to the ICICI_Watchlist >> CurrentQuote column. As soon as the column is updated
with new price the trigger on that table fires up and checks if the price is
closer by the expected target price (QuoteVariance), if yes it will generate
a signal.
Is the signal buy or sell ? - this depends on QuoteVariance. If QuoteVariance
is set as minus that means you are looking for price going lower and lower, so
BUY signal is generated for PreviousClosing-(PreviousClosing*QuoteVariance)
Similarly if the you are looking to sell your holdings if price goes above +5% (QuoteVariance) of previous day closing then SELL signal is generated
In the next part, I will explain how this signal is processed and order is actual placed.
2 comments:
Hi, so when is the next section of your blog coming? And how is your stock trading automation working out? Thanks!
Hi...
Thank you for sharing the code. I wanted to work on this possibility from some time.
Please make time to post the last section.
Post a Comment