Can I send an order for a Stop order, as soon as I close a previous order at the stop loss value within the same function ?

 

This has to be a repost as I've not solved it yet

I have tried this code, but I don't know what to do to get it to work

void killTrade(int pips, int ticket)

{

double price = NormalizeDouble(OrderOpenPrice(),Digits);

bool reopen = 0;

if(OrderSelect(ticket, SELECT_BY_TICKET)==true)

{

if(pips <= -1*getMaxLoss())

{

if(OrderType()==1)

{

OrderClose(ticket,OrderLots(),Ask,3,Red);

OrderSend(Symbol(),4,4,price+30*Point,3,Bid-50*Point,Ask+50*Point,"AUTO_BUY_STOP",11111,0,DarkBlue);

Alert(GetLastError()) ;

// or use the original SL and TP values taken from the original trade

// but try this first to get it working

// NormalizeDouble(ord,Digits)

}

if(OrderType()==0)

{

OrderClose(ticket,OrderLots(),Bid,3,Red);

OrderSend(Symbol(),5,4,price-30*Point,3,Ask+50*Point,Bid-50*Point,"AUTO_SELL_STOP",11111,0,Red);

Alert(GetLastError()) ;

}

}

}

}

It has actually worked once but I can not tell why, or why the rest of the time it doesnt.

thanks


 

Please do the following:

U have already been asked to do so (https://www.mql5.com/en/forum/132091).

 

I'm sure I've commented on this or similar code, suggesting several improvements that have not been incorporated yet.

MG, are you expecting us to do the coding for you and provide you with the finished program?

 

You might prefer to use OrderProfit() instead of trying to do

if(pips <= -1*getMaxLoss())

 

Code compiles ok so it is doing what you programed it to do BUT is it doing what you WANT it to?

getMaxLoss() function not posted so compiler tells me this is an error

//----------------------------------------------------------------
void killTrade(int pips, int ticket)
{
  double price = NormalizeDouble(OrderOpenPrice(),Digits);
  bool reopen = 0; //Assign with True or False not a value and not used anywhere in this function
  if (OrderSelect(ticket, SELECT_BY_TICKET)==true)
    {
     
     if (pips <= -1*getMaxLoss())
       {
       if (OrderType()==1) //Use OP_BUY etc as easier to read code
         {
          OrderClose(ticket,OrderLots(),Ask,3,Red); //
          OrderSend(Symbol(),4,4,price+30*Point,3,Bid-50*Point,Ask+50*Point,"AUTO_BUY_STOP",11111,0,DarkBlue);
          Alert(GetLastError()) ; //Alert will show even if there is no error
// or use the original SL and TP values taken from the original trade
// but try this first to get it working
// NormalizeDouble(ord,Digits)
         } //end if (OrderType()==1)
         if (OrderType()==0)//Use OP_SELL etc as easier to read code
           {
            OrderClose(ticket,OrderLots(),Bid,3,Red);
            OrderSend(Symbol(),5,4,price-30*Point,3,Ask+50*Point,Bid-50*Point,"AUTO_SELL_STOP",11111,0,Red);
            Alert(GetLastError()) ;
           }//end if (OrderType()==0)
       }//end if (pips <= -1*getMaxLoss())
    }// end if (OrderSelect(ticket, SELECT_BY_TICKET)==true)
}//end killTrade(int pips, int ticket)
Please use the SRC option to insert code - I know it is confusing at first but well worth the effort to learn.
 

There are some timing issues but code for coping with sending Orders and resending are dealt with in the Book and scattered thoughout the forum.

 
gordon:

Please do the following:

U have already been asked to do so (https://www.mql5.com/en/forum/132091).


hey Gordon, Ive just been learning this about two weeks, ok ?


Im not only learning how to logically write my strategy so that MT can understand it, I have also never programmed before.

and that previous post was asking

Function for getting the value of pips does not work

not

Can I send an order for a Stop order, as soon as I close a previous order at the stop loss value within the same function ?

thanks

 
Ickyrus:

There are some timing issues but code for coping with sending Orders and resending are dealt with in the Book and scattered thoughout the forum.


thankyou Ickyrus, as you know, I am just learning this, and piecing the whole thing together just from a book draws a dead end with me sometimes. I use the book and I use the forum, but sometimes when I just cant seem to find the answer that way I post on here.
 
Ickyrus:

Code compiles ok so it is doing what you programed it to do BUT is it doing what you WANT it to?

getMaxLoss() function not posted so compiler tells me this is an error

Please use the SRC option to insert code - I know it is confusing at first but well worth the effort to learn.

Ickyrus, how did you get that code to display like that, in colour and indented ?
 
brewmanz:

I'm sure I've commented on this or similar code, suggesting several improvements that have not been incorporated yet.

MG, are you expecting us to do the coding for you and provide you with the finished program?


No I am not expecting you to do the coding thank you very much. I am completely knew to this, and I am only asking for advice where I cant see how to do it. Is this not a forum where I can ask this ? Why is it called MQL forum ?

I have been learning this not even two weeks now, and I only post after I have tried to work out the problem myself.

 
brewmanz:

I'm sure I've commented on this or similar code, suggesting several improvements that have not been incorporated yet.

MG, are you expecting us to do the coding for you and provide you with the finished program?


"suggesting several improvements that have not been incorporated yet"

hey, Im just learning ok, and if I havent changed anything its because I either dont understand how or Ive chosen not to use it right now as I am trying to develop something else at this moment in time.

Reason: