OpenOrderTime

Price Data Components
Orders Execution
It automatically opens orders when conditions are reachedIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screenUses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
OpenOrderTime
ÿþ/*-----------------------------------------------------------------

     OpenOrderTime.mq4 

     Measure time of open and close of market orders

     Alexey Volchanskiy, 09 March 2016 

------------------------------------------------------------------*/

#property copyright "Alexey Volchanskiy http://mql4.wordpress.com"

#property version "1.03"

#property link    "http://mql4.wordpress.com"

#property description "Measure time of open and close of market orders"

#property show_inputs

#property strict



#include <stdlib.mqh>



enum ETradeOperations

{

   Buy,        //Buy

   Sell,       //Sell

   BuyLimit,   //Buy Limit   

   SellLimit,  //Sell Limit

   BuyStop,    //Buy Stop

   SellStop    //Sell Stop

};



input bool      CloseAfterOpen = true; 

input int       Operations     = 1;

input int       Delay          = 5000;

input double    LotSize        = 0.01;

input double    StopLoss       = 250;

input double    TakeProfit     = 100;

input int       SlipPage       = 30; 



int LogFile = -1;



string GetMyLastError()

{

   int err = GetLastError();

   string serr = ErrorDescription(err);

   return(serr);

}



int OnStart()

{

    double sl = 0, tp = 0, price = 0;

    string dts;

    if(WindowOnDropped() != 0) // if the script is not dropped in main window 

    {

        Alert("Please drop the script in main chart window");

        return (1);

    }

    

    string fileName = AccountCompany() + "_" + AccountName() + "_" + IntegerToString(AccountNumber()) + ".log";      

    Print("fileName = ", fileName);

    LogFile = FileOpen(fileName, FILE_READ |FILE_CSV | FILE_WRITE, ' ');

    if(LogFile < 1)

    {

        Alert("Can not open log-file: " + fileName, "   error=", GetMyLastError());

        return(-1);

    }    

    FileSeek(LogFile, 0, SEEK_END);



    double dropPrice = WindowPriceOnDropped();

    Print("dropPrice=", DoubleToStr(dropPrice, Digits()));

    int cmd = -1, clr = Aqua;

    uint dt1, dt2;

    for(int n = 0; n < Operations; n++)

    {

        RefreshRates();

        if(dropPrice > Ask)

        {

            cmd = OP_BUY;

            price = Ask;

            clr = Yellow;

            if(StopLoss > 0)

                sl = NormalizeDouble(Bid - StopLoss * _Point, _Digits);

            if(TakeProfit > 0)

                tp = NormalizeDouble(Bid + TakeProfit * _Point, _Digits);

        }

        if(dropPrice < Bid)

        {

            cmd = OP_SELL;

            price = Bid;

            clr = Aqua;

            if(StopLoss >= 1)

                sl = NormalizeDouble(Ask + StopLoss * _Point, _Digits);

            if(TakeProfit >= 1)

                tp = NormalizeDouble(Ask - TakeProfit * _Point, _Digits);

        }



        ResetLastError();

        dt1 = GetTickCount();      

        int ticket = OrderSend(Symbol(), cmd, LotSize, price, SlipPage, sl, tp, "OpenOrderTime", 0, 0, clr);

        dt2 = GetTickCount();  

        if(ticket >= 0)

        {    

            if (OrderSelect(ticket, SELECT_BY_TICKET))

            {

                double openPrice = OrderOpenPrice();

                datetime ordOpenTime = OrderOpenTime();

                // =C6=> 70?@0H820BL @50;L=K9 >1J5<, B.:. =0 AG5B0E ECN ?@8 ?>AK;:5 >@45@0 A 1>;LH8< >1J5<>< <>65B >B:@KBLAO =5A:>;L:> >@45@>2

                // A @07=K<8 >1J5<0<8 8 @07=K<8 F5=0<8 87-70 B>3>, GB> 2 AB0:0=5 ?@>AB> =5B 70O2:8 A =C6=K< ;>B>< ?> 70?@>H5==>9 F5=5

                double volume = OrderLots();

                double stoploss = OrderStopLoss();

                double takeprofit = OrderTakeProfit();

                WriteMsg("Open time, ms =" + IntegerToString(dt2-dt1), cmd, ticket, Symbol(), "volume=" + DoubleToStr(volume, 2) + "  request price=" + DoubleToStr(price, _Digits) + 

                    "  opened price=" + DoubleToStr(openPrice, _Digits) + "  sl=" + DoubleToStr(stoploss, _Digits) + "  tp=" + DoubleToStr(takeprofit, _Digits));

                    

                if(CloseAfterOpen)   

                {

                    Sleep(1000);    

                    double closePrice = 0;

                    RefreshRates();

                    switch(cmd)

                    {

                    case OP_BUY:

                        closePrice = Bid;  

                        break;

                    case OP_SELL:

                        closePrice = Ask;  

                        break;

                    }

                    ResetLastError();    

                    dt1 = GetTickCount();   

                    bool closeRes = OrderClose(ticket, OrderLots(), closePrice, SlipPage, clr); 

                    dt2 = GetTickCount();      

                    if(closeRes)

                        WriteMsg("Close time, ms =" + IntegerToString(dt2-dt1), cmd, ticket, Symbol(), "  request price=" + DoubleToStr(closePrice, _Digits) + 

                            "  closed price=" + DoubleToStr(OrderClosePrice(), _Digits));

                    else

                    {

                        WriteMsg("Can't close order", cmd, ticket, Symbol(), "   error=" + GetMyLastError());

                        FileClose(LogFile);  

                        return(-1);

                    }

                }

            }

            else

            {

                WriteMsg("Can't select ticket by OrderSelect function", cmd, ticket, Symbol(), "   error=" + GetMyLastError());

                FileClose(LogFile);  

                return(-1);

            }    

         }  

         else

         {

            WriteMsg("Can't open order", cmd, ticket, Symbol(), "   error=" + GetMyLastError());

            FileClose(LogFile);  

            return(-1);

         }           

         Sleep(Delay);   

    }    

    FileClose(LogFile);    

    return(0);

}



void WriteMsg(string msg0, int cmd, int orderTicket, string orderSymbol, string msg1)

{

    string dts = TimeToStr(TimeCurrent(), TIME_DATE | TIME_MINUTES | TIME_SECONDS);

    string op;

    switch (cmd)

    {

    case OP_BUY:        op = "BUY"; break;

    case OP_SELL:       op = "SELL"; break;

    case OP_BUYLIMIT:   op = "BUYLIMIT"; break;

    case OP_BUYSTOP:    op = "BUYSTOP"; break;

    case OP_SELLLIMIT:  op = "SELLLIMIT"; break;

    case OP_SELLSTOP:   op = "SELLSTOP"; break;

    default:            op = "NONE"; break;

    }

    FileWrite(LogFile, dts, "  " + msg0, "  Ticket= " + IntegerToString(orderTicket), "  " + op, "  " + orderSymbol, "  " + msg1);

}





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