canal-m_a_sim-v2

Author: m_a_sim@mail.ru - ������� ������

This script is designed to automate trading on the MetaTrader platform based on certain technical indicators. Here's a breakdown of how it works:

Overall Strategy:

The script tries to identify potential buying and selling opportunities based on the relative positions of a few custom indicators and moving averages. It essentially attempts to buy when prices are low relative to a calculated range, and sell when prices are high.

Key Components:

  1. Initialization: When the script starts, it sets up some initial parameters (variables). These parameters act like settings for the trading strategy, such as:

    • period, period1, period2: These determine how far back in time certain indicator calculations look (think of it like the "sensitivity" of the indicators).
    • stopmax and stopmin: These define the maximum and minimum range to control the size of the automatic stop-loss used to protect the trade.
    • Hours and Hours1: Defines after how much time trades are closed.
    • lot: The amount of currency to trade in each transaction.
    • magicBUY, magicSELL, magicBUY1, magicSELL1: Unique identification numbers for each type of trade the script places. This helps the script manage its own orders without interfering with manually placed trades or other automated strategies.
  2. Data Collection: The script regularly checks the current market conditions and indicator values. It calculates:

    • MAX and MIN: Highest and lowest values calculated using a custom indicator named "trend_v3_5." This likely defines an upper and lower boundary or channel.
    • H: Half the distance between MAX and MIN.
    • A0, A1, A2: Values from other custom indicators ("trend_v3" and "trend_v2"). These are used as triggers for initiating buy or sell orders based on how they relate to MAX and MIN.
  3. Trading Logic (Buy Orders):

    • The script checks if a "buy" order of type "magicBUY" is already open. If not, it looks for a situation where indicator A0 is below the MIN line AND A1 is greater than or equal to zero.
    • If both conditions are met, it places a buy order.
    • The stop variable is calculated based on H, the distance between MAX and MIN, acting as a stop loss.
    • The script also includes a similar logic block for opening buy orders of type "magicBUY1", using A0, MAX, and A2 with different rules, opening trades only if A0 is above the MAX line AND A2 is less than or equal to MAX.
    • For both the script checks if the current order is older than a set amount of hours ("Hours" and "Hours1"). If it is and profitable, the script closes it.
  4. Trading Logic (Sell Orders):

    • The script checks if a "sell" order of type "magicSELL" is already open. If not, it checks if A0 is above the MAX line AND A1 is less than or equal to zero.
    • If these conditions are met, it places a sell order.
    • The stop variable is calculated based on H, the distance between MAX and MIN, acting as a stop loss.
    • Similar to the buy orders, the script has another logic block for opening sell orders of type "magicSELL1", using A0, MIN, and A2 with the rule A0 being below the MIN line and A2 being greater than or equal to MIN.
    • For both the script checks if the current order is older than a set amount of hours ("Hours" and "Hours1"). If it is and profitable, the script closes it.
  5. Order Management:

    • The script keeps track of the orders it has placed using the "magic number".
    • After a set amount of hours has passed since opening a trade, the script check if it is profitable. If so, the trade gets closed.

In Summary: This script is an automated trading system that uses custom indicators to find potential buy and sell signals, places orders, and manages them by closing them automatically after a specific duration if in profit.

Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
4 Views
0 Downloads
0 Favorites
canal-m_a_sim-v2
//+------------------------------------------------------------------+
//|                                             canal-m_a_sim-v2.mq4 |
//|                                  m_a_sim@mail.ru - Ñèìàêîâ Ìèõàèë|
//|                             http://www.mql4.com/ru/users/m_a_sim |
//+------------------------------------------------------------------+

#property copyright "m_a_sim@mail.ru - Ñèìàêîâ Ìèõàèë"
#property link      "http://www.mql4.com/ru/users/m_a_sim"


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern int period =200;
extern int period1 =6;
extern int period2 =6;
extern int stopmax=200;
extern int stopmin=100;
extern int Hours=72;
extern int Hours1=72;
extern double lot=0.1;
extern int magicBUY=101;
extern int magicSELL=201;
extern int magicBUY1=102;
extern int magicSELL1=202;
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int bar;
double MAX,MIN,H,A0,A1,A2;
int start()
  {
//----
      int   i, j,jj, k, g, q, ticket,l;
    double stop;
if (bar!=Bars ){
MAX=iCustom(NULL,0,"trend_v3_5",period,1,1);
MIN=iCustom(NULL,0,"trend_v3_5",period,2,1);
H=(MAX-MIN)/2;
A0=iCustom(NULL,0,"trend_v3",period1,0,1);
A2=iCustom(NULL,0,"trend_v3",period1,0,2);
A1=iCustom(NULL,0,"trend_v2",period2,0,1);
bar=Bars;
}

//BUY=====================================

jj=0;
 if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++){
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 if(OrderMagicNumber()==magicBUY){jj=1;}
 }}


if (jj==0 ){
if (A0<MIN && A1>=0){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_BUY ,lot,NormalizeDouble(Ask, Digits),5,Bid-stop,0," ",magicBUY,0, Blue );
}}
  
  if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber()==magicBUY){
      if ( Time[0]-OrderOpenTime()>Hours*60*60 ) {
      if (OrderOpenPrice()<Bid ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid, Digits),5,Violet); }
    }}}}
  
   
 jj=0;
 if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++){
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 if(OrderMagicNumber()==magicBUY1){jj=1;}
 }}


if (jj==0 ){
if (A0>MAX && A2<=MAX){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_BUY ,lot,NormalizeDouble(Ask, Digits),5,Bid-stop,0," ",magicBUY1,0, Blue );
}}

if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber()==magicBUY1){
      if ( Time[0]-OrderOpenTime()>Hours1*60*60 ) {
      if (OrderOpenPrice()<Bid ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid, Digits),5,Violet); }
    }}}}    
//SELL================================

jj=0;
 if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++){
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 if(OrderMagicNumber()==magicSELL){jj=1;}
 }}


if (jj==0 ){
if (A0>MAX && A1<=0){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_SELL ,lot,NormalizeDouble(Bid, Digits),5,Ask+stop,0," ",magicSELL,0, Blue );
}}

if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber()==magicSELL){
      if ( Time[0]-OrderOpenTime()>Hours*60*60  ) {
      if (OrderOpenPrice()>Ask ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask, Digits),5,Violet); }
    }}}}
//----
jj=0;
 if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++){
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 if(OrderMagicNumber()==magicSELL1){jj=1;}
 }}


if (jj==0 ){
if (A0<MIN && A2>=MIN){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_SELL ,lot,NormalizeDouble(Bid, Digits),5,Ask+stop,0," ",magicSELL1,0, Blue );
}}
   if (OrdersTotal()>0){
 for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber()==magicSELL1){
      if ( Time[0]-OrderOpenTime()>Hours1*60*60  ) {
      if (OrderOpenPrice()>Ask ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask, Digits),5,Violet); }
    }}}}
   return(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 ---