I am desperately looking for a solution, how to code the Exit rules (Sell or Cover) in the following trading system. I could imagine that the solution will be a simple loop, but unfortunately I have no clue how to code loops. I just don´t get it. I need to mention, that in the following system, “Close” is not the price of the stock in the chartwindow, but represents a whole “basket of stocks”, that was calculated by getting the quotes from other stocks through the function foreign(“”,”C”). So basically the system trades a selfmade stockindex. I guess that might be the reason why function Equity(1,0) did not help here.
System and Buy/Short rules:
The basic idea of the system is, that the system simply opens a Buy or a Short position
(only one position per Tradingsession), after the price of the stockindex (Close) has moved in one direction to a certain extend (EntryLevel_up or EntryLevel_dn). Important is, that the first (!) movement above or below the EntryLevel (Buy or Short) represents
a final decision. Every other Buy or Short signal, that occurs after the first signal while beeing in the same tradingsession (here starting at 8 a.m. and ends after 12 hours), has to be ignored through function: (Entry_all = ExRem(Entry_all,StartUS). The way I coded it seems to work for Buy and Short well.
My question is how to code the Sell/Cover exit:
The system seems to remember the first Entrysignal, but now needs to control three possible exit scenarios:
#1 The exit (Sell or Cover) should happen, after the stockindex moved further to
a defined target level (above ExitLevel_up for Buy position or below ExitLevel_dn
in case of a Short position), or
#2 when the stockindex moved at least at the end (!) of the Tradingsession (LastExit)
above the Entrylevel (EntryLevel_up for Buy position) or below the Entrylevel
(EntryLevel_dn for Short position, because exit #1 was never reached, or
#3 when #1 and #2 did not happen. This means that the Exitlevels where never reached
while being in the Tradingsession and the stockindex moved below the former
Entrylevel (EntryLevel_up for Buy position) or above the former Entrylevel
(EntryLevel_dn for Short position) at the end of the Tradingsession (LatestExit).
#1 represents the main goal to exit at a certain level, #2 is at least still profitable and #3 represents exit at a loss.
// Session begin and end in an hourly chart
TradingSessionStart = Hour()==8 AND Minute()==0;
LatestExit = Ref(TradingSessionStart,-12);
SessionBar = BarsSince();
LastExitBar = ValueWhen(LatestExit,SessionBar,1);
// Buy and Short Entry Rules
Entry_up = Close>EntryLevel_up AND SessionBar<=LastExitBar;
Entry_dn = Close<EntryLevel_dn AND SessionBar<=LastExitBar;
Entry_all = Entry_up OR Entry_dn;
Entry_all = ExRem(Entry_all,StartUS);
Buy = TradingSessionStart==0 AND Entry_up AND Entry_all;
Short = TradingSessionStart ==0 AND Entry_dn AND Entry_all;
// Plotting the Signals in the chart
// Now how should I code the Sell and Cover rules ????
A thousand thanks for any help in advance !!!!!!