Australian (ASX) Stock Market Forum

Amibroker coding question - desperately looking for help

Joined
4 September 2011
Posts
4
Reactions
0
Hello all,

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

Plot(1,"",IIf(Buy,colorBlue,IIf(Short,colorOrange,
colorBlack)),styleOwnScale|styleArea|styleNoLabel,0,100);

// Now how should I code the Sell and Cover rules ????

A thousand thanks for any help in advance !!!!!!

Kind regards
Robert
 
Sorry, here still a correction of one coding line :)

//Correct
Entry_all = ExRem(Entry_all, TradingSessionStart);
 
... found another fault - here ´s now the complete code once more ..

// Session begin and end in an hourly chart

TradingSessionStart = Hour()==8 AND Minute()==0;
LatestExit = Ref(TradingSessionStart,-12);
SessionBar = BarsSince(TradingSessionStart);
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, TradingSessionStart);

Buy = TradingSessionStart==0 AND Entry_up AND Entry_all;
Short = TradingSessionStart ==0 AND Entry_dn AND Entry_all;

// Plotting the Signals in the chart

Plot(1,"",IIf(Buy,colorBlue,IIf(Short,colorOrange,
colorBlack)),styleOwnScale|styleArea|styleNoLabel,0,100);
 
Hello to all,

I just wanted you to know, that Equity function actually seems to work.

Here ´s the code for Sell/Cover.

Thanks anyway & regards
Robert


Buy = Position_up;
Short = Position_dn;

Sell = Portfolio>ExitLevel_up OR LatestExitPortfolio;
Cover = Portfolio<ExitLevel_dn OR LatestExitPortfolio;

Equity(1,0);

Winner_up = ValueWhen(Sell,Portfolio,1)>ExitLevel_up;
Profit_up = ValueWhen(Sell,Portfolio,1)>EntryLevel_up;
Looser_up = ValueWhen(Sell,Portfolio,1)<EntryLevel_up;

Winner_dn = ValueWhen(Cover,Portfolio,1)<ExitLevel_dn;
Profit_dn = ValueWhen(Cover,Portfolio,1)<EntryLevel_dn;
Looser_dn = ValueWhen(Cover,Portfolio,1)>EntryLevel_dn;

Plot(2,"",
IIf(Sell AND Winner_up,colorBrightGreen,
IIf(Sell AND Profit_up,colorBlue,
IIf(Sell AND Looser_up,colorRed,
IIf(Cover AND Winner_dn,colorBrightGreen,
IIf(Cover AND Profit_dn,colorBlue,
IIf(Cover AND Looser_dn,colorRed,
colorBlack)))))),styleOwnScale|styleArea|styleNoLabel,0,100);
 
Top