0% found this document useful (0 votes)
14 views1 page

Dynamic Lot Size Calculation in Trading

The document defines a Lot_Size function that calculates different trade statistics like win/loss ratios, average profits/losses, and Kelly criterion or optimal trade size based on past order history and parameters passed to the function. It loops through the order history to aggregate the numbers of wins/losses and profits/losses for buys and sells, then calculates various ratios from these values to determine a recommended lot size.

Uploaded by

Cardoso Penha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Dynamic Lot Size Calculation in Trading

The document defines a Lot_Size function that calculates different trade statistics like win/loss ratios, average profits/losses, and Kelly criterion or optimal trade size based on past order history and parameters passed to the function. It loops through the order history to aggregate the numbers of wins/losses and profits/losses for buys and sells, then calculates various ratios from these values to determine a recommended lot size.

Uploaded by

Cardoso Penha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

double Lot_Size(int Switch){

static int Saved_His_Total;


int His_Total=OrdersHistoryTotal();
if(His_Total==0){return(0.1);}
if(Saved_His_Total != His_Total){
Saved_His_Total=His_Total;
for(int i=His_Total; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
&& OrderMagicNumber()==Magic
&& OrderSymbol()==Symbol()
){
static int Trade_Total; Trade_Total++;
static int Buy_Wins; static int Sel_Wins;
static int Buy_Loss; static int Sel_Loss;
static double Buy_Profit, Buy_Losses;
static double Sel_Profit, Sel_Losses;
if(OrderType()==OP_BUY && OrderProfit()>0){
Buy_Wins++; Buy_Profit+=OrderProfit();}
if(OrderType()==OP_BUY && OrderProfit()<=0){
Buy_Loss++; Buy_Losses+=OrderProfit();}
if(OrderType()==OP_SELL && OrderProfit()>0){
Sel_Wins++; Sel_Profit+=OrderProfit();}
if(OrderType()==OP_SELL && OrderProfit()<=0){
Sel_Loss++; Sel_Losses+=OrderProfit();} break;
} }
double Win_Total = Buy_Wins + Sel_Wins;
double Loss_Total = Buy_Loss + Sel_Loss;
double Profit_Total = Buy_Profit + Sel_Profit;
double Losses_Total = Buy_Losses + Sel_Losses;
if(Win_Total !=0){double Avg_Profit = Profit_Total / Win_Total;}
if(Loss_Total!=0){double Avg_Losses = Losses_Total / Loss_Total;}
if(Trade_Total !=0){double W2L_Ratio = Win_Total / Trade_Total;}
if(Trade_Total !=0){double L2W_Ratio = Loss_Total / Trade_Total;}
if(Avg_Losses !=0){double P2L_Ratio = Avg_Profit / Avg_Losses;}
if(Avg_Losses !=0){double Kd=Avg_Profit/Avg_Losses;}
//~~~~~~~~~~~~~~~~~~~~~
if(W2L_Ratio !=0){double RvRoulette=(0.47/W2L_Ratio)*0.1;}
//~~~~~~~~~~~~~~~~~~~~~
//[Link]
if(Kd !=0){double Kelly=(W2L_Ratio-L2W_Ratio)/(Avg_Profit/Avg_Losses);}
if(P2L_Ratio!=0){double Optimal_f=((P2L_Ratio + 1)*W2L_Ratio-1)/P2L_Ratio;}
//~~~~~~~~~~~~~~~~~~~~~
if(Trade_Total==0){return(0.1);}
if(Switch=='R'){return(RvRoulette);}
if(Switch=='K'){return(Kelly);}
if(Switch=='F'){return(Optimal_f);}

You might also like