Increasing lots depending previous orders (problem with expired orders)

 
#property copyright "Jáá"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//definování externích proměnných
     //velikost obchodovaného lotu
extern int     TP       = 100;      //velikost TP
extern int     SL       = 100;      //velikost SL
extern int     OT       = 50;
int     exp_date  = 1;       //vypršení příkazu v hodinách
int     slippage = 3;        //slippage-povolená změna ceny predtím než ji koupíme
double  posun_na_BE =145;

string  text     = "Strategie na svíčky";
int     magic    = 555124;   //magic number
double  lot      = 0.01;
double OpenPrice;
double StopLossPrice;
double TakeProfitPrice;
double sellPriceBullish, sellSLBullish, sellTPBullish;
double buyPriceBullish, buySLBullish, buyTPBullish;    
double SELLssslll, SELLtttppp;
      double vysledek1, vysledek2;
double lot1 = 0.01    ;
double lot2 = 0.02    ;
double lot3 = 0.03    ;

//proměné do příkzů ORDERSEND
double   buyPrice,   //definice buystop ceny
         buyTP,      //take profit buystopu
         buySL,      
         sellPrice,  //definice sellstop ceny
         sellTP,
         sellSL;        
//definice proměnných pro normální ceny svíček
double   open0, close0, low0, high0;
double   open1, close1, low1, high1;
double   open2, close2, low2, high2;
                               
double   _point   = MarketInfo(Symbol(), MODE_POINT);         
datetime _ExpDate=0;    //lokální proměnná na definování čekajícího pokynu a jeho trvajícího času
datetime timeBarInside; //čas baru, v kterém byl otevřen obchod, aby se zabránilo znovu otevření
int      ticket, q, modify, p, a, b, c, d;  
double spread = MarketInfo(Symbol(), MODE_SPREAD)*_point; //zjistí hodnotu spreadu    
      
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
int start()
  {

     //definování normálních cen od 0-8 svíček zpátky
   open0    =  NormalizeDouble (iOpen(Symbol(),Period(),0),Digits);  close0   =  NormalizeDouble (iClose(Symbol(),Period(),0),Digits);
   low0     =  NormalizeDouble (iLow(Symbol(),Period(),0),Digits);   high0    =  NormalizeDouble (iHigh(Symbol(),Period(),0),Digits);
   open1    =  NormalizeDouble (iOpen(Symbol(),Period(),1),Digits);  close1   =  NormalizeDouble (iClose(Symbol(),Period(),1),Digits);
   low1     =  NormalizeDouble (iLow(Symbol(),Period(),1),Digits);   high1    =  NormalizeDouble (iHigh(Symbol(),Period(),1),Digits);

      double   _bid     = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits); //define a lower price 
      double   _ask     = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits); //define an upper price
      double   BE       = 140*_point; //převod pipové hodnoty na cenové vyjádření
 
      int history=OrdersHistoryTotal();



if (OrderSelect(history-1,SELECT_BY_POS,MODE_HISTORY)==true && ((OrderClosePrice() == OrderTakeProfit()) || (OrderClosePrice() == OrderStopLoss()))) {vysledek1=OrderProfit();}
if (OrderSelect(history-2,SELECT_BY_POS,MODE_HISTORY)==true && ((OrderClosePrice() == OrderTakeProfit()) || (OrderClosePrice() == OrderStopLoss()))) {vysledek2=OrderProfit();}
  
   //omezovač počtu vstupů
   for (q=0; q<OrdersTotal();q++)
      {if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()==magic)
   return (0);}
   
   if (vysledek1<0 && vysledek2<0)
   {
   //Třetí
   if (timeBarInside!=iTime(Symbol(),Period(),1) 
      && open1>close1)           
      {sellPrice=NormalizeDouble(close1-OT*_point,Digits); _ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); sellSL=NormalizeDouble(sellPrice+SL*_point,Digits); sellTP=NormalizeDouble(sellPrice-TP*_point,Digits); 
      a=OrderSend(Symbol(),OP_SELLSTOP,lot3,sellPrice, slippage,sellSL,sellTP,text,magic,_ExpDate,Red);}

   if (timeBarInside!=iTime(Symbol(),Period(),1)
      && close1>open1) 
      {_ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); buyPriceBullish=NormalizeDouble(close1+OT*_point,Digits); buySLBullish=NormalizeDouble(buyPriceBullish-SL*_point,Digits); buyTPBullish=NormalizeDouble(buyPriceBullish+TP*_point,Digits);
      c=OrderSend(Symbol(),OP_BUYSTOP,lot3,buyPriceBullish, slippage,buySLBullish,buyTPBullish,text,magic,_ExpDate,Blue);}      
   }
  
   if (vysledek1<0)
   {
   //Druhý
   if (timeBarInside!=iTime(Symbol(),Period(),1) 
      && open1>close1)           
      {sellPrice=NormalizeDouble(close1-OT*_point,Digits); _ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); sellSL=NormalizeDouble(sellPrice+SL*_point,Digits); sellTP=NormalizeDouble(sellPrice-TP*_point,Digits); 
      a=OrderSend(Symbol(),OP_SELLSTOP,lot2,sellPrice, slippage,sellSL,sellTP,text,magic,_ExpDate,Red);}

   if (timeBarInside!=iTime(Symbol(),Period(),1)
      && close1>open1) 
      {_ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); buyPriceBullish=NormalizeDouble(close1+OT*_point,Digits); buySLBullish=NormalizeDouble(buyPriceBullish-SL*_point,Digits); buyTPBullish=NormalizeDouble(buyPriceBullish+TP*_point,Digits);
      c=OrderSend(Symbol(),OP_BUYSTOP,lot2,buyPriceBullish, slippage,buySLBullish,buyTPBullish,text,magic,_ExpDate,Blue);}      
   }


   //První
   if (timeBarInside!=iTime(Symbol(),Period(),1) 
      && open1>close1)           
      {sellPrice=NormalizeDouble(close1-OT*_point,Digits); _ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); sellSL=NormalizeDouble(sellPrice+SL*_point,Digits); sellTP=NormalizeDouble(sellPrice-TP*_point,Digits); 
      a=OrderSend(Symbol(),OP_SELLSTOP,lot1,sellPrice, slippage,sellSL,sellTP,text,magic,_ExpDate,Red);}

   if (timeBarInside!=iTime(Symbol(),Period(),1)
      && close1>open1) 
      {_ExpDate=TimeCurrent()+exp_date*60*60; timeBarInside=iTime(Symbol(),Period(),1); buyPriceBullish=NormalizeDouble(close1+OT*_point,Digits); buySLBullish=NormalizeDouble(buyPriceBullish-SL*_point,Digits); buyTPBullish=NormalizeDouble(buyPriceBullish+TP*_point,Digits);
      c=OrderSend(Symbol(),OP_BUYSTOP,lot1,buyPriceBullish, slippage,buySLBullish,buyTPBullish,text,magic,_ExpDate,Blue);}      

    return(0);} 
  
Hi, i have this ea and i want increasing lots when trade loose. But there is a problem with expiration trades. See attached picture. Thank you guys for help
 

The problem

Lot is increasing on expired trades  too. I need increase it only on lost trades. Thanks for your tips

 
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  2. if (OrderSelect(history-1,SELECT_BY_POS,MODE_HISTORY
    You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
  3. No filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  4. There are other things in history besides closed order. Check the type.

  5. if (timeBarInside!=iTime(Symbol(),Period(),1)
    Why use a function call instead of the Predefined Variables - MQL4 Reference
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  7. "i want increasing lots when trade loose." Martingale will guarantee you blow your account.
 

First of all thank you for your answer. 

I am begginer at programming so i understand that in this ea is a lot or thinks wrong but in programming i understood that there are many ways how to do things working and now it is working so why should i change all of that things ?:)

But thx for all of your tips i will look at all of them :) but honestly it is too much for me now :D i just need help with my problem and answer :  "Martingale will guarantee you blow your account." really does not help me with it :)

Can you then help me with running this ea with condition what i was asking at start :) i think it is some stupid little thing what imust add there. I must select only trades with hitted SL or TP and then increasing but i dont know how.

Reason: