Trader’s Tech – Writing Your Own EA Part 32 – Reusable Code

Forex RobotGood morning, fellow traders!

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 31 we elaborated on the ins and outs of the MAXover() function. In this installment, I’m going to share another super-secret, handshake-only include function, PcntTradeSize(), which returns the trade size that will result in a trade risk that is a specified percentage of the lower of your Account Balance or Account Equity.

Let’s start with clarifying the difference between Account Balance and Account Equity. My assumption from the beginning of this article series has been that the reader is already a trader and understands basic trading terminology. My other assumption is that the trader is not familiar with any aspect of programming. I know that makes it tough for new traders and experience programmers, but I’ve got to have a starting point, right? In addition, I’ve also assumed that the reader may or may not be familiar with MetaTrader. I’m pretty sure that the terminology Account Balance and Account Equity is fairly common in the trading community, but I want to be sure we’re on the same page.

Account Balance is the total amount deposited in your trading account not taking into account any open positions. Account Equity is the Account Balance adjusted by the profit/loss and margin consumed by the currently open positions, in other words, this would be the amount in your account if you closed all the currently open positions. So, if all the positions you have open are net positive (including used margin), your Account Equity could be greater than your Account Balance. If your positions are net negative, your Account Equity could be smaller than your Account Balance. Programmatically, the internal functions AccountBalance() and AccountEquity() return the Account Balance and Account Equity, respectively, in your account. The PcntTradeSize() uses the LOWER of Account Balance and Account Equity to calculate your trade size. This could create a problem for which you would have to make adjustment in your coding if you are using a martingale-type money management scheme, as the trade sizes would get smaller as the Account Equity got smaller.  The function could be easily adjust to always use the Account Balance for the calculation if you desired. Or you could adjust it in such a way that you could tell it each time how you wanted the percentage calculated. But that’s an issue for another time.

As before, I’m just going to blow out the code and we’ll talk about the ins and outs in our next installment.

 

//+------------------------------------------------------------------+
//|                                                PcntTradeSize.mq4 |
//|                         Copyright © 2011, T Black and Associates |
//|                                    http://winnersedgetrading.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, T Black and Associates"
#property link      "http://winnersedgetrading.com"

double PcntTradeSize(string symbol,int PipRisk,double Pcnt,int NumOfPos=1,
                     bool FixedAmt=false,bool Diagnose=false)
   {
   double AcctAmt;
   double TradeSize;
   double Multiplier=Pcnt/100;
   int   FiveDig;
   string Version="v1.13";

   if(Digits==5||Digits==3)
      FiveDig = 10;
   else
      FiveDig = 1;

   if(Diagnose)
      {
      Print("---PcntTradeSize() AccountBalance()=",AccountBalance(),
            ", AccountEquity()=",AccountEquity(),", MODE_TICKVALUE=",
            MarketInfo(symbol,MODE_TICKVALUE));
      Print("---PcntTradeSize() MODE_MAXLOT=",MarketInfo(symbol,MODE_MAXLOT),
            ", MODE_MINLOT=",MarketInfo(symbol,MODE_MINLOT),", MODE_LOTSTEP=",
            MarketInfo(symbol,MODE_LOTSTEP));
      }  //if(Diagnose)

   // Use lower of AccountBalanc() & AccountEquity()
   AcctAmt = MathMin(AccountBalance(), AccountEquity());
   if(Diagnose)       
      Print("---PcntTradeSize() AcctAmt=",AcctAmt,", Multiplier=",Multiplier,
            ", PipRisk=",PipRisk);    
   if(!FixedAmt)          
      TradeSize = AcctAmt*Multiplier/PipRisk/(MarketInfo(symbol,MODE_TICKVALUE)*
                  FiveDig);    
   else       
      TradeSize = Pcnt/PipRisk/(MarketInfo(symbol,MODE_TICKVALUE)*FiveDig);    
   if(Diagnose)       
      Print("---PcntTradeSize() Interim TradeSize=",TradeSize,
            ", NumOfPos=",NumOfPos);    
   TradeSize = TradeSize / NumOfPos;    
   Print("PcntTradeSize() - Initial TradeSize=",TradeSize);        
   if(TradeSize > MarketInfo(symbol,MODE_MAXLOT))
      TradeSize = MarketInfo(symbol,MODE_MAXLOT);
   if(TradeSize < MarketInfo(symbol,MODE_MINLOT))
      TradeSize = MarketInfo(symbol,MODE_MINLOT);

//   Print("TickValue=",MarketInfo(symbol,MODE_TICKVALUE),
           ", LotStep=",MarketInfo(symbol,MODE_LOTSTEP)); 
//   Print("Before=",TradeSize); 

   TradeSize = MathRound(TradeSize / MarketInfo(symbol,MODE_LOTSTEP) - 0.5);

   TradeSize = TradeSize * MarketInfo(symbol,MODE_LOTSTEP);

   Print("PcntTradeSize() ",Version," ",symbol," Calc Trade Size=",TradeSize,
         ", PipRisk=",PipRisk,", Acct Pcnt=",Pcnt); 

   //Normalize TradeSize
   NormalizeDouble(TradeSize,2);
   return(TradeSize);
   }  //double PcntTradeSize(string symbol,int PipRisk,double Pcnt...

Next installment we’ll look at the ins and outs of the PcntTradeSize() function and discuss a couple of programming tricks you might see in it. Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Connect_With_Winners_Edge

Get trade set ups everyday!

 

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news