• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 7:48am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 7:48am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

auto lot size based on risk% 8 replies

Which would you prefer: increasing lot size or TP size? 21 replies

At what lot size/account size do you have scale issues? 35 replies

calculating lot size based on money to risk, not % 4 replies

Lot size, contract size, max position ? 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 3
Attachments: Code to work out lot size based on risk
Exit Attachments
Tags: Code to work out lot size based on risk
Cancel

Code to work out lot size based on risk

  • Last Post
  •  
  • 1 Page 2
  • 1 Page 2
  •  
  • Post #21
  • Quote
  • Sep 1, 2011 1:20pm Sep 1, 2011 1:20pm
  •  pipmyride
  • | Joined Jan 2009 | Status: Member | 1,067 Posts
i must be really tripping, that script gives me correct result, but in my EA im getting that totally incorrect calculation, and i lifted the script and pasted into the EA :nuts:
 
 
  • Post #22
  • Quote
  • Sep 1, 2011 1:31pm Sep 1, 2011 1:31pm
  •  Xaphod
  • Joined Mar 2010 | Status: Member | 1,380 Posts
Restart your mt4 instance.
 
 
  • Post #23
  • Quote
  • Sep 1, 2011 2:14pm Sep 1, 2011 2:14pm
  •  pipmyride
  • | Joined Jan 2009 | Status: Member | 1,067 Posts
wish it were that easy but no

pretty funny , this shows the output of the exact same code, one called in EA, other is the script:
Inserted Code
04:12:46 Assistant USDCZK,M15: Alt calculation; Capital=48939
Risk=1.0%
Stoploss=27
Lots=30.66
04:12:46 Assistant USDCZK,M15: Lot size calculated using 1.00000000% and stop: 27.00000000 = 30.65963366
04:12:46 Assistant USDCZK,M15: Using lot size of 30.65963366
04:12:46 Assistant USDCZK,M15: Checking param: M5 price: 1.07280000 sl: 1.07550000
04:12:46 Assistant USDCZK,M15: Here we go...
04:12:47 Assistant USDCZK,M15: Here we go...
04:13:09 Assistant USDCZK,M15: OrderSend failed with error #134

04:15:07 PosizionSize AUDUSD,M15: loaded successfully
04:15:07 PosizionSize AUDUSD,M15 inputs: risk=1; stoploss=36; 
04:15:07 PosizionSize AUDUSD,M15: Alert: Capital=48939\nRisk=1.0% Stoploss=36 Lots=1.36
04:15:07 PosizionSize AUDUSD,M15: Capital=48939
Risk=1.0%
Stoploss=36
Lots=1.36

code again, copy-paste from working script:
Inserted Code
double CalcLotSize(int iSL) {
   
  int iPipMult[]={1,10,1,10,1,10,100};       
  double dRiskCapital=MaxRisk/100*AccountBalance(); 
  double tmpLot =  dRiskCapital/(iSL*MarketInfo(Symbol(),MODE_TICKVALUE)*iPipMult[Digits]); 
  
  Print("Alt calculation; Capital=",DoubleToStr(AccountBalance(),0),"\nRisk=",DoubleToStr(MaxRisk,1)+"%",
            "\nStoploss=",DoubleToStr(iSL,0),"\nLots=",DoubleToStr(tmpLot,2));
            
  return(tmpLot);
}
im gonna go have a few beers, maybe it'll come to me then ...
 
 
  • Post #24
  • Quote
  • Sep 1, 2011 2:32pm Sep 1, 2011 2:32pm
  •  RaptorUK
  • Joined Jan 2008 | Status: Member | 870 Posts
You are comparing the position size calculated for AUDUSD against USDCZK . . . they WILL be different . . .

Run your EA on AUDUSD or run the script on USDCZK
20 pips a day isn't too much to ask . . .
 
 
  • Post #25
  • Quote
  • Sep 1, 2011 2:35pm Sep 1, 2011 2:35pm
  •  RaptorUK
  • Joined Jan 2008 | Status: Member | 870 Posts
"OrderSend failed with error #134" is because you don't have enough margin for the trade you are trying to place . . do you check for the margin requirement before you try to place the trade ?
20 pips a day isn't too much to ask . . .
 
 
  • Post #26
  • Quote
  • Sep 1, 2011 3:15pm Sep 1, 2011 3:15pm
  •  pipmyride
  • | Joined Jan 2009 | Status: Member | 1,067 Posts
nah raptor the EA is running on that chart but not executing on that pair - its still AU.
And the margin issue is after the calculation is incorrect.

xaphod i almost wish u could remote login to my PC so u can see im not on drugs ..
 
 
  • Post #27
  • Quote
  • Sep 1, 2011 3:30pm Sep 1, 2011 3:30pm
  •  zznbrm
  • Joined Jul 2008 | Status: Member | 878 Posts
Quoting pipmyride
Disliked
nah raptor the EA is running on that chart but not executing on that pair - its still AU.
Ignored
Not if you are using a chart-specific function like "Symbol()" and a chart-specific property like "Digits" in your lot calculation.
 
 
  • Post #28
  • Quote
  • Sep 1, 2011 3:46pm Sep 1, 2011 3:46pm
  •  Xaphod
  • Joined Mar 2010 | Status: Member | 1,380 Posts
Quoting pipmyride
Disliked
nah raptor the EA is running on that chart but not executing on that pair - its still AU.
And the margin issue is after the calculation is incorrect.

xaphod i almost wish u could remote login to my PC so u can see im not on drugs ..
Ignored
It is difficult to debug code one has no access to and knows nothing about. Break down the calculation to it's smallest individual steps and print out the data from each step and verify it is correct.

As zznbrm points out symbol() refers to the symbol of the chart the EA is running on. So you are using the tick value for USDCZK. RaptorUK pointed out that red flag too. The code works as it should. Your input data is incorrect.

Note: The code was written for the major pairs. I have not verified its functionality with the more exotic pairs like USDCZK etc.
 
 
  • Post #29
  • Quote
  • Sep 1, 2011 4:11pm Sep 1, 2011 4:11pm
  •  pipmyride
  • | Joined Jan 2009 | Status: Member | 1,067 Posts
Quoting zznbrm
Disliked
Not if you are using a chart-specific function like "Symbol()" and a chart-specific property like "Digits" in your lot calculation.
Ignored
hahahah u legend !!
see thats wot a 2nd pair of eyes does !

Indeed, thats exactly it, thx very much all for helping !
 
 
  • Post #30
  • Quote
  • Mar 28, 2018 10:40am Mar 28, 2018 10:40am
  •  Murysco
  • Joined Mar 2015 | Status: Member | 501 Posts
Quoting pipmyride
Disliked
{quote} hahahah u legend !! see thats wot a 2nd pair of eyes does ! Indeed, thats exactly it, thx very much all for helping !
Ignored
Hello Pipmyride,

Can you please post your end result compiled code for the lotsize calculator?

I have been looking for a correct way to calculate my lotsizes. I saw all the codes shared here but unfortunately for me, I do not have any knowledge about doing anything with codes.

Thank you.
Patience, Persistence and discipline = Holy Grail.
 
 
  • Post #31
  • Quote
  • Last Post: Apr 22, 2020 10:42pm Apr 22, 2020 10:42pm
  •  seanlu21
  • | Joined Mar 2020 | Status: Member | 8 Posts
Quoting CodersGuru
Disliked
I usually use this function: double CalcLots(double Risk) { double tmpLot = 0, MinLot = 0, MaxLot = 0; MinLot = MarketInfo(Symbol(),MODE_MINLOT); MaxLot = MarketInfo(Symbol(),MODE_MAXLOT); tmpLot = NormalizeDouble(AccountBalance()*Risk/1000,2); if(tmpLot < MinLot) { Print("LotSize is Smaller than the broker allow minimum Lot!"); return(MinLot); } if(tmpLot > MaxLot) { Print ("LotSize is Greater than the broker allow minimum Lot!"); return(MaxLot); } return(tmpLot); }
Ignored
Hi, CodersGuru, may I know why you divide risked balance by "1000"? thank you.
 
 
  • Platform Tech
  • /
  • Code to work out lot size based on risk
  • Reply to Thread
    • 1 Page 2
    • 1 Page 2
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023