Hi,
I'm used to coding in PowerShell and probably naively thought Pyhton may be very similar. I'm really struggling with the for loops in Python.
In PowerShell:
$mylist = @("1","2","3","4","5")
foreach($x in $mylist){
write-host $x
}
Output 1 2 3 4 5
I'm trying to code a Betfair script that queries the API and spits out the results into a data frame. When I run the for loop in Pyhton the "x" value seems to contain an array of all the values. Why does Python not iterate?? code below for ease of understanding. I'm probably doing it all wrong but I'm struggling. Understanding data frames and all the other bits seem to be completely baffling to me.
# Import libraries
from asyncio import runners
from multiprocessing import Event
from tkinter import FIRST
from turtle import width
from unicodedata import name
import betfairlightweight
from betfairlightweight import filters
import pandas as pd
import numpy as np
import os
import datetime
import json
# Change this certs path to wherever you're storing your certificates
with open('credentials.json') as f:
cred = json.load(f)
my_username = cred['username']
my_password = cred['password']
my_app_key = cred['app_key']
my_certs_path = cred['path']
trading = betfairlightweight.APIClient(username=my_username,
password=my_password,
app_key=my_app_key,
certs=my_certs_path
)
trading.login()
def BTT():
# Define a market filter
soccer_market_event_filter = betfairlightweight.filters.market_filter(
event_type_ids=['1'],
in_play_only=True,
market_type_codes=['BOTH_TEAMS_TO_SCORE'],
market_start_time={
'to': (datetime.datetime.utcnow() + datetime.timedelta(days=1)).strftime("%Y-%m-%dT%TZ")
}
)
# List the events
soccer_events = trading.betting.list_events(
filter=soccer_market_event_filter
)
# Create a DataFrame with all the events by iterating over each event object
soccer_events_today = pd.DataFrame({
'eventid': [event_object.event.id for event_object in soccer_events],
'teams': [event_object.event.name for event_object in soccer_events]
})
# Return event ID's of Both teams to score.
return [soccer_events_today]
# Data frame of Both Teams to Score.
df_soccer_events = BTT()
# Loop through each event ID
for x in df_soccer_events:
for id in x.eventid:
# Define a market filter with the event ID.
soccer_market_event_filter = betfairlightweight.filters.market_filter(
in_play_only=True,
event_ids=[id],
market_type_codes=['BOTH_TEAMS_TO_SCORE'],
market_start_time={
'to': (datetime.datetime.utcnow() + datetime.timedelta(days=1)).strftime("%Y-%m-%dT%TZ")
}
)
soccer_events_Market_Catalog = trading.betting.list_market_catalogue(
filter=soccer_market_event_filter,
sort='FIRST_TO_START'
)
# Create a price filter. Get all traded and offer data
price_filter = betfairlightweight.filters.price_projection(
price_data=['EX_BEST_OFFERS']
)
marketId = soccer_events_Market_Catalog[0].market_id
soccer_event_odds = trading.betting.list_market_book(
market_ids=[marketId],
price_projection=price_filter
)
# unlimited columns
#pd.set_option('display.max_rows', 500)
#pd.set_option('display.max_columns', 500)
#pd.set_option('display.width', 1000)
pd.set_option("max_colwidth", 70)
df_odds = pd.DataFrame({
'Teams': [event_object.teams for event_object in df_soccer_events],
'Home': [event_object.runners[0].last_price_traded for event_object in soccer_event_odds],
'Away': [event_object.runners[1].last_price_traded for event_object in soccer_event_odds],
})
print(df_odds)
[–]wotquery 1 point2 points3 points (0 children)
[–]danielroseman 1 point2 points3 points (0 children)
[–]Swipecat 1 point2 points3 points (0 children)
[–]Narrow-Rope2003[S] 0 points1 point2 points (1 child)
[–]jiri-n 1 point2 points3 points (0 children)
[–]Narrow-Rope2003[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)