Need some experts' help to see my mistake

Locked
juniorlcq93
Trader
Posts: 18
Joined: Sun Dec 22, 2013 9:13 am

Need some experts' help to see my mistake

Post by juniorlcq93 »

Hello guys .

I had been trying to make an trailing function inside my EA that will go through all orders in the pool , and modify by trailing the stop loss for every order in the pool that meets the condition . I tried it on 2 different demo account , but it seems like always the last order in the pool , the stop loss only moved ONCE which it actually should move more than once since it meet the trailing target . It only work sometimes not all the time , I'm not sure what's happening but I'd look through it many times the code SEEMS ( not confirmed ) fine TO ME .



Anyone mind to give some help :( ?

Code: Select all

//+------------------------------------------------------------------+
//| Program's global variables                                       |
//+------------------------------------------------------------------+
   extern double StartTrailingStop = 15.0 ;
   extern double TrailingStop = 5.0 ;
   extern double CummulativeValue = 5.0 ;
   extern int    NumberOfTrail = 10 ;
   double Figure ;
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Program's sub-function                                           |
//+------------------------------------------------------------------+
 double PointsDetection()
   {
      
      Figure = 0.0 ;
      
      
      
      
      
         if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.01 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.001 )
         {
            Figure = 0.01 ;
         }
         else
         {
            if ( MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.0001 || MarketInfo ( OrderSymbol() , MODE_POINT ) == 0.00001 )
            {
               Figure = 0.0001 ;
            }
         }
         
   return ( Figure ) ;
   }



//+------------------------------------------------------------------+
//| Program's sub-function                                           |
//+------------------------------------------------------------------+
   void ProcessTrailingStop()
   {
      
      bool Result ;
      
      
      
      
      
         for ( int x = ( OrdersTotal() - 1 ) ; x >= 0 ; x-- )
         {
            if ( OrderSelect ( x , SELECT_BY_POS , MODE_TRADES ) == True )
            {
               PointsDetection() ;
               
               
               

                  if ( OrderType() == OP_BUY )
                  {
                     for ( int xai = 0 ; xai < NumberOfTrail ; xai++ )
                     {
                        if ( xai == 0 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() <= OrderOpenPrice() ) || ( OrderStopLoss() < ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                        
                        
                        
                        if ( xai == 1 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( TrailingStop * Figure ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                        
                        
                        
                        if ( xai >= 2 )
                        {
                           if ( ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * ( xai - 1 ) ) ) * Figure ) ) ) )
                           {
                              Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * xai ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                           }
                        }
                     }
                  }
                  else
                  {
                     if ( OrderType() == OP_SELL )
                     {
                        for ( int xaii = 0 ; xaii < NumberOfTrail ; xaii++ )
                        {
                           if ( xaii == 0 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= StartTrailingStop ) && ( ( OrderStopLoss() == 0 ) || ( OrderStopLoss() >= OrderOpenPrice() ) || ( OrderStopLoss() > ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                           
                           
                           
                           if ( xaii == 1 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( TrailingStop * Figure ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                           
                           
                           
                           if ( xaii >= 2 )
                           {
                              if ( ( ( ( OrderOpenPrice() - OrderClosePrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xaii ) ) ) && ( OrderStopLoss() == ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * ( xaii - 1 ) ) ) * Figure ) ) ) )
                              {
                                 Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() - ( ( TrailingStop + ( CummulativeValue * xaii ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                              }
                           }
                        }
                     }
                  }


User avatar
milanese
TechAdmin
Posts: 3293
Joined: Wed Jan 09, 2013 9:02 am
Location: btr rdx, r8 +

Need some experts' help to see my mistake

Post by milanese »

I am not sure what you want to, do but OrderClosePrice() will imo not work you should try with Bid and Ask, as OrderClosePrice() returns close price of the currently selected order, and you can only trail open and not closed orders..

Cheers :)

Tommaso
Global Prime is the official SHF broker :yahoo:
Searching for Servers and Workstations with individual configuration?
Just PM
:smile: Click here to go to the BoardKnowledgeBase
NOTE: Cookies and JavaScript are required for the using the board, with full functionality
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Need some experts' help to see my mistake

Post by SteveHopwood »

Understand also that MarketInfo ( OrderSymbol() , MODE_POINT ) is notorious for returning zero, nicely buggering up everything. Dip into my shell code at http://www.stevehopwoodforex.com/phpBB3 ... ?f=15&t=79
and use lifesys' pip factor conversion instead. You will also find a working trailing stop function you can adapt to your own needs.

:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
juniorlcq93
Trader
Posts: 18
Joined: Sun Dec 22, 2013 9:13 am

Need some experts' help to see my mistake

Post by juniorlcq93 »

milanese » Sun Jul 06, 2014 4:58 pm wrote:I am not sure what you want to, do but OrderClosePrice() will imo not work you should try with Bid and Ask, as OrderClosePrice() returns close price of the currently selected order, and you can only trail open and not closed orders..

Cheers :)

Tommaso


Hi , what I wanted to build was to trail every pairs available in the pool singularly .
Let's say I have 3 pairs currently floating , all these 3 pairs will be trail individually according to my trailing setup .

1st :
When each individual order's price has a net movement of more than/equals to StartTrailingStop and the order's stop loss is equals to no stop loss OR stop loss is less or more than or equals to ( depending whether is buy/sell order ) open price OR stop loss is less or more than ( depending whether is buy/sell order ) open price + value of TrailingStop , it will move stop loss to open price + value of TrailingStop .

2nd :
When each individual order's price has a net movement of more than/equals to StartTrailingStop + CummulativeValue and the order's stop loss is equals to the 1st's stop loss which is open price + value of TrailingStop , it will move new stop loss to open price + value of TrailingStop + CummulativeValue .

3rd :
This part will start to trail accordingly . I uses a for loop for this part after the 1st and 2nd condition had met .....


I thought that OrderClosePrice() for opened orders is equal to the price stated on the right of the TP position in Empty4 ?
Last edited by juniorlcq93 on Sun Jul 06, 2014 11:46 am, edited 1 time in total.
juniorlcq93
Trader
Posts: 18
Joined: Sun Dec 22, 2013 9:13 am

Need some experts' help to see my mistake

Post by juniorlcq93 »

SteveHopwood » Sun Jul 06, 2014 6:33 pm wrote:Understand also that MarketInfo ( OrderSymbol() , MODE_POINT ) is notorious for returning zero, nicely buggering up everything. Dip into my shell code at http://www.stevehopwoodforex.com/phpBB3 ... ?f=15&t=79
and use lifesys' pip factor conversion instead. You will also find a working trailing stop function you can adapt to your own needs.

:xm:


Hi Steve , hmmmph strange but my EA uses OrderClosePrice() works fine though ..... I uses MarketInfo in scripts ....
User avatar
milanese
TechAdmin
Posts: 3293
Joined: Wed Jan 09, 2013 9:02 am
Location: btr rdx, r8 +

Need some experts' help to see my mistake

Post by milanese »

juniorlcq93 » Sun Jul 06, 2014 11:26 am wrote:
Hi , what I wanted to build was to trail every pairs available in the pool singularly .
Let's say I have 3 pairs currently floating , all these 3 pairs will be trail individually according to my trailing setup .

1st :
When each individual order's price has a net movement of more than/equals to StartTrailingStop and the order's stop loss is equals to no stop loss OR stop loss is less or more than or equals to ( depending whether is buy/sell order ) open price OR stop loss is less or more than ( depending whether is buy/sell order ) open price + value of TrailingStop , it will move stop loss to open price + value of TrailingStop .

2nd :
When each individual order's price has a net movement of more than/equals to StartTrailingStop + CummulativeValue and the order's stop loss is equals to the 1st's stop loss which is open price + value of TrailingStop , it will move new stop loss to open price + value of TrailingStop + CummulativeValue .

3rd :
This part will start to trail accordingly ....

I thought that OrderClosePrice() for opened orders is equal to the price stated on the right of the TP position in Empty4 ?
no this is with BUY orders the Bid and with SELL orders the Ask price..
You should take a look to Steve's shell..

Cheers :)

Tommaso

Tommaso
Global Prime is the official SHF broker :yahoo:
Searching for Servers and Workstations with individual configuration?
Just PM
:smile: Click here to go to the BoardKnowledgeBase
NOTE: Cookies and JavaScript are required for the using the board, with full functionality
juniorlcq93
Trader
Posts: 18
Joined: Sun Dec 22, 2013 9:13 am

Need some experts' help to see my mistake

Post by juniorlcq93 »

I tried swaping OrderClosePrice() to the MarketInfo one on the previous code and the edited ( removing the && condition and put them as a if statement after the previous if statement , the one in the 2nd for loop ) , but the result is still sometimes working sometimes not working . :arrrg: :arrrg: :arrrg: :cry: :cry: :cry:
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Need some experts' help to see my mistake

Post by SteveHopwood »

juniorlcq93 » Sun Jul 06, 2014 11:43 am wrote:
Hi Steve , hmmmph strange but my EA uses OrderClosePrice() works fine though ..... I uses MarketInfo in scripts ....
Ok buster. It is entirely up to you whether you accept the advice offered to you here. You can continue to use MarketInfo if you want. Maybe one day, after many, many hours hunting through your code for a reason why a variable that clearly should not have a value of zero indeed has that value, your memory might dimly stir a passing recollection of the advice I offered you.

You come across as a bit of a beginner in all this.

I have been coding this stuff for about 8 years. More to the point, some of the truly professional coders here have helped me refine my code to the point that it is (mostly) fool proof. I have offered you advice.

milanese is one of the cleverest people you will meet in this forum. He has been coding this stuff for years and laughs at the sort of thing you are having difficulty with.

I have offered you advice. He has offered you advice. You appear to be rejecting all of this. This is not a good idea.

Accept good advice when offered, or piss off an play in someone else's sandpit. Do not ask for help if you are unwilling to accept it when offered. It will not be offered a second time.

To give you a tiny insight into what happens when someone like you annoys me a bit, this thread is now locked. Imagine what will happen if you annoy me a lot.

:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Locked

Return to “Coders Hangout”