Forex
Google
New signals service!

Go Back   Forex Trading > Downloads > Tools and utilities


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

 
 
LinkBack Thread Tools
 
Old 05-28-2008, 05:01 AM
seawolf's Avatar
Junior Member
 
Join Date: Jul 2007
Posts: 3
seawolf is on a distinguished road
Position Sizing Calculator

I'm building a Position Sizing calculator as a function based on the "Kelly Formula"

(Win Rate-((1-Win Rate)/(Avg Win/Avg Loss)

I've got the over all code and calculations working with manual inputs (extern) for the required variables and am now trying to get the function working dynamically by calling certain account information (namely I want to calculate the Winning consistency rate (%), the avg # pips per winning trade, and the avg # pips per lossing trade)

I could use any and all help getting the three functions (WinRate AvgWin & AvgLoss) operating. I have been using the manual input variation for months and it works great. Here is the complete code for this (automated) version to this point... in testing I am getting no dynamic output, everything goes back to the default setting (50, 40, 20). I have this set up as it's own EA for testing and easy modularization into any existing EA. once attached to any chart, the output is printed in the log/expert tab. the use of fractals is intentional so that maximum account growth (or minimal loss) is exploited. as a note most Brokers offering the MT trader platform allow fractal trading for either mini or std lots. This will prove use full in the future with money management that can take off partial lot positions (ie: remove 25% of 1 Lot). anyway...

in order to collect the real time account info I need I am trying to...
1. count all trades
2. count trades that are profitable
etc. etc.

I may or may not be going about this the right way.


Thanks in advance for all the help...
SeaWolf







//+------------------------------------------------------------------+
//| KellyFormula.mq4 |
//+------------------------------------------------------------------+
#property copyright "seawolf"
#property link "seawolf"
//+------------------------------------------------------------------+
//| EXTERNAL INFORMATION INPUT |
//+------------------------------------------------------------------+
extern int MyAccount = 1001; //------>>>> Account ID
extern int ExpertID = 500001; //------>>>> Magic Number for this EA

extern double PipValue= 1.00; //------>>>> use for ALL calc's
extern double LotCost= 50.0; //------>>>> use for ALL calc's
extern double PercentMax= 24.0; //------>>>> max % account leveraged @ one time
extern int TradesMax= 3; //------>>>> max simultaniouse trades (example: 24%/3 trades = 8% per trade)

extern bool UseKelly= true; //------>>>> Manual overide toggle
extern double ManualLots= 1.0; //------>>>> # lots if "UseKelly" is false
extern double mWinRate= 50.00; //------>>>> winning consistancy in % (manual overide)
extern int mAvgWin= 40; //------>>>> avg # pips per winning trade (manual overide)
extern int mAvgLoss= 20; //------>>>> avg # pips per lossing trade (manual overide)


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

PositionSize();
{
Print("Lots=",PositionSize()," WinRate=",WinRate()," AvgWin=",AvgWin()," AvgLoss=",AvgLoss());
}

Comment("Current Time is ",TimeToStr(TimeCurrent(),TIME_MINUTES)," GMT ",TimeToStr(TimeCurrent(),TIME_DATE)," ... Win Rate= ",WinRate()," Avg Win= ",AvgWin()," Avg Loss= ",AvgLoss());

//----
return(0);
}
//----
//+------------------------------------------------------------------+
//| CALCULATE POSITION SIZE FOR ALL NEW TRADES |
//+------------------------------------------------------------------+
//------------------------>>>>
double PositionSize()
{
//------------------------>>>> DO NOT USE KELLY FORMULA, USE FLAT RATE
if(UseKelly == true)
{
double KelyForm = WinRate()-((1-WinRate())/(AvgWin()/AvgLoss()));
double PerTrade;
double Lots;

if(KelyForm > PercentMax)
{
PerTrade = (PercentMax/10)/TradesMax;
}
else if(KelyForm < PercentMax)
{
PerTrade = (KelyForm/10)/TradesMax;
}
else if(KelyForm == PercentMax)
{
PerTrade = (KelyForm/10)/TradesMax;
}
Lots = (PerTrade * AccountBalance()/LotCost);
return(MathRound(Lots)/10);
}
}

//+------------------------------------------------------------------+
//| COLLECT REAL TIME ACCOUNT INFO |
//+------------------------------------------------------------------+
//------------------------>>>>
double WinRate()
{
double Ticket;
double CountWins = 0;

for(Ticket=0;Ticket<OrdersTotal();Ticket++)
{
OrderSelect(Ticket,SELECT_BY_TICKET,MODE_HISTORY);
if(MyAccount==AccountNumber() && OrderSymbol()==Symbol() && OrderMagicNumber() == ExpertID)
{
//------>>>>
if(OrderType()==OP_BUY)
{
if(OrderClosePrice() >= OrderOpenPrice())
CountWins++;
}
else if(OrderType()==OP_SELL)
{
if(OrderClosePrice() <= OrderOpenPrice())
CountWins++;
}
}
}
if(CountWins > 0)
return(MathRound(CountWins/OrdersHistoryTotal())*10);
else
Print("Real Time WinRate not Available");
return(mWinRate);
}
//------>>>>
//------------------------>>>>
double AvgWin()
{
double Ticket;
double CountTrades = 0;
double CountPips = 0;

for(Ticket=0;Ticket<OrdersTotal();Ticket++)
{
OrderSelect(Ticket,SELECT_BY_TICKET,MODE_HISTORY);
if(MyAccount==AccountNumber() && OrderSymbol()==Symbol() && OrderMagicNumber() == ExpertID)
{
//------>>>>
if(OrderType()==OP_BUY && OrderClosePrice()>=OrderOpenPrice())
CountTrades++;
{
if(OrderProfit() >= 0)
CountPips++;
}
if(OrderType()==OP_SELL && OrderClosePrice()<=OrderOpenPrice())
CountTrades++;
{
if(OrderProfit() >= 0)
CountPips++;
}
}
}
if(CountPips > 0)
return(MathRound(CountPips/CountTrades)*10);
else
Print("Real Time AvgWin not Available");
return(mAvgWin);
}

//------>>>>
//------------------------>>>>
double AvgLoss()
{
double Ticket;
double CountTrades = 0;
double CountPips = 0;

for(Ticket=0;Ticket<OrdersTotal();Ticket++)
{
OrderSelect(Ticket,SELECT_BY_TICKET,MODE_HISTORY);
if(MyAccount==AccountNumber() && OrderSymbol()==Symbol() && OrderMagicNumber() == ExpertID)
{
//------>>>>
if(OrderType()==OP_BUY && OrderClosePrice()<OrderOpenPrice())
CountTrades++;
{
if(OrderProfit() < 0)
CountPips++;
}
if(OrderType()==OP_SELL && OrderClosePrice()>OrderOpenPrice())
CountTrades++;
{
if(OrderProfit() < 0)
CountPips++;
}
}
}
if(CountPips > 0)
return(MathRound(CountPips/CountTrades)*10);
else
Print("Real Time AvgLoss not Available");
return(mAvgLoss);
}

//---------------------------------------------------------------------+
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 05-28-2008, 07:31 AM
Junior Member
 
Join Date: Feb 2007
Posts: 3
Reunion is on a distinguished road
I'm finding money management model. Please help me

I've developed a trading system that guarantee 50% win with TP=15,SL=15 . If it combine with a proper MM it would be a profitable system.

Please suggest me some great money management model

Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-05-2008, 01:49 AM
Junior Member
 
Join Date: Dec 2007
Posts: 8
khalchian is on a distinguished road
Question how to Adjust lot size for money mgmt???

Hi Friends
I was reading the money mgmt thread and came across a suggestion by one of the member that one can control the lot size (Pip value) according to your risk management. I was just wondering how to accomplish that fete in MT4.

Last edited by khalchian; 06-05-2008 at 02:02 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-05-2008, 02:10 AM
Senior Member
 
Join Date: Nov 2006
Location: Shelbyville, TN
Posts: 104
frameguy is on a distinguished road
Lot Sizing

khalchian :

I will attempt to attach a PDF. Lot of reading. It's based on Van Tharp Theory.
It came from Linuxser's website.

Hope this helps. PDF is copyrighted.
Attached Files
File Type: pdf What-do-you-expect.pdf (221.8 KB, 134 views)
__________________
Not Everything that looks straight is: Until you measure it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-17-2008, 05:53 PM
Senior Member
 
Join Date: Oct 2007
Posts: 198
mikkom is on a distinguished road
Kelly ratio money management

Hi all,

I have been testing some interesting kelly variations, basically I have two variables that I'm using, the normal kelly position sizing and then short term win factor, for example last ten orders for the same type (short/long).

What I use is kelly*short_term_win_ratio (with some risk limits) and I'm going to experiment with "short term kelly" next.

At least my tests show that even the simple short term win ratio sizing gives a real edge with position sizing, it basically smooths whipsaws because the market itself tells when it's a good idea to trade long and/or short or when the both are not favourable.

I think this mostly applies to medium term trend following style techniques but I'm interested to hear if anyone else has tested anything similar.

Last edited by mikkom; 06-17-2008 at 06:03 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-17-2008, 07:52 PM
Senior Member
 
Join Date: Jan 2006
Posts: 1,119
omelette is on a distinguished road
If you have an 'edge' Kelly MM seems to do great - unfortunately, despite the equity curve below, (with Kelly MM turned on) once it hits 'real' pricedata, (not hard to spot!) the phantom-edge vanishes and no type of MM will make a loser profitable. At least, that's my experience...

Pity, it had me convinced I was onto something big for a while
Attached Images
File Type: gif TesterGraph.gif (8.3 KB, 430 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-17-2008, 08:03 PM
Senior Member
 
Join Date: Oct 2007
Posts: 198
mikkom is on a distinguished road
Quote:
Originally Posted by omelette View Post
If you have an 'edge' Kelly MM seems to do great - unfortunately, despite the equity curve below, (with Kelly MM turned on) once it hits 'real' pricedata, (not hard to spot!) the phantom-edge vanishes and no type of MM will make a loser profitable. At least, that's my experience...

Pity, it had me convinced I was onto something big for a while
Kelly is just a method to get optimal results from your trading algorithm, nothing more.

What I was talking was an adaptive method of using kelly to get better results than plain risk position sizing but too bad that now this whole issue will bury under page X of some gigantic thread and nobody will ever read about this.

By the way looking at your curve, you are NOT using kelly at least right.

Last edited by mikkom; 06-17-2008 at 08:22 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-17-2008, 11:53 PM
Senior Member
 
Join Date: Jan 2006
Posts: 1,119
omelette is on a distinguished road
Quote:
Originally Posted by mikkom View Post
Kelly is just a method to get optimal results from your trading algorithm, nothing more.

What I was talking was an adaptive method of using kelly to get better results than plain risk position sizing but too bad that now this whole issue will bury under page X of some gigantic thread and nobody will ever read about this.

By the way looking at your curve, you are NOT using kelly at least right.
I am, but it's too profitable initially so you only see it in the bit where the lotsize turns up - after that, lotsizing then goes on the kaputz 'cos WHC imposes a 20 lot limit thereafter! Still, despite unrealistic results like these, many people continue to trust this data...

Of course, irrespective of whose to 'blame', in the end you're right I suppose...

EDIT - Oh yeah, forgot that there's also a 'double-lots-during-off_peak_hours' switch turned on as well - so granted, not Kelly-MM...

Last edited by omelette; 06-17-2008 at 11:59 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-25-2008, 10:27 PM
darkkiller's Avatar
Senior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 207
darkkiller is on a distinguished road
who have MQ4 version for this LotCalculator?thanks
Attached Images
File Type: jpg lot.JPG (12.9 KB, 361 views)
Attached Files
File Type: ex4 LotCalculator v1.ex4 (4.4 KB, 104 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 06-26-2008, 02:20 PM
Drake's Avatar
Junior Member
 
Join Date: Jun 2008
Posts: 11
Drake is on a distinguished road
Nice,

But it doesn't show the risk percent in decimal size. It alwais rounds off by excess.

Is it possible to change it in this way, and select the size of character too ?

Thanks

Last edited by Drake; 06-26-2008 at 02:26 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 

Bookmarks

Tags
money management, money management code, moneymanagement
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed Making Perfect Trailing Stop EA sbwent Expert Advisors - Metatrader 4 27 03-13-2008 05:30 AM
The perfect trader black ice General Discussion 9 11-14-2006 06:51 AM
10k lot size brokers kevmcfoster Suggestions for Trading Systems 12 10-07-2006 12:25 AM
Must have utility for MT coders who are not 100% perfect ycomp Tools and utilities 2 02-23-2006 11:45 PM


All times are GMT. The time now is 08:26 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.