Need help reformatting/rearranging copy/pasted text string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
phensonbtp
Posts: 2
Joined: 28 Jul 2020, 18:01

Need help reformatting/rearranging copy/pasted text string

28 Jul 2020, 18:55

I'm not sure where to start with this project, I'm hoping someone can at least look over what I'm trying to do and say if its something AHK would work for.
Basically I'm trying to take market trade alerts, and transform the alert data into the string TD Ameritrade's ThinkorSwim recognizes as an order you can paste into the app and submit.
In Windows, if I copy a trade alert string which looks like this:

LUV 2020-08-21 34.0 Calls

I want it to be arranged in the following format, replacing the contents of the clipboard with this, a format ThinkorSwim recognizes as an order ready to be placed:

BUY +100 LUV 100 21 AUG 20 34.0 CALL @ LMT

Thats the basic idea.
I dont know what tools I need to make it happen.

Here's the actual nitty gritty. Maybe color coding will help simplify my explanation.
The input text will change but always be in this structure, with a single space between data:

Input:
"LUV 2020-08-21 34.0 Calls"
is arranged as:

Ticker Date Strike Side
or
LUV 2020-08-21 34.0 Calls

The output I want will always be the following static text (the black bolded parts), with the data from Input replacing their respective colored parts

Output:
BUY +100 LUV 100 21 AUG 20 34.0 CALL @ LMT

Data structure for reference:
BUY +100 Ticker 100 Date Strike Side @ LMT


Again, the "BUY +100, 100, @ LMT" will always be there in the base output string template.

Lastly here are the rough parameters for each input data, uh, section.

Ticker can be used as is, whatever it is, move it to the new output string.

Date needs to be reformatted from "2020-08-21" (year-month-day) to "21 AUG 20", thats day, 3 letter month, 2 digit year, space instead of dash.

Strike can be used as is, moved to the new output string.

Side input will either be "Calls" or "Puts", replace with "CALL" or "PUT" when moving to the output string.

In my head I can sort of plan out how I'd have to script the rules, I started putting together an excel document that would parse a text file and kind of apply some rule based formatting but that was a whole other rabit hole.
In reality I need a solution thats streamlined. I copy the text, hit a hotkey, the copied text gets processed and placed back in the clipboard ready for me to directly paste these market trade alerts as new orders in TD Ameritrade's thinkorswim.

I feel like AHK is where I should be looking, I'm ok with learning and researching, hopefully y'all can say I'm looking at the right platform
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Need help reformatting/rearranging copy/pasted text string  Topic is solved

28 Jul 2020, 20:08

I believe this does what you requested:

Code: Select all

#Persistent

OnClipboardChange("FormatTD")
return

FormatTD() {
	if !RegExMatch(Clipboard, "(\w+) (\d{4}-\d{2}-\d{2}) ([\d\.]+) (Calls|Puts)", M)
		return
	Date := SubStr(M2, 9, 2) . A_Space
		. ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"][SubStr(M2, 6, 2)]
		. A_Space . SubStr(M2, 3, 2)
	Side := RTrim(M4, "s")
	StringUpper, Side, Side
	Clipboard := "BUY +100 " M1 " 100 " Date . A_Space . M3 . A_Space . Side " @ LMT"
}
Edit: Minor tweak. Output is the same.
Last edited by boiler on 28 Jul 2020, 20:25, edited 1 time in total.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Need help reformatting/rearranging copy/pasted text string

28 Jul 2020, 20:16

By the way, no hotkey needed. Whenever the clipboard changes, it will see if the copied text meets the format, and if so it automatically replaces it. If not, it leaves it alone. This version might be better in that it will show a ToolTip for a few seconds showing the modified clipboard contents as a confirmation that it has accepted and replaced it:

Code: Select all

#Persistent

OnClipboardChange("FormatTD")
return

FormatTD() {
	if !RegExMatch(Clipboard, "(\w+) (\d{4}-\d{2}-\d{2}) ([\d\.]+) (Calls|Puts)", M)
		return
	Date := SubStr(M2, 9, 2) . A_Space
		. ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"][SubStr(M2, 6, 2)]
		. A_Space . SubStr(M2, 3, 2)
	Side := RTrim(M4, "s")
	StringUpper, Side, Side
	Clipboard := "BUY +100 " M1 " 100 " Date . A_Space . M3 . A_Space . Side " @ LMT"
	ToolTip, % Clipboard
	SetTimer, TipClose, -3000
}

TipClose:
	ToolTip
return
If you prefer a hotkey so you have the option of copying text with that exact format without transforming it, that can be done. In that case, you wouldn't need to copy it first. We can just make it so you highlight the text then hit the hotkey and it will copy it and transform it if it meets the format requirement.
phensonbtp
Posts: 2
Joined: 28 Jul 2020, 18:01

Re: Need help reformatting/rearranging copy/pasted text string

28 Jul 2020, 20:31

Wow thank you so much! These work perfectly, what do I owe you? :bravo:
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Need help reformatting/rearranging copy/pasted text string

28 Jul 2020, 20:37

Glad to help! Nothing owed. It's my pleasure.
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Need help reformatting/rearranging copy/pasted text string

28 Jul 2020, 21:50

Another take. Call with OnClipboardChange or from a hotkey (i would prefer the latter as I would not like to lose my regular clipboard function)

Code: Select all

trnsfrm(){
tmp := strsplit(clipboard," ")
FormatTime, ndt, % strreplace(tmp.2,"-"), d MMM yy
msgbox,,, % clipboard := "BUY +100 " tmp.1 " 100 " ndt " " tmp.3 " " Format("{:U}", trim(tmp.4,"s")) " @ LMT",2
}
14.3 & 1.3.7
rc76
Posts: 144
Joined: 07 Nov 2020, 01:45

Re: Need help reformatting/rearranging copy/pasted text string

27 Sep 2021, 23:21

@phensonbtp , can you paste text into Thinkorswim to submit options order?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Exies and 123 guests