HowTo: Write custom stats using your own SQL functions

Forum for users that want to write their own custom queries against the PT database either via the Structured Query Language (SQL) or using the PT3 custom stats/reports interface.

Moderator: Moderators

HowTo: Write custom stats using your own SQL functions

Postby Mike0550 » Wed Nov 11, 2009 7:10 pm

This has been tested to work with PostgreSQL 8.3, and 8.4, and parses hands from Stars, Full Tilt, and Pacific. I only use Stars right now, so its possible the others don't work.

I while back I realized that I needed some info on the hands being played that wasn't included with Poker Tracker. So, I wrote functions to re-parse the hand histories, store the data (so they only get parsed once), and allow me to use them for HUD stats.

The Functions are for getting the first preflop limper, first preflop raiser, the number of limpers, the preflop aggressor position, if the hand was folded to the blinds, if the first raiser was the last raiser (for determining if there was a cold 4-bet, or if it was the original raiser that 4-bet), and if the BB got a walk. The function called "holdem_getpreflopinfo" is the most interesting one because it does all of the parsing.

Code: Select all
CREATE LANGUAGE plpgsql;

-- Function: holdem_iswalk(integer)
-- DROP FUNCTION holdem_iswalk(integer);
CREATE OR REPLACE FUNCTION holdem_iswalk(ihand integer)
  RETURNS boolean AS
$BODY$

DECLARE
   iRaiser int;
   iLimper int;

BEGIN
   SELECT Holdem_GetFirstPreflopRaiser(iHand) INTO iRaiser;
   SELECT Holdem_GetFirstPreflopLimper(iHand) INTO iLimper;
   IF (iRaiser = -1 AND iLimper = -1) THEN
      RETURN 1;
   END IF;
   RETURN 0;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_iswalk(integer) OWNER TO postgres;

-- Function: holdem_isfirstandlastraisersame(integer)
-- DROP FUNCTION holdem_isfirstandlastraisersame(integer);
CREATE OR REPLACE FUNCTION holdem_isfirstandlastraisersame(ihand integer)
  RETURNS boolean AS
$BODY$

DECLARE
   iFirstRaiser int;
   iLastRaiser int;

BEGIN
   SELECT Holdem_GetPreflopAggressorPosition(ihand) INTO iLastRaiser;
   SELECT Holdem_GetFirstPreflopRaiser(ihand) INTO iFirstRaiser;
   IF (iLastRaiser = iFirstRaiser) THEN
      RETURN TRUE;
   ELSE
      RETURN FALSE;
   END IF;
   END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_isfirstandlastraisersame(integer) OWNER TO postgres;

-- Function: holdem_isblindsonly(integer)
-- DROP FUNCTION holdem_isblindsonly(integer);
CREATE OR REPLACE FUNCTION holdem_isblindsonly(ihand integer)
  RETURNS boolean AS
$BODY$

DECLARE
   bBlindsOnly boolean;

BEGIN
   SELECT IsBlindsOnly INTO bBlindsOnly FROM Holdem_UserAddon WHERE id_hand = iHand;
   IF (bBlindsOnly IS NULL) THEN
      IF (SELECT Holdem_GetPreflopInfo(ihand, 4)) = 1 THEN
         bBlindsOnly := true;
      ELSE
         bBlindsOnly := false;
      END IF;

   END IF;
   RETURN bBlindsOnly;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_isblindsonly(integer) OWNER TO postgres;


-- Function: holdem_getpreflopinfo(integer, integer)
-- DROP FUNCTION holdem_getpreflopinfo(integer, integer);
CREATE OR REPLACE FUNCTION holdem_getpreflopinfo(ihand integer, iret integer)
  RETURNS integer AS
$BODY$DECLARE

   sSiteType text;
   
   iFirstRaisePosition integer;
   iFirstLimpPosition integer;
   iLimpCount integer;

   iHolePlace integer;
   iFlopPlace integer;
   iFoldPlace integer;
   iRaisePlace integer;
   iActionPlace integer;
   iSummaryPlace integer;
   iCallPlace integer;
   iStopPlace integer;
   iNoRaise integer;
   iNoLimp integer;
   iPlayerCount integer;
   sHH text;
   sPreFlop text;
   sPreFlopStart text;
   sRaiseText text;

   sStartFind text;
   sSummaryFind text;
   sFlopFind text;
   sCheckText text;
   sCallText text;
   sRaiseFind text;
   sFoldText text;
   
   bBlindsOnly boolean;

BEGIN
   SELECT history INTO sHH FROM holdem_hand_histories WHERE id_hand = iHand;
   
   IF (position('GAME #' in sHH) = 1) THEN
      -- Full Tilt
      sSiteType := 'Full Tilt';
      sStartFind := '*** HOLE CARDS ***';
      sSummaryFind := '*** SUMMARY ***';
      sFlopFind := '*** FLOP ***';
      sRaiseFind := 'Raise';
      sCheckText := 'Check';
      sCallText := 'Call';
      sFoldText := 'Fold';
   ELSEIF (position('PokerStars Game #' in sHH) = 1) THEN
      -- Poker Stars
      sSiteType := 'PokerStars';
      sStartFind := '*** HOLE CARDS ***';
      sSummaryFind := '*** SUMMARY ***';
      sFlopFind := '*** FLOP ***';
      sRaiseFind := 'raises';
      sCheckText := 'checks';
      sCallText := 'calls';
      sFoldText := 'folds';
   ELSEIF (position('Pacific Hand History for Game ' in sHH) > 0) THEN
      -- Pacific
      sSiteType := 'Pacific';
      sStartFind := '** Dealing down cards **';
      sSummaryFind := '** Summary **';
      sFlopFind := '** Dealing Flop **';
      sRaiseFind := 'raises';
      sCheckText := 'checks';
      sCallText := 'calls';
      sFoldText := 'folds';
   END IF;

   IF (sSiteType IS NOT NULL) THEN

      SELECT cnt_players INTO iPlayerCount FROM holdem_hand_summary WHERE id_hand = iHand;

      iFirstRaisePosition := -100;
      iFirstLimpPosition := -100;
      iLimpCount := -1;
      bBlindsOnly := false;
   
      iHolePlace := position(sStartFind in sHH);
      IF (iHolePlace > 0) THEN
         iFirstRaisePosition := -1;
         iFirstLimpPosition := -1;
         iLimpCount := 0;

         iSummaryPlace := position(sSummaryFind in sHH);
         iFlopPlace := position(sFlopFind in sHH);
         IF (iFlopPlace < 1) THEN
            iStopPlace := iSummaryPlace;
         ELSE
            iStopPlace := iFlopPlace;
         END IF;
         sPreFlop := substring(sHH from iHolePlace for iStopPlace - iHolePlace);
         sPreFlopStart := sPreFlop;
         iRaisePlace := position(sRaiseFind in sPreFlop);

         iNoRaise := 0;
         iNoLimp := 0;
      
         IF (iRaisePlace < 1) THEN
            iFirstRaisePosition := -1;
            sRaiseText := sPreFlop;
         ELSE
            sRaiseText := substring(sPreFlop from 0 for iRaisePlace);
            iFirstRaisePosition := -2;
         END IF;
      
         sRaiseText := replace(sRaiseText, sCheckText, sCallText);
         LOOP
      
            iFoldPlace := position(sFoldText in sRaiseText);
            iCallPlace := position(sCallText in sRaiseText);
            IF (iFoldPlace < 1) THEN
               IF (iCallPlace < 1) THEN
                  EXIT;
               ELSE
                  iActionPlace := iCallPlace;
               END IF;
            ELSE
               IF (iCallPlace < 1 OR iFoldPlace < iCallPlace) THEN
                  iActionPlace := iFoldPlace;
               ELSE
                  iActionPlace := iCallPlace;               
               END IF;
            END IF;
            IF (iActionPlace = iCallPlace) THEN
               iLimpCount := iLimpCount + 1;
            END IF;
            
            sRaiseText := substring(sRaiseText from iActionPlace + 1);
            iNoRaise := iNoRaise + 1;
         
            IF (iLimpCount = 0) THEN
               iNoLimp := iNoLimp + 1;
            END IF;
         END LOOP;
         
         IF (iFirstRaisePosition != -1) THEN
            iFirstRaisePosition := iPlayerCount - 3 - iNoRaise;
            IF (iFirstRaisePosition = -1) THEN
               iFirstRaisePosition := 9;
            ELSEIF (iFirstRaisePosition = -2) THEN
               iFirstRaisePosition := 8;
            END IF;
         END IF;

         iFirstLimpPosition := iPlayerCount - 3 - iNoLimp;
         IF (iFirstLimpPosition = -1) THEN
            iFirstLimpPosition := 9;
         ELSEIF (iFirstLimpPosition = -2) THEN
            iFirstLimpPosition := -1;      -- Walk
         END IF;
      
         IF (iLimpCount = 0) THEN
            iFirstLimpPosition := -1;
         END IF;

         IF (iFirstRaisePosition = -1) THEN
            iLimpCount := iLimpCount - 1;
         END IF;
   
         bBlindsOnly := iFirstLimpPosition = 9 OR (iFirstRaisePosition = 9 AND iLimpCount = 0);
      END IF;
   
      INSERT INTO Holdem_UserAddon (id_hand, FirstPreflopRaiser, FirstPreflopLimper, NumberOfLimpers, IsBlindsOnly) VALUES (iHand, iFirstRaisePosition, iFirstLimpPosition, iLimpCount, bBlindsOnly);
   
      IF (iRet = 1) THEN
         RETURN iFirstRaisePosition;
      ELSEIF (iRet = 2) THEN
         RETURN iFirstLimpPosition;
      ELSEIF (iRet = 3) THEN
         RETURN iLimpCount;
      ELSE
         IF (bBlindsOnly) THEN
            RETURN 1;
         ELSE
            RETURN 0;
         END IF;
      END IF;
   ELSE
      RETURN -2;
   END IF;

END;$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_getpreflopinfo(integer, integer) OWNER TO postgres;

-- Function: holdem_getpreflopaggressorposition(integer)
-- DROP FUNCTION holdem_getpreflopaggressorposition(integer);
CREATE OR REPLACE FUNCTION holdem_getpreflopaggressorposition(ihand integer)
  RETURNS integer AS
$BODY$

DECLARE
   iPosition integer;
   iAgg integer;

BEGIN
   SELECT val_p_raise_aggressor_pos INTO iAgg FROM holdem_hand_player_detail WHERE id_hand = iHand AND val_p_raise_aggressor_pos != -1;
   SELECT Holdem_FixAggressorPosition(ihand, iAgg) INTO iPosition;

   RETURN iPosition;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_getpreflopaggressorposition(integer) OWNER TO postgres;


-- Function: holdem_getlimpcount(integer)
-- DROP FUNCTION holdem_getlimpcount(integer);
CREATE OR REPLACE FUNCTION holdem_getlimpcount(ihand integer)
  RETURNS integer AS
$BODY$

DECLARE
   iLimpCount integer;

BEGIN
   SELECT NumberOfLimpers INTO iLimpCount FROM Holdem_UserAddon WHERE id_hand = iHand;
   IF (iLimpCount IS NULL) THEN
      SELECT Holdem_GetPreflopInfo(ihand, 3) INTO iLimpCount;

   END IF;
   RETURN iLimpCount;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_getlimpcount(integer) OWNER TO postgres;

-- Function: holdem_getfirstpreflopraiser(integer)
-- DROP FUNCTION holdem_getfirstpreflopraiser(integer);
CREATE OR REPLACE FUNCTION holdem_getfirstpreflopraiser(ihand integer)
  RETURNS integer AS
$BODY$

DECLARE
   iFirstRaisePosition integer;

BEGIN
   SELECT FirstPreflopRaiser INTO iFirstRaisePosition FROM Holdem_UserAddon WHERE id_hand = iHand;
   IF (iFirstRaisePosition IS NULL) THEN
      SELECT Holdem_GetPreflopInfo(ihand, 1) INTO iFirstRaisePosition;

   END IF;
   RETURN iFirstRaisePosition;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_getfirstpreflopraiser(integer) OWNER TO postgres;

-- Function: holdem_getfirstprefloplimper(integer)
-- DROP FUNCTION holdem_getfirstprefloplimper(integer);
CREATE OR REPLACE FUNCTION holdem_getfirstprefloplimper(ihand integer)
  RETURNS integer AS
$BODY$

DECLARE
   iFirstLimpPosition integer;

BEGIN
   SELECT FirstPreflopLimper INTO iFirstLimpPosition FROM Holdem_UserAddon WHERE id_hand = iHand;
   IF (iFirstLimpPosition IS NULL) THEN
      SELECT Holdem_GetPreflopInfo(ihand, 2) INTO iFirstLimpPosition;

   END IF;
   RETURN iFirstLimpPosition;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_getfirstprefloplimper(integer) OWNER TO postgres;

-- Function: holdem_fixaggressorposition(integer, integer)
-- DROP FUNCTION holdem_fixaggressorposition(integer, integer);
CREATE OR REPLACE FUNCTION holdem_fixaggressorposition(ihand integer, pos integer)
  RETURNS integer AS
$BODY$

DECLARE
   iPlayerCount integer;
   iPosition integer;

BEGIN
   -- input is the aggressor position, put sb and bb to 9 and 8
   SELECT cnt_players INTO iPlayerCount FROM holdem_hand_summary WHERE id_hand = iHand;

   IF (pos = NULL) THEN
      iPosition = -1;
   ELSEIF (pos = iPlayerCount - 2) THEN
      iPosition := 8;
   ELSEIF (pos = iPlayerCount - 1) THEN
      iPosition := 9;
   ELSE
      iPosition := pos;
   END IF;

   RETURN iPosition;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION holdem_fixaggressorposition(integer, integer) OWNER TO postgres;


-- Table: holdem_useraddon
-- DROP TABLE holdem_useraddon;
CREATE TABLE holdem_useraddon
(
  id_hand integer NOT NULL,
  firstpreflopraiser integer NOT NULL,
  firstprefloplimper integer NOT NULL,
  numberoflimpers integer NOT NULL,
  isblindsonly boolean NOT NULL,
  CONSTRAINT "ID" PRIMARY KEY (id_hand)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE holdem_useraddon OWNER TO postgres;


This will need to be run with the query tool in PostgreSQL Admin. Copy and paste everything to the query editor, and then run it. Once you run this, you will have extra info to build stats with. I will provide a couple examples here.

How to determine the number of times someone has limp re-raised from early position (full ring):
Code: Select all
sum(if[(holdem_hand_player_statistics.position = 7 OR holdem_hand_player_statistics.position = 6 OR holdem_hand_player_statistics.position = 5) AND (SELECT Holdem_GetFirstPreflopLimper(holdem_hand_player_statistics.id_hand)) = holdem_hand_player_statistics.position AND (holdem_hand_player_statistics.flg_p_3bet OR holdem_hand_player_statistics.enum_p_3bet_action = 'R' OR holdem_hand_player_statistics.enum_p_4bet_action = 'R'), 1, 0])

And the opportunities to limp re-raise from early:
Code: Select all
sum(if[(holdem_hand_player_statistics.position = 7 OR holdem_hand_player_statistics.position = 6 OR holdem_hand_player_statistics.position = 5) AND (SELECT Holdem_GetFirstPreflopLimper(holdem_hand_player_statistics.id_hand)) = holdem_hand_player_statistics.position AND (holdem_hand_player_statistics.flg_p_3bet_opp OR holdem_hand_player_statistics.enum_p_3bet_action != 'N' OR holdem_hand_player_statistics.enum_p_4bet_action != 'N'), 1, 0])



How to determine the number of times the Hijack has tried to steal, the BB has tried a resteal, and won:
Code: Select all
sum(if[holdem_hand_player_statistics.position = 2 AND (SELECT Holdem_GetLimpCount(holdem_hand_player_statistics.id_hand)) = 0 AND (SELECT Holdem_GetFirstPreflopRaiser(holdem_hand_player_statistics.id_hand)) = 2 AND (SELECT Holdem_GetPreflopAggressorPosition(holdem_hand_player_statistics.id_hand)) = 8 AND holdem_hand_player_statistics.enum_p_3bet_action = 'F', 1, 0])

And the opportunities to do the same:
Code: Select all
sum(if[holdem_hand_player_statistics.position = 2 AND (SELECT Holdem_GetLimpCount(holdem_hand_player_statistics.id_hand)) = 0 AND (SELECT Holdem_GetFirstPreflopRaiser(holdem_hand_player_statistics.id_hand)) = 2 AND (SELECT Holdem_GetPreflopAggressorPosition(holdem_hand_player_statistics.id_hand)) = 8 AND NOT holdem_hand_player_statistics.enum_p_3bet_action = 'N', 1, 0])


This should give you an idea of the level of detail you can go into here with being able to use this extra information.

I should also note that custom functions being queried for HUD stats can only pass one variable.

I hope someone finds this as useful as I have, and let me know if you have any questions.
Mike0550
 
Posts: 24
Joined: Thu May 15, 2008 1:03 pm

Re: HowTo: Write custom stats using your own SQL functions

Postby WhiteRider » Thu Nov 12, 2009 5:05 am

Thanks for sharing - that looks really useful.
WhiteRider
Moderator
 
Posts: 54025
Joined: Sat Jan 19, 2008 7:06 pm
Location: UK

Re: HowTo: Write custom stats using your own SQL functions

Postby zubs1aa » Wed Dec 30, 2009 2:09 am

Mike0550 wrote:
I while back I realized that I needed some info on the hands being played that wasn't included with Poker Tracker. So, I wrote functions to re-parse the hand histories, store the data (so they only get parsed once), and allow me to use them for HUD stats.

....

This will need to be run with the query tool in PostgreSQL Admin. Copy and paste everything to the query editor, and then run it. Once you run this, you will have extra info to build stats with. I will provide a couple examples here.


I hope someone finds this as useful as I have, and let me know if you have any questions.


OK i have a question!

Could something like this be run live in the background so that it updates as you are playing? Or is that tool something you'd have to run on a one-off and hand trigger?
zubs1aa
 
Posts: 2219
Joined: Fri Feb 08, 2008 1:52 pm

Re: HowTo: Write custom stats using your own SQL functions

Postby WhiteRider » Wed Dec 30, 2009 6:20 am

You might be able to write a script to trigger it, but bear in mind if you do it too often you are creating more work for your computer. I doubt it would slow things down too much, though.
WhiteRider
Moderator
 
Posts: 54025
Joined: Sat Jan 19, 2008 7:06 pm
Location: UK

Re: HowTo: Write custom stats using your own SQL functions

Postby Mike0550 » Sat Jan 30, 2010 4:00 am

The first time a hand is used, it gets parsed, and the info is stored in the database. This does happen live, or on a cache update, whatever comes first.

I play 24 tables doing this, and it does slow things down, but not so much that I can't use it, however I have a very fast machine.
Mike0550
 
Posts: 24
Joined: Thu May 15, 2008 1:03 pm

Re: HowTo: Write custom stats using your own SQL functions

Postby stasia42 » Sun Jan 31, 2010 12:31 am

I'm not sure I totally understood how it works, so... Let's say for your example of HJ tries a steal attempt, and BB 3-bets, making the HJ to fold.. When this happens while playing, does the HUD stat of "player tried a steal attempt from HJ and folded to Blind 3-bet" gets updated instantly...? or do you need to finish the session, update cache, and this stat will be available for your next session?
stasia42
 
Posts: 35
Joined: Tue Aug 25, 2009 12:51 pm

Re: HowTo: Write custom stats using your own SQL functions

Postby Mike0550 » Fri Feb 05, 2010 2:18 am

It will be updated the next time your hud updates. If it updates every hand, then it will be available the next hand, so yes it does update instantly.

As you can tell, this would be pretty worthless if it wasn't updated immediately.
Mike0550
 
Posts: 24
Joined: Thu May 15, 2008 1:03 pm


Return to Custom Stats, Reports, and SQL [Read Only]

Who is online

Users browsing this forum: No registered users and 0 guests