Hy Guys! I have made a simple code, and then i have added trailing stop. Works nice, but i was thinking to reverse the order when sl is touched. My ideas is this:
Trailing
Maybe i will add an option to start the trailing when order is 20 pips in profit. To reversing i have used same code, but on this way:
But seems that something is wrong, i don't know what. Could someone help me?
Hello,
OrderClosePrice()
is only for history orders, not for current.
In your code, use it on open orders..
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
Hello,
is only for history orders, not for current.Oh, right! So, i had better to change on this way
for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) { if(Ask == OrderStopLoss()) { Alert("StopLoss reached on " +Symbol() + " order is now reversed"); int secondticket = OrderSend(_Symbol,OP_SELL,Lots*2,Bid,3,Bid+600*_Point,Bid-400*_Point,"Renko Sell Order",MagicNumber,0,Red); Sleep(60000); } } } for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) { if(Bid == OrderStopLoss()) { Alert("StopLoss reached on " +Symbol() + " order is now reversed"); int secondticket2 = OrderSend(_Symbol,OP_BUY,Lots*2,Ask,3,Ask-600*_Point,Ask+400*_Point,"Renko Buy Order",MagicNumber,0,Green); Sleep(60000); } } }
This should be correct right?
Hello,
is only for history orders, not for current.
In your code, use it on open orders..
That is not true. OrderClosePrice() is also used on MODE_TRADES.
In MODE_TRADES, OrderClosePrice() = Bid for Buy Order and Ask for Sell order.
Please get tested if you don't believe it.
That is not true. OrderClosePrice() is also used on MODE_TRADES.
In MODE_TRADES, OrderClosePrice() = Bid for Buy Order and Ask for Sell order.
Please get tested if you don't believe it.
So, what do you think is the error?
-
if(OrderType()==OP_BUY){ if(OrderStopLoss() < Ask - (200*_Point))
You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To
trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
-
if(Ask == OrderStopLoss())
Doubles are rarely equal. Understand the links in:
The == operand. - MQL4 programming forum - Once the market hits the SL, the order is closed. Your condition will never occur.
- You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
- You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell
orders you pay the spread on close.
- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches
it. To trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
- Doubles are rarely equal. Understand the links in:
The == operand. - MQL4 programming forum - Once the market hits the SL, the order is closed. Your condition will never occur.
- You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
My original code was made with OrderClosePrice(), but looks like is not working. So, for Trailing, i had better to change on this way?
for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) { if(OrderStopLoss() < Bid - (200*_Point)) OrderModify(OrderTicket(),OrderOpenPrice(),Ask - (120*_Point),OrderTakeProfit(),0,Green); } } for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) { if(OrderStopLoss() < Ask + (200*_Point)) OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (120*_Point),OrderTakeProfit(),0,Green); } }
This trailing is correct?
So, what do you think is the error?
For Buy order, it must be Bid> = OrderStopLoss ()
For Sell order, it must Ask <= OrderStopLoss ()
Buy order: Ask == OrderStopLoss () or Sell order: Bid == OrderStopLoss (),
then the stop loss has not been reached by the price.
To suit your purpose, it's best to check after the order closes by stop loss, in MODE_HISTORY.
If OrderClosePrice()> = OrderStopLoss() (for buy order) or OrderClosePrice() <= OrderStopLoss() (for sell order).
For Buy order, it must be Bid> = OrderStopLoss ()
For Sell order, it must Ask <= OrderStopLoss ()
Buy order: Ask == OrderStopLoss () or Sell order: Bid == OrderStopLoss (),
then the stop loss has not been reached by the price.
To suit your purpose, it's best to check after the order closes by stop loss, in MODE_HISTORY.
If OrderClosePrice()> = OrderStopLoss() (for buy order) or OrderClosePrice() <= OrderStopLoss() (for sell order).
So, guys you are all a bit confusing me. Let me know if i am right with everything:
//Reverse order for the BUY for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) { if(OrderClosePrice()>= OrderStopLoss()) { Alert("StopLoss reached on " +Symbol() + " order is now reversed"); int secondticket = OrderSend(_Symbol,OP_SELL,Lots*2,Bid,3,Bid+600*_Point,Bid-400*_Point,"Renko Sell Order",MagicNumber,0,Red); Sleep(60000); } } } //Reverse order for the SELL for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) { if(OrderClosePrice() <= OrderStopLoss()) { Alert("StopLoss reached on " +Symbol() + " order is now reversed"); int secondticket2 = OrderSend(_Symbol,OP_BUY,Lots*2,Ask,3,Ask-600*_Point,Ask+400*_Point,"Renko Buy Order",MagicNumber,0,Green); Sleep(60000); } } } //Trailing Stop for the BUY for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) { if(Bid >= OrderStopLoss() + (200*_Point)) OrderModify(OrderTicket(),OrderOpenPrice(),Ask - (120*_Point),OrderTakeProfit(),0,Green); } } //Trailing Stop for the SELL for (int b= OrdersTotal()-1; b>=0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) { if(Ask <= OrderStopLoss() - (200*_Point)) OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (120*_Point),OrderTakeProfit(),0,Green); } }
So, guys you are all a bit confusing me. Let me know if i am right with everything:
That's all wrong.
You cannot check orders in mode_trades if the order is closed by stop loss.
What's more you want to modify an order that has closed at a stop loss, at a price of 200 points greater (buy) or smaller than order stop loss (sell) ?
Please learn more MQL4/MQL5 reference documents.
That's all wrong.
You cannot check orders in mode_trades if the order is closed by stop loss.
What's more you want to modify an order that has closed at a stop loss, at a price of 200 points greater (buy) or smaller than order stop loss (sell) ?
Please learn more MQL4/MQL5 reference documents.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hy Guys! I have made a simple code, and then i have added trailing stop. Works nice, but i was thinking to reverse the order when sl is touched. My ideas is this:
Trailing
Maybe i will add an option to start the trailing when order is 20 pips in profit. To reversing i have used same code, but on this way:
But seems that something is wrong, i don't know what. Could someone help me?