I know that this is a rather long post but as I really require a solution to this problem, I earnestly request all the AHK gurus to please spare me their valuable time and effort and assist me in solving this. Sincere Thanks in advance…
I use a trading software (called SACTP) to buy and sell stuff. The program I use allows me to load file(s) from my hard drive (only one file at a time), which have been prepared by me in the Microsoft Excel (.xls) format, containing details on the articles that I would like to trade in. I load these files in the front end trading software (SACTP) and then execute them as and when required. Collectively, I have forty such files with my trade details – twenty each for the buy side and for the sell side. The files are named as 01.xls, 02.xls --- 20.xls for both the Buy and Sell side. Each of these files has different data in them.
For loading the Buy side and Sell side files in the front end trading software (SACTP), I always follow an ascending and descending order pattern
contrary to each other, which could also be required to be reversed at any point during my trading hours. Depending on the market dynamics, I decide on which order pattern (ascending or descending), I would like to employ for loading the Buy side and Sell side files.
The files need to be loaded in an
opposite order pattern from one another and follow a
sequential order pattern from within – that is – let me explain –
Opposite order pattern - if the buy side files are being loaded in an ascending order pattern then the sell side files will always need to be loaded in a descending order pattern. Like I mentioned earlier these files are not loaded together (at the same time) but as and when an opportunity arises for either buy or sell. This in other words means that if I am following the ascending order pattern for the buy side files and have loaded
and executed file number five for the buy side then the next one for the buy side would always be file number six. Not only that I could continue to load and execute the Buy side files one after another in an ascending order pattern (file number six followed by seven, eight etc.) till I see the need to load and execute a Sell side file. This is totally market dependant. Only thing is - Whilst the buy side is adopting the ascending order pattern at the same time the sell side files would be following the descending order pattern. Therefore, for the sell side, if I have loaded and executed file number fifteen then the next one for the sell side would be file number fourteen.
The reverse would be in effect if it is the descending order pattern for the buy side files and the ascending order pattern for the sell side files.
Sequential order pattern from within – The file number for the buy side and sell side do
not have to match regardless of which order pattern (ascending or descending pattern) is being followed – that is – if I have loaded and executed file number five for the buy side it does not mean that file number five would need to be loaded and executed for the sell side. As mentioned above, the file to be loaded is determined by the user and it follows a sequential order pattern from within and not interdependent on the other.
The reversals of the ascending and descending order patterns – that is – shifting the buy or sell side files from either their ascending to descending order pattern or vice-versa needs to be changed at any point during the course of the trading hours dependant totally on the market dynamics prevalent at the time.
Please Note - I would like to mention that these files are always used in a
loop – that is – as mentioned earlier – there are twenty files each for the buy and sell side, which are used in a sequential order. Depending on the pattern for which they are loaded (ascending or descending), the files would move in a loop upwards or downwards. Like if the ascending order pattern were being followed then once file number twenty (the last file in the list) is executed, I would like to go back to file number one (the first file in the list) to be loaded and then be ready to be executed. Similarly if the descending order pattern were being followed then once file number one (the first file in the list) is executed, I would like to go back to file number twenty (the last file in the list) to be loaded and then be ready to be executed.
Using AHK I have written a script, which makes loading these files a lot easier and quicker as opposed to doing it manually
and I have managed to get the script to make the files load sequentially but these are things that I cannot make it do –
- Loop it back to file number one if the ascending pattern is in operation
- Loop it back to file number twenty if the descending pattern is in operation
- Be able to change the ascending / descending pattern on-the-fly perhaps by a supported GUI, Hotkey etc. or any other method that I cannot think of to reverse the order pattern for the buy side and sell side.
- A few quirks in the code that I have written, which I will highlight in the code tags below
This is the code that I have so far –
Code:
DetectHiddenWindows, On
#NoEnv
#SingleInstance Force
#WinActivateForce
SetBatchLines, -1
SetControlDelay, 0
SetTitleMatchMode, RegEx
#IfWinActive, SACTP
$F9:: ; Please see No.1 below
InputBox, XX, Enter File Number, Please enter the File number that you want to BUY, , 200, 150
If ErrorLevel
{
WinActivate, SACTP
Return
}
Else
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
If (XX>20) ; Please see No.3a below
{
MsgBox, You have entered an Incorrect File Number
Return
}
If (XX<1) ; Please see No.3b below
{
MsgBox, You have entered an Incorrect File Number
Return
}
{
SendInput, !b
WinWaitActive, Open
SendInput, C:\TradeFiles\Buy\%XX%.xls
}
Return
$F10:: ; Please see No.1 below
InputBox, XX, Enter File Number, Please enter the File number that you want to SELL, , 200, 150
If ErrorLevel
{
WinActivate, SACTP
Return
}
Else
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
If (XX>20) ; Please see No.3a below
{
MsgBox, You have entered an Incorrect File Number
Return
}
If (XX<1) ; Please see No.3b below
{
MsgBox, You have entered an Incorrect File Number
Return
}
{
SendInput, !b
WinWaitActive, Open
SendInput, C:\TradeFiles\Sell\%XX%.xls
}
Return
#IfWinActive
SetTitleMatchMode, 3
#IfWinActive, Paper Order - SACTP
NumpadEnter:: ;Please see No.1 below
WinWaitActive, Paper Order - SACTP
MsgBox, 4129,File Entry,CONFIRM?,
IfMsgBox, Cancel
Return
SendInput, !i ; Please see No.4 below
ControlGetText, YY, Edit7, Paper Order – SACTP ; Please see No.5 below
If YY contains Buy
{
SendInput, !b
WinWaitActive, Open
XX++ ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Buy\%XX%.xls
Return
}
If YY contains Sell
{
SendInput, !b
WinWaitActive, Open
XX-- ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Sell\%XX%.xls
Return
}
Return
No.1 – I have assigned the F9, F10 and NumpadEnter keys as Hotkeys to do the following –
F9 – This Hotkey would invoke the Input box to enter the value for the Buy side files to be loaded. For Example – Once I have entered the value of say 15 in the Input box it will load the file C:\TradeFiles\Buy\15.xls and in the Window with the WinTitle Paper Order – SACTP in the Edit7 control this file name and path is displayed.
Basically it will load only the Buy side files as the path in the code is C:\TradeFiles\
Buy\
F10 – This Hotkey would invoke the Input box to enter the value for the Sell side files to be loaded. For Example – Once I have entered the value of say 15 in the Input box it will load the file C:\TradeFiles\Sell\15.xls and in the Window with the WinTitle Paper Order – SACTP in the Edit7 control this file name and path is displayed.
Basically it will load only the Sell side files as the path in the code is C:\TradeFiles\
Sell\
NumpadEnter – Please notice the two lines of code preceding this Hotkey –
SetTitleMatchMode, 3
#IfWinActive, Paper Order – SACTPThis means that I need this Hotkey to function
only if this window is active and not otherwise.
This is the perhaps the main Hotkey as it first executes the file that is already loaded by selecting the option in the Input box generated by either the F9 or F10 Hotkey and then attempts to load the next file in the ascending or descending order pattern. First by sending the Alt+I key sequence to the SACTP Program (which is what the SACTP Program accepts to execute the file) it will execute the file. Thereafter, depending on the value obtained from the Edit7 control (using the ControlGetText command) it will load the next file in sequence. Currently this code is limited to only the ascending order pattern in the Buy side and the descending order pattern in the Sell side.
This code needs to be altered on two counts –
First – If the ascending order pattern is in force then it should be able to loop back to file number one once file number twenty is executed and if the descending order pattern is in force then it should loop back to file number twenty once file number one is executed. Currently what the code does is (if it is the ascending order) – for example - loads file number eighteen after file number seventeen has been executed and similarly will attempt to load file number twenty-one after file number twenty has been executed but will fail as file number twenty-one does not exist. At this point it must loop back to file number one. Likewise for the descending order pattern – from file number one back to file number twenty.
Second – I do not know how this can be done – perhaps by first constructing a GUI that would allow me to change the ascending and descending order by the F9 and F10 keys and then correspondingly it should also change it under the NumpadEnter key.
I have attempted to design a GUI to highlight what it should do. I have given the GUI code below.
Code:
DetectHiddenWindows, On
#SingleInstance Force
#WinActivateForce
#NoEnv
SetBatchLines -1
SetTitleMatchMode, RegEx
#IfWinActive, SACTP
$F9::
Gui, Destroy
Gui, Font, cBlue S8 bold, Verdana
Gui, Add, Text,, Please enter the File number that you want to BUY
Gui, Add, GroupBox, % "section x1 w75 h53 y", File Number
Gui, Font, cRed S8 bold, Verdana
Gui, Add, Edit, xs+10 ys+20 h25 w40 section UpDown vXX gloadfile Number Limit2, No.
Gui, Font, cBlue S8 bold, Verdana
Gui, Add, GroupBox, % "section x1 w270 h52 y", Order Pattern
Gui, Font, cRed S8 bold, Verdana
Gui, Add, CheckBox, xs+20 ys+20 h25 w155 vAscend_Buy, &Ascending
Gui, Add, CheckBox, xs+150 ys+20 h25 w155 vDescend_Buy, &Descending
Gui, Font, cBlack S8 bold, Verdana
Gui, Add, GroupBox, % "section x100 w200 h52 y", Tasks
Gui, Add, Button, xs+10 ys+20 h25 w80 section gDone Default, &OK
Gui, Add, Button, xs+100 ys+0 h25 w80 section gCancel, &Cancel
Gui, Show, AutoSize Center, Files
Return
Done:
Gui, Submit
Return
Loadfile:
SendInput, !b
WinWaitActive, Open
If (XX<10)
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Buy\%XX%.xls
Return
$F10::
Gui, Destroy
Gui, Font, cBlue S8 bold, Verdana
Gui, Add, Text,, Please enter the File number that you want to SELL
Gui, Add, GroupBox, % "section x1 w125 h53 y", File Number
Gui, Font, cRed S8 bold, Verdana
Gui, Add, Edit, xs+10 ys+20 h25 w40 section UpDown vYY gloadfile1 Number Limit2, No.
Gui, Font, cBlue S8 bold, Verdana
Gui, Add, GroupBox, % "section x1 w270 h52 y", Order Pattern
Gui, Font, cRed S8 bold, Verdana
Gui, Add, CheckBox, xs+20 ys+20 h25 w155 vAscend_Sell, &Ascending
Gui, Add, CheckBox, xs+150 ys+20 h25 w155 vDescend_Sell, &Descending
;Gui, Add, CheckBox, xs+150 ys+20 h25 w155 gButtonOK, &Descending
Gui, Font, cBlack S8 bold, Verdana
Gui, Add, GroupBox, % "section x100 w200 h52 y", Tasks
Gui, Add, Button, xs+10 ys+20 h25 w80 section gDone1 Default, &OK
Gui, Add, Button, xs+100 ys+0 h25 w80 section gCancel, &Cancel
Gui, Show, AutoSize Center, Files
Return
Done1:
Gui, Submit
Return
loadfile1:
SendInput, !b
WinWaitActive, Open
If (YY<10)
{
YY=0%YY%
}
SendInput, C:\TradeFiles\Sell\%YY%.xls
Return
;perhaps under the NumpadEnter Hotkey after it asks whether I would like to execute the file like it does in my code above (Alt+I key sequence) it could be like -
ControlGetText, YY, Edit7, Paper Order – SACTP ; Please see No.5 below
;Ascend_Buy
If YY contains Buy
{
SendInput, !b
WinWaitActive, Open
XX++ ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Buy\%XX%.xls
Return
}
;Descend_Buy
If YY contains Buy
{
SendInput, !b
WinWaitActive, Open
XX-- ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Buy\%XX%.xls
Return
}
;Ascend_Sell
If YY contains Buy
{
SendInput, !b
WinWaitActive, Open
XX-- ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Sell\%XX%.xls
Return
}
;Descend_Sell
If YY contains Buy
{
SendInput, !b
WinWaitActive, Open
XX++ ; Please see No.6 below
If (XX<10) ; Please see No.2 below
{
XX=0%XX%
}
SendInput, C:\TradeFiles\Sell\%XX%.xls
Return
}
Cancel:
GuiClose:
GuiEscape:
Gui, Destroy
Return
#IfWinActive
Obviously the above is a non-functional GUI code as my prowess with AHK isn’t that good. I have no clue which variable goes where and how to even load the file and what to do to execute it etc. What I am trying to highlight is that a GUI be created that can load the file just like my code with the Input box does. Here in the GUI, I should be able to select either the ascending or descending order pattern for both the Buy (F9 Hotkey) and Sell (F10 Hotkey) side files.
The GUI should be designed in such a way that if one is selected then the other automatically gets unselected as both cannot work at the same time. Also, it should also be able to retain the selection until I unselect it for another option. Once it has been selected there should be some label or something in the code that is under the NumpadEnter key GUI, which tells it what needs to be done.
Then there should be the NumpadEnter key GUI (which I do not know how to create), which will pick up the order pattern that has been selected on the Buy (F9 Hotkey) or Sell (F10 Hotkey) side and execute the file and then load the next file in sequence and in a loop.
As the NumpadEnter key is the Hotkey that executes the file to the program whatever code needs to scripted must be linked to this Hotkey only after it has been executed. F9 and F10 simply load the file and keep it ready. When the opportunity arises, the NumpadEnter key executes the file, which is when the need for loading the next file in sequence comes into effect.
The GUI should also have other error messages in check as given in
No.3a & No.3b below.
No.2 – As the files are named 01.xls, 02.xls, 03.xls etc. this section of the code tells the script that even if the user were to input 1, 2, 3 etc. instead of 01, 02, 03 etc. in the Input box as the value for the variable XX, the code would understand the same in both inputs.
No.3a & No.3b – Basically this message box would pop-up if the value entered in the Input Box were higher than 20 or lower than 1.
Unfortunately, I did not know how to merge the two together so I have given the code separately for the same.
No.4 – This is the key sequence that executes the file from the Program
No.5 – This is the Edit control where the details of which filename has been loaded is available. If it is a Buy file then it would be C:\TradeFiles\Buy\%XX%.xls or if it is a Sell file then it would be C:\TradeFiles\Sell\%XX%.xls. Depending on whether it is a Buy or a Sell value the code would then load the next file in sequence.
No.6 – As mentioned above after No.4 and No.5 have been completed above, if value in the YY variable is Buy then this part of the code will increment the variable value by one and load the next file. Similarly, it would decrement the value by one if it is a Sell value.
I hope I have been able to aptly describe what I need to get done. I have tried my level best and I truly hope that someone can help me with this. Fingers crossed…
