//+------------------------------------------------------------------+
//|                                              StochScalp^^2^^.mq4 |
//|                                                      Nicholishen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Nicholishen @ Apex Group Investments,LLC"
#property link      "www.forex-tsd.com"

#include <stdlib.mqh>
#include <WinUser32.mqh>


   extern string  ________Inputs_________;
   extern int     Confirm=3000;
   extern int     K=3;
   //extern int     D=2;
   //extern int     SL=1;
   extern double  lots=1;            
   extern int     TakeProfit=9;             
   extern int     StopLoss=25;
   extern int     ob=99;
   extern int     os=1;
   extern string  _______TradeTime________;            
   extern bool UseTimeFilter=false; 
   extern int     USA_Session=1100;
   extern int     USA_End=1500;
   extern int     Asian_Session=2300;
   extern int     Asian_End=300;
   extern int     London_Session=600;
   extern int     London_End=1000;
   
   extern string  ______TrailingStop_______;
   extern  bool   UseTrail    = true;
   extern double  TrailingAct   = 6;    
   extern double  TrailingStep   = 6;  

 
  
  //extern int BeginHour=8;
  //extern int EndHour=18;
 
  
   int bar;
   int TestStart;
   int k;
   int mm,dd,yy,hh,min,ss,tf;
   string comment;
   string syym;
   string qwerty;
   int OrderID;
   double TrailPrice;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

 int TimeFilter(){
   if (((Hour()>=USA_Session && Hour() < USA_End) || (Hour()>=Asian_Session && Hour() < Asian_End) || (Hour()>=London_Session && Hour()< London_End)) ) {
         return(1);
   }
   return(0);
}
      
      
      
//+------------------------------------------------------------------+
// Calculates Current Orders on TF,Pair,EA
//+------------------------------------------------------------------+

int CalculateCurrentOrders(){
   int orders=0;
   
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderID ){
            orders++;
         }
      }
   }
   
   return(orders);
}


        
  

int TradeSignal(int functyp){
   bool Rev=false;  
   int x = Confirm;
   double sto1=iRSI(NULL,0,K,0,x);
 //  double sto2=iStochastic(NULL,0,15,2,1,MODE_SMA,0,MODE_SIGNAL,1); 
 //  double stdev=iStdDev(NULL,0,10,MODE_SMA,0,0,0);
  //double sto3=iStochastic(NULL,1440,15,2,1,MODE_SMA,0,MODE_MAIN,0);
 // double sto4=iStochastic(NULL,1440,15,2,1,MODE_SMA,0,MODE_SIGNAL,0);
   
   
if(functyp==1){
   if(sto1 >ob ){
      if(Rev){
         return(1);
      }else{
         return(2);
      }
   }

   if(sto1 < os ){
      if(Rev){
         return(2);
      } else{
         return(1);
      }
   }
}
if(functyp==2){
   if(sto1 >ob){
      if(Rev){
         return(1);
      }else{
         return(2);
      }
   }
   if(sto1 < os){
      if(Rev){
         return(2);
      } else{
         return(1);
      }
   }
}
   return(0);
}

//+------------------------------------------------------------------+
//| Open Conditions                       |
//+------------------------------------------------------------------+

void CheckForOpen(){
double sl,tp; int res,error;



  if(TradeSignal(1)==2){
      if (StopLoss==0) {sl=0;} else sl=Bid+Point*StopLoss;
      if (TakeProfit==0) {tp=0;} else tp=Bid-Point*TakeProfit;
      bar=Bars;
      res = OrderSend(Symbol(),OP_SELL,lots,Bid,3,sl,tp,"Indian Scalp",OrderID,0,Blue); // def
      if(res<0){
         error=GetLastError();
         Print("Error = ",ErrorDescription(error));
      }
  }
  if(TradeSignal(1)==1){
      if (StopLoss==0) {sl=0;} else sl=Ask-Point*StopLoss;
      if (TakeProfit==0) {tp=0;} else tp=Ask+Point*TakeProfit;
      bar=Bars;
      res = OrderSend(Symbol(),OP_BUY,lots,Ask,3,sl,tp,"Indian Scalp",OrderID,0,Red); // def
      if(res<0){
         error=GetLastError();
         Print("Error = ",ErrorDescription(error));
      }
  }
}   
  
//+------------------------------------------------------------------+
//| Close conditions                      |
//+------------------------------------------------------------------+
void CheckForClose(){

   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);    
      if (OrderType()==OP_BUY && OrderMagicNumber()==OrderID && Symbol()==OrderSymbol()){
         if (TradeSignal(2)==2) {                          // MA SELL signals
            int res = OrderClose(OrderTicket(),OrderLots(),Bid,3,White); // close 
            TrailPrice=0;
            if(res<0){
               int error=GetLastError();
               Print("Error = ",ErrorDescription(error));
            }
         }     
      } 
      if (OrderType()==OP_SELL && OrderMagicNumber()==OrderID && Symbol()==OrderSymbol() ){
         if (TradeSignal(2)==1) {                          // MA BUY signals
            res = OrderClose(OrderTicket(),OrderLots(),Ask,3,White); // close 
            TrailPrice=0;
            if(res<0){
               error=GetLastError();
               Print("Error = ",ErrorDescription(error));
            }
         }     
      }  
   }    
}
void TrailingPositions() {
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderMagicNumber()==OrderID ) {
         if (OrderType()==OP_BUY) {
            if (Bid-OrderOpenPrice()>TrailingAct*Point && TrailPrice ==0) {
               TrailPrice=Bid-TrailingStep*Point;
               Print("TRAIL PRICE MODIFIED: ",TrailPrice);
            }
            if (TrailPrice>0 && TrailPrice < Bid-TrailingStep*Point){
               TrailPrice=Bid-TrailingStep*Point;
               Print("TRAIL PRICE MODIFIED: ",TrailPrice);
            }
            if (TrailPrice >0 && TrailPrice >= Bid-TrailingStep*Point){
               CloseOrder(1);
            }
         }
         if (OrderType()==OP_SELL) {
            if (OrderOpenPrice()-Ask > TrailingAct*Point && TrailPrice ==0) {
               TrailPrice=Ask+TrailingStep*Point;
               Print("TRAIL PRICE MODIFIED: ",TrailPrice);
            }
            if (TrailPrice>0 && TrailPrice > Ask+TrailingStep*Point){
               TrailPrice=Ask+TrailingStep*Point;
               Print("TRAIL PRICE MODIFIED: ",TrailPrice);
            }
            if (TrailPrice >0 && TrailPrice <= Ask+TrailingStep*Point){
               CloseOrder(2);
            }   
         }
      }
   }
}}

void CloseOrder(int ord){
    for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);    
      if (OrderType()==OP_BUY && OrderMagicNumber()==OrderID){
         if (ord==1){
         int res = OrderClose(OrderTicket(),OrderLots(),Bid,3,White); // close 
         TrailPrice=0;
         if(res<0){
            int error=GetLastError();
            Print("Error = ",ErrorDescription(error));
         }
      }}     
      
      if (OrderType()==OP_SELL && OrderMagicNumber()==OrderID ){
         if (ord==2) {                          // MA BUY signals
            res = OrderClose(OrderTicket(),OrderLots(),Ask,3,White); // close 
            TrailPrice=0;
            if(res<0){
               error=GetLastError();
               Print("Error = ",ErrorDescription(error));
            }
         }     
      }  
   }    
 }  

int start(){
   

   if(Bars<100 || IsTradeAllowed()==false) return;

   if(CalculateCurrentOrders()==0) {
      TrailPrice=0;
      if(UseTimeFilter && TimeFilter()==1)return;
      CheckForOpen();
   }else{
      CheckForClose();
   }

   if(UseTrail){TrailingPositions();}
 //  if(UseEmailAlerts){MailAlert();}
   
   return(0);
}  
//------------------------------------------------------------+