If Buy Order reach above 30 Point from Entry price, close Sell Order
If Sell Order reach below 30 Point from Entry Price, Close Buy Order.
// Define variables to store order information int buyOrder = -1; int sellOrder = -1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Initialization of variables here return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Deinitialization of variables here } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Now check for buy order if(buyOrder != -1) { // Calculate the profit/loss in points double profit = (NormalizeDouble(OrderClosePrice() - OrderOpenPrice(), Digits) / Point); // Check if the profit exceeds 30 points if(profit >= 30) { // Close the buy order OrderClose(buyOrder, OrderLots(), Bid, Slippage); // Reset buy order buyOrder = -1; } } // Check for sell order if(sellOrder != -1) { // Calculate the profit/loss in points double profit = (NormalizeDouble(OrderOpenPrice() - OrderClosePrice(), Digits) / Point); // Check if the profit exceeds 30 points if(profit >= 30) { // Close the sell order OrderClose(sellOrder, OrderLots(), Ask, Slippage); // Reset sell order sellOrder = -1; } } // Check for new buy order if(buyOrder == -1 && OrdersTotal() == 0) { // Place new buy order buyOrder = OrderSend(Symbol(), OP_BUY, 0.1, Ask, Slippage, 0, 0, "Buy Order", 0, 0, clrNONE); } // Check for new sell order if(sellOrder == -1 && OrdersTotal() == 0) { // Place new sell order sellOrder = OrderSend(Symbol(), OP_SELL, 0.1, Bid, Slippage, 0, 0, "Sell Order", 0, 0, clrNONE); } } //+------------------------------------------------------------------+
This way can be used as a template
Thanks for template, But this won't work as i want. Instead, i changed my code and trying like this
if(LastOrderPrice(OP_BUY, gBuyMagic) > 0 && trade_count_ordertype(OP_SELL, gSellMagic) == 1 && Bid - LastOrderPrice(OP_BUY, gBuyMagic) > gRangeClosing_Automatic * Point) { CloseOpenAndPendingTrades(gSellMagic); Print("Range Closing => Sell Order Closed"); } if(LastOrderPrice(OP_SELL, gSellMagic) > 0 && trade_count_ordertype(OP_BUY, gBuyMagic) == 1 && LastOrderPrice(OP_SELL, gSellMagic) - Ask > gRangeClosing_Automatic * Point) { CloseOpenAndPendingTrades(gBuyMagic); Print("Range Closing => Buy Order Closed"); } double LastOrderPrice(int LastOrderPrice_OrderType, int LastOrderPrice_magic) { double LastOrderPrice_Price = 0; // Initialize to some default value for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++) { OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() != Symbol() || OrderMagicNumber() != LastOrderPrice_magic) continue; if(OrderType() == LastOrderPrice_OrderType) LastOrderPrice_Price = OrderOpenPrice(); } return LastOrderPrice_Price; } int trade_count_ordertype(int trade_count_ordertype_value, int trade_count_ordertype_magic) { int count_4 = 0; for(int pos_8 = 0; pos_8 < OrdersTotal(); pos_8++) { OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_count_ordertype_magic) continue; if(trade_count_ordertype_value == OrderType()) count_4++; } return count_4; }
Hi
What you described here is simply a stop Loss for a trade. You can simply set the SL on the chosen distance – along with opened trade and it will be closed automatically on the proper level.
For example (for Buy trade):
//… double entryprice = NormalizeDouble(Ask, Digits()); double sl =NormalizeDouble(entryprice-30*Point(), Digits()); buyOrder = OrderSend(Symbol(), OP_BUY, 0.1, entryprice, Slippage, sl, 0, "Buy Order", 0, 0, clrNONE); //…
Best Regards
Hi
What you described here is simply a stop Loss for a trade. You can simply set the SL on the chosen distance – along with opened trade and it will be closed automatically on the proper level.
For example (for Buy trade):
Best Regards
I am not looking for Stop Loss. Let me explain again,
Here, Buy and Sell get opened in at same time (Like a hedge)
If Buy move above by 30 Point, close sell order
If Sell move down by 30 Point, Close Buy Order.
Hi
Ok, I get it now. But you can still use SL here – but set this after opening both trades.
You can then get entry price for sell and calculate the SL for Buy as buysl = NormalizeDouble(sell_entry-30*Point(), _Digits). And get Buy entry price and calculate SL for sel: sellsl = NormalizeDouble(buy_entry-30*Point(), _Digits).
Then you can modify the trades with this new SLs and the trades will be closed when proper levels would be reached.
Best Regards
Function to
Very often (according to my observations) people are looking for a magic function for some complex task.
Your problem statement is incorrect. When pursuing the goal of writing one magic function, you are initially going in the wrong direction. Implementing this as a single function will be bad either way.
All current orders must be collected into an array of structures/classes using a single loop. In the process of collecting information about orders, it will be easy to calculate the profit of the order in points (as one of the fields of the structure/class of order properties).
After this, using the already collected data, you can close orders whose profit in points satisfies your condition. This is also absolutely not difficult.
Writing a dozen functions, inside each of which code similar to the code from the quote below, is a terrible idea.
for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++) { OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
Unfortunately, many beginner (and not only beginners) developers do this. Perhaps the idiotic guides on YouTube, which promote this type of thinking, are to blame for this.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I am making a code,
If Buy Order reach above 30 Point from Entry price, close Sell Order
If Sell Order reach below 30 Point from Entry Price, Close Buy Order.
Problem :
Both condition some how getting true and it closing both Buy and Sell order at same time