Need help regarding OrderClose and OrderModify - page 3

 
1.  About OrderLots thing, I changed to normalizedouble, so that it will be able to always divide by 2, and yes i understand that it can not close or have remaining less than MinLot, but it was not an issue as starting lot was 2. 

A code should not rely on the lotsize being 2. It should be coded to deal with all possibilities.

ModifyHalfShortPosition = OrderModify(OrderTicket(), Bid, OrderOpenPrice(), Ask - (OrderOpenPrice() - OrderTakeProfit()), 0);

Don't use Bid when modifying an order. Unless you want to change the price of a pending order, use OrderOpenPrice()


If I were you, I would take William's advise

You also must check if you have already done it, to avoid repeated closing. Alternatives:
  • Move SL to Break Even+1 before the partial close. That way you know that you already did it.
  • Set a flag in persistent storage (files, global variables w/flush)
  • Open two orders initially, and close one (manually or by TP.)

Personally  I would never use

if(Bid >= OrderTakeProfit()-1*Point()) 

I think that trying to get the jump on the broker closing the trade at TP is a bad idea.

I would open the trade with the TP set at TP2 and then when the price is halfway there, I would do the partial close.

 

@Keith Watford


Thanks to your advice, throughout the passed week, I was able to successfully fix abit. However, I have a new obstacle that is different topic, would it be possible for me to ask some guidance from you? It is regarding opening only one orders per bar.


I would truly appreciate it if you would reply to this forum, however, I am not so sure whether you will see this forum or not.


So would it be alright for me to open another forum if you do not reply within a certain period of time? I assure you, it is regarding different topics.

 

If you open a new topic and it concerns MT4, please keep it in this section

https://www.mql5.com/en/forum/mql4

 

Hi,

If the stochastic main and signal crossed, I want to close my position.

Whats the problem with this?

MAny thanks


extern int number = 12;

void OnTick()

  {

double main2=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0);

double signal2=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);

if(main2=signal2)

{

     int ticket=OrderClose( number, 1, Bid, 2 );


 }

  }

 
huberpeter:


Please edit your post and

use the code button (Alt+S) when pasting code.

if(main2=signal2)  //This is not boolean!
int ticket=OrderClose( number, 1, Bid, 2 );

OrderClose returns a bool not an int. Check GetLastError() if it fails.

bool  OrderClose(
   int        ticket,      // ticket
   double     lots,        // volume
   double     price,       // close price
   int        slippage,    // slippage
   color      arrow_color  // color
   );



Is number a valid ticket number?

Is 1 a valid lot size for the order?

Is bid  a valid close price? We don't know if it is a buy or sell.

 
Keith Watford

Personally  I would never use

if(Bid >= OrderTakeProfit()-1*Point()) 

I think that trying to get the jump on the broker closing the trade at TP is a bad idea.

You can not modify/close an order if the Stops (or pending open price) are closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

On some ECN type brokers the value might be zero (broker doesn't know.) Use a minimum of two (2) PIPs.