Hack Forums
[Python] Automating investing with M1Finance & Python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Automating investing with M1Finance & Python (/showthread.php?tid=5955665)



Automating investing with M1Finance & Python - Dark - Apr 1, 2019

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:
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 complete
Starting amount: $2,850.62
Ending 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()



RE: Automating investing with M1Finance & Python - demarco - May 17, 2019

Very cool, what do you want to do with this?