Financial optimization with compound interest rate Suppose that I have $P$ amount of money in a bank that accrues fixed interest rate $\bar{r}$ continuously. This interest rate charged on this bank account is simple, instead of compound interest rate.
Now, I can transfer the interest accrued after time $t$, $\bar{r}Pt$, to another bank account that collects compound interest rate with the same interest rate $\bar{r}$. However, I can only transfer the interest earned from the first bank account, instead of transferring the whole $P$. Every time I transfer the interest from the first bank account to second bank account, I incur a fixed cost $\phi$. I can transfer the accrued interest from the first bank to the second bank at any time $t$, and for as many times as I want to.
Q: What is the optimal strategy function $f$ that takes in $(P,\bar{r}, \phi, T)$ and tells me when I should transfer the interest accrued from the first bank to the second bank to maximize my income at time $T$, starting from time $t = 0$? Is there even a closed form solution to this problem?
This is motivated by a real-world example I witnessed, and wanted to know how I can tackle this question. It seems that I need to wait for an ample amount of time for the interest collected to be high enough that I would profit from moving to the second bank despite the fixed cost $\phi$. If I wait too long, then I may miss out on the opportunity that interest collected could have earned more compound interest.
EDIT: Note that there is a finite amount of time, since a comment suggested that there is no solution given infinite amount of time.
 A: Let's say that you have the principal of $P=1$ at the first bank and $r=1$ (amount and time scaling is trivial) and transfer at the time moments $t_1<\dots<t_n<T$. Then your gain compared to doing nothing is (we set $t_0=0$)
$$
\sum_{j=1}^n (t_j-t_{j-1})[e^{T-t_j}-1]-\varphi n=e^T\sum_{j=1}^n(t_j-t_{j-1})e^{-t_j}-t_n-\varphi n\,.
$$
For fixed $n$ and $t_n$, we can now fix $t_{j-1}, t_{j+1}$ and look at $t_j$. This leads to the maximization problem $(s-t)e^{-s}+te^{-t}\to\max$, $0\le t\le s$. Differentiation yields $-e^{-s}+e^{-t}-te^{-t}=0$, so $t=1-e^{-(s-t)}$. This gives the relation $\Delta_{j+1}=\log \frac1{1-\Delta_j}$ between $\Delta_j=t_{j}-t_{j-1}$. Thus we reduced the problem to a two-parameter one. We need to choose an integer $n$ and the initial $\Delta=\Delta_1$ and then run this recursive sequence and compute and maximize the result $F_n(\Delta)$ in $n$ and $\Delta$ in the admissible range.
Thus it is clear that the closed form expression is out of question, but the programming is relatively easy. It looks like for each $n$, $F_n$ is unimodal in $\Delta$ in its domain and the maximum of $F_n$ is a unimodal function of $n$. If so, the computation of the maximum can be done rather simply and efficiently, but I'll not attempt to prove these properties now.
Here is a code in Asymptote (the syntax is almost the same as C but somewhat more flexible and it allows you to draw pictures easily, so I use it most of the time). It outputs the optimal schedule and the gain from each transaction as well as the total gain.
import graph;
size(300,300,IgnoreAspect);

real 
rate=0.03,       // your annual rate r 
Tyears=30,       // your time period in years
penalty=100,     //transfer fee 
principal=10000;  //the principal P

real T=Tyears*rate, phi=penalty/principal;

real f(int n,real d, bool b=false)
{
real s=0;
real dl=d, t=0;
for(int k=0;k<n;++k)
{
t+=dl; s+=dl*exp(-t); if(b) write(t/rate, ((exp(T-t)-1)*dl-phi)*principal);
if((k<n-1 && dl>=1)||t>T) {s=0; break;}
else if(k<n-1) dl=-log(1-dl);
}
if(b) write("total gain="+string((exp(T)*s-t-phi*n)*principal));
return max(exp(T)*s-t,0)-phi*n;

}

real[] F(int n)
{
real[] q;

real u=0,v=0.0001,w=1; 
real M=f(n,v);
for(int k=0;k<30;++k)
{
real uv=(u+v)/2, vw=(v+w)/2;
real M1=f(n,uv); 
if(M1>M) {M=M1; w=v;v=uv;} else
{
real M2=f(n,vw);
if(M2>M) {M=M2; u=v;v=vw;} else
{u=uv; w=vw;}
}
}
q[0]=v; q[1]=f(n,v);
return q;
}
 
real M=0,d;
int N;

for(int n=1;n<100;++n) 
{
real g(real x){return f(n,x);}
//draw(graph(g,0,1));
real[] q=F(n);
//dot((q[0],q[1]),red);
//write(n,q[0],q[1]);
if(q[1]>M){N=n; M=q[1]; d=q[0];}
}
write("schedule "+string(N));
f(N,d,true);

pause();

A: Let's start with the case where $ \phi=0 $. No matter the choice of $ \overline{r} $ or $ T $, you are best off withdrawing continually. You're getting $ P\overline{r}t $ from the simple interest. Continually transferring it to your continuously compounding account will effectively set that account's interest at $ P\overline{r}t $, so you'll be getting $ P\overline{r}te^{\overline{r}t} $, and nothing from the simple interest account. Let's call the strategy of continual transfer $ B $ and the strategy of no transfer $ A $. So at time $ t $, $ B = P + P\overline{r}te^{\overline{r}t} $ (the first term is your principal on the simple interest account), and $ A = P+P\overline{r}t $. Assume $ B $ outperforms $ A $. Then, $ P + P\overline{r}te^{\overline{r}t} > P+P\overline{r}t $. This reduces to $ e^{\overline{r}t} > 1 $, which is true $ \forall t>0$, regardless of choice of $ r $.
Let's move on to the case where you are allowed the option to withdraw once, at time $ 0 < t_1 < T $. The first inequality is strict because the simple interest account has generated no interest yet. The last inequality is strict because withdrawing at T cannot possibly yield more than withdrawing the moment before (you'd just be losing your premium $ \phi $). Let's call $ B $ the strategy where you withdraw. At time $ T $, $ B = P\overline{r}(T-t_1)+P\overline{r}t_1e^{\overline{r}(T-t_1)} - \phi $, and the strategy of no withdraw $ A = P\overline{r}T $. Assume $ B $ outperforms $ A $, that is $ B > A $. So $ P\overline{r}(T-t_1)+P\overline{r}t_1e^{\overline{r}(T-t_1)} - \phi > P\overline{r}T $. Distribute on the $ \textit{l.h.s.} $ to get $ P\overline{r}T-P\overline{r}t_1+P\overline{r}t_1e^{\overline{r}(T-t_1)} - \phi > P\overline{r}T $. Subtract $ P\overline{r}T $ from both sides to get $ -P\overline{r}t_1+P\overline{r}t_1e^{\overline{r}(T-t_1)} - \phi > 0 $. Add $ \phi $ to both sides and factor out a $ -P\overline{r}t_1 $ from the $ \textit{l.h.s.} $ to get $ -P\overline{r}t_1(1-e^{\overline{r}(T-t_1)}) > \phi $. This is your condition for making profit allowed one withdrawal. For a chosen $ \phi $, you should withdraw at $ t_1 $. If solving for $ t_1 $ gives a value out of bounds (i.e., greater than $ T $), do not withdraw. It is only more profitable if the statement holds true. Solve for $ t_1 $ by numerically maximizing the difference in the inequality (I've added an example in the link below).
Let's consider the case where you are allowed to withdraw exactly twice, at times $ 0 < t_1 < t_2 < T $. If you choose this option, at time $ T $ you will have $ B = P\overline{r}(T-t_2)+P\overline{r}t_1e^{\overline{r}(t_2-t_1)} + P\overline{r}t_2e^{\overline{r}(T-t_2)} - 2\phi $, whereas if you choose to not withdraw, you will have $ A = P\overline{r}T $, the zero-withdrawal strategy. Assume $ B $ outperforms $ A $. That is, $ P\overline{r}(T-t_2)+P\overline{r}t_1e^{\overline{r}(t_2-t_1)} + P\overline{r}t_2e^{\overline{r}(T-t_2)} - 2\phi > P\overline{r}T $. Solve for $ (t_1, t_2) $ by numerically maximizing the difference in the inequality. That is, $ \underset{(t_1,t_2)}{\mathrm{argmax}}  -P\overline{r}t_2+P\overline{r}t_1e^{\overline{r}(t_2-t_1)} + P\overline{r}t_2e^{\overline{r}(T-t_2)} - 2\phi $ . If it is out of bounds (i.e., $ 0 < t_1 < t_2 < T $ is not true), then you do not withdraw two times.
Continue on in this way until you reach a $ t_{n+1} $ that is out of bounds. Compare the profits under each of the cases: 0 withdrawals, 1 withdrawal, ... , n withdrawals, with the times you found. Then you withdraw at the times that give the highest profit.
Here is an example of numerically optimizing the withdrawal time in the one-withdrawal case, written in a short Python Jupyter Notebook.
https://colab.research.google.com/drive/18nAQLOUhrNxNEnv-U5-NCb3XGHAZ7uTc?usp=sharing
