Sunday, 30 September 2012

Tweeting directly from Oracle

Finally set up the bespoke Oracle Twitter using OAuth rather than using RSS/XML from Twitterfeed.

{easy once you've spent a couple of days working out what the hell is all the fuss about OAuth}

The steps are:

1. set up an application on your Twitter account here

2. make a note of the consumer and account keys and secrets

3. in Oracle create urlencode function:
-=-=-=-=-=-=-

create or replace function urlencode( p_str in varchar2 ) return varchar2
as
    /* FROM: http://www.orafaq.com/forum/t/48047/0/
     * POSTED BY USER: http://www.orafaq.com/forum/u/45693/0/
     * KUDOS!
     */
    l_tmp   varchar2(6000);
    l_bad   varchar2(100) default ' >%}\~];?@&<#{|^[`/:=$+''"';
    l_char  char(1);
begin
    for i in 1 .. nvl(length(p_str),0) loop
        l_char :=  substr(p_str,i,1);
        if ( instr( l_bad, l_char ) > 0 )
        then
            l_tmp := l_tmp || '%' ||
                            to_char( ascii(l_char), 'fmXX' );
        else
            l_tmp := l_tmp || l_char;
        end if;
    end loop;

    return l_tmp;
end;
-=-=-=-=-=-=-=





4. update the XXXXX codes below with the relevant values from step 1 and run code within an SQL worksheet window - create a package from this, go on you know how - :

-=-=-=-=-=


SET SERVEROUTPUT ON

DECLARE

  l_oauth_request_token_url CONSTANT VARCHAR2 (500) := 'http://api.twitter.com/1/statuses/update.xml';
  l_oauth_consumer_key CONSTANT VARCHAR2 (500) := 'XXXX';
  l_oauth_token  CONSTANT VARCHAR2 (500) := 'XXXXX';
  l_oauth_secret CONSTANT VARCHAR2 (500) := 'XXXXX';
  l_oauth_nonce VARCHAR2 (500);
  l_oauth_signature_method CONSTANT VARCHAR2 (10) := urlencode ('HMAC-SHA1');
  l_oauth_timestamp VARCHAR2 (100);
  l_oauth_version CONSTANT VARCHAR2 (5) := urlencode ('1.0');
  l_oauth_consumer_secret CONSTANT VARCHAR2 (500) := 'XXXXXX';

  l_http_method VARCHAR2 (5) := 'POST';
  l_oauth_base_string VARCHAR2 (2000);

  l_oauth_key VARCHAR2 (500) := l_oauth_consumer_secret || '&' || l_oauth_secret ;

  l_sig_mac RAW (2000);
  l_base64_sig_mac VARCHAR2 (100);

  http_req UTL_HTTP.req;
  http_resp UTL_HTTP.resp;

  l_update_send VARCHAR2(2000);
  l_oauth_header  VARCHAR2(2000);

  l_line  VARCHAR2(1024);

  resp_name  VARCHAR2(256);
  resp_value VARCHAR2(1024);

  l_content varchar2(140) := urlencode('hello world');

  l_random varchar2(15);

BEGIN

select dbil.urlencode((sysdate - to_date('01-01-1970','DD-MM-YYYY')) * (86400)) into
l_oauth_timestamp from dual;

  -- RANDOM oauth_nonce
  SELECT dbms_random.string('A', 15)
  INTO l_random
  FROM DUAL;

  SELECT dbil.urlencode (UTL_ENCODE.base64_encode(UTL_I18N.string_to_raw (l_random, 'AL32UTF8')))
  INTO l_oauth_nonce
  FROM DUAL;



  l_oauth_base_string := l_http_method
                          || '&'
                          || urlencode (l_oauth_request_token_url)
                          || '&'
                          || urlencode ( 'oauth_consumer_key'
                              || '='
                              || l_oauth_consumer_key
                              || '&'
                              || 'oauth_nonce'
                              || '='
                              || l_oauth_nonce
                              || '&'
                              || 'oauth_signature_method'
                              || '='
                              || l_oauth_signature_method
                              || '&'
                              || 'oauth_timestamp'
                              || '='
                              || l_oauth_timestamp
                              || '&'
                              || 'oauth_token'
                              || '='
                              || l_oauth_token
                              || '&'
                              || 'oauth_version'
                              || '='
                              || l_oauth_version
                              || '&'
                              || 'status'
                              || '='
                              || l_content);
                           
  DBMS_OUTPUT.put_line (l_oauth_base_string);

  l_sig_mac := dbms_crypto.mac (  utl_i18n.string_to_raw (l_oauth_base_string, 'AL32UTF8')
                                , DBMS_CRYPTO.hmac_sh1
                                , UTL_I18N.string_to_raw (l_oauth_key, 'AL32UTF8'));
                             
  DBMS_OUTPUT.put_line ('Combined sig: ' || l_oauth_key);
                             
  l_base64_sig_mac := UTL_RAW.cast_to_varchar2 (UTL_ENCODE.base64_encode (l_sig_mac));

  DBMS_OUTPUT.put_line ('MAC Signature (Base64-encoded): ' ||  l_base64_sig_mac);

  l_update_send := l_oauth_request_token_url || '?status=' || l_content;
                 
    http_req := UTL_HTTP.begin_request (  l_update_send
                                        , l_http_method
                                        , UTL_HTTP.http_version_1_1);
                                     
   DBMS_OUTPUT.put_line ('UPDATE URL ' || l_update_send);
 
   UTL_HTTP.set_response_error_check (TRUE);
   UTL_HTTP.set_detailed_excp_support (TRUE);
 


    l_oauth_header := 'OAuth oauth_nonce="' || l_oauth_nonce || '", '
                      || 'oauth_signature_method="'|| l_oauth_signature_method || '", '
                      || 'oauth_timestamp="'|| l_oauth_timestamp || '", '
                      || 'oauth_consumer_key="'|| l_oauth_consumer_key || '", '
                      || 'oauth_token="' || l_oauth_token || '", '
                      || 'oauth_signature="' || urlencode (l_base64_sig_mac) || '", '
                      || 'oauth_version="' || l_oauth_version || '"';
                   
    utl_http.set_header ( r => http_req,
                          NAME => 'Authorization', VALUE => l_oauth_header);
                       
    DBMS_OUTPUT.put_line  ('HEADER: ' || l_oauth_header);                      
                       
    utl_http.write_text(  r => http_req, DATA => l_content);
   
    http_resp := utl_http.get_response(r => http_req);
   
   DBMS_OUTPUT.put_line('GETTING RESPONSE HEADERS! ');
 
   FOR i IN 1..utl_http.get_header_count(http_resp) LOOP
    utl_http.get_header(http_resp, i, resp_name, resp_value);
    dbms_output.put_line(resp_name || ': ' || resp_value);
   END LOOP;
 
  DBMS_OUTPUT.put_line('Getting content:');
  BEGIN
      LOOP
        utl_http.read_line(http_resp, resp_value, TRUE);
        dbms_output.put_line(resp_value);
      END LOOP;
   
      EXCEPTION
      WHEN utl_http.end_of_body THEN
        DBMS_OUTPUT.put_line('No more content.');
  END;

   utl_http.end_response(r => http_resp);


  EXCEPTION
    when others then
      DBMS_OUTPUT.put_line('HTTP ERROR: ' || utl_http.get_detailed_sqlerrm);

END;



-=-=-=-=-=

And that's it - you should be tweeting away from your Oracle Database - comment if you have any difficulties.

I am in the process now of changing the twitter feeds to contain all information rather than just the top 5.

Thursday, 27 September 2012

Candlestick/Markov/Trace

Just back from hols and will be documenting here the new Candlestick/Markov/Trace analysis that I have been working on - isnt that what holidays are for ?

But as a quick indicator, I am up to my eighth straight 11.5%; just had TOM, WSG, SEA and BRT.

Wednesday, 5 September 2012

AHT and new system

AHT did rather well yesterday, I was out at 11.5%, but its still going up.

New system delivered and setup - to the right is the new system screen shot of processor cores under calc mode - oasis is parallel processing

Currently into SEA, KMG, SIE, SLE.

Saturday, 1 September 2012

AHT

Today's AHT success coefficients are 1900/221 which are pretty good

Friday, 31 August 2012

even more trace analysis

Well this is interesting. All of the reports below (apart from HCM, this is included as a demo) are current live indicators for today - 31st-Aug-2012.
The columns in order left to right are:
company, date tweeted, opening price for the next day, 5, 10 and 15 day percentage increases, tweedledee, tweedledum (two success coefficients)
(empty columns denote that we do not yet know the opening price because today's data has not been analysed - it not being the end of day today yet)

I have noticed that any tweedledee below 200, with a low tweedledum (less than about 100) more often than not indicates a mediocre performance in our given time frame:

FTF 30-AUG-12



189 0
FTF 29-AUG-12 86.25 0 0 0 189 0
FTF 28-AUG-12 85 1.47 1.47 1.47 189 0
FTF 24-AUG-12 87 0 0 0 153 0
FTO 30-AUG-12



27 1000
FTO 29-AUG-12 9.4 1.65 1.65 1.65 27 1000
FTO 28-AUG-12 9.5 0.58 0.58 0.58 27 1000
FTO 24-AUG-12 9.3 3.23 3.23 3.23 27 1000
FTO 23-AUG-12 9.5 1.05 1.05 1.05 27 1000


On the other hand a large tweedledee denotes a good performance indicator within our time frame:

HCM 03-AUG-12 410 6.1 6.1 6.1 2481 705
HCM 02-AUG-12 400 8.75 8.75 8.75 2481 705
HCM 01-AUG-12 405 7.41 7.41 7.41 2480 704
HCM 31-JUL-12 390 11.54 11.54 11.54 2480 704
HCM 30-JUL-12 366 18.85 18.85 18.85 2377 692
HCM 27-JUL-12 345 23.19 26.09 26.09 2377 692
HCM 26-JUL-12 346 21.39 25.72 25.72 2377 692
HCM 25-JUL-12 350 17.14 24.29 24.29 2377 692
HCM 24-JUL-12 355 14.08 22.54 22.54 2377 692
HCM 23-JUL-12 366 10.66 18.85 18.85 2377 692

But as you can see it is also dependent on time - the success coefficients move in a wave like form, and there is an optimal time when we should trade.

(Also tweedledee has to be taken with tweedledum)

The two above statements together make for an interesting concept - as demonstrated below:

SIE30-AUG-12



30001280
SIE29-AUG-1274.492.42.42.430001280
SIE28-AUG-1275.071.611.611.6130001280
SIE24-AUG-1274.872.192.192.192180
SIE23-AUG-1274.093.273.273.271560
SIE22-AUG-1275.1451.821.821.821560
SIE21-AUG-1275.1451.821.821.821560
SIE20-AUG-1275.18951.761.761.761560
SIE17-AUG-1273.93.533.533.531560
SIE16-AUG-1273.034.774.774.772180
SIE15-AUG-1274.112.893.243.242180
SIE14-AUG-1274.1652.813.163.162180
SIE13-AUG-1274.352.562.912.912180
SIE10-AUG-1274.15052.833.183.182180
SIE09-AUG-1273.85852.483.593.592180
SIE08-AUG-1274.3251.842.592.942180

What we see here is that SIE has been trundling away not doing anything in particular but all of a sudden the success coefficients have increased. I am guessing that SIE should have been bought on the opening of the 29th Aug 2012.

As a bonus for today AHT has just triggered today with a tweedledee of 1865 and a tweedledum of 208.




Thursday, 30 August 2012

traces - more results

So here we are, where each night we compare the 300,000 odd Markov chains to the indicators triggered for each individual company.

To the right is a screen shot of all the LSE tweets around a randomly chosen date around 1st Aug this year - counting backwards the columns are: last column, success coefficient tweedledum, is the average number of time each individual indicator has occurred above the required 2 (the numbers are in 0.000s and so 231 would mean 2.0231 ), the penultimate column is the number of times separate individual indicators have been triggered, success coefficient tweedledee, here 561 means 561. The next three columns are percentage increase over the last 5, 10 and 15 days (reading left to right), The first column is the company, then the date and then the opening price of the next day (approximate price at which we would buy).

Currently analyzing these results, but have been trading high tweedledee and tweedledum numbers and it has been working (EXI, AGTA, HUM, SXX (although I missed buying this one, but it did act as predicted))

Wednesday, 22 August 2012

Fibonacci

Fibonacci is another buy/sell indicator sort of thing and am looking at incorporating - it is one where the first statement is 'if up trend do this, and if down trend do the other' (the same as Candlestick calcs) as well as pin pointing tops and bottoms of trends - luckily I have been working a long time trying to implement tops and bottoms, and am piggy backing our trace system onto Fibonacci -... as well as working on the 5.3b Candlesticks calculations (first step below, new bigger faster system)

First up, calculate the Markov chains for Fibonacci retracements.
Second, do something to get 5.3b Candlestick calculations to a reasonable time.

Sold AGTA into TOM just now on LSE.