Stochastic_v5

Author: Safe-Forex.ru

Okay, here's a breakdown of what this MetaTrader script does, explained in plain language for someone who doesn't code:

Overall Purpose

This script is designed to automatically open and manage trades on the MetaTrader platform, based on signals generated by the Stochastic Oscillator indicator. The Stochastic Oscillator is a tool used in technical analysis to predict potential price movements. The script is designed to automatically enter and manage positions based on these signals.

Here's how it works:

  1. Initial Setup:

    • When the script starts, it reads in a bunch of settings that you can customize. These settings control things like the size of the trades, the take profit and stop loss levels (more on those later), and the settings for the Stochastic Oscillator.
  2. Watching the Market (Tick by Tick):

    • The script constantly monitors the price movements of the currency pair it's attached to.
    • Every time there is a price change, the OnTick function is triggered.
  3. New Candle Check:

    • It checks if a new candle has formed in the chart.
  4. Signal Check:

    • The script uses the Stochastic Oscillator to look for potential trading opportunities.
    • It looks at two lines from the Stochastic Oscillator (the main line and the signal line)
  5. Buy and Sell Conditions:

    • If there is a buy signal based on the Stochastic Oscillator
      • It checks if there is already an open position
      • If there are no positions, it sets the allowOpenBuy to true.
      • A message indicating a possible "Buy" is printed.
    • If there is a sell signal based on the Stochastic Oscillator
      • It checks if there is already an open position
      • If there are no positions, it sets the allowOpenSell to true.
      • A message indicating a possible "Sell" is printed.
  6. Opening Trades:

    • If the allowOpenBuy is true, it will open a buy order.
    • If the allowOpenSell is true, it will open a sell order.
  7. Setting Stop Loss and Take Profit:

    • For each trade, the script automatically sets a "stop loss" and a "take profit" level. These are safety nets:
      • Stop Loss: If the price moves against the trade, the trade will automatically close at the stop loss level to limit potential losses.
      • Take Profit: If the price moves in favor of the trade, the trade will automatically close at the take profit level to secure the profit.
      • The script also checks the validity of the stop loss and take profit levels in order to avoid errors from the server.

In Summary:

The script is an automated trading tool that uses the Stochastic Oscillator to generate trading signals, and then automatically opens and manages trades based on those signals, with built-in risk management in the form of stop loss and take profit levels.

Orders Execution
It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyChecks for the total of open orders
Indicators Used
Stochastic oscillator
2 Views
0 Downloads
0 Favorites
Stochastic_v5
ÿþ//+-------------------------------------------------------------------------------------------------------------------------------------------------+

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

//| !>25B=8::    Stochastic.mq4                                                                                                                     |

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

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

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

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

#property copyright "Safe-Forex.ru"

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

#property strict



input string         settings      = "";          // 0AB@>9:8 A>25B=8:0

input int            magic         = 111;         // 038:

input double         fixVolume     = 0.1;         // 1J5<

input int            takeProfit    = 20;          // @81K;L

input int            stopLoss      = 20;          // #1KB>:

input int            buyLevel      = 20;          // #@>25=L ?>:C?:8

input int            sellLevel     = 80;          // #@>25=L ?@>4068



input string         stochSettings = "";          // 0AB@>9:8 8=48:0B>@0 Stochastic Oscillator

input int            stochPeriodK  = 5;           // 5@8>4 %K

input int            stochPeriodD  = 3;           // 5@8>4 %D

input int            stochSlowing  = 3;           // 0<54;5=85

input ENUM_STO_PRICE stochPrice    = STO_LOWHIGH; // &5=K

input ENUM_MA_METHOD stochMethod   = MODE_SMA;    // 5B>4



datetime newCandle;     // =>20O A25G0 =0 B5:CI5< 3@0D8:5

bool     allowOpenBuy,  // @07@5H5=85 >B:@KBL ?>78F8N Buy

         allowOpenSell; // @07@5H5=85 >B:@KBL ?>78F8N Sell

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

int OnInit(void)

{

   return INIT_SUCCEEDED;

}

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

void OnDeinit(const int reason)

{

}

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

void OnTick(void)

{

   if(newCandle!=Time[0]) CheckSignalExist(); newCandle=Time[0];

   

   if(allowOpenBuy) OpenPosition(OP_BUY);

   if(allowOpenSell) OpenPosition(OP_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)

      {

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

         allowOpenBuy=false;

         SetStopOrders(ticket);

      }

      if(type==OP_SELL)

      {

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

         allowOpenSell=false;

         SetStopOrders(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 CAB0=02;8205B AB>? >@45@0                                                                                                               |

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

void SetStopOrders(int ticket)

{

   double stopLevel=MarketInfo(_Symbol,MODE_STOPLEVEL),

          spread   =MarketInfo(_Symbol,MODE_SPREAD),

          tp       =takeProfit,

          sl       =stopLoss;

   

   stopLevel+=spread;

   

   if(tp<stopLevel)

   {

      tp=stopLevel;

      Print("#AB0=>2;5=0 <8=8<0;L=0O 48AB0=F8O 4;O ?@81K;8");

   }

   if(sl<stopLevel)

   {

      sl=stopLevel;

      Print("#AB0=>2;5=0 <8=8<0;L=0O 48AB0=F8O 4;O C1KB:0");

   }

   if(OrderSelect(ticket,SELECT_BY_TICKET))

   {

      int    type=OrderType();

      double opp =OrderOpenPrice();

      

      if(type==OP_BUY)  {tp=opp+tp*_Point; sl=opp-sl*_Point;}

      if(type==OP_SELL) {tp=opp-tp*_Point; sl=opp+sl*_Point;}

      

      if(OrderModify(ticket,opp,sl,tp,0))

      {

         if(type==OP_BUY) Print("#AB0=>28;8AL C@>2=8 ?@81K;8 8 C1KB:0 4;O ?>78F88 Buy, B8:5B: ",ticket);

         if(type==OP_SELL) Print("#AB0=>28;8AL C@>2=8 ?@81K;8 8 C1KB:0 4;O ?>78F88 Sell, B8:5B: ",ticket);

      }

      else {

         if(type==OP_BUY) Print("#@>2=8 ?@81K;8 8 C1KB:0 4;O ?>78F88 Buy =5 CAB0=>28;8AL, B8:5B: ",ticket,", >H81:0: ",GetLastError());

         if(type==OP_SELL) Print("#@>2=8 ?@81K;8 8 C1KB:0 4;O ?>78F88 Sell =5 CAB0=>28;8AL, B8:5B: ",ticket,", >H81:0: ",GetLastError());

      }

   }

}

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

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

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

void CheckSignalExist(void)

{

   double mainLine1=iStochastic(_Symbol,PERIOD_CURRENT,stochPeriodK,stochPeriodD,stochSlowing,stochMethod,stochPrice,MODE_MAIN,1),

          mainLine2=iStochastic(_Symbol,PERIOD_CURRENT,stochPeriodK,stochPeriodD,stochSlowing,stochMethod,stochPrice,MODE_MAIN,2),

          signLine1=iStochastic(_Symbol,PERIOD_CURRENT,stochPeriodK,stochPeriodD,stochSlowing,stochMethod,stochPrice,MODE_SIGNAL,1),

          signLine2=iStochastic(_Symbol,PERIOD_CURRENT,stochPeriodK,stochPeriodD,stochSlowing,stochMethod,stochPrice,MODE_SIGNAL,2);

   

   if(mainLine1>signLine1 && mainLine2<signLine2 && mainLine1<buyLevel && mainLine2<buyLevel && signLine1<buyLevel && signLine2<buyLevel)

   {

      Print(">O28;AO A83=0; 4;O >B:@KB8O ?>78F88 Buy");

      if(GetPositionsNumber()==0)

      {

         allowOpenBuy=true;

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

      }

   }

   if(mainLine1<signLine1 && mainLine2>signLine2 && mainLine1>sellLevel && mainLine2>sellLevel && signLine1>sellLevel && signLine2>sellLevel)

   {

      Print(">O28;AO A83=0; 4;O >B:@KB8O ?>78F88 Sell");

      if(GetPositionsNumber()==0)

      {

         allowOpenSell=true;

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

      }

   }

}

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

//| $C=:F8O 2>72@0I05B :>;8G5AB2> >B:@KBKE ?>78F89                                                                                                  |

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

int GetPositionsNumber(void)

{

   int number=0;

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

      if(OrderSelect(i,SELECT_BY_POS))

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

            number++;

   return number;

}

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

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