I had been trying to make an trailing function inside my EA that will go through all orders in the pool , and modify by trailing the stop loss for every order in the pool that meets the condition . I tried it on 2 different demo account , but it seems like always the last order in the pool , the stop loss only moved ONCE which it actually should move more than once since it meet the trailing target . It only work sometimes not all the time , I'm not sure what's happening but I'd look through it many times the code SEEMS ( not confirmed ) fine TO ME .
Anyone mind to give some help
Code: Select all
//+------------------------------------------------------------------+
//| Program's global variables |
//+------------------------------------------------------------------+
extern double StartTrailingStop = 15.0 ;
extern double TrailingStop = 5.0 ;
extern double CummulativeValue = 5.0 ;
extern int NumberOfTrail = 10 ;
double Figure ;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Program's sub-function |
//+------------------------------------------------------------------+
double PointsDetection()
{
Figure = 0.0 ;
if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.01 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.001 )
{
Figure = 0.01 ;
}
else
{
if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.0001 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.00001 )
{
Figure = 0.0001 ;
}
}
return ( Figure ) ;
}
//+------------------------------------------------------------------+
//| Program's sub-function |
//+------------------------------------------------------------------+
void ProcessTrailingStop()
{
bool Result ;
for ( int x = ( OrdersTotal() - 1 ) ; x >= 0 ; x-- )
{
if ( OrderSelect ( x , SELECT_BY_POS , MODE_TRADES ) == True )
{
PointsDetection() ;
if ( OrderType() == OP_BUY )
{
for ( int xai = 0 ; xai < NumberOfTrail ; xai++ )
{
if ( xai == 0 )
{
if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() <= OrderOpenPrice() ) || ( OrderStopLoss() < ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
if ( xai == 1 )
{
if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
if ( xai >= 2 )
{
if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * ( xai - 1 ) ) ) * Figure ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * xai ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
}
}
else
{
if ( OrderType() == OP_SELL )
{
for ( int xaii = 0 ; xaii < NumberOfTrail ; xaii++ )
{
if ( xaii == 0 )
{
if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() >= OrderOpenPrice() ) || ( OrderStopLoss() > ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
if ( xaii == 1 )
{
if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
if ( xaii >= 2 )
{
if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xaii ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * ( xaii - 1 ) ) ) * Figure ) ) ) )
{
Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * xaii ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
}
}
}
}
}