; Created/Shared by Kevin N altered by SilverWolf
; https://usethinkscript.com/threads/auto-trade-algo-in-tos.7546/post-114283
; Version Beta .11 - Last Modified: 12-27-22 - Added GUI and introduced Lower indicator calculations
;Added option to disable pre-buy/sell flatten for quicker entries.
; double tap the flatten button.
; last mod creates a delay after trade closes and new trade allowed
; .07 Added entryPause Added error clearing feature.
; ver .062 Added pause for x number of minutes features.
; Made some adjustments to the screen and added auto status. This version to be used with the algo version 5.5 or later.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance [Forced]
CoordMode, ToolTip, Screen
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
; CheapWindowsVPS_10
; User Defineable variables
; Use the following to set the vertical and horizontal coordinates for each.
buttonsHorizontal = 1630 ; Horizontal Coordinates, this will be the same for buy, flat, sell.
buyCoord = 155 ; Vertical Coordinates, middle of button
sellCoord = 193 ; Vertical Coordinates, middle of button
flatCoord = 225 ; Vertical Coordinates, middle of button
statusHorizontal = 75 ; The horizontal setting for the status bar
statusCoord = 760 ; The vertical setting for the status bar
labelSearchX = 1160 ; How far to the right to search for the buy/sell label
labelSearchY = 260 ; How far down to search for the buy/sell label
SingleShare = 1
tradeLimit = 100 ; How many trades are allowed in clearCounterPeriod before sitting out resetPeriod
resetPeriod = 900 ; In Seconds, if tradeLimit is reached in clearCounterPeriod, how long to wait before another trade
clearCounterPeriod = 1800 ; In Seconds, if tradeLimit is reached in this amount of time, sit out resetPeriod
entryDelay = 5 ; In Seconds, the amount of time that must pass before another entry is allowed
exitDelay = 0 ; In Seconds, Time that must pass after a trade exits before its allowed to buy again.
delayTime = 0 ; In Seconds, the amount of time to wait sleep after buy, short, or flat.
entryPause = 0 ; In Seconds, the amount of seconds to pause before pressing the buy/sell button.
flatBeforeEntry = 0 ; In Seconds, amount of time to wait after pre-buy/sell flatten, if 0 then no pre-buy/sell flatten.
; End user defineable
status = 0
trades = 0
stopTimer = 0
tosErrors = 0
Gui, Destroy
;Gui,+AlwaysOnTop
Gui -Caption +ToolWindow +Border +LastFound +AlwaysOnTop -Border +hWndhGUI
Gui, Color, 313131
Gui, Font, S7 CDefault Bold, Verdana
Gui, Add, Button, x2 y-4 w50 h30 , Start
Gui, Add, Button, x52 y-4 w50 h30 , Pause
Gui, Add, Button, x362 y-4 w50 h30 , Exit
Gui, Add, Button, x102 y-4 w50 h30 , Reset
Gui, Add, Button, x202 y-4 w50 h30 , Garbage
Gui, Add, Button, x152 y-4 w50 h30 , Refresh
Gui, Add, Button, x252 y-4 w50 h30 , Focus
Gui, Add, Button, x302 y-4 w30 h30 , 1
Gui, Add, Button, x332 y-4 w30 h30 , 2
; Generated using SmartGUI Creator 4.0
Gui, Show, x760 y51 h21 w418, TOS Algo Trading System
Return
ButtonExit:
GuiClose:
ExitApp 2
ButtonPause:
Send Esc
Pause,, 1
tooltip Algo is PAUSED, %statusCoord%, %statusHorizontal%
Return
ButtonReset:
status = 0
UpBuySignal = 0
Lw1Bsignal = 0
Lw2Bsignal = 0
BUYvalue = 0
UpSellSignal = 0
Lw1Ssignal = 0
Lw2Ssignal = 0
SELLvalue = 0
statusWord = Flat
trades = 0
stopTimer = 0
Return
ButtonRefresh:
Run, lib/TOS refresh chart
Return
ButtonGarbage:
Run, lib/TOS collect garbage
Return
ButtonFocus:
Run, lib/TOS focus
Return
Button1:
Run, lib/TOS ActiveTrader
Return
Button2:
Run, lib/TOS Chart
Return
^B:: MouseClick, Left, %buttonsHorizontal%, %buyCoord%, 1, 0
^F:: MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
^S:: MouseClick, Left, %buttonsHorizontal%, %sellCoord%, 1, 0
^ESC:: ExitApp
ESC:: Pause
^R:: trades = 0 ;Reset the trades
^T:: stopTimer = 0
^D:: delayFunction(86400,"Waiting") ; 86400 = 1 Day
^F1:: delayFunction(3600,"Pausing for 1 hour")
^F2:: delayFunction(7200,"Pausing for 2 hours")
^F4:: delayFunction(14400,"Pausing for 4 hours")
^0:: status = 0
^1:: status = 1
^2:: status = 2
ButtonStart:
;Main:
a := entryDelay
Toggle := !Toggle
While Toggle {
sleep 1000
a++
b++
status = 0
UpBuySignal = 0
Lw1Bsignal = 0
Lw2Bsignal = 0
BUYvalue = 0
UpSellSignal = 0
Lw1Ssignal = 0
Lw2Ssignal = 0
SELLvalue = 0
;Maintain single share
ImageSearch, FoundX, FoundY, 0, 0, 1840, 440, *w70 *h26 Images\TwoBuy.tiff
if (ErrorLevel = 0 and SingleShare = 1){
status = 1
;Correct and sell one back
MouseClick, Left, %buttonsHorizontal%, %sellCoord%, 1, 0
sleep 3000
}
ImageSearch, FoundX, FoundY, 0, 0, 1840, 440, *w71 *h26 Images\TwoSell.tiff
if (ErrorLevel = 0 and SingleShare = 1){
status = 2
;Correct and buy one back
MouseClick, Left, %buttonsHorizontal%, %buyCoord%, 1, 0
sleep 3000
}
;Get status from TOS
ImageSearch, FoundX, FoundY, 0, 0, 1840, 440, *w71 *h26 Images\FlatPic.tiff
if (ErrorLevel = 0){
status = 0
statusWord = Flat
sleep 100
}
ImageSearch, FoundX, FoundY, 0, 0, 1840, 440, *w59 *h26 Images\Positive.tiff
if (ErrorLevel = 0){
status = 1
statusWord = Long
sleep 100
}
ImageSearch, FoundX, FoundY, 0, 0, 1840, 440, *w58 *h26 Images\Negative.tiff
if (ErrorLevel = 0){
status = 2
statusWord = Short
sleep 100
}
;Three indicator buy signal
ImageSearch, FoundX, FoundY, 150, 205, 1200, 235, *w31 *h21 Images\ABuyLabel.tiff
if (ErrorLevel = 0){
UpBuySignal = 1
;FindText().MouseTip(v.x, v.y)
}
ImageSearch, FoundX, FoundY, 0, 700, 400, 1000, *w40 *h21 Images\ALwr1Buy.tiff
if (ErrorLevel = 0){
Lw1Bsignal = 1
;FindText().MouseTip(v.x, v.y)
}
ImageSearch, FoundX, FoundY, 0, 700, 400, 1000, *w40 *h20 Images\ALwr2Buy.tiff
if (ErrorLevel = 0){
Lw2Bsignal = 1
;FindText().MouseTip(v.x, v.y)
}
BUYvalue := UpBuySignal+Lw1Bsignal+Lw2Bsignal
;MsgBox, % "Buy Total: " BUYvalue
;Three indicator sell signal
ImageSearch, FoundX, FoundY, 150, 205, 1200, 235, *w28 *h20 Images\ASellLabel.tiff
if (ErrorLevel = 0){
UpSellSignal = 1
;FindText().MouseTip(v.x, v.y)
}
ImageSearch, FoundX, FoundY, 0, 700, 400, 1000, *w38 *h20 Images\ALwr1Sell.tiff
if (ErrorLevel = 0){
Lw1Ssignal = 1
;FindText().MouseTip(v.x, v.y)
}
ImageSearch, FoundX, FoundY, 0, 700, 400, 1000, *w38 *h20 Images\ALwr2Sell.tiff
if (ErrorLevel = 0){
Lw2Ssignal = 1
;FindText().MouseTip(v.x, v.y)
}
SELLvalue := UpSellSignal+Lw1Ssignal+Lw2Ssignal
;MsgBox, % "Sell Total: " SELLvalue
;Evaluate and Go Long
if (BUYvalue >= 3 and status = 0 and a >= entryDelay) { ;goLong
if (entryPause > 0) {
k := entryPause
while (k > 0) {
;tooltip Status: Delaying Entry: %k% Seconds, %statusCoord%, %buttonsHorizontal%
sleep 100
k--
}
}
;Click Flatten Button
if (flatBeforeEntry > 0) {
MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
delayFunction(flatBeforeEntry, "Flat Before Next Entry")
}
;Click Buy (Long)
MouseClick, Left, %buttonsHorizontal%, %buyCoord%, 1, 0
MouseMove, 1800, 175, 100
status = 1
statusWord = Long
a = 0
trades++
sleep delayTime * 100
}
;Evaluate and Go Short
if (SELLvalue >= 3 and status = 0 and a >= entryDelay) { ;goShort
if (entryPause > 0) {
k := entryPause
while (k > 0) {
;tooltip Status: Delaying Entry: %k% Seconds, %statusCoord%, %buttonsHorizontal%
sleep 100
k--
}
}
;Click Flatten Button
if (flatBeforeEntry > 0) {
MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
delayFunction(flatBeforeEntry, "Flat Before Next Entry")
}
;Click Sell (short)
MouseClick, Left, %buttonsHorizontal%, %sellCoord%, 1, 0
MouseMove, 1800, 175, 100
status = 2
statusWord = Short
a = 0
trades++
sleep delayTime * 100
}
ImageSearch, FoundX, FoundY, 150, 205, 1200, 235, *w30 *h20 Images\ABuyFlat.tiff
if (ErrorLevel = 0 and status = 1) { ;Long Close
;Click Flatten Button
MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
MouseMove, 1800, 175, 100
status = 0
statusWord = Flat
sleep delayTime * 100
delayFunction(exitDelay, "Next Entry is Delayed")
}
ImageSearch, FoundX, FoundY, 150, 205, 1200, 235, *w30 *h20 Images\ASellFlat.tiff
if (ErrorLevel = 0 and status = 2) { ;Short Close
;Click Flatten Button
MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
MouseMove, 1800, 175, 100
status = 0
statusWord = Flat
sleep delayTime * 1000
delayFunction(exitDelay, "Next Entry is Delayed")
}
timeLeft := clearCounterPeriod - b
entryTime := FormatSeconds(a)
countdownTime := FormatSeconds(timeLeft)
FormatTime, TimeString,, Time
tooltip Algo is LIVE | Status: %statusWord% | Entry: %entryTime% | Trades: %trades% of %tradeLimit% | Count: %countDownTime% | Time: %TimeString%, %statusCoord%, %statusHorizontal%
if (trades >= tradeLimit) {
trades = 0
;Click Flatten Button
MouseClick, Left, %buttonsHorizontal%, %flatCoord%, 1, 0
sleep resetPeriod * 100
j++
if (j == 2 ) {
tooltip Reset Count: %j% Time: %TimeString%, %statusCoord%, %statusHorizontal%
exit
}
}
if (b >= clearCounterPeriod) {
b = 0
trades = 0
}
}return
FormatSeconds(NumberOfSeconds) ; Convert the specified number of seconds to hh:mm:ss format.
{
time := 19990101 ; *Midnight* of an arbitrary date.
time += NumberOfSeconds, seconds
FormatTime, mmss, %time%, mm:ss
return NumberOfSeconds//3600 ":" mmss
/*
; Unlike the method used above, this would not support more than 24 hours worth of seconds:
FormatTime, hmmss, %time%, h:mm:ss
return hmmss
*/
}
delayFunction(SecondsToWait, Message )
{
k := SecondsToWait
global stopTimer = 1
global statusCoord
global statusHorizontal
while (k > 0)
{
if (stopTimer != 1) {
break
}
timeLeft:= FormatSeconds(k)
tooltip %Message%: %timeLeft%, %statusCoord%, %statusHorizontal%
sleep 1000
k--
}
stopTimer = 0
}