Skip to content

Instantly share code, notes, and snippets.

@rainmattertech
Last active June 8, 2022 18:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save rainmattertech/29b837b94efd9e0f0333864a0353f6a5 to your computer and use it in GitHub Desktop.
Save rainmattertech/29b837b94efd9e0f0333864a0353f6a5 to your computer and use it in GitHub Desktop.
"""
Simple Moving Average implemented using KiteConnect Python library. -- [https://kite.trade](kite.trade)
Rainmatter (c) 2016
License
-------
This GIST is licensed under the MIT License
"""
from kiteconnect import KiteConnect
# Initialize all the variables we need
api_key = "YOUR API KEY"
access_token = "YOUR ACCESS TOKEN"
client_id = "YOUR CLIENT ID"
# Instrument token of RELIANCE
instrument_token = "738561"
# Dates between which we need historical data
from_date = "2016-10-01"
to_date = "2016-10-17"
# Interval(minute, day, 3 minute, 5 minute...)
interval = "5minute"
kite = KiteConnect(api_key=api_key)
kite.set_access_token(access_token)
# Gets historical data from Kite Connect
def get_historical_data():
return kite.historical(instrument_token, from_date, to_date, interval)
"""
Implementation of the moving average strategy.
We go through the historical records that
we received form Kite Connect, calculate moving average,
and place a BUY or SELL order.
"""
def strategy(records):
total_closing_price = 0
record_count = 0
order_placed = False
last_order_placed = None
last_order_price = 0
profit = 0
for record in records:
record_count += 1
total_closing_price += record["close"]
#Moving avearge is calculated for every 5 ticks(records)
if record_count >= 5:
moving_average = total_closing_price/5
# If moving average is greater than last tick, place a buy order
if record["close"] > moving_average:
if last_order_placed == "SELL" or last_order_placed is None:
# If last order was sell, exit the stock first
if last_order_placed == "SELL":
print "Exit SELL"
# Calculate profit
profit += last_order_price - record["close"]
last_order_price = record["close"]
# Fresh BUY order
print "place new BUY order"
last_order_placed = "BUY"
# If moving average is lesser than last tick, and there is a position, place a sell order
elif record["close"] < moving_average:
if last_order_placed == "BUY":
# As last order was a buy, first let's exit the position
print "Exit BUY"
# Calculate profit
profit += record["close"] - last_order_price
last_order_price = record["close"]
# Fresh SELL order
print "place new SELL order"
last_order_placed = "SELL"
total_closing_price -= records[record_count-5]["close"]
print "Gross Profit ", profit
# PLace the last order
place_order(last_order_placed)
# Place an order based upon transaction type(BUY/SELL)
def place_order(transaction_type):
kite.order_place(tradingsymbol="RELIANCE", exchange="NSE", quantity=1, transaction_type=transaction_type, order_type="MARKET", product="CNC")
def start():
records = get_historical_data()
strategy(records)
start()
@yashuyadav
Copy link

yashuyadav commented Mar 28, 2018

Hi,

I think this script has a bug. While placing BUY order for the first time, it is not assigning value to element 'last_order_price'. Which leads to incorrect (equal to the price at the time of selling) estimation of profit for the first order.

Hope this is helpful.

Regards,
YY

@prpattan
Copy link

prpattan commented Aug 8, 2020

In Line no 53 moving_average = total_closing_price/5 , it should be moving_average = total_closing_price/record_count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment