Author: Copyright 2017, AM2
Price Data Components
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of closed ordersChecks for the total of open orders
0 Views
0 Downloads
0 Favorites
MT45
ÿþ//+------------------------------------------------------------------+

//|                                                        MT45.mq45 |

//|                                              Copyright 2017, AM2 |

//|                                      http://www.forexsystems.biz |

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

#property copyright "Copyright 2017, AM2"

#property link      "http://www.forexsystems.biz"

#property version   "1.00"

#property strict



//--- ?5@5>?@545;8< B8?K >@45@>2 87 MQL4 2 MQL5

#ifdef __MQL5__ 

#define OrdersTotal  PositionsTotal

#define OP_BUY  ORDER_TYPE_BUY        

#define OP_SELL ORDER_TYPE_SELL       

#endif 



input int    Stop = 600;  // AB>?;>AA

input int    Take = 700;  // B59:?@>D8B

input int    Slip = 100;  // ?@>A:0;L7K20=85

input int    MN   = 123;  // <038:

input double LT   = 0.01; // ;>B

input double KL   = 2;    // C25;8G5=85 ;>B0

input double ML   = 10;   // <0:A8<0;L=K9 ;>B



int bars=0;

bool b=true,s=true;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---



//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| >B:@K205B ?>78F8N                                                |

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

bool PutOrder(ENUM_ORDER_TYPE type,double price,double sl,double tp)

  {

//---  MQL4

#ifdef __MQL4__

   int ticket=OrderSend(_Symbol,type,Lot(),price,Slip,sl,tp,"",MN);

   if(ticket<0)

      PrintFormat("OrderSend error %d",GetLastError());

#endif

//---  MQL5         

#ifdef __MQL5__

//--- >1JO2;5=85 8 8=8F80;870F8O 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request={0};

   MqlTradeResult  result={0};

//--- ?0@0<5B@K 70?@>A0

   request.action   =TRADE_ACTION_DEAL;                     // B8? B>@3>2>9 >?5@0F88

   request.symbol   =Symbol();                              // A8<2>;

   request.volume   =Lot();                                 // >1J5< 

   request.type     =type;                                  // B8? >@45@0

   request.price    =price;                                 // F5=0 4;O >B:@KB8O

   request.sl       =sl;                                    // F5=0 StopLoss

   request.tp       =tp;                                    // F5=0 TakeProfit

   request.deviation=Slip;                                  // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

   request.magic    =MN;                                    // MagicNumber >@45@0

//--- >B?@02:0 70?@>A0

   if(!OrderSend(request,result))

      PrintFormat("OrderSend error %d",GetLastError());     // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

//--- 8=D>@<0F8O >1 >?5@0F88

   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

#endif  

//---

   return(false);

  }

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

//|                                                                  |

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

double Lot()

  {

   double lot=LT;

//---  MQL4

#ifdef __MQL4__

   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))

     {

      if(OrderProfit()>0) lot=LT;

      if(OrderProfit()<0) lot=OrderLots()*KL;

     }

#endif



//---  MQL5

#ifdef __MQL5__

   if(HistorySelect(0,TimeCurrent()))

     {

      double profit=HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_PROFIT);

      double LastLot=HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_VOLUME);

      if(profit>0) lot=LT;

      if(profit<0) lot=LastLot*KL;

     }

#endif



   if(lot>ML)lot=LT;

   return(lot);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

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



   double ASK=SymbolInfoDouble(_Symbol,SYMBOL_ASK);

   double BID=SymbolInfoDouble(_Symbol,SYMBOL_BID);



   if(bars!=Bars(_Symbol,0))

     {

      if(b && OrdersTotal()<1)

        {

         price=NormalizeDouble(ASK,_Digits);

         sl=NormalizeDouble(BID-Stop*_Point,_Digits);

         tp=NormalizeDouble(BID+Take*_Point,_Digits);

         PutOrder(0,ASK,sl,tp);

         b=false;s=true;

        }



      if(s && OrdersTotal()<1)

        {

         price=NormalizeDouble(BID,_Digits);

         sl=NormalizeDouble(ASK+Stop*_Point,_Digits);

         tp=NormalizeDouble(ASK-Take*_Point,_Digits);

         PutOrder(1,BID,sl,tp);

         s=false;b=true;

        }

      bars=Bars(_Symbol,0);

     }

  }

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

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