Help with this code, Open only sell posision

 

Hello,

Who could fix the code to make full transactions sellstop and buy stop. Now only the sellstop deals are opened and the buy stop deals are blocked by hedge or else.

This code was made 2013 year, and now not working on Metatrader 4.


Please FIX problem.


#property copyright "DEXTERs"
string        EAName="3_Level_ZZ_Semafor EA";
extern string _____________________="Parametrai";
extern double Lot=0.01;
extern bool Adaptation...price_digits_SL_TP=false;
extern bool Hedge=true;
//extern int    Atideto_sandorio_galiojimas_val= 24;
extern int    Stop_sandoriu_atitolinimas=5;
extern int    SL=20;
extern int    TP=20;
extern int    Praslidimas=5;
extern int    MagicNumber=2001;
extern int    Max_Orders=1000;
int    Zvake_orderiui=3;
extern string _______________="3_Level_ZZ_Semafor parametrai";
extern double Period1=5; 
extern double Period2=13; 
extern double Period3=34; 
extern string   Dev_Step_1="1,3";
extern string   Dev_Step_2="8,5";
extern string   Dev_Step_3="21,12";
extern int Symbol_1_Kod=140;
extern int Symbol_2_Kod=141;
extern int Symbol_3_Kod=142;
int   _last_error1;
int   _last_error2;
int   _last_error3;
int   _last_error4;
int   apval;
int init(){return(0);}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int start(){
 int valandos;
 int minutes;
 int sekundes;
 int kieklaiko;
int atidejimas;
//atidejimas=3600*Atideto_sandorio_galiojimas_val;
//      datetime expiry=D'2013.09.03';
//if(TimeCurrent()>expiry){Alert("Print");}
 
         
             
double price_4;
static double PointValue;
bool flag;
int cnt = 0, total, totalhis;
bool TradeNow = FALSE, LongTrade = FALSE, ShortTrade = FALSE; 
     
if (Adaptation...price_digits_SL_TP==true){
      if (Digits==2) {PointValue = 0.01;apval=2;}
      if (Digits==3) {PointValue = 0.01;apval=2;}
      if (Digits==4) {PointValue = 0.0001;apval=4;}
      if (Digits==5) {PointValue = 0.0001;apval=4;}}
if (Adaptation...price_digits_SL_TP==false){
      if (Digits==2) {PointValue = 0.01;apval=2;}
      if (Digits==3) {PointValue = 0.001;apval=3;}
      if (Digits==4) {PointValue = 0.0001;apval=4;}
      if (Digits==5) {PointValue = 0.00001;apval=5;}}

double buf1;
double buf2;
double buf3;
double buf4;
double buf5;
double buf6;
buf1  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,0,Zvake_orderiui-1); //1 apacioje
buf2  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,1,Zvake_orderiui-1); //1 virsuje
buf3  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,2,Zvake_orderiui-1); //2 apacioje
buf4  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,3,Zvake_orderiui-1); //2 virsuje
buf5  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,4,Zvake_orderiui-1); //3 apacioje
buf6  = iCustom(NULL,0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,5,Zvake_orderiui-1); //3 virsuje

static int type_b;
static int type_s;
static int bar_b;
static int bar_s;

if(buf1>0 ||  buf3>0 ||  buf5>0){Print("buy"); type_b=1;} else {type_b=0;}
if(buf2>0 ||  buf4>0 ||  buf6>0){Print("sell"); type_s=2;} else {type_s=0;} 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    total = CountTrades();
   if (total == 0) flag = FALSE;
   for (cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY || OP_BUYLIMIT) {
            LongTrade = FALSE;
            ShortTrade = TRUE;
            break;}}
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_SELL || OP_SELLLIMIT) {
            LongTrade = TRUE;
            ShortTrade = FALSE;
            break;}}}
static int yra;                 
if (total <= Max_Orders) {
      ShortTrade = FALSE;
      LongTrade = FALSE;
      TradeNow = TRUE;}
Print("        1:",DoubleToStr(buf1,4),"          3:",DoubleToStr(buf3,4),"        5:",DoubleToStr(buf5,4),"        2:",DoubleToStr(buf2,4),"        4:",DoubleToStr(buf4,4),"        6:",DoubleToStr(buf6,4),"           bar_b:",bar_b, " bar:",Bars,"    <<<>>>    type_b:", type_b,  " type_s:" , type_s);
///////////////////////////////////////////////////////////////////////////////////////////////////
    for (int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--) {
      OrderSelect(pos_0, SELECT_BY_POS);
       
      if (OrderSymbol() == Symbol()  && OrderMagicNumber() == MagicNumber) {
         price_4 = MarketInfo(OrderSymbol(), MODE_ASK);
         if (OrderType() == OP_BUY) price_4 = MarketInfo(OrderSymbol(), MODE_BID);
         if (Hedge==false && type_b==1) {if (OrderType() == OP_SELL) { OrderClose(OrderTicket(), OrderLots(), price_4, Praslidimas); Print("Uzdaryta del priesingo sandorio ");  }}
Sleep(100);
         RefreshRates();
      }   
//////////////////////////////////////////////////////////////////////////////     
      if (OrderSymbol() == Symbol()  && OrderMagicNumber() == MagicNumber) {
         price_4 = MarketInfo(OrderSymbol(), MODE_ASK);
         if (OrderType() == OP_BUY) price_4 = MarketInfo(OrderSymbol(), MODE_BID);
         if (Hedge==false && type_s==2) {if (OrderType() == OP_BUY) { OrderClose(OrderTicket(), OrderLots(), price_4, Praslidimas);  Print("Uzdaryta del priesingo sandorio "); }}
Sleep(100);
         RefreshRates();
      }   
   } 
 valandos=(23-Hour())*3600;
 minutes=(59-Minute())*60;
 sekundes=59-Seconds();
 kieklaiko=(valandos+minutes+sekundes-20);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(TradeNow) {
if (1==1) { if (type_b==1 && bar_b!=Bars)  { OrderSend(Symbol(),OP_BUYSTOP,Lot,High[1]+(Stop_sandoriu_atitolinimas*PointValue),Praslidimas*PointValue,High[1]-(SL*PointValue)+(Stop_sandoriu_atitolinimas*PointValue),High[1]+(TP*PointValue)+(Stop_sandoriu_atitolinimas*PointValue),"Magic: " + MagicNumber +  " - " +EAName + "-" + " BuySTOP",MagicNumber,TimeCurrent()+kieklaiko,Green); Print ("BUYSTOP sandoris atidaromas"); bar_b=Bars; type_b=0;} else {type_b=0;}
      _last_error1=GetLastError(); if (_last_error1>0) {Alert( "Open BUYSTOP error - code ",_last_error1);  bar_b=1; type_b=1;}}

if (1==1) { if (type_s==2 && bar_s!=Bars) { OrderSend(Symbol(),OP_SELLSTOP,Lot,Low[1]-(Stop_sandoriu_atitolinimas*PointValue),Praslidimas*PointValue,Low[1]+(SL*PointValue)-(Stop_sandoriu_atitolinimas*PointValue),Low[1]-(TP*PointValue)-(Stop_sandoriu_atitolinimas*PointValue),"Magic: " + MagicNumber +  " - " +EAName + "-" + " sellSTOP",MagicNumber,TimeCurrent()+kieklaiko,Red); Print ("SELLSTOP sandoris atidaromas"); bar_s=Bars; type_s=0;} else {type_s=0;}
      _last_error2=GetLastError(); if (_last_error2>0) {Alert( "Open SELLSTOP error - code ",_last_error2);  bar_s=1; type_s=2;}}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      string P= "\n-------- 3_Level_ZZ_Semafor EA  ----------\n";
       P=P + "Info log: 1buy:" + DoubleToStr(buf1,4) + "      2buy:" + DoubleToStr(buf3,4) + "    3buy:" + DoubleToStr(buf5,4) + "   <<<>>>    1sell:" + DoubleToStr(buf2,4) + "    2sell:" + DoubleToStr(buf4,4) + "    3sell:" + DoubleToStr(buf6,4) + "   <<<>>>    bar_b:" + bar_b +  " bar:" + Bars  + "   <<<>>>    type_b:" + type_b +  " type_s:" + type_s   + "\n";       
       P=P + "            Balance:  " + DoubleToStr(AccountBalance(),2)   + "\n";       
      P=P + "            Equity:  " + DoubleToStr(AccountEquity(),2)  + "\n";
      P=P + "            FreeMargin: " + DoubleToStr(AccountFreeMargin(),2)  + "\n";
      P=P + "            Margin: " + DoubleToStr(AccountMargin(),2)  + "\n";      
      P=P + "            Profit: " + DoubleToStr(AccountProfit(),2)   + "\n";
      P=P + "            Dabar sandoriu: " + DoubleToStr(total,0)   + "\n";
      P=P + "            MagicNumber: " + DoubleToStr(MagicNumber,0)   + "\n";
      P=P + "    ----------------------------------------------- "   + "\n";
      P=P + "           Swap Long:  "+ DoubleToStr(MarketInfo(Symbol(),MODE_SWAPLONG),2) + "\n";
      P=P + "           Swap Short:  "+ DoubleToStr(MarketInfo(Symbol(),MODE_SWAPSHORT),2) + "\n"; 
      P=P + "                 Spread:  " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD),0) + "\n"; 
      Comment(P);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
return;}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CountTrades() {
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) count++;}
   return (count);}
/////////////////////////////////////////////////////////////////////////////////////////