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

coding help; if closed trade was at loss...
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4620
Page 1 of 1
Author:  revotrader [ Sun Feb 21, 2016 12:52 am ]
Post subject:  coding help; if closed trade was at loss...

not sure if this post is in correct place, but here goes...

I am a beginner-coder, and have come to a block wall; i am unable to find solution mentioned in any of my coding material!

I have condition, if cross-under (number of open trades), but i want to add "&& if most recent trade closed was at a loss"; then open trade. What do i add after && ??

Appreciate your answer or even advise where to go find the answer myself, THANKS.
Author:  renexxxx [ Sun Feb 21, 2016 2:42 am ]
Post subject:  coding help; if closed trade was at loss...

revotrader » Sun Feb 21, 2016 10:52 am wrote:not sure if this post is in correct place, but here goes...

I am a beginner-coder, and have come to a block wall; i am unable to find solution mentioned in any of my coding material!

I have condition, if cross-under (number of open trades), but i want to add "&& if most recent trade closed was at a loss"; then open trade. What do i add after && ??

Appreciate your answer or even advise where to go find the answer myself, THANKS.
Without knowing anything more about your requirements, you can do something like this:

Code: Select all

   double profit = 0.0; 
   if ( OrderSelect( OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY ) ) {
      profit = OrderProfit() + OrderSwap() + OrderCommission();
   }
   
   if ( /* cross-under */ && ( profit < 0.0 ) ) {
      
      // Open trades ...
   }
This assumes that there are no other EAs with other magic numbers at work on the same account, and the most recent order you want is the one indexed by OrdersHistoryTotal() -1.

Hope this helps.
Author:  SteveHopwood [ Sun Feb 21, 2016 9:54 am ]
Post subject:  coding help; if closed trade was at loss...

Code: Select all

   double profit = 0;
   
   //Loop backwards through the history tab to find the first trade owned by the EA
   for (int cc = 0; cc <= OrdersHistoryTotal(); cc++)
   {
      //Does the EA 'own' this trade
      if (!OrderSelect(cc, SELECT_BY_POS, MODE_HISTORY) ) continue;
      if (OrderSymbol() != Symbol() ) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      
      //We have got this far, so we have found a trade owned by the ea.
      profit = OrderSwap() + OrderProfit() + OrderCommission();
      
      //Was it a loser?
      if (profit < 0)
         if (cross under requirement is met)
         {
            do stuff to send the trade
         }//if (cross under requirement is met)
         
      //EITHER
      //Nothing else to do, so exit the function
      return;
      
      //OR
      //Nothing else to do, so exit the loop
      break;
      
   }//for (int cc = 0; cc <= tot -1; cc++)
   
:xm:
Author:  revotrader [ Sun Feb 21, 2016 11:24 pm ]
Post subject:  coding help; if closed trade was at loss...

wow! amazing, fast responses, thanks. pre-lim testing shows change in p/l, so my added code appears to have made a change, now to test it if it works in real time pa! woohoo!

Thanks again.
Author:  SteveHopwood [ Sun Feb 21, 2016 11:32 pm ]
Post subject:  coding help; if closed trade was at loss...

revotrader » Sun Feb 21, 2016 11:24 pm wrote:wow! amazing, fast responses, thanks. pre-lim testing shows change in p/l, so my added code appears to have made a change, now to test it if it works in real time pa! woohoo!

Thanks again.
Up an' at 'em, Tiger.

I was once where you are now. Keep at it. You will not be sorry in the long term. Short term: misery and pain. Hey ho.

:xm:
Author:  revotrader [ Thu Feb 25, 2016 1:41 am ]
Post subject:  coding help; if closed trade was at loss...

after testing, my code was major fail. I have attached my code.

Please add code "if trade closed at loss" to each of the crossunder conditions to open trades.
Author:  SteveHopwood [ Fri Feb 26, 2016 11:55 pm ]
Post subject:  coding help; if closed trade was at loss...

Give anybody here one single reason for adding the code you require. What do they have to gain? You come across as a wet wimp who does not deserve help.

You are perfectly entitled to ask for help - indeed there are many and varied people at shf only too willing to offer such help. They are prepared to help; they are not prepared to write your code for you free of charge. To ask them to do so is insulting.

I made my offering here on the assumption that you were genuinely trying to help yourself. Clearly I was wrong.

Stop being wet an pathetic and do some work for yourself.

Reason I am so unsympathetic? Working stuff out for myself is what I did. For hours and hours, over days and days, and weeks and weeks and months and months. For years.

Yes, I did ask for help. People were only too happy to give it because they had watched me work stuff out form myself for hours and hours..................................

Grow up. This thread is locked.
All times are UTC Page 1 of 1