[To. coders]If buy order closed, I want to open sell order only.. plz shard you idea.

 

while making an EA, I am in a kinda trouble.

If the buy orders closed, I wnat to open only sell order...

Even if every condition meets the criteria of opening buy position, I dont want to buy positions again.

What code do I have to add

if(iRSI(NULL,0,OPBUY_LEFT_1_Period,OPBUY_LEFT_1_AppliedPrice,0)<OPBUY_RIGHT_1_Input) {

OPBUYChk[2][1]=OP_BUY;
} else {
if(OPBUYChk[2][1]!=OP_BUY) OPBUYChk[2][1]=100;

}

This is the part of my code for opening buy position based on RSI signal.

any help will be appreciate.

Thank you.

 
kyle3738:

while making an EA, I am in a kinda trouble.

If the buy orders closed, I wnat to open only sell order...

Even if every condition meets the criteria of opening buy position, I dont want to buy positions again.

What code do I have to add

if(iRSI(NULL,0,OPBUY_LEFT_1_Period,OPBUY_LEFT_1_AppliedPrice,0)<OPBUY_RIGHT_1_Input) {

OPBUYChk[2][1]=OP_BUY;
} else {
if(OPBUYChk[2][1]!=OP_BUY) OPBUYChk[2][1]=100;

}

This is the part of my code for opening buy position based on RSI signal.

any help will be appreciate.

Thank you.


sorry, but with this part of code only you is knowing what is gettin on there .... ;-)

If you close the buy by EA you can send new Order also after OrderClose.

If you close order by TP or SL you need to find our last closed order in orderhistory and find out if it was buy or sell. The search on top show you lot exampleas about orderhistory

 
extern int MagicNo = 100000;
extern int PipStarter = 110;
extern double Multiflier = 1.05;
extern double RSIBUY = 50;
extern double RSISELL = 50;
extern int MaxSpread = 8;
extern double OpenLot = 0.1;
extern bool TrailingstopUse = true;
extern int Trailingstop = 80;
extern int Trailingstep = 35;
extern int TrailingSinglestop = 50;
extern int TrailingSinglestep = 25;
extern int Totalprofit = 200;

string Symbolname = "NULL";
int TimeFrame = 0;
int MaxSlippage = 3;
bool TradeModeOpen = true;
bool SignalDisplayOpen = false;
bool SignalSoundOpen = false;
string SoundFileOpen = "";
bool TradeModeClose = true;
bool SignalDisplayClose = false;
bool SignalSoundClose = false;
string SoundFileClose = "";
bool OpenTimeSet = false;
bool OpenWeekSet = false;
bool StoplossUse = false;
bool TakeprofitUse = false;
int Stoploss = 10;
int Takeprofit = 10;
bool PipCloseUse = 0;
int ClosePip = 10;
int BuyClosePip = 10;
int OpenBarCount = 1;
int SellClosePip = 10;
bool TotalprofitUse = 0;
int Buyprofit = 10;
int Sellprofit = 10;
bool TotallossUse = 0;
int Totalloss = 10;
int Buyloss = 10;
int Sellloss = 10;
bool sttpUse = false;
int PrimaryDigit=10;
double TicketOpenLot,tmpStoploss,tmpTakeprofit;
int BuyBar,SellBar;
int i;
int tmpClose;
int Hedge=1000;
int LossBuyCount,LossSellCount;
double PointDigit;
int OPBUY_LEFT_1_Period = 14;
int OPBUY_LEFT_1_AppliedPrice = 0;
//double OPBUY_RIGHT_1_Input = 25;
int OPSELL_LEFT_1_Period = 14;
int OPSELL_LEFT_1_AppliedPrice = 0;
//double OPSELL_RIGHT_1_Input = 75;
double OCount = 0;

int init() {

    Print("START");
    if(Digits==3 || Digits==5) PrimaryDigit=10; else PrimaryDigit=1;
    PointDigit=Point*PrimaryDigit;
}

int deinit() {
    Print("END");
}

int start() {

        int OPBUYCheck[6];
        int ORBUYCheck[6];
        int OCBUYCheck[6];
        int OPSELLCheck[6];
        int ORSELLCheck[6];
        int OCSELLCheck[6];
        int OPBUYChk[6][6];
        int ORBUYChk[6][6];
        int OCBUYChk[6][6];
        int OPSELLChk[6][6];
        int ORSELLChk[6][6];
        int OCSELLChk[6][6];
        double OBLot=0;
        double OSLot=0;
        ArrayInitialize(OPBUYCheck,1000);
        ArrayInitialize(ORBUYCheck,1000);
        ArrayInitialize(OCBUYCheck,1000);
        ArrayInitialize(OPSELLCheck,1000);
        ArrayInitialize(ORSELLCheck,1000);
        ArrayInitialize(OCSELLCheck,1000);
        ArrayInitialize(OPBUYChk,1000);
        ArrayInitialize(ORBUYChk,1000);
        ArrayInitialize(OCBUYChk,1000);
        ArrayInitialize(OPSELLChk,1000);
        ArrayInitialize(ORSELLChk,1000);
        ArrayInitialize(OCSELLChk,1000);
        int total=OrdersTotal();
        int TotalBuyProfit=0;
        int TotalSellProfit=0;
        int FirstBuyTime,LastBuyTime,FirstSellTime,LastSellTime,FirstTotalTime,LastTotalTime;
// ================== Averaging
   int OpenCount = 0;
   double AvgPrice = 0;
   double SumPrice = 0;
   int SuperBuyStopLoss =0;
// ===================Averaging
        double FirstBuyPrice,LastBuyPrice,FirstSellPrice,LastSellPrice,FirstTotalPrice,LastTotalPrice;
        double FirstBuyLot,LastBuyLot,FirstSellLot,LastSellLot,FirstTotalLot,LastTotalLot;
        double FirstBuyStopLoss,LastBuyStopLoss,FirstSellStopLoss,LastSellStopLoss,FirstTotalStopLoss,LastTotalStopLoss;
        double FirstBuyTakeProfit,LastBuyTakeProfit,FirstSellTakeProfit,LastSellTakeProfit,FirstTotalTakeProfit,LastTotalTakeProfit;
        int FirstPosition,LastPosition=1000;
   
   SumPrice = 0; // make sure we start with 0 sum
   double Elots=0;





   int last_closed_order_ticket;
   int last_closed_time = 0;
   for(int k=9;k>=0;k--) {// Check the last 10 closed orders in history{
      if(OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)) {
         if(OrderCloseTime()>last_closed_time)
         {
         last_closed_time=OrderCloseTime();
         last_closed_order_ticket=OrderTicket();
         }
      }
   }

   for (i=total-1;i>=0;i--) 
   if (   OrderSelect(i, SELECT_BY_POS)&& OrderMagicNumber() == MagicNo&& OrderType() <= OP_SELL) {
   SumPrice += OrderOpenPrice() * OrderLots(); // sum price * lots, not just price
   Elots    += OrderLots();
   }
   
   if(Elots != 0) AvgPrice = SumPrice / Elots;
        
        int open_buy,open_sell=0;

        for (i=total-1;i>=0;i--) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==TRUE) {
                if(OrderMagicNumber()==MagicNo) {


    //================Averaging*/
                        if(OrderType()==OP_BUY) {
                                TotalBuyProfit=TotalBuyProfit+OrderProfit()+OrderSwap()+OrderCommission();
                                open_buy++;
                                if(FirstBuyTime==0 || FirstBuyTime>OrderOpenTime()) {
                                        FirstBuyTime=OrderOpenTime();
                                        FirstBuyPrice=OrderOpenPrice();
                                        FirstBuyLot=OrderLots();
                                        FirstBuyStopLoss=OrderStopLoss();
                                        FirstBuyTakeProfit=OrderTakeProfit();
                                }
                                if(LastBuyTime<OrderOpenTime()) {
                                        LastBuyTime=OrderOpenTime();
                                        LastBuyPrice=OrderOpenPrice();
                                        LastBuyLot=OrderLots();
                                        LastBuyStopLoss=OrderStopLoss();
                                        LastBuyTakeProfit=OrderTakeProfit();
                                }
                                if(OrderStopLoss()==0 || OrderTakeProfit()==0) {
                                        if(StoplossUse) tmpStoploss=OrderOpenPrice()-Stoploss*PointDigit; else tmpStoploss=0;
                                        if(TakeprofitUse) tmpTakeprofit=OrderOpenPrice()+Takeprofit*PointDigit; else tmpTakeprofit=0;
                                        if(tmpStoploss>0 || tmpTakeprofit>0) OrderModify(OrderTicket(),OrderOpenPrice(),tmpStoploss,tmpTakeprofit,0,DarkOrange);
                                }
                                if(TrailingstopUse && Trailingstop > 0 ) {
                                        if(Bid-AvgPrice>Trailingstop*PointDigit) {
                                        if(OrderStopLoss() < Bid - (Trailingstop+Trailingstep-1)*PointDigit) {
                                                OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (Trailingstep)*PointDigit, OrderTakeProfit(), 0, MediumSeaGreen);
                                        }
                                        }
                                }
//                              if(TrailingstopUse && Trailingstop > 0 && open_buy == 1 ) {
//                                      if(Bid-AvgPrice>TrailingSinglestop*PointDigit) {
//                                      if(OrderStopLoss() < Bid - (TrailingSinglestop+TrailingSinglestep-1)*PointDigit) {
//                                              OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrailingSinglestep)*PointDigit, OrderTakeProfit(), 0, MediumSeaGreen);
//                                      }
//                                      }
//                              }
                                if (AvgPrice > 0 && AvgPrice + 50*PointDigit < Bid){
                                       SuperBuyStopLoss = Bid - 30*PointDigit;
                                    }
                        }
                        if(OrderType()==OP_SELL) {
                                open_sell++;
                                TotalSellProfit=TotalSellProfit+OrderProfit()+OrderSwap()+OrderCommission();
                                if(FirstSellTime==0 || FirstSellTime>OrderOpenTime()) {
                                        FirstSellTime=OrderOpenTime();
                                        FirstSellPrice=OrderOpenPrice();
                                        FirstSellLot=OrderLots();
                                        FirstSellStopLoss=OrderStopLoss();
                                        FirstSellTakeProfit=OrderTakeProfit();
                                }
                                if(LastSellTime<OrderOpenTime()) {
                                        LastSellTime=OrderOpenTime();
                                        LastSellPrice=OrderOpenPrice();
                                        LastSellLot=OrderLots();
                                        LastSellStopLoss=OrderStopLoss();
                                        LastSellTakeProfit=OrderTakeProfit();
                                }
                                if(OrderStopLoss()==0 || OrderTakeProfit()==0) {
                                        if(StoplossUse) tmpStoploss=OrderOpenPrice()+Stoploss*PointDigit; else tmpStoploss=0;
                                        if(TakeprofitUse) tmpTakeprofit=OrderOpenPrice()-Takeprofit*PointDigit; else tmpTakeprofit=0;
                                        if(tmpStoploss>0 || tmpTakeprofit>0) OrderModify(OrderTicket(),OrderOpenPrice(),tmpStoploss,tmpTakeprofit,0,DarkOrange);
                                }
                                if(TrailingstopUse && Trailingstop > 0 && Elots >= 0.02 ) {
                                        if((AvgPrice - Ask) > Trailingstop*PointDigit) {
                                        if((OrderStopLoss() > Ask + (Trailingstop+Trailingstep-1)*PointDigit) || (OrderStopLoss() == 0)) {
                                                OrderModify(OrderTicket(), OrderOpenPrice(), Ask + (Trailingstep) * PointDigit, OrderTakeProfit(), 0, DarkOrange);
                                        }
                                        }
                                }
                                if(TrailingstopUse && Trailingstop > 0 ) {
                                        if((AvgPrice - Ask) > TrailingSinglestop*PointDigit) {
                                        if((OrderStopLoss() > Ask + (TrailingSinglestop+TrailingSinglestep-1)*PointDigit) || (OrderStopLoss() == 0)) {
                                                OrderModify(OrderTicket(), OrderOpenPrice(), Ask + (TrailingSinglestep) * PointDigit, OrderTakeProfit(), 0, DarkOrange);
                                        }
                                        }
                                }
                        }
                } 
         }
  }
        if((FirstBuyTime<FirstSellTime || FirstSellTime==0) && FirstBuyTime>0) {
                FirstTotalTime=FirstBuyTime;
                FirstTotalPrice=FirstBuyPrice;
                FirstTotalLot=FirstBuyLot;
                FirstTotalStopLoss=FirstBuyStopLoss;
                FirstTotalTakeProfit=FirstBuyTakeProfit;
                FirstPosition=OP_BUY;
        } else {
                FirstTotalTime=FirstSellTime;
                FirstTotalPrice=FirstSellPrice;
                FirstTotalLot=FirstSellLot;
                FirstTotalStopLoss=FirstSellStopLoss;
                FirstTotalTakeProfit=FirstSellTakeProfit;
                FirstPosition=OP_SELL;
        }
        if(LastBuyTime>LastSellTime) {
                LastTotalTime=LastBuyTime;
                LastTotalPrice=LastBuyPrice;
                LastTotalLot=LastBuyLot;
                LastTotalStopLoss=LastBuyStopLoss;
                LastTotalTakeProfit=LastBuyTakeProfit;
                LastPosition=OP_BUY;
        } else {
                LastTotalTime=LastSellTime;
                LastTotalPrice=LastSellPrice;
                LastTotalLot=LastSellLot;
                LastTotalStopLoss=LastSellStopLoss;
                LastTotalTakeProfit=LastSellTakeProfit;
                LastPosition=OP_SELL;
        }

                if(iRSI(NULL,0,OPBUY_LEFT_1_Period,OPBUY_LEFT_1_AppliedPrice,0)<RSIBUY)  {  //캐진입 RSI
//              if (OrderSelect(last_closed_order_ticket, SELECT_BY_TICKET, MODE_HISTORY) == true){
//                 if(last_closed_order_ticket == OP_SELL || OrdersHistoryTotal() < 1){
                                                OPBUYChk[3][1]=OP_BUY;
//                              }}
                                } else {
                                        if(OPBUYChk[3][1]!=OP_BUY) OPBUYChk[3][1]=100;
                                

                                }
        //if(OpenCount >0 && Open //////////////////////////////////sdfsdfsdfsdfsdfsdf
        if(SuperBuyStopLoss >= Bid) CloseAll();
        if(TotalBuyProfit+TotalSellProfit>0 && TotalBuyProfit+TotalSellProfit>Totalprofit) CloseAll();          
        if(OPBUYChk[3][1]==OP_BUY) OPBUYCheck[1]=OP_BUY; else OPBUYCheck[1]=100;
        if(OPBUYCheck[1]==OP_BUY || OPBUYCheck[2]==OP_BUY || OPBUYCheck[3]==OP_BUY || OPBUYCheck[4]==OP_BUY || OPBUYCheck[5]==OP_BUY) OPBUYCheck[0]=OP_BUY;
        

                if(iRSI(NULL,0,OPSELL_LEFT_1_Period,OPSELL_LEFT_1_AppliedPrice,0)>RSISELL)  { //캐진입 RSI
//              if (OrderSelect(last_closed_order_ticket, SELECT_BY_TICKET, MODE_HISTORY) == true){
//                if(last_closed_order_ticket == OP_BUY) {
                                                OPSELLChk[3][1]=OP_SELL;
//                              }}
                                } else {
                                        if(OPSELLChk[3][1]!=OP_SELL) OPSELLChk[3][1]=100;
                                

                                }
This is the part of the code of my EA. I have two problems : 1. I want to open only buy position after close sell position and open only sell position after close buy position.. 2. I want to apply different trailing stop pips if the opening count is 1. ………….. I tried to figure those out by my self,,.,, I read the book section of mql4.com and a lot of threads.. but,, I counldnt get the right answer.. Please help me.. I spent almost 3weeks for these 2 problems... You may know how I tried to solve the problems if you read my code.. Please SAVE THIS STUPID NEWBIE CODER…T^T
 
  1.  Print("START");
        if(Digits==3 || Digits==5) PrimaryDigit=10; else PrimaryDigit=1;
        PointDigit=Point*PrimaryDigit;
    For 5 digit brokers you must modify TP, SL, and slippage
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  2. kyle3738:
    1. I want to open only buy position after close sell position and open only sell position after close buy position.. 2. I want to apply different trailing stop pips if the opening count is 1.
    static int lastType = -1;
    int openCount = 0;
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number          // my magic number
    &&  OrderSymbol()      == Symbol() ){           // and period and symbol
        openCount++;
        lastType = OrderType();
        ...
    }
    if (openCount == 0){
       if (lastType < 0){ // First time since init.
          // decide on a direction, look at history, etc.
       }
       else {
           int newType = (!lastType); // OP_BUY -> OP_SELL, OP_SELL -> OP_BUY
           ...
       }
    }
    

Reason: