Unfortunately, no trades are actually executed. Anyone have an idea how to do this?
Code: Select all
Inputs:
BegDate(1210101),
EndDate(1231231),
RsiLength(14),
CandlesPerSession(6); // Number of candles per session
Vars:
RsiValue(0),
LongEntry(False),
ShortEntry(False),
BarsSinceEntry(0),
SessionCounter(0),
BarsSinceSession(0),
SessionStartBar(0),
TradingDaysInMonth(0),
CurrentTradingDay(0);
if DateToJulian(Date) >= BegDate and DateToJulian(Date) <= EndDate then begin
RsiValue = RSI(Close, RsiLength);
// Count the number of trading days in the month
if Month(Date) <> Month(Date[1]) then begin
TradingDaysInMonth = 1;
CurrentTradingDay = 1;
end
else if DayOfWeek(Date) <> DayOfWeek(Date[1]) then begin
TradingDaysInMonth = TradingDaysInMonth + 1;
CurrentTradingDay = CurrentTradingDay + 1;
end;
// Calculate the starting bar of each session
SessionStartBar = (CurrentTradingDay - 1) * CandlesPerSession * 4 + 1;
// Check if the session has started
if BarNumber >= SessionStartBar then
BarsSinceSession = BarNumber - SessionStartBar + 1;
// Long entry condition: RSI crosses above zero
LongEntry = crosses_above(RsiValue, 0);
// Short entry condition: RSI crosses below zero
ShortEntry = crosses_below(RsiValue, 0);
if LongEntry then begin
Buy next bar at market;
BarsSinceEntry = 0;
end
else if ShortEntry then begin
SellShort next bar at market;
BarsSinceEntry = 0;
end
else begin
BarsSinceEntry = BarsSinceEntry + 1;
if BarsSinceSession >= CandlesPerSession * 4 then begin
BarsSinceSession = 0;
if MarketPosition = 1 then
Sell next bar at market
else if MarketPosition = -1 then
BuyToCover next bar at market;
end;
end;
end;