Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Configuration;
using Alpaca.Markets;
namespace example
{
class Program
{
static void Main(string[] args)
{
@yoshyoshi
yoshyoshi / tradingbot.py
Created July 24, 2018 04:56
trading bot code snippet
from datetime import datetime
import numpy as np
import talib
import alpaca_trade_api as tradeapi
api = tradeapi.REST(key_id=<your key id>,secret_key=<your secret key>)
barTimeframe = "1H" # 1Min, 5Min, 15Min, 1H, 1D
assetsToTrade = ["SPY","MSFT","AAPL","NFLX"]
positionSizing = 0.25
@yoshyoshi
yoshyoshi / visualizer.py
Created July 24, 2018 04:51
visualizer script
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
import talib
import alpaca_trade_api as tradeapi
api = tradeapi.REST(key_id=<your key id>,secret_key=<your secret key>)
barTimeframe = "1H" # 1Min, 5Min, 15Min, 1H, 1D
assetsToDownload = ["SPY","MSFT","AAPL","NFLX"]
@yoshyoshi
yoshyoshi / multisymbol.py
Created July 24, 2018 00:43
a multi-symbol back-tester based on position handler
import matplotlib.pyplot as plt
from datetime import datetime
import alpaca_trade_api as tradeapi
api = tradeapi.REST(key_id=<your key id>,secret_key=<your secret key>)
backtester = positionHandler(startingBalance=10000,liveTrading=False) # Using position handler from previous article
backtestSymbolList = ["SPY","AAPL","BBRY"]
positionSizing = 0.33
import alpaca_trade_api as tradeapi
api = tradeapi.REST(key_id=<your key id>,secret_key=<your secret key>)
class positionHandler:
def __init__(self,startingBalance=10000,liveTrading=False):
self.cashBalance = startingBalance
self.livePositions = {} # Dictionary of currently open positions
self.openOrders = [] # List of open orders
self.positionHistory = [] # List of items [Symbol, new position size]
@yoshyoshi
yoshyoshi / gist:73f130026c25a7dcdb9d6909b1990277
Last active July 10, 2018 03:23
read data CSV files and process into trading indicators
import numpy as np
import talib
rawDataLocation = "<your folder location>"
storageLocation = "<your folder location>"
assetsToProcess = ["SPY"]
# Data types for the CSV format of the previous post
# ISO8601 date-time string should be read using |S24 due to its standard length
@yoshyoshi
yoshyoshi / gist:5a35a23ac263747eabc70906fd037ff3
Last active September 1, 2021 14:42
download and store OHLCV data into a CSV
import alpaca_trade_api as tradeapi
api = tradeapi.REST(key_id=<your key id>,secret_key=<your secret key>)
storageLocation = "<your folder location>"
barTimeframe = "1H" # 1Min, 5Min, 15Min, 1H, 1D
assetsToDownload = ["SPY","MSFT","AAPL","NFLX"]
iteratorPos = 0 # Tracks position in list of symbols to download
assetListLen = len(assetsToDownload)