How can I solve the redraw of chart object after timeframe change?

 

Hi,


I have a problem. If I change the timeframe on a chart with EA running, the object what my EA writes stay unchanged. I like the write on the chart example the profit. After TF change the profit write refresh doesn't work, I see example 0 if the start profit was 0.

Here is my code from my EA:

int OnInit()
  {
//---
ObjectCreate(ChartID(),"profit_usd",OBJ_LABEL,0,0,0);                        
ObjectCreate(ChartID(),"profit_text",OBJ_LABEL,0,0,0);
ObjectSet("profit_usd",OBJPROP_XDISTANCE,1100);
ObjectSet("profit_usd",OBJPROP_YDISTANCE,20);                                  
ObjectSet("profit_usd",OBJPROP_COLOR,clrWhite);                                  
ObjectSet("profit_usd",OBJPROP_FONTSIZE,14);
ObjectSet("profit_text",OBJPROP_XDISTANCE,1040);
ObjectSet("profit_text",OBJPROP_YDISTANCE,20);                                   
ObjectSet("profit_text",OBJPROP_COLOR,clrWhite);                                   
ObjectSet("profit_text",OBJPROP_FONTSIZE,14);
ObjectSetString(ChartID(),"profit_text",OBJPROP_TEXT,"Profit:");
infowindow_upper=DoubleToString(total_profit,2);
ObjectSetString(ChartID(),"profit_usd",OBJPROP_TEXT,infowindow_upper);

...
...
void OnTick()
{.......
//in this session I rewrite the text if the profit changed.
infowindow_upper=DoubleToString(total_profit,2);
ObjectSetString(ChartID(),"profit_usd",OBJPROP_TEXT,infowindow_upper);

For me is not clear wich is better to use: ObjectSetString or ObjectSetText?

With the code above the "profit" will change on chart, BUT after I change the timeframe, the text stay unchanged and not refreshed if "profit" changes. Need I use ChartRedraw? I tried, but not lead to success.

Thanks for helping.

 

Your code should work as long as the variable total_profit is assigned the correct value

As you don't show how this variable is given a value, we are clueless. 

 
GumRai:

Your code should work as long as the variable total_profit is assigned the correct value

As you don't show how this variable is given a value, we are clueless. 

The code of total_profit I thought is unnecessary, but here is it:

close_order_profit=OrderProfit() + OrderSwap() + OrderCommission();
total_profit=total_profit+close_order_profit;

//and after this code comes

infowindow_upper=DoubleToString(total_profit,2);
ObjectSetString(ChartID(),"profit_usd",OBJPROP_TEXT,infowindow_upper);

The total_profit and rest of the code is working. My problem is not to give value to the total_profit variable, but this variable as chart text is not working if I change timeframe of the chart. Example:

the EA is running on a chart with TF H1. I change TF to H4 or M30 (it makes no difference) I will see 0. The 0 is the start value of total_profit. If I change back to TF H1 I will see the right value of total_profit on the chart.

i tried the code with ChartRedraw, but not works.

 
pecskeke1976:

The code of total_profit I thought is unnecessary, but here is it:

The total_profit and rest of the code is working. My problem is not to give value to the total_profit variable, but this variable as chart text is not working if I change timeframe of the chart. Example:

the EA is running on a chart with TF H1. I change TF to H4 or M30 (it makes no difference) I will see 0. The 0 is the start value of total_profit. If I change back to TF H1 I will see the right value of total_profit on the chart.

i tried the code with ChartRedraw, but not works.

Your code does not retrieve a certain value

close_order_profit=OrderProfit() + OrderSwap() + OrderCommission();
total_profit=total_profit+close_order_profit;

 There is no loop.

No order is selected.

We have no idea whether the  variables are declared locally or global-scope

 
GumRai:

Your code does not retrieve a certain value

 There is no loop.

No order is selected.

We have no idea whether the  variables are declared locally or global-scope

 

GumRai, why are you asking the loop and order selected code, because of the chart objects problem? This loop and orderselect are working good.

I have problem with charts object and refreshing them with

ObjectSetString
 
pecskeke1976:

GumRai, why are you asking the loop and order selected code, because of the chart objects problem? This loop and orderselect are working good.

I have problem with charts object and refreshing them with

OK fair enough. I can't help you with the info that you have given, maybe somebody else can.
 
pecskeke1976:

Hi,

I have a problem. If I change the timeframe on a chart with EA running, the object what my EA writes stay unchanged. I like the write on the chart example the profit. After TF change the profit write refresh doesn't work, I see example 0 if the start profit was 0.

Here is my code from my EA:

For me is not clear wich is better to use: ObjectSetString or ObjectSetText?

With the code above the "profit" will change on chart, BUT after I change the timeframe, the text stay unchanged and not refreshed if "profit" changes. Need I use ChartRedraw? I tried, but not lead to success.

Thanks for helping.

May be reading the reference about ObjectSetString(..) = "The function sets the value of the corresponding object property. ..." and

ObjectSetText(..) = "The function changes the object description" would have helped you to clarify the difference?

I guess one can be seen on the chart the other not..?



 
gooly:

May be reading the reference about ObjectSetString(..) = "The function sets the value of the corresponding objectproperty. ..." and

ObjectSetText(..) = "The function changesthe object description" would have helped you to clarify the difference?

I guess one can be seen on the chart the other not..?



I read it. As I wrote, the first text appeared with the code above, but it will not change if I change timeframe. If I change back the TF the chart text is working. As far as I can see the ChartID() depend on chart timeframe?
 

1) ChartID() gives you the ChartID of the current chart. Check (Comment(ChartId()) if it changes if you change the timeframe..?

2) Can it be that your Objects hasn't any different information to display if you change the timeframe? Add _Periods to find out.. ?

 
gooly:

1) ChartID() gives you the ChartID of the current chart. Check (Comment(ChartId()) if it changes if you change the timeframe..?

2) Can it be that your Objects hasn't any different information to display if you change the timeframe? Add _Periods to find out.. ?

2) I start the EA, takes trade, profit 0. The EA writes it on the chart. I see "Profit: 0" on the chart. After this the trade makes +10 usd profit and closed. This profit will be written on the chart with code above. Now I see "Profit: +10" on the chart. So far the code works good. Now if I change the timeframe of the current chart, I will see "Profit: 0" instead of "Profit: +10". Then I change the TF back, and I see "Profit: +10".
Why?
 
The the problem is how you calculate total_profit - you didn't post the code.
Reason: