Total-risk-indicator // how to execute the OnStart() when a stoploss-level is moved?

Back to topics list To post a new topic, please log in or register
avatar
370
mar 2015.02.06 13:11
 
I made an indicator which shows the total risk of all running trades. I don't want to let it run and check all orders with every tick because this is not necessary and wastes processor time. Therefore I check if the number of open orders (OP_BUY and OP_SELL) have changed and only then I check all orders and calculate the total risk. But the problem is that the risk is also be changed if the stoploss is moved. But to check this I have to control if the stoploss of each order is still at the same level as it was one tick before. That's not my intention because then I have to do all calculations with every tick.

I wanted to use CHARTEVENT_OBJECT_DRAG but that doesn't work because the stoploss-lines from MT4 are no objects I can work with. So I thought about using a chart event like a mouse click. If a stoploss is changed it is usually done by dragging the stoploss level with the mouse.


Here is my attempt but it doesn't work like I want to. Does anyone have any advice for me? Maybe there is a better way instead of checking for a mouse click?

 

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   static int TotalOrders;
   if (OrdersTotal() != TotalOrders || mouseClick) {
      openOrders  = 0;
      totalRisk   = 0;
      for (int i = 0; i < OrdersTotal(); i++) {
         int direction = 0;
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderType() == OP_BUY) direction = 1;
            else if (OrderType() == OP_SELL) direction = -1;
            openOrders++;
            riskPips    = direction*(OrderOpenPrice() - OrderStopLoss());
            riskMoney   = riskPips/MarketInfo(OrderSymbol(), MODE_POINT)*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            totalRisk   = totalRisk + riskMoney;
         }
      }
      TotalOrders = OrdersTotal();
   }
   Comment((string)openOrders+" TRADES | TOTAL RISK: "+DoubleToStr(totalRisk, 2));
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_CLICK) mouseClick = true;
   else mouseClick = false;
  }
//+------------------------------------------------------------------+

 

avatar
12983
WHRoeder 2015.02.06 14:05 #
 
mar: Here is my attempt but it doesn't work like I want to.
You are setting it false for every other event. So you never see it.
   if (OrdersTotal() != TotalOrders || mouseClick) {   
:
   if(id==CHARTEVENT_CLICK) mouseClick = true;
   else mouseClick = false;
Clear it, only if it has been handled.
   if (OrdersTotal() != TotalOrders || mouseClick) {   
      mouseClick = false;
:
   if(id==CHARTEVENT_CLICK) mouseClick = true;         
//   else mouseClick = false;

avatar
370
mar 2015.02.06 16:13 #
 

Thank you! Works perfect!

I just think about the fact that one could also change the stoploss in the terminal. Then it is not a mouseclick anymore.

Do you have another idea how to check if a stoploss is changed without doing that each tick?

And I would beg you to have a look here: https://forum.mql4.com/66622

I am still trying to figure out if it is possible to wait for the current data to be loaded before taking a screenshot.

Thanks! 

Back to topics list  

To add comments, please log in or register