[Solved] How to calculate margin while hegding

 

Hi,

I'm trying to calculate the margin while hedging. In the code below I made an atempt to calculate the correct margin. The function works fine if there are only trades open in one direction. But it's totally off when the EA has trades open in both directions. Any help would be highly appreciated!


thanks!


Russell


#define GET       0
#define PUT       1
#define RESET     2
#define INIT      3

#define EA_BALANCE      0
#define EA_PROFIT       1
#define EA_MARGIN       2
#define EA_FREEMARGIN   3
#define EA_EQUITY       4
#define EA_TOTAL_SELL   5
#define EA_TOTAL_BUY    6

#define DATA_PROFIT     0
#define DATA_MARGIN     1
#define DATA_TOTAL      2
double gdFund = 500;
int giMagicNumber = 1234;

int init(){
   InfoMagicNumber(0, giMagicNumber, INIT);
}

int start(){
   ldRisk = 3;
   InfoMagicNumber(0, giMagicNumber, PUT);
   ldLots = InfoMagicNumber(EA_FREEMARGIN, giMagicNumber)*ldRisk/10000.0; 
   
   // more code
}

double InfoMagicNumber(int iValue, int iMagicNumber, int iFunction = GET){

   if (IsOptimization() != FALSE || IsTesting() != FALSE){
      //return(InfoMagicNumberOptimization(iValue, iMagicNumber, iFunction));
   }
   
   static double ldData[3][2];
   static int liOHTCounted = 0;
   if(iFunction == INIT){
      liOHTCounted = 0;
      ldData[DATA_PROFIT][0] = gdFund;
   }
   if(iFunction == PUT){
      int    liOT = OrdersTotal();
      int    liOHT = OrdersHistoryTotal(); 
            
      ldData[DATA_PROFIT][1] = 0;            
      ldData[DATA_MARGIN][1] = 0;
      ldData[DATA_TOTAL][OP_SELL] = 0;
      ldData[DATA_TOTAL][OP_BUY] = 0;
      
      for(int i = liOHTCounted; i < liOHT; i++){
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber){
            ldData[DATA_PROFIT][0]+= OrderProfit()+OrderCommission()+OrderSwap();                  
         }         
      }
      liOHTCounted = liOHT;
      
      for(i = 0; i < liOT; i++){
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber){
            ldData[DATA_PROFIT][1]+= OrderProfit()+OrderCommission()+OrderSwap();  
            //ldData[DATA_MARGIN][1]+= (MarketInfo(OrderSymbol(), MODE_MARGINREQUIRED)*OrderLots());//*AccountLeverage();
            ldData[DATA_TOTAL][OrderType()]++;   
         }
      }
      double ldHedgeTotal=0;
      int liHedgeCount=0;
      ldHedgeTotal = ldData[DATA_TOTAL][OP_SELL]*2;
      if (ldData[DATA_TOTAL][OP_BUY] < ldData[DATA_TOTAL][OP_SELL]){
         ldHedgeTotal = ldData[DATA_TOTAL][OP_BUY]*2;
      }
      
      for(i = 0; i < liOT; i++){
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber){ 
            if (liHedgeCount >= liOT-ldHedgeTotal){
               ldData[DATA_MARGIN][1]+= (MarketInfo(OrderSymbol(), MODE_MARGINREQUIRED)*OrderLots()*0.5);
               
            } else {
               ldData[DATA_MARGIN][1]+= (MarketInfo(OrderSymbol(), MODE_MARGINREQUIRED)*OrderLots());
            }
            liHedgeCount++;
         }
      }
      
   } else if(iFunction == GET){
      switch(iValue){
         case EA_BALANCE:
            return(ldData[DATA_PROFIT][0]);
            break;
         case EA_PROFIT:
            return(ldData[DATA_PROFIT][1]);
            break;
         case EA_MARGIN:
            return(ldData[DATA_MARGIN][1]);
            break;
         case EA_FREEMARGIN:
            Print (AccountMargin()," ",(ldData[DATA_MARGIN][1]));            
            return(ldData[DATA_PROFIT][0]+ldData[DATA_PROFIT][1]-ldData[DATA_MARGIN][1]);
            break;
         case EA_EQUITY:
            return(ldData[DATA_PROFIT][0]+ldData[DATA_PROFIT][1]);
            break;
         case EA_TOTAL_SELL:
            return(ldData[DATA_TOTAL][OP_SELL]);
            break;
         case EA_TOTAL_BUY:
            return(ldData[DATA_TOTAL][OP_BUY]);
            break;
      }
   }
}
 
Russell, I know you are asking for help here yourself, but (if I may) it seems like you have a similar hedge challenge as I do. I'm trying to automate Stop and Limit entry so that I can place an order and achieve a stop that's X times the spread doubled, and a Limit that's X + 1 times the spread doubled PLUS one pip. (So if the spread for a Buy is 2 pips, then 10 x 4 = 40...that's the Stop. And 11 x 4 + 1 = 45...the Limit) These variables can be tightened up significantly if I enjoy the speed of automation. I'm betting I won't experience a reverse within that spread range plus my one pip profit. Buy and Sell every currency (the conventional hedge), and just use the above math to make a profit. Does any of this make sense, and (more importantly) can it be automated? Do you know anyone who could help? Sparks
 

Sparks,

Any linear process can be automated with fairly simple coding. So I guess it won't be a problem.

As for my problem... just found the answer: I made an error not taking the lotsize into account while calculating the margin.


Russell

Reason: