Of course it doesn't get executed if you do not call it anywhere.
Also the order has to be selected by OrderSelect() function first.
Thank you Marco, for pointing me in the right direction !
I made some adjustments to my code (in NT you do not need to 'call' functions explicitly, so this is new for me)
I now have this in my start section: (I mirrored this from that UniversalMACrossEA.mq4 mentioned in my first post)
Now, if there is an order live that was opened by this EA, then the function 'subTrailingStop' is called, no ? And it is called on a specific OrderType
So further down in my code I have this function defined. (The case is always 1, since I have "extern int TrailingStopType = 1;" in my inputs)
What am I missing ?
I made some adjustments to my code (in NT you do not need to 'call' functions explicitly, so this is new for me)
I now have this in my start section: (I mirrored this from that UniversalMACrossEA.mq4 mentioned in my first post)
int start()
{
int cnt;
int total;
if(TrailingStop>0 && subTotalTrade()>0)
{
total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
dummyResult = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==s_symbol && OrderMagicNumber()==MagicNumber)
{
subTrailingStop(OrderType());
}
}
}
return(0);
}
I think this code starts counting the number of orders when the chart is initialized. And it keeps doing this counting indefinitely, right ?{
int cnt;
int total;
if(TrailingStop>0 && subTotalTrade()>0)
{
total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
dummyResult = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==s_symbol && OrderMagicNumber()==MagicNumber)
{
subTrailingStop(OrderType());
}
}
}
return(0);
}
Now, if there is an order live that was opened by this EA, then the function 'subTrailingStop' is called, no ? And it is called on a specific OrderType
So further down in my code I have this function defined. (The case is always 1, since I have "extern int TrailingStopType = 1;" in my inputs)
void subTrailingStop(int Type)
{
RefreshRates();
if(Type==OP_BUY) // buy position is opened
DrawDotBlue();
{
switch(TrailingStopType)
{
//----------------------- AFTER PROFIT TRAILING STOP
case 1:
if(Bid-NormalizeDouble(OrderOpenPrice(),digit)>pPoint*TrailingStop*pipMultiplier &&
NormalizeDouble(OrderStopLoss(),digit)<Bid-pPoint*TrailingStop*pipMultiplier)
{
DrawDotBlue();
dummyResult = OrderModify(OrderTicket(),NormalizeDouble(OrderOpenPrice(),digit),NormalizeDouble(Bid-pPoint*TrailingStop*pipMultiplier,digit),NormalizeDouble(OrderTakeProfit(),digit),0,Green);
return;
}
break
Should this not activate an after-profit trailing stop on my buy orders ?? Similar code exists for "(Type==OP_SELL)"{
RefreshRates();
if(Type==OP_BUY) // buy position is opened
DrawDotBlue();
{
switch(TrailingStopType)
{
//----------------------- AFTER PROFIT TRAILING STOP
case 1:
if(Bid-NormalizeDouble(OrderOpenPrice(),digit)>pPoint*TrailingStop*pipMultiplier &&
NormalizeDouble(OrderStopLoss(),digit)<Bid-pPoint*TrailingStop*pipMultiplier)
{
DrawDotBlue();
dummyResult = OrderModify(OrderTicket(),NormalizeDouble(OrderOpenPrice(),digit),NormalizeDouble(Bid-pPoint*TrailingStop*pipMultiplier,digit),NormalizeDouble(OrderTakeProfit(),digit),0,Green);
return;
}
break
What am I missing ?
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I switched from NinjaTrader to MT recently and am trying to get the hang of MQL-code, so please be gentle.
I am currently trying to add a 'Breakeven / Trailling' functionality to my EA.
I copied code from the (in?)famous UniversalMACrossEA.mq4 (https://www.mql5.com/en/forum) and will gladly throw lots of flowers to the gentle coders there :) Special thanks to Codersguru and Firedave !
The EA open and close functions work just fine, and now I would like to add the BE/TR to lock in more profit.
For some reason the subTrailingStop-codeblock does not get executed when I perform a (visual) backtest :(
Like I said: I coded in NT before, but my MQL4-debugging-skills aren't up to par yet. Can someone glance at my code please and tell me why the BE/TR isn't firing and/or how to debug this ?