//+------------------------------------------------------------------+
//|                                                          fix.mq4 |
//|                                             sakymns test program |
//|                                             http://fxde.tou3.com |
//+------------------------------------------------------------------+
#property copyright "sakymns test program"
#property link      "http://fxde.tou3.com"

//マジックナンバー 
#define MAGIC 0001 
//パラメーター 
extern double Lots = 0.1;//ロット数 
extern int Slip = 3;//許容スリップ 
extern int K_Period = 5;//ストキャス％Ｋの計算期間 
extern int D_Period = 3;//ストキャス％Ｄの計算期間 
extern int SD_Period = 3;//ストキャス％ＳＤの計算期間 
extern int SL = 25;//ＰＩＰＳ 
extern int TP = 50;//ＰＩＰＳ 
extern int loses = 2;   

int losecount = 0;
double buyprice;
double selprice;
int timekeeper;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits % 2 == 0) //桁数チェック
   {
   
   }
   else
   {
      Slip = Slip * 10;//許容スリップ 
      SL = SL * 10;//ＰＩＰＳ 
      TP = TP * 10;//ＰＩＰＳ 
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
if (timekeeper != Time[0])
{

   //トレード可否 
   if(IsTradeAllowed()==false) return(0); 

   //ポジション数の確認 
   int Pos = 0; 
   for(int i=0; i<OrdersTotal(); i++) 
   { 
      if(OrderSelect(i,SELECT_BY_POS) == false) break; 
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != MAGIC) continue; 
      { 
         if(OrderType() == OP_BUY || OrderType() == OP_SELL)
         {
            Pos++;//現在のポジション数 
         }
      } 
   } 

   //ストキャスの計算 
   double Stochas_Main1 = iStochastic(Symbol(),0,K_Period,D_Period,SD_Period,MODE_SMA,0,MODE_MAIN,1); 
   double Stochas_Main2 = iStochastic(Symbol(),0,K_Period,D_Period,SD_Period,MODE_SMA,0,MODE_MAIN,2); 
   double Stochas_Signal1 = iStochastic(Symbol(),0,K_Period,D_Period,SD_Period,MODE_SMA,0,MODE_SIGNAL,1); 
   double Stochas_Signal2 = iStochastic(Symbol(),0,K_Period,D_Period,SD_Period,MODE_SMA,0,MODE_SIGNAL,2); 
   int ticket;
   //買いシグナル 
   if(Pos==0 && Stochas_Main1<30 && Stochas_Main2<=Stochas_Signal2 && Stochas_Main1>Stochas_Signal1) 
   { 
      if(losecount >=loses)
      {
         Alert("losecount ga =",losecount,"ni nattanode BUY&losecount=0");
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,Ask-SL*Point,Ask+TP*Point,"",MAGIC,Blue); 
         losecount = 0;
         //時間所得
         timekeeper = Time[0];
      } else {
         Alert("losecount =",losecount," Buy shinai");
         if(buyprice == 0)
            buyprice = Ask;
         //時間所得
         timekeeper = Time[0];
      }
   //売りシグナル
   }
   else if(Pos==0 && Stochas_Main1>70 && Stochas_Main2>=Stochas_Signal2 && Stochas_Main1<Stochas_Signal1)
   {
      if(losecount >=loses)
      {
         Alert("losecount ga =",losecount,"ni nattanode SELL&losecount=0");
         ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,Bid+SL*Point,Bid-TP*Point,"",MAGIC,Red); 
         losecount = 0;
         //時間所得
         timekeeper = Time[0];
      } else {
         Alert("losecount =",losecount," SELL shinai");
         if(selprice == 0)
            selprice = Bid;
         //時間所得
         timekeeper = Time[0];
      }
   }   
      
   //勝ち負け判定
   if((buyprice - Bid > SL * Point && buyprice != 0) || (Ask - selprice > SL * Point && selprice != 0))
   {
      losecount++;
      buyprice = 0;
      selprice = 0;
      Alert("kasou lose,countup! losecount = ",losecount);
      //時間所得
      timekeeper = Time[0];
   }
   else if((Bid - buyprice > TP * Point && buyprice != 0) || (selprice - Ask > TP * Point && selprice != 0))
   {
      losecount = 0;
      buyprice = 0;
      selprice = 0;
      Alert("kasou win,countdelete! losecount = ",losecount);
      //時間所得
      timekeeper = Time[0];
   }
}

return(0); 
} 
//+------------------------------------------------------------------+