Exit from a trade that doesn't work

Hello there;
I wrote this code (excerpt):

Buy = (Cross(MACD_Rif,MACDSIGN_Rif) AND ((BBandTop(C,20,2) - BBandBot(C,20,2)) > BBandWidth));

Short = (Cross(MACDSIGN_Rif,MACD_Rif) AND ((BBandTop(C,20,2) - BBandBot(C,20,2)) > BBandWidth) AND TradeOn == True);


//===========	EXIT 1   ===========

Sell = (MA(C,2) < Ref(MA(C,2),-1) AND Ref(MA(C,2),-1) < Ref(MA(C,2),-2) AND BuyPrice > 0);
Cover = (MA(C,2) > Ref(MA(C,2),-1) AND Ref(MA(C,2),-1) > Ref(MA(C,2),-2) AND ShortPrice > 0);

//===========	EXIT 2   ===========

Sell = (Cross(MACDSIGN_Rif,MACD_Rif));

Cover = (Cross(MACD_Rif,MACDSIGN_Rif));

type or paste code here

The trade system works fine when entering the trade and when exiting with ApplyStop or the code marked as "EXIT 2" (inverted entry signal). The code marked "EXIT 1" doesn't work. I've tried with the "Detailed Log" report and with the debugging tool but I've not seen anything wrong. With "Exploration" I can see that the value of the Sell array or the Cover array are always 0 also when the conditions inside the parenthesis are true.
I'm not confident with the "array mode" in Amibroker. I prefer the old way of programming with "if statement" , looping, etc. but it is more time expensive.
Anybody could help me? Thanks to everyone.

If you are running the code exactly as it's shown here, then the second time you assign the Sell and Cover arrays (under // EXIT 2) you are overwriting whatever was assigned the first time (under // EXIT 1).

Also, you didn't show us how you're assigning BuyPrice and SellPrice. Since those are used in your exit logic, nobody can really tell where your error might be.

Thank You mradtke. Your suggestion seems to me very useful. I will try to work on it and then I'll be back here if necessary.

Well. The matter now is: How can I know that I'm in the market? I want to exit with the "EXIT 1" code and only if it doesn't happen, I want to exit with the other code "EXIT 2". Don't worry about BuyPrice and ShortPrice; I removed them.

@RockerGab, how can anyone know without seeing the code in question?

It's difficult for the doctor to prescribe without first seeing the patient.

1 Like

Sell = Exit1 OR Exit2

Your statement implies that you want to wait some amount of time to see if Exit 1 is triggered, and only start looking for the Exit 2 condition after that time. If that is the case, then you need to provide a much better description of what you're trying to achieve as @ghanson already pointed out.

Then for Exits 2 simply write

Sell2 = Sell_Cond2 AND NOT Sell1;
Cover2 = Cover_Cond2 AND NOT Cover1;

So overall...

/// https://forum.amibroker.com/t/exit-from-a-trade-that-doesnt-work/14388/7
/// modification of 
/// https://forum.amibroker.com/t/exit-from-a-trade-that-doesnt-work/14388
SetPositionSize(1, spsShares);

BBandWidth = 20;// points ($)
SL_Opt = 2;// points ($)
TradeOn = 1;// 0 or 1

MACD_Rif = MACD();
MACDSIGN_Rif = Signal();

bb_top = BBandTop(C,20,2);
bb_bot = BBandBot(C,20,2);
bb_cond = (bb_top - bb_bot) > BBandWidth;

Buy_Cross = Cross(MACD_Rif,MACDSIGN_Rif);
Short_cross = Cross(MACDSIGN_Rif,MACD_Rif);

Buy = Buy_Cross AND bb_cond;
Short = Short_cross AND bb_cond AND TradeOn == True;


//===========	EXIT 1   ===========

bars = 2;
rc = ROC(MA(C,2),1);
Sell1 = Sum(rc < 0, bars) == bars;
Cover1 = Sum(rc > 0, bars) == bars;

//===========	EXIT 2   ===========
// Exit only if EXIT 1 does not happen

Sell2 = Short_cross AND NOT Sell1;
Cover2 = Buy_Cross AND NOT Cover1;

//===========	EXIT   ===========

Sell = Sell1 OR Sell2;
Cover = Cover1 OR Cover2;

In case you wonder... this

Sell = (MA(C,2) < Ref(MA(C,2),-1) AND Ref(MA(C,2),-1) < Ref(MA(C,2),-2) );
Cover = (MA(C,2) > Ref(MA(C,2),-1) AND Ref(MA(C,2),-1) > Ref(MA(C,2),-2));

can be shortened to this

rc = ROC(MA(C,2),1);
Sell1 = Sum(rc < 0, 2) == 2;
Cover1 = Sum(rc > 0, 2) == 2;

BTW, exit1 is much more likely to happen than Exit 2 since it is just checked for two bars negative or two bars positive ROC. If you increase for more than two bars e.g. 10 bars

Sell1 = Sum(rc < 0, 10) == 10;

then you will see that Exit 2 has chance to come into play.

So both upper exits do work.

BTW; If you want Exit2 to happen only after Exit1 time limit then you need to say so but you didn't (AFAICS).

1 Like

Thanks very much to everybody, specially fxshrat that had wrote a lot about my topic. I've wrote only a part of my trading exercise but it was the core. Next time I'll write the entire code. At the moment I've already many suggestions to study. As I wrote, I'm a beginner with the array proggramming of Amibroker AFL. Thanks again.