automatic lot-size calculating function? - page 3

 

Ah, I see the problem...you set the script to place a "BUY" order instead of changing the external variable to be BUY STOP...that is why it placed a market order.

From your post:

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;

Try BUY STOP.


I get 0.12 lots for EURJPY on a demo account funded with $2300 USD

2010.11.04 20:10:16 Assisted_Order_Script_2010.11.01 EURJPY,H1: open #155713274 buy stop 0.12 EURJPY at 115.840 sl: 115.689 tp: 115.885 ok
2010.11.04 20:10:15 Assisted_Order_Script_2010.11.01 EURJPY,H1: attempted OP_BUYSTOP 0.12000000 lots @115.84000000 sl:115.68900000 tp:115.88500000
2010.11.04 20:10:15 Assisted_Order_Script_2010.11.01 EURJPY,H1: Current EquityAtRisk = $22.44 and Current Lotsize = 0.12 and Profit Target = $6.69 for a 0.3:1 Profit:Loss ratio
2010.11.04 20:10:15 Assisted_Order_Script_2010.11.01 EURJPY,H1: Max allowed EquityAtRisk = $23.00 and Max computed Lotsize = 0.123
2010.11.04 20:10:15 Assisted_Order_Script_2010.11.01 EURJPY,H1 inputs: Order_Type="BUY STOP"; OpenBidPrice=115.827; StopLossBidPrice=115.689; TakeProfitBidPrice=115.885; MaxPercentEquityAtRisk=1; MinLotOverRide=false;
 

I also did the NZDJPY buystop example:

2010.11.04 20:14:16 Assisted_Order_Script_2010.11.01 NZDJPY,H1: open #155714279 buy stop 0.10 NZDJPY at 64.319 sl: 64.134 tp: 64.460 ok
2010.11.04 20:14:15 Assisted_Order_Script_2010.11.01 NZDJPY,H1: attempted OP_BUYSTOP 0.10000000 lots @64.31900000 sl:64.13400000 tp:64.46000000
2010.11.04 20:14:15 Assisted_Order_Script_2010.11.01 NZDJPY,H1: Current EquityAtRisk = $22.90 and Current Lotsize = 0.1 and Profit Target = $17.45 for a 0.8:1 Profit:Loss ratio
2010.11.04 20:14:15 Assisted_Order_Script_2010.11.01 NZDJPY,H1: Max allowed EquityAtRisk = $23.00 and Max computed Lotsize = 0.1004
2010.11.04 20:14:15 Assisted_Order_Script_2010.11.01 NZDJPY,H1 inputs: Order_Type="BUY STOP"; OpenBidPrice=64.297; StopLossBidPrice=64.134; TakeProfitBidPrice=64.46; MaxPercentEquityAtRisk=1; MinLotOverRide=false;

Now the attempted entry order price of 64.297 was too close to the current market price so the orderreliable routine moved the entry price to 64.319 for that reason, but I still get a reasonable 0.10 lotsize.

At this point I'm going to have to conclude there is something borked in your code in how you've implemented the include files and/or how you use the outputs. You'll have to upload the code for me to inspect in order for me to chase this down any further.

 

Yup, it's a USD denominated demo account, leverage 1:100. OK, I just ran it on EURJPY as a BUY STOP, looks like we got good numbers this time. Expert log says:


2010.11.04 21:37:18 Assisted_Order_Script_2010.11.01 EURJPY,H1: removed
2010.11.04 21:37:18 Assisted_Order_Script_2010.11.01 EURJPY,H1: uninit reason 0
2010.11.04 21:37:18 Assisted_Order_Script_2010.11.01 EURJPY,H1: open #155719095 buy stop 0.11 EURJPY at 115.843 sl: 115.689 tp: 115.885 ok
2010.11.04 21:37:16 Assisted_Order_Script_2010.11.01 EURJPY,H1: attempted OP_BUYSTOP 0.11000000 lots @115.84300000 sl:115.68900000 tp:115.88500000
2010.11.04 21:37:16 Assisted_Order_Script_2010.11.01 EURJPY,H1: Current EquityAtRisk = $20.95 and Current Lotsize = 0.11 and Profit Target = $5.71 for a 0.3:1 Profit:Loss ratio
2010.11.04 21:37:16 Assisted_Order_Script_2010.11.01 EURJPY,H1: Max allowed EquityAtRisk = $22.73 and Max computed Lotsize = 0.1193
2010.11.04 21:37:16 Assisted_Order_Script_2010.11.01 EURJPY,H1 inputs: Order_Type="BUY STOP"; OpenBidPrice=115.827; StopLossBidPrice=115.689; TakeProfitBidPrice=115.885; MaxPercentEquityAtRisk=1; MinLotOverRide=false;
2010.11.04 21:36:23 Assisted_Order_Script_2010.11.01 EURJPY,H1: loaded successfully
2010.11.04 21:34:16 Assisted_Order_Script_2010.11.01 EURJPY,H1: removed
2010.11.04 21:34:11 Assisted_Order_Script_2010.11.01 EURJPY,H1: loaded successfully



... and it placed the pending buystop order as 0.11 lots. perfect. Yep, i guess something's "borked up" in my code, as you say... lol. I was ever so ultra-careful putting your code in though... what could it be. OK, I'll do some debugging here and I'll let you know. Sorry to pester you Phillip!


Thanks

Shawn

 

Should I start afresh with your newer version that you just posted, Phillip?


Shawn

 

Makes sense to use the most recent, I am.

 

Hi Phillip, I was about to do surgery on my EA again to upgrade to your latest version that you posted a few days back, but after reading over that post again... it left me with a fair number of questions. I thought I should ask you first before I rip my EA up:


(1) You mention there's 2 new include files:


Analyze_Currency_Symbol_2010.10.29.mqh (34.54 KB)

Trade_Position_Management_2010.10.29.mqh (26.68 KB)


... what about the the old StopLoss_Manager_2010.05.24.mqh? That file is no longer needed at all in your new version?


(2) Where you say:


"Step 2: Add the following to the top of your EA so it has access to the call functions contained in the attached files

#include <OrderReliable_2010.10.12.mqh>

#include <Trade_Position_Management_2010.10.29.mqh>"


... is this a new version or OrderReliable? I was using LibOrderReliable.mqh. Has something changed with that? Also, don't we need to include the new Analyze_Currency_Symbol_2010.10.29.mqh file too?


(3) In this line of code you say to add when using your new version:


double CurrentLotSize=LotSize(CurrentEquityAtRisk,OpenPrice_ND,StopLossPrice_ND,CurrentOrderType);


... what is OpenPrice_ND and StopLossPrice_ND? Are these variables I need to declare, or are they embedded somewhere in your include files? Also in this new version of your LotSize function, the number of parameters and order of them has changed quite a bit, hasn't it? The call to LotSize in your previous version was:


CurrentLotSize=LotSize(CurrentEquityAtRisk,CurrentStopLossPrice,CurrentOrderType,CurrentSymbolType,CurrentCounterPairForCross);


(4) I noticed in my code using your previous version, I had assigned CurrentOrderType=OP_BUY; In reality, I'm doing a buy stop order. Would that make a difference?



Sorry to pester you, but I was just kinda bewildered a bit by these things.


Thanks

Shawn

 

(1) You mention there's 2 new include files:


Analyze_Currency_Symbol_2010.10.29.mqh (34.54 KB)

Trade_Position_Management_2010.10.29.mqh (26.68 KB)


... what about the the old StopLoss_Manager_2010.05.24.mqh? That file is no longer needed at all in your new version?


I am really not sure why I added that part to my original post, other than to ensure the end-user would have a complete set of file to draw upon.

I personally use an updated version of that stoploss manager but it has no bearing on this thread here, you should implement whatever stoploss procedures you prefer to use. The code I uploaded in this thread does not rely on a stoploss include file but it does rely on you to correctly setup your code to identify the stoploss price so that you can send it to the equityatrisk call function.

(2) Where you say:


"Step 2: Add the following to the top of your EA so it has access to the call functions contained in the attached files

#include <OrderReliable_2010.10.12.mqh>

#include <Trade_Position_Management_2010.10.29.mqh>"


... is this a new version or OrderReliable? I was using LibOrderReliable.mqh. Has something changed with that? Also, don't we need to include the new Analyze_Currency_Symbol_2010.10.29.mqh file too?

It is a new version, I posted the link to it at the bottom of my post on the bottom of the first page of this thread (I'd link to it but the search is BROKEN, argh!)

I modified the new version such that it transparently works with ecn-type brokers. Send your orders through ordersendreliable and it doesn't matter whether or not the broker accepts SL & TP on market-orders, the orderreliable script handles it for you.

(3) In this line of code you say to add when using your new version:


double CurrentLotSize=LotSize(CurrentEquityAtRisk,OpenPrice_ND,StopLossPrice_ND,CurrentOrderType);


... what is OpenPrice_ND and StopLossPrice_ND? Are these variables I need to declare, or are they embedded somewhere in your include files? Also in this new version of your LotSize function, the number of parameters and order of them has changed quite a bit, hasn't it? The call to LotSize in your previous version was:

Yes these are variables you need to declare at some point in your code and assign them the market prices for which you which to open the new position (OpenPrice_ND) and the stoploss price (StopLossPrice_ND).

Alternatively you could rename my variables with the ones you are already using. Consider those variables as placeholders, substitute your variables where appropriate.

(4) I noticed in my code using your previous version, I had assigned CurrentOrderType=OP_BUY; In reality, I'm doing a buy stop order. Would that make a difference?
Makes zero difference. All that is important is that you pass the ordertype correctly to the respective call functions. They are already coded to do the right thing depending the ordertype. If you don't send the correct ordertype to the call function then you will definitely get invalid results from the call functions themselves.

These are all good questions, if you don't ask then you can't expect yourself to learn ;)
 
1005phillip:

I am really not sure why I added that part to my original post, other than to ensure the end-user would have a complete set of file to draw upon.

I personally use an updated version of that stoploss manager but it has no bearing on this thread here, you should implement whatever stoploss procedures you prefer to use. The code I uploaded in this thread does not rely on a stoploss include file but it does rely on you to correctly setup your code to identify the stoploss price so that you can send it to the equityatrisk call function.



>>> OK! Got my own stoplosses so I don't need that - will remove it!



It is a new version, I posted the link to it at the bottom of my post on the bottom of the first page of this thread (I'd link to it but the search is BROKEN, argh!)

I modified the new version such that it transparently works with ecn-type brokers. Send your orders through ordersendreliable and it doesn't matter whether or not the broker accepts SL & TP on market-orders, the orderreliable script handles it for you.


>>> OK, I'm already using the OrderSendReliable2Step, so I'll just leave my code as is, using my existing LibOrderReliable.mqh. Still, don't we need to include the new Analyze_Currency_Symbol_2010.10.29.mqh file too?



Yes these are variables you need to declare at some point in your code and assign them the market prices for which you which to open the new position (OpenPrice_ND) and the stoploss price (StopLossPrice_ND).

Alternatively you could rename my variables with the ones you are already using. Consider those variables as placeholders, substitute your variables where appropriate.


>>> Gotcha, that's what I figured they were. I think you skipped over this comment I had made --> "Also in this new version of your LotSize function, the number of parameters and order of them has changed quite a bit, hasn't it?"




Makes zero difference. All that is important is that you pass the ordertype correctly to the respective call functions. They are already coded to do the right thing depending the ordertype. If you don't send the correct ordertype to the call function then you will definitely get invalid results from the call functions themselves.


>>> OK!



These are all good questions, if you don't ask then you can't expect yourself to learn ;)


>>> I'm trying! Thanks Phillip.


Shawn


 

>>> don't we need to include the new Analyze_Currency_Symbol_2010.10.29.mqh file too?

Not in your EA, but you need the file in your include directory because it is called from the Trade Position Management include file.

By having #include <Trade_Position_Management_2010.10.29.mqh> in your EA you have already linked to both Trade_Position_Management_2010.10.29.mqh and Analyze_Currency_Symbol_2010.10.29.mqh.

>>>Also in this new version of your LotSize function, the number of parameters and order of them has changed quite a bit, hasn't it

You no longer need to base the CurrentSymbolType nor the CurrentCounterPairForCross. It is handled internally now.

If you look inside the Trade Position file you will see the equityatrisk section and there are comments regarding the usage of the function. They might help, not sure how self-explanatory they are though.

 

Is it a bug that only happened to me?
I can not compile any file with MetaEditor mq4 if I put a line with # include.
Nor have I been able to compile mq4 files that have a line # include in the code.
Only enables lines
# Include <stderror.mqh>
# Include <stdlib.mqh>
# Include <WinUser32.mqh>

Reason: