This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MLStrategy(Strategy): | |
n1=5 | |
n2=10 | |
def init(self): | |
self.sma1 = self.I(SMA, self.data.Close, self.n1) | |
self.sma2 = self.I(SMA, self.data.Close, self.n2) | |
def next(self): | |
if crossover(self.sma1, self.sma2) and self.data.target == 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from backtesting import Backtest, Strategy | |
from backtesting.lib import crossover | |
from backtesting.test import SMA | |
class MLStrategy(Strategy): | |
n1=5 | |
n2=10 | |
def init(self): | |
self.sma1 = self.I(SMA, self.data.Close, self.n1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_ohlc(ticker, period='60d', interval='5m'): | |
cols = ['Open','High','Low','Close'] | |
ohlc = Ticker(ticker).history(period=period, interval=interval)[cols][:-1] | |
return ohlc | |
df = get_ohlc('ETH-USD') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run(request): | |
alert = request.get_json() | |
# # If alert request is a buy, we buy | |
symbol = alert['ticker'] | |
pair = symbol + "-USD" | |
side = alert['side'] | |
usd_price = client.fetch_ticker(pair)['last'] | |
position = client.fetch_balance()[symbol]['free'] | |
size = usd_price * position | |
if side == 'buy' and size < 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def buy_max(pair, client=client): | |
# Use max possible usd size | |
price = client.fetchTicker(pair)['last'] | |
size = client.fetchBalance()['USD']['free'] / price | |
client.createMarketBuyOrder(pair, amount=size) | |
print(f"bought ${size} of {pair}") | |
def sell_max(pair, client=client): | |
symbol = pair.split("-")[0] | |
size = client.fetchBalance()[symbol]['free'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ccxt import coinbasepro | |
def get_client(): | |
client = coinbasepro({ | |
'apiKey': '', | |
'secret': '', | |
'password': '', | |
'enableRateLimit': True, | |
}) | |
return client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = get_data('ETH-USD') | |
df = df/1000 | |
strategies = [SmaCross, EmaCross, WmaCross] | |
all_runs = [] | |
for strategy in strategies: | |
bt = Backtest(df, strategy, cash=100, commission=.002) | |
stats = bt.optimize(n1=range(5, 90, 5), | |
n2=range(10, 120, 5), | |
maximize='Return (Ann.) [%]', | |
constraint=lambda param: param.n1 < param.n2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_data(pair): | |
ticker = Ticker(pair) | |
df = ticker.history(period='3y', rounding=True)[['Open','High','Low','Close']] | |
return df |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmaCross(Strategy): | |
# Define the two EMA lags as *class variables* | |
# for later optimization | |
n1 = 5 | |
n2 = 10 | |
def init(self): | |
# Precompute two moving averages | |
self.ema1 = self.I(EMA, self.data.Close, self.n1) | |
self.ema2 = self.I(EMA, self.data.Close, self.n2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from backtesting import Strategy | |
from backtesting.lib import crossover | |
from backtesting import Backtest | |
from yfinance import Ticker | |
import pandas as pd | |
from talib import EMA, SMA, WMA |
NewerOlder