Author: Safe-Forex.ru
Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Week Grid
ÿþ//+-------------------------------------------------------------------------------------------------------------------------------------------------+

//|  07@01>BG8:: 8=052 =4@59                                                                                                                      |

//| !>25B=8::    Week Grid.mql4                                                                                                                     |

//| >GB0:       minaev.work@mail.ru                                                                                                                |

//| !:09?:       live:minaev.work                                                                                                                   |

//| !09B:        safe-forex.ru                                                                                                                      |

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

#property copyright "Safe-Forex.ru"

#property link      "https://safe-forex.ru"

#property strict



extern int    magic         = 123; // 038:

extern double fixVolume     = 0.1; // 1J5<

extern double maxCandleSize = 200; // 0:A8<0;L=K9 @07<5@ =545;L=>9 A25G8

extern double minCandleSize = 100; // 8=8<0;L=K9 @07<5@ =545;L=>9 A25G8

extern double profitInCurr  = 10;  // @81K;L

extern double gridStep      = 50;  // (03 A5B:8



datetime newWeek;

bool     allowOpenBuy,

         allowOpenSell;

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

int OnInit(void)

{

   return INIT_SUCCEEDED;

}

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

void OnDeinit(const int reason)

{   

}

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

void OnTick(void)

{

   CheckSignalExist();

   CheckPositionsProfit();

   

   if(allowOpenBuy) OpenPosition(OP_BUY);

   if(allowOpenSell) OpenPosition(OP_SELL);

   

   if(CountPositionsNumber(OP_BUY)>0)

   {

      double dist=(GetLastOpenPrice(OP_BUY)-Ask)/_Point;

      if(gridStep<=dist)

      {

         allowOpenBuy=true;

         Print(" 07@5H5=> >B:@KBL ?>78F8N Buy");

      }

   }

   if(CountPositionsNumber(OP_SELL)>0)

   {

      double dist=(Bid-GetLastOpenPrice(OP_SELL))/_Point;

      if(gridStep<=dist)

      {

         allowOpenSell=true;

         Print(" 07@5H5=> >B:@KBL ?>78F8N Sell");

      }

   }

}

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

//| $C=:F8O >B:@K205B ?>78F8N ?> B5:CI59 F5=5                                                                                                       |

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

void OpenPosition(int type)

{

   double price=0.0;

   if(type==OP_BUY)  price=Ask;

   if(type==OP_SELL) price=Bid;

   

   int ticket=OrderSend(_Symbol,type,fixVolume,price,0,0,0,"",magic,0);

   

   if(ticket>0)

   {

      if(type==OP_BUY)

      {

         allowOpenBuy=false;

         Print("B:@K;0AL ?>78F8O Buy, B8:5B: ",ticket);

      }

      if(type==OP_SELL)

      {

         allowOpenSell=false;

         Print("B:@K;0AL ?>78F8O Sell, B8:5B: ",ticket);

      }

   }

   if(ticket<0)

   {

      if(type==OP_BUY)  Print(">78F8O Buy =5 >B:@K;0AL, >H81:0: ", GetLastError());

      if(type==OP_SELL) Print(">78F8O Sell =5 >B:@K;0AL, >H81:0: ",GetLastError());

   }

}

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

//| $C=:F8O 70:@K205B ?>78F8N ?> B5:CI59 F5=5                                                                                                       |

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

void ClosePosition(void)

{

   for(int i=OrdersTotal()-1; i>=0; i--)

      if(OrderSelect(i,SELECT_BY_POS))

         if(OrderSymbol()==_Symbol && OrderMagicNumber()==magic)

         {

            if(OrderType()==OP_BUY)

               if(OrderClose(OrderTicket(),OrderLots(),Bid,0))

                  Print("0:@K;0AL ?>78F8O Buy, B8:5B: ", OrderTicket());

               else

                  Print(">78F8O Buy =5 70:@K;0AL, B8:5B: ", OrderTicket(),", >H81:0: ",GetLastError());

                  

            if(OrderType()==OP_SELL)

               if(OrderClose(OrderTicket(),OrderLots(),Ask,0))

                  Print("0:@K;0AL ?>78F8O Sell, B8:5B: ", OrderTicket());

               else

                  Print(">78F8O Sell =5 70:@K;0AL, B8:5B: ", OrderTicket(),", >H81:0: ",GetLastError());

         }

}

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

//| $C=:F8O 2>72@0I05B F5=C >B:@KB8O ?>A;54=59 ?>78F88                                                                                              |

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

double GetLastOpenPrice(int type)

{

   double price=0;

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

      if(OrderSelect(i,SELECT_BY_POS))

         if(OrderSymbol()==_Symbol && OrderMagicNumber()==magic && OrderType()==type)

            price=OrderOpenPrice();

   return price;

}

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

//| $C=:F8O ?>4AG8BK205B :>;8G5AB2> >B:@KBKE ?>78F89                                                                                                |

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

int CountPositionsNumber(int type)

{

   int number=0;

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

      if(OrderSelect(i,SELECT_BY_POS))

         if(OrderSymbol()==_Symbol && OrderMagicNumber()==magic && OrderType()==type)

            number++;

   return number;

}

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

//| $C=:F8O ?@>25@O5B ?@>D8B >B:@KBKE ?>78F89                                                                                                       |

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

void CheckPositionsProfit(void)

{

   if(AccountBalance()+profitInCurr<=AccountEquity()) ClosePosition();

}

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

//| $C=:F8O ?@>25@O5B =0;8G85 A83=0;0                                                                                                               |

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

void CheckSignalExist(void)

{

   datetime currWeek=iTime(_Symbol,PERIOD_W1,0);

   if(newWeek==currWeek) return;

   newWeek=currWeek;

   

   if(CountPositionsNumber(OP_BUY)>0 || CountPositionsNumber(OP_SELL)>0) return;

   

   double open=iOpen(_Symbol,PERIOD_W1,1),

          close=iClose(_Symbol,PERIOD_W1,1),

          candleSize=0;

   

   candleSize=MathAbs(open-close)/_Point;

   

   if(minCandleSize<candleSize && candleSize<maxCandleSize)

   {

      if(open>close)

      {

         allowOpenBuy=true;

         Print(" 07@5H5=> >B:@KBL ?>78F8N Buy");

      }

      if(open<close)

      {

         allowOpenSell=true;

         Print(" 07@5H5=> >B:@KBL ?>78F8N Sell");

      }

   }

}

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

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