34
loading...
This website collects cookies to deliver better user experience
import pandas as pd
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import requests
import pandas as pd
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
def get_ohlc():
response = requests.get(
'https://data.lemon.markets/v1/ohlc/d1/?isin=US0378331005 \
&from=2021-06-25T00:00:00&to=2021-11-14T00:00:00',
headers={"Authorization": "Bearer YOUR-API-KEY"})
results = response.json()['results']
return results
def calculate_returns():
df = pd.DataFrame(get_ohlc())
# calculate returns based on closing price
df['r'] = df['c'].pct_change().fillna(method='bfill')
df.tail()
return df
def plot_returns():
returns = calculate_returns()['r']
# plot returns
plt.hist(returns, bins=25, density=True, alpha=0.6, color='darkorange')
# plot normal distribution fitted to returns
mu, std = norm.fit(returns)
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=2)
plt.xlabel('Daily Tesla Returns')
plt.ylabel('Probability Density Function')
plt.show()
plot_returns()
import talib as ta
import statsmodels.api as sm
import stan
import ffn
import requests
import json
def place_order(isin: str, side: str, quantity: int):
if trading_signal:
request = requests.post("https://paper-trading.lemon.markets/v1/orders/",
data=json.dumps({
"isin": isin,
"expires_at": "p7d",
"side": side,
"quantity": quantity,
"space_id": YOUR-SPACE-ID,
"venue": "XMUN"}),
headers={"Authorization": "BEARER YOUR-API-KEY"})
print("Your trade was placed")
else:
print("Your trade was not placed")
place_order("US0378331005", "buy", 1)
trading_signal
indicates whether the trade should be placed or not (this is where the aforementioned libraries come in handy). However, in this script we have not defined it yet.import requests
request = requests.get("https://paper-trading.lemon.markets/v1/portfolio/",
headers={"Authorization": "BEARER YOUR-API-KEY"})
print(request.json())
ISIN : {YOUR-SPACE-ID: {
'buy_orders_total': '',
'buy_price_avg': '',
'buy_price_avg_historical': '',
'buy_price_max': '',
'buy_price_min': '',
'buy_quantity': ''
'orders_total': '',
'quantity': '',
'sell_orders_total': '',
'sell_price_avg_historical': '',
'sell_price_max': '',
'sell_price_min': '',
'sell_quantity': ''}
import empyrical as em
import pyfolio as pf