AHK IF statement - arrays

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

Related

Prevent Amibroker from entering the same day as its exit in the same symbol

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

reboot in a click mouse loop not working

I am trying to reboot PC at specific hour when a click mouse loop is running. I used if + else conditions and when I try to run both pieces of code separately the script works but when put together only the mouse clicking loop is running while the timer process takes no effect. Any ideas what’s wrong?
^+f::
SetTimer, Chronos, 500
return
Chronos:
FormatTime, TimeToMeet,,HHmm
If (TimeToMeet = 2018)
{
Run, %comspec% /c shutdown -r -f -t 0
}
else
{
loop
{
MouseClick, Left, 787, 512, 1,0
Sleep 10000
}
}
Return
k::Pause
The loop statement never exits.
You're using a timer so I assume you want your code to be event driven. If that's the case, make the MouseClick part of a timer instead of a sleeping loop.
If you don't want to use events, put your shutdown clause inside of the loop
This clicks (787,512) every 10 seconds until 20:18, at which time it shuts the computer down
^+f::
SetTimer, Chronos, 500
return
Chronos:
loop
{
FormatTime, TimeToMeet,,HHmm
If (TimeToMeet = 2018)
{
Run, %comspec% /c shutdown -r -f -t 0
}
MouseClick, Left, 787, 512, 1,0
Sleep 10000
}
k::Pause
This clicks (787,512) every 10 seconds until 20:18, at which time it shuts the computer down
F2:: start()
F3:: stop()
start()
{
global running := 1
while running {
FormatTime TimeToMeet,,HHmm
if (TimeToMeet = 1349 )
Shutdown 13
click 787,512
sleep 10000
}
}
stop()
{
global running := 0
}

Use of pace in gatling to control rate

I have following scenario which has two requests (RequestOne and RequestTwo). It is setup to run for 3 users and 1 repetition. The simulation should have taken at least 20 seconds to finish as I am using 20 seconds as pacing. However, every time I run it, it finishes in less than 20 seconds. I tried with different values for pacing as well.
val Workload = scenario("Load Test")
.repeat(1, "repetition") {
pace(20 seconds)
.exitBlockOnFail {
.feed(requestIdFeeder)
.group("Load Test") {
.exec(session => {
session.set("url", spURL)
})
.group("RequestOne") {exec(requestOne)}
.feed(requestIdFeeder)
.group("RequestTwo") {exec(requestTwo)}
}
}
}
setUp(Workload.inject(atOnceUsers(3))).protocols(httpProtocol)
output
Simulation com.performance.LoadTest completed in 11 seconds
Found the problem. I used only 1 repetition so the scenario didn't need to wait for the 20sec pacing to complete and it exited early. Setting repetition to > 1 helped achieve the desired rate.
val Workload = scenario("Load Test")
.repeat(10, "repetition") {
pace(20 seconds)
.exitBlockOnFail {
So, if you want to achieve fixed number of transactions in your simulation, use repetition, otherwise use "forever (" as mentioned in gatling docoumentation to achieve consistent rate.
val Workload = scenario("Load Test")
.forever (
pace(20 seconds)
.exitBlockOnFail {

AHK script for a timed autofire function

Goal of the script: to continually press Numpad0 for 10 seconds each time hotkey is pressed.
Current code:
toggle = 0
#MaxThreadsPerHotkey 2
timerToggle:
Toggle := !Toggle
sleep 10000
Toggle := !Toggle
F12::
SetTimer, timerToggle, -1
While Toggle{
send {NumPad0}
sleep 100
}
return
At present, the script will run as intended, but only once. Attempting to run it again after the first time does nothing. What am I missing?
I would rather use SetTimers instead of a 10 sec. long while like so
F12::
Send {Numpad0}
SetTimer, start, 100
SetTimer, stop, -10000
return
start:
Send {Numpad0}
return
stop:
SetTimer, start, off
return
Your script likely doesn't toggle your variable correctly. Here is a cleaner version of what you are trying to do which uses A_TickCount:
F12::SetTimer, HoldNumPad, -1
HoldNumPad:
kDown := A_TickCount
While ((A_TickCount - kDown) < 10000)
{
Send {Numpad0}
Sleep 100
}
Return
Note that pressing F12 while the label is running will not have any affect.
EDIT: Made SetTimer use -1 period to run only once, thanks to MCL.

AutoHotKey run program on any key

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

Resources