Please edit your post and
use the code button (Alt+S) when pasting code
- John2010: if the price is between open price and take profit, then sell half of the position and move the stoploss to breakeven. I
The problem is to remember that you have already sold half. Move to breakeven first and then close.
-
OrderClose(OrderTicket(), OrderLots()/2,
Will not work. The close amount and the remaining amount must both be between min and max and be a multiple of lot step.
-
if(Bid == (OrderTakeProfit() + OrderOpenPrice())/2)
Doubles are rarely equal. Understand the links in:
The == operand. - MQL4 programming forum #2 2013.06.07
if(OrderType() == 0) // if it's a buy if((Bid > ((OrderTakeProfit() + OrderOpenPrice())/2)) && (OrderOpenPrice()!= OrderStopLoss())&& (OrderTakeProfit()!= 0)) // if the price is half-way to take profi { double MN1 = OrderMagicNumber(); double OP1 = OrderOpenPrice(); OrderClose(OrderTicket(), OrderLots()/2, Bid, 2, clrDarkOrange); // close half of the position if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol()) ) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration(), clrNONE); // move the stoploss to breakeven } if(OrderType()== 1) // if it's a sell if((Ask < ((OrderTakeProfit() + OrderOpenPrice())/2)) && (OrderOpenPrice()!= OrderStopLoss()) && (OrderTakeProfit()!= 0)) // if the price is half-way to take profit { double MN2 = OrderMagicNumber(); double OP2 = OrderOpenPrice(); OrderClose(OrderTicket(), OrderLots()/2, Ask, 2, clrDarkOrange); // close half of the position if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol())) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration(), clrNONE); // move the stoploss to breakeven }
SunSoil Crypto #:
Not working … if close half of the order and then try to select, the remaining order will have different index .Also, OrderLots()/2 is wrong … order lots should be normalized to lotstep and checked to be between LotMax and LotMin
Also, as said above, doubles are almost never equal
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Please help me with this
Hope someone can give me a clear and helpful answer
Here is the code I wrote to check if the price is between open price and take profit, then sell half of the position and move the stoploss to breakeven. It works good, when reaching to the half profit point, it will close half of the position and move the stoploss to the open price of the original position (breakeven). My question is that how can I do this only once for each trade. How to make this code happen just once to any single trade. Now it does the same thing over and over on a trade, for example, first time 500 profit (half of 1000 take profit), immediately 250 profit, then 125 profit, then 62.5 profit, etc. until it's close to zero. I want to take the profit when we're half way through, and let the rest half of the profit run for much more profits.