Author: Copyright © 2017, Vladimir Hlystov
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
cm_TP_NL
ÿþ//+------------------------------------------------------------------+

//|                               Copyright © 2017, Vladimir Hlystov |

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

#property copyright "Copyright © 2017, Vladimir Hlystov"

#property link      "cmillion@narod.ru"

#property version   "1.00"

#property strict

#property description "!>25B=8: 2KAB02;O5B B59:?@>D8B 2A5< ?>78F8O< =0 @0AAB>O=88 AC<<0@=>3> 157C1KB:0"

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

extern int    Takeprofit   = 50;

extern int    Magic        = -1;

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

string AC;

double MINLOT,MAXLOT;

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

void OnTick() {OnTimer();}

void OnTimer()

  {

   double STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

   double OL=0,LB=0,LS=0,PB=0,PS=0;

   double OSL,OTP,OOP;

   int i,tip;

   for(i=0; i<OrdersTotal(); i++)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && (Magic==OrderMagicNumber() || Magic==-1))

           {

            tip = OrderType();

            OOP = NormalizeDouble(OrderOpenPrice(),Digits);

            OL=OrderLots();

            if(tip==OP_BUY)

              {

               PB+=OOP*OL; LB+=OL;

              }

            if(tip==OP_SELL)

              {

               PS+=OOP*OL; LS+=OL;

              }

           }

        }

     }

//---

   if(LB+LS>0)

     {

      double NLb=0,NLs=0;

      if(LB>0) NLb = NormalizeDouble(PB/LB,Digits);

      if(LS>0) NLs = NormalizeDouble(PS/LS,Digits);

      DrawArrow(0,"cm NL Buy",NLb,clrBlue);

      DrawArrow(0,"cm NL Sell",NLs,clrRed);



      double TPB=0,TPS=0;

      if(LB>0) TPB = NormalizeDouble(NLb+Takeprofit*Point,Digits);

      if(LS>0) TPS = NormalizeDouble(NLs-Takeprofit*Point,Digits);

      //---



      for(i=0; i<OrdersTotal(); i++)

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

           {

            if(OrderSymbol()==Symbol() && (Magic==OrderMagicNumber() || Magic==-1))

              {

               tip = OrderType();

               OSL = NormalizeDouble(OrderStopLoss(),Digits);

               OTP = NormalizeDouble(OrderTakeProfit(),Digits);

               OOP = NormalizeDouble(OrderOpenPrice(),Digits);

               if(tip==OP_BUY)

                 {

                  if(TPB!=OTP)

                    {

                     if(OTP==0 && (TPB-Ask)/Point<STOPLEVEL) TPB = NormalizeDouble(Ask+STOPLEVEL*Point,Digits);

                     if(!OrderModify(OrderTicket(),OOP,OSL,TPB,0,White))

                        Print("Error OrderModify TP Martin ",GetLastError());

                    }

                 }

               if(tip==OP_SELL)

                 {

                  if(TPS!=OTP)

                    {

                     if(OTP==0 && (Bid-TPS)/Point<STOPLEVEL) TPS = NormalizeDouble(Bid-STOPLEVEL*Point,Digits);

                     if(!OrderModify(OrderTicket(),OOP,OSL,TPS,0,White))

                        Print("Error OrderModify TP Martin ",GetLastError());

                    }

                 }

              }

           }

        }

     }

   return;

  }

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

void OnDeinit(const int reason)

  {

   EventKillTimer();

   Print(":>=G0=85 @01>BK A>25B=8:0 cm_TP_NL");

  }

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

int OnInit()

  {

   EventSetTimer(1);

   Print("!B0@B A>25B=8:0 cm_TP_NL");

   return(INIT_SUCCEEDED);

  }

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

void DrawArrow(int win,string name,double price,color colr)

  {

   ObjectDelete(name);

   ObjectCreate(name,OBJ_ARROW,win,Time[0],price,0,0,0,0);

   ObjectSet(name,OBJPROP_ARROWCODE,6);

   ObjectSet(name,OBJPROP_COLOR,colr);

  }

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

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