Vill lägga till en parameter i ett Samurize-script

Permalänk

Vill lägga till en parameter i ett Samurize-script

Jag använder mig av Samurize Yahoo stock-script för att kunna se aktiekurser på skrivbordet. Funkar bra. Saknar dock att kunna se min vinst/förlust om jag skulle sälja aktien just nu. För att kunna få detta värde så behövs ytteligare en parameter - där jag skriver in hur mycket pengar jag investerat i aktien.

Ekvationen borde alltså se ut såhär: A=(B*C)-D
A = Min vinst/förlust för ögonblicket (Det är alltså detta värdet jag är ute efter)
B = Antalet aktier jag äger (D/C)
C = Aktiekursen (Detta värdet hämtas redan automatiskt från finance.yahoo.com)
D = Summan jag investerat i aktien (Detta är det enda värdet man ska behöva ange)

Eller för att göra det ännu enklare: A=((D/C)*C)-D

Nån som vet hur jag lägger in detta i scriptet?

Skulle också vilja flytta decimalen i aktiekursvärdet (C) två steg. Så att istället för t.ex 79,00 ska där stå 0,79. Vill ju se värdet i kronor - inte öre.

Permalänk
Hedersmedlem

*Tråd flyttad*

Visa signatur
Permalänk

Här är koden till scriptet ifråga:

vbs content: (about 600 lines)

'============================================================================================= ' Program Description '============================================================================================= ' ' YahooStockCsv.vbs. ' ' This script uses Yahoo's CSV option/format to grab data (as discussed in a Samurize forum). ' Information is buffered to the local hard disk shortly after the stock market closes. At ' this time, the script will stock sending requests to Yahoo and use the local copy. ' ' To use, just input the stock symbol you wish to get as the parameter. ' ' Some popular Stock Symbols: ' Yahoo: yhoo ' Microsoft: msft ' ' Some popular Stock Indexes: ' DOW: ^dji ' NAS: ^ixic ' S&P: ^spb ' ' Valid 'symbolTypes' for "stockChangeSymbol" are: Arrow, Filled, Outlined, Smiley, Text ' '============================================================================================= ' DISCLAIMER '============================================================================================= ' ' THE AUTHOR(S) MAKE AND LICENSEE RECEIVES NO WARRANTY EXPRESS OR IMPLIED AND THERE ARE ' EXPRESSLY EXCLUDED ALL WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ' PURPOSE. THE AUTHOR(S) SHALL HAVE NO LIABILITY FOR CONSEQUENTIAL, EXEMPLARY, OR INCIDENTAL ' DAMAGES EVEN IF IT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. ' '============================================================================================= ' Version Information '============================================================================================= ' ' 1/22/2004 - 1.00. Program by Charlie Kong. Started out as a hack of NeM's YahooStock.vbs ' which kind of got away from me while I was learning from it. This is the first ' VBscript I've cobbled together. Be nice. ' '============================================================================================= ' Global variables '============================================================================================= ' ' use Market Open Flag. This flag controls how the script gets it's data. It's really my ' attempt to be a better NetCitizen and not hammer Yahoo with requests 24 hours a day (which ' can add up depending on how many people use this script or its like). The value can be set ' to zero for testing or troubleshooting purposes as well. Note: The MarketOpenCheck() ' function is unaffected by this setting and should report a correct value always. ' ' 0 = always get information from web ' 1 = check if Market is open; grab data from buffer if Market has been closed > 30 minutes; ' if data is unavailable from buffer, then grab from web useMarketOpenFlag = 1 ' Time Zone Flag and Value. Set this in case drag your machine/laptop to different time ' zones and don't want the script to automatically set it. ' ' TimeZoneFlag = -1 : determine automatically ' TimeZoneFlag = 0 : use TimeZoneValue TimeZoneFlag = -1 TimeZoneValue = +1 ' Daylight Saving Time Flag. DST begins the first Sunday of April at 2am and ends the last ' Sunday of October at 2am. It can be manually set in case you move your computer/laptop to ' a different time zone and don't want the script to automatically determine it. ' ' -1 = determine automatically ' 0 = always false ' 1 = always true DSTFlag = -1 ' dataFileName. Name of the stock's data file that is saved to disk for the times that the ' Market is closed. If left blank, this global variable will be set by a call to the ' "dataFileNameInit" function. If not blank, then the Init function will leave the value ' untouched. This is useful for troubleshooting purposes. dataFileName = "" '============================================================================================= ' Public Functions '============================================================================================= ' Generic stock report. Desired aspects of a stock can be accessed here and formated ' accordingly. Unused variables are kept for reference and future use. Function reportBrief1(stockSymbol) ' Get the stock elements sLast = getStockElement(stockSymbol, 1) sChange = getStockElement(stockSymbol, 4) ' Format the report elements sLast = right(Space(10) & FormatNumber(sLast), 10) sChange = right(Space( 9) & sChange, 9) ' Concantinate the report string reportBrief1 = sLast & sChange End Function '--------------------------------------------------------------------------------------------- ' Another Generic stock report. Includes Date & Time. Function reportBrief2(stockSymbol) ' Get the stock elements sLast = getStockElement(stockSymbol, 1) sChange = getStockElement(stockSymbol, 4) sDate = getStockElement(stockSymbol, 2) sTime = getStockElement(stockSymbol, 3) ' Format the report elements sLast = right(Space(10) & FormatNumber(sLast), 10) sChange = right(Space( 9) & sChange, 9) sDate = right(Space(12) & sDate, 12) sTime = right(Space( 9) & LCase(sTime), 9) ' Concantinate the report string reportBrief2 = sLast & sChange & sDate & sTime End Function '--------------------------------------------------------------------------------------------- Function reportFullH(stockSymbol) ' Get the stock elements sSymbol = getStockElement(stockSymbol, 0) sLast = getStockElement(stockSymbol, 1) sDate = getStockElement(stockSymbol, 2) sTime = getStockElement(stockSymbol, 3) sChange = getStockElement(stockSymbol, 4) sOpen = getStockElement(stockSymbol, 5) sHigh = getStockElement(stockSymbol, 6) sLow = getStockElement(stockSymbol, 7) sVolume = getStockElement(stockSymbol, 8) ' Format the report elements sSymbol = left(sSymbol & Space(6), 6) sDate = sDate & " " sTime = right(Space( 8) & LCase(sTime), 8) sLast = right(Space(11) & FormatNumber(sLast), 11) sChange = right(Space( 9) & sChange, 9) sOpen = right(Space(11) & FormatNumber(sOpen), 11) sHigh = right(Space(11) & FormatNumber(sHigh), 11) sLow = right(Space(11) & FormatNumber(sLow), 11) sVolume = right(Space(13) & FormatNumber(sVolume, 0), 13) ' Concantinate the report string reportFullH =sSymbol & sDate & sTime & sLast & sChange & sOpen & sHigh & sLow & sVolume End Function '--------------------------------------------------------------------------------------------- Function reportFullV(stockSymbol) ' Get the stock elements sSymbol = getStockElement(stockSymbol, 0) sLast = getStockElement(stockSymbol, 1) sDate = getStockElement(stockSymbol, 2) sTime = getStockElement(stockSymbol, 3) sChange = getStockElement(stockSymbol, 4) sOpen = getStockElement(stockSymbol, 5) sHigh = getStockElement(stockSymbol, 6) sLow = getStockElement(stockSymbol, 7) sVolume = getStockElement(stockSymbol, 8) ' Format the report elements sSymbol = "Symbol " & right(Space(12) & sSymbol , 12) & vbCrLf sDate = "Date " & right(Space(12) & sDate , 12) & vbCrLf sTime = "Time " & right(Space(12) & LCase(sTime) , 12) & vbCrLf sOpen = "Open " & right(Space(12) & FormatNumber(sOpen) , 12) & vbCrLf sHigh = "High " & right(Space(12) & FormatNumber(sHigh) , 12) & vbCrLf sLow = "Low " & right(Space(12) & FormatNumber(sLow) , 12) & vbCrLf sLast = "Last " & right(Space(12) & FormatNumber(sLast) , 12) & vbCrLf sChange = "Change " & right(Space(12) & sChange , 12) & vbCrLf sVolume = "Volume " & right(Space(12) & FormatNumber(sVolume, 0), 12) ' Concantinate the report string reportFullV = sSymbol & sDate & sTime & sOpen & sHigh & sLow & sLast & sChange & sVolume End Function '--------------------------------------------------------------------------------------------- Function stockChange(stockSymbol) stockChange = FormatNumber(getStockElement(stockSymbol, 4)) End Function '--------------------------------------------------------------------------------------------- ' This function displays a single character to indicate the stock's change. Set the font as ' noted. For a fancier effect, set the color=green, check the "Enable Alert Value" box, set ' alert when "Value equals" to "i", "q", "s", or as appropriate with color=red. Function stockChangeSymbol(stockSymbol, symbolType) ' Get the symbols select case LCase(symbolType) case "arrow" symbolStr = "h i" ' Font = WingDings3 case "filled" symbolStr = "p q" ' Font = WingDings3 case "outlined" symbolStr = "r s" ' Font = WingDings3 case "smiley" symbolStr = "JKL" ' Font = WingDings case "text" symbolStr = "^ v" ' Font = Courier New case else symbolStr = "" end select ' Read in the stock price change stockChangeStr = getStockElement(stockSymbol, 4) ' Get the appropriate symbol result = "?" if stockChangeStr <> "" and symbolStr <> "" then plusMinus = mid(stockChangeStr, 1, 1) result = mid(symbolStr, 1, 2) ' No change if plusMinus = "+" then result = mid(symbolStr, 1, 1) ' Up symbol if plusMinus = "-" then result = mid(symbolStr, 3, 1) ' Down symbol else ' Instructions result = "Valid symbolTypes are:" & vbCrLf result = result & " Arrow (font=WingDings3)" & vbCrLf result = result & " Filled (font=WingDings3)" & vbCrLf result = result & " Outlined (font=WingDings3)" & vbCrLf result = result & " Smiley (font=WingDings)" & vbCrLf result = result & " Text (font=Courier New)" & vbCrLf end if stockChangeSymbol = result End Function '--------------------------------------------------------------------------------------------- Function stockDate(stockSymbol) stockDate = getStockElement(stockSymbol, 2) End Function '--------------------------------------------------------------------------------------------- Function stockHigh(stockSymbol) stockHigh = FormatNumber(getStockElement(stockSymbol, 6)) End Function '--------------------------------------------------------------------------------------------- Function stockLast(stockSymbol) stockLast = FormatNumber(getStockElement(stockSymbol, 1)) End Function '--------------------------------------------------------------------------------------------- Function stockLow(stockSymbol) stockLow = FormatNumber(getStockElement(stockSymbol, 7)) End Function '--------------------------------------------------------------------------------------------- Function stockMarketStatus() mktStatus = funcMarketStatus() if mktStatus <> "Open" then mktStatus = "Closed" mktStatus = lcase(mktStatus) stockMarketStatus = mktStatus End Function '--------------------------------------------------------------------------------------------- Function stockOpen(stockSymbol) stockOpen = getStockElement(stockSymbol, 5) End Function '--------------------------------------------------------------------------------------------- Function stockTime(stockSymbol) stockTime = getStockElement(stockSymbol, 3) End Function '--------------------------------------------------------------------------------------------- Function stockVolume(stockSymbol) stockVolume = FormatNumber(getStockElement(stockSymbol, 8), 0) End Function '============================================================================================= ' Private Functions '============================================================================================= ' Get the Stock 'element' ' ' Element values: Symbol = 0, LastPrice = 1, Date = 2, Time = 3, PriceChange = 4 ' OpenningPrice = 5, HighPrice = 6, LowPrice = 7, Volume = 8 Private Function getStockElement(stockSymbol, elementNum) stockCsv = getStockInfo(stockSymbol) stockValue = ucase( split(stockCsv , ",")(elementNum) ) stockValue = replace(stockValue, chr(34), "") getStockElement = stockValue End Function '--------------------------------------------------------------------------------------------- ' This function either reads the from disk or from Yahoo via its "CSV" interface. Set font to ' a fixed width (like Courier New) so everything lines up right ' ' Sample query from Yahoo looks like this: ' "^DJI",10425.04,"12/30/2003","4:03pm",-24.96,10449.70,10456.07,10405.85,132795232 ' ' Sample output from this Function looks like this: ' "^DJI","10425.04","12/30/2003","4:03pm","-24.96","10449.70","10456.07","10405.85","132795232" Private Function getStockInfo(stockSymbol) ' Init variables dim result, queryWebFlag queryWebFlag = 0 ' No stockSymbol given so give back empty information if stockSymbol = "" then result = "'need stock symbol','0.00','N/A','N/A','N/A','N/A','N/A','N/A','N/A'" result = replace(result, "'", chr(34)) queryWebFlag = -1 end if ' Always get data from Web if flag is not set if queryWebFlag = 0 then if useMarketOpenFlag = 0 then queryWebFlag = 1 end if ' Get data from Web if Market is open if queryWebFlag = 0 then if funcMarketStatus() = "Open" then queryWebFlag = 2 end if ' Keep querying up to 30 minutes after the Market closes (based on file date/time stamp) if queryWebFlag = 0 then fileDate = getFileDate(stockSymbol) ClosedTime = OpenCloseTime( estTime(fileDate) ) if ClosedTime <= (30 * 60) then queryWebFlag = 3 end if ' Read data from buffer if queryWebFlag = 0 then result = readBuf(stockSymbol) ' Check for a result if result = "" then queryWebFlag = 4 ' Querying when Market is open (based on Stock's date/time stamp) else dim csvArray csvArray = split(result, ",") sDate = replace(csvArray(2), chr(34), "") sTime = replace(csvArray(3), chr(34), "") ClosedTime = OpenCloseTime(CDate(sDate & " " & sTime)) if ClosedTime = 0 then queryWebFlag = 5 end if end if ' Read data from Web if queryWebFlag > 0 then ' All else failed...read quote directly from Yahoo if left(stockSymbol, 1) = "^" then stockSymbol = "%5e"& mid(stockSymbol, 2) stockURL = "http://finance.yahoo.com/d/quotes.csv?s=" & stockSymbol & "&f=sl1d1t1c1ohgv" result = returnHTML(stockURL) ' Remove any blanks for CR/LFs from the string result = trim(result) result = replace(result, vbCrLf, "") result = replace(result, vbLf, "") ' Enclose everything with quotes to avoid losing data/formating result = replace(result, chr(34), "") result = replace(result, ",", chr(34) & "," & chr(34)) result = chr(34) & result & chr(34) ' Write data to disk if necessary if useMarketOpenFlag = 1 and queryWebFlag <> 2 then writeBuf(result) end if getStockInfo = result End Function '--------------------------------------------------------------------------------------------- ' This function takes a URL and returns the result ' Function copied from YahooStock.vbs by NeM Private Function returnHTML(sURL) dim objXMLHTTP,HTML Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP") objXMLHTTP.Open "GET", sURL, False objXMLHTTP.Send HTML = objXMLHTTP.responseBody set objRS = CreateObject("ADODB.Recordset") objRS.Fields.Append "txt", 200, 45000, &H00000080 objRS.Open objRS.AddNew objRS.Fields("txt").AppendChunk HTML returnHTML = objRS("txt").Value objRS.Close set objRS = Nothing set objXMLHTTP = Nothing End Function '--------------------------------------------------------------------------------------------- ' This function determines if the Market is open Private Function funcMarketStatus() ' Init variables statusTemp = "Open" ' Scheduled open/close closedTime = openCloseTime(estTime(Now)) if closedTime <> 0 then statusTemp = "Closed" ' Holidays (some of them anyways) dateTemp = estTime(Date) if dateTemp = estTime( CDate( "1/1/" & Cstr(Year(Date)))) then statusTemp = "Holiday" ' New Years if dateTemp = estTime( CDate( "7/4/" & Cstr(Year(Date)))) then statusTemp = "Holiday" ' Independance Day if dateTemp = estTime( CDate("12/25/" & Cstr(Year(Date)))) then statusTemp = "Holiday" ' Christmas ' Weekends dateTemp = estTime(Now) if Weekday(dateTemp) = 1 then statusTemp = "Weekend" if Weekday(dateTemp) = 7 then statusTemp = "Weekend" ' Return result funcMarketStatus = statusTemp End Function '--------------------------------------------------------------------------------------------- ' Determine in how many seconds the Market will either Open or Close ' Note: "dateTimeIn" has to be adjusted to EST (MarketTime) Private Function openCloseTime(dateTimeIn) DST = isDST(dateTimeIn) MarketStartDiff = DateDiff("s", CDate(CStr(Date) & " " & "8:00:00 am"), dateTimeIn) MarketEndDiff = DateDiff("s", CDate(CStr(Date) & " " & "3:59:59 pm"), dateTimeIn) result = 0 ' Zero means Market is currently open if MarketStartDiff < 0 then result = MarketStartDiff ' Negative means Market hasn't opened yet if MarketEndDiff > 0 then result = MarketEndDiff ' Positive means Market closed openCloseTime = result End Function '--------------------------------------------------------------------------------------------- ' Convert given time to Easter Standard Time Private Function estTime(dateTimeIn) ' Set the TimeZone offset if TimeZoneFlag = 0 then ' Force to given value tzoneOffset = TimeZoneValue else ' Determine automatically set shell = createobject("wscript.shell") strValueName = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias" tzoneOffset = shell.regread(strValueName) set shell = Nothing end if ' Using DST and offset, calc EST DST = 1 * isDST(dateTimeIn) ' Daylight Saving Time 0=false 1=true dateTemp = DateAdd("n", tzoneOffset, dateTimeIn) ' Change current time to GMT dateTemp = DateAdd("h", -5 + DST , dateTemp) ' Change GMT time to EST estTime = dateTemp End Function '--------------------------------------------------------------------------------------------- ' This function determine if the testDate is within Daylight Saving Time ' Code modified from original function by Paul R. Sadowski Private Function isDST(testDate) ' Set the DSTFlag result = DSTFlag ' Determine automatically if DSTFlag = -1 and IsDate(testDate) = True then Dim StartDate, EndDate, StartDOW, EndDOW, TargetDOW Dim BeginDST, EndDST ' Find the beginning of DST (first Sunday in April) StartDate = CDate("4/1/" & Cstr(Year(testDate))) StartDOW = Weekday(StartDate) if StartDOW <> 1 then TargetDOW = 8 - StartDOW else TargetDOW = 0 end if BeginDST = DateAdd("d", TargetDOW, StartDate) BeginDST = BeginDST & " 2:00:00 am" ' Find the end of DST (last Sunday in October) EndDate = CDate("10/31/" & Cstr(Year(testDate))) EndDOW = Weekday(EndDate) if EndDOW <> 1 then TargetDOW = 1 - EndDOW else TargetDOW = 0 end if EndDST = DateAdd("d", TargetDOW, EndDate) EndDST = EndDST & " 2:00:00 am" ' Set the DST Flag/result if DateDiff("d", BeginDST, testDate) >= 0 and DateDiff("d", EndDST, testDate) < 0 then result = 1 else result = 0 end if end if ' Return the result isDST = result End Function '--------------------------------------------------------------------------------------------- ' Reads the CSV encoded stock from the disk drive Private Function readBuf(stockSymbol) ' Init dataFileName if not done already if len(dataFileName) = 0 and len(stockSymbol) <> 0 then dataFileNameInit(stockSymbol) dim contents set fso = CreateObject ("Scripting.FileSystemObject") if fso.FileExists(dataFileName) then set filePath = fso.GetFile(dataFileName) set tso = filePath.OpenAsTextStream (1, -2) contents = tso.readall tso.close set tso = nothing set filePath = nothing else contents = "" end if set fso = nothing readBuf = contents End Function '--------------------------------------------------------------------------------------------- ' Writes the given CSV string to Samurise's Script directory. Private Function writeBuf(stockCsv) ' Init dataFileName if not done already stockSymbol = ucase( split(stockCsv , ",")(0) ) stockSymbol = replace(stockSymbol, chr(34), "") if len(dataFileName) = 0 and len(stockSymbol) <> 0 then dataFileNameInit(stockSymbol) ' Init variables dim fso ' FileSystemObject dim tso ' TextStreamObject ' Write data to disk set fso = CreateObject("Scripting.FileSystemObject") set tso = fso.OpenTextFile(dataFileName, 2, True) ' 2=replace, 8=append tso.write stockCsv tso.close ' Release memory set tso = Nothing set fso = Nothing End Function '--------------------------------------------------------------------------------------------- ' Get the file date/time Private Function getFileDate(stockSymbol) ' Init dataFileName if not done already if len(dataFileName) = 0 and len(stockSymbol) <> 0 then dataFileNameInit(stockSymbol) ' Get the file date set fso = CreateObject("Scripting.FileSystemObject") if (fso.FileExists(dataFileName)) then set file = fso.GetFile(dataFileName) fDate = file.DateLastModified else fDate = 0 end if set fso = Nothing ' Return the result getFileDate = fDate End Function '--------------------------------------------------------------------------------------------- ' Sets the data file name for a particular stock symbol. This function should be called at ' least once. Private Function dataFileNameInit(stockSymbol) if dataFileName = "" and stockSymbol <> "" then ' Get the Samurize directory from the Registry set shell = createobject("wscript.shell") strValueName = "HKEY_CURRENT_USER\Software\Serious Samurize\General\DirPath" dataFileName = shell.regread(strValueName) strValueName = None set shell = Nothing ' Add the Scripts directory and filename dataFileName = dataFileName & "Scripts\" dataFileName = dataFileName & "YahooStockCsv_" & LCase(stockSymbol) & ".dat" end if End Function

ini code:

[Project] FileVersion=v1.15 OffsetX=374 OffsetY=197 Width=328 Height=67 Interval=1200 ReloadKey= PauseKey= LoadMBMStarter=0 TrayIcon=MAINICON [Source 0] Type=TActiveScriptCollector ScriptName=YahooStockCSV.vbs ScriptFunction=reportBrief2 Interval=5 IntervalSeconds=0 Param0=^dji [Output 0] Type=TTextOutput Text=DOW: %v Color=FF808080 Align=0 AlignVertical=0 FontName=Courier New FontSize=8 FontStyle=[] LineSpacing=1 NrOfDecimals=0 TabSize=10 Left=374 Top=197 Right=702 Bottom=210 AllowLink=0 Link= AllowAlertValue=0 AlertValue= AlertColor=FF0000FF AlertColor2=FFFF0000 AlertWhen=0 [Source 1] Type=TActiveScriptCollector ScriptName=YahooStockCSV.vbs ScriptFunction=reportBrief2 Interval=5 IntervalSeconds=0 Param0=^ixic [Output 1] Type=TTextOutput Text=NAS: %v Color=FF808080 Align=0 AlignVertical=0 FontName=Courier New FontSize=8 FontStyle=[] LineSpacing=1 NrOfDecimals=0 TabSize=10 Left=374 Top=210 Right=701 Bottom=224 AllowLink=0 Link= AllowAlertValue=0 AlertValue= AlertColor=FF0000FF AlertColor2=FFFF0000 AlertWhen=0 [Source 2] Type=TActiveScriptCollector ScriptName=YahooStockCSV.vbs ScriptFunction=reportBrief2 Interval=5 IntervalSeconds=0 Param0=^spb [Output 2] Type=TTextOutput Text=S&P: %v Color=FF808080 Align=0 AlignVertical=0 FontName=Courier New FontSize=8 FontStyle=[] LineSpacing=1 NrOfDecimals=0 TabSize=10 Left=374 Top=224 Right=701 Bottom=240 AllowLink=0 Link= AllowAlertValue=0 AlertValue= AlertColor=FF0000FF AlertColor2=FFFF0000 AlertWhen=0 [Source 3] Type=TActiveScriptCollector ScriptName=YahooStockCsv.vbs ScriptFunction=stockMarketStatus Interval=1 IntervalSeconds=0 [Output 3] Type=TTextOutput Text=Market is %v Color=FF808080 Align=0 AlignVertical=0 FontName=Tahoma FontSize=8 FontStyle=[] LineSpacing=1 NrOfDecimals=0 TabSize=8 Left=374 Top=245 Right=576 Bottom=264 AllowLink=0 Link= AllowAlertValue=0 AlertValue= AlertColor=FF0000FF AlertColor2=FFFF0000 AlertWhen=0

Permalänk

Någon som vet hur man får stockholmsbörsen att fungera me den där scripten?

Permalänk
Medlem

Jag skulle gärna ha svar på ovanståendes fråga!