// Software je dodáván „tak jak je“. Výrobce neodpovídá za případné ztráty vzniklé jeho použitím.

#define SIGNAL_NONE 0

#define SIGNAL_BUY_1   1
#define SIGNAL_BUY_2   2
#define SIGNAL_BUY_3   3


#define SIGNAL_SELL_1  11
#define SIGNAL_SELL_2  12
#define SIGNAL_SELL_3  13


#define SIGNAL_CLOSEBUY 21
#define SIGNAL_CLOSESELL 22


extern double Lots_1 = 0.3;
extern double Lots_2 = 0.2;
extern double Lots_3 = 0.1;

extern int Slippage = 200;

extern bool UseStopLoss = true;
extern int StopLoss = 100;

extern bool UseTakeProfit = true;
extern int TakeProfit = 100;

extern bool UseTrailingStop = false;
extern int TrailingStop = 35;


int ticketBuy_1;
int ticketBuy_2;
int ticketBuy_3;


int ticketSell_1;
int ticketSell_2;
int ticketSell_3;


/////////////////////////////////////////////////////////////////

int start() {
   int Order = SIGNAL_NONE;
   int Total;
   double StopLossLevel, TakeProfitLevel;



   Total = OrdersTotal();
   Order = SIGNAL_NONE;

// parametry



// Check position

   bool IsTrade_buy_1 = False;
   bool IsTrade_buy_2 = False;
   bool IsTrade_buy_3 = False;
 
   bool IsTrade_sell_1 = False;
   bool IsTrade_sell_2 = False;
   bool IsTrade_sell_3 = False;
 
// CLOSE

   if ((Close[1] <Close[2])) Order = SIGNAL_CLOSEBUY;


   if ((Close[1] >Close[2])) Order = SIGNAL_CLOSESELL;


// BUY level 1
            if (OrderSelect(ticketBuy_1, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 1)) {
            if (OrderType() == OP_BUY) {
            IsTrade_buy_1 = True;


            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSEBUY)) {

            OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);


            IsTrade_buy_1 = False;
            ticketBuy_1 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                  }
               }
            }
            }
            }


// BUY level 2
            if (OrderSelect(ticketBuy_2, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 2)) {
            if (OrderType() == OP_BUY) {
            IsTrade_buy_2 = True;


            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSEBUY)) {

            OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);


            IsTrade_buy_2 = False;
            ticketBuy_2 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                  }
               }
            }
            }
            }


// BUY level 3
            if (OrderSelect(ticketBuy_3, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 3)) {
            if (OrderType() == OP_BUY) {
            IsTrade_buy_3 = True;


            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSEBUY)) {

            OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);


            IsTrade_buy_3 = False;
            ticketBuy_3 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                  }
               }
            }
            }
            }



// SELL level 1
            if (OrderSelect(ticketSell_1, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 20)) {
            if (OrderType() == OP_SELL) {
            IsTrade_sell_1 = True;



            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSESELL)) {

            OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

            IsTrade_sell_1 = False;
            ticketSell_1 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                  }
               }
            }
            }
            }


// SELL level 2
            if (OrderSelect(ticketSell_2, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 21)) {
            if (OrderType() == OP_SELL) {
            IsTrade_sell_2 = True;



            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSESELL)) {

            OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

            IsTrade_sell_2 = False;
            ticketSell_2 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                  }
               }
            }
            }
            }

// SELL level 3
            if (OrderSelect(ticketSell_3, SELECT_BY_TICKET, MODE_TRADES) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 22)) {
            if (OrderType() == OP_SELL) {
            IsTrade_sell_3 = True;



            if ((OrderCloseTime() > 0) || (Order == SIGNAL_CLOSESELL)) {

            OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

            IsTrade_sell_3 = False;
            ticketSell_3 = 0;
            }

            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                  }
               }
            }
            }
            }



// condition

if ((Close[2] < Open[2]) && (Close[1] > Open[1])) Order = SIGNAL_BUY_1;
if ((Close[3] < Open[3]) && (Close[1] > Open[1]) && (Close[2] > Open[2])) Order = SIGNAL_BUY_2;
if ((Close[4] < Open[4]) && (Close[1] > Open[1]) && (Close[2] > Open[2]) && (Close[3] > Open[3])) Order = SIGNAL_BUY_3;


if (Close[1] < Close[2]) Order = SIGNAL_SELL_1;
if ((Close[1] < Close[2]) && (Close[2] < Close[3])) Order = SIGNAL_SELL_2;
if ((Close[1] < Close[2]) && (Close[2] < Close[3]) && (Close[3] < Close[4])) Order = SIGNAL_SELL_3;


// Order send

   //Buy level 1
   if (Order == SIGNAL_BUY_1) {
      if(!IsTrade_buy_1) {

            ticketBuy_1 = OrderSend(Symbol(), OP_BUY, Lots_1, Ask, Slippage, 0, 0, 0, 1, 0, DodgerBlue);   // 1 is magic number

            if(ticketBuy_1 > 0) {
            if (OrderSelect(ticketBuy_1, SELECT_BY_TICKET, MODE_TRADES)) {

                           if(ticketBuy_1 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }
			                  if(ticketBuy_1 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
            }
            }

            if (UseStopLoss) StopLossLevel = OrderOpenPrice() - StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() + TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketBuy_1,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DodgerBlue);

         return(0);
      }
   }


   //Buy level 2
   if (Order == SIGNAL_BUY_2) {
      if(!IsTrade_buy_2) {

            ticketBuy_2 = OrderSend(Symbol(), OP_BUY, Lots_2, Ask, Slippage, 0, 0, 0, 2, 0, DodgerBlue);

            if(ticketBuy_2 > 0) {
            if (OrderSelect(ticketBuy_2, SELECT_BY_TICKET, MODE_TRADES)) {

                           if(ticketBuy_2 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }
			                  if(ticketBuy_2 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
            }
            }

            if (UseStopLoss) StopLossLevel = OrderOpenPrice() - StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() + TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketBuy_2,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DodgerBlue);

         return(0);
      }
   }


   //Buy level 3
   if (Order == SIGNAL_BUY_3) {
      if(!IsTrade_buy_3) {

            ticketBuy_3 = OrderSend(Symbol(), OP_BUY, Lots_3, Ask, Slippage, 0, 0, 0, 3, 0, DodgerBlue);

            if(ticketBuy_3 > 0) {
            if (OrderSelect(ticketBuy_3, SELECT_BY_TICKET, MODE_TRADES)) {

                           if(ticketBuy_3 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }
			                  if(ticketBuy_3 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
            }
            }

            if (UseStopLoss) StopLossLevel = OrderOpenPrice() - StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() + TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketBuy_3,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DodgerBlue);

         return(0);
      }
   }




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


   //Sell level 1
   if (Order == SIGNAL_SELL_1) {
      if(!IsTrade_sell_1) {

            ticketSell_1 = OrderSend(Symbol(), OP_SELL, Lots_1, Bid, Slippage, 0, 0, 0, 20, 0, DeepPink);

            if (ticketSell_1 > 0) {
            if (OrderSelect(ticketSell_1, SELECT_BY_TICKET, MODE_TRADES)) {

            if(ticketSell_1 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }

			   if(ticketSell_1 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
			   }
			   }

			   if (UseStopLoss) StopLossLevel = OrderOpenPrice() + StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() - TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketSell_1,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DeepPink);


         return(0);
      }
   }



   //Sell level 2
   if (Order == SIGNAL_SELL_2) {
      if(!IsTrade_sell_2) {

            ticketSell_2 = OrderSend(Symbol(), OP_SELL, Lots_2, Bid, Slippage, 0, 0, 0, 21, 0, DeepPink);

            if (ticketSell_2 > 0) {
            if (OrderSelect(ticketSell_2, SELECT_BY_TICKET, MODE_TRADES)) {

            if(ticketSell_2 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }

			   if(ticketSell_2 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
			   }
			   }

			   if (UseStopLoss) StopLossLevel = OrderOpenPrice() + StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() - TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketSell_2,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DeepPink);


         return(0);
      }
   }


   //Sell level 3
   if (Order == SIGNAL_SELL_3) {
      if(!IsTrade_sell_3) {

            ticketSell_3 = OrderSend(Symbol(), OP_SELL, Lots_3, Bid, Slippage, 0, 0, 0, 22, 0, DeepPink);

            if (ticketSell_3 > 0) {
            if (OrderSelect(ticketSell_3, SELECT_BY_TICKET, MODE_TRADES)) {

            if(ticketSell_3 > 0) {
				Print("BUY order opened : ", OrderOpenPrice());
            }

			   if(ticketSell_3 == 0) {
				Print("Error opening BUY order : ", GetLastError());
			   }
			   }
			   }

			   if (UseStopLoss) StopLossLevel = OrderOpenPrice() + StopLoss * Point; else StopLossLevel = 0.0;
            if (UseTakeProfit) TakeProfitLevel = OrderOpenPrice() - TakeProfit * Point; else TakeProfitLevel = 0.0;

            OrderModify(ticketSell_3,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,DeepPink);


         return(0);
      }
   }


   return(0);
}
//+------------------------------------------------------------------+