Apr 1, 2019 03:17 AM
(This post was last modified: Apr 1, 2019 03:19 AM by Dark.)
M1 Finance is a US-based stock brokerage that offers fractional sharing and a different type of investing strategy compared to other brokerages. There is no API so I decided to create a Python wrapper for it.
I've implemented a trading script that will calculate the inverse of a particular stock (SPXS) and determine a 'base' amount to invest each day. If the S&P 500 is up for the day, the bot invests less for that particular day. These are just my particular options as the wrapper allows you to write just about anything you want. I'll be updating my portfolio status over the week here during my test run
Output:Starting amount: $2,850.62
Ending amount (4/5/19):
Script:
I've implemented a trading script that will calculate the inverse of a particular stock (SPXS) and determine a 'base' amount to invest each day. If the S&P 500 is up for the day, the bot invests less for that particular day. These are just my particular options as the wrapper allows you to write just about anything you want. I'll be updating my portfolio status over the week here during my test run
Output:
Code
DEBUG: Beginning User Login
DEBUG: I successfully ran the login function.
[20.86, -1.97, 1.16, '257.3M', 0.93]
DEBUG: 20.07 #This is the amount I'm purchasing found from the percent_change (-1.97/10) * base_order(25)
DEBUG: Beginning Account Selection
DEBUG: Current Account:
DEBUG: Selecting Account:Roth
DEBUG: Current Account:
Beginning Pie Purchase
DEBUG: Confirming Order
DEBUG: Checking purchase of UlBTOjVRTjQyODE1LDYwODE5MA%3D%3D
DEBUG: Order complete
DEBUG: Bot run completeEnding amount (4/5/19):
Script:
Code
from m1lib import *
import config
import datetime
#This script intends to show the m1lib.py file in use.
day = 60*60*24
def checkDay():
x = datetime.datetime.now()
if (x.strftime("%a") == "Sat" or x.strftime("%a") == "Sun"):
DebugCommand("Weekend date, skipping")
time.sleep(day)
pass
else:
runBot()
def runBot():
if (login(config.username, config.password) == True):
DebugCommand("I successfully ran the login function.")
ticker_results = searchTicker("SPXS")
print (ticker_results)
pchange = ticker_results[1]
if pchange > 0:
baseorder = 25 * ((-pchange/10)+1)
baseorder = round(baseorder, 2)
else:
baseorder = 25 * ((pchange/10)+1)
baseorder = round(baseorder, 2)
DebugCommand(baseorder)
orderPV(baseorder, "Roth")
#End your file with...
closeSession()
DebugCommand("Bot run complete")
time.sleep(day)
checkDay()
while 1 > 0:
checkDay()
