Jump to content
Twix mafia

Copy + calculate

Recommended Posts

Hi

 

I am having some problems with coding a part of my scar script after the newer changes (and that i have forgotten many things).

I want scar to:

copy (save) a tekst (number) from a website

then say that number times 5 (lets call the new number a)

then copy (save) another number from another website (lets call this b)

then i want scar to calculate the difference between the numbers - and if the difference is over lets say 1 then it should proceed

Link to comment
Share on other sites

well... - i dont have any code that matches what i need to write right now :) - but i need scar to somehow regonize and use/interpret values found on a website.

I've made scar highlight the number i need it to use - then i thought if i could say ctrl+C to copy it - maybe SCAR could import the data from the clipboard? anyways i need scar to import some numbers from a website - the way it accesses/imports the numbers dosen't matter :)

 

edit:

Yea i used a lot of time on the scar wiki - but all the changes in the programming language have completely confused me! :o

Edited by Twix mafia
Link to comment
Share on other sites

Oh thank you - but how do i replace it with Ctrl + c?..... :confused:

 

edit:

well i have to define it as a variable, right? then i can set the variable as the clipboard - but how can i make scar copy something? - thats something i could make work before all this updates.. - i cant press ctrl + c anymore - it wont work with the things i try ;)

Edited by Twix mafia
Link to comment
Share on other sites

begin
 VKeyDown(VK_CONTROL);
 VKeyDown(CharToVKey('C'));
 VKeyUp(CharToVKey('C'));
 VKeyUp(VK_CONTROL);
 WriteLn(GetClipboard);
end.

 

- - - Updated - - -

 

However... Using SCAR Divi's internet-related functions would be the smarter choice really.

Clipboard works aswell I guess but... I am pretty sure it would be a lot more stable using those internet functions.

What is the webpage you are using the script for? If you can't give it to public, you could always private message it to me.

 

Also, if you give me nice details, maybe I could come up with something. ;)

 

-Jani

Link to comment
Share on other sites

oh thank you :)

The side is Bitcoinity EUR

I want scar to read the exchange rate. - i see it godt the ID "last_price"

Then i want it to calculate the exchange rate to USD (i think i will just give it the rate of the day manually)

after that it has to go to Bitcoinity USD and read that exchange rate.

after that i want scar to find the difference and then do different actions whether the difference is over/under eg. 1. Here i can use the "if" and "if not" i think? :)

Link to comment
Share on other sites

Here is a little function:

 

function ExchangeRate(exchange: string): Extended;
begin
 Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetPage('http://bitcoinity.org/markets/mtgox/' + Trim(exchange))), -1);
end;

begin
 ClearDebug;
 WriteLn('Last Price [EUR]: ' + FloatToStr(ExchangeRate('EUR')));
 WriteLn('Last Price [uSD]: ' + FloatToStr(ExchangeRate('USD')));
end.

Link to comment
Share on other sites

NOTE: It returns -1, if for some reason it couldn't capture the rate correctly. :) So you can (well, need to) add failsafes for those cases.

It might return -1 sometimes when the page is updating the rate... I think.

 

Anyways, now it's pretty much math that you will need to plant in. :P

 

- - - Updated - - -

 

If you want version that wont ever return as -1, then you can do this:

 

function ExchangeRate(exchange: string): Extended;
begin
 repeat
   Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetPage('http://bitcoinity.org/markets/mtgox/' + Trim(exchange))), -1); 
 until (Result > -1);
end;

begin
 ClearDebug;
 WriteLn('Last Price [EUR]: ' + FloatToStr(ExchangeRate('EUR')));
 WriteLn('Last Price [uSD]: ' + FloatToStr(ExchangeRate('USD')));
end.

Link to comment
Share on other sites

oh i see! thats smart! - but i think i will put in a wait before until - just so my computer wont crash if bitcoinity goes down ;)

 

edit:

well, thats not that smart to do, is it? - then it will wait before et goes on - even if there is no error..?

Edited by Twix mafia
Link to comment
Share on other sites

Here is also another way (method) that you can use to get the rate:

 

var
 client: Integer;

procedure ScriptTerminate;
begin
 FreeHTTPClient(client); 
end;

function ExchangeRate(exchange: string): Extended;
begin
 repeat
   Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetHTTPPage(client, 'http://bitcoinity.org/markets/mtgox/' + exchange)), -1); 
 until (Result > -1);
end;

var
 t, i: Integer;

begin
 ClearDebug;
 client := InitializeHTTPClient(True, True);  
 for i := 0 to 9 do
 begin                                                                                                                                                
   t := GetSystemTime;
   WriteLn(FloatToStr(ExchangeRate('EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]');
 end;
end.

 

That requires the client variable, but it is a little faster than GetPage() most of the time.

 

You can compare that to the GetPage() version, below:

 

function ExchangeRate(exchange: string): Extended;
begin
 repeat
   Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetPage('http://bitcoinity.org/markets/mtgox/' + Trim(exchange))), -1); 
 until (Result > -1);
end;

var
 t, i: Integer;

begin
 ClearDebug;
 for i := 0 to 9 do
 begin                                                                                                                                                
   t := GetSystemTime;
   WriteLn(FloatToStr(ExchangeRate('EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]');
 end;
end.

 

Most of the time the GetHTTPPage() wins.

 

-Jani

Link to comment
Share on other sites

You are right - et seems like GetHTTPPage() is about 200 ms faster to get the Exhange rate :)

 

EDIT: (FORGET THIS - YOU ALREDY ANSWERED)

 

can you tell my why this does not work?

var
 client: Integer;

procedure ScriptTerminate;
begin
 FreeHTTPClient(client); 
end;

function ExchangeRate(exchange: string): Extended;
begin
 repeat
   Result := StrToFloatDef(Between('<span class="bld">', ' USD</span>', GetHTTPPage(client, 'https://www.google.com/finance?q=' + exchange)), -1); 
   wait(200);
 until (Result > -1);
end;


begin
 ClearDebug;
 client := InitializeHTTPClient(True, True);  
 WriteLn(FloatToStr(ExchangeRate('EURUSD')));  

end.

 

Im trying to import the exchange rate for EUR to USD from: https://www.google.com/finance?q=EURUSD

Edited by Twix mafia
Link to comment
Share on other sites

Also, here is with function that uses X-Rates.com page for currency rate:

 

GetPage() version:

function ExchangeRate(exchange: string): Extended;
begin
 exchange := Trim(exchange);
 case (exchange <> '') of
   True:
   repeat
     Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetPage('http://bitcoinity.org/markets/mtgox/' + exchange)), -1); 
   until (Result > -1); 
   False: Result := -1;
 end;
end;

function CalculateCurrencyRate(amount: Extended; fromCurrency, toCurrency: string): Extended;
var
 data: string;  
 tmp: Extended;
begin
 fromCurrency := Trim(fromCurrency);
 toCurrency := Trim(toCurrency);
 case ((fromCurrency <> '') and (toCurrency <> '')) of
   True:
   repeat                                                 
     data := GetPage('http://www.x-rates.com/calculator/?from=' + fromCurrency + '&to=' + toCurrency + '&amount=' + FloatToStr(amount));
     Result := StrToFloatDef(Between('<span class="ccOutputRslt">', '<span class="ccOutputTrail">', data), -1);  
     if (Result > -1) then  
     begin
       tmp := StrToFloatDef(FloatToStr(Result) + Between('<span class="ccOutputTrail">', '</span><span class="ccOutputCode">', data), -1);
       if (tmp > -1) then
         Result := tmp;  
     end;
     data := '';
   until (Result > -1); 
   False: Result := -1;      
 end;
end;

var
 t: Integer;

begin
 ClearDebug;         
 t := GetSystemTime;              
 WriteLn('USD => EUR: ' + FloatToStr(CalculateCurrencyRate(1, 'USD', 'EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn('EUR => USD: ' + FloatToStr(CalculateCurrencyRate(1, 'EUR', 'USD')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn(FloatToStr(ExchangeRate('EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]');
end.

 

..and GetHTTPPage() version:

var
 client: Integer;

procedure ScriptTerminate;
begin
 FreeHTTPClient(client); 
end;

function ExchangeRate(exchange: string): Extended;
begin 
 exchange := Trim(exchange);
 case (exchange <> '') of
   True:
   repeat
     Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetHTTPPage(client, 'http://bitcoinity.org/markets/mtgox/' + exchange)), -1); 
   until (Result > -1);    
   False: Result := -1;
 end;
end;

function CalculateCurrencyRate(amount: Extended; fromCurrency, toCurrency: string): Extended;
var
 data: string;  
 tmp: Extended;
begin
 fromCurrency := Trim(fromCurrency);
 toCurrency := Trim(toCurrency);
 case ((fromCurrency <> '') and (toCurrency <> '')) of
   True:
   repeat                                                 
     data := GetHTTPPage(client, 'http://www.x-rates.com/calculator/?from=' + fromCurrency + '&to=' + toCurrency + '&amount=' + FloatToStr(amount));
     Result := StrToFloatDef(Between('<span class="ccOutputRslt">', '<span class="ccOutputTrail">', data), -1);  
     if (Result > -1) then  
     begin
       tmp := StrToFloatDef(FloatToStr(Result) + Between('<span class="ccOutputTrail">', '</span><span class="ccOutputCode">', data), -1);
       if (tmp > -1) then
         Result := tmp;  
     end;
     data := '';
   until (Result > -1);    
   False: Result := -1;      
 end;
end;

var
 t: Integer;

begin
 ClearDebug; 
 client := InitializeHTTPClient(True, True);         
 t := GetSystemTime;              
 WriteLn('USD => EUR: ' + FloatToStr(CalculateCurrencyRate(1, 'USD', 'EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn('EUR => USD: ' + FloatToStr(CalculateCurrencyRate(1, 'EUR', 'USD')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn(FloatToStr(ExchangeRate('EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]');
end.

 

-Jani

Link to comment
Share on other sites

wow - i begin to do not understand what is going on anymore :o - but well... it works perfectly! :D

 

but now i got all 3 things i need:

EUR to USD

USD/BTC

EUR/BTC

 

How do i then make it calculate how much the EUR rate is in USD?

a := FloatToStr(ExchangeRate('EUR')
b := FloatToStr(CalculateCurrencyRate(1, 'EUR', 'USD')
c := a*b
WriteLn(c);

or something like that??

Edited by Twix mafia
Link to comment
Share on other sites

If you want to use that Google page for the currency rate, then here (NOTE: it's limited to amount as 1):

 

GetPage() version:

function ExchangeRate(exchange: string): Extended;
begin
 exchange := Trim(exchange);
 case (exchange <> '') of
   True:
   repeat
     Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetPage('http://bitcoinity.org/markets/mtgox/' + exchange)), -1); 
   until (Result > -1); 
   False: Result := -1;
 end;
end;

function CalculateCurrencyRate(fromCurrency, toCurrency: string): Extended;
var
 data: string;  
 tmp: Extended;
begin
 fromCurrency := Trim(fromCurrency);
 toCurrency := Trim(toCurrency);
 case ((fromCurrency <> '') and (toCurrency <> '')) of
   True:
   repeat                                    
     data := GetPage('https://www.google.com/finance?q=' + fromCurrency + toCurrency);
     Result := StrToFloatDef(Between('1 ' + fromCurrency + ' = <span class=bld>', ' ' + toCurrency + '</span>', data), -1);  
     data := '';
   until (Result > -1); 
   False: Result := -1;      
 end;
end;

var
 t: Integer;

begin
 ClearDebug;         
 t := GetSystemTime;              
 WriteLn('USD => EUR: ' + FloatToStr(CalculateCurrencyRate('USD', 'EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn('EUR => USD: ' + FloatToStr(CalculateCurrencyRate('EUR', 'USD')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]'); 
 t := GetSystemTime;
 WriteLn(FloatToStr(ExchangeRate('EUR')) + ' [' + IntToStr(GetSystemTime - t) + ' ms.]');
end.

 

For some reason Google doesn't work with GetHTTPPage() version - I have absolutely no idea why. :\

 

As to your question, not sure what you need? Which rate? :P

 

Do remember, that the ExchangeRate() function grabs the information from the bitcoinity page, so this one: bitcoinity.org/markets

Link to comment
Share on other sites

EDIT: I think we will need BTC / EUR & BTC / USD conversions to do the calculation(s) you are after.. Is there somewhere BTC => EUR and BTC USD => conversion at mtgox webpage? Hmm..

 

The reason why GetHTTPPage() doesn't work with that Google page, is because it doesn't support SSL, whereas GetPage() does - I got this information from SCAR Divi wiki. :)

Link to comment
Share on other sites

Well if we say the EUR/BTC rate (about 90) * the USD/EUR rate (EUR => USD) rate then we have the price of one BTC on the EU market but in the USD currency. like:

https://www.google.dk/search?safe=off&q=89.89+*+1.298448&oq=89.89+*+1.298448&gs_l=serp.3...1516.1765.0.1943.2.2.0.0.0.0.74.137.2.2.0.cappswebhl..0.0...1.1.12.serp.uQh7QqfNy1w

 

 

You ask about mtgox... well its the same rates you see on mtgox as on bitcoinity... - so the BTC => EUR and BTC USD => conversion would be the same.

Im interestet in seeing the difference on the two markets.

Edited by Twix mafia
Link to comment
Share on other sites

With the calculation you added to your post here a little earlier, this would be the correct way to do it (so, I am using the way you did, because I am getting mindfucked atm, LOL!):

 

var
 client: Integer;

procedure ScriptTerminate;
begin
 FreeHTTPClient(client); 
end;

function ExchangeRate(exchange: string): Extended;
begin 
 exchange := Trim(exchange);
 case (exchange <> '') of
   True:
   repeat
     Result := StrToFloatDef(Between('<span id=''last_price''>', '</span>', GetHTTPPage(client, 'http://bitcoinity.org/markets/mtgox/' + exchange)), -1); 
   until (Result > -1);    
   False: Result := -1;
 end;
end;

function CalculateCurrencyRate(amount: Extended; fromCurrency, toCurrency: string): Extended;
var
 data: string;  
 tmp: Extended;
begin
 fromCurrency := Trim(fromCurrency);
 toCurrency := Trim(toCurrency);
 case ((fromCurrency <> '') and (toCurrency <> '')) of
   True:
   repeat                                                 
     data := GetHTTPPage(client, 'http://www.x-rates.com/calculator/?from=' + fromCurrency + '&to=' + toCurrency + '&amount=' + FloatToStr(amount));
     Result := StrToFloatDef(Between('<span class="ccOutputRslt">', '<span class="ccOutputTrail">', data), -1);  
     if (Result > -1) then  
     begin
       tmp := StrToFloatDef(FloatToStr(Result) + Between('<span class="ccOutputTrail">', '</span><span class="ccOutputCode">', data), -1);
       if (tmp > -1) then
         Result := tmp;  
     end;
     data := '';
   until (Result > -1);    
   False: Result := -1;      
 end;
end;

var
 t: Integer;
 a, b, c: Extended;

begin
 ClearDebug; 
 client := InitializeHTTPClient(True, True);         
 t := GetSystemTime;              
 a := ExchangeRate('EUR');
 b := CalculateCurrencyRate(1, 'EUR', 'USD');
 c := (a * b)
 WriteLn(c);
end.

Link to comment
Share on other sites

ooooh you are so nice! :D thank you! :D

I edited the end a bit - so i will get the difference:

var
 t: Integer;
 a, b, c, d, e: Extended;

begin
 ClearDebug; 
 client := InitializeHTTPClient(True, True);         
 t := GetSystemTime;              
 a := ExchangeRate('EUR');
 b := CalculateCurrencyRate(1, 'EUR', 'USD');
 c := (a * b);      
 d := ExchangeRate('USD');   
 e := (d - c);    
 WriteLn(e);
end.

 

I dont have time to make more right now - but maybe tomorrow or in next week i have to make mtgox make transactions according to the rate :) - i will probably need help for that too if you have time and desire? :)

 

How can i thank you? By sending you something in LOL? - by giving you Dota 2? reddit gold? anything? :D

Edited by Twix mafia
Link to comment
Share on other sites

ooooh you are so nice! :D thank you! :D

I edited the end a bit - so i will get the difference:

var
 t: Integer;
 a, b, c, d, e: Extended;

begin
 ClearDebug; 
 client := InitializeHTTPClient(True, True);         
 t := GetSystemTime;              
 a := ExchangeRate('EUR');
 b := CalculateCurrencyRate(1, 'EUR', 'USD');
 c := (a * b);      
 d := ExchangeRate('USD');   
 e := (d - c);    
 WriteLn(e);
end.

 

I dont have time to make more right now - but maybe tomorrow or in next week i have to make mtgox make transactions according to the rate :) - i will probably need help for that too if you have time and desire? :)

 

How can i thank you? By sending you something in LOL? - by giving you Dota 2? anything? :D

Glad to see/hear it works buddy!

I will definitely try and help you (and others) here in future aswell, well, always when I am just able to help out, because there is sometimes cases which I can't solve, but sometimes someone else can solve those cases! :P

 

Anyways, I wasn't after any goodies here, but thanks for the great offers anyways!

Only trying to be helpful here. +I really enjoy challenges like these!

I am just happy to see you appreciate my help here - thanks man.

Positive feedback is always welcome, makes anyone feel good. :D

 

-Jani

Link to comment
Share on other sites

I've added a little more to the ending

var
 t: Integer;
 a, b, c, d, e, f: Extended;

begin
 ClearDebug; 
 client := InitializeHTTPClient(True, True);         
 t := GetSystemTime;              
 a := ExchangeRate('EUR');
 b := CalculateCurrencyRate(1, 'EUR', 'USD');
 c := (a * b); //Mtgox EUR i USD      
 d := ExchangeRate('USD');   
 e := (d - c); //difference. Skal være højre end f
 WriteLn(e);     
 f := ((1/0.994)*(c-(0.994*c))); //Fees. Skal være mindre end e. 
 WriteLn(f);   
 if (e>f) then begin
   WriteLn('Forskellen er større end fees');     
 end;
 if not  (e>f) then begin
   WriteLn('lort');
 end;  
end.

 

The two writelines just says go for it and do not go for it...

Can you somehow make Scar buy you BTC automaticly from MTgox on the EUR market and instant after reciving the BTC sell it on the USD market? :)

 

Its maybe not the smartest idéa do have the login informations stored in a scar script - so i thought, can you log in with your browser - and just leave it logged in. And then just send the information about buying and selling with scar? - og do i have to make scar send/have the login informations too ?

Edited by Twix mafia
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...