From Forex to cfds. Pips , Lots and used Margin calculation

 

Hi.

I used to trade only forex , and i was using this part of the code to find out how many pips is the stop loss, and if it was more than 300 pip then i entered the trade.


//to find the stop value i use fibonacci.




   double SymPoint=0;
   if(SymbolInfoInteger(thissymbol,SYMBOL_DIGITS) == 3)
     {
      SymPoint = 0.01;
     }
   if(SymbolInfoInteger(thissymbol,SYMBOL_DIGITS) == 4)
     {
      SymPoint = 0.0001;
     }
   if(SymbolInfoInteger(thissymbol,SYMBOL_DIGITS) == 5)
     {
      SymPoint = 0.0001;
     }


 double  pipsdistance=MathAbs(iClose(thissymbol,TF,0) - stop)/SymPoint;


before entering the trade i used the bellow part of the code to make sure that each symbol uses the same amount of margin

double Deposit= 10000;
   double division =100000;
   double high=AccountInfoDouble(ACCOUNT_BALANCE);
   double Lots=high/division;
   
/// For every trade i want to use 50$ used margin. So x=50
     double x=50;
   
   
   double Mgn=0;
   
   
               if(dir=="buy")
               {
                  if(OrderCalcMargin(ORDER_TYPE_BUY,thissymbol,Lots,SymbolInfoDouble(thissymbol,SYMBOL_ASK),Mgn)==true)
                     {
                    
                     }              
               }
            if(dir=="sell")
               {
                  if(OrderCalcMargin(ORDER_TYPE_SELL,thissymbol,Lots,SymbolInfoDouble(thissymbol,SYMBOL_BID),Mgn)==true)
                     {
                       
                     }           
               }
   

   int k=1;
   while (Mgn<x)
   {
   k=k+1;
     double x=50*(high/division);
               if(dir=="buy")
               {
                  if(OrderCalcMargin(ORDER_TYPE_BUY,thissymbol,Lots,SymbolInfoDouble(thissymbol,SYMBOL_ASK),Mgn)==true)Lots=(high/division)*k;
               }
            if(dir=="sell")
               {
                  if(OrderCalcMargin(ORDER_TYPE_SELL,thissymbol,Lots,SymbolInfoDouble(thissymbol,SYMBOL_BID),Mgn)==true)Lots=(high/division)*k;
               }
   }





So now i want to try my expert advisor on cfds too, but i see that the contract size is difrerent (usually 10 ) and it used to be 100,000 on forex.

And i suppose this will affect my lotsize.

So before trying my code, i want to ask  how to calculate the pipsdistance the Used Margin (Mgn) and the Lots size on cfds.

Thank you.

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5