Author: Copyright 2020, Ve
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorStochastic oscillator
1 Views
0 Downloads
0 Favorites
EmaStoh
ÿþ//+------------------------------------------------------------------+

//|                                                   EmaStoh.mq4    |

//|                                              Copyright 2021, Vt  |

//|                                            kupiscript@yandex.ru  |

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

#property copyright "Copyright 2020, Ve"

#property link      "kupiscript@yandex.ru"

#property version   "1.0"

#property strict

//--- Inputs

extern string Base = "07>2K5 =0AB@>9:8";

extern double Lots       = 0.001;    // Fix Lot

extern double Risk       = 0;        // Risk

extern string MoneyManagment = "#?@02;5=85 @445@0<8";

extern int TrailingStop  = 40;        // B@0;

extern int TrailingStep  = 50;        // B@0; AB5?

extern int Slip          = 30;       // Requot

extern int Magic         = 123;      // Magic

extern string TimeTrade = "@5<O B>@3>2;8";

extern int StartHour     = 9;        // G0A =0G0;0 B>@3>2;8

extern int StartMin      = 30;       // <8=CB0 =0G0;0 B>@3>2;8

extern int EndHour       = 21;       // G0A >:>=G0=8O B>@3>2;8

extern int EndMin        = 0;       // <8=CB0 >:>=G0=8O B>@3>2;8

extern int NewsTime      =16;

//---

double MINLOT,MAXLOT;

datetime t=0;



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

//| Expert initialization function                                   |

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

int OnInit()

  { 

  MINLOT = MarketInfo(Symbol(),MODE_MINLOT);

  MAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//------------------------

  }

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

//|                                                                  |

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



void PutOrder(int type,double price,double stop,double tp)

  {

   int r=0;

   color clr=Lime;



   if(type==1 || type==3 || type==5)

     {

      clr=Red;

     }



   if(type==0 || type==2 || type==4)

     {

      clr=Blue;

     }



   r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,stop,tp,"EmaStoh",Magic,0,clr);

   return;

  }

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

//|                                                                  |

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

void Trailing()

  {

   bool mod;

   double all=0,count=0,sl=0;



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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

           {

            if(OrderType()<2)

              {

               all+=OrderOpenPrice()*OrderLots();

               count+=OrderLots();

              }

           }

        }

     }



   if(count>0)

      all=NormalizeDouble(all/count,_Digits);



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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

           {

            if(OrderType()==OP_BUY)

              {

               if(Bid-all>TrailingStop*_Point)

                 {

                  if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep)*_Point)

                    {

                     sl=NormalizeDouble(Bid-TrailingStop*_Point,_Digits);

                     if(OrderStopLoss()!=sl)

                        mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);

                    }

                 }

              }



            if(OrderType()==OP_SELL)

              {

               if(all-Ask>TrailingStop*_Point)

                 {

                  if((OrderStopLoss()>(Ask+(TrailingStop+TrailingStep)*_Point)) || (OrderStopLoss()==0))

                    {

                     sl=NormalizeDouble(Bid+TrailingStop*_Point,_Digits);

                     if(OrderStopLoss()!=sl)

                        mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);

                    }

                 }

              }

           }

        }

     }

  }

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

//|                       2@5<O B>@3>2;8                             |

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

bool TimeSession(int aStartHour,int aStartMinute,int aStopHour,int aStopMinute,datetime aTimeCur)

  {

//--- 2@5<O =0G0;0 A5AA88

   int StartTime=3600*aStartHour+60*aStartMinute;

//--- 2@5<O >:>=G0=8O A5AA88

   int StopTime=3600*aStopHour+60*aStopMinute;

//--- B5:CI55 2@5<O 2 A5:C=40E >B =0G0;0 4=O

   int s1NewsTime=3600*NewsTime-1800;

   int s2NewsTime=3600*NewsTime+1800;

   aTimeCur=aTimeCur%86400;

   if(StopTime<StartTime)

     {

      //--- ?5@5E>4 G5@57 ?>;=>GL

      if(aTimeCur>=StartTime || aTimeCur<StopTime)

        {

         return(true);

        }

     }

   else

     {

      //--- 2=CB@8 >4=>3> 4=O

      if(NewsTime>0)

        {

        if(aTimeCur>=StartTime && aTimeCur<s1NewsTime)

          {

           return(true);

          }

        if(aTimeCur>=s2NewsTime && aTimeCur<StopTime)

          {

            return(true);

          }

         }

       else

         {

         if(aTimeCur>=StartTime && aTimeCur<StopTime)

           {

            return(true);

           }

          }

       }

   

   return(false);

  }

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

//|                                                                  |

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

int CountTrades(int type=-1)

  {

   int count=0;

   

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

           {

            if(OrderType()==type || OrderType()<2 || type==-1)

               count++;

           }

        }

     }

   return(count);

  }

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

//| >4AG5B >@45@>2 ?> B8?C                                          |

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

int CountOrders(int type)

  {

   int count=0;

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

           {

            if(OrderType()==type)

               count++;

           }

        }

     }

   return(count);

  }

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

//|                                                                  |

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

double Lot()

  {

   double lot=Lots;

   if(Risk>0)

     {

      lot=NormalizeDouble(AccountBalance()/(Risk*100),2);

     }

    if (lot>MAXLOT) lot = MAXLOT;

    if (lot<MINLOT) lot = MINLOT;

   return(lot);

  }

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

//|                                                                  |

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

int Sig()

  {

   int ert=3;

   int Per=9;

   double EMA50=iMA(NULL,0,50,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA100=iMA(NULL,0,100,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA150=iMA(NULL,0,150,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA250=iMA(NULL,0,250,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA330=iMA(NULL,0,330,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA720=iMA(NULL,0,720,1,MODE_EMA,PRICE_CLOSE,1);

   double Stoh=iStochastic(NULL,0,14,3,3,MODE_EMA,1,0,1);  

   double h400=High[iHighest(NULL,0,MODE_HIGH,400,1)];    

   double l400=Low[iLowest(NULL,0,MODE_LOW,400,1)];     

   

   

   double SHMax = High[iHighest(NULL,0,MODE_HIGH,Per,1)];

   double SHMax1 = High[iHighest(NULL,0,MODE_HIGH,Per*2,Per)];

   double SHMax2 = High[iHighest(NULL,0,MODE_HIGH,Per*3,Per*2)];

   

   double SHMin = Low[iLowest(NULL,0,MODE_LOW,Per,1)];

   double SHMin1 = Low[iLowest(NULL,0,MODE_LOW,Per*2,Per)];

   double SHMin2 = Low[iLowest(NULL,0,MODE_LOW,Per*3,Per*2)];

   

   //----------------------------------------------------------------------

   bool EMABuy=EMA50>EMA100>EMA150>EMA330>EMA720;

   //bool EMABuy=(EMA50>EMA100 && EMA100>EMA150 && EMA150>EMA330 && EMA330>EMA720);

   bool EMASell=EMA50<EMA100<EMA150<EMA330<EMA720;

   //bool EMASell=(EMA50<EMA100 && EMA100<EMA150 && EMA150<EMA330 && EMA330<EMA720);

   

   if(EMABuy)

     {

      if(Stoh<30 && Close[1]<=EMA150 && CountHigh()>h400)

        {

         ert=0;

        }

     }

   if(EMASell)

     {

      if(Stoh>70 && Close[1]>=EMA150 && CountLow()<l400)

        {

         ert=1;

        }

     }

   return(ert);

  }

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

//|                                                                  |

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

double CountHigh()

  {

   double SHMax,count=0;

   int cr=0, n=9;

   

   for(int i=n; i<=400; i=i+n)

     {

         if(OrderSymbol()==Symbol())

           {    

            SHMax = High[iHighest(NULL,0,MODE_HIGH,i,cr)];

            if(SHMax>count)

              {

               count=SHMax;

              }

             cr=cr+n;

           }

        }

     

   return(count);

  }

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

//|                                                                  |

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

double CountLow()

  {

   double SHMax,count=Low[iLowest(NULL,0,MODE_LOW,9,1)];

   int cr=0, n=9;

   

   for(int i=n; i<=400; i=i+n)

     {

         if(OrderSymbol()==Symbol())

           {    

            SHMax = Low[iLowest(NULL,0,MODE_LOW,i,cr)];

            if(SHMax<count)

              {

               count=SHMax;

              }

             cr=cr+n;

           }

        }

     

   return(count);

  }

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

//| #40;5=85 >B;>65==KE >@45@>2                                      |

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

void DelOrder(int type=-1)

  {

   bool del;

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

           {

            if(OrderType()==4 && (type==4 || type==-1))

               del=OrderDelete(OrderTicket());

            if(OrderType()==5 && (type==5 || type==-1))

               del=OrderDelete(OrderTicket());

           }

        }

     }

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   double h500=High[iHighest(NULL,0,MODE_HIGH,500,0)];  

   double l500=Low[iLowest(NULL,0,MODE_LOW,500,0)];     

  

   double Buy=NormalizeDouble(h500+10*_Point,_Digits);

   double Sell=NormalizeDouble(l500-10*_Point,_Digits);

  

   double SLB=Ask-((Buy-Ask)*.6);

   double SLS=Bid+((Bid-Sell)*.6);

 

   double TPB=NormalizeDouble(Ask+((Buy-Ask)*1.5),_Digits);

   double TPS=NormalizeDouble(Bid-((Bid-Sell)*1.5),_Digits);

   

   double EMA50=iMA(NULL,0,50,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA100=iMA(NULL,0,100,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA150=iMA(NULL,0,150,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA250=iMA(NULL,0,250,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA330=iMA(NULL,0,330,1,MODE_EMA,PRICE_CLOSE,1);

   double EMA720=iMA(NULL,0,720,1,MODE_EMA,PRICE_CLOSE,1);

   double Stoh=iStochastic(NULL,0,14,3,3,MODE_EMA,1,0,1); 

    

   bool EMABuy=EMA50>EMA100>EMA150>EMA330>EMA720;

   bool EMASell=EMA50<EMA100<EMA150<EMA330<EMA720;

   

  

   if(t!=Time[0])

     {   

     if(CountOrders(4)>0 && (!EMABuy || Ask<EMA250))

       {

        DelOrder(4);

       }

     if(CountOrders(5)>0 && (!EMASell || Ask>EMA250))

       {

        DelOrder(5);

       }

   

     if(CountTrades()<1)

       {

       if(Sig()==0)

         {

          DelOrder();

          PutOrder(4,Buy,SLB,TPB);

         }

       if(Sig()==1)

         {

          DelOrder();

          PutOrder(5,Sell,SLS,TPS);

         }

        }   

      t=Time[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 ---