Script stops running after a while

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
m1k33
Posts: 4
Joined: 29 Mar 2021, 18:16

Script stops running after a while

13 Apr 2021, 19:25

Hey fellas!
I'm having a really hard time getting my script to run continuously. For whatever reason, it just stops after a few minutes or a few hours. I did some reading and potentially found that it might be to do with SetTimer. Any help is much appreciated :) I'm expecting I'll need to give some more information, just let me know what and I'll do my best to provide it ASAP.


Code: Select all

#Include C:\AutohotKey\Chrome.ahk_v1.2\Chrome.ahk

;Variables-----------------------------------------------------------------------
InputBuffer=0
Found=0
Address=0
y1=0
y2=0
y3=0
CAD=1000
InitialRate=0
Invested=0
Running=0
SamplePointer=1
AvgPointer=1
SampleRingBufferSize=10
AvgRingBufferSize=10
AvgBuffer=0
GUICounter=0
;Arrays--------------------------------------------------------------------------
PairName:=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
UsedAddresses:=[]
InputData:=[]
Rate:=[]
High:=[]
Gain:=[]
Volume:=[]
ExchangeRate:=[]
Viable:=[0,0,0,0,0,0,0,0,0,0]
Viability:=[]
Average:=[]
GrowthFactors:=[]
AverageGrowthFactor:=[]
Samples:=[]

Loop,10
	{
	AverageBuffer[A_Index]:=0
	}
	
Loop,10
	{
	LVL1PTR:=A_Index
	Loop,%RingBufferSize%
		{
		Average[LVL1PTR,A_Index]:=0
		}
	}

ChromeProfile := "C:\Users\michaelc\AppData\Local\Google\Chrome\User Data\Default"
ChromeInst := new Chrome(ChromeProfile)
FileRead,FileContents,CompoundBot.log
PageInstance := ChromeInst.GetPage()
PageInstance.Call("Page.navigate", {"url": "https www.binance.com /en/markets"})  Broken Link for safety
PageInstance.WaitForLoad()


;Gui, Add, Edit, x22 y69 w70 h20 , Edit
;Gui, Add, Edit, x22 y99 w70 h20 , Edit
;Gui, Add, Edit, x22 y129 w70 h20 , Edit
Gui, Add, Button, x22 y69 w60 h20 gRun, Run
;Gui, Add, Button, x102 y99 w120 h20 gWriteAverages, Write Averages
;Gui, Add, Button, x102 y129 w120 h20 gWriteSamples, Write Samples

Gui, Add, Text, x22 y10 w200 h15 vInvestedPairNameText,% "Invested Pair: " InvestedPairName
Gui, Add, Text, x22 y25 w200 h15 vInvestedGainText,% "Gain: " InvestedGain
Gui, Add, Text, x22 y40 w200 h15 vInvestedValueText,% "Value: " InvestedValue
Gui, Add, Edit, x250 y10 w250 h540 vLog readOnly,%FileContents%
Gui, Add, Button, x440 y555 w60 h20 gClearLog, Clear Log

Loop, 10
{
y1:=(A_Index*40)+140
y2:=(A_Index*40)+155
Gui, Add, Text, x22 y%y1% w100 h15 vPairName%A_Index%Text,% PairName%A_Index%
Gui, Add, Text, x130 y%y1% w100 h15 vHigh%A_Index%Text,% High%A_Index%
Gui, Add, Text, x130 y%y2% w100 h15 vRate%A_Index%Text,% Rate%A_Index%
Gui, Add, Text, x230 y%y1% w10 h15 vViability%A_Index%Text,% Viable[A_Index]
Gui, Add, Text, x230 y%y2% w10 h15 vAvgGrowthFactor%A_Index%Text,% AverageGrowthFactor[A_Index]
}

Gui, Show, x219 y116 h588 w520, CompoundBot



SetTimer, UpdateData, 1000
SetTimer, UpdateGUI, 3000


UpdateData:

if (Running=1)
	{
		Loop,10
		{
		UsedAddresses[A_Index]:=0
		;msgbox, % UsedAddresses[A_Index]
		}
		Loop,10
			{
			InputIndex:=A_Index - 1
			InputBuffer:= % PageInstance.Evaluate("document.querySelectorAll('.css-1uly3p5')['" InputIndex "'].innerText").Value
			InputData:= strSplit(InputBuffer,"`n","+" "%" "/" " " "$" ",")
			PairName_:=InputData[1] "/" InputData[2]
			Found=0	
			Loop,10
				{
				if (PairName_=PairName[A_Index])
					{
					Address:=A_Index			
					Found=1
					UsedAddresses[Address]:=1
					Goto ContinueLoop
					}
				}
			if (Found=0)
				{
				Loop,10
					{
					if (PairName[A_Index]=0)
						{
						Address:=A_Index					
						PairName[Address]:=PairName_
						UsedAddresses[Address]:=1
						Goto ContinueLoop
						}
					}
				}
			ContinueLoop:
			Rate_:=InputData[3]
			IfNotInString,Rate_, x
				{
				ExchangeRate_:= InputData[4]
				Gain_:=InputData[5]
				High_:=InputData[6]
				Volume_:=StrReplace(InputData[9], ",","")
				}
			Else
				{
				Rate_:=InputData[4]
				ExchangeRate_:= InputData[5]
				Gain_:=InputData[6]
				High_:=InputData[7]
				Volume_:=StrReplace(InputData[10], ",","")

				}
				
			Rate[Address]:=Rate_
			High[Address]:=High_
			Gain[Address]:=Gain_
			ExchangeRate[Address]:=ExchangeRate_
			Volume[Address]:=Volume_
			Samples[Address,SamplePointer]:= Rate_
			Viability[Address]:=Rate_/High_
			if (Viability[Address]>0.99)
				{
				Viable[Address]:=1
				ViablePairFound:=1
				}
				
			}
			SamplePointer++
			loop,10
				{
				Address:=A_Index
				if (UsedAddresses[Address]=0)
					{
					PairName[Address]:=0
					}
						
				}
				if (ViablePairFound=1)
				{
				if (Invested=0)
					{
					Goto Invest
					}

				if (Invested=1)
					{
					Goto Monitor
					}
				}
	}
Return

Invest:
HighestVolume=1000
loop,10
	{
	if (Viable[A_Index]=1)
		{
		if (Volume[A_Index]>HighestVolume)
			{
			HighestVolume:=Volume[A_Index]
			HighestVolumeAddress:=A_Index
			InitialRate:=Rate[HighestVolumeAddress]
			}
		}
	}
InvestedPairName:=PairName[HighestVolumeAddress]
SubmissionTime:=A_Now
FormatTime, SubmissionTime, ,dd/MM/yy h:mm:ss tt
FileAppend, %InvestedPairName% bought at %SubmissionTime%  / Start Value: %CAD%`n`n, CompoundBot.log
FileRead,FileContents,Compoundbot.log
GuiControl, , Log, %FileContents%
Invested:=1

return

Monitor:
InvestedGain:=Rate[HighestVolumeAddress]/InitialRate
InvestedValue:=CAD*InvestedGain
if (Viability[HighestVolumeAddress]<0.97)
	{
	Viable[Address]:=0
	CAD:=CAD*InvestedGain
	ViablePairFound=0
	Invested=0
	SubmissionTime:=A_Now
	FormatTime, SubmissionTime, ,dd/MM/yy h:mm:ss tt
	FileAppend, %InvestedPairName% sold at %SubmissionTime%  / End Value: %CAD% / Final Outcome: %InvestedGain%`n`n, CompoundBot.log
	FileRead,FileContents,Compoundbot.log
	GuiControl, , Log, %FileContents%
	InvestedPairName:=0
	InvestedGain:=0
	}
Return

UpdateGUI:
Loop, 10
{
GuiControl,  ,InvestedPairNameText,% "Invested Pair: " InvestedPairName
GuiControl,  ,InvestedGainText,% "Invested Gain: " InvestedGain
GuiControl,  ,InvestedValueText,% "Invested Value: $" InvestedValue
GuiControl,  ,PairName%A_Index%Text, % PairName[A_Index]
GuiControl,  ,Rate%A_Index%Text, %  "Rate: " Rate[A_Index]
GuiControl,  ,High%A_Index%Text, % "High: " High[A_Index]
GuiControl,  ,Viability%A_Index%Text, % Viable[A_Index]
GuiControl,  ,AvgGrowthFactor%A_Index%Text, % AverageGrowthFactor[A_Index]
}
Return

Run:
if (Running=0)
	{
	Running=1
	}
else
	{
	Running=0	
	}
Return

GuiClose:
ExitApp
return

ClearLog:
MsgBox, 4,, Are you sure you want to clear the log?
IfMsgBox, Yes
file := FileOpen("C:\AutoHotKey\Chrome.ahk_v1.2\CompoundBot.log", "w") ;this wipes out all text in the file and overwrites it as a blank .txt file
file.close() ;this closes the file
FileRead,FileContents,CompoundBot.log
GuiControl, , Log, %FileContents%
return
IfMsgBox, No
return
[Mod edit: [code][/code] tags added.]
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script stops running after a while

13 Apr 2021, 19:29

As a side note - binance has an official WebAPI, afaik, returning data in JSON format. (Edit: https://binance-docs.github.io/apidocs/spot/en/#introduction)
That would probably be a more reliable and efficient solution than scraping a page.

Edit 2: Some older posts re the binance API (might not be up-to-date, but might still help):
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=49256&p=219024#p219024
https://www.autohotkey.com/boards/viewtopic.php?f=9&t=44370&p=201080#p201080
For just getting ticker data, you might not even need an API key. Iirc, there are public API endpoints for that.

There might be more posts...
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Script stops running after a while

13 Apr 2021, 19:36

The question that you may wish to ask is, "How can I debug my script?" There are various ways. Since the script does not work, break it down into something smaller. Delete loops and non-essential lines, until you have the basic skeleton working. You can then add back parts in small amounts, testing each time, along the way. An additional approach in some cases is to log some essential lines to a file; you can then determine where the process breaks.

Regarding IfMsgBox, Yes, without a block enclosed in braces, this command will apply only to the single line that follows it. In your current script, the penultimate line cannot be executed, because it is preceded by Return.
m1k33
Posts: 4
Joined: 29 Mar 2021, 18:16

Re: Script stops running after a while

13 Apr 2021, 21:13

Thanks for the heads up. I'm not by any means a good programmer. I can usually use loops and google to do what needs to be done, but honestly I have no idea what an API is... yet. Thanks so much for pointing me in this direction.

gregster wrote:
13 Apr 2021, 19:29
As a side note - binance has an official WebAPI, afaik, returning data in JSON format. (Edit: https binance-docs.github.io /apidocs/spot/en/#introduction) Broken Link for safety
That would probably be a more reliable and efficient solution than scraping a page.

Edit 2: Some older posts re the binance API (might not be up-to-date, but might still help):
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=49256&p=219024#p219024
https://www.autohotkey.com/boards/viewtopic.php?f=9&t=44370&p=201080#p201080
For just getting ticker data, you might not even need an API key. Iirc, there are public API endpoints for that.

There might be more posts...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy and 386 guests