Sunday, May 31, 2009

Part III - Order Placement

In Part II - Trade Signal Generation, we looked at how Buy/Sell sigal is generated in the following section I will explain how these signal is picked up and processed.

Here is the AutoIt script to achieve the purpose,




#include <IE.au3>
#include <String.au3>
#include <array.au3>
#Include <Date.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include "myFunctions.au3"

;~ ; Set variables (read from database)
;~ ;Dim $myStartDateTime


Dim $Ticker, $iBasePrc, $chars, $isAliveCounter
$SIGNAL_TICKER_LIST = ""
$isAliveCounter = 0
$START_TIME = fnFetchConfigs("START_TIME") ; START_TIME
$UID = fnFetchConfigs("UID") ; $UID
$PWD = fnFetchConfigs("PWD") ; $PWD
$RUN_TIME = fnFetchConfigs("RUN_TIME") ; RUN_TIME
$CHUNK_AMT = fnFetchConfigs("CHUNK_AMT") ; CHUNK_AMT
$myStartDateTime = fnFetchConfigs("STARTED_AT") ; STARTED_AT ;-------------------- @YEAR & "/" & @MON & "/" & @MDAY & " " & $START_TIME


If $UID="" OR $START_TIME="" OR $RUN_TIME="" OR $CHUNK_AMT="" OR $myStartDateTime="" then
msgbox (0, "Alert", "Application variables are not set properly, EXIT'ing")
Exit
EndIf


If _DateTimeFormat( _NowCalc(), 5) > $START_TIME Then

WriteToLogFile ("New watch cycle Started.")


$minutesRemaining = _DateDiff ( 'n', $myStartDateTime, _NowCalc())

WHILE ($RUN_TIME - $minutesRemaining) >= 0


; this is debugging piece only..
$isAliveCounter = $isAliveCounter + 1
if $isAliveCounter > 360 Then
WriteToLogFile ("Watch cycle is alive.")
$isAliveCounter = 0 ;--reset the counter
EndIf
; debugging piece ends..


if fnSignalExists() > 0 Then

WriteToLogFile ("Signal found, initiating Order placement")
fnPlaceOrder()
WriteToLogFile ("Control returned to watching cycle")

EndIf

;==================================================
; moved to a seperate PlaceOrder function
;==================================================

Sleep(fnFetchConfigs("PLACE_ORDER_REFRESH")) ;Refresh every 5 seconds
$minutesRemaining = _DateDiff ( 'n', $myStartDateTime, _NowCalc())
$SIGNAL_TICKER_LIST = ""
;ConsoleWrite("Looping... " & $minutesRemaining & @CRLF)

WEND

WriteToLogFile ("End of watch cycle, system going to Sleep mode")
;Shutdown(32) ; put computer in Sleep mode

Else

WriteToLogFile ("Trading Session has not yet started!")

EndIf



Func fnPlaceOrder()

; Site login begins
WriteToLogFile ("Connecting to ICICI Direct.")

$oIE = fnOpenURL("https://secure.icicidirect.com/customer/logon.asp", "4000")
WriteToLogFile ("Logging-in to site.")

$oForm = _IEFormGetObjByName ($oIE, "loginform")
$oElement = _IEFormElementGetObjByName ($oForm, "FML_USR_ID")
_IEFormElementSetValue ($oElement, $UID)

$oElement = _IEFormElementGetObjByName ($oForm, "FML_USR_USR_PSSWRD")
_IEFormElementSetValue ($oElement, $PWD)

$oElement = _IEFormElementGetObjByName($oForm, "Submit1")
_IEAction ($oElement, "click")
Sleep(5000) ; some kind of IE context issue, looking for work around.
_IELoadWait ($oIE)

; Site login Ends

_IENavigate ($oIE, "https://secure.icicidirect.com/trading/equity/trading_basket_order.asp", 1)

;~ $oIE = _IEAttach ("ICICI direct.com :: Trading :: Basket Order", "title")

Sleep(2000)
_IELoadWait ($oIE)

WriteToLogFile ("Placing Order.")

$SplitTICKER_LIST = StringSplit(fnFetchSignalTickerList(), "?")

For $i = 1 to UBound($SplitTICKER_LIST, 1)-2

$OrderValues = StringSplit($SplitTICKER_LIST[$i], "")

$iBasePrc = Round(Number($OrderValues[2]), 1)

If $iBasePrc <> "" Then

$iQty = Int($CHUNK_AMT/$iBasePrc)

if $iQty * $iBasePrc < $CHUNK_AMT then $iQty = $iQty+1

$oForm = _IEFormGetCollection ($oIE, 1)

$oElement = _IEFormElementGetObjByName ($oForm, "FML_ORD_XCHNG_CD" & $i) ; Exchange
_IEFormElementOptionSelect ($oElement, "NSE", 1, "byText")

$oElement = _IEFormElementGetObjByName ($oForm, "FML_ORD_PRDCT_TYP" & $i) ; Product
_IEFormElementOptionSelect ($oElement, "CASH", 1, "byText")

$oElement = _IEFormElementGetObjByName ($oForm, "FML_ACTION" & $i) ; Action
_IEFormElementOptionSelect ($oElement, $OrderValues[3], 1, "byText")

$oElement = _IEFormElementGetObjByName ($oForm, "FML_STCK_CD" & $i) ; Ticker/Stock
_IEFormElementSetValue ($oElement, $OrderValues[1])

$oElement = _IEFormElementGetObjByName ($oForm, "FML_QTY" & $i) ; Quantity
_IEFormElementSetValue ($oElement, $iQty)

_IEFormElementRadioSelect ($oForm, "L", "FML_ORD_TYP" & $i, 1, "byValue")

$oElement = _IEFormElementGetObjByName ($oForm, "FML_ORD_LMT_RT" & $i) ; Limit price
_IEFormElementSetValue ($oElement, $iBasePrc)

fnWriteToDB("update ICICI_Signal set SignalActive = 0 where SignalActive = 1 and SignalTicker='"& $OrderValues[1] &"';")
WriteToLogFile ("Buy signal is de-activitated for " & $OrderValues[1])

$iBasePrc = ""
$Ticker = ""

EndIf
Next ; $i



;~ it was wasting some time so disabled it
;~ ;Take screenshot after basket it filled
;~ WriteToLogFile ("Order form screenshot - " & TakeScreenShot("ICICI direct.com :: Trading :: Basket Order"))
;~ Sleep(1000)

WriteToLogFile ("Order form submitted")
$oElement = _IEFormElementGetObjByName ($oForm, "Submit")
_IEAction ($oElement, "click")
Sleep(2000)
_IELoadWait ($oIE)


;===== Confirmation window
$oForm = _IEFormGetCollection ($oIE, 1)

$oElement = _IEFormElementGetObjByName ($oForm, "CheckUnCheck")
_IEAction ($oElement, "click")

WriteToLogFile ("Order confirmation screenshot - " & TakeScreenShot("ICICI direct.com :: Trading :: Basket Order Verification"))

$oElement = _IEFormElementGetObjByName ($oForm, "Submit_Butt")
_IEAction ($oElement, "click")
Sleep(2000)
WriteToLogFile ("Order Confirmation done.")

_IELoadWait ($oIE)
WriteToLogFile ("Confirmation submitted screenshot - " & TakeScreenShot("ICICI direct.com :: Trading :: Basket Order Acknowledgement"))
;===== Confirmation window Ends


;~ msgbox (0, "CAUTION", "Hard exit ! next")
;~ Exit



; signing out
WriteToLogFile ("Signing out.")
fnClickOnLink ("Logout", $oIE)
sleep(7000)
WinActivate("Windows Internet Explorer")
sleep(1000)
SendKeepActive("Windows Internet Explorer")
Send("{ENTER}")
_IEQuit ($oIE)
WriteToLogFile ("Exiting PlaceOrder function.")

EndFunc




As I said before I am not going to explain this script in plain english as its already simple, this is the reason I recommend AutoIt as the best hobbyist programmer language

What about the order quantity ? - The order quantity is based on CHUNK_AMOUNT variable set in ICICI_Configs table. It will check how many units it can buy with
CHUNK_AMOUNT/OrderPrice rounded off to integer. This allows buying any stock in chunks rather than in one single go.

Ones the order placement is done it will update the YstClosingQuote value for the respective ticker/symbol. Look at ICICI_Log table screenshot in Part II to get better understanding on sequence of activities.

No comments:

Testing Google Search Results

Twin Star Adjustable Height Desk, White (ODP1055548D908) https://www.costcobusinessdelivery.com/Twin-Star-Adjustable-Height-Des...