Skip to content

Instantly share code, notes, and snippets.

View marcosan93's full-sized avatar

Marco Santos marcosan93

View GitHub Profile
import random
def check_ace(hand):
"""
Checks if there's an ace in the hand in case total went over 21
"""
if 'A' in hand:
hand[hand.index('A')] = 'A.'
return True
else:
def dealer_turn(your_hand, dealer_hand, total, dtotal, r_count, true_cnt, deck, turn=True):
"""
Activates the dealer's turn if player's move was 'stay'
"""
# Tallying wins, losses, and draws
wins = 0
draw = 0
loss = 0
# Looping through the moves
import pandas as pd
def card_counter(hand, strategy='Hi-Lo'):
"""
Counting cards based on strategy selected
Returns sum of the values
"""
df = pd.read_pickle('Card_Counting_Values')
import pandas as pd
# Creating a simple dictionary to start the values
vals = {'2': 1, '3': 1, '4': 1, '5': 1, '6': 1,
'7': 0, '8': 0, '9': 0, '10': -1, 'J': -1,
'Q': -1, 'K': -1, 'A': -1, 'A.': -1}
# Converting to a DataFrame
df = pd.DataFrame(vals, index=[0])
def player_move(your_hand, limit, true_cnt, dealer_hand):
"""
Chooses 'hit' or 'stay' depending on the limit set and count
"""
dtotal = hand_total(dealer_hand[:1])
# Meaning there are plenty of face cards left
if true_cnt > 0:
if hand_total(your_hand) >= limit:
# Declaring variables for use
results = {}
winnings = 0
total_games = 0
games_sim = 1000
rec_rounds = 10
limit = 16
strategies = list(df.index)
random.shuffle(strategies) # shuffling the index for more randomization
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.figure(figsize=(16,8))
# Plotting each strategies result
for i in results:
plt.plot(games_sim, results[i], label=i+': '+str(results[i][-1])+'%')
plt.title("Blackjack Probability \nGraph 3")
plt.ylabel("Percentage Won (also Draws)")
# Reading in the data
bc = pd.read_csv('BTC-USD.csv')
# Converting to datetime
bc['Date'] = pd.to_datetime(bc.Date)
# Setting the index as the dates
bc.set_index('Date', inplace=True)
# Selecting only the dates from 2017-01-01 onwards
# Converting the data to a logarithmic scale
bc_log = pd.DataFrame(np.log(bc.Close))
# Differencing the log values
log_diff = bc_log.diff().dropna()
# Using the Dickey-Fuller test to check for stationarity
results = adfuller(log_diff.Close)
print(f"P-value: {results[1]}")
def best_param(model, data, pdq, pdqs):
"""
Loops through each possible combo for pdq and pdqs
Runs the model for each combo
Retrieves the model with lowest AIC score
"""
ans = []
for comb in tqdm(pdq):
for combs in tqdm(pdqs):
try: