Skip to content

Instantly share code, notes, and snippets.

View dipanjannag's full-sized avatar
🏠
Working from home

Dipanjan dipanjannag

🏠
Working from home
View GitHub Profile
date rate
28-07-2017 64.1483
27-07-2017 64.1216
26-07-2017 64.4208
25-07-2017 64.358
24-07-2017 64.4494
21-07-2017 64.3185
20-07-2017 64.4273
19-07-2017 64.3211
18-07-2017 64.3301
@dipanjannag
dipanjannag / SparkCopyPostgres.scala
Created July 23, 2017 15:38 — forked from longcao/SparkCopyPostgres.scala
COPY Spark DataFrame rows to PostgreSQL (via JDBC)
import java.io.InputStream
import org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils
import org.apache.spark.sql.{ DataFrame, Row }
import org.postgresql.copy.CopyManager
import org.postgresql.core.BaseConnection
val jdbcUrl = s"jdbc:postgresql://..." // db credentials elided
val connectionProperties = {
var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
AWS.config.region = 'us-east-1';
var lexruntime = new AWS.LexRuntime();
var params = {
botAlias: "prod",
botName: "OrderFlowers",
inputText: event["body-json"].message,
userId: event["body-json"].uuid,
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
def trade_points(data,max_spread, pair=(136308228,2714625)):
test_bse = data[data.instrument_token==pair[0]]
test_nse = data[data.instrument_token==pair[1]]
#print test_bse.head()
#print test_nse.head()
test_nse['market'] = 'nse'
test_bse['market'] = 'bse'
bse_bid = test_bse.iloc[0]['depth_buy_price_0 ']
nse_bid = test_nse.iloc[0]['depth_buy_price_0 ']
bse_offer = test_bse.iloc[0]['depth_sell_price_0 ']
{
"cells": [
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
#Return on quantity traded, across different sell value
x_b = range(10, 250, 10)
y_i = []
for i in range(10):
y_i.append([(0.2*i*x)-2*charge(1001,1001+(0.2*i),x) for x in x_b])
for i in range(10):
plt.plot(x_b, y_i[i], '-', label = 'Sell @'+str((0.2*i)+1001))
plt.legend()
import matplotlib.pyplot as plt
from pylab import rcParams
#Lets plot quantity against charge of arbitrage
rcParams['figure.figsize'] = 15, 10
qty = range(10, 2500, 10)
# multiply by 2. Because, to do arbitrage in intraday we need to do 4 trades
# i.e. 2 complete trade cycle
c_y = [2*charge(1000,1000.5, x) for x in qty]
fig, ax = plt.subplots()
ax.plot(qty,c_y,'-')
## Function to calculate charges paid on a full cycle trade
## Asumptions:
## 1. Transection charge: High value transaction happened on NSE
## i.e. the sell side in normal arbitrage. So for large quantity
## it will be higher. So we might calculate higher brokerage, but
## never lower. Better to have a safety margin.
def charge(buy, sell, qty):
buy = buy*qty
sell = sell*qty
turnover = buy+sell
def arbitrage_opportunity(nse_id, bse_id):
import pandas as pd
import datetime
tick_data = pd.read_csv('log_raw.csv')
tick_data.timestamp = pd.to_datetime(tick_data.timestamp)
# filter-out pre-market data. 03:45 UTC means 9:15 IST. 9:00 to 9:15 is pe-market session
tick_data = tick_data.loc[tick_data.timestamp > '2017-06-19 03:45:00.000']
stock_nse = tick_data[tick_data.instrument_token == nse_id]
stock_bse = tick_data[tick_data.instrument_token == bse_id]
print "bse feed mean interval",stock_bse.timestamp.diff().fillna(0).mean()