eaStochastic

Author: Yuriy Tokman (YTG)
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Stochastic oscillator
0 Views
0 Downloads
0 Favorites
eaStochastic
ÿþ//+------------------------------------------------------------------+

//|                                                 eaStochastic.mq5 |

//|                                               Yuriy Tokman (YTG) |

//|                       https://www.mql5.com/ru/users/satop/seller |

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

#property copyright "Yuriy Tokman (YTG)"

#property link      "https://www.mql5.com/ru/users/satop/seller"

#property version   "1.00"



input double                  Lot            = 0.1;              // >1J5<

input int                     SL_            = 100;              // AB>? ;>A

input int                     TP_            = 100;              // B59: ?@>D8B

input int                     MagicNumber    = 2808;             // <038:

input ENUM_ORDER_TYPE_FILLING fill           = ORDER_FILLING_FOK;// A?>A>1 8A?>;=5=8O

input int                     Slipage        = 30;               // ?@>A:0;L7K20=85

input bool                    IndicatorChart = true;             // 8=48:0B>@ =0 3@0D8:

input int                     Kperiod        = 5;                // K-?5@8>4 (:>;8G5AB2> 10@>2 4;O @0AG5B>2)

input int                     Dperiod        = 3;                // D-?5@8>4 (?5@8>4 ?5@28G=>3> A3;06820=8O)

input int                     slowing        = 3;                // ?5@8>4 4;O >:>=G0B5;L=>3> A3;06820=8O

input ENUM_MA_METHOD          ma_method      = MODE_SMA;         // B8? A3;06820=8O

input ENUM_STO_PRICE          price_field    = STO_LOWHIGH;      // A?>A>1 @0AG5B0 AB>E0AB8:0

input double                  level_up       = 80;               // 25@E=89 C@>25=L

input double                  level_dn       = 20;               // =86=89 C@>25=L

input int                     shift          = 1;                // A83=0;L=K9 10@



int    h_sth ;

double STH[3];

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   h_sth=iStochastic(Symbol(),PERIOD_CURRENT,Kperiod,Dperiod,slowing,ma_method,price_field);

//--- if the handle is not created

   if(h_sth==INVALID_HANDLE)

     {

      PrintFormat("Failed to create handle of the custom indicator for the symbol %s/%s, error code %d",

                  Symbol(),EnumToString(Period()),GetLastError());

      return(INIT_FAILED);

     }



   if(IndicatorChart)

     {

      ChartIndicatorAdd(0,1,h_sth);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   ResetLastError();

   if(CopyBuffer(h_sth,0,0,3,STH)<0)

     {

      PrintFormat("Failed to copy data from the custom indicator, error code %d",GetLastError());

      return;

     }

//---

   double ll =Lot;

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(ll<min_volume)

      ll=min_volume;

   int summ_b=0, summ_s=0;



   for(int cnt=0; cnt<=PositionsTotal(); cnt++)

     {

      if(PositionGetSymbol(cnt)==Symbol())

        {

         if(PositionGetInteger(POSITION_MAGIC) == MagicNumber)

           {

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

              {

               summ_b++;

              }

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

              {

               summ_s++;

              }

           }

        }

     }

//----

   if(STH_buy1() && summ_b+summ_s==0)

      OnBuy(ll);



   if(STH_sell1() && summ_b+summ_s==0)

      OnSell(ll);

//----

  }

//----

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

//|                                                                  |

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

bool STH_buy1()

  {

   if(STH[shift]<level_dn)

      return(true);

   return(false);

  }

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

//|                                                                  |

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

bool STH_sell1()

  {

   if(STH[shift]>level_up)

      return(true);

   return(false);

  }

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

//|                                                                  |

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

void OnBuy(double ll)

  {

   int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);

   int TP=MathMax(stops_level,TP_);

   int SL=MathMax(stops_level,SL_);

if(!CheckMoneyForTrade(Symbol(),ll,ORDER_TYPE_BUY))return;

if(!CheckStopLoss_Takeprofit(ORDER_TYPE_BUY,0,TP))return;

//--- >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   =ll;                                    // >1J5< 2 0.1 ;>B

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

   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // F5=0 4;O >B:@KB8O

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

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

   request.tp = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK)+TP*SymbolInfoDouble(Symbol(),SYMBOL_POINT),_Digits);

   request.sl = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID)-SL*SymbolInfoDouble(Symbol(),SYMBOL_POINT),_Digits);

   request.type_filling = fill;

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

   if(!OrderSend(request,result))

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

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

  }

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

//| B:@KB85 ?>78F88 Sell                                            |

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

void OnSell(double ll)

  {

   int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);

   int TP=MathMax(stops_level,TP_);

   int SL=MathMax(stops_level,SL_);

if(!CheckMoneyForTrade(Symbol(),ll,ORDER_TYPE_SELL))return;

if(!CheckStopLoss_Takeprofit(ORDER_TYPE_SELL,0,TP))return;

//--- >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   =ll;                                          // >1J5< 2 0.2 ;>B

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

   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID);       // F5=0 4;O >B:@KB8O

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

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

   request.tp = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID)-TP*SymbolInfoDouble(Symbol(),SYMBOL_POINT),_Digits);

   request.sl = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK)+SL*SymbolInfoDouble(Symbol(),SYMBOL_POINT),_Digits);

   request.type_filling = fill;

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

   if(!OrderSend(request,result))

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

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

  }

//----

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

//|                                                                  |

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

bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)

  {

//--- ?>;CG8< F5=C >B:@KB8O

   MqlTick mqltick;

   SymbolInfoTick(symb,mqltick);

   double price=mqltick.ask;

   if(type==ORDER_TYPE_SELL)

      price=mqltick.bid;

//--- 7=0G5=8O =5>1E>48<>9 8 A2>1>4=>9 <0@68

   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);

   //--- 2K7>25< DC=:F8N ?@>25@:8

   if(!OrderCalcMargin(type,symb,lots,price,margin))

     {

      //--- GB>-B> ?>H;> =5 B0:, A>>1I8< 8 25@=5< false

      Print("Error in "+__FUNCTION__+" code="+(string)GetLastError());

      return(false);

     }

   //--- 5A;8 =5 E20B05B A@54AB2 =0 ?@>2545=85 >?5@0F88

   if(margin>free_margin)

     {

      //--- A>>1I8< >1 >H81:5 8 25@=5< false

      Print("Not enough money for "+EnumToString(type)+" "+(string)lots+" "+symb+" Error code="+(string)GetLastError());

      return(false);

     }

//--- ?@>25@:0 ?@>H;0 CA?5H=>

   return(true);

  }

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

//|                                                                  |

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

bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type,double SL=0,double TP=0)

  {

//--- ?>;CG8< C@>25=L SYMBOL_TRADE_STOPS_LEVEL

   int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);

   

   MqlTick mqltick;

   SymbolInfoTick(Symbol(),mqltick);

   double Ask = mqltick.ask;

   double Bid = mqltick.bid;   

//---

   bool SL_check=false,TP_check=false;

//--- ?@>25@O5< B>;L:> 420 B8?0 >@45@>2

   switch(type)

     {

      //--- >?5@0F8O ?>:C?:0

      case  ORDER_TYPE_BUY:

        {

         //--- ?@>25@8< StopLoss

         SL_check=(Bid-SL>stops_level*_Point);

         if(SL==0)SL_check=true;

         if(!SL_check)

            Print(" For order   StopLoss ERROR");

         //--- ?@>25@8< TakeProfit

         TP_check=(TP-Bid>stops_level*_Point);

         if(TP==0)TP_check=true;

         if(!TP_check)

            PrintFormat("For order  TakeProfit ERROR ");

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(SL_check&&TP_check);

        }

      //--- >?5@0F8O ?@>4060

      case  ORDER_TYPE_SELL:

        {

         //--- ?@>25@8< StopLoss

         SL_check=(SL-Ask>stops_level*_Point);

         if(SL==0)SL_check=true;

         if(!SL_check)

            PrintFormat("For order StopLoss ERROR ");

         //--- ?@>25@8< TakeProfit

         TP_check=(Ask-TP>stops_level*_Point);

         if(TP==0)TP_check=true;

         if(!TP_check)

            PrintFormat("For order TakeProfit ERROR ");

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(TP_check&&SL_check);

        }

      break;

     }

//--- 4;O >B;>65==KE >@45@>2 =C6=0 =5<=>3> 4@C30O DC=:F8O

   return false;

  }

//----

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