15minute opening range breakout strategy in python

samphel
Dear all,
As i'm new to python as well as new to algo trading, i'm writing my first algo code and it is 15min ORB strategy so following is the code i have written and i need some help regarding this matter, as i need to know weather my code will work or not.

def on_ticks(ws,ticks):
for data in ticks:
current_time = datetime.datetime.now()
crt_time = current_time.time()
break_out_start_time = datetime.time(9, 15)
break_out_end_time = datetime.time(9, 30)

if crt_time >= break_out_start_time:
h_value = [data_details["name"], data['ohlc']['high']]
l_value = [data_details["name"], data['ohlc']['low']]

if crt_time == break_out_end_time:
h_value = [data_details["name"], data['ohlc']['high']]
high.append(h_value)
l_value = [data_details["name"], data['ohlc']['low']]
low.append(l_value)


so with the above code what is happening is sometime updates are not there and all the time h_value and l_value are updated even after my breakout time of 9:30 is elapsed, please help me on this from any of members ,it would be great help.
  • Imran
    hii @samphel
    if you are a beginner in algo trading then this series can help you...
    also, I have shown how to make full orb code in some last lectures.

    https://kite.trade/forum/discussion/6216/python-and-kiteconnect-series#latest
  • samphel
    hi @Imran i have gone through your video and i'm not able to find my answer in that , as you can see above i need to save high and low value of first 15 minute in a variable then only i can further go for 15 minute orb strategy , but in your videos it simply compared ltp of current high and low.
  • tahseen
    @samphel First of all this program is a total mess. You don't do so much condition inside on_tick. Anyway I wouldn't want to get into that detail

    To answer your question, I need to see indented python code as you can see your code copy paste has lost indentation. I don't know what is within if and what is not. And I don't want to make a guess to answer your question
  • samphel
    @tahseen here is the code:



  • Jigpylab
    If you have to run the strategy at specific time and strategy is only based on OHLC data, you don't need tick data. Just get kite.ohlc(symbol), which is more stable to get. Let me know if you need more help.
  • tahseen
    Although I really wonder why wrote all this if you really want to just work on ORB. But let me tell you the issue is that your first if condition would always keep working as it is >= start time

    And worst your second condition is a == for an exact match, where crt_time would be in Hour and Minute, Seconds, Milli Second whereas your breakout time is in Hour and Minute. It would never match

    I think you should learn proper python before delving into Algorithmic trading. Take my honest advice, your style of coding is very bad. Even if you are the best of trader, wrong coding would wipe you out
  • Amit Ghosh
    Amit Ghosh edited December 2019
    Slight contribution -

    def pcl15min(token):
    # starting_date="2019-12-01"
    # ending_date="2020-01-01"
    starting_date=datetime.datetime.now().strftime("%Y-%m-%d")
    ending_date=(datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    time_period="15minute"
    zap=kite.historical_data(token,starting_date,ending_date,time_period,0)
    zapp=pd.DataFrame(zap).tail(10)
    print(zapp)
    #print(zapp.iloc[-1,-1])
    return zapp.iloc[-2,-3] #Selects 3rd Last Column and 2nd Last Row i.e. low of Previous 15m candle
    #print(zapp.iloc[-2,-4]) #Selects High of Previous 15m candle

    token=get_insToken("INFY","NSE")
    pcl15min(token)

    Please check indentation.
  • tahseen
    Too much ===> datetime.datetime.now().strftime("%Y-%m-%d")

    Simpler ===> str( datetime.date.today() )
  • samphel
    @tahseen thankyou you have pointed out my flaw now i'm going through python course again it looks like i'm doing something terrible wrong smile :smile:
Sign In or Register to comment.