Download MetaTrader 5

Problem with Modifying Stop Loss.

To add comments, please log in or register
Arav007
412
Arav007  

Even the day before yesterday, this EA was working fine.

It was taking orders, modifying Stop Losses (Trailing Stops) on multiple pairs accordingly.

Suddenly yesterday I saw that 'Invalid Stops' error was showing while it was trying to open 'Buy' order at EURJPY and after failing it was opening a 'Sell' order while the 'Sell Condition' was not met actually. And it was opening Sell orders at every Tick so numerous 'Sell' orders got opened and closed almost simultaneously!

And today I am seeing it isn't working with any Pair. Everywhere it is showing 'Invalid Stops' It's because it's selecting 'Ticket' of other opened 'EURJPY' order! It's even fetching EURJPY's quote for Opening Price, SL, Tp etc.

But if there is no EURJPY order opened(i.e. the EA isn't attached to EURJPY chart), the Error isn't showing!

I'm not understanding why this is happening?

The code is in the next post.

Regards

Arav007
412
Arav007  
N/A
Arav007
412
Arav007  
Got it.
Tjipke de Vries
6338
Tjipke de Vries  
void TrailOrder(double Trailingstart,double Trailingstop){
int ticket = 0;

sLog_TrailOrder = "    Start - TrailOrder function--------------------";

double tStopLoss;

//tStopLoss = NormalizeDouble(OrderStopLoss(), Digits); // Stop Loss. OrderSelect has to be run first before assinging a value to tStopLoss

int cnt;
double vPoint,vSlippage;

//double sl     = OrderStopLoss(); // Stop Loss. OrderSelect has to be run first before assinging a value to sl
double sl;


if (Digits == 3 || Digits == 5)
    {vPoint = Point * 10; vSlippage = Slippage * 10;    }
    else
    {vPoint = Point; vSlippage = Slippage;}

RefreshRates();
if(OrdersTotal()>0){

        for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
        
        if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
                
                sLog_TrailOrder = sLog_TrailOrder + sNL + "    Order selected. OrderTicket=" + OrderTicket();
                sLog_TrailOrder = sLog_TrailOrder + sNL + "    OrderOpenPrice=" + DoubleToStr(OrderOpenPrice(),Digits);
                sLog_TrailOrder = sLog_TrailOrder + sNL + "    OrderStopLoss=" + DoubleToStr(OrderStopLoss(),Digits);
                sLog_TrailOrder = sLog_TrailOrder + sNL + "    OrderTakeProfit=" + DoubleToStr(OrderTakeProfit(),Digits);
                sLog_TrailOrder = sLog_TrailOrder + sNL + "    ------------------------------";
        
                sl     = OrderStopLoss(); // Stop Loss. 
                tStopLoss = NormalizeDouble(OrderStopLoss(), Digits); // Stop Loss. 
                
                if(OrderType()==OP_BUY){
                
                        if(Ask> NormalizeDouble(OrderOpenPrice()+TrailingStart* vPoint,Digits)
                        && tStopLoss < NormalizeDouble(Bid-(TrailingStop+TrailingStep)*vPoint,Digits)){
                        
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    Ask=" + DoubleToStr(Ask,Digits);
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    OrderOpenPrice=" + DoubleToStr(OrderOpenPrice(),Digits);
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    TrailingStart=" + TrailingStart;
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    vPoint=" + DoubleToStr(vPoint,6);
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    TrailingStop=" + TrailingStop;
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    TrailingStep=" + DoubleToStr(TrailingStep,Digits);
                        
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    Min Price=" + DoubleToStr(NormalizeDouble(OrderOpenPrice()+TrailingStart* vPoint,Digits),Digits);
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    tStopLoss=" + DoubleToStr(tStopLoss,Digits);
                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    Min target SL=" + DoubleToStr(NormalizeDouble(Bid-(TrailingStop+TrailingStep)*vPoint,Digits),Digits);
                        
                                tStopLoss = NormalizeDouble(Bid-TrailingStop*vPoint,Digits);
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    new tStopLoss=" + DoubleToStr(tStopLoss,Digits);
                                
                                ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Blue);

                                if (ticket > 0){
                                        Print ("TrailingStop #2 Activated: ", OrderSymbol(), ": SL", tStopLoss, ": Bid", Bid);
                                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    ticket " + ticket + " modified. New StopLoss=" + DoubleToStr(tStopLoss,Digits);
                                        
                                        }
                                        else{
                                        iLastError = GetLastError();
                                        
                                        sLog_TrailOrder = sLog_TrailOrder + sNL +  "    Error modifying stop loss. Error code=" + ErrorDescription(iLastError);
                                        //return(0);
                                        }
                        }//end if(Ask> NormalizeDouble(OrderOpenPrice()+TrailingStart* vPoint,Digits) && tStopLoss < NormalizeDouble(Bid-(TrailingStop+TrailingStep)*vPoint,Digits)){
                }//end if(OrderType()==OP_BUY){


                if (OrderType()==OP_SELL) {
                        if (Bid < NormalizeDouble(OrderOpenPrice()-TrailingStart*vPoint,Digits)
                        && (sl >(NormalizeDouble(Ask+(TrailingStop+TrailingStep)*vPoint,Digits)))
                        || (OrderStopLoss()==0)){
                        
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    Bid=" + DoubleToStr(Bid,Digits);
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    Max Price=" + DoubleToStr(NormalizeDouble(OrderOpenPrice()-TrailingStart* vPoint,Digits),Digits);
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    sl=" + DoubleToStr(sl,Digits);
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    Min target SL=" + DoubleToStr(NormalizeDouble(Ask+(TrailingStop+TrailingStep)*vPoint,Digits),Digits);
                                
                                sl = NormalizeDouble(Ask+TrailingStop*vPoint,Digits);
                                                                
                                sLog_TrailOrder = sLog_TrailOrder + sNL + "    new sl=" + DoubleToStr(sl,Digits);
                                
                                ticket = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Red);
                                
                                if (ticket > 0){
                                        Print ("Trailing #2 Activated: ", OrderSymbol(), ": SL ",sl, ": Ask ", Ask);
                                        sLog_TrailOrder = sLog_TrailOrder + sNL + "    ticket " + ticket + " modified. New StopLoss=" + DoubleToStr(sl,Digits);
                                        
                                        }
                                        else{
                                        iLastError = GetLastError();
                                        
                                        sLog_TrailOrder = sLog_TrailOrder + sNL +  "    Error modifying stop loss. Error code=" + ErrorDescription(iLastError);
                                        //return(0);
                                        }
                                }
                        }


}//end for(cnt=OrdersTotal();cnt>=0;cnt--){
}//end if(OrdersTotal()>0){

sLog_TrailOrder = sLog_TrailOrder + sNL + "    End - TrailOrder function--------------------";

return(0);
}//end void TrailOrder(double Trailingstart,double Trailingstop){

what symbol has the selected trade

what is the magicnumber of the selected trade ??

Arav007
412
Arav007  
It is supposed to be the Symbol of the chart it's attached with.
whroeder1
15013
whroeder1  
Arav007: the Error isn't showing!
OutputDebugStringA(out);
Strings are now Unicode, fix your debug and start tracking it down.
Tjipke de Vries
6338
Tjipke de Vries  
Arav007:
It is supposed to be the Symbol of the chart it's attached with.


no it isn't it can be any symbol()

it can be also every trade you have open on your account

no matter if it is opend by other EA's or manually placed

it even can modify prices other trades if the modifying isn't give an error

you never programmed .... have you ??

this ea you can't trust on a real account

        for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
         

if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;

after this line

new line put in a line like...

if(OrderSymbol() == Symbol() && OrderMagicNumber() ==  MagicNumber)
Arav007
412
Arav007  
WHRoeder:
Strings are now Unicode, fix your debug and start tracking it down.


Actually by "the Error isn't showing" I meant that when the EA is detached from EURJPY chart, such error isn't occurring.

Anyway, from debugview:

Arav007
412
Arav007  
deVries:


no it isn't it can be any symbol()

it can be also every trade you have open on your account

no matter if it is opend by other EA's or manually placed

it even can modify prices other trades if the modifying isn't give an error

you never programmed .... have you ??

this ea you can't trust on a real account

if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;

after this line

new line put in a line like...


Thanks,

I've placed it and now testing.

This is what needed.

Being honest, I've programmed a bit but I'm emphasizing on 'Examples' more for such 'Trailing Stops', 'Closing Orders' etc. and using the examples as 'Template' in my code.

Also getting suggestions from Experienced coders often and adding them too.

Regards

To add comments, please log in or register