datetime buyOrderTime=TimeCurrent(); for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()==OP_BUY) { BuyLots+=NormalizeDouble(OrderLots(),2); BuyProfit+=NormalizeDouble(OrderProfit(),2); OpenPriceLong=NormalizeDouble(OrderOpenPrice(),Digits); OpenPriceLongSum+=NormalizeDouble(OrderOpenPrice(),Digits); BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2); if(OrderOpenTime()<buyOrderTime) { buyOrderTime=OrderOpenTime(); FirstBuyOrder=OrderTicket(); FirstBuyProfit=OrderProfit(); } }
Not compiled or tested.
Not compiled or tested.
So OrderOpenTime could be any before TimeCurrent, think's not a solution.
You're not seeing it.
Obviously, the first run of the loop, the variable will store the open time of that order.
Subsequently only those with an earlier open time will update the variable.
So by the time the loop completes, it must be the earliest.
if(OrderOpenTime()<buyOrderTime){ buyOrderTime=OrderOpenTime(); FirstBuyOrder=OrderTicket(); FirstBuyProfit=OrderProfit(); }
Keith is correct. Each time it finds an earlier order, it updates the variables. The variable buyOrderTime should probably be renamed earliestBuyTime.BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2);
For the break even price you will need to addint BuyCount=0; for(…){ if(…){ if(OrderType()==OP_BUY){ ++BuyCount; ⋮ } BreakEvenPriceLong = BreakevenFactorLong / MathMax(1,BuyCount);
- No need for the TimeCurrent() call where a constant will do.
#define MAX_DATETIME D'3000.12.31 23:59:59' datetime buyOrderTime=MAX_DATETIME;
- And I assume the variables being summed are zero before the loop, (BuyLots, BuyProfit, OpenPriceLongSum, BreakevenFactorLong.)
You're not seeing it.
Obviously, the first run of the loop, the variable will store the open time of that order.
Subsequently only those with an earlier open time will update the variable.
So by the time the loop completes, it must be the earliest.
What if i try to get profit values from index POS?
I'm working in this code for two weeks and still got no solution.
Have an internal loop to get some values for expert operation, where one of them is FirstBuyProfit. It's suposed to be the first buy order current profit.
for(int i=0;i<OrdersTotal();i++){ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber){ if(OrderType()==OP_BUY){ BuyLots+=NormalizeDouble(OrderLots(),2); BuyProfit+=NormalizeDouble(OrderProfit(),2); FirstBuyProfit=NormalizeDouble(OrderProfit(),2); OpenPriceLong=NormalizeDouble(OrderOpenPrice(),Digits); OpenPriceLongSum+=NormalizeDouble(OrderOpenPrice(),Digits); BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2); }
Then I have into the order management loop, which is backcounter (i--), the averaging cycle where I compare FirstBuyProfit to LastBuyProfit in order to close them by overlapping:
if(Bid<=AveragingLong){ // DISTANCE CONDITION (TRUE) Print("Averaging LONG under ",AveragingLong); Comment("Averaging LONG under ",AveragingLong); double LastBuyProfit=OrderProfit(); if(LastBuyProfit+FirstBuyProfit>=0){ // CONDITION NEVER MET Print("Close LONG by averaging"); Comment("Close LONG by averaging"); if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)){ if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE)){ Print("OrderClose error ",GetLastError()); RefreshRates(); return; } } if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrSilver)){ Print("OrderClose error ",GetLastError()); RefreshRates(); return; } }So does anyone have some idea?
So does anyone have some idea?
You've been given the answer to your first question
if(OrderOpenTime()<buyOrderTime){
buyOrderTime=OrderOpenTime();
FirstBuyOrder=OrderTicket();
FirstBuyProfit=OrderProfit();
}
yet you have chosen to ignore it.
You've been given the answer to your first question
yet you have chosen to ignore it.
You've been given it.
You're being argumentative just like you previously did at
Last Open Price issue - Symbols - MQL4 programming forum #2 #4
You've been given it.
You're being argumentative just like you previously did at
Last Open Price issue - Symbols - MQL4 programming forum #2 #4
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Have this loop to get some values for code operation:
As far as I could see, FirstTicket and FirstProfit values are not corrresponding to the first order in every case, so I've heard that it's needed a datetime condition to get FirstTicket and its profit value. So anybody can enlighten us?