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

News Code - Help!
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1868
Page 1 of 1
Author:  theforexedge [ Mon Apr 08, 2013 6:04 pm ]
Post subject:  News Code - Help!

Can anyone check this code and let me know if it's correct or not..

I'm trying to get FFCal to stop trading at News Time and then close any trades that are in profit.

The Stop Trading code works, but the close orders code seems to just close the trade thats open regardless or in profit or drawdown

note: profitToClose is setup as extern bool

BTW i'm not a coder so this is a first / basic attempt!! so any help advide appreciated..

Jason

if (UseNews)
{
NewsHandling();
if (NewsTime) {BuyOp=false;SellOp=false;}//stop trading
{
if (OrderProfit()>profitToClose)
closeOrders(OP_SELL);closeOrders(OP_BUY);
}
}
Author:  slipshod [ Tue Apr 09, 2013 1:26 am ]
Post subject:  Re: News Code - Help!

Hi Jason, firstly don't worry - everyone starts as a new coder at some time, and kudos to you for giving it a go! Keep at it and you'll learn the ropes in no time.

Regarding your code snippet, I'd need to see what the closeOrders() function does to be sure, but my guess is that it simply closes every open trade. If you created a new closeWinningOrders() function as follows below & called it in your code instead of closeOrders(), it might do what you're after. I've assumed your EA uses a magic number, and I've represented it below in a variable called Magic - you might want to change that if needed. I've also assumed that the EA's running only on the chart you've dragged it onto rather than being a "basket" EA.

Code: Select all

void closeWinningOrders()
{
   int total = OrdersTotal(), i, close_total = 0;
   static int orders[];

   ArrayResize(orders, total);

   for (i=total-1; i>=0; i--)
   {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if (OrderMagicNumber() == Magic && Symbol() == OrderSymbol())
       {
           if ((OrderType() == OP_BUY && Bid > OrderOpenPrice()) ||
               (OrderType() == OP_SELL && Ask < OrderOpenPrice()))
           {
               orders[close_total] = OrderTicket();
               close_total++;
           }
       }
   }

   for (i=0; i<close_total; i++)
   {
      OrderSelect(orders[i], SELECT_BY_TICKET, MODE_TRADES);
      if (OrderType() == OP_BUY)
         OrderClose(OrderTicket(), OrderLots(), Bid, 30, CLR_NONE);
      else
         OrderClose(OrderTicket(), OrderLots(), Ask, 30, CLR_NONE);
   }
}
I'd suggest using LibOrderReliable (from another thread in the Coders forum), in which case the only change to the above code would be to call OrderCloseReliable instead of OrderClose.

I've made it a two-pass operation, first finding the orders and then closing them rather than closing them in the first loop - this is simply out of paranoia, making sure that the act of closing an order doesn't make the first for() loop skip other orders. I trust Empty4 as far as I can kick it.
Author:  theforexedge [ Tue Apr 09, 2013 9:23 am ]
Post subject:  Re: News Code - Help!

Slipshod

Thank you for the reply and encouragment...

I'm sitting here thumbing through Expert Advisor Programming book, but not sure if much yet is clicking!! I'm thinking that I need to go back to basics and learn a language like Cobra, as I'm lacking in the basics as the last time I did any programming was back in the 80's during collage..

I will persevere with the mod and let you know how I got on!!

Thank you, Jason
Author:  slipshod [ Tue Apr 09, 2013 12:31 pm ]
Post subject:  Re: News Code - Help!

Actually you'd probably be better off learning the basics of C, C++ or Java as its pretty close to them syntax-wise. I say basics, as MQL has none of the fine-grained pointer handling of C, nor the object-oriented features of C++ or Java.
Author:  dietcoke [ Tue Apr 09, 2013 1:18 pm ]
Post subject:  Re: News Code - Help!

theforexedge wrote:Can anyone check this code and let me know if it's correct or not..

I'm trying to get FFCal to stop trading at News Time and then close any trades that are in profit.

The Stop Trading code works, but the close orders code seems to just close the trade thats open regardless or in profit or drawdown

note: profitToClose is setup as extern bool

BTW i'm not a coder so this is a first / basic attempt!! so any help advide appreciated..

Jason

if (UseNews)
{
NewsHandling();
if (NewsTime) {BuyOp=false;SellOp=false;}//stop trading
{
if (OrderProfit()>profitToClose)
closeOrders(OP_SELL);closeOrders(OP_BUY);
}
}

Hi Jason,

This looks like the changed bit of code I Pm'd you?

is it not working?

You'll probably need to post the whole EA andFFcal indi you are using because the problem is probably not in the bit of code you posted.

When I looked at it, there was no obvious fault with the returns from your news handling function but there was a problem with the logic which decided whether to stop trading. The change I made appeared to fix that when I tested it (briefly, I must admit)
Author:  theforexedge [ Wed Apr 10, 2013 10:40 pm ]
Post subject:  Re: News Code - Help!

DC

I can past the code here but not sure how to use the feature to paste in the code to the reply.

Jason
Author:  mobthehop [ Thu Apr 11, 2013 3:32 am ]
Post subject:  Re: News Code - Help!

theforexedge
1. click on quick reply
2. choose full editor
3. you get screen as per attached
4. click on Code button
5. Copy paste your code between the two code tags

Done
Author:  theforexedge [ Thu Apr 11, 2013 6:20 am ]
Post subject:  Re: News Code - Help!

Thanks Mob :D
All times are UTC Page 1 of 1