untoha

stock_watcher.au3

Feb 23rd, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.59 KB | None | 0 0
  1. ;default icons here %SystemRoot%\system32\SHELL32.dll
  2.  
  3. #include <EditConstants.au3>
  4. #include <GUIConstantsEx.au3>
  5. #include <StaticConstants.au3>
  6. #include <WindowsConstants.au3>
  7. #include <Inet.au3>
  8. #include <JSON.au3>
  9. #include <File.au3>
  10. #include <FontConstants.au3>
  11. #include <GUIConstantsEx.au3>
  12.  
  13.  
  14. #Region ### START Koda GUI section ### Form=
  15. Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
  16. Opt("TrayIconHide", 0)
  17.  
  18. $stock_watcher = GUICreate("stock_watcher", 97, 93, 473, 309, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE,$WS_EX_TOOLWINDOW))
  19. ;GUICtrlSetFont(-1, 9, $FW_NORMAL, $GUI_FONTUNDER, "Comic Sans Ms 2pt") ;
  20. ;GUISetFont(8,  $FW_NORMAL, $GUI_FONTUNDER, "Comic Sans Ms")
  21. $Total = GUICtrlCreateLabel("Result", 1, 0, 41, 17)
  22. GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  23. $total_val = GUICtrlCreateLabel("0", 48, 0, 42, 17)
  24. GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  25. $amont = GUICtrlCreateInput("amount", 0, 32, 41, 21)
  26. GUICtrlSetTip( -1, "amount")
  27. GUICtrlSetOnEvent(-1, "setamount")
  28. $prce = GUICtrlCreateInput("price", 48, 32, 49, 21)    ;buy price
  29. GUICtrlSetTip( -1, "price")
  30. GUICtrlSetOnEvent(-1, "setprice")
  31. $last_deal = GUICtrlCreateLabel("last deal", 2, 15, 43, 17)
  32. $last_deal_val = GUICtrlCreateLabel("0", 48, 15, 42, 17)
  33. $long = GUICtrlCreateRadio("long", 0, 54, 41, 17)
  34. GUICtrlSetOnEvent(-1, "setlong")
  35. $short = GUICtrlCreateRadio("short", 48, 54, 49, 17)
  36. GUICtrlSetOnEvent(-1, "setshort")
  37. $stp = GUICtrlCreateInput("0.01", 0, 72, 41, 21)
  38. GUICtrlSetTip( -1, "step")
  39. $stp_price = GUICtrlCreateInput("5.7", 48, 72, 49, 21)
  40. GUICtrlSetTip( -1, "step price")
  41. GUICtrlSetOnEvent(-1, "setstepprice")
  42. GUISetState(@SW_SHOW)
  43.  
  44. GUISetOnEvent($GUI_EVENT_CLOSE, "stop")
  45. GUIRegisterMsg($WM_COMMAND, "ED_WM_COMMAND")
  46. ControlFocus($stock_watcher, "", $Total)
  47. ;HotKeySet("{ESC}", "nullfunc")
  48.  
  49. #EndRegion ### END Koda GUI section ###
  50.  
  51. global $dfile = "data";
  52. loaddata();
  53. Global $iter = 100;
  54. Global $lastdeal = 0;
  55. global $step_price = ControlGetText($stock_watcher, "", $stp_price)
  56. global $amount = ControlGetText($stock_watcher, "", $amont)
  57. global $price = ControlGetText($stock_watcher, "", $prce)
  58. global $way = 0;
  59. If GUICtrlRead($long) = 1 Then
  60.    $way = 1
  61. ElseIf GUICtrlRead($short) = 1 Then
  62.    $way = -1
  63. EndIf
  64. global $step = 0.01
  65. global $step_price = 5.7
  66.  
  67. While 1
  68.     $nMsg = GUIGetMsg()
  69.     Switch $nMsg
  70.         Case $GUI_EVENT_CLOSE
  71.             Exit
  72.  
  73.         Case $amont
  74.         Case $price
  75.    EndSwitch
  76.    Sleep(1000)
  77.    if $iter < 9 Then
  78.       $iter = $iter + 1
  79.    Else
  80.       $iter = 0
  81.       update()
  82.    EndIf
  83. WEnd
  84.  
  85. Func ED_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
  86.     #forceref $hWnd, $iMsg
  87.     Local $iCode = BitShift($wParam, 16)
  88.     Switch $lParam
  89.          Case GUICtrlGetHandle($prce)
  90.             Switch $iCode
  91.                 Case $EN_SETFOCUS
  92.                      if ControlGetText($stock_watcher, "", $prce) = "price" then
  93.                         ControlSetText($stock_watcher, "", $prce, "")
  94.                      EndIf
  95.                 Case $EN_KILLFOCUS
  96.                     ;GUICtrlSetData($hLabel, "Edit does not have focus")
  97.                  EndSwitch
  98.          Case GUICtrlGetHandle($amont)
  99.             Switch $iCode
  100.                 Case $EN_SETFOCUS
  101.                      if ControlGetText($stock_watcher, "", $amont) = "amount" then
  102.                         ControlSetText($stock_watcher, "", $amont, "")
  103.                      EndIf
  104.                 Case $EN_KILLFOCUS
  105.                     ;GUICtrlSetData($hLabel, "Edit does not have focus")
  106.          EndSwitch
  107.     EndSwitch
  108.  
  109.     Return $GUI_RUNDEFMSG
  110. EndFunc  ;==>ED_WM_COMMAND
  111.  
  112. Func setamount()
  113.    $amount = ControlGetText($stock_watcher, "", $amont)
  114.    ControlFocus($stock_watcher, "", $Total)
  115.    savedata("amount",$amount);
  116. EndFunc
  117.  
  118. Func setprice()
  119.    $price = ControlGetText($stock_watcher, "", $prce)
  120.    ControlFocus($stock_watcher, "", $Total)
  121.    savedata("price",$price);
  122. EndFunc
  123.  
  124. Func setlong()
  125.    $way = 1
  126.    savedata("way","1");
  127. EndFunc
  128.  
  129. Func setshort()
  130.    $way = -1
  131.    savedata("way","-1");
  132. EndFunc
  133.  
  134. Func setstepprice()
  135.    $step_price = ControlGetText($stock_watcher, "", $stp_price)
  136. EndFunc
  137.  
  138. Func stop()
  139.    exit
  140. EndFunc
  141.  
  142. Func nullfunc()
  143. EndFunc
  144.  
  145. Func update()
  146.    if $way = 0 Then
  147.       return;
  148.    EndIf
  149.    local $d = _INetGetSource('http://www.cmegroup.com/CmeWS/mvc/Quotes/FutureContracts/XNYM/G?quoteCodes=clk8') ;NYMEX:CLK2018
  150.    Local $objJson = json_Decode($d)
  151.    Local $lastdeal_new = json_Get($objJson, '["quotes"][0]["last"]');.Keys[0]
  152.    if $lastdeal <> $lastdeal_new Then
  153.       $lastdeal = $lastdeal_new
  154.       ControlSetText("stock_watcher", "", $last_deal_val, $lastdeal)
  155.    EndIf
  156.    local $res
  157.    if $way = 1 Then
  158.       $res = ((($lastdeal - $price) / $step) * $step_price) * $amount
  159.    ElseIf $way = -1 Then
  160.       $res = ((($price - $lastdeal) / $step) * $step_price) * $amount
  161.    EndIf
  162.    ControlSetText("stock_watcher", "", $total_val, $res)
  163. EndFunc
  164.  
  165. Func savedata($line,$nd)
  166.    Local $aRetArray, $d
  167.    _FileReadToArray($dfile, $aRetArray)
  168.    for $i=1 to $aRetArray[0]
  169.       $d = StringSplit($aRetArray[$i], ";")
  170.       if $d[1]=$line Then
  171.          $aRetArray[$i] = $d[1]&";"&$nd
  172.          ConsoleWrite($aRetArray[$i]&@CRLF);
  173.       EndIf
  174.    Next
  175.    _FileWriteFromArray($dfile, $aRetArray, $FRTA_COUNT , Default, @CRLF)
  176. EndFunc
  177.  
  178. Func loaddata()
  179.    Local $aRetArray
  180.    _FileReadToArray($dfile, $aRetArray)
  181.    ;_ArrayDisplay($aRetArray, "1D array - count", Default, 8)
  182.    local $d = StringSplit($aRetArray[1], ";")
  183.    ControlSetText($stock_watcher, $d[2], $amont, $d[2])
  184.    $d = StringSplit($aRetArray[2], ";")
  185.    ControlSetText($stock_watcher, $d[2], $prce, $d[2])
  186.    $d = StringSplit($aRetArray[3], ";")
  187.    if $d[2]="1" then
  188.       GUICtrlSetState ($long, $GUI_CHECKED)
  189.    ElseIf $d[2]="-1" Then
  190.       GUICtrlSetState ($short, $GUI_CHECKED)
  191.    EndIf
  192. EndFunc
Add Comment
Please, Sign In to add comment