% Simple momentum strategy pseudocode - Roberto Maria Caloi - 2022 % For educational purposes only. OnStart: S.lambda = 0.94; %Initialize strategy's parameter S.mmp = S.p; %Initialize exponential moving average of price updates S.q = 0; %Initialize portfolio position S.CF = 0; %Initialize portfolio Cash Flow S.p = S.getCurrentPrice(now); %Initialize current instrument price OnPriceUpdate (newPrice): S.PL = S.CF + S.q * newPrice; %Update Profit And Loss (P&L) S.p = newPrice; %Update Current Price S.mmp = lambda * S.mmp + (1-lambda) * S.p; %Update Exp Moving Average %Check Buy/Sell Condition if S.p > S.mmp S.buy(1-S.q, newPrice); % Take a long position of quantity 1 at price newPrice S.q=1; % Assume immediate execution at newPrice (!) without fees S.CF = S.CF-(1-S.q)*newPrice; % Update Cash Flow else S.sell(1+S.q, newPrice); % Tale a short position for quantity 1 at price newPrice S.q=-1; % Assume immediate execution at newPrice(!) without fees S.CF = S.CF+(1+S.q)*newPrice; % Update Cash Flow end if checkfilters() == false % Security checks failed (!) callStopStrategy(); % Stop trading end OnStop: % Close your position (buy or sell) if S.q==1 S.sell(1,S.p); S.q = 0; S.PL = S.CF + S.p; elseif S.q==-1 S.buy(1, S.p); S.q = 0; S.PL = S.CF - S.p; end