indicator issue: Code supposed to update every tick, but doesn't.

 

Hi peoples,

I wrote an indicator that performs some simple calculations and displays them on the chart. The calculations are correct, and they are supposed to update every tick, however they often don't. Even after waiting quite a few ticks, sometimes it works, sometimes it doesnt. And if it doesn't, it gets 'stuck', and I have to recompile the EA to reload it.

If anyone might be able to suggest something that I am doing wrong, I'd really appreciate it. If it helps, the 'stuck' issue is noticed more with "RealEQ" than it is with locked_in_profit.

I am not sure, but it seems like maybe the indicator does not 'see' new orders, only existing ones.

Thanks in advance,

Seb

int elapsed_bars;
int last_period;
int current_cell;
int init()
{
ObjectCreate("status", OBJ_LABEL, 0, 100, 0);
ObjectSet("status", OBJPROP_CORNER, 2);
ObjectSet("status", OBJPROP_XDISTANCE, 20);
ObjectSet("status", OBJPROP_YDISTANCE, 20);
generate_checkerboard();

elapsed_bars = Bars;
last_period = Period();
current_cell = MathFloor(Ask);
start();
return(0);
}

int deinit()
{

return(0);
}

int start()
{

if (last_period != Period()) {
ObjectsDeleteAll(0,OBJ_RECTANGLE);
generate_checkerboard();
last_period = Period();
}

if (Bars > elapsed_bars) {
ObjectsDeleteAll(0,OBJ_RECTANGLE);
generate_checkerboard();
}
//get the real total used equity.
if (get_real_equity() > 0) {
ObjectSetText("status", "[RealEQ $" + DoubleToStr(get_real_equity(),2) + "][LiP $" + DoubleToStr(get_locked_profit(),2) + "][Cell " + (current_cell-1) + "-" + current_cell + "]",18,"Tahoma",Green);
} else {
ObjectSetText("status", "[RealEQ $" + DoubleToStr(get_real_equity(),2) + "][LiP $" + DoubleToStr(get_locked_profit(),2) + "][Cell " + (current_cell-1) + "-" + current_cell + "]",18,"Tahoma",Red);
}


elapsed_bars = Bars;
return(0);
}


double get_real_equity() {
double total_used = 0;
for (int index = 0; index < OrdersTotal(); index++) {
OrderSelect(index, SELECT_BY_POS);
if (OrderStopLoss() < OrderOpenPrice()) {
total_used = total_used + (OrderOpenPrice()*1000*OrderLots());
}
}
double xa_status_realequity = AccountBalance() - total_used;
return(xa_status_realequity);
}






//###############################################

void generate_checkerboard() {
bool flipflop = true;
for (int numlines = 0; numlines < 100; numlines++) {
ObjectCreate("cb"+numlines, OBJ_RECTANGLE, 0, Time[200], numlines, Time[0], numlines+1);
ObjectSet("cb"+numlines, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);
if (flipflop == true) {
ObjectSet("cb"+numlines, OBJPROP_COLOR, LightGray);
flipflop = false;
} else {
ObjectSet("cb"+numlines, OBJPROP_COLOR, AliceBlue);
flipflop = true;
}
}

}


double get_locked_profit() {
double total_locked_profit;
total_locked_profit = 0;
for (int index = 0; index < OrdersTotal(); index++) {
OrderSelect(index, SELECT_BY_POS);
if (OrderStopLoss() > OrderOpenPrice()) {
double delta_price;
delta_price = OrderStopLoss() - OrderOpenPrice();
total_locked_profit = total_locked_profit + (delta_price*OrderLots()*1000);
}
}
return(total_locked_profit);
}

 
Are you sure that this is doing what you think it is doing ? if (OrderStopLoss() < OrderOpenPrice()) do you only place Buy orders ?
 
  1. for (int index = 0; index < OrdersTotal(); index++) {
    OrderSelect(index, SELECT_BY_POS);
    This makes the EA incompatible with every other including itself on other charts and manual trading. If the orderSelect fails, everything below is bogus.
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){
    

  2. int start()
    {
    
    if (last_period != Period()) {
    ObjectsDeleteAll(0,OBJ_RECTANGLE);
    generate_checkerboard();
    last_period = Period();
    }
    Unnecessary check, Delete the objects in deinit and reset elapsed_bars to zero in init.
  3. simplify
    // if (flipflop == true) {
    // ObjectSet("cb"+numlines, OBJPROP_COLOR, LightGray);
    // flipflop = false;
    // } else {
    // ObjectSet("cb"+numlines, OBJPROP_COLOR, AliceBlue);
    // flipflop = true;
    // }
    color clr[2] = { LightGray, AliceBlue};
    ObjectSet("cb"+numlines, OBJPROP_COLOR, clr[numlines%2]);
    

 

RaptorUK - yes I only place buy orders.

WHRoeder: thanks for the snippets. Regarding snippet 1, this account only trades one pair, and one direction, orders are opened manually only, so there is no confusion. This account does one thing and one thing only.

Regarding snippet 2 - i dont think its unnecessary check, it triggers when the timeframe is changed, and later also triggers if the bar changes, but doesnt do anything per tick. I am pretty sure EA's and indicators don't deinit/reinit when changing timeframe? Either way I just noticed that it is also not updating well, so I implemented the change you suggested anyways.

I'll update the thread when I have more info to see if its still playing up.. thanks

 
When the timeframe is changed you go through a de-init, init cycle. UninitializeReason
case REASON_CHARTCHANGE:
Reason: