EA Order processing - missing the basics ?

 

Hi I am new to Forex and MT4 but I have programming background.

I am writing my firs EA and there is one thing that bugs me, I had an impression that the logic flow of buying and selling the currency is as simple as buying something over the counter, i.e if i send a buy order, I am buying certain amount of a currency but then i noticed that there is also the concept of Closing an order ?

So am I right assuming that each buy and sell action consists of two steps Sending an order and Closing it ?

  • If this is the case, why this can't just be done in one go, why this has to be broken down in two steps ?
  • Can i send buy order and close it in the same tick ?
  • Am I correct assuming that I can only sell and order once the buy order has ben closed ?
  • What is actually the difference between opening an order and closing it ?

Can someone provide some link that would answer those questions ?


I just seems unintuitive that in order to buy a currency you have send a buy order and then close it ? unless I am mistaken and you don't have to.

Thanks

 
  1. You enter an other, you must eventually exit it.
  2. An order consists of your entry price and optionally a take profit price and/or a stop loss price. If the market hits the TP/SL you are out at that price, otherwise at the market price, when you close the order.
  3. You buy at the Ask and close at the Bid. The difference is called the spread. If you do both at ruffly the same time, the market hasn't moved, but you've lost money, based on the price difference and the amount of lots.
  4. In the US you can't hedge (have a buy and a sell open at the same time.) Once you close an order, you can do whatever you want.
  5. A symbol (e.g. EURUSD,) consists of a base (EUR) and quote (USD) currencies. If you buy, you buy 100,000 Euros and sell 100,000 Dollars. When you close the buy order you are rebuying the Dollars with your previously brought Euros. Changes in the exchange rate determines whether you made or lost money in the trade.
  6. Learn Forex Trading With BabyPips.com Learn How to Trade the Markets






 

whroeder1


Thanks for the information but some things are still not clear to me

  1. A symbol (e.g. EURUSD,) consists of a base (EUR) and quote (USD) currencies. If you buy, you buy 100,000 Euros and sell 100,000 Dollars. When you close the buy order you are rebuying the Dollars with your previously brought Euros. Changes in the exchange rate determines whether you made or lost money in the trade.

So do you mean that when i call function "SendOrder" with a OP_BUY flag im actually buying a 100,000 Euros for 100,000 Dollars and then when i close this order im selling the Euro for Dollars ? if this is the case, why there is  an option to also use "SendOrder" with OP_SELL flag ? can't i just use that to sell the EUR/USD instead of using "CloseOrder" ?

        Let's say i already have 100,000 Dollars deposit on my account, if my EA only deals with EUR/USD pair, that meas that I will never have to use "SendOrder" with "OP_SELL" flag because I will only use OP_BUY and "CloseOrder"                     to do the transactions  is this correct ?

Learn Forex Trading With BabyPips.com
  • 2017.06.08
  • Hucklekiwi Pip
  • www.babypips.com
Canada will release its latest jobs report this Friday. And if you’re planning to pillage some pips from the Loonie on that event, then you better read up.
 
Ironhide83: So do you mean that when i call function "SendOrder" with a OP_BUY flag im actually buying a 100,000 Euros for 100,000 Dollars and then when i close this order im selling the Euro for Dollars ? if this is the case, why there is  an option to also use "SendOrder" with OP_SELL flag ? can't i just use that to sell the EUR/USD instead of using "CloseOrder" ?
  1. Then you are selling 100,000 Euros and buying 100,000 Dollars. The opposite of a buy order.
  2. No you can't. A) That is hedging, not allowed in US. Twice the margin, both orders open. B) In other platforms a sell cancels a buy, but that's not how MT4 works (model is open price+SL+TP all with the same lots.)
 
whroeder1:
  1. Then you are selling 100,000 Euros and buying 100,000 Dollars. The opposite of a buy order.
  2. No you can't. A) That is hedging, not allowed in US. Twice the margin, both orders open. B) In other platforms a sell cancels a buy, but that's not how MT4 works (model is open price+SL+TP all with the same lots.)

Ok so I should only use OrderSend with correct SL TP to buy and sell the currency right ? or i could also use OrderSend wiht TP and SL set to 0 and then use OrderClose is this correct ?
 
  1. If you don't have a SL, you have infinite risk. If you loose connection, power failure, etc. How can you call OrderClose?
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  2. Does your strategy use a target price? Then set a TP
  3. Does your strategy use multiple targets? Then you want to use OrderClose to partially exit at each level.
  4. Does your strategy do a stop and reverse on a opposite signal? Then you can use OrderClose to stop.
  5. Otherwise, trail the SL until you are stopped out.
Reason: