Hello
I ve amazing Divergence Expert based on MACD histogram Candelistick systems needs to be converted from VTtrader to Metastock formula , its a little bit similar to Metastock formula its working perfect with FX and Commodties and i need to use it with stocks .
the code is :
Barnum:= BarCount();
{Candle Patterns}
BullEngulf:= O < Ref(Open, -1) AND
O < Ref(Close, -1) AND
C > Ref(Close, -1) AND
C > Ref(Open, -1) AND
Abs(Ref((C+O)/2,-1)) < Ref(Mov(C,10,E),-1);
BearEngulf:= O > Ref(Open, -1) AND
O > Ref(Close, -1) AND
C < Ref(Close, -1) AND
C < Ref(Open, -1) AND
Abs(Ref((C+O)/2,-1)) > Ref(Mov(C,10,E),-1);
{MACD}
FL:= Mov(spr,Sh,smat) - Mov(lpr,Lg,lmat);
SL:= Mov(FL,sig,sigmat);
OsMA:= FL-SL;
OsMAZeroLine:= 0;
{MACD Divergence}
{This will detect a trough of MACD Histogram, then detect a second
trough lower than the first trough}
DefineTrough:= if(OsMA<0 and ref(OsMA,-1)<0 and ref(OsMA,-2)<0 and OsMA>ref(OsMA,-1) and ref(OsMA,-1)<ref(OsMA,-2), 1, 0);
PrevTrough:= valuewhen(2,DefineTrough,ref(OsMA,-1));
ActualTrough:= if(DefineTrough and PrevTrough<ref(OsMA,-1) and (BarsSince(OsMA>0)>BarsSince(ref(DefineTrough,-1))),1,0);
PrevPrcTrough:= valuewhen(2,DefineTrough,ref(L,-1));
ActualPrcTrough:= if(DefineTrough and PrevPrcTrough>ref(L,-1),1,0);
BullishDivergence:= if(ActualTrough=1 and ActualPrcTrough=1,1,0);
{This will detect a peak of MACD Histogram, then detect a second
peak higher than the first peak}
DefinePeak:= if(OsMA>0 and ref(OsMA,-1)>0 and ref(OsMA,-2)>0 and OsMA<ref(OsMA,-1) and ref(OsMA,-1)>ref(OsMA,-2), 1, 0);
PrevPeak:= valuewhen(2,DefinePeak,ref(OsMA,-1));
ActualPeak:= if(DefinePeak and PrevPeak>ref(OsMA,-1) and (BarsSince(OsMA<0)>BarsSince(ref(DefinePeak,-1))),1,0);
PrevPrcPeak:= valuewhen(2,DefinePeak,ref(H,-1));
ActualPrcPeak:= if(DefinePeak and PrevPrcPeak<ref(H,-1),1,0);
BearishDivergence:= if(ActualPeak=1 and ActualPrcPeak=1,1,0);
{Support/Resistance}
_PipDistance:= PipDistance * SymbolPoint();
_Support:= Support;
_Resistance:= Resistance;
{Do NOT change the NAMES of the Variables below.
If you Changed The NAMES of Indicator1 or Indicator2,
you will have to change them below.}
{Define Final Trade Entry/Exit Criteria}
LongEntryCond1:= BullEngulf;
LongEntryCond2:= BullishDivergence;
LongEntryCond3:= min(O,C) > _Support;
LongEntryCond4:= (min(O,C) - _Support) <= _PipDistance;
ShortEntryCond1:= BearEngulf;
ShortEntryCond2:= BearishDivergence;
ShortEntryCond3:= max(O,C) < _Resistance;
ShortEntryCond4:= (_Resistance - max(O,C)) <= _PipDistance;
LongEntrySetup:= LongEntryCond1 AND LongEntryCond2 AND LongEntryCond3 AND LongEntryCond4;
LongExitSetup:= ShortEntryCond1 AND ShortEntryCond2 AND ShortEntryCond3 AND ShortEntryCond4;
ShortEntrySetup:= ShortEntryCond1 AND ShortEntryCond2 AND ShortEntryCond3 AND ShortEntryCond4;
ShortExitSetup:= LongEntryCond1 AND LongEntryCond2 AND LongEntryCond3 AND LongEntryCond4;
{***********************************************}
{That's it! You're done! There is no need to
edit below this point.}
{***********************************************}
{Determine the Pip Value for the currency chart being used}
_SymbolPoint:= SymbolPoint();
_InitialStoploss:= InitialStoploss * _SymbolPoint;
_BEP:= BEP * _SymbolPoint;
_TrailingStoploss:= TrailingStoploss * _SymbolPoint;
_ProfitTarget:= ProfitTarget * _SymbolPoint;
_Spread:= Spread * _SymbolPoint;
{Define Active Market Hours for Taking Trade Entries}
{For Example: StartHour=2 and StartMinute=0 will allow trade entries beginning at 2:00 AM EST}
{For Example: EndHour=14 and EndMinute=30 will allow trade entries until 2:30 PM EST}
_hour:= hour();
_minute:= minute();
TradingSessionOnStart:= _hour=StartHour and _minute>=StartMinute;
TradingSessionOnEnd:= _hour=EndHour and _minute>=EndMinute;
TradingSessionOn:= SignalFlag(TradingSessionOnStart,TradingSessionOnEnd);
TradingSessionOff:= 1;
TradeEntryTimeFilter:= if(TradeEntryTimeFilterMode=0, TradingSessionOn, TradingSessionOff);
TradeEntryTimeFilterNotEnabled:= TradeEntryTimeFilter=0;
TradeEntryTimeFilterEnabled:= TradeEntryTimeFilter=1;
{***********************************************}
LongEntrySignal:= (LongTradeAlert=0 AND ShortTradeAlert=0 AND TradeEntryTimeFilter=1 AND LongEntrySetup) OR
(LongTradeAlert=0 AND Cross(0.5,ShortTradeAlert) AND TradeEntryTimeFilter=1 AND LongEntrySetup) OR
(LongTradeAlert=0 AND TradeEntryTimeFilter=1 AND ShortExitSetup);
LongEntryPrice:= valuewhen(1,LongEntrySignal,C) + _Spread;
BarsSinceLongEntry:= BarsSince(LongEntrySignal);
LongEntryInitialStop:= if(LongTradeAlert=1 OR LongEntrySignal OR LongExitSignal, LongEntryPrice - _InitialStoploss, null);
DisplayLongEntryInitialStop:= if(InitialStoplossMode=0, LongEntryInitialStop, null);
LongEntryBEStoplossFlag:= SignalFlag(LongTradeAlert=1 AND C >= (LongEntryPrice + _BEP), LongExitSignal);
LongEntryBEStoploss:= if(LongEntryBEStoplossFlag=1 OR LongEntrySignal OR LongExitSignal, LongEntryPrice, null);
DisplayLongEntryBEStoploss:= if(BEStoplossMode=0, LongEntryBEStoploss, null);
LongEntryPipTrailingStop:= if(LongTradeAlert=1 OR LongEntrySignal OR LongExitSignal, max((C - _TrailingStoploss), PREV(LongEntryPrice - _TrailingStoploss)), null);
LongEntryATRTrailingStop:= if(LongTradeAlert=1 OR LongEntrySignal OR LongExitSignal, max(HHV(H,BarsSinceLongEntry) - (ATR(ATRper) * atrmultiplier), PREV(valuewhen(1,LongEntrySignal,H) - (ATR(ATRper) * atrmultiplier))), null);
DisplayLongEntryTrailingStop:= if(TrailingStoplossMode=0 AND TrailingStoplossType=1, LongEntryPipTrailingStop,
if(TrailingStoplossMode=0 AND TrailingStoplossType=0, LongEntryATRTrailingStop,
null));
LongEntryProfitTarget:= if(LongTradeAlert=1 OR LongEntrySignal OR LongExitSignal, LongEntryPrice + _ProfitTarget, null);
DisplayLongEntryProfitTarget:= if(ProfitTargetMode=0, LongEntryProfitTarget, null);
LongExitSignal:= (LongTradeAlert=1 AND InitialStoplossMode=0 AND Cross(LongEntryInitialStop,C))
OR (LongTradeAlert=1 AND BEStoplossMode=0 AND Cross(LongEntryBEStoploss,C))
OR (LongTradeAlert=1 AND TrailingStoplossMode=0 AND TrailingStoplossType=1 AND Cross(LongEntryPipTrailingStop,C))
OR (LongTradeAlert=1 AND TrailingStoplossMode=0 AND TrailingStoplossType=0 AND Cross(LongEntryATRTrailingStop,C))
OR (LongTradeAlert=1 AND ProfitTargetMode=0 AND Cross(C,LongEntryProfitTarget))
OR (LongTradeAlert=1 AND LongExitSetup);
LongExitPrice:= valuewhen(1,LongExitSignal,C);
ShortEntrySignal:= (ShortTradeAlert=0 AND LongTradeAlert=0 AND TradeEntryTimeFilter=1 AND ShortEntrySetup) OR
(ShortTradeAlert=0 AND Cross(0.5,LongTradeAlert) AND TradeEntryTimeFilter=1 AND ShortEntrySetup) OR
(ShortTradeAlert=0 AND TradeEntryTimeFilter=1 AND LongExitSetup);
ShortEntryPrice:= valuewhen(1,ShortEntrySignal,C);
BarsSinceShortEntry:= BarsSince(ShortEntrySignal);
ShortEntryInitialStop:= if(ShortTradeAlert=1 OR ShortEntrySignal OR ShortExitSignal, ShortEntryPrice + _InitialStoploss - _Spread, null);
DisplayShortEntryInitialStop:= if(InitialStoplossMode=0, ShortEntryInitialStop, null);
ShortEntryBEStoplossFlag:= SignalFlag(ShortTradeAlert=1 AND C <= (ShortEntryPrice - _BEP), ShortExitSignal);
ShortEntryBEStoploss:= if(ShortEntryBEStoplossFlag=1 OR ShortEntrySignal OR ShortExitSignal, ShortEntryPrice - _Spread, null);
DisplayShortEntryBEStoploss:= if(BEStoplossMode=0, ShortEntryBEStoploss, null);
ShortEntryPipTrailingStop:= if(ShortTradeAlert=1 OR ShortEntrySignal OR ShortExitSignal, min((C + _TrailingStoploss - _Spread), PREV(ShortEntryPrice + _TrailingStoploss - _Spread)), null);
ShortEntryATRTrailingStop:= if(ShortTradeAlert=1 OR ShortEntrySignal OR ShortExitSignal, min(LLV(L,BarsSinceShortEntry) + (ATR(ATRper) * atrmultiplier), PREV(valuewhen(1,ShortEntrySignal,L) + (ATR(ATRper) * atrmultiplier))), null);
DisplayShortEntryTrailingStop:= if(TrailingStoplossMode=0 AND TrailingStoplossType=1, ShortEntryPipTrailingStop,
if(TrailingStoplossMode=0 AND TrailingStoplossType=0, ShortEntryATRTrailingStop,
null));
ShortEntryProfitTarget:= if(ShortTradeAlert=1 OR ShortEntrySignal OR ShortExitSignal, ShortEntryPrice - _ProfitTarget - _Spread, null);
DisplayShortEntryProfitTarget:= if(ProfitTargetMode=0, ShortEntryProfitTarget, null);
ShortExitSignal:= (ShortTradeAlert=1 AND InitialStoplossMode=0 AND Cross(C,ShortEntryInitialStop))
OR (ShortTradeAlert=1 AND BEStoplossMode=0 AND Cross(C,ShortEntryBEStoploss))
OR (ShortTradeAlert=1 AND TrailingStoplossMode=0 AND TrailingStoplossType=1 AND Cross(C,ShortEntryPipTrailingStop))
OR (ShortTradeAlert=1 AND TrailingStoplossMode=0 AND TrailingStoplossType=0 AND Cross(C,ShortEntryATRTrailingStop))
OR (ShortTradeAlert=1 AND ProfitTargetMode=0 AND Cross(ShortEntryProfitTarget,C))
OR (ShortTradeAlert=1 AND ShortExitSetup);
ShortExitPrice:= valuewhen(1,ShortExitSignal,C) + _Spread;
{Simulated Open Trade Determination and Trade Direction}
LongTradeAlert:= SignalFlag(LongEntrySignal,LongExitSignal);
ShortTradeAlert:= SignalFlag(ShortEntrySignal,ShortExitSignal);
{Create Auto-Trading Functionality}
OpenBuy:= LongEntrySignal and (eventCount('OpenBuy')=eventCount('CloseBuy'));
CloseBuy:= LongExitSignal and (eventCount('OpenBuy')>eventCount('CloseBuy'));
OpenSell:= ShortEntrySignal and (eventCount('OpenSell')=eventCount('CloseSell'));
CloseSell:= ShortExitSignal and (eventCount('OpenSell')>eventCount('CloseSell'));
{***********************************************}
{Calculate Simulated Individual Trade Profit In Pips}
_TradeProfitInPips:= If(LongExitSignal=1,LongExitPrice - LongEntryPrice,
If(ShortExitSignal=1,ShortEntryPrice - ShortExitPrice,
0));
TradeProfitInPips:= if(SymbolDigits()=4, _TradeProfitInPips * 10000, _TradeProfitInPips * 100);
{Calculate Simulated Total Profit In Pips}
TotalProfitInPips:= cum(TradeProfitInPips);
{Calculate Number of Trades}
LongTrades:= cum(LongExitSignal);
ShortTrades:= cum(ShortExitSignal);
LosingTrades:= cum(If(TradeProfitInPips < 0,1,0));
WinningTrades:= cum(If(TradeProfitInPips > 0,1,0));
BreakEvenTrades:= if((LongExitSignal=1 or ShortExitSignal=1) and TradeProfitInPips=0, PREV(0)+1, PREV(0));
TotalTrades:= cum(LongExitSignal) + cum(ShortExitSignal);
{Calculate Additional Stats}
WinningTradesPips:= if(TradeProfitInPips > 0,TradeProfitInPips + PREV(0), PREV(0));
LosingTradesPips:= if(TradeProfitInPips < 0,TradeProfitInPips + PREV(0), PREV(0));
LargestWinningTradeInPips:= max(TradeProfitInPips,PREV(0));
LargestLosingTradeInPips:= min(TradeProfitInPips,PREV(0));
AverageWinningTradeInPips:= WinningTradesPips / WinningTrades;
AverageLosingTradeInPips:= LosingTradesPips / LosingTrades;
PercentProfitable:= (WinningTrades/TotalTrades) * 100;
AverageWinLossRatio:= AverageWinningTradeInPips / AverageLosingTradeInPips;
{***********************************************}