Jump to content

I need to copy then divide and then paste a number - (Moved)


Recommended Posts

I specialize in day trading and am trying to learn code for the purpose of programming hotkeys to assist me in my trading. There is a quantity box on the trading platform where I type in my share size and I have so far programmed 10 hotkeys to automatically enter 1000 to 10,000 shares for quicker entry and this has worked beautifully. However I want to add a hotkey that can cut any of those values in half at the push of a button to be able to sell half of my position. I know how to copy the number from the platform and paste it back to the platform but how do I get it to divide that number by 2 before pasting it. I've read through the help file and scoured the web but am not finding an answer to this seemingly easy question. Any help would be appreciated. Thank you.

Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Assuming you have a variable $iShares containing the numeric value of shares, you can divide it in half with this command --

$iShares /= 2

which is that same as writing

$iShares = $iShares / 2

You didn't share any code, so we don't know the resulting format of the data after you've copied it. You may need to convert it using Number() before you can perform calculations on it.

Link to comment
Share on other sites

My apologies for not sharing the code. I don't have a variable.  Here's what I have so far.

 

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=C:\Users\dadda\OneDrive\Documents\6 Win Ord Bnk.exe
#AutoIt3Wrapper_UseUpx=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


--------------------------------------

HotKeySet('^{NUMPADDIV}', 'HotKey8')                 ;Half


;-------------------------------------
While 11
    sleep(50)
WEnd
;--------------------------------------
 

Func HotKey8()  ;
            MouseClick('primary', 1439, -999, 1, 1)     ;Click the quantity box
            Send('^a')                                       ;Select All
            Send('^c')                                       ;Copy The Number

    EndFunc

Edited by Keybanger
Link to comment
Share on other sites

You could create another HotKey that divides any value by two (let's call it "Division-Hotkey") .

The process would be as follows : Press the HotKey for e.g. 6000 but do not send anything yet. Set up a waiting loop (duration e.g. 2 seconds). During this time you can press the "Division-Hotkey", which divides the value by 2. If you do nothing for 2 seconds, the original value is sent.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Thanks Marc I tried the clipget and Clipput and they work for any number below 1000. But if I type in 1000 then hit the hotkey it pastes a 0.5 instead of 500. It returns a decimal for any number above 1000. I could multiply by 1000 but then the lower numbers don't work right. Hmmmmm! Any ideas?

 

Func HotKey8()
            MouseClick('primary', 1774, 216, 1, 1)        ;Click the quantity box
            Send('^a')                                                          ;Select All
            Send('^a')                                                          ;Select All
            Send('^a')                                                          ;Select All
            sleep(200)
            Send('^c')                                                          ;Copy The Number
            $value = Clipget()                                            ;Retrieve from clipbaord
            $value = $value / 2                                         ;Divide
            Clipput($value)                                                ;Store in clipboard
            MouseClick('primary', 1774, 216, 1, 1)         ;Click the quantity box
            sleep(200)
            Send("{del}{del}{del}{del}{del}{del}")           ;Delete All
            sleep(200)
            Send("{BS}{BS}{BS}{BS}{BS}{BS}{BS}")         ;Delete All
            sleep(200)
            Send('^v')                                                           ;Paste The Number
            sleep(200)
            Send("{ENTER}")                                              ;Enter
    EndFunc

Link to comment
Share on other sites

After messing with this more here's what I am seeing.  With the code below I am still not getting correct values when trying to divide numbers over 1000. For example 800/2=400 and  900/2=450 and 1000/2=0.5 and 2000/2 =1  and so on. It's dropping the 3 zeros at the end.

             $value = Clipget()                                            ;Retrieve from clipbaord
            $value = $value / 2                                         ;Divide
            Clipput($value)                                                ;Store in clipboard

 

When I remove the divide by 2 and just tell it to add a zero ($value = $value + 0 ) then I get  800+0=800 and 900+0=900 and 1000+0=1 and 2000+0=2

I also tried this using excel where I type 1000 in a cell and it returns the undesired result to excel when the number is 1000 or above. I this a known bug?

                  

Link to comment
Share on other sites

30 minutes ago, Keybanger said:

900/2=450 and 1000/2=0.5

I this a known bug?

Hmm :

Global $g_Result
$g_Result = (500 / 2)
ConsoleWrite("500 / 2     = " & $g_Result & @CRLF)
$g_Result = (1000 / 2)
ConsoleWrite("1000 / 2    = " & $g_Result & @CRLF)
$g_Result = (10000 / 2)
ConsoleWrite("10000 / 2   = " & $g_Result & @CRLF)

$g_Result = "10000" / 2 ; Value is a String
ConsoleWrite("'10000' / 2 = " & $g_Result & @CRLF)

Output =

500 / 2     = 250
1000 / 2    = 500
10000 / 2   = 5000
'10000' / 2 = 5000

@Keybanger  : Try

$value = Number(Clipget())

as already suggested by @Danp2 ;)

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

If I change the value to a static number such as  $value = 1000 / 2 then I get the desired result. For some reason the variable is messing everything up. Maybe this just isn't possible using this method. I'll have to keep studying and maybe eventually I'll find a way one of these days. Thanks for the help.

Link to comment
Share on other sites

ehrm, you say you get the error when the number is above 1000.

Is the number you are capturing perhaps formatted with a decimal point (eg. "1.000" instead of "1000")?

Then it would calculate 1/2 which is of course 0.5 and you'd need to remove the thousands-separator.

$value = Clipget()                      ;Retrieve from clipbaord
$value = Stringreplace($value,".", "")  ; remove .
$value = $value / 2                     ;Divide
Clipput($value)                         ;Store in clipboard

 

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

Welcome to the world of integers and floating variables and how to define them before using them and have to account for remainders/rounding. Get used to defining your variable names with less generic ones other than "$value", as down the track you will find it clashes with some nifty external subroutine you have included in your code that uses just the very same variable name and has made it global, and now you are changing it at random as the code threads it way between your code and the external one. Debugging becomes an exercise in coffee and loss of hair (and in your case possible loss of trades).

You may wish to locate your mouse by identifying the input box specifically rather than by screen co-ordinates, just in case your screen resolution changes.

You appear to have redundant code with delete and backspace in your input field, and most of your pauses may be superfluous.

I'd omit the last >>Send("{ENTER}")<< until you got your code right - it may be an expensive exercise!

Keep plugging away - it is how you learn!

It would be nice if your trading software offered that functionality inbuilt. Have you approached the developers to include it in their next version?

Link to comment
Share on other sites

Thanks Confuzzled. Unfortunately the window name is always changing so locating the mouse via input box isn't an option to the best of my knowledge which of course is limited at this point. The redundancy is needed because the platform doesn't always respond the same way when clicking an input field ie sometimes it highlights and sometimes not and sometimes you have to click twice. Very annoying. Think-or-Swim is buggy but I have the ability to code within it which is why I use it. Unfortunately there is no option to code hot keys yet. I'm glad AutoIt gives me this option. Hopefully I will find a solution soon. I have tried changing the variable names to something other then value but still not dividing correct with any number over 1000. Thanks for your input. 

Link to comment
Share on other sites

Question: Is this functionality not available in the included thinkScript®?  Maybe do your arithmetic within the thinkScript® and just use AutoIT3 for your hotkeys. Is it worth asking the developers for hotkey functionality for precisely what you are doing? Other traders may have exactly the same needs.

https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Basic/Chapter-2---Mathematical-Functions

I see they have rounding functionality and declared variable sizing/precision too, similar to AutoIT3.

Comment: Learning multiple scripting languages as a secondary function of what you want to do is a painful exercise.

Hint: Debugging your code where you send intermediate results of variables to the console or a messagebox usually finds your oversights quickly and helps you learn a lot more.

My uncle once told me: Remember you are in a no win scenario - for you to make lots of money on the share market with trading, somebody else has to be losing it. You are betting against others that may have insider knowledge, better information to base their decisions on, and faster/more powerful tools to trade. You don't have to be good, you have to be the best. Best of luck.

Link to comment
Share on other sites

Interestingly I output the math to the console and it turns out that 1000 divided by two does indeed equal 500. But it Think or Swim it still equals 0.5. I think the issue is with the trading platform and not AutoIt. Thanks again confuzzled for the help. Take care.

Link to comment
Share on other sites

Glad you found the underlying problem.

When you report the problem to the developer, ask them to look at the code to maybe add the original functionality you desired. While they are in the bowels of the program fixing the error, they may as well add it in at the same time and call the new release an added features release as well as a bugfix.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...