twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.2k stars 1.01k forks source link

How to use or implement Opening Range Breakout Strategy with EMA in real market #138

Open rahulmr opened 3 years ago

rahulmr commented 3 years ago

Which version are you running? The lastest version is on Github. Pip is for major releases.

import pandas_ta as ta
print(ta.version)

0.1.97b

Upgrade.

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Is your feature request related to a problem? Please describe. Not exactly. I want to understand how to use pandas_ta for opening range or time range breakout strategies in real market. Is it only for technical analysis or can be used in real market.

Describe the solution you'd like Some example in real market suppose I want to use it for Indian broker like Zerodha/AliceBlue/Samco who provide API and tick data. I want to create a simple opening range breakout strategy for first 15min or 30min or 1hr time. But at the same time want to check whether LTP is above EMA 21, how can I do it using pandas_ta? How to create this custom strategy and where I should use the broker API when I get a signal. I saw the jupyter notebooks there is too much information and it confuses me a lot. Simple examples can be helpful.

Thanks for using Pandas TA!

twopirllc commented 3 years ago

Hello @rahulmr,

Upgrade

You have not upgraded. Here is how to upgrade:

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Questions

Is it only for technical analysis or can be used in real market.

Pandas TA is used to calculate indicators and can be used in a real market. Pandas TA is one component of an Algo Trading system. It is up to the user to implement their Algo Trading components: Broker, Order Execution, TA, Backtesting/Forward Testing, et al or modify an existing Algo System to fit your needs.

I saw the jupyter notebooks there is too much information and it confuses me a lot.

I'm sorry to hear that it is confusing for you. Would you like to submit a simpler example Notebook that would be helpful for you and other users that may also find them confusing? If so, that would be greatly appreciated.

ORBx Pseudo Algo

I recommend reading How to utilize this Framework for live ticks Issue #87. Note: It doesn't matter that it mentions ticks, it can be applied to minutes or 5 minutes et al. Much of the logic is already written in that post however you will not need to use the ta.strategy() method since your only TA is an ema. If you are using more than ema, then I recommend using ta.strategy() as it is designed to handle bulk indicator processing by utilizing mulitprocessing.

Also please take some time and review some of the other repositories that you have forked regarding Breakout Strategies. There may be some good info to include as well.

You should be testing on a Paper Trading Account like your Broker or simulate it yourself.

Here is some pseudocode for ORBx, where x is the number of minutes:

  1. Init some stuff
    1. Set ORBx_high and ORBx_low to NaN
    2. Init a Signal Series to 0. Use 1 buy, 0 nothing, -1 sell
  2. Prepare Historical Data (1 min)
    1. Get historical data as DataFrame df
    2. Calculate EMA21 by df.ta.ema(length=21, append=True)
  3. Process Live Data (1 min)
    1. While Live Data
      • Calc TA: df.ta.ema(length=21, append=True)
      • If current time != x, pass. Otherwise, update ORBx_high and ORBx_low.
      • If LTP >= x or LTP <= x, create a buy or sell signal respectively in the Signal Series (for post analysis)
    2. If buy/sell signal, handle trade execution
      • Execute entry/exits
      • Set stops if you use them
      • Store trade information
    3. Update historical DataFrame df
      • Store it however you wish
  4. Intraday Complete
    1. Verify Data
    2. Post analysis

If you would like for me to do it, then we can discuss it offline. However if you would like some assistance with this, then you will have to do the coding above and share it here so I can help. Or you can create a Jupyter Notebook and make a Pull Request and we can do it together that way and help everyone using the library.

Hope this helps!

Thanks, KJ

twopirllc commented 3 years ago

Hello @rahulmr,

I do not know how to help you if you do not participate in the discussion. As such, this Issue will be closed in a few days due to inactivity.

Best Regards, KJ

rahulmr commented 3 years ago

@twopirllc Hey sorry was busy with some office work. Thanks for the detailed explanation. I will try it out. Sometimes too much is also confusing so asked for helped. Once I am done with the code I will get back to you. Thanks for creating a good module. The TA-Lib installation itself was too hectic.

rahulmr commented 3 years ago

And yes I want to do it for commodity - Natural Gas futures only. I will not try on too many stocks at a time.

rahulmr commented 3 years ago

@twopirllc Just one suggestion if you find it good. Like many people who start coding in python for algo trading and then look for strategies and backtesting, I also keep looking for some existing python code for strategies. Many people are interested in indicators but when I see lot of youtube videos they always say that Price Action yields more money from trading rather than using lagging technical indicators so that is the reason I wanted more examples of that sort.

Small examples with minimal code and which are simple. For example, say an Inside Bar strategy, it is very simple but has little variations.

  1. Previous Candle is bigger than current candle and if price goes below low of current then sell
  2. Above case but price goes above high of current candle then buy signal.
  3. Above 2 cases but want to get signals based on previous candles like say crosses above high of previous candle or below low of previous candle.
  4. Engulfing candles are also promising.

I found that for NaturalGas in MCX India the Inside Bar with target of 1 gives good profit. So will be trying that. But as I said with small examples it could be great (even converting TradingView most common scripts) May be you can ask for pull requests in the project readme. No need of Jupyter Notebook always, it can be python script also.

twopirllc commented 3 years ago

Hello @rahulmr,

The TA-Lib installation itself was too hectic.

Yes, this is one of the primary reasons why I made Pandas TA. In some cases TA Lib is hard to install and it's unfortunately development stalled.

And yes I want to do it for commodity - Natural Gas futures only. I will not try on too many stocks at a time.

Ok. Seems reasonable.

Many people are interested in indicators but when I see lot of youtube videos they always say that Price Action yields more money from trading rather than using lagging technical indicators so that is the reason I wanted more examples of that sort.

If we are considering Intraday, then maybe Price Action yields more money. Are these Day Trading Videos? Have any links? Are they using any indicators? I am curious how they understand what indicators are and what they are promoting. Do they have proof and Third Party audited results?

If we are considering Swing and Positional Trading, I highly doubt Price Action yields more money is always true. For daily, weekly or monthly time frames, everyone is using them in some capacity even if it just one additional non-constant feature calculated from the initial data set. Often quoted in American Financial Media are the following indicators, SMA10, SMA20, SMA50, SMA200, and Vol SMA20 and act as a "baseline".

In the case of the Inside Bar Strategy, the general code is similar to the AI Example Notebook. However we will need to change the long variable to it's Inside Bar Long Logic prior to generating the trend, it's trend return and trading signals as the rest of the Notebook shows.

Screen Shot 2020-10-09 at 1 49 40 PM

Furthermore, in my opinion, there is some false narratives out there regarding indicators. But that's a different discussion altogether.

But as I said with small examples it could be great (even converting TradingView most common scripts)

I have converted several popular indicators from TradingView already, including a few of my own. For instance Lazybear's Squeeze (one of the most popular TV indicators) is in the Example Notebook. You likely will not find these indicators anywhere else for the time being in Python Open Source Libraries.

I found that for NatGas in MCX India the Inside Bar with target of 1 gives good profit. So will be trying that.

Awesome! Can you get some of it built? Even if it's only acquiring data and getting a live stream running, that is ok. Create it in the examples directory. Submit a Pull Request and I'll merge it into the development branch and we can go from there.

May be you can ask for pull requests in the project readme.

How come? Some come submit PRs and usually I add more indicators from multiple sources. Plus I have other features that I am trying to add to Pandas TA like stochastic generators, backtesting and risk analysis to start. Plenty to do with my limited free time.

No need of Jupyter Notebook always, it can be python script also.

You are right, there is no need to always use a Jupyter Notebook. However in many cases, visualization of the indicators carry more weight. Once someone is comfortable with the indicator visualization, it is easy to later include it in it's own module.

In fact I have already done so in the Examples directory with the watchlist module. The watchlist module's use is shown in both PandasTA Strategy Examples and AIExample. Users are welcome to copy and modify the watchlist module to be modified to include your own datasource/broker to download the data and generate indicator results with a Pandas Strategy. Contributions are welcome to the watchlist module and potentially grow into it's own Open Source package.

Regards, KJ

rahulmr commented 3 years ago

@twopirllc Thanks I am loving the discussion. I will work on the points you have suggested.

twopirllc commented 3 years ago

Hello @rahulmr,

Great!

Also, I recently came across this blog post. The Traders Guide to Mythological Constructs - Paracurve. A lot of good stuff to digest when you have some time.

Regards, KJ

rahulmr commented 3 years ago

@twopirllc Check this jupyter notebook. I am getting this kind of data which I want to store in 1/5/10/15 minute(s) dataframe and check the various indicators and candlestick patterns from pandas_ta Not sure how can I generate this for a streaming data above without interrupting the ticker thread. Running strategy for day trading. I will share more code as I progress.

twopirllc commented 3 years ago

@rahulmr,

Cool. I will check it out as soon as I can.

One thing I noticed is that you shared your access_token. I would fix it ASAP by installing python-dotenv to use a .env file to load your access_token so others do not have access to it.

KJ

rahulmr commented 3 years ago

I also got a way to get streaming data from TV - check it here with crypto example and valid for some NSE stocks as well.

rahulmr commented 3 years ago

@rahulmr,

Cool. I will check it out as soon as I can.

One thing I noticed is that you shared your access_token. I would fix it ASAP by installing python-dotenv to use a .env file to load your access_token so others do not have access to it.

KJ

Don't worry about the access token so much. It will expire someday. And it is only for streaming data and not for order placement.

rahulmr commented 3 years ago

@twopirllc I was trying to run the below code on a dataframe

df.ta.strategy("Momentum")
print(df)

It goes into infinite loop and consumes all CPU on my windows 10 laptop. Then I need to kill all VSCodium and python exes. Could you please help?

twopirllc commented 3 years ago

Hello @rahulmr,

Let's handle this as a separate Issue #176.

Thanks, KJ

rahulmr commented 3 years ago

So finally I started working on this long pending stuff. Here is my code

import yfinance as yf
import pandas_ta as ta

import schedule

sbin = yf.Ticker("SBIN.NS")

def place_order(order_type):
    print(f"Place {order_type} order")

def run_strategy():
    df = sbin.history(period="3d", interval="15m")
    print(df)
    df.ta.cdl_inside(append=True)
    df.ta.vwap(append=True)
    print(df)
    condition = df.iloc[-1]['CDL_INSIDE'] == 1 and df.iloc[-1]['VWAP'] > df.iloc[-1]['close']
    if condition:
        place_order('buy')

# This is expected to run every 15 minutes (some 10-15 seconds like 9:30:10 AM IST) 
# Yfinance provides data with some delay - broker data will not have that delay
schedule.every(interval=15).minutes.do(run_strategy)

# df = sbin.history(period="3d", interval="15m")
# print(df)
# df.ta.cdl_inside(append=True)
# df.ta.vwap(append=True)
# print(df.iloc[-1]['CDL_INSIDE'])
# print(df.iloc[-1]['VWAP'])
# ## TODO Add EMA 7 and EMA 21 , can we also get Pivot Points Fibonacci ?
## I also have thoughts on how can we create a simulation of previous days
# Like getting data by adding 15 minutes to it. That is another part though.
# creating a watchlist of Indian Stocks and applying this.
rahulmr commented 3 years ago

So finally I started working on this long pending stuff. Here is my code

import yfinance as yf
import pandas_ta as ta

import schedule

sbin = yf.Ticker("SBIN.NS")

def place_order(order_type):
    print(f"Place {order_type} order")

def run_strategy():
    df = sbin.history(period="3d", interval="15m")
    print(df)
    df.ta.cdl_inside(append=True)
    df.ta.vwap(append=True)
    print(df)
    condition = df.iloc[-1]['CDL_INSIDE'] == 1 and df.iloc[-1]['VWAP'] > df.iloc[-1]['close']
    if condition:
        place_order('buy')

# This is expected to run every 15 minutes (some 10-15 seconds like 9:30:10 AM IST) 
# Yfinance provides data with some delay - broker data will not have that delay
schedule.every(interval=15).minutes.do(run_strategy)

# df = sbin.history(period="3d", interval="15m")
# print(df)
# df.ta.cdl_inside(append=True)
# df.ta.vwap(append=True)
# print(df.iloc[-1]['CDL_INSIDE'])
# print(df.iloc[-1]['VWAP'])
# ## TODO Add EMA 7 and EMA 21 , can we also get Pivot Points Fibonacci ?
## I also have thoughts on how can we create a simulation of previous days
# Like getting data by adding 15 minutes to it. That is another part though.
# creating a watchlist of Indian Stocks and applying this.

@twopirllc Do you think this can be implemented? Whenever you get time, please look into the code. Every 15 minute a new candle will be added and indicators will be applied.

Entry condition => Current candle is Inside Candle (child candle) and VWAP greater than close of current candle. Buy price => High of Child Candle + buffer Current candle is Inside Candle (child candle) and VWAP less than close of current candle. Sell side price => Low of Child Candle - buffer

Exit Condition => When price (this can be ltp coming from websocket) is twice the length of child candle

Stop Loss => Buy Side => Low of Child Candle - buffer Sell Side => High of Child Candle + buffer

So my query is how we can take help of pandas_ta in coding this strategy.

twopirllc commented 3 years ago

@rahulmr,

I will take a gander as soon as I can and answer the rest of the sub questions then.

Regards, KJ

rahulmr commented 3 years ago

Image of Strategy image

rahulmr commented 3 years ago

Better image::

image

twopirllc commented 3 years ago

@rahulmr

Thanks for the screenshots. Just letting you know I haven't forgotten and not ignoring the issue. I have some personal stuff to take care of and can hopefully get back to helping you soon.

Thanks KJ

rahulmr commented 3 years ago

@twopirllc No worry no hurry. When you get time please update. And thanks for all the discussion. Actually there are many challenges when you do algo trading using python or any other language. Following are some which come to my mind.

  1. How to better utilize API limits? Every broker has Limits per second or per minute for calling APIs
  2. Continuously run script in the background
  3. Start a script at particular time.
  4. Error handling.
  5. Squaring off all Intraday positions before auto square off time.
  6. How to use websocket streaming data.

This module seems quite helpful. I will code more and share with you.

twopirllc commented 3 years ago

Hello @rahulmr,

Just came across a MT5 Algo Trading System. It covers the basics pretty well with additional functionality. It may address some of your questions.

KJ

twopirllc commented 3 years ago

Hello @rahulmr,

Just came across hackingthemarkets: Fullstack Trading App repo. It contains an ORB Strategy (backtest.py) but using Alpaca API (import alpaca_trade_api) for trade execution. It may be a good starting point to decompose and replace Alpaca with your broker. Of course you will need to forward test on a paper/demo account and adjust it for live execution.

Kind Regards, KJ

rahulmr commented 3 years ago

Hello @rahulmr,

Just came across hackingthemarkets: Fullstack Trading App repo. It contains an ORB Strategy (backtest.py) but using Alpaca API (import alpaca_trade_api) for trade execution. It may be a good starting point to decompose and replace Alpaca with your broker. Of course you will need to forward test on a paper/demo account and adjust it for live execution.

Kind Regards, KJ

Thank I am already working on it. @hackingthemarkets Larry is too good.