Hello,
Im really lost here but I have the majority of my EA working.... the last part of it is to figure out how I move my stop to breakeven once Im up 50 pips...
I imagine to do this Ill need to use OrderModify() as well as OrderProfit() but im really lost how to do this... my trade ID is 16384
[...]
Broadly speaking, and without actually trying out the following code (so apologies for dumb errors). No error handling in the following (e.g. around problems because of MODE_FREEZELEVEL). However, if you have something like this in start() then the code should simply keep retrying on each market tick until the request succeeds.
// Select the order - which assumes that 16384 is the ticket, not the magic number you're using
OrderSelect(16384, SELECT_BY_TICKET);
// Is the profit on the order equivalent to the value of at least 50 pips?
// (N.B. This avoids the need to compare prices. You could instead look
// at the current price versus the opening price of the trade, and then divide the
// difference by the pip size, but then you'd need different handling for buys and
// sells - i.e. you'd need to look at the current bid on long positions and the
// current ask on shorts. If you want to include swap in the calculation, then
// use (OrderProfit() + OrderSwap()).
if (OrderProfit() / (MarketInfo(Symbol(), MODE_TICKVALUE) * OrderLots()) >= 50)
{
// Has the stop loss already been altered on a previous pass?
// I.e. is the stop loss already set to break-even?
if (OrderStopLoss() != OrderOpenPrice()) {
// Change the order, specifying the open price as the new stop loss
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration());
}
}
I have tried ot put this code in to move the stop loss but it only moves the stop loss if the pair goes 10 pips above or below price on the opening bar. If the trade goes idebit then goes into profit several bars later this doesn't set the stop loss at breakeven. I hope that makes sense. It doesn't seem to run this for all open orders each tick. Any advice?
I am very new to this so the code is clunky and often borrowed :-)
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)
{
// Select the correct trade with orderselect & use this code to set the stop equal to the entry price once profit is past 10 pips:
{
if (OrderProfit() / (MarketInfo(Symbol(), MODE_TICKVALUE) * OrderLots()) >= 100)
{
if (OrderStopLoss() != OrderOpenPrice())
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration());
}
}
}
// Here is close buy rule ....................
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
}
}
else
{
// Select the correct trade with orderselect & use this code to set the stop equal to the entry price once profit is past 10 pips:
{
if (OrderProfit() / (MarketInfo(Symbol(), MODE_TICKVALUE) * OrderLots()) >= 100)
{
if (OrderStopLoss() != OrderOpenPrice())
{
// This doesn't seem to work unless it is on the opening bar??????
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration());
}
}
}
// Here is close sell rule .........................
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
}
}
}
}
return(0);
// code for a sell order; OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if (OrdersTotal()>0) { if (OrderMagicNumber()==/*MagicNumber*/) { if (OrderProfit()>=50.00) { // 50 points would be something like 50 dollars for 0.1 lots (if you are using EURUSD currency pair); { bool moveSL = true; }}}} if (moveSL==true) { { OrderStopLoss()-50*Point; moveSL==false; }} // be sure to consider the spread of the currency pair that you are using before launch & I wrote 50*Point assuming that your SL - // is 50 points too. Also 50 might be 500 if your broker is 5 digit. Try 50 or 500. // I didn't think too long to come up with a solution, so there might be a few specifications requiring your personal editing. There are multiple solutions.
Thank you
Thank you
Thanks, I'll give it a go.
What I find odd is that the code i have works well for some orders. It doesn't seem to loop throhg all open orders all ticks.
As orders open and close does it matter if I do this select by POS or TICKET?
@ n4btb:
1. Please type what you wish to write at the top of a new message.
2. Please type your question in bold with 1.,2.,3., etc at the bottom of a new message.
Please attach the code that you wish to have assistance with by using the "SRC" button at the top of a new message or add file by using "Attach file" at the bottom of a new message.
There are multiple ways to select orders. "OrderSelect()" is simply one way to select orders. There are other ways, but I believe that in your case the simplest way to accomplish the task is to select orders by using the "OrderSelect()" function. I highly recomend using the code that I provided and you can tailor it to your specifications.
I hope this helped.
Thank you.
Use select by pos, because you have to already know the ticket number to use select by ticket
Make sure you Normalize the OrderOpenPrice() and OrderTakeProfit(), if you dont you will probably have problems.
and if you do anything like if (OrderStopLoss() != OrderOpenPrice()) Normalize them before you compare
to test for pip movement I do
buys .. if(Bid >= openprice+50*Point)
sells .. if(Ask <= openprice-50*Point)
Thanks for the advice.....Can I normalize like this?
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
// Once you have selected the correct trade with orderselect you can use this code to set the stop equal to the entry price once profit is past 10 pips:
{
if (OrderProfit() / (MarketInfo(Symbol(), MODE_TICKVALUE) * OrderLots()) >= 100)
if (NormalizeDouble(OrderStopLoss(),5) != NormalizeDouble(OrderOpenPrice(),5))
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration());
}
}
{
Please see attached chart for an example of what is happening.....
There are 3 sell orders on the chart.
All should have stop loss moved to orderopenprice as all get more than 10 pips into profit at some time.
The first amd third do not, even though both trade get into profit on the 9th Jan. I would expect to see them stopped out at break even but they don't, they go onto a loss.
The 2nd, placed just before 14.45 on the 7th Jan does get stop loss moved to orderopenprice because the trade moves 10 pips into profit on the same bar as the trade is opened.
The code seems to work, but it only works if the trade moves into profit on the same bar that it is opened...
Is this a code issue or an issue of where I am running this piece?
n4btb:
Thanks for the advice.....Can I normalize like this?
There are 3 sell orders on the chart.
All should have stop loss moved to orderopenprice as all get more than 10 pips into profit at some time.
You DO NOT need to Normalize OrderStopLoss() or OrderOpenPrice() . . really, you do not.
So one of your OrderModify() calls fails, why ? what error is generated ? I see no code to check the return value or report any error . . . why ?
Read this: What are Function return values ? How do I use them ? and use the return value from your OrderModify() and report any errors, then you might know what is going wrong.
You DO NOT need to Normalize OrderStopLoss() or OrderOpenPrice() . . really, you do not.
So one of your OrderModify() calls fails, why ? what error is generated ? I see no code to check the return value or report any error . . . why ?
Read this: What are Function return values ? How do I use them ? and use the return value from your OrderModify() and report any errors, then you might know what is going wrong.
I looked at the journal and it has Order Modify Error 1. I saw from a previous post of yours that this likely means I am trying to modify an order but with no changes. But I can make no sense of that.
Why doesn't if (OrderStopLoss() != OrderOpenPrice()) in the code below prevent that?
It seems ot only run on the initial bar the order was placed in? Unless I am making assumptions.
Is there any reason that the OrderSelect will return different orders in the same position? Do order move positions from tick to tick?
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
// Once you have selected the correct trade with orderselect you can use this code to set the stop equal to the entry price once profit is past 10 pips:
{
if (OrderProfit() / (MarketInfo(Symbol(), MODE_TICKVALUE) * OrderLots()) >= 100)
{
if (OrderStopLoss() != OrderOpenPrice())
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration());
}
}
}
- 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,
Im really lost here but I have the majority of my EA working.... the last part of it is to figure out how I move my stop to breakeven once Im up 50 pips...
I imagine to do this Ill need to use OrderModify() as well as OrderProfit() but im really lost how to do this... my trade ID is 16384
Here's what Im trying to do:
if trade 16384 is up 50 pips
then
move stop to break even
else
do nothing
end if