Commonly, stochastic control is the basis for optimal trading (either in execution or market-making). Has any research been done (or why not, if none) as to PID controllers for these applications?
1 Answer
Check the code below for basic logic. You may need to improvise it (may be).Just a quick and dirty work. In case it works, reshare. Enjoy !!
How PID controller works - a crash (fast fast) course ?
- PID controller works on Error to minimise error !! Wow, that is mouthful. Error(e) = SetPoint (SP) - ProcessValue (PV). This is the most basic thing.
- Now there are three terms in PID controller equation which are actually multipliers
-
- kp - this dictates how much the output change with each unit change if error (repeat error).
-
- Kd - Tricky part starts from here. Kd is a multiple also but it is multiple of rate of change of error i.e. how much error is changing wrt time -3. Ki - Ki is also multiple but it is multiple of a term which has past memory of error i.e. Ki multiples summation of error.
Now you see...Kp dicatats how much you output changes wrt to difference between setpoint and actual value i.e. proprotional part , Kd dicatact how the rate of change of error affect the output, ki dictate how much error is minimised. Suppose Setpoint and output becomes same then error is zeror, there is no rate of change of error and summation of error is also zero.
//@version=5
indicator("PID Controller", overlay=false)
// Input Variables
lookback = input.int(title="Lookback Period", defval=20, minval=1)
kp = input.float(title="Kp", defval=0.1, minval=0)
kd = input.float(title="Kd", defval=0.1, minval=0)
ki = input.float(title="Ki", defval=0.1, minval=0)
price_src = input(close, title="Price Source")
// Variables
var float error = 0.0
var float error_sum = 0.0
var float error_diff = 0.0
var float pid = 0.0
// Arrays
var float[] pid_array = array.new_float(0)
// Loop
for i = 0 to 10
// Calculate error and PID
error := price_src - ta.sma(price_src, lookback)
error_sum := error_sum + error
error_diff := error - nz(error[1])
pid := kperror + kierror_sum + kd*error_diff
// Add PID value to array
array.push(pid_array, pid)
// Wait for next bar
//bar_wait(0)
// Calculate average PID value
var pid_sum = 0.0
for i = 0 to array.size(pid_array)-1
pid_sum := pid_sum + array.get(pid_array, i)
var float pid_avg = pid_sum / array.size(pid_array)
// Plotting
plot(pid_avg, color=color.green, linewidth=1, title="PID")
-
1$\begingroup$ This is just a general implementation of PID (available in any Control book) it has nothing to do with trading or marketmaking, which was the subject of the question. And the question asked for research results, of which AFAIK there are not any published for these applications. $\endgroup$– nbbo2Commented Apr 19, 2023 at 7:14
-
1$\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$– Community BotCommented Apr 20, 2023 at 23:03
-
-
$\begingroup$ Edited the answer and because gentleman wanted one, code is given. Please note that PID control implementation in context of trading would be more of trailing stop loss and there are good algorithms for trailing. So rather than going for PID code in which you want to control some outcome but which you can not in context of stock market, hence try using stop loss algorithm. One such could be Chandelier exit or use indicator such as Garman-Klass-Yang-Zhang Historical Volatility with Kaufman's Adaptive Moving Average (Trading view indicator name: JFD-Adaptive, GKYZ-Filtered KAMA [Loxx]).. $\endgroup$ Commented Apr 27, 2023 at 2:17