def main():
    #Load in events first so we can make sure the server is open
    EventList = Scheduler.readEventsCSV('Events.csv')
    if (liveData == True):
        Scheduler.waitForOpen(EventList)

    #Allows us to know in the main file if there are trades open
    trades = []

    #Initialization
    if (liveData == True):
        #Start OANDA instance
        MinutesSinceOpen = Scheduler.minutesSinceEvent(
            Scheduler.getLastClosedEvent(EventList))
        print("Minutes since market opened are: ", MinutesSinceOpen)
        Trading = OANDA.Account(loginFileName, trades)
        trades = Trading.assessCurrentState()
        data = Data_Handling.InputData(liveData,
                                       Trading,
                                       SinceOpen=MinutesSinceOpen)
        margin = Trading.margin

        #Display if there were trades already opened, load them into the program's awareness
        print(Trading.previouslyOpenedTrades)

        ## Code I used to close all currently trades if I needed to reset things
        #openTrades = Trading.getOpenTrades()
        #for tradeIDNum in openTrades:
        #    Trading.closeTrade(None, tradeIDNum)
    else:
        # Initialize the things that still need to exist when just backtesting
        Trading = None
        data = Data_Handling.InputData(liveData, Trading)
        margin = 1000  # Use any number you like that allows your strategy to work
    tradeManagement = Trade_Handling.Trades(liveData, Trading)
    output = Algorithm.AlgoLogging()

    #### HERE IS THE EVENT LOOP ####
    for i in range(0, minutesToRun):
        Waiting = True  # used to run the code once a minute
        while (Waiting):
            d = datetime.now(timezone.utc)
            time.sleep(.01)
            if (d.second < 2 or liveData == False):

                #Time for the once-a-minute run
                if (
                        Scheduler.timeToMarketClose(EventList) < 2
                ):  # We closed everything 10 minutes before the weekend already
                    time.sleep(
                        180
                    )  # Wait to make sure the market really will be closed
                    Scheduler.waitForOpen(EventList)
                    MinutesSinceOpen = Scheduler.minutesSinceEvent(
                        Scheduler.getLastClosedEvent(EventList))
                    data = Data_Handling.InputData(liveData,
                                                   Trading,
                                                   SinceOpen=MinutesSinceOpen)
                Waiting = False
                data.getNextMinute()
                Algorithm.Model(data.dataForAlgorithm(), data.bidAsk(),
                                trades, margin,
                                Scheduler.timeToNextEvent(EventList), output)
                tradeManagement.manageTrades(trades, data.bidAsk())

                i += 1
                if (liveData == True):
                    time.sleep(3)
                    Trading.saveCSVs(
                    )  #Every minute we close and reopen the accessible event log
                if (d.minute == 30 and liveData == True):
                    Trading.accountInfo()  #Updates current margin etc.

    #Cleanup when the code ends
    tradeManagement.closeCSVs(trades, data.bidAsk())
    output.closeLog