Hello there;
I've written the code illustrated below but it does'nt works as I'm expecting to do.
I need an exit when the highest low value from the first Buy signal retrace of an optimized value /and vice versa for short trades). It seems that the exit signal happens from the latest Buy signal instead of the very first Buy signal of my trade (I see this with the Exploration tool). Maybe I might use Exrem but I haven't understood how it works.
Here my code:
Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;
PriceAtBuy = 0;
PriceAtShort = 0;
OnMarket = 0;
cs = 0;
bar = BarIndex();
//Versione Future
SetOption("FuturesMode",True);
PositionSize = MarginDeposit = 1; //Acquista/vende sempre solo 1 contratto
TRL_Par = Param("TRL",150,100,250,50);
TRL_Opt = Optimize("TRL",TRL_Par,100,250,50);
ValueDiffMACD_Par = Param("Diff MACD",50,50,100,10);
ValueDiffMACD_Opt = Optimize("Diff MACD",ValueDiffMACD_Par,50,100,10);
MACD_Rif = MACD(fast = 12, slow = 26);
MACDSIGN_Rif = Signal(12,26,9);
//======================= End Variables ==================================================================================================================
Flag_Long = Flip(Cross(MACD_Rif,MACDSIGN_Rif),Cross(MACDSIGN_Rif,MACD_Rif));
Buy = (MACD_Rif - MACDSIGN_Rif > ValueDiffMACD_Opt AND Flag_Long == 1);
PriceAtBuy = ValueWhen(Buy,C,1);
Flag_Short = Flip(Cross(MACDSIGN_Rif,MACD_Rif),Cross(MACD_Rif,MACDSIGN_Rif));
Short = (MACDSIGN_Rif - MACD_Rif > ValueDiffMACD_Opt AND Flag_Short == 1);
PriceAtShort = ValueWhen(Short,C,1);
//ApplyStop(0,2,SL_Opt,1); //Stop Loss
//Debug trap
//cs = IIf(bar > 1170,1,0);
//=========== EXIT 1 ===========
Trailing_Par_Long = HighestSince(Buy,L);
Sell = IIf(L < Trailing_Par_Long - TRL_Opt,True,False);
SellPrice = (Trailing_Par_Long - TRL_Opt);
Trailing_Par_Short = LowestSince(Short,H);
Cover = IIf(H > Trailing_Par_Short + TRL_Opt,True,False);
CoverPrice = (Trailing_Par_Short + TRL_Opt);
//Monitoring section
BI = BarIndex();
Filter = 1;
AddColumn(BI,"BI",1.0);
AddColumn(Flag_Long,"Flag_Long",1.0);
AddColumn(Flag_Short,"Flag_Short",1.0);
AddColumn(Trailing_Par_Long,"Trailing_Par_L",1.0);
AddColumn(Buy,"Buy",1.0);
AddColumn(BuyPrice,"BuyPrice",1.2);
AddColumn(PriceAtBuy,"PriceAtBuy",1.2);
AddColumn(Short,"Short",1.0);
AddColumn(ShortPrice,"ShortPrice",1.2);
AddColumn(PriceAtShort,"PriceAtShort",1.2);
AddColumn(Cover,"Cover",1.0);
AddColumn(CoverPrice,"CoverPrice",1.2);
AddColumn(Sell,"Sell",1.0);
AddColumn(SellPrice,"SellPrice",1.2);
AddColumn(MACD_Rif,"MACD(12)",1.4);
AddColumn(MACDSIGN_Rif,"MACD(12)Sign",1.4);
Thanks in advance to everybody.