trailing stop issue

 
//trailing stop function
void CheckTrailingStop(){
double newsl = OrderStopLoss();

for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=exMagic || OrderSymbol()!=Symbol()) continue;







if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==exMagic)
{
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*exTrailingStop,OrderTakeProfit(),0,Red);
}

if((OrderOpenPrice()-Ask)>(gdPoint*exTrailingStop))
{
if(newsl<(Ask+gdPoint*exTrailingStop))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+gdPoint*exTrailingStop,OrderTakeProfit(),0,Red);
}
}


}


if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==exMagic)
{
if (OrderStopLoss()>=0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*exTrailingStop,OrderTakeProfit(),0,Green);





being a NEWBY


i can get struff wrong


my ea works well except for the trailing stop


my code for the trailing stop was mostly from this forum and then i have tried to fit my ea to it


it moves the stop just fine and it trails correctly to a point


but it follows the market up and down on a buy

for instance


an I am not getting why so help would be appreciated

Thanks Ron


here is the code for it



note to WHRoeder I might have misunderstood your intentions if so sorry

i was thinking someone could look at this code and say oh you did this wrong or that wrong

not just complain because i didnt post it right

and then go on about everything but trailing stops

 

Tradingjunky:

being a NEWBY

my code for the trailing stop was mostly from this forum and then i have tried to fit my ea to it
  1. Obviously since you didn't use SRC
  2. You didn't paste ALL the code. No mind-readers here.
  3. You should count DOWN in the presence of multiple orders (multiple charts)
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        ){
    
  4. EA's must adjust for 4/5 digit broker, TP, SL, AND slippage. Your gdPoint may be ok, but you didn't show it's value or the OrderSend
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
        /* On ECN brokers you must open first and THEN set stops
        int ticket = OrderSend(...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_POS))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket()...)
           Alert("OrderModify failed: ", GetLastError());
         */
    

  5. Always test return codes
    if (!OrderModify(...)
       Alert("OrderModify failed: ", GetLastError());

 
WHRoeder:
  1. Obviously since you didn't use SRC
  2. You didn't paste ALL the code. No mind-readers here.
  3. You should count DOWN in the presence of multiple orders (multiple charts)
  4. EA's must adjust for 4/5 digit broker, TP, SL, AND slippage. Your gdPoint may be ok, but you didn't show it's value or the OrderSend
  5. Always test return codes

your as helpful as brick to a drowning man


the code i post was adequate to tell me what to look for in my coding


all the stuffl you on about is not at issue

I already said it would work without the trailing stop function

so all i need to fix is the trailing stop function nothing else


why comment at all if your intention is not to help


 
Tradingjunky:

why comment at all if your intention is not to help

Why insist on making it hard for people to help you ? if you insist on doing that you won't get help . . . please edit your first post and use SRC . . please.
 
  1. I posted what I saw, and asked for more information in order to try and help you. Then you start bitch'n. That will put you in the Troll category and you won't get any help from anyone then. Grow up, the world does not revolve around you. I really shouldn't help you any but I'll try once more.
  2. if(newsl<(Ask+gdPoint*exTrailingStop))
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+gdPoint*exTrailingStop,

    The IF tests if the current SL is below where it should be for the sell and then moves it up. It's a sell, SL should be moving down.

    Had you just printed both parts Print(newsL," <? ", Ask+gdPoint*exTrailingStop) you would see the < is wrong.


  3. If you have no SL you set one but then the IF will still be using the old value. IF (newsl==0) {modify()} else if ( >)


  4. but it follows the market up and down on a buy
    Don't know about the Buy code since you still haven't posted it. No mind readers here!
 
WHRoeder:
  1. I posted what I saw, and asked for more information in order to try and help you. Then you start bitch'n. That will put you in the Troll category and you won't get any help from anyone then. Grow up, the world does not revolve around you. I really shouldn't help you any but I'll try once more.
  2. The IF tests if the current SL is below where it should be for the sell and then moves it up. It's a sell, SL should be moving down.

    Had you just printed both parts Print(newsL," <? ", Ask+gdPoint*exTrailingStop) you would see the < is wrong.


  3. If you have no SL you set one but then the IF will still be using the old value. IF (newsl==0) {modify()} else if ( >)


  4. but it follows the market up and down on a buy
    Don't know about the Buy code since you still haven't posted it. No mind readers here!

its probably not what you asked me it the way you asked


any way never mind

i will persue this on my own


Thanks

 

Sorry, but I don't think Tradingjunky deserved the response he got.

Obviously since you didn't use SRC <= suggest "There is a button to add source code that makes it easier for others read and help.

You didn't paste ALL the code. No mind-readers here. <= suggest: "Could you paste more of the surrounding code? There are other parts that might be relevant."

You should count DOWN in the presence of multiple orders (multiple charts) <= was not the question

EA's must adjust for 4/5 digit broker, TP, SL, AND slippage. <= was not the question

Always test return codes <= was not the question

 
//trailing stop function
void CheckTrailingStop(){
double newsl = OrderStopLoss();

for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=exMagic || OrderSymbol()!=Symbol()) continue;


put the OrderStopLoss() under the orderselect function, otherwise the 'newsl' will always be the same, for all orders.


//trailing stop function
void CheckTrailingStop(){

for(int i=OrdersTotal()-1;i>0=;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=exMagic || OrderSymbol()!=Symbol()) continue;


double newsl = OrderStopLoss(); // Moved this




if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==exMagic)   // you dont need to check magicnumber and symbol twice, unless you really want to =)
{
   if (OrderStopLoss()==0)
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*exTrailingStop,OrderTakeProfit(),0,Red);  // Think you mean gdPoint instead of Point
   }

   if((OrderOpenPrice()-Ask)>(gdPoint*exTrailingStop))
   {
      if(newsl<(Ask+gdPoint*exTrailingStop))    // isnt the current sl (newsl) supposed to be larger than the new sl to be allowed to change? for SELL
      {
         OrderModify(OrderTicket(),OrderOpenPrice(),Ask+gdPoint*exTrailingStop,OrderTakeProfit(),0,Red);
      }
   }


}


if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==exMagic) // same goes here, see SELL
{
   if (OrderStopLoss()>=0) // see SELL unless you want ordermodify error 1.
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*exTrailingStop,OrderTakeProfit(),0,Green);   // Point again....


Hoped this helped you some. (haven't checked or compiled anything)


Ohh and WHRoeder might seem harsh some times, I have got some remarks too, but I think he's a really good and helpful fellow.

He helps more people here than anyone i have seen and beein' here sometime i have noticed many *stupid* questinons that can easely be answered by searching, I too have felt fustrated when same questions appear 2 to 3 times a week.

 
serpentsnoir:

Sorry, but I don't think Tradingjunky deserved the response he got.

You didn't paste ALL the code. No mind-readers here. <= suggest: "Could you paste more of the surrounding code? There are other parts that might be relevant."

You should count DOWN in the presence of multiple orders (multiple charts) <= was not the question

EA's must adjust for 4/5 digit broker, TP, SL, AND slippage. <= was not the question

Always test return codes <= was not the question


  1. He asked SPECIFICALLY about the buy trailing stop and didn't post ANY buy code. None at all. I'm not going to coddle him (or you) with nice soft language. This isn't the third grade and he shouldn't need hand holding. If he can't do a small amount of work or even proof read his post, should I even bother?
  2. Not the question. Couldn't answer the question because of #1. But it will be a problem later.
  3. Not the question but could be part of the problem now and will be a problem later.
  4. Not the question but could be part of the problem now. Test and print errors and he might be able to solve it him self. Should I only answer the exact question asked and then answer the next question tomorrow and the next after that ...? Or should I post what I saw to he could learn?
 

Should I only answer the exact question asked and then answer the next question tomorrow and the next after that ...?

This would be great - it would be a form of student-led learning.

 

Some advice for WHRoeder