Hello there:
I am writing a code in order to trade high volatile stocks (since algorithms are much faster than humans I think… this is the best way to do it)… Now some of the Highly volatile US stocks can have huge returns, (specially concerning the very first minutes since the market is open ), consider for example Ticker : DUO (Fangdd Network Group Ltd) today (march 16th 2022), This morning it has opned aroung 0.43 and has gone as high as 0.53s (more than 20%) in less than half hour.
So in order to trade stocks like this one… I am writing a code, I intent to use 1 minute candle and get into the trade when there are higher lows from 3 consecutive candles, I intend to send a limit order at the price specified by a linear regression on the lows buying the equivalent of 100 shares at that price.
I also intent to use fetch the price and compare it to a 96 % of the entry (4% risk level) in order to send a market order to get out the trade if the trend doesn't continue.
The code I am writing for this is the following:
import numpy as np
import pandas as pd
import datetime
from datetime import datetime
import pytz
import time
import os
import sys
sys.path.append('D:\Trading\Quantitative Trading\IbridgePy\Strategies')
from linear_forecast import linear_forecast
import talib as tb
def initialize(context):
context.flag = False
context.flag1 = False
ticker = 'DUO'
context.security = superSymbol(secType='STK', symbol=ticker, currency='USD', exchange='SMART')
context.ammount = 100
def handle_data(context, data):
tz_NY = pytz.timezone('America/New_York')
datetime_NY = datetime.now(tz_NY)
# Here we put the program so thet it executes only in the first hour after the market opened
if datetime_NY.strftime("%H") == '09' and datetime_NY.strftime("%M") >= '30' :
hist_minute = request_historical_data(context.security, '1 min', '200 S')
hist_daily = request_historical_data(context.security, '1 day', '1 D')
Op = hist_daily["low"][0] # Open of the day
L0 = hist_minute["low"][0] # Low of the first minute
L1 = hist_minute["low"][1] # Low of the second minute
L2 = hist_minute["low"][2] # Low of the third minute
if (data.current(context.security,'price') > Op) and (L0 < L1) and (L1 < L2) and context.flag1 == False :
entry = linear_forecast([L0, L1, L2])
order(context.security, round (context.ammount/entry) , LimitOrder(limit_price=entry)
context.flag1 = True
if (context.flag==False and position.amount != 0):
if data.current(context.security,'price') <= 0.96*(entry):
order(context.security,-1*position.amount , style=MarketOrder(), outsideRth=True)
context.flag = True
end()
There is an outside script called linear_forecast, this is the script:
import numpy as np
import pandas as pd
import datetime
from datetime import datetime
import pytz
import time
import os
from scipy import stats
This method provides a linear forecast of a numpy arrays, it returns
the forecast using linear regression model
def linear_forecast(y):
# number of observations/points
x = np.arange(len(y))
n = (len(x))
# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(yx) - nm_ym_x
SS_xx = np.sum(xx) - nm_xm_x
# calculating regression coefficients
slope = SS_xy / SS_xx
intercept = m_y - (slope)m_x
forecast = float (len(y)(slope) + intercept)
return (forecast)
of course I need to continue writing more lines of code to have a criteria to take profits, but
What do you think about this code?? could that be used well in order to do what I intend to do??
Is it good, it is bad?? why??
Thanks