News Code - Help!

Post Reply
theforexedge
Trader
Posts: 220
Joined: Thu Apr 26, 2012 10:31 pm
Location: Torquay

News Code - Help!

Post by theforexedge »

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);
}
}
User avatar
slipshod
Trader
Posts: 404
Joined: Tue Dec 27, 2011 9:14 am
Location: Australia

Re: News Code - Help!

Post by slipshod »

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.
Last edited by slipshod on Tue Apr 09, 2013 12:32 pm, edited 1 time in total.
theforexedge
Trader
Posts: 220
Joined: Thu Apr 26, 2012 10:31 pm
Location: Torquay

Re: News Code - Help!

Post by theforexedge »

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
User avatar
slipshod
Trader
Posts: 404
Joined: Tue Dec 27, 2011 9:14 am
Location: Australia

Re: News Code - Help!

Post by slipshod »

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.
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: News Code - Help!

Post by dietcoke »

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)
Image
theforexedge
Trader
Posts: 220
Joined: Thu Apr 26, 2012 10:31 pm
Location: Torquay

Re: News Code - Help!

Post by theforexedge »

DC

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

Jason
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: News Code - Help!

Post by mobthehop »

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
You do not have the required permissions to view the files attached to this post.
theforexedge
Trader
Posts: 220
Joined: Thu Apr 26, 2012 10:31 pm
Location: Torquay

Re: News Code - Help!

Post by theforexedge »

Thanks Mob :D
Post Reply

Return to “Coders Hangout”