Nested If Doesnt Work, Need Help With Syntax For If

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 14:49

Hi, Im trying to nest an If Statement, it refuses to display msgbox, but i'm pretty sure its the syntax, probably need to use And as well, but i cant figure out the syntax ...

Its a pretty basic if statement, but i cant figure out the syntax.

Thanks.,

The nested If Statement on line 147

Code: Select all

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Dropped Higher Then 20

							}



The Nested If Statements From The Code

Code: Select all

If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Dropped Higher Then 20

							}

							GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}
							}






The Complete Code :

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force
SetFormat,Float,3.2
;LTrim: Omits spaces and tabs at the beginning of each line




Loop 180
{
x++
    FileReadLine, NewVarArray%x%,  D:\Project\VSync\PriceTest.txt, %A_Index%
    
}

MyVar=
(
54000
54050
54100
54150
54200
54250
54300
54350
54400
54450
54500
54550
54600
54650
54700
54750
54800
54850
54900
54950
55000
53950
53900
53850
53800
53750
53700
53650
53600
53550
53500
53450
)


          
StringSplit, NewVarArray, MyVar, `n ; Separates a string into an pseudo-array

;MsgBox,% NewVarArray4






Gui, Add, Text,x10 w900 y20   vPrice  ,  Price

Gui, Add, Text,x10 w900 y50   vGoneup  , GoneUp 




Gui, Show,h200 w900 x1600 y100,1x



TotalBought:=3000	
PriceBoughtFor:=54000	
CurrentPrice:=55000

Amount:=TotalBought/PriceBoughtFor

PercentageRisenBy:=(CurrentPrice-PriceBoughtFor)/(PriceBoughtFor/100)

ProfitMade:=(TotalBought*PercentageRisenBy)/100

																											;msgbox %ProfitMade% %PercentageRisenBy% %ProfitMade2%


loop
{

PriceRiseDetected=No
PriceLowerDetected=No
MainInc:=1
Detect2:=1

Loop 30
{





CurrentPrice:=NewVarArray%MainInc%

;Calc Profit Made

;1. Calc PercentageRisen By 55000-54000 / 54000/100 =1.81%
PercentageRisenBy:=(CurrentPrice-PriceBoughtFor)/(PriceBoughtFor/100) 

;2. Multiply By TotalBought * PercentageRisenBy / 100  3000 * 1.81% / 100
ProfitMade:=(TotalBought*PercentageRisenBy)/100


GuiControl,, Price, %CurrentPrice%   %ProfitMade% %PercentageRisenBy%



if Detect2 >= 2
{

FindPreviousPrice:=MainInc-1

DetectPriceInArray1:=NewVarArray%FindPreviousPrice%

;msgbox %DetectPriceInArray1%

DetectPriceInArray2:=NewVarArray%MainInc%

;msgbox %DetectPriceInArray2%



								
							If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Dropped Higher Then 20

							}

							GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}
							}


							If (DetectPriceInArray2 < DetectPriceInArray1)
							{
							If PriceLowerDetected=No
							{
							;msgbox GoneDown From %DetectPrice1% To %DetectPrice2%
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							GuiControl,, GoneUp,  GoneDown From %DetectPrice1% To %DetectPrice2%
							
							If (ProfitMade <= -20)
							{

							msgbox Dropped Lower Then -20

							}



							PriceRiseDetected=No
							PriceLowerDetected=Yes
							}
							}

Detect2:=0
}


Sleep 150
MainInc++
Detect2++



}

}
return
















esc::
{

sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
mikeyww
Posts: 26611
Joined: 09 Sep 2014, 18:38

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 14:56

You may also be asking the question, "How do I go about debugging my script?" If you are wondering why ProfitMade is not greater than 20, you can display its value in the script, and then backtrack and determine how it arrived at its value. You can add some display lines to show you the values of contributing variables.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:03

Please run the complete code. If you run the code, the script works fine, profitmade goes above 20.

Its the position of the if > 20 in the middle of the nested if statements, thats causing it to refuse working.

The if statement works fine if I place it here

Code: Select all

	If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							
							GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Dropped Higher Then 20

							}

							
							}

But it refuses to work if its placed in the centre of the if statements like this ... I need it in the centre of the nested If statements ... But it refuses to work in this position ...

Code: Select all

		
							If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Dropped Higher Then 20

							}

							GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}
							}
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:12

Ok, this is a simplified version of the script, should be alot easier to see its the position of the if profitmade > xs statement, causing the problem ...

Code: Select all


Detect2:=2

if Detect2 >= 2
{


PriceRiseDetected=No
ProfitMade:=21
DetectPriceInArray1:=20

DetectPriceInArray2:=21
								
							If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Raised Higher Then 20

							}

							;GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}
							}
}
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:19

Ok, nm you're right ... lol

Any idea's how to get the if profitmade > xs statement on line 141 to work, would be great ...

Complete code again, just in case ...

Thanks.,

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force
SetFormat,Float,3.2
;LTrim: Omits spaces and tabs at the beginning of each line




Loop 180
{
x++
    FileReadLine, NewVarArray%x%,  D:\Project\VSync\PriceTest.txt, %A_Index%
    
}

MyVar=
(
54000
54050
54100
54150
54200
54250
54300
54350
54400
54450
54500
54550
54600
54650
54700
54750
54800
54850
54900
54950
55000
53950
53900
53850
53800
53750
53700
53650
53600
53550
53500
53450
)


          
StringSplit, NewVarArray, MyVar, `n ; Separates a string into an pseudo-array

;MsgBox,% NewVarArray4






Gui, Add, Text,x10 w900 y20   vPrice  ,  Price

Gui, Add, Text,x10 w900 y50   vGoneup  , GoneUp 




Gui, Show,h200 w900 x1600 y100,1x



TotalBought:=3000	
PriceBoughtFor:=54000	
CurrentPrice:=55000

Amount:=TotalBought/PriceBoughtFor

PercentageRisenBy:=(CurrentPrice-PriceBoughtFor)/(PriceBoughtFor/100)

ProfitMade:=(TotalBought*PercentageRisenBy)/100

																											;msgbox %ProfitMade% %PercentageRisenBy% %ProfitMade2%


loop
{

PriceRiseDetected=No
PriceLowerDetected=No
MainInc:=1
Detect2:=1

Loop 30
{





CurrentPrice:=NewVarArray%MainInc%

;Calc Profit Made

;1. Calc PercentageRisen By 55000-54000 / 54000/100 =1.81%
PercentageRisenBy:=(CurrentPrice-PriceBoughtFor)/(PriceBoughtFor/100) 

;2. Multiply By TotalBought * PercentageRisenBy / 100  3000 * 1.81% / 100
ProfitMade:=(TotalBought*PercentageRisenBy)/100


GuiControl,, Price, %CurrentPrice%   %ProfitMade% %PercentageRisenBy%

Detect2:=2

if Detect2 >= 2
{


PriceRiseDetected=No
ProfitMade:=21
DetectPriceInArray1:=20

DetectPriceInArray2:=21
								
							If (DetectPriceInArray2 > DetectPriceInArray1)
							{
							If PriceRiseDetected=No

							{
								
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							;msgbox GoneUp From %DetectPrice1% To %DetectPrice2%

							xs:=20
							If (ProfitMade > xs)
							{

							msgbox Raised Higher Then 20

							}

							;GuiControl,,  GoneUp,  GoneUp From %DetectPrice1% To %DetectPrice2%


							PriceRiseDetected=Yes
							PriceLowerDetected=No
							}
							}
}

							If (DetectPriceInArray2 < DetectPriceInArray1)
							{
							If PriceLowerDetected=No
							{
							;msgbox GoneDown From %DetectPrice1% To %DetectPrice2%
							DetectPrice1:=DetectPriceInArray1
							DetectPrice2:=DetectPriceInArray2

							GuiControl,, GoneUp,  GoneDown From %DetectPrice1% To %DetectPrice2%
							
							If (ProfitMade <= -20)
							{

							msgbox Dropped Lower Then -20

							}



							PriceRiseDetected=No
							PriceLowerDetected=Yes
							}
							}

Detect2:=0
}


Sleep 150
MainInc++
Detect2++



}

}
return
















esc::
{

sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
mikeyww
Posts: 26611
Joined: 09 Sep 2014, 18:38

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:25

I think you are missing the point about how to debug the script. Add a MsgBox just before that conditional statement. The MsgBox should display the value of ProfitMade. See what it is.

If the MsgBox appears, then it means that you will see the value, and you can then backtrack to determine why it is less than or equal to 20.

If the MsgBox does not appear, then it means that the previous conditional statement (or any parent conditional statement) is false, and you can determine why it is false.
Last edited by mikeyww on 08 May 2021, 15:34, edited 2 times in total.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:31

Its not detecting 20, because that portion of the script triggers before profitmade hits 20.

If I place it outside the if statement, the msgbox just pops up in an infinite loop ... Some sort of switch should stop it from popping up in an infinite loop.
User avatar
mikeyww
Posts: 26611
Joined: 09 Sep 2014, 18:38

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:34

I am surprised that you were able to run your last script, because it generates a syntax error.

Since your script does not work, break it down into smaller parts. Remove the non-essential lines, and loops. Work with only the problematic lines. Get those working first.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:40

Thanks, I got it ... Could you give me a quick hand for the syntax for AND, cant get it to work for some reason ...

Code: Select all

	
	xs:=20
	ProfitMade:=21
	PriceHigherThen20=No

							If (ProfitMade > xs And PriceHigherThen20=No)
							{

							msgbox Raised Higher Then 20
							PriceHigherThen20=Yes
							}
User avatar
mikeyww
Posts: 26611
Joined: 09 Sep 2014, 18:38

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:49

If uses expressions.

Code: Select all

If (ProfitMade > xs And PriceHigherThen20="No")
Explained: ExpressionsIf

One way to avoid the trap:

Code: Select all

xs := 20
ProfitMade := 21
PriceHigherThen20 := False
If (ProfitMade > xs And !PriceHigherThen20)
{
 MsgBox, Raised Higher Then 20
 PriceHigherThen20 := True
}
Although not required, approaches like this decrease the chance that a bug may be introduced into the script.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Nested If Doesnt Work, Need Help With Syntax For If

08 May 2021, 15:52

Ah Thanks, much appreciated, code is working fine, thanks for pointing out the mistake ...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 145 guests