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

Best Bob
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=3566
Page 6 of 8
Author:  phil_trade [ Thu May 08, 2014 6:02 pm ]
Post subject:  Best Bob

Steve

I have improve my Unique Magic Generator to allow Empty4 switch down/up and recover the same MN

init() should be change with :

Code: Select all

  // Test pour éviter même magicNumber sur Chaque Symbol
   int UniqueMagicNumber = ReturnMagicNumberOfOpenTrade();
   
   if (UniqueMagicNumber == 0)
   {
   UniqueMagicNumber = WindowHandle(Symbol(),0);
   TrendMagicNumber = UniqueMagicNumber;
   CtMagicNumber    = UniqueMagicNumber+1;
   }
   else
   {
   TrendMagicNumber = UniqueMagicNumber;
   CtMagicNumber    = UniqueMagicNumber+1;
   }

   if (TrendMagicNumber == 0)  TrendMagicNumber=837746;
   if (CtMagicNumber == 0)  TrendMagicNumber=837747;
   
   Alert(Symbol()+" MN Trend = "+TrendMagicNumber+" MN CT Trend = "+CtMagicNumber);
and new function to be added :

Code: Select all

int ReturnMagicNumberOfOpenTrade()
{

   if (OrdersTotal() == 0) return(0);
   
   for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
   {
      if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue;
      if (OrderSymbol() != Symbol() ) continue;
      
      return(OrderMagicNumber());
   }  //for (int cc = OrdersTotal() - 1; cc >= 0; cc--)  
   return(0);
} // int ReturnMagicNumberOfOpenTrade(MySymbol : string)
This assure a unique Magic Number for each pair.
Author:  SteveHopwood [ Thu May 08, 2014 6:52 pm ]
Post subject:  Best Bob

Thanks for some really helpful posts guys. :clap: I had time to think about all this when driving to and from the hospital. Here is what I shall do after something to eat.
  • store the value of OrdersTotal() before firing the loop
  • exit the loop and entire function if the saved value and OrdersTotal() mismatch.
  • turn CountOpenTrades() into a boolean that returns true if the loop runs smoothly, or false if not. That way, start() will know whether to continue to run or to abort and await the next tick.
:xm:
Author:  SteveHopwood [ Thu May 08, 2014 8:23 pm ]
Post subject:  Best Bob

V 1c is in post 1, with the changes I described in my previous post.

To hand-code these changes, copy this over your existing CountOpenTrades() function:

Code: Select all

bool CountOpenTrades()
{
   //Not all these will be needed. Which ones are depends on the individual EA.
   OpenTrades = 0;
   TicketNo = -1;
   BuyOpen = false;
   SellOpen = false;
   PendingBuyOpen = false;
   PendingSellOpen = false;
   int type;//Saves the OrderType() for consulatation later in the function
   TradeHasPartClosed=false;
   
   upl = 0;//Unrealised profit and loss for hedging/recovery basket closure decisions

   if (OrdersTotal() == 0) return(true);
   
   //Define a variable to avoid the situation that Phil describes at http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=92449#p92449
   double ot = OrdersTotal();
   
   //Iterating backwards through the orders list caters more easily for closed trades than iterating forwards
   for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
   {
      if (ot != OrdersTotal() )
         return(false);
         
      bool TradeWasClosed = false;//See 'check for possible trade closure'

      //Ensure the trade is still open
      if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue;
      //Ensure the EA 'owns' this trade
      if (OrderSymbol() != Symbol() ) continue;
      if (OrderMagicNumber() != TrendMagicNumber) 
         if (OrderMagicNumber() != CtMagicNumber) 
            continue;
      if (OrderCloseTime() > 0) continue; 
      
      //All conditions passed, so carry on
      type = OrderType();//Store the order type
      
      OpenTrades++;
      //Store the latest trade sent. Most of my EA's only need this final ticket number as either they are single trade
      //bots or the last trade in the sequence is the important one. Adapt this code for your own use.
      if (TicketNo  == -1) TicketNo = OrderTicket();
      
      //upl might not be needed. Depends on the individual EA
      upl+= (OrderProfit() + OrderSwap() + OrderCommission()); 
      //The next line of code calculates the pips upl of an open trade. As yet, I have done nothing with it.
      //something = CalculateTradeProfitInPips()
      
      //These might need extra coding if both stop and limit orders are used
      if (OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) PendingBuyOpen = true;
      if (OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) PendingSellOpen = true;
      //Add missing tp/sl in case rapidly moving markets prevent their addition - ECN
      if (CloseEnough(OrderStopLoss(), 0) && !CloseEnough(StopLoss, 0)) InsertStopLoss(OrderTicket());
      if (CloseEnough(OrderTakeProfit(), 0) && !CloseEnough(TakeProfit, 0)) InsertTakeProfit(OrderTicket() );

      //Replace missing tp and sl lines
      if (HiddenPips > 0) ReplaceMissingSlTpLines();
      
      
      TradeWasClosed = LookForTradeClosure(OrderTicket() );
      if (TradeWasClosed) 
      {
         if (type == OP_BUY) BuyOpen = false;//Will be reset if subsequent trades are buys that are not closed
         if (type == OP_SELL) SellOpen = false;//Will be reset if subsequent trades are sells that are not closed
         cc++;
         continue;
      }//if (TradeWasClosed) 
         
      //Profitable trade management
      if (OrderProfit() > 0) 
      {
         TradeManagementModule();
         if (TradeHasPartClosed) continue;//Part-closing changes the ticket number, so we cannot continue the current loop iteration
      }//if (OrderProfit() > 0) 
            
      //Trade types and half-close, and adjust sl/tp in case slippage has caused them to be inaccurate when the trade was sent.
      if (OrderType() == OP_BUY) 
      {
         BuyOpen = true;
         if (PartCloseEnabled && OrderComment() == TradeComment && OrderStopLoss() >= OrderOpenPrice() ) PartCloseTrade(OrderTicket() );
      }//if (OrderType() == OP_BUY) 
      
      if (OrderType() == OP_SELL) 
      {
         SellOpen = true;
         if (PartCloseEnabled && OrderComment() == TradeComment && OrderStopLoss() <= OrderOpenPrice()  && !CloseEnough(OrderStopLoss(), 0)) PartCloseTrade(OrderTicket() );
      }//if (OrderType() == OP_SELL) 
      
   }//for (int cc = OrdersTotal() - 1; cc <= 0; c`c--)
   
   //Loop exited correctly, so tell the rest of the code
   return(true);
   
}//End bool CountOpenTrades();
Then go to start() and replace the call to CountOpenTrades() with:

Code: Select all

if (!CountOpenTrades() )
      return(0);
There is a call to CountOpenTrades in init() as well, but it doesn't matter if that call fails the OrdersHistory() test.

I reckon that anyone using my shells needs to implement these changes across their files.

:xm:
Author:  Haggadah [ Tue May 27, 2014 11:15 am ]
Post subject:  Demo Account and Live, need help

I opened a demo account and downloaded the EA. I was wondering how I could be able to detach the EA from my live account because each time I log-on back to my live account, the EA from my demo is there. There must be a way to refresh but I cannot figure it out. Any help is very much appreciated.

Haggadah
Author:  milanese [ Tue May 27, 2014 11:18 am ]
Post subject:  Demo Account and Live, need help

Haggadah » Tue May 27, 2014 11:15 am wrote:I opened a demo account and downloaded the EA. I was wondering how I could be able to detach the EA from my live account because each time I log-on back to my live account, the EA from my demo is there. There must be a way to refresh but I cannot figure it out. Any help is very much appreciated.

Haggadah
You should use seperate Empty4 installations for your live and your demo account, just install your demo(or your live) account in an other directory..

Cheers :)

Tommaso
Author:  Haggadah [ Tue May 27, 2014 12:04 pm ]
Post subject:  Demo Account and Live, need help

milanese » Tue May 27, 2014 12:18 pm wrote:
Haggadah » Tue May 27, 2014 11:15 am wrote:I opened a demo account and downloaded the EA. I was wondering how I could be able to detach the EA from my live account because each time I log-on back to my live account, the EA from my demo is there. There must be a way to refresh but I cannot figure it out. Any help is very much appreciated.

Haggadah
You should use seperate Empty4 installations for your live and your demo account, just install your demo(or your live) account in an other directory..

Cheers :)

Tommaso
Thank you Tommaso, I will do that.

Haggadah
Author:  SteveHopwood [ Tue May 27, 2014 1:02 pm ]
Post subject:  Demo Account and Live, need help

Haggadah » Tue May 27, 2014 11:15 am wrote:I opened a demo account and downloaded the EA. I was wondering how I could be able to detach the EA from my live account because each time I log-on back to my live account, the EA from my demo is there. There must be a way to refresh but I cannot figure it out. Any help is very much appreciated.

Haggadah
Ouch. Never ever, ever, ever, ever, ever mix your live with your demo account on the same platform - quintuply so when your demo is running an EA. The potential for cataclysmic disaster in colossal. :o

Next time you are tempted to do so, get someone to beat you over the head with something heavy. :D

:xm:
Author:  patmontes [ Mon Aug 11, 2014 1:08 pm ]
Post subject:  Best Bob

Giving this a spin today on 4H. Yellow line flip turned off and TMA exit to true.
Author:  SteveHopwood [ Mon Aug 11, 2014 9:20 pm ]
Post subject:  Best Bob

patmontes » Mon Aug 11, 2014 1:08 pm wrote:Giving this a spin today on 4H. Yellow line flip turned off and TMA exit to true.
I wouldn't bother if I were you? Notice the quiet around here lately? There is a reason.

:xm:
Author:  patmontes [ Mon Aug 11, 2014 10:51 pm ]
Post subject:  Best Bob

SteveHopwood » Mon Aug 11, 2014 9:20 pm wrote:
patmontes » Mon Aug 11, 2014 1:08 pm wrote:Giving this a spin today on 4H. Yellow line flip turned off and TMA exit to true.
I wouldn't bother if I were you? Notice the quiet around here lately? There is a reason.

:xm:
Hmmm yes.... all too quiet...
I guess best if i trade it manual then on D1
All times are UTC Page 6 of 8