Agressao_Salvar_CSV

Author: Sidnei da Silva Santos Junior
Miscellaneous
Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
Agressao_Salvar_CSV
ÿþ//+------------------------------------------------------------------+

//|                                            Saldo de Agressão.mqh |

//|                               Copyright © 2021, Sidnei S. Junior |

//|                                   sidneijunior@icloud.com        |

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

#property copyright                 "Sidnei da Silva Santos Junior"

#property link                      "sidneijunior@icloud.com"

#property version                   "1.00"

#property description               "TR Saldo de Agressao"

#property indicator_plots 1

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_type1  DRAW_COLOR_HISTOGRAM

#property indicator_style1 STYLE_SOLID

#property indicator_color1 clrForestGreen,clrRed

#property indicator_width1 3



int                        _INPticks            = 26;            // Numero de Ticks resquisitados

double SaldoP[],SaldoP_COR[], volume_compra,volume_venda, balanco;

MqlTick tick_array[];

int handle_file;

int OnInit()

  {

   SetIndexBuffer(0,SaldoP,INDICATOR_DATA);

   SetIndexBuffer(1,SaldoP_COR,INDICATOR_COLOR_INDEX);

   ArrayInitialize(SaldoP,0);

   ArrayInitialize(SaldoP_COR,EMPTY_VALUE);

   handle_file=FileOpen("Dados de Agressao "+Symbol()+" - "+TimeToString(TimeCurrent(),TIME_DATE)+".csv",FILE_COMMON|FILE_WRITE|FILE_CSV);

   FileWrite(handle_file,"Data e hora","Compradores","Vendedores","Balanco");



   return(INIT_SUCCEEDED);

  }







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

//|                                                                  |

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

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[])

  {



   if(prev_calculated==0)

     {

      ArrayInitialize(SaldoP,EMPTY_VALUE);

      ArrayInitialize(SaldoP_COR,EMPTY_VALUE);

     }

   else

     {



      int copied=CopyTicks(Symbol(),tick_array,COPY_TICKS_TRADE,0,_INPticks);

      ArraySetAsSeries(tick_array,true);



      if(copied>0)

        {

         for(int i=0; i<_INPticks; i++)

           {

            MqlTick tick=tick_array[i]; //Requisitando informações do tick

            bool buy    = tick.flags == TICK_FLAG_BUY+TICK_FLAG_VOLUME+TICK_FLAG_LAST; //Checagem dos tipos de flags

            bool sell   = tick.flags == TICK_FLAG_SELL+TICK_FLAG_VOLUME+TICK_FLAG_LAST;

            if(buy)

              {

               volume_compra+=(double)tick.volume; //Se o flag for comprador, somar ao 'volume_compra'

              }

            else

               if(sell)

                 {

                  volume_venda+=(double)tick.volume; //Se o flag for vendedorm, somar ao 'volume_venda'

                 }

           }

        }



      balanco = volume_compra-volume_venda;//Tira balanço

      SaldoP[rates_total-1]=balanco/100; //Plota no histograma o valor - NOTA: a divisão por 100 é apenas para ajustar a escala mais próxima as outras plataformas brasileiras



      if(SaldoP[rates_total-1]>0)

        {

         SaldoP_COR[rates_total-1]=0; //Colore de verde para saldo comprador

        }

      else

        {

         SaldoP_COR[rates_total-1]=1; //Colore de verm para saldo vendedor

        }

        

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

   //|   Caso haja uma nova barra, grave os dados da barra anterior,    |

   //|   Zere as variaveis, e checar por horário para fechar o arquivo  |

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

      

      if(isNewBar())

        {

         FileWrite(handle_file,TimeToString(TimeTradeServer(),TIME_MINUTES),volume_compra,volume_venda,balanco);

         volume_compra=0;

         volume_venda=0;

        }

      if((TimeToString(TimeTradeServer(),TIME_MINUTES))=="19:00")

        {

         FileClose(handle_file);

        }

     }

   return(rates_total);

  }

  

  

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

//| Checagem por uma nova barra                                      |

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

bool isNewBar()

  {

//--- memorize the time of opening of the last bar in the static variable

   static datetime last_time=0;

//--- current time

   datetime lastbar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);



//--- if it is the first call of the function

   if(last_time==0)

     {

      //--- set the time and exit

      last_time=lastbar_time;

      return(false);

     }



//--- if the time differs

   if(last_time!=lastbar_time)

     {

      //--- memorize the time and return true

      last_time=lastbar_time;

      return(true);

     }

//--- if we passed to this line, then the bar is not new; return false

   return(false);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---