stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Basket SL & TP within a single chart
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4698
Page 1 of 4
Author:  traderduke [ Sat Apr 30, 2016 1:26 pm ]
Post subject:  Basket SL & TP within a single chart

I need help to build a Void or function that can be added to an existing EA. This function needs to address:
input bool doBasketExit = true; // Check for Basket Exit conditions
input double BasketTP = 250.0; // Basket Take Profit (Account Currency) The chart Pair
input double BasketSL = -100.0; // Basket Stop Loss (Account Currency) The chart Pair

I’m running 9 pairs/Charts with BnG-v1w H1 and I want pair specific basket trading on each of the 9 charts.
Here's what just happened to me. 9 charts running, 3 charts are in trade with, of course, 3 separate pairs, each with 15 trades, yes 45 trades open, 2 are in profit & one is losing badly. I looking for a way to add up each group of the 15 separately and rub each group against a Basket SL & TP boundaries.

Why do I want this, I can't hedge so this my Stop_And_Limit_Order idea, crazy or not??

I was searching other EAs & TMs and found this at Rene's MADTrader 1.1, lines 372-418. I hoping someone can help me change this to a Basket specific pair “CloseAll”

See the following code for starters from Rene's MADTrader 1.1, lines 372-418:

Code: Select all

void closeTrades( CURRENCY* &currencies[] ) {
   // Check for Basket Exit
   if ( doBasketExit ) {
      double basketProfit = getBasketProfit();
      if ( ( ( BasketTP > 0.0 ) && ( basketProfit > BasketTP ) ) ||
           ( ( BasketSL < 0.0 ) && ( basketProfit < BasketSL ) ) ) {
         closeAll();
      }
   }
double getBasketProfit() {
   double basketProfit = 0.0;
   for (int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if (OrderSelect(iOrder,SELECT_BY_POS,MODE_TRADES)) {
         if ( OrderMagicNumber() == MagicNumber ) {
            if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) ) {
               basketProfit += OrderProfit() + OrderSwap() + OrderCommission();
            }
         }
      }
   }
   return(basketProfit);
}

void closeAll() {
   double closePrice;
   string symbolName;
   int    result;   
   for (int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if (OrderSelect(iOrder,SELECT_BY_POS,MODE_TRADES)) {
         if ( OrderMagicNumber() == MagicNumber ) {
            if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) ) {
               symbolName = OrderSymbol();
               closePrice = ( OrderType() == OP_BUY ) ? MarketInfo( symbolName, MODE_BID ) : MarketInfo( symbolName, MODE_ASK );
               result = OrderCloseReliable(OrderTicket(), OrderLots(), closePrice, 50 );
            }
         }
      }
   }
}
Thanks for your concern
Ray
Author:  SteveHopwood [ Sat Apr 30, 2016 6:29 pm ]
Post subject:  Basket SL & TP within a single chart

Trying to do this from within an auto-trader that is intended to trade only one pair is a recipe for chaos. If one ea trading the three pairs recognises the closure condition, then so will the other two. Yes, you can get them talking to one another, but only with a lot of difficulty and it is not worth the effort. You suggest any method of doing so and I bet you a pound to a penny that I can explain how this will break.

Guess how I know? :arrrg:

You need to write/obtain a basket manager to do this for you.

:xm:
Author:  SteveHopwood [ Sat Apr 30, 2016 6:37 pm ]
Post subject:  Basket SL & TP within a single chart

I just read your reply in APTM. I think I get what you are after. You need x profitable baskets closing to offset a losing one.

It would be horrible to code. Just sitting here contemplating the complexity makes my head reel. Mind, I am on my second glass of dinner wine.

Having said that, maybe we are merely taking about Offsetting again? This time, whole baskets are offset instead of groups of trades. Not sure.

It is still a basket manager and not a function of a single-pair EA. Good luck coding that one.

:xm:
Author:  traderduke [ Sat Apr 30, 2016 9:51 pm ]
Post subject:  Basket SL & TP within a single chart

Steve
Thanks for at least understanding. I thought I was going nuts and couldn't speak enough English to get my point across. I'm on my second Black Russian, that's a trip. I wouldn't have this problem in Tradestation but nobody could translate this Godzilla into easy language, you are the man. Thanks a lot for all of it. I don't know how you keep up with it.

Best Regards

Ray
Author:  renexxxx [ Sat Apr 30, 2016 11:31 pm ]
Post subject:  Basket SL & TP within a single chart

traderduke » Sat Apr 30, 2016 11:26 pm wrote:I hoping someone can help me change this to a Basket specific pair “CloseAll”
You can turn this muti-symbol closeAll() function into a pair specific closeAll(), like so:

Code: Select all

void closeAll(string symbol = "") {
   double closePrice;
   string symbolName;
   int    result;  
   for (int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if (OrderSelect(iOrder,SELECT_BY_POS,MODE_TRADES)) {
         if ( OrderMagicNumber() == MagicNumber ) {
            if ( (symbol != "") && (symbol != OrderSymbol()) ) continue;
            if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) ) {
               symbolName = OrderSymbol();
               closePrice = ( OrderType() == OP_BUY ) ? MarketInfo( symbolName, MODE_BID ) : MarketInfo( symbolName, MODE_ASK );
               result = OrderCloseReliable(OrderTicket(), OrderLots(), closePrice, 50 );
            }
         }
      }
   }
}
and then you call closeAll in your single-pair EA like:

Code: Select all

   closeAll( Symbol() );
If you want a multipair basket controller, that offsets negative pair specific baskets, against positive pair specific baskets, you need to run this as a separate multi-pair EA, that, for each open pair, calculates its basket profit, sorts these values and trades the highest basket profit(s) against the lowest basket profit. Let me know if you need help with this.

Rene.
Author:  traderduke [ Sun May 01, 2016 8:20 pm ]
Post subject:  Basket SL & TP within a single chart

rene
I appreciate your help. Its your TradeReport that got me thinking this was possible. I've searched TSD, FF and the internet for answers. I'll attach the void & the EA. My simplistic idea was If I have 9 charts & attach a magic number from 1-9 or comment the pair, such as EU, of each chart could we add up the OrderProfit trades by Symbol and check them against the Basket SL & TP,
certainly sounds reasonable. :yahoo:

My v1w void is on lines 5821-5871.
Thanks again for the interest

Ray

See Attached
Holy Graily Bob 'n Grid-v1z-rk-rem but.mq4

Code: Select all

void   closeTrades(string symbol = "") {   //closeTrades( CURRENCY* &currencies[] ) {

   // Check for Basket Exit
   if ( doBasketExit ) {
      double basketProfit = getBasketProfit();
      
      if ( ( ( BasketTP > 0.0 ) && ( basketProfit > BasketTP ) ) ||
           ( ( BasketSL < 0.0 ) && ( basketProfit < BasketSL ) ) ) {
         closeAll();
      }
   }
}

double getBasketProfit() {

   double basketProfit = 0.0;

   for (int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if (OrderSelect(iOrder,SELECT_BY_POS,MODE_TRADES)) {
         if ( OrderMagicNumber() == MagicNumber ) {
           if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) ) {
               basketProfit += OrderProfit() + OrderSwap() + OrderCommission(); //need to be specific pair
            }
         }
      }
   }
   return(basketProfit);
 }

void closeAll(string symbol = "") {       //changed

   double closePrice;
   string symbolName;
   int    result;
   
   for (int iOrder=OrdersTotal()-1; iOrder >= 0; iOrder--) {
      if (OrderSelect(iOrder,SELECT_BY_POS,MODE_TRADES)) {
         if ( OrderMagicNumber() == MagicNumber ) {
           if ( (symbol != "") && (symbol != OrderSymbol()) ) continue; //added
            if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) ) {
               symbolName = OrderSymbol();
               closePrice = ( OrderType() == OP_BUY ) ? MarketInfo( symbolName, MODE_BID ) : MarketInfo( symbolName, MODE_ASK );
               result = OrderCloseReliable(OrderTicket(), OrderLots(), closePrice, 50 );
            }
         }
      }
   }
}

closeAll( Symbol() );
Author:  renexxxx [ Sun May 01, 2016 8:51 pm ]
Post subject:  Basket SL & TP within a single chart

I still don't quite understand what it is you're after. Let me try to fish out that specification.

1. You have 9 charts (for 9 different symbols), each running a (single-symbol) EA, that is opening and closing one or more trades of that symbol it is attached to. You could call the collection of these trades, a (symbol specific) basket.

Now it gets fuzzy, but let's try:

2. You need a multi-symbol basket controller EA that:

2.1 Works out the profit for each (symbol specific) basket
2.2 Closes the (symbol specific) basket if it passes some test (this could be that the basket profit exceeds a BasketTP or drops below a BasketSL, however, in this case, this code could go inside the single-symbol EA that is attached to the 9 charts)

May be this partial specification, jogs your mind, and helps you to formulate what you need.

There is no need to give the 9 single-symbol EA's different Magic Numbers and/or Order Comments, as you can keep trades apart by just looking at their symbol name.
Author:  traderduke [ Mon May 02, 2016 12:39 am ]
Post subject:  Basket SL & TP within a single chart

renexxxx » Sun May 01, 2016 3:51 pm wrote:I still don't quite understand what it is you're after. Let me try to fish out that specification.

1. You have 9 charts (for 9 different symbols), each running a (single-symbol) EA, that is opening and closing one or more trades of that symbol it is attached to. You could call the collection of these trades, a (symbol specific) basket.

Correct!

Now it gets fuzzy, but let's try:

2. You need a multi-symbol basket controller EA that:

Or I thought you could put the VOID within the v1w EA as I have tried on lines 5821-5871, or due you believe it to be un-code able?

2.1 Works out the profit for each (symbol specific) basket

Yes!

2.2 Closes the (symbol specific) basket if it passes some test (this could be that the basket profit exceeds a BasketTP or drops below a BasketSL, however, in this case, this code could go inside the single-symbol EA that is attached to the 9 charts)

Yes! exactly.

May be this partial specification, jogs your mind, and helps you to formulate what you need.

Check lines 5821-5971 for my exact thoughts. I just don't know how to code it.

There is no need to give the 9 single-symbol EA's different Magic Numbers and/or Order Comments, as you can keep trades apart by just looking at their symbol name.
Exactly, I only mentioned the magic number to give more options.:!!: :!!:

So far using this BnG-v1w with a H1 TF and running it from EDT 01:00 to 11:00 using .03 pips. I have been LUCKY enough to manually close 4 winners averaging 350 a close, some right through the JPY slide. The v1w does the entry and the basket could do the close.

Sorry to ramble
thanks for the interest

Ray
Author:  traderduke [ Fri Jun 10, 2016 3:21 pm ]
Post subject:  Basket SL & TP within a single chart

Rene
I have been hoping you find time for my request. I am still interested. I agree with your solution which follows;
I need a multi-symbol basket controller EA or function within the single-symbol EA that:
1 Works out the profit for each (symbol specific) basket
2 Closes the (symbol specific) basket if it passes some test (this could be that the basket profit exceeds a BasketTP or drops below a BasketSL, however, in this case, this code could go inside the single-symbol EA that is attached to the 9 charts.

Yes, a function that could be added to an existing EA such as BnG which buys and sells several of the same symbol. This would weed out the losers and let the winners roll.

Look forward to hearing from you
Thank You for your interest in my problem

Ray
Author:  renexxxx [ Fri Jun 10, 2016 9:14 pm ]
Post subject:  Basket SL & TP within a single chart

traderduke » Sat Jun 11, 2016 1:21 am wrote: Look forward to hearing from you
Thank You for your interest in my problem
I have attached some snippets of code that you can use for this purpose:

In a nutshell, assuming that each single-chart EA (that opens multiple symbols and multiple of the same symbol), has a unique MagicNumber:
  • First get the list of unique open symbols
  • For each symbol in that list
    • Determine its "symbol-specific" basket profit
    • If that basket profit exceeds a given BasketTP or BasketSL, close that basket
That's all to it. I haven't tested the attached code as that is your job.

Have fun.
All times are UTC Page 1 of 4