I'm running an Autohotkey script that auto capitalizes the first character of a sentence (for example in Texstudio or Chrome). The script (specifically the loop I guess) sometime causes 30–40% of CPU. Therefore, I am wondering if there is a possibility to optimize the code (maybe without using loop?) to reduce the CPU usage. Thanks in advance. Here is the code:
#SingleInstance force
#NoEnv
SetBatchLines -1
Loop {
if WinActive("ahk_exe texstudio.exe") or WinActive("ahk_exe chrome.exe")
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Tab}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key) {
If state = 1
state = 2
} Else {
If state = 2
Send {BS}{%key%}
state =
}
}
Return
SetTimer consumes much less CPU because of the period.
#SingleInstance force
#NoEnv
#Persistent
; SetBatchLines -1
; create a group of the programs in which you want auto-capitalize
GroupAdd, auto_capitalize_group, ahk_exe texstudio.exe
GroupAdd, auto_capitalize_group, ahk_exe chrome.exe
SetTimer, auto_capitalize, 300 ; check every 300 ms
Return
auto_capitalize:
if !WinActive("ahk_group auto_capitalize_group")
return ; do nothing
; otherwise:
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Tab}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key)
{
If state = 1
state = 2
}
Else
{
If state = 2
Send {BS}{%key%}
state =
}
Return
Related
I am attempting to create a share/stock portfolio based system that will enter at the open and possibly exit on the same day at close if the conditions are met. I have this basicaly working. The thing i cant get going is that I would like my stock system to only ever have 1 open postion in a company at any time.
It seems that if there is both an exit and an entry on the same day, amibroker backtesting is allowing the same company to be purchased on the open, if that same company has a sell order on that same day. Here is an example of this:
Notice at point 1 - we would be entering at the open on the 17th
At point 2, we get a sell signal that day, so we should exit at Close on the 24th.
However at point 3 - we have an entry for the same company on the same day.
To be clear - I would like to allow multiple entries on the same day - this is working. The only thing i would like to figure out is to prevent the backtester from entering the SAME company on the SAME day it exits, as due to the system rules, we would have one day of having 2 positions in the 1 company.
Here is the sample code to replicate this:
SetOption("AllowSameBarExit", True );
SetOption("SettlementDelay", 1 );
Buy = C > MA(C,10);
Sell = C < MA(C,10) OR C > O;
// trade on todays open
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = Open;
SellPrice = Close;
SetPositionSize( 20, spsPercentOfEquity );
I have read and re-read the page on portfolio timing: here but I still cant figure out how to prevent the entries for the same company on the same day as an exit.
Any help would be greatly appreciated!
UPDATE
It appears that using the OR C > O in the SELL condition is effecting this. If I remove the OR C > O part, I get the correct behaviour. It is entering on the NEXT day. Now Im wondering how to use that exit without reverting back to same bar same company entry and exit...
Thanks to Tomasz from Amibroker for posting the below solution:
SetOption("AllowSameBarExit", True );
BuyPrice = Open;
SellPrice = Close;
Buy = Ref( Close > MA( Close, 10 ), -1 );
Sell = Close > Open OR Close < MA( Close,10);
// removing buys you don't want
intrade = False;
for( i = 0; i < BarCount; i++ )
{
if( NOT intrade )
{
if( Buy[ i ] )
{
intrade = True;
// same bar sell
if( Sell[ i ] ) intrade = False;
}
}
else
{
if( Sell[ i ] )
{
intrade = False;
Buy[ i ] = False; // remove buy if exited this bar
}
}
}
You can find : a detailed discussion here
I developed a simple app in MIT APP Inventor that controls a heat pump water heater.
The app sends a different string for each button pressed and the NodeMcu verifies which button was pressed, as you can see in the code below.
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = ""
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
local _on,_off = "",""
if(_GET.pin == "ip")then
local ip=wifi.sta.getip()
local ler_ip=string.sub(ip,1,13)
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint("IP="..ler_ip)
elseif(_GET.pin == "05a60")then
sp_temperaturas[5]=60
elseif(_GET.pin == "06a60")then
sp_temperaturas[6]=60
elseif(_GET.pin == "Ferias")then
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint(" Modo ferias ")
modo_ferias()
elseif(_GET.pin == "Parar2")then
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint(" Parar")
end
end)
conn:on("sent", function (c) c:close() end)
end)
And when the button "Ferias" is pressed the system starts the heating process by calling the function modo_ferias().
function aquecer()
local kp=100/10
local ki=5
local kd=5
local tempo_on=0
local tempo_off=0
i=0
local duty=0
erro=sp_temp-t_atual
soma_erro = soma_erro + erro/5;
dif_erro = (erro - erro_ant)/5;
erro_ant = erro;
print(erro.." "..soma_erro.." "..dif_erro)
duty= kp * erro + soma_erro / ki + dif_erro * kd
print(duty)
tempo_on= duty *50
if (tempo_on > 5000) then
tempo_on = 5000
end
tempo_off = 5000 - tempo_on
repeat
i = i + 1
tmr.delay(1000)
until (i >= tempo_off)
gpio.write(8, gpio.HIGH)
repeat
i = i + 1
tmr.delay(1000)
until (i == 5000)
gpio.mode(8, gpio.INPUT)
end
function modo_ferias()
repeat
sair_ferias=0
pressao()
if (pressao_psi <=3)
sair_ferias=1
end
t_atual=ler_temperatura()
local int = string.format("%d", t_atual ) -- parte inteira
local dec = string.format("%.1d", t_atual % 10000) -- parte decimal com uma casa
t_display = int .. "." .. dec
rtc()
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint("Temp="..t_display.." ST="..sp_temp..data_horas)
sp_temp=40
local horas_ferias=hour
if(horas_ferias==0 or horas_ferias==1 or horas_ferias==2 or horas_ferias==3 or horas_ferias==4 or horas_ferias==5 or horas_ferias==6) then
sp_temp=70
end
if (sp_temp>t_atual) then
aquecer()
end
until (sair_ferias==1)
end
And here is where my problem appears. If I press a button form the app after the "Ferias" button been pressed, the NodeMcu won't know it because the program is in the heating functions and not verifying if the app sended any instruction.
Is there any way to listen the app commands and to do the heating process at the same time?
There are a few things
Global state
because the program is in the heating functions and not verifying if the app sended any instruction
If the commands triggered by pressing the various buttons can not run independently from each other then you need some form of global state to make sure they don't interfere.
Busy loop
repeat
i = i + 1
tmr.delay(1000)
until (i == 5000)
This is a no-go with NodeMCU as it's essentially a stop-the-world busy loop. Besides, tmr.delay is scheduled to be removed because it's abused so often.
Task posting
node.task.post is a possible solution to scheduling tasks for execution "in the near future". You could use this to post task from the on-receive callback rather executing them synchronously.
ArrayCount = 0
Loop, Read, Times.txt ; This loop retrieves each line from the file.
{
ArrayCount += 1 ; Keep track of how many items are in the array.
ArrayTime%ArrayCount% := A_LoopReadLine
}
WinGetTitle, Title, A
Loop %ArrayCount%
{
element := ArrayTime%A_Index%
Time = %A_WDay%%A_Hour%%A_Min%
msgbox %Time% , %element%
if (Time=%element%)
{
IfWinExist, Test.txt
{
WinActivate
Sleep 500
Send Hi{enter}
msgbox %Time% , %element%
Sleep 500
WinActivate, %Title%
}
}
}
Ok so the main issue is with this part:
if (Time=%element%)
I have also tried
if (%Time%=%element%)
if (A_WDay . A_Hour . A_Min=%element%)
And I think some other similar variations, the problem I'm getting is it's either always true, or always false, depending on how I have it written.
Inside the text file is a list like this:
10000
10700
11400
20400
21100
I add an extra line that has the current time for testing, and I added the msgbox to compare, and I can clearly see they're both the same when it doesn't work, or that they're different when it does. Sorry for such a basic question but I feel like I've really been trying for a long time and read everything I can on variables and IF statements, thanks for any help.
Also the point of it is I need it to go off every 7 hours starting at midnight on sunday, this is what I came up with, if there's maybe a completely better way in general I'd be happy to hear that too.
Try this:
if % Time = element
{
MsgBox, Equal!
}
As for the scheduling part, try running your script through Windows Task Scheduler (hit Windows+R, type taskschd.msc and press Enter). There are tutorials on the Internet explaining how to create new tasks.
With regard to timers, have a look at this as an example.
SetTimer, AlertType1, 60000
ToAlertType1:=1
ToAlertType2:=1
AlertType1:
;If A_WDay between 2 and 7 ; is day monday - sunday?
;{
If (A_Hour = 7 or A_Hour = 13)
{
If (ToAlertType1)
{
SoundBeep, 500, 500
ToAlertType2:=1
ToAlertType1:=0
MsgBox, 4096,%AppName%, Msg1.
Return
}
}
Else if (A_Hour = 21)
{
If (ToAlertType2)
{
SoundBeep, 500, 500
ToAlertType2:=0
ToAlertType1:=1
MsgBox, 4096,%AppName%, Msg2.
Return
}
}
;}
Return
I'd like to run a program when any key is pressed with AutoHotKey
Something like:
AnyKey::Run, D:\my\program\to\run\on\any\key.bat
EDIT2:
This code is working perfectly:
#InstallKeybdHook
SetTimer, AnyKeyPressed, 100
AnyKeyPressed:
if( A_TimeIdlePhysical < 100 ){
Run, D:\my\program\to\run\on\any\key.bat
}
^!p::pause
A simple solution:
#InstallKeybdHook ; this MUST be called at the start of your script
AnyKeyPressed() ; returns a 1 if any keyboard key is pressed, else returns 0
{
if( A_TimeIdlePhysical < 25 )
return 1
return 0
}
Note this function will return 1 if any key is pressed OR being held down, so change your code appropriately.
What happens is; the #InstallKeybdHook will change the behaviour of A_TimeIdlePhysical to only look for keyboard events.
You have to check A_TimeIdlePhysical periodically, not just once on script start:
#InstallKeybdHook
SetTimer, CheckActivity, 100
Exit
CheckActivity:
if(A_TimeIdlePhysical < 100) {
Run, myNastyPictureMaker.bat
ExitApp
}
return
You can use SetTimer for recurring tasks. The script stops when the first activity was detected; otherwise, it would take a picture every 100 ms (or whatever timeout you set).
P.S: I hope you only want to use such a script on your private PC and not some publically available computer...
Use Input, AnyKey, L1 to wait for any key to be pressed. L1 means after one key was pressed, without a [end] key required. You can check the content of AnyKey, but don't really need to.
Perhaps a list of known keys might work?
keys = ``1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./
Loop Parse, keys
Run, D:\my\program\to\run\on\any\key.bat
return
This is what comes to mind.
#Persistent
#InstallKeyBDHook
SetTimer, WaitingForKey, 100
Return
WaitingForKey:
Input, LogChar, B I L1 V
LogWord := LogWord . LogChar
ToolTip, % LogWord
;Run, D:\my\program\to\run\on\any\key.bat
LogWord:=
Return
^!p::pause
Ok, I realize this is a very niche issue, but I'm hoping the process is straight forward enough...
I'm tasked with creating a data file out of Customer/Order information. Problem is, the datafile has a 5 product max limit.
Basically, I get my data, group by cust_id, create the file structure, within that loop, group by product_id, rewrite the fields in previous file_struct with new product info. That's worked all well and good until a user exceeded that max.
A brief example.. (keep in mind, the structure of the array is set by another process, this CANNOT change)
orderArray = arranyew(2);
set order = 1;
loop over cust_id;
field[order][1] = "field(1)"; // cust_id
field[order][2] = "field(2)"; // name
field[order][3] = "field(3)"; // phone
field[order][4] = ""; // product_1
field[order][5] = ""; // quantity_1
field[order][6] = ""; // product_2
field[order][7] = ""; // quantity_2
field[order][8] = ""; // product_3
field[order][9] = ""; // quantity_3
field[order][10] = ""; // product_4
field[order][11] = ""; // quantity_4
field[order][12] = ""; // product_5
field[order][13] = ""; // quantity_5
field[order][14] = "field(4)"; // trx_id
field[order][15] = "field(5)"; // total_cost
counter = 0;
loop over product_id
field[order[4+counter] = productCode;
field[order[5+counter] = quantity;
counter = counter + 2;
end inner loop;
order = order + 1;
end outer loop;
Like I said, this worked fine until I had a user who ordered more than 5 products.
What I basically want to do is check the number of products for each user if that number is greater than 5, start a new line in the text field, but I'm stufk on how to get there.
I've tried numerous fixes, but nothing gives the results I need.
I can send the entire file if It can help, but I don't want to post it all here.
You need to move the inserting of the header and footer fields into product loop eg. the custid and trx_id fields.
Here's a rough idea of one why you can go about this based on the pseudo code you provided. I'm sure that there are more elegant ways that you could code this.
set order = 0;
loop over cust_id;
counter = 1;
order = order + 1;
loop over product_id
if (counter == 1 || counter == 6) {
if (counter == 6) {
counter == 1;
order= order+1;
}
field[order][1] = "field(1)"; // cust_id
field[order][2] = "field(2)"; // name
field[order][3] = "field(3)"; // phone
}
field[order][counter+3] = productCode; // product_1
field[order][counter+4] = quantity; // quantity_1
counter = counter + 1;
if (counter == 6) {
field[order][14] = "field(4)"; // trx_id
field[order][15] = "field(5)"; // total_cost
}
end inner loop;
if (counter == 6) {
// loop here to insert blank columns and the totals field to fill out the row.
}
end outer loop;
One thing goes concern me. If you start a new line every five products then your transaction id and total cost is going to be entered into the file more than once. You know the receiving system. It may be a non-issue.
Hope this helps
As you put the data into the row, you need check if there are more than 5 products and then create an additional line.
loop over product_id
if (counter mod 10 == 0 and counter > 0) {
// create the new row, and mark it as a continuation of the previous order
counter = 0;
order = order + 1;
field[order][1] = "";
...
field[order][15] = "";
}
field[order[4+counter] = productCode;
field[order[5+counter] = quantity;
counter = counter + 2;
end inner loop;
I've actually done the export from an ecommerce system to MOM, but that code has since been lost. I have samples of code in classic ASP.