Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Automating investing with M1Finance & Python

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()
Very cool, what do you want to do with this?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by dinosaur770Jul 9, 2022 10:57 PM 6 129 Jul 9, 2022 10:57 PM
Last Post: dinosaur770
[Python] by DisparityFeb 18, 2018 01:35 PM 2 120 Feb 18, 2018 01:35 PM
Last Post: Disparity