Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some more helper methods odds_table, set_betfair_odds and get_o…

…dds_spread.
  • Loading branch information...
commit b8605516391bdd1d188da27efb663f745022ab39 1 parent 1f02949
Luke Byrne authored
Showing with 143 additions and 7 deletions.
  1. +29 −0 README.md
  2. +61 −0 lib/betfair/api.rb
  3. +53 −7 spec/betfair/api_spec.rb
View
29 README.md
@@ -332,6 +332,35 @@ foo.first returns
{ :prices_string=>nil, :runner_matched=>0, :last_back_price=>0, :wom=>0.6054936499440416, :b1=>4.2, :b1_available=>430.35, :b2=>4.1, :b2_available=>311.51, :b3=>3.85,
:b3_available=>4.75, :l1=>4.4, :l1_available=>155.46, :l2=>4.6, :l2_available=>230.69, :l3=>5.9, :l3_available=>100.3
}
+
+
+## Set Betfair Odds ##
+Method for dealing with calculating the Betfair increments from a price passed in.
+
+ set_betfair_odds(price, pips = 0, round_up = false, round_down = false)
+
+ helpers.set_betfair_odds(2.31, 1, false, false)
+
+ { :price=>2.31, :prc=>2.34, :pips=>1, :increment=>0.02 }
+
+ helpers.set_betfair_odds(2.31, 1, false, true)
+
+ { :price=>2.31, :prc=>2.32, :pips=>1, :increment=>0.02 }
+
+ helpers.set_betfair_odds(2.31, 1, false, true)
+
+ { :price=>2.31, :prc=>2.32, :pips=>1, :increment=>0.02 }
+
+ helpers.set_betfair_odds(132, 0, false, true)
+
+ { :price=>132.0, :prc=>130.0, :pips=>0, :increment=>10 }
+
+## Get Odds Spead ##
+
+Work out the distance between a high and low spread. Useful for when the B1 and L1 are miles apart and you are
+trying to set a threshold on when to place a bet.
+
+ helpers.get_odds_spread(1.28, 3.43)
# Extra
## API Limits ##
View
61 lib/betfair/api.rb
@@ -599,6 +599,67 @@ def price_string(string, prices_only = false)
return price
end
+ def odds_table
+ odds_table = []
+ (1.01..1.99).step(0.01).each { |i| odds_table << i.round(2) }
+ (2..2.98).step(0.02).each { |i| odds_table << i.round(2) }
+ (3..3.95).step(0.05).each { |i| odds_table << i.round(2) }
+ (4..5.9).step(0.1).each {|i| odds_table << i.round(2) }
+ (6..9.8).step(0.2).each {|i| odds_table << i.round(2) }
+ (10..19.5).step(0.5).each {|i| odds_table << i.round(2) }
+ (20..29).step(1).each {|i| odds_table << i.round }
+ (30..48).step(2).each {|i| odds_table << i.round }
+ (50..95).step(5).each {|i| odds_table << i.round }
+ (100..1000).step(10).each {|i| odds_table << i.round }
+ return odds_table
+ end
+
+ def set_betfair_odds(price, pips = 0, round_up = false, round_down = false)
+ price = price.to_f
+ prc = price
+ case price
+ when 0..1 then prc = increment = 1.01
+ when 1.01..1.99 then increment = 0.01
+ when 2..2.98 then increment = 0.02
+ when 3..3.95 then increment = 0.05
+ when 4..5.9 then increment = 0.1
+ when 6..9.8 then increment = 0.2
+ when 10..19.5 then increment = 0.5
+ when 20..29 then increment = 1
+ when 30..48 then increment = 2
+ when 50..95 then increment = 5
+ when 100..1000 then increment = 10
+ else
+ price = 1000
+ increment = 1000
+ end
+
+ if round_up == true
+ prc = ( (prc / increment).ceil * increment ).round(2)
+ elsif round_down == true
+ prc = ( (prc / increment).floor * increment ).round(2)
+ else
+ prc = ( (prc / increment).round * increment ).round(2)
+ end
+
+ ot = odds_table # Set up the odds table
+ unless pips == 0 and odds_table.count > 0 # If pips is 0
+ index = ot.index(prc) + pips
+ index = 0 if index < 0
+ index = 349 if index > 349
+ prc = ot[index] # Grab x number of pips above
+ end
+
+ { price: price, prc: prc, pips: pips, increment: increment }
+
+ end
+
+ def get_odds_spread(back_odds = 0, lay_odds = 0)
+ back_odds = set_betfair_odds(back_odds)
+ lay_odds = set_betfair_odds(lay_odds)
+ diff = lay_odds[:prc] - back_odds[:prc]
+ end
+
end
end
View
60 spec/betfair/api_spec.rb
@@ -11,7 +11,7 @@ module Betfair
@helpers = Betfair::Helpers.new
end
- describe "Create a hash from the get_all_markets API call" do
+ describe "create a hash from the get_all_markets API call" do
it "pulls the relevant stuff out of get_all_markets and puts it in a hash" do
savon.expects(:get_all_markets).returns(:success)
markets = @bf.get_all_markets(@session_token, 2)
@@ -20,7 +20,7 @@ module Betfair
end
end
- describe "Create a hash for the market details" do
+ describe "create a hash for the market details" do
it "pulls the relevant stuff out of market details and puts it in a hash" do
savon.expects(:get_market).returns(:success)
market = @bf.get_market(@session_token, 2, 10038633)
@@ -29,7 +29,7 @@ module Betfair
end
end
- describe "Cleans up the get market details" do
+ describe "cleans up the get market details" do
it "sort the runners for each market out " do
savon.expects(:get_market).returns(:success)
market = @bf.get_market(@session_token, 2, 10038633)
@@ -38,7 +38,7 @@ module Betfair
end
end
- describe "Get the price string for a runner" do
+ describe "get the price string for a runner" do
it "so that we can combine it together with market info" do
savon.expects(:get_market_prices_compressed).returns(:success)
prices = @bf.get_market_prices_compressed(@session_token, 2, 10038633)
@@ -47,8 +47,8 @@ module Betfair
end
end
- describe "Combine market details and runner prices api call" do
- it "Combines the two api calls of get_market and get_market_prices_compressed " do
+ describe "combine market details and runner prices api call" do
+ it "combines the two api calls of get_market and get_market_prices_compressed " do
savon.expects(:get_market).returns(:success)
market = @bf.get_market(@session_token, 2, 10038633)
@@ -60,7 +60,53 @@ module Betfair
combined.should_not be_nil
end
end
-
+
+ describe "set up an odds tables of all possible Betfair Odds" do
+ it "should return a hash of all possible Betfair odds with correct increments" do
+ odds_table = @helpers.odds_table
+ odds_table.should_not be_nil
+ odds_table.should be_an_instance_of(Array)
+ odds_table.count.should eq(350)
+ odds_table[256].should eq(85)
+ end
+
+ it "should return a standard hash" do
+ betfair_odds = @helpers.set_betfair_odds(275, 0, false, false)
+ betfair_odds.should be_an_instance_of(Hash)
+ betfair_odds[:price].should eq(275)
+ betfair_odds[:prc].should eq(280)
+ betfair_odds[:increment].should eq(10)
+ betfair_odds[:pips].should eq(0)
+ end
+
+ it "should return a standard hash with prc at 1 pip and price rounded up" do
+ betfair_odds = @helpers.set_betfair_odds(2.31, 1, true, false)
+ betfair_odds.should be_an_instance_of(Hash)
+ betfair_odds[:price].should eq(2.31)
+ betfair_odds[:prc].should eq(2.34)
+ betfair_odds[:increment].should eq(0.02)
+ betfair_odds[:pips].should eq(1)
+ end
+
+ it "should return a standard hash with prc at 2 pip and price rounded down" do
+ betfair_odds = @helpers.set_betfair_odds(2.31, 5, false, true)
+ betfair_odds.should be_an_instance_of(Hash)
+ betfair_odds[:price].should eq(2.31)
+ betfair_odds[:prc].should eq(2.4)
+ betfair_odds[:increment].should eq(0.02)
+ betfair_odds[:pips].should eq(5)
+ end
+
+ it "should return an even spread off odds based on the odds_table method" do
+ spread = @helpers.get_odds_spread(271, 343)
+ spread.should eq(70.0)
+ spread = @helpers.get_odds_spread(1.28, 3.43)
+ spread.should eq(2.17)
+ end
+
+ end
+
+
end
describe "Placing and cancelling bets - " do
Please sign in to comment.
Something went wrong with that request. Please try again.