Sign in to follow this  
Followers 0
VirajPatel

Need Help with Starting and Pausing a script

5 posts in this topic

Hi I am new to autoit and have spent maybe a week scripting.

I found some code that creates a gui and adds buttons for start and stop.

It's using a switch or interrupt to swtich between two functions. One functions runs my main progroam (func1) the other just idles and intterupts the first program and stops it from continuing further (func2). When i first run the script the gui comes up and i can lcick start and the first function runs as it should. I click stop and the first function halts (and hte second function starts which is to idle). Now if i click start again it wont run the first function, i have to click start twice after stopping to get the main function to run. Any help would be appreciated. I've ommited some stuff for the main func that shouldnt matter.

#include 
#include 


; Declare a flag
$fInterrupt = 0

Opt("GUIOnEventMode", 1)
Opt('MouseCoordMode', 0)


$hGUI = GUICreate("Trading Script", 200, 100)
GUISetBkColor (0xff3300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$hButton_1 = GUICtrlCreateButton("Start", 15, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Stop", 100, 10, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
$hButton_3 = GUICtrlCreateButton("Run", 40, 50, 125, 30)
GUICtrlSetOnEvent($hButton_3, "_Func_3")



GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


While 1
sleep(10)
WEnd

Func _Func_1() ;FUNCTION START


; Make sure the flag is cleared
$fInterrupt = 0
$runner = Not $runner


While $runner
GUISetBkColor (0x33ff33)



Local $getfile = 0
While $getfile = 0

;Interrupt switch
If $fInterrupt <> 0 Then

; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("SCRIPT HALTED" & @CRLF)
EndSwitch
Return
EndIf
;Interrupt switch

If FileExists("C.txt") Then
$file = FileOpen("C.txt", 0)
$All = FileReadLine($file, -1)
$Upper = StringMid($All, 7, 6)
$Lower = StringMid($All, 20, 6)
$Last = StringMid($All, 33, 6)
$getfile = 1


Else
ConsoleWrite("No File" & @CRLF)
$getfile = 0
sleep(1250)
EndIf
WEnd
EndFunc

Func _Func_2()
GUISetBkColor (0xff3300)


Sleep(100)

EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)

; The Func 2 button was pressed so set the flag
If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1


Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The code does not run as posted...one while...wend pair is not terminated.

kylomas

edit: and you are missing a couple functions

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I was reluctant to post the entire code, but here goes anyways.

local $Upper, $Lower, $Last ;Calcualtion variables
Local $file, $All ;File and last string variable
Local $getfile ;local loop variable
global $runner ;global loop variable


#include
#include


; Declare a flag
$fInterrupt = 0

Opt("GUIOnEventMode", 1)
Opt('MouseCoordMode', 0)


$hGUI = GUICreate("Trading Script", 200, 100)
GUISetBkColor (0xff3300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$hButton_1 = GUICtrlCreateButton("Start", 15, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Stop", 100, 10, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
$hButton_3 = GUICtrlCreateButton("Run Ninja and Rithmic", 40, 50, 125, 30)
GUICtrlSetOnEvent($hButton_3, "_Func_3")



GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


While 1
sleep(10)
WEnd



#cs
Function for start button
Get data compare prices and bid or offer
#ce

Func _Func_1() ;FUNCTION START


; Make sure the flag is cleared
$fInterrupt = 0
$runner = Not $runner


While $runner
GUISetBkColor (0x33ff33)



Local $getfile = 0
While $getfile = 0

;Interrupt switch
If $fInterrupt <> 0 Then

; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("SCRIPT HALTED" & @CRLF)
EndSwitch
Return
EndIf
;Interrupt switch

If FileExists("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt") Then
$file = FileOpen("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt", 0)
$All = FileReadLine($file, -1)
$Upper = StringMid($All, 7, 6)
$Lower = StringMid($All, 20, 6)
$Last = StringMid($All, 33, 6)
$getfile = 1


Else
ConsoleWrite("No File" & @CRLF)
$getfile = 0
sleep(1250)
EndIf
WEnd






FileClose($file)
FileDelete("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt")
$getfile = 0

If $upper > 0 and $Last >= $Upper - 0.0035 then
ConsoleWrite("SELL @ " & $Upper & @CR)

ControlSend("Place Order", "", "[CLASS:Edit; INSTANCE:1]", "5g") ; Set Account

ControlSend("Place Order", "", "[NAME:PRI_oCbExchange]", "CME") ; Set Exchange

ControlSend( "Place Order" , "", "[NAME:PRI_oCbTxnType]", "Sell") ; Set Buy or Sell

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:9]", "1") ; Set Quantity

ControlSetText("Place Order", "", "[CLASS:Edit; INSTANCE:2]", "M6EZ2") ; Set Symbol

ControlSend ( "Place Order", "", "[NAME:PRI_oCbOrderType]", "Limit") ; Set Limit

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:11]", $Upper) ; Sell Price


ControlFocus("Place Order", "", "[NAME:PRI_oBnPlaceOrder]")
ControlClick("Place Order", "", "[NAME:PRI_oBnPlaceOrder]", "left")
Sleep(5000)

ElseIf $lower > 0 and $Last <= $Lower + 0.0035 Then

ConsoleWrite("BUY @ " & $Lower & @CR)

ControlSend("Place Order", "", "[CLASS:Edit; INSTANCE:1]", "g") ; Set Account

ControlSend("Place Order", "", "[NAME:PRI_oCbExchange]", "CME") ; Set Exchange

ControlSend( "Place Order" , "", "[NAME:PRI_oCbTxnType]", "Buy") ; Set Buy or Sell

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:9]", "1") ; Set Quantity

ControlSetText("Place Order", "", "[CLASS:Edit; INSTANCE:2]", "M6EZ2") ; Set Symbol

ControlSend ( "Place Order", "", "[NAME:PRI_oCbOrderType]", "Limit") ; Set Limit
sleep(100)
ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:11]", $Lower) ; Sell Price
sleep(100)

ControlFocus("Place Order", "", "[NAME:PRI_oBnPlaceOrder]")
ControlClick("Place Order", "", "[NAME:PRI_oBnPlaceOrder]", "left")
Sleep(500)

EndIf
WEnd


EndFunc

Func _Func_2()
GUISetBkColor (0xff3300)


Sleep(100)

EndFunc

Func _Func_3()
;Check For Ninjatrader
$ProcessNinja = "Ninjatrader.exe"

If ProcessExists($ProcessNinja) Then

Else
run("C:Program Files (x86)NinjaTrader 7bin64NinjaTrader.exe")
sleep(10000)
EndIf


$ProcessRithmic = "Rithmic Trader.exe"


If ProcessExists($ProcessRithmic) Then

WinActivate("Rithmic Trader[Zen-Fire Internet] - Patel, Viraj (v) - Trader","Zen-Fire Internet : ")
MouseClick("left",81,37,1)
MouseClick("left",88,59,1)
sleep(250)
ControlClick("Place Order", "", "[NAME:PRI_oChkCloseAfter]", "left")


Else
run("C:Program Files (x86)RithmicRithmic TraderRithmic Trader.exe")
winwait(" Rithmic Trader 10.43.2.0")
ControlSetText(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtUserId]", "")
ControlSetText(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtPassword]", "")
sleep(25)
ControlSend(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtPassword]", "{Enter}")

WinWait("Rithmic Trader[Zen-Fire Internet] - Patel, Viraj (v) - Trader","Zen-Fire Internet : ")
mouseclick("left",81,37,1)
mouseClick("left",88,59,1)
sleep(500)

ControlFocus("Place Order", "", "[NAME:PRI_oChkCloseAfter]")
ControlClick("Place Order", "", "[NAME:PRI_oChkCloseAfter]", "left")


EndIf


EndFunc

Func _Exit()
Exit
EndFunc




Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)

; The Func 2 button was pressed so set the flag
If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1


Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND
Edited by VirajPatel

Share this post


Link to post
Share on other sites

Sorry Ignor Post #3 script doesnt run,

This shoudl work:

local $Upper, $Lower, $Last ;Calcualtion variables
Local $file, $All ;File and last string variable
Local $getfile ;local loop variable
global $runner ;global loop variable


#include 
#include 


; Declare a flag
$fInterrupt = 0

Opt("GUIOnEventMode", 1)
Opt('MouseCoordMode', 0)


$hGUI = GUICreate("Trading Script", 200, 100)
GUISetBkColor (0xff3300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$hButton_1 = GUICtrlCreateButton("Start", 15, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Stop", 100, 10, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
$hButton_3 = GUICtrlCreateButton("Run Ninja and Rithmic", 40, 50, 125, 30)
GUICtrlSetOnEvent($hButton_3, "_Func_3")



GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


While 1
sleep(10)
WEnd



#cs
Function for start button
Get data compare prices and bid or offer
#ce

Func _Func_1() ;FUNCTION START


; Make sure the flag is cleared
$fInterrupt = 0
$runner = Not $runner


While $runner
GUISetBkColor (0x33ff33)
;Interrupt switch
If $fInterrupt <> 0 Then

; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("SCRIPT HALTED" & @CRLF)
EndSwitch
Return
EndIf
;Interrupt switch



Local $getfile = 0
While $getfile = 0

;Interrupt switch
If $fInterrupt <> 0 Then

; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("SCRIPT HALTED" & @CRLF)
EndSwitch
Return
EndIf
;Interrupt switch

If FileExists("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt") Then
$file = FileOpen("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt", 0)
$All = FileReadLine($file, -1)
$Upper = StringMid($All, 7, 6)
$Lower = StringMid($All, 20, 6)
$Last = StringMid($All, 33, 6)
$getfile = 1


Else
ConsoleWrite("No File" & @CRLF)
$getfile = 0
sleep(1250)
EndIf
WEnd






FileClose($file)
FileDelete("C:UsersFloydDocumentsNinjaTrader 7BBPrinter.txt")
$getfile = 0

If $upper > 0 and $Last >= $Upper - 0.0050 then
ConsoleWrite("SELL @ " & $Upper & @CR)

ControlSend("Place Order", "", "[CLASS:Edit; INSTANCE:1]", "") ; Set Account

ControlSend("Place Order", "", "[NAME:PRI_oCbExchange]", "CME") ; Set Exchange

ControlSend( "Place Order" , "", "[NAME:PRI_oCbTxnType]", "Sell") ; Set Buy or Sell

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:9]", "1") ; Set Quantity

ControlSetText("Place Order", "", "[CLASS:Edit; INSTANCE:2]", "M6EZ2") ; Set Symbol

ControlSend ( "Place Order", "", "[NAME:PRI_oCbOrderType]", "Limit") ; Set Limit

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:11]", $Upper) ; Sell Price


ControlFocus("Place Order", "", "[NAME:PRI_oBnPlaceOrder]")
ControlClick("Place Order", "", "[NAME:PRI_oBnPlaceOrder]", "left")
Sleep(5000)

ElseIf $lower > 0 and $Last <= $Lower + 0.0025 Then

ConsoleWrite("BUY @ " & $Lower & @CR)

ControlSend("Place Order", "", "[CLASS:Edit; INSTANCE:1]", "") ; Set Account

ControlSend("Place Order", "", "[NAME:PRI_oCbExchange]", "CME") ; Set Exchange

ControlSend( "Place Order" , "", "[NAME:PRI_oCbTxnType]", "Buy") ; Set Buy or Sell

ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:9]", "1") ; Set Quantity

ControlSetText("Place Order", "", "[CLASS:Edit; INSTANCE:2]", "M6EZ2") ; Set Symbol

ControlSend ( "Place Order", "", "[NAME:PRI_oCbOrderType]", "Limit") ; Set Limit
sleep(100)
ControlSetText("Place Order", "", "[CLASS:WindowsForms10.EDIT.app.0.3ce0bb8; INSTANCE:11]", $Lower) ; Sell Price
sleep(100)

ControlFocus("Place Order", "", "[NAME:PRI_oBnPlaceOrder]")
ControlClick("Place Order", "", "[NAME:PRI_oBnPlaceOrder]", "left")
Sleep(500)

EndIf
WEnd


EndFunc

Func _Func_2()
GUISetBkColor (0xff3300)


Sleep(100)

EndFunc

Func _Func_3()
;Check For Ninjatrader
$ProcessNinja = "Ninjatrader.exe"

If ProcessExists($ProcessNinja) Then

Else
run("C:Program Files (x86)NinjaTrader 7bin64NinjaTrader.exe")
sleep(10000)
EndIf


$ProcessRithmic = "Rithmic Trader.exe"


If ProcessExists($ProcessRithmic) Then

WinActivate("Rithmic Trader[Zen-Fire Internet] - Patel, Viraj (v) - Trader","Zen-Fire Internet : ")
MouseClick("left",81,37,1)
MouseClick("left",88,59,1)
sleep(250)
ControlClick("Place Order", "", "[NAME:PRI_oChkCloseAfter]", "left")


Else
run("C:Program Files (x86)RithmicRithmic TraderRithmic Trader.exe")
winwait(" Rithmic Trader 10.43.2.0")
ControlSetText(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtUserId]", "")
ControlSetText(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtPassword]", "")
sleep(25)
ControlSend(" Rithmic Trader 10.43.2.0", "", "[NAME:PRI_oTxtPassword]", "{Enter}")

WinWait("Rithmic Trader[Zen-Fire Internet] - Patel, Viraj (v) - Trader","Zen-Fire Internet : ")
mouseclick("left",81,37,1)
mouseClick("left",88,59,1)
sleep(500)

ControlFocus("Place Order", "", "[NAME:PRI_oChkCloseAfter]")
ControlClick("Place Order", "", "[NAME:PRI_oChkCloseAfter]", "left")


EndIf


EndFunc

Func _Exit()
Exit
EndFunc




Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)

; The Func 2 button was pressed so set the flag
If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1


Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hey I found the answer. I just added this to the second function:

$runner = Not $runner

works fine now.

Edited by VirajPatel

Share this post


Link to post
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
Sign in to follow this  
Followers 0