Basket SL & TP within a single chart

User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Basket SL & TP within a single chart

Post by renexxxx »

traderduke » Tue Jun 21, 2016 1:16 pm wrote: Let me know what you think.
The function CheckForClosures() is never called from the OnTick() thread. There is a function inside OnTick() called ShouldTradesBeClosed() that implements the standard MM. Perhaps you should call CheckForClosures() after ShouldTradesBeClosed() (line 6849). In any case, if CheckForClosures() is never called, so it won't do the basket closures.

Cheers ... R.
User avatar
traderduke
Trader
Posts: 306
Joined: Mon Nov 28, 2011 7:30 pm
Location: East Coast USA

Basket SL & TP within a single chart

Post by traderduke »

Rene
I should have known that call was necessary. :oops:

Thanks, I'll get on it.
Ray
User avatar
traderduke
Trader
Posts: 306
Joined: Mon Nov 28, 2011 7:30 pm
Location: East Coast USA

Basket SL & TP within a single chart

Post by traderduke »

Rene
The basket alone works perfectly! I added a user display and got rid of old code. Now I will test the Atr TP, Be, JS with basket. I'll let you know. I'll either paste the finish product here or start a new thread.

a great time to test Basket StopLOOOEESS

Thanks a million
Ray
steven
Posts: 9
Joined: Sun Jul 03, 2016 5:54 pm

Basket SL & TP within a single chart

Post by steven »

Hello to all , i am new here.
Actually i also have problem to adding the close all function to my martingale ea .
I have searching many forum , but i cant find the answer .
after reading this thread , i think i have the problem with the code symbol () too.
(I think is a same problem so i reply here .)

I want the close all function close profit/loss when it have open more then 1 order .
(if single open trade is in profit just let it continue )
When i attach the ea to single pair chart is working fine , but if i attach to more the 1 chart it will direct close when it hit the profit / loss i set .
Any one please help me to see , what should i need to change the code to specified the symbol() to the ea .

Thank you .

Code: Select all

int start()
{
 // close all
 if (OrdersTotal()>1 && Profit_>0 && NetProfit()>=Profit_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>1) CloseAll(0);
    }
    
 if (OrdersTotal()>1 && Loss_>0 && NetProfit()<=-Loss_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>1) CloseAll(0);
    } 

Code: Select all

int CloseAll(int OrdrType) 
{ 
bool ClTicket=false;
   for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) 
   { 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
       
      if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell)  && OrderCloseTime()==0) 
       { 
            if((OrderType()==OP_BUY)&&((OrdrType==0)||(OrdrType==1)))  ClTicket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),5*Spread,Blue); 
            if((OrderType()==OP_SELL)&&((OrdrType==0)||(OrdrType==-1))) ClTicket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),5*Spread,Red); 
       
   }
 }  
   return(0); 
}
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Basket SL & TP within a single chart

Post by renexxxx »

I am not entirely sure that I understand the problem, but I believe that the problem is that NetProfit() returns the total profit of all trades over all symbols combined. If you wish that NetProfit() only returns the total profit of trades for the current symbol, you would need to define NetProfit() like this:

Code: Select all

double NetProfit() {

   double profit = 0.0;
   int ordersTotal = OrdersTotal();
   
   for( int iOrder=ordersTotal-1; iOrder >= 0; iOrder-- ) {
      if ( OrderSelect( iOrder, SELECT_BY_POS, MODE_TRADES ) ) {
         if ( OrderSymbol() != Symbol() ) continue;
         if ( (OrderMagicNumber() != MagicNumberBuy) && (OrderMagicNumber() != MagicNumberSell) ) continue;
         if ( (OrderType() != OP_BUY) && (OrderType() != OP_SELL) ) continue;
         profit += OrderProfit() + OrderSwap() + OrderCommission();
      }
   }
   
   return(profit);
}
Hope this helps.
steven
Posts: 9
Joined: Sun Jul 03, 2016 5:54 pm

Basket SL & TP within a single chart

Post by steven »

Thanks , i will try it in forward test . :good:
steven
Posts: 9
Joined: Sun Jul 03, 2016 5:54 pm

Basket SL & TP within a single chart

Post by steven »

renexxxx » Sun Jul 03, 2016 8:49 pm wrote:I am not entirely sure that I understand the problem, but I believe that the problem is that NetProfit() returns the total profit of all trades over all symbols combined. If you wish that NetProfit() only returns the total profit of trades for the current symbol, you would need to define NetProfit() like this:

Code: Select all

double NetProfit() {

   double profit = 0.0;
   int ordersTotal = OrdersTotal();
   
   for( int iOrder=ordersTotal-1; iOrder >= 0; iOrder-- ) {
      if ( OrderSelect( iOrder, SELECT_BY_POS, MODE_TRADES ) ) {
         if ( OrderSymbol() != Symbol() ) continue;
         if ( (OrderMagicNumber() != MagicNumberBuy) && (OrderMagicNumber() != MagicNumberSell) ) continue;
         if ( (OrderType() != OP_BUY) && (OrderType() != OP_SELL) ) continue;
         profit += OrderProfit() + OrderSwap() + OrderCommission();
      }
   }
   
   return(profit);
}
Hope this helps.
Hi renexxxx , is me again .
After testing for 1 days , it remain the same .
This is my original code , is that the same ?
Can you help me to check 1 more time please

Code: Select all

int start()
{

 // close all
 if (OrdersTotal()>1 && Profit_>0 && NetProfit()>=Profit_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>1) CloseAll(0);
    }
    
 if (OrdersTotal()>1 && Loss_>0 && NetProfit()<=-Loss_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>1) CloseAll(0);
    } 

Code: Select all

//================================================= Calculate Net Profit ===============================================//

double NetProfit() {
   double Profit = 0;
   for (int i4 = OrdersTotal() - 1; i4 >= 0; i4--) 
   {
      if(OrderSelect(i4, SELECT_BY_POS, MODE_TRADES))
      {
      if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell)) 
      {
      if (OrderType() <= OP_SELL) Profit += OrderProfit() + OrderSwap() + OrderCommission();
      }
   }
   }
   return (Profit);
} 
 

Code: Select all

//================================================== Close All Orders ===================================================//

int CloseAll(int OrdrType) 
{ 
bool ClTicket=false;
   for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) 
   { 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      { 
      if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell)  && OrderCloseTime()==0) 
      { 
            if((OrderType()==OP_BUY)&&((OrdrType==0)||(OrdrType==1)))  ClTicket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),5*Spread,Blue);
            if((OrderType()==OP_SELL)&&((OrdrType==0)||(OrdrType==-1))) ClTicket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),5*Spread,Red);
      } 
   }
   }
   return(0); 
}
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Basket SL & TP within a single chart

Post by renexxxx »

OrdersTotal() returns the total number of open orders on the account. I guess that you want a version of OrdersTotal() that only returns the number of open trades for the current symbol, something like this:

Code: Select all

int ordersTotal( string symbol ) {

   int total = 0; 
   
   for(int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if ( OrderSelect( iOrder, SELECT_BY_POS, MODE_TRADES ) ) {
         if ( (OrderMagicNumber() != MagicNumberBuy) && (OrderMagicNumber() != MagicNumberSell) ) continue;
         if ( OrderSymbol() == symbol ) total++;
      }
   }
   return(total);
}
and then replace in your code OrdersTotal() with ordersTotal(_Symbol).

Hope this helps.
steven
Posts: 9
Joined: Sun Jul 03, 2016 5:54 pm

Basket SL & TP within a single chart

Post by steven »

Just want to double confirm , which part should be change the code ?
The part of Net profit or Close all ? or both also need to change ?
Thank you .
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Basket SL & TP within a single chart

Post by renexxxx »

steven » Wed Jul 06, 2016 3:23 pm wrote:Just want to double confirm , which part should be change the code ?
Steven! Come On! Think for yourself ...

Just replace your start() function with this code:

Code: Select all

int start() {

   double netProfit = 0.0;

   if ( ordersTotal( _Symbol ) > 1 ) {
   
      netProfit = NetProfit();

      if ( ( (Profit_ > 0.0) && (netProfit >= Profit_) ) || 
           ( (Loss_ > 0.0) && (netProfit <= -Loss_) ) ) {
         CloseAll(0);
      }
   }
}
There is no need for the Sleep(1000) and the double CloseAll(). Also, I have assigned the NetProfit() to a variable, so that it is only calculated once.

I will remove these last few posts from this thread as I don't think that this is of any benefit to anybody else.
Post Reply

Return to “Coders Hangout”