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

Case code
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4441
Page 1 of 1
Author:  simonhill11a [ Tue Oct 06, 2015 3:57 pm ]
Post subject:  Case code

I am trying to increase the lot size on successive losses using the case function but it jumps from step 1 to step 14 after one loss and can't figure out why can anyone help, thank you.

Code: Select all

 LossTrades = 0;
   LossCount=1;
   int LossCountCount=1;
   WinTrades = 0;
   OverallProfit = 0;
   
   
   
   for (int cc = 0; cc <= OrdersHistoryTotal(); cc++)
   {
      if (!OrderSelect(cc, SELECT_BY_POS, MODE_HISTORY) ) continue;
      if (OrderSymbol() != Symbol() ) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      
      OverallProfit+= (OrderProfit() + OrderSwap() + OrderCommission() );
      if (OrderProfit() > 0) WinTrades++;
      if (OrderProfit() < 0) LossTrades++; 
      
                           // End of the 'switch' body   
      
      
      
      
      
      if (OrderProfit() < 0) LossCountCount++;
     if (OrderProfit() > 0) LossCountCount=1;
   //  GlobalVariableSet("longtrade"+Symbol(),0);
    // GlobalVariableSet("shorttrade"+Symbol(),0);
     // if (OrderProfit() < 0) {step=step+1;}
     
     
      switch (LossCountCount)                           // Header of the operator 'switch'
     {                                  // Start of the 'switch' body
      case 1 : LossCount=1;          // Variations..
      case 2 : LossCount=1;    
      case 3 : LossCount=2;    
      case 4 : LossCount=3;          // Here are 10 variations ..
      case 5 : LossCount=4;          // ..'case' presented, but, in general, ..
      case 6 : LossCount=6;          // ..the amount of variations..
      case 7 : LossCount=9;          // ..'case' is unlimited  
      case 8 : LossCount=14;break;
         
      default: LossCount=1;// It is not the same as the 'case'
     }                                  // End of the 'switch' body   
      
     // GlobalVariableSet("Losscount"+Symbol(),LossCountCount);
      
     GlobalVariableSet("Step"+Symbol(),LossCount);
    
      
     // GlobalVariableSet("step"+Symbol(),step);
      
    }//for (int cc = 0; cc <= tot -1; cc++)
   
   
Author:  renexxxx [ Wed Oct 07, 2015 1:10 am ]
Post subject:  Case code

You must add a "break;" after the code in each case:, like so:

Code: Select all

      switch (LossCountCount)                           // Header of the operator 'switch'
     {                                  // Start of the 'switch' body
      case 1 : LossCount=1; break;          // Variations..
      case 2 : LossCount=1; break;   
      case 3 : LossCount=2; break;   
      case 4 : LossCount=3; break;         // Here are 10 variations ..
      case 5 : LossCount=4; break;         // ..'case' presented, but, in general, ..
      case 6 : LossCount=6; break;         // ..the amount of variations..
      case 7 : LossCount=9; break;         // ..'case' is unlimited  
      case 8 : LossCount=14;break;
         
      default: LossCount=1;// It is not the same as the 'case'
     }                                  // End of the 'switch' body  
The "switch-case" code construct, was designed to "fall-through", i.e. it executes the first case that applies and all the following cases thereafter, unless you break out of the switch.
Author:  simonhill11a [ Wed Oct 07, 2015 7:19 am ]
Post subject:  Case code

Thank you for your help
All times are UTC Page 1 of 1