RETAIL_NETTING_MONEY

Author: Copyright 2020, MetaQuotes Software Corp.
Orders Execution
Checks for the total of open orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
RETAIL_NETTING_MONEY
ÿþ//+------------------------------------------------------------------+

//|                                                        MONEY.mq4 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property script_show_inputs

input bool calcswap=true;//€Q†¹pî]ÓNo`

input double calcoutprice=0.0;//¡‹—{÷N<h)R¦m

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   double buy_lots = 0,sell_lots=0;

   double price_sum = 0;

   double value_sum = 0;

   double comiss_sum = 0;

   double calc_profit_sum =0;

   double max = DBL_MIN;

   double min = DBL_MAX;

   datetime now=TimeCurrent();

   int total=OrdersTotal();

   int num = 0;

   double diffs[][2];

   ArrayResize(diffs,total);

   ArrayInitialize(diffs,0.0);

//------

   for(int i = total - 1; i >= 0; i--)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==_Symbol)

           {

            double price=OrderOpenPrice();

            if(OrderType() == OP_BUY)

              {

               if(calcoutprice>0 && OrderProfit())

                  calc_profit_sum+=OrderSwap()+OrderProfit()*(calcoutprice-price)/(Bid - price);

               if(calcswap)

                 {

                  diffs[num][0] = OrderProfit() ? OrderSwap() * (Bid - price) /OrderProfit() : Ask - Bid;

                  diffs[num][1] = (int)(now-OrderOpenTime())/PeriodSeconds(PERIOD_D1);//iBarShift(_Symbol,PERIOD_D1,(datetime)OrderOpenTime());

                 }

               buy_lots +=OrderLots();

               value_sum +=OrderLots()*(price-diffs[num][0]);

              }

            if(OrderType() == OP_SELL)

              {

               if(calcoutprice>0 && OrderProfit())

                  calc_profit_sum+=OrderSwap()+OrderProfit()*(calcoutprice-price)/(Ask - price);

               if(calcswap)

                 {

                  diffs[num][0]  = OrderProfit() ? OrderSwap() * (Ask - price) /OrderProfit() :  Bid-Ask;

                  diffs[num][1] = (int)(now-OrderOpenTime())/PeriodSeconds(PERIOD_D1);

                 }

               sell_lots +=OrderLots();

               value_sum -=OrderLots()*(price-diffs[num][0]);

              }

            if(OrderType()<2 && price!=0)

              {

               max = MathMax(max,price);

               min = MathMin(min,price);

               price_sum += price;

               comiss_sum += OrderCommission();

               num++;

              }

           }

     }

   for(int i=num-1; i>=0; i--)

      printf("[%d],%.0f,%f",i,diffs[i][1],diffs[i][0]/_Point);

//-----------

   string name_0=_Symbol+"_weight";

   string name_1=_Symbol+"_avgp";

   string name_2=_Symbol+"_mean";

   if(buy_lots!=sell_lots)

     {

      double quantity=buy_lots-sell_lots;

      double avgp=NormalizeDouble(value_sum /quantity,_Digits);// total_lots

      printf("1:%f,%f,%f",quantity,avgp,comiss_sum/(buy_lots+sell_lots));



      if(ObjectFind(0,name_0)<0)

        {

         ObjectCreate(0,name_0,OBJ_HLINE,0,0,avgp);

         ObjectSetInteger(0,name_0,OBJPROP_COLOR,clrGold);

         ObjectSetInteger(0,name_0,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_0,0,0,avgp);

     }

   else

      ObjectDelete(0,name_0);

   if(calc_profit_sum!=0)

      Alert(StringFormat("price %f %s's profit:%f",calcoutprice,_Symbol,calc_profit_sum));

//--------------

   if(num > 0)

     {

      double avgp=NormalizeDouble(price_sum / num,_Digits);

      double mean=NormalizeDouble((max + min) / 2.0,_Digits);

      printf("2:%d,%f,%f,",num,avgp,mean);



      if(ObjectFind(0,name_1)<0)

        {

         ObjectCreate(0,name_1,OBJ_HLINE,0,0,avgp);

         ObjectSetInteger(0,name_1,OBJPROP_COLOR,clrLime);

         ObjectSetInteger(0,name_1,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_1,0,0,avgp);



      if(ObjectFind(0,name_2)<0)

        {

         ObjectCreate(0,name_2,OBJ_HLINE,0,0,mean);

         ObjectSetInteger(0,name_2,OBJPROP_COLOR,clrSnow);

         ObjectSetInteger(0,name_2,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_2,0,0,mean);

     }

   else

     {

      ObjectDelete(0,name_1);

      ObjectDelete(0,name_2);

     }

  }

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

Comments