You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Enjoy...but do not rob yourself of the confidence that comes with building your own code and thusly knowing exactly how it ticks. Using pre-existing code is a good way to improve your knowledge in a "pulling yourself up by your bootstraps" fashion, I wouldn't be here if I didn't have code to use as examples in my early days, but do be sure and force yourself to learn to fish. There's value to come from it in everything else you do.
@Phillip
What do you think of this?:
tradeVolume = AccountFreeMargin() * risk/100 / ( stopLossPoints * MarketInfo( Symbol(), MODE_TICKVALUE ) );
if(tradeVolume<MarketInfo(Symbol(),MODE_MINLOT)) tradeVolume=MarketInfo(Symbol(),MODE_MINLOT);
if(tradeVolume>MarketInfo(Symbol(),MODE_MAXLOT)) tradeVolume=MarketInfo(Symbol(),MODE_MAXLOT);
res=OrderSend(Symbol(),OP_BUY,tradeVolume,Ask,3,Ask - 0.2,Ask + 0.4,"",MAGICID,0,Red);
tradeVolume = AccountFreeMargin() * risk/100 / ( stopLossPoints * MarketInfo( Symbol(), MODE_TICKVALUE ) );
Without testing it personally to confirm the math, but assuming the math is correct, that looks correct provided you applying it solely to currency pairs for which the counter currency is also the account's denomination.
E.g. if your account is USD-based then you be looking to only apply that method of tradevolume computation to currency pairs with USD as counter - EURUSD, GBPUSD, etc.
In my codes this currency pair type is designated as "Type 2".
The calculation will be in error if you apply it to a symbol that has the account's denomination as the base (USDJPY when account is USD-based for example). And likewise will be in error if applied to any crosses.
Dig through that SymboType() call function in the "Analyze Currency Symbol" include file that was in my other post, the commented header section kinda explains the basis for the different symboltypes and why you need to compute equity at risk and volumesize (lotsize) differently depending on the symboltype.
But if your plan is to just trade those EURUSD and GBPUSD type pairs then your code looks good to go!
Hi again Phillip... you might recall in one of my earlier posts I had said "Sounds like the code had some problems with JPY pairs though. Has this been resolved?". Well, I think that problem may have cropped up again with me. I added your routine correctly (I'm pretty sure) into my EA and I set my MaxPercentEquityAtRisk= 1.0 (1 percent). The equity in my demo account stands at around US$2300, so I am willing to risk approximately $23 on any given trade. My EA uses equal size stop losses and profit targets - so if I'm risking $23 on a trade, I'm shooting for a $23 gain. Anyway, my EA generated 2 trades last night:
(1) BUY EUR/JPY, 9.8 pips stoploss and 9.8 pips profit target. Your routine calculated a lot size of 0.80 lots (way too big) and the trade resulted in a 10 pip profit of $96.91
(2) BUY NZDJPY, 16.3 pips stoploss and 16.3 pips profit target. Your routine calculated a lot size of 0.28 lots and the trade resulted in a loss of -$56.56.
Seems to me both of these trades should have given me approximately a $23 win or a $23 loss, given that I'm only risking 1% of my $2300 equity, no?
Thanks!
Shawn
This might help Phillp - here's the log of the print statements from your code just after it calculates the lot sizes for both of the orders I mentioned above:
05:14:56 EURJPY,H1: BUY - EURJPY Max EquityAtRisk = $21.99 and Max Lotsize = 0.8085
05:14:56 EURJPY,H1: BUY - EURJPY Current EquityAtRisk = $21.76 and Current Lotsize = 0.8
05:14:56 EURJPY,H1: BUY - EURJPY MarketInfo(MODE_STOPLEVEL) = 30.00000
05:14:56 LibOrderReliable EURJPY,H1: loaded successfully
05:14:56 LibOrderReliable EURJPY,H1: OrderSendReliable v3.1:
05:14:56 LibOrderReliable EURJPY,H1: OrderSendReliable v3.1: • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
05:14:56 LibOrderReliable EURJPY,H1: OrderSendReliable v3.1: Attempted BUY STOP 0.80000000 lots @114.78700000 sl:114.68900000 tp:114.88500000
09:02:36 NZDJPY,H1: BUY - NZDJPY Max EquityAtRisk = $23.29 and Max Lotsize = 0.2814
09:02:36 NZDJPY,H1: BUY - NZDJPY Current EquityAtRisk = $23.18 and Current Lotsize = 0.28
09:02:36 NZDJPY,H1: BUY - NZDJPY MarketInfo(MODE_STOPLEVEL) = 70.00000
09:02:36 LibOrderReliable NZDJPY,H1: loaded successfully
09:02:36 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1:
09:02:36 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1: • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
09:02:36 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1: Attempted BUY STOP 0.28000000 lots @64.29700000 sl:64.13400000 tp:64.46000000
09:02:37 LibOrderReliable NZDJPY,H1: open #155492665 buy stop 0.28 NZDJPY at 64.297 sl: 64.134 tp: 64.460 ok
09:02:37 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1: Ticket #155492665: Successful BUY STOP order placed, details follow.
09:02:37 LibOrderReliable NZDJPY,H1: #155492665 2010.11.04 13:02 buy stop 0.28 NZDJPY 64.297 64.134 64.460 64.197 0.00 0.00 0.00 NZDJPY73650016 73650016
09:02:37 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1: • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
09:02:37 LibOrderReliable NZDJPY,H1: OrderSendReliable v3.1:
... looks like it's calculating the Max EquityAtRisk figure fine... but those lotsizes result in profits/losses much much greater than my 1% of equity desired risk.
Thanks
Shawn
What is your broker?
On FXDD when I input those stop orders I get a lotsize of 0.15 for the EURJPY (@25.66 EaR)
2010.11.04 16:05:38 Assisted_Order_Script_2010.11.01 EURJPY,Daily: attempted OP_BUYSTOP 0.15000000 lots @114.82700000 sl:114.68900000 tp:114.88500000
2010.11.04 16:05:38 Assisted_Order_Script_2010.11.01 EURJPY,Daily: Current EquityAtRisk = $25.66 and Current Lotsize = 0.15 and Profit Target = $10.78 for a 0.4:1 Profit:Loss ratio
2010.11.04 16:05:38 Assisted_Order_Script_2010.11.01 EURJPY,Daily: Max allowed EquityAtRisk = $25.84 and Max computed Lotsize = 0.1511
2010.11.04 16:05:38 Assisted_Order_Script_2010.11.01 EURJPY,Daily inputs: Order_Type="BUY STOP"; OpenBidPrice=114.787; StopLossBidPrice=114.689; TakeProfitBidPrice=114.885; MaxPercentEquityAtRisk=0.5; MinLotOverRide=false;
On IBFX I get a lotsize of 0.16 with an EaR of $26.77 (larger starting equity in the account)
2010.11.04 16:09:34 Assisted_Order_Script_2010.11.01 EURJPY,H4: attempted OP_BUYSTOP 0.16000000 lots @114.82400000 sl:114.68900000 tp:114.88500000
2010.11.04 16:09:34 Assisted_Order_Script_2010.11.01 EURJPY,H4: Current EquityAtRisk = $26.77 and Current Lotsize = 0.16 and Profit Target = $12.10 for a 0.5:1 Profit:Loss ratio
2010.11.04 16:09:33 Assisted_Order_Script_2010.11.01 EURJPY,H4: Max allowed EquityAtRisk = $27.19 and Max computed Lotsize = 0.1625
2010.11.04 16:09:33 Assisted_Order_Script_2010.11.01 EURJPY,H4 inputs: Order_Type="BUY STOP"; OpenBidPrice=114.787; StopLossBidPrice=114.689; TakeProfitBidPrice=114.885; MaxPercentEquityAtRisk=0.5; MinLotOverRide=false;
With the NZDJPY I get a lotsize of 0.09 lots and an EaR of 24.53 on IBFX:
2010.11.04 16:11:59 Assisted_Order_Script_2010.11.01 NZDJPY,H4: attempted OP_BUYSTOP 0.09000000 lots @64.35400000 sl:64.13400000 tp:64.46000000
2010.11.04 16:11:59 Assisted_Order_Script_2010.11.01 NZDJPY,H4: Current EquityAtRisk = $24.53 and Current Lotsize = 0.09 and Profit Target = $11.82 for a 0.5:1 Profit:Loss ratio
2010.11.04 16:11:59 Assisted_Order_Script_2010.11.01 NZDJPY,H4: Max allowed EquityAtRisk = $27.19 and Max computed Lotsize = 0.0998
2010.11.04 16:11:59 Assisted_Order_Script_2010.11.01 NZDJPY,H4 inputs: Order_Type="BUY STOP"; OpenBidPrice=64.297; StopLossBidPrice=64.134; TakeProfitBidPrice=64.46; MaxPercentEquityAtRisk=0.5; MinLotOverRide=false;
There are nine brokers I test for compatibility with, these codes are known to work on Alpari(US), CitiFXPro, CMS, Forex.com (Gain Capital), FXCM, FXDD, IBFX, MIG Bank, and ODL.
These brokers differ enough that I've been satisfied up until now that the codes are broker agnostic based on them deftly handling the differences in broker market paramters spanning these brokers.
But we are assuming your implementation of the code is not broken, so let's eliminate that first. Try the attached script, drag-and-drop on EURJPY and input the parameters for your BUY STOP as I did above and let me know the results. (use only on a demo account obviously)
Thanks Phillip... I'm using an Alpari UK demo, account equity US$2272.85. OK, I added the script to a EURJPY hourly chart (just like my EA uses). It didn't do anything or place any pending order, so I checked the "Experts" tab for any error messages... here's what it said:
2010.11.04 20:39:15 Assisted_Order_Script_2010.11.01 EURJPY,H1: removed
2010.11.04 20:38:38 Assisted_Order_Script_2010.11.01 EURJPY,H1: loaded successfully
2010.11.04 20:37:54 Assisted_Order_Script_2010.11.01 EURJPY,H1: removed
2010.11.04 20:37:54 Assisted_Order_Script_2010.11.01 EURJPY,H1: uninit reason 0
2010.11.04 20:37:54 Assisted_Order_Script_2010.11.01 EURJPY,H1: zero divide
2010.11.04 20:37:53 Assisted_Order_Script_2010.11.01 EURJPY,H1: Max allowed EquityAtRisk = $22.73 and Max computed Lotsize = 0
2010.11.04 20:37:53 Assisted_Order_Script_2010.11.01 EURJPY,H1 inputs: Order_Type="BUY"; OpenBidPrice=115.827; StopLossBidPrice=115.689; TakeProfitBidPrice=115.885; MaxPercentEquityAtRisk=1; MinLotOverRide=false;
Thanks!
Shawn
Note: I tried the script again just now Phillip, same numbers but set MinLotOverride to true.. it launched an immediate MARKET order (even though my buy price was 115.827 and the market was only at 114.945. Lotsize = 0.01 and no stoploss or profit target order - they were both 0.0000
Thanks
Shawn
Just to confirm, your account denomination is USD, correct?
I'm downloading Alpari UK now to check it out.
The order opening as market instead of buy stop is entirely controlled by the orderreliable routine...very odd. Will have to investigate that as well then.
One thing that catches my attention is this notice:
"The leverage on a demo account is automatically set to the maximum leverage of 1:500."
Not that the leverage should be affecting a lotsize calc, it should only effect freemargin calcs IIRC.
Edit: nevermind, I see the platform actually doesn't default to this, it's 1:100 max leverage