void md(int m){int i,r;double tpnya,dtp,bbep,sbep,ssize,bsize; for (i = 0; i < OrdersTotal(); i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; //tpnya=OrderTakeProfit(); m=OrderType(); if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();} if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();} } if (bbep>0) bbep/=bsize; if (sbep>0) sbep/=ssize; if(m==0)tpnya=bbep+TP*pt; if(m==1)tpnya=sbep-TP*pt; for (i = OrdersTotal() - 1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderSymbol() != Symbol()) continue; m=OrderType(); if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();} if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();} } if (bbep>0) bbep/=bsize; if (sbep>0) sbep/=ssize; if(m==0)dtp=bbep+TP*pt; if(m==1)dtp=sbep-TP*pt; if( tpnya!=dtp && OrderType()==m)r=OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tpnya, 0, CLR_NONE); }
still got me error
please help..im stuck with the order modify for takeprofit..
//I understand that this process will modify the fixTP when the OrderTakeProfit become bigger,fixTP will be profit if the lotsize and pipstep is well calculated.
//I need to make it dynamic when the fixTP become bigger and still close in profit even the pipstep and lot are not well calculate.
Problem that im facing:
1.how to calculate the distance to its breakeven point 2.
2.how to calculate the distance when it will not loss and distance when it will not profit. and combine with that code?
Based on your codes, you add all the order open prices and divide them by the total lot size before attempting to calculate a new TP from there, which will result in ridiculous tpnya value.
I think the proper steps should be (1) Loop through all orders (divided by buys and sells) and add up their profits and lots, then (2) using total lots, calculate how many more pips/points are required to reach your ideal profit level (separately for buys and sells), offset by the total corresponding profits, and finally (3) add/subtract that pips/points from the current Bid/Ask.
So, assuming at the time of calculation, all your orders already acquired a Profit/Loss of $100, but you want to target $150, so you'll use the total lots and calculate the number of pips/points required to gain another $50, since you already have $100.
- Seng Joo Thio: Based on your codes, you add all the order open prices and divide them by the total lot size before attempting to calculate a new TP from there, which will result in ridiculous tpnya value.@Seng Joo Thio Look again. He is computing Lots Weighted Average Price (∑ opi• loti / ∑ loti) This is the break even price of all orders combined.
OrderOpenPrice question . - MQL4 programming forum
looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum - amerul.trader: still got me error
void md(int m){int i,r;double tpnya,dtp,bbep,sbep,ssize,bsize; for (i = 0; i < OrdersTotal(); i++){ if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; m=OrderType(); if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
- Zero your sums before starting your loop. Always use strict. Fixing the warnings will save you hours of debugging.
- Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your
code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles -
if(m==0)tpnya=bbep+TP*pt; if(m==1)tpnya=sbep-TP*pt; if(m==0) dtp=bbep+TP*pt; if(m==1) dtp=sbep-TP*pt; if( tpnya!=dtp
When can that compare ever be true? - Your error is that after computing BE price and then dtp, you need to do an OrderSelect loop to
modify all orders.
- @Seng Joo Thio Look again. He is computing Lots
Weighted Average Price (∑
opi•
loti / ∑
loti) This is the break even price of all orders combined.
OrderOpenPrice question . - MQL4 programming forum
looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum - Zero your sums before starting your loop. Always use strict. Fixing the warnings will save you hours of debugging.
- Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means
your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles - When can that compare ever be true?
- Your error is that after computing BE price and then dtp, you need to do an OrderSelect
loop to modify all orders.
Thank you for you comment.
the main idea actually is to modify OrderTakeProfit() to not close losses when totalOrder is getting greater since the TP and pipstep is fixed.
attach below the code for more understanding.
my understanding in looping still bad..need to learn more.. thank you all for helping.
- @Seng Joo Thio Look again. He is computing Lots Weighted Average Price (∑ opi• loti / ∑ loti) This is the break even price of all orders combined.
Oooooops...
the main idea actually is to modify OrderTakeProfit() to not close losses when totalOrder is getting greater since the TP and pipstep is fixed.
Nailed the bugs! - highlighted below are changes I made:
void ModifyTP(int m) { int i,r; double tpnya,dtp,bbep,sbep,ssize,bsize; tpnya=0;dtp=0;bbep=0;sbep=0;ssize=0;bsize=0; for (i=0; i<OrdersTotal();i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()|| OrderMagicNumber() != MagicNumber || OrderType()!=m) continue; tpnya=OrderTakeProfit(); if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();} if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= ssize+OrderLots();} } if (bbep>0) { bbep/=bsize; tpnya=bbep + TP*pt; } if (sbep>0) { sbep/=ssize; tpnya=sbep - TP*pt; } for (i=OrdersTotal()-1; i>=0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderSymbol() != Symbol()|| OrderMagicNumber() != MagicNumber|| OrderType()!=m) continue; dtp=OrderTakeProfit(); //if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();} //if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();} Print("tpnya",tpnya,": ",i); Print("dtp",dtp,": ",i); if( tpnya!=dtp && OrderType()==m) r = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tpnya, 0, CLR_NONE); } //if (bbep>0) bbep/=bsize; dtp=bbep; //if (sbep>0) sbep/=ssize; dtp=sbep; }
And here's the backtest results based on EURUSD M15 from 1st Apr to mid May 🤑
Oooooops...
Nailed the bugs! - highlighted below are changes I made:
And here's the backtest results based on EURUSD M15 from 1st Apr to mid May 🤑
Yeahh!! thanks man..it works.. but somehow it still showing error OrderModify error 1 though.. anyway thanks again!
if (bbep>0) { bbep/=bsize; tpnya=NormalizeDouble(bbep + TP*pt,Digits); }
if (sbep>0) { sbep/=ssize; tpnya=NormalizeDouble(sbep - TP*pt,Digits); }
solved the problem.. thank you!
Yeahh!! thanks man..it works.. but somehow it still showing error OrderModify error 1 though.. anyway thanks again!
Oh... that... is because when you check "tpnya!=dtp", a very tiny difference between -pt and pt, except 0, will return true as well, and that'll take you to OrderModify().
But OrderModify() will not be happy with a change of less than pt... that's why you see the error.
To remove that error, just change "tpnya!=dtp" to "MathAbs(tpnya-dtp)>=pt"
if( tpnya!=dtp
Doubles are rarely equal. In this case, usually compare not equals. Understand the links in:
The
== operand. - MQL4 programming forum
Your new TP should be normalized to tick size. The OrderTakeProfit (dtp) already is. Then the compare will succeed without the absolute value.
- Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented
exactly. (like 1/10.)
Double-precision floating-point format - Wikipedia, the free encyclopediaSee also The == operand. - MQL4 programming forum
Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.
- SL/TP (stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum
- Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum
- Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
- MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
please help..im stuck with the order modify for takeprofit..
//I understand that this process will modify the fixTP when the OrderTakeProfit become bigger,fixTP will be profit if the lotsize and pipstep is well calculated.
//I need to make it dynamic when the fixTP become bigger and still close in profit even the pipstep and lot are not well calculate.
Problem that im facing:
1.how to calculate the distance to its breakeven point 2.
2.how to calculate the distance when it will not loss and distance when it will not profit. and combine with that code?