need help creating objects in chart

 

hi guys,

i coded something that if some condition becomes true it will create an object in my chart.

the object is a text and a rectangle.

my problem is that if i do backtests over a day/week/month, my code will only create 2-5 objects.

but i this periode of backtesting the code should create about 100 objects, not to be exact.


can you guys see where my problem is?

thanks in advance.

 
FlashX:

can you guys see where my problem is?

thanks in advance.

Your code is wrong . . .
 

but i dont find the problem ni my code...

shall i add my code that you can look for the mistake?

 

Yes, please do show your codes. We cannot debug what we cannot see.


 

i got another problem which is a bit more important right now:

can you say me why metatrader tells me that:

orderclose error 4051

invalid ticket for orderclose function


i dont understand why there should be an invalid ticket, because when i print the ordernumbers the counter goes up 1,2,3,4,...

the code is the following one:

int openOrderNumber = 0;

int start()
{
  ...if open buy/sell order then:
  
   openOrderNumber = createOrderNumber();
   
   OrderSelect(openOrderNumber, SELECT_BY_POS, MODE_TRADES);
   OrderClose(openOrderNumber,lots,price,slippage,0);

   return(0);
}


int createOrderNumber()
{
   openOrderNumber = openOrderNumber + 1;
   return(openOrderNumber);
}
 
  1. No mind readers here. No code - no help.
  2. Are you using unique object names? name+x where x could be a count or time. Otherwise you're reusing the same object and ObjectCreate only works once. For reuse I use this pattern in my code:
    void Rect(string name, datetime T0,double P0, datetime T1,double P1, color clr){
        if (!Show.Objects)  return;                         #define WINDOW_MAIN 0
        if      (ObjectMove( name, 0, T0, P0 ))     ObjectMove(name, 1, T1, P1);
        else if (!ObjectCreate( name, OBJ_RECTANGLE, WINDOW_MAIN, T0, P0, T1, P1 ))
            Alert("ObjectCreate(",name,",RECT) failed: ", GetLastError() );
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [3] failed: ", GetLastError());
        string  P0t = PriceToStr(P0);           if (MathAbs(P0 - P1) >= Point)
                P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));
        if (!ObjectSetText(name, P0t, 10))
            Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
    }
    

 

You need to ask if OrderSelect() is true or false.

OrderClose() only takes OrderTicket....Not order position.

Example:

    for(i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            if(OrderMagicNumber()==iMagic && OrderSymbol()==iSymbol_){
                if( MathAbs(OrderOpenPrice()-OrderClosePrice())>iPoint2Pip*iFreeze_Lv){
                    OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),iSlippage*iPip2Point,White);
                }
 
why is it i=OrdersTotal()-1? why -1?
 
The order position starts at 0, 1st is 0, 2nd is 1, etc . . so the last is OrderTotal() - 1
 

but OrdersTotal() represents the amount of open orders not their position!?


so if i have 2 open orders it say OrderTotal() = 2.

When i would say OrdersTotal()-1, then i would have 1 open order instead of 2.

so why should OrdersTotal represent positions and not an amount?

 
ubzen:

You need to ask if OrderSelect() is true or false.

OrderClose() only takes OrderTicket....Not order position.

Example:


ty for the code i tried it but it still says invalid tickets for orderclose function -.-
Reason: