-
-
Save krishnavelu/e0df312ccf5f022edb1823461ff4230e to your computer and use it in GitHub Desktop.
Simple Moving average strategy using alice_blue library
This file contains 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
import logging | |
import datetime | |
import statistics | |
from time import sleep | |
from alice_blue import * | |
# Config | |
username = 'username' | |
password = 'password' | |
app_id = 'app_id' | |
api_secret = 'api_secret' | |
twoFA = '1993' | |
EMA_CROSS_SCRIP = 'INFY' | |
logging.basicConfig(level=logging.DEBUG) # Optional for getting debug messages. | |
# Config | |
ltp = 0 | |
alice = None | |
def event_handler_quote_update(message): | |
global ltp | |
ltp = message['ltp'] | |
def buy_signal(ins_scrip): | |
global alice | |
alice.place_order(transaction_type = TransactionType.Buy, | |
instrument = ins_scrip, | |
quantity = 1, | |
order_type = OrderType.Market, | |
product_type = ProductType.Intraday) | |
def sell_signal(ins_scrip): | |
global alice | |
alice.place_order(transaction_type = TransactionType.Sell, | |
instrument = ins_scrip, | |
quantity = 1, | |
order_type = OrderType.Market, | |
product_type = ProductType.Intraday) | |
def main(): | |
global alice | |
global username | |
global password | |
global twoFA | |
global app_id | |
global api_secret | |
global EMA_CROSS_SCRIP | |
minute_close = [] | |
session_id = AliceBlue.login_and_get_sessionID( username = username, | |
password = password, | |
twoFA = twoFA, | |
app_id = app_id, | |
api_secret = api_secret) | |
alice = AliceBlue(username = "username", session_id = session_id, master_contracts_to_download=['NSE']) | |
print(alice.get_balance()) # get balance / margin limits | |
print(alice.get_profile()) # get profile | |
print(alice.get_daywise_positions()) # get daywise positions | |
print(alice.get_netwise_positions()) # get netwise positions | |
print(alice.get_holding_positions()) # get holding positions | |
ins_scrip = alice.get_instrument_by_symbol('NSE', EMA_CROSS_SCRIP) | |
alice.start_websocket(subscribe_callback=event_handler_quote_update) | |
alice.subscribe(ins_scrip, LiveFeedType.TICK_DATA) | |
current_signal = '' | |
while True: | |
if(datetime.datetime.now().second == 0): | |
minute_close.append(ltp) | |
if(len(minute_close) > 20): | |
sma_5 = statistics.mean(minute_close[-5:]) | |
sma_20 = statistics.mean(minute_close[-20:]) | |
if(current_signal != 'buy'): | |
if(sma_5 > sma_20): | |
buy_signal(ins_scrip) | |
current_signal = 'buy' | |
if(current_signal != 'sell'): | |
if(sma_5 < sma_20): | |
sell_signal(ins_scrip) | |
current_signal = 'sell' | |
sleep(1) # sleep for 1 seconds | |
sleep(0.2) # sleep for 200ms | |
if(__name__ == '__main__'): | |
main() |
All,
Updated SMA strategy as per new 2.0 version.
You are still showing the old code. That is not workign now. Why this muchu lethargy in uploading a workign version.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this documentation still hold good?
the run_in_background option seems to be omitted from the API itself..
abObj.start_websocket(subscribe_callback=touchlineupdates,order_update_callback=ordUpdates,run_in_background=True, TypeError: AliceBlue.start_websocket() got an unexpected keyword argument 'run_in_background'
Could you please confirm