Auto Trade (ALGO) in TOS

strategy, backtested in TOS , looks great but rarely does it implement well or consistently over time.

So true! When backtested with a decade or more of data, it's VERY difficult to find a strategy that is robust.

OT ... and can I just say, if MerryDay is lurking on the thread, that I just love the seasonal flamingos 😊
 
Last edited:
Oh ok I use auto hot key. how you send an order, Is it auto or your using it like a hot key ?
This is a very broad explanation, but in short, you have AHK watch your TOS strategy chart for a specific label. For example, I have a white label show up in my top left corner when I want to go long and a blue label when I want to exit. From there when AHK see's each respective label, it will push the buy or flatten button on TOS, or anyplace else on your screen you designate. If this is something you're interested in, I highly recommend you read the threads on here, there are some caveats and AHK can be tricky on that label recognition unless you have it setup correctly.
 
This is a very broad explanation, but in short, you have AHK watch your TOS strategy chart for a specific label. For example, I have a white label show up in my top left corner when I want to go long and a blue label when I want to exit. From there when AHK see's each respective label, it will push the buy or flatten button on TOS, or anyplace else on your screen you designate. If this is something you're interested in, I highly recommend you read the threads on here, there are some caveats and AHK can be tricky on that label recognition unless you have it setup correctly.
ahk triggerd by a color on a chart? if so this is genius
 
Good day I used the script in this thread by @Svanoy as a test to get me going. I did image search and the mouse coordinates using AHK windows spy mine is -something because I’m using a second screen but Running into a issue not doing anything the ^0 (pause) works but will not trade.

I run the script as an administrator

1 Dose it work on simulator trades?
2 Dose it work with a second screen?
3 after running do I click on TOS window for AHK script to recognize that program?
 
@Goingdark365

There are issues with 2nd screens.
ImageSearch does not like negative numbers, you should be able to change your primary display to the screen displaying TOS and use those coordinates.

You should have a key combination to 'start' the script after running the AHK script as administrator.
 
Ive got it working with @aCuddlyTurtle / @Svanoy RSI strategy. Amazing workaround. Thank you very much Svanoy for you willingness to help. There is a lot to get it working. @Goingdark365 Ive found the following two scripts to be most helpful when setting up your AHK system to "see" the right things. LabelTester.ahk will look in the coordinates you are testing and give you a "0" if label is found.
TestAreasbyMovingMouse.ahk will move the mouse on the screen to test the areas you want to watch. The indicator is a great tool to test the colors in combination with LabelTester you should be able to setup.

LabelTester.ahk
CSS:
#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, Pixel, Screen

Delay = 1000000

^0:: Pause
^9::
Loop{


ImageSearch, FoundX, FoundY, 610, 200, 910, 250, *w20 *h20 *2 C:\Users\WIN10USER\Desktop\AutoHotKey Images\SellLabel.tiff

MsgBox, 0, , %ErrorLevel%, 5
}Return

and

TestAreasbyMovingMouse.ahk
Code:
#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, Pixel, Screen

Delay = 10000000

^0:: Pause
^9::
Loop{
; Resting Mouse placement
    MouseMove,  1150, 60, 100
    Delay = 10000000
; Outline the Label box
    MouseMove,  710, 200, 100
    Delay = 10000000
    MouseMove,  810, 250, 100
    Delay = 10000000
}Return

Label Color Indicator
Code:
input showLong = yes;
input showShort = no;
input showFlatten = no;



#### Labels ####
AddLabel(yes, "       ", if  showLong then Color.GREEN else Color.MAGENTA);
AddLabel(yes, "       ", if showShort then Color.RED else Color.YELLOW);
AddLabel(yes, "       ", if  showFlatten then Color.BLUE else Color.Cyan);
 
While Im at it.. Here is an indicator to move the trigger labels to the right as alerts and messages can cover them if theyre all the way to the left.

CSS:
input blink = no;


def NewBar = if close[1]!=close[2] or high[1]!=high[2] or low[1]!=low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1]==1 then 0 else if !IsNaN(NewBar) and Clock[1]==0 then 1 else Clock[1];

AddLabel(blink, "CTRL9 to Start Script                                    ALGO LABELS                                      ---   ---   ---   ---   ---   ---   ---   ---   --->                      ", if Clock==0 then Color.Light_GRAY else Color.Light_ORANGE);

AddLabel(!blink, "CTRL9 to Start Script                                    ALGO LABELS                                      ---   ---   ---   ---   ---   ---   ---   ---   --->                      ",  Color.Light_GRAY);
 
I'm going to save you guys hours of work. Here's the AHK script I have developed to work on any strategy. It's very simple and uses 3 color labels to do the following. White = Buy, Blue = Flatten, Orange = Short. You'll have to come up with your own strategy on what you feel works for you. As for this AHK script, I have commented on much of it to give some clear directions on how to use it. It has some error clearing features built in so if TOS throws up an error in the middle of the night it will clear it out.

Here's a link to the images I use. Wherever you put the script below, create a subfolder called images, then place the images from the url in that subfolder. In case they don't download as the default name, I put the proper name in the description.

Let's see if this works for the images...
Have fun....

Code:
; Version Beta .07 - Last Modified: 11-14-22 09:18 - 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     = 130     ; Horizontal Coordinates, this will be the same for buy, flat, sell.
buyCoord              = 75     ; Vertical Coordinates, middle of button
flatCoord             = 150     ; Vertical Coordinates, middle of button
sellCoord             = 240     ; Vertical Coordinates, middle of button
statusHorizontal     = 125     ; The horizontal setting for the status bar
statusCoord          = 320     ; The vertical setting for the status bar
labelSearchX         = 275     ; How far to the right to search for the buy/sell label
labelSearchY         = 275     ; How far down to search for the buy/sell label

tradeLimit             = 5     ; 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             = 0     ; 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.
                            ;  - this is used to make sure we're flat before hitting the buy or sell entries buttons.
useTradovate        = 0     ; default is 1 (on), off is 0 (zero)
tradoBuyCoord          = 900    ; Vertical Coordinates, middle of button
tradoFlatCoord         = 1010     ; Vertical Coordinates, middle of button
tradoSellCoord         = 1135     ; Vertical Coordinates, middle of button
tradoAlignH            = 233    ; Alignment tool, this will be your horizontal setting for buy/Exit/Sell buttons.
tradoAlginV            = 1140    ; Alignment tool, this should align to the right of all your buttons.
tradoAlignT            = 0        ; 1 - yes, 0 - no, Use/display Align Tool

; End user defineable
status                 = 0
trades                 = 0
stopTimer             = 0
tosErrors            = 0

^B:: MouseClick, Left, %buyCoord%,  %buttonsHorizontal%, 1, 0
^F:: MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0
^S:: MouseClick, Left, %sellCoord%, %buttonsHorizontal%, 1, 0
^P:: Pause
^E:: checkForErrors()
^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 ; use this and the next two below to adjust trade status, 0 = Flat
^1:: status = 1    ; 1 = We are long
^2:: status = 2 ; 2 = We are short;

esc::
Main:
sleep 1000
a := entryDelay
;checkForErrors()

loop {
    sleep 1000
    a++
    b++      
    if (status != 1 and a >= entryDelay or status == 2 and a >= entryDelay) {
        ImageSearch, FoundX, FoundY, 0, 0, %labelSearchX%, %labelSearchY%, *w50 *h15 *10 images\white.png
        if (ErrorLevel = 0) {
            if (entryPause > 0) {
                k := entryPause
                while (k > 0) {
                    tooltip Status: Delaying Entry: %k% Seconds, %statusCoord%, %buttonsHorizontal%
                    sleep 1000
                    k--
                }
            }
           
            ;Click Flatten Button
            if (flatBeforeEntry > 0) {
                MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0
           
                ;TradoVate ------------
                if (useTradovate == 1) {
                    MouseClick, Left, %tradoFlatCoord%, %tradoAlignH%, 1, 0
                }      
                delayFunction(flatBeforeEntry, "Flat Before Next Entry")
            }
           
            ;Click Buy Button if green status and we're not already in a trade
            MouseClick, Left, %buyCoord%, %buttonsHorizontal%, 1, 0
           
            ;TradoVate ------------
            if (useTradovate == 1) {
                MouseClick, Left, %tradoBuyCoord%, %tradoAlignH%, 1, 0
            }
           
            status = 1
            a = 0
            trades++
            tooltip Status: Buy Image Cords: %FoundX% x %FoundY%, %statusCoord%, %buttonsHorizontal%
            sleep delayTime * 1000
            ;checkForErrors()
        }
    }  
    if (status != 0) {
        ImageSearch, FoundX, FoundY, 0, 0, %labelSearchX%, %labelSearchY%, *w50 *h15 *10 images\blue.png
        if (ErrorLevel = 0) {
            ;Click Flatten Button
            MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0
           
            ;TradoVate ------------
            if (useTradovate == 1) {
                MouseClick, Left, %tradoFlatCoord%, %tradoAlignH%, 1, 0
            }
           
            delayFunction(15, "Waiting to hit the flat button again")
           
            ; Lets hit the flat button again, to be sure after delay above
            if (useTradovate == 1) {
                MouseClick, Left, %tradoFlatCoord%, %tradoAlignH%, 1, 0
            }
            ;Click Flatten Button
            ; Let's hit TOS flat once more
            MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0          
       
           
            status = 0
            tooltip Status: Flat Image Cords: %FoundX% x %FoundY%, %statusCoord%, %statusHorizontal%
            sleep delayTime * 1000
            ;checkForErrors()
            delayFunction(exitDelay, "Next Entry is Delayed")
           
        }
    }
    if (status != 2 and a >= entryDelay or status == 1 and a >= entryDelay) {
        ImageSearch, FoundX, FoundY, 0, 0, %labelSearchX%, %labelSearchY%, *w50 *h15 *10 images\dark_orange.png
        if (ErrorLevel = 0) {          
            if (entryPause > 0) {
                k := entryPause
                while (k > 0) {
                    tooltip Status: Delaying Entry: %k% Seconds, %statusCoord%, %buttonsHorizontal%
                    sleep 1000
                    k--
                }
            }
            ;Click Flatten Button
            if (flatBeforeEntry > 0) {
                MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0
           
                ;TradoVate ------------
                if (useTradovate == 1) {
                    MouseClick, Left, %tradoFlatCoord%, %tradoAlignH%, 1, 0
                }      
                delayFunction(flatBeforeEntry, "Flat Before Next Entry")
            }
           
            ;Click Sell (short)
            MouseClick, Left, %sellCoord%, %buttonsHorizontal%, 1, 0
           
            ;TradoVate -------------
            if (useTradovate == 1) {
                MouseClick, Left, %tradoSellCoord%, %tradoAlignH%, 1, 0
            }
           
            status = 2
            a = 0
            trades++
            tooltip Status: Short Image Cords: %FoundX% x %FoundY%, %statusCoord%, %statusHorizontal%
            sleep delayTime * 1000
            ;checkForErrors()
        }
    }
   
    timeLeft := clearCounterPeriod - b
    entryTime := FormatSeconds(a)
    countdownTime := FormatSeconds(timeLeft)
    FormatTime, TimeString,, Time
   
    ;tooltip Status: %status%  Trades: %trades%  Count: %TimeLeft%  Entry: %a%  Time: %TimeString%, %statusCoord%, %statusHorizontal%
    tooltip Status: %status% | Trades: %trades% of %tradeLimit% | Count: %countDownTime% | Entry: %entryTime% | Time: %TimeString% | Errors: %tosErrors%, %statusCoord%, %statusHorizontal%
   
    ; Tradovate
    if (tradoAlignT == 1) {
        tooltip "<----", %tradoAlignV%, %tradoAlignH%, 2
    }
   
    if (trades >= tradeLimit) {
        trades = 0
        ;Click Flatten Button
        MouseClick, Left, %flatCoord%, %buttonsHorizontal%, 1, 0
        sleep resetPeriod * 1000
        j++
       
        if (j == 2 ) {
            tooltip Reset Count: %j% Time: %TimeString%, %statusCoord%, %statusHorizontal%
            exit
        }
    }
    if (b >= clearCounterPeriod) {
        b = 0
        trades = 0
        checkForErrors()
    }
}

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
}

checkForErrors()
{  
    global statusCoord
    global statusHorizontal
    global tosErrors
   
    tosHorizontal := statusHorizontal - 30
    tooltip Errors:  %tosErrors% | %FoundX% x %FoundY%, %statusCoord%, %tosHorizontal%
   
    ImageSearch, FoundX, FoundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *50 images\tosWarningOK.bmp
    if (ErrorLevel = 0) {
            ;Clear the TOS Performance Issue Message
            FoundX:=FoundX+5, FoundY:=FoundY+5
            MouseClick, Left, %FoundX%, %FoundY%, 1, 0
            tosErrors++
            tooltip OK Warnings Cleared: %tosErrors% | %FoundX% x %FoundY%, %statusCoord%, %tosHorizontal%

    }
    ImageSearch, FoundX, FoundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *140 images\tosWarningContinue.png
    if (ErrorLevel = 0) {
            ;Clear the TOS Order Submit Error Message
            FoundX:=FoundX+10, FoundY:=FoundY+10
            MouseClick, Left, %FoundX%, %FoundY%, 1, 0
            tosErrors++
            tooltip CONTINUE Warnings Cleared: %tosErrors% | %FoundX% x %FoundY%, %statusCoord%, %tosHorizontal%
    }
   
    ; Lets mouse click top left to clear this weird drop down menu error I've received many times
    MouseClick, Left, 5, 5, 1, 0
   
    return  
}

I forgot to mention above, run the script as administrator, that's the only way it will click the buttons for you. Also, to start the script hit the esc on your keyboard, then there are several keys I did not comment what they are, but should be kinda obvious, it's all the ones that have the ^ (Ctrl) in front of them, so Ctrl + F1 will pause the script for one hour, etc...
 
so seems like I have a problem with clicks
I used the labeltester.ahk and worked at first gave me a “0” and now it’s giving me a “2”
testareasbymovingmouse.ahk didn’t seem to work even with the 10000000 seems to jump way to fast
 
@Goingdark365
Doublecheck your coordinates.

ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ImageFile


X1,Y1 The X and Y coordinates of the upper left corner of the rectangle to search, which can be expressions. Coordinates are relative to the active window unless CoordMode was used to change that.
X2,Y2 The X and Y coordinates of the lower right corner of the rectangle to search, which can be expressions. Coordinates are relative to the active window unless CoordMode was used to change that.
 
@Kevin N thank you for sharing the script I’ll give it a try

@Svanoy the outputvarx and outputvary was the fix it, I’m trying it out on paper trades over night

Once the script is running can I still use the mouse?
Let me give a couple more tips...

1. Virtual Machines and VPS - I run my setups on virtual machines, right now I'm using the one by Oracle, it's free. You can download evaluation copies of windows server that are good for 6 months. In addition to that, I run two on a VPS from CheapWindowsVPS, search for coupon codes for them, I'm running a windows 10 pro VPS for $8.50 per month, also a windows 2016 server. From there, I use Chrome Remote Desktop to remote in, and it's like I have another computer running with a reliable internet connection. With the virtual machine from Oracle that you run on your local PC, you can minimize it once it's up and running, then no issues with other work you want to do on your computer.

2. Less is more - Try to find a good strategy that does not trade a gazillion times a day. With this setup in a fast moving market, slippage is a real thing. When I'm trading /MNQ I can easily loose 5 - 10 pts to slippage. So remember, less is more when using AHK with TOS. The less your system trades the more realistic your backtests will be. I'm currently running systems based on the Opening Range Break, this trades 1 - 3x a day on avg. Also, keep up with market news, there is no way your algo will keep up when things like Fed Rate Announcements come out.
 
Let me give a couple more tips...

1. Virtual Machines and VPS - I run my setups on virtual machines, right now I'm using the one by Oracle, it's free. You can download evaluation copies of windows server that are good for 6 months. In addition to that, I run two on a VPS from CheapWindowsVPS, search for coupon codes for them, I'm running a windows 10 pro VPS for $8.50 per month, also a windows 2016 server. From there, I use Chrome Remote Desktop to remote in, and it's like I have another computer running with a reliable internet connection. With the virtual machine from Oracle that you run on your local PC, you can minimize it once it's up and running, then no issues with other work you want to do on your computer.

2. Less is more - Try to find a good strategy that does not trade a gazillion times a day. With this setup in a fast moving market, slippage is a real thing. When I'm trading /MNQ I can easily loose 5 - 10 pts to slippage. So remember, less is more when using AHK with TOS. The less your system trades the more realistic your backtests will be. I'm currently running systems based on the Opening Range Break, this trades 1 - 3x a day on avg. Also, keep up with market news, there is no way your algo will keep up when things like Fed Rate Announcements come out.
Such good info! Thank you.
 
Hey @Kevin N how long did you test your system before going live if you dont mind me asking? Im seeing good results with my ParabolicSAR strategy. Got that FOMO whispering.... lol Changing stops and tick chart settings dynamically is my next goal. Im hoping to signal market info to the AHK script and change the chart timeframe and stops since im using the oco orders within TOS.
 
Hey @Kevin N how long did you test your system before going live if you dont mind me asking? Im seeing good results with my ParabolicSAR strategy. Got that FOMO whispering.... lol Changing stops and tick chart settings dynamically is my next goal. Im hoping to signal market info to the AHK script and change the chart timeframe and stops since im using the oco orders within TOS.
I would papertest a MINIMUM of a week to make sure the algo is doing what you want it to do. As for the strategy, I would test a good month. Verify the algo is taking trades when expected, its executing at the expected price, and verify your strategy is not repainting and signaling a buy or sell before the current bar closes.

Here's another tip, if you want to test your system, but you have the itch for real money and real risk, you can sign up to one of these prop accounts like Elite Trader Funding, and they are always running sales where you can get a 10k account for like $28. In my AHK script above, I have it setup to execute buy/sell/flatten signals on my Tradovate platform, in addition to my TOS (real and paper), and are running side by side. This will give you an opportunity to make real money but all you have at risk is your $28. I find it's fun and a great way to test out new strategies without risking hundreds or thousands.

Here's a visual of what I'm doing. I'm running ThinkOrSwim on a CheapWindowsVPS server, connected via Chrome Remote Desktop, using TOS on paper to trigger trades on my two Tradovate prop accounts. The script above is setup for ONE tradovate, but I've just started running two as seen in the image.


 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
228 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top