Live positions to csv

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Series array that contains open time of each barSeries array that contains tick volumes of each bar
Miscellaneous
Uses files from the file systemIt issuies visual alerts to the screenIt writes information to file
0 Views
0 Downloads
0 Favorites
Live positions to csv
ÿþ//+------------------------------------------------------------------+

//|                                        Live positions to csv.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property script_show_inputs

//---

#include <Trade\PositionInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

//--- input parameters

input string   InpFileName       = "LivePositions";   // File name (in 'Common Data Folder')

input bool     InpTicket         = true;

input bool     InpTime           = true;

input bool     InpType           = true;

input bool     InpVolume         = true;

input bool     InpPriceOpen      = true;

input bool     InpSL             = true;

input bool     InpTP             = true;

input bool     InpPriceCurrent   = true;

input bool     InpSwap           = true;

input bool     InpProfit         = true;

input bool     InpMagic          = true;

input bool     InpComment        = true;

//---

int   m_file_handle=0;

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

//| Script program start function                                    |

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

void OnStart()

  {

//--- open the file

   ResetLastError();

   m_file_handle=FileOpen(InpFileName+".csv",FILE_WRITE|FILE_CSV|FILE_COMMON);

   if(m_file_handle==INVALID_HANDLE)

     {

      Alert(StringFormat("Failed to open %s file, Error code = %d",InpFileName+".csv",GetLastError()));

      return;

     }

   string text="Symbol";

   if(InpTicket)

      text=text+"\t"+"Ticket";

   if(InpTime)

      text=text+"\t"+"Time";

   if(InpType)

      text=text+"\t"+"Type";

   if(InpVolume)

      text=text+"\t"+"Volume";

   if(InpPriceOpen)

      text=text+"\t"+"Price";

   if(InpSL)

      text=text+"\t"+"S/L";

   if(InpTP)

      text=text+"\t"+"T/P";

   if(InpPriceCurrent)

      text=text+"\t"+"Price";

   if(InpSwap)

      text=text+"\t"+"Swap";

   if(InpProfit)

      text=text+"\t"+"Profit";

   if(InpMagic)

      text=text+"\t"+"Magic";

   if(InpComment)

      text=text+"\t"+"Comment";

   FileWriteString(m_file_handle,text+"\r\n");

//---

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

        {

         text=m_position.Symbol();

         if(InpTicket)

            text=text+"\t"+IntegerToString(m_position.Ticket());

         if(InpTime)

            text=text+"\t"+TimeToString(m_position.Time(),TIME_DATE|TIME_SECONDS);

         if(InpType)

           {

            string pos_type=(m_position.PositionType()==POSITION_TYPE_BUY)?"buy":"sell";

            text=text+"\t"+pos_type;

           }

         if(InpVolume)

            text=text+"\t"+DoubleToString(m_position.Volume(),2);

         if(InpPriceOpen)

            text=text+"\t"+DoubleToString(m_position.PriceOpen(),8);

         if(InpSL)

            text=text+"\t"+DoubleToString(m_position.StopLoss(),8);

         if(InpTP)

            text=text+"\t"+DoubleToString(m_position.TakeProfit(),8);

         if(InpPriceCurrent)

            text=text+"\t"+DoubleToString(m_position.PriceCurrent(),8);

         if(InpSwap)

            text=text+"\t"+DoubleToString(m_position.Swap(),8);

         if(InpProfit)

            text=text+"\t"+DoubleToString(m_position.Profit(),8);

         if(InpMagic)

            text=text+"\t"+IntegerToString(m_position.Magic());

         if(InpComment)

            text=text+"\t"+m_position.Comment();

         //---

         FileWriteString(m_file_handle,text+"\r\n");

        }

//---

   FileClose(m_file_handle);

  }

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

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 ---