My lot size is calculated according to the GBP I'm willing to risk on the trade i.e. it is directly related to my entry and SL.
I do not have time to plug in the details of entry, SL, risk, account balance into a position size calculator to calculate the required lot size. Hence in the past I have used 3rd party scripts (MT4 platform) to achieve this which work by dragging lines on a chart to my required entry, SL, TP, specifying the required risk (normally preset) and order type (market, limit, stop) in an on chart GUI and hitting submit and orders are placed.
This is my very crude attempt at trying to re-create this for MC (limit orders only at present). Bare with me, I have zero coding experience of any kind so this signal has been put together just from using this forum and the wiki as reference - learning by doing more than anything.
I have a few specific questions:
1. I prefer to use account balance over equity but couldn't find a reserved word for this?
2. As you can see the extent of my comparison statements is pretty much limited to If statements! At the moment you have to make sure that the entry TL is black and has ID 1 etc what would be better is if the TL can be any ID but the only requirement is the colour is black - i.e. the signal searches the TL's for colour and stores the ID and associated values. I figured out (well found the code somewhere else) how to do this for one using this:
Code: Select all
Value1 = TL_GetFirst(7) ;
if TL_GetColor(Value1)= Black then
Value2 = TL_GetValue(Value1, Date, Time )
Else
While Value2 = 0 begin
Value1 = TL_GetNext(Value1, 7) ;
if TL_GetColor(Value1)= Black then
Value2 = TL_GetValue(Value1, Date, Time );
end;
3. Any tips on generalising the pip value calculation for each symbol group rather than the long winded way I've done it for the specific symbols I'm trading?
4. If a trade is subsequently opened through the signal (i.e. a limit order is triggered) and I close the trade manually I encounter a problem whereby the attached limit/stop (for SL and TP) can't be deleted - the strategy 'thinks' there is still a trade in place (strategy open P/L is still running whereas Open P/L is closed within the strategy positions tab of the order and positions tracker window) and keeps attached the limit/stop order again. I need to insert some code to check the current actual open position and if zero close the SL/TP orders but I couldn't figure this out.
Finally, any general coding tips or identifying any potential issues would be helpful.
There is a short video of it in action on a short order this morning (ticks were slow this morning so it's a bit long winded!) and it also shows the deletion of SL/TP order mentioned above.
http://screencast.com/t/tysSGile
Many thanks
Code: Select all
[IntrabarOrderGeneration = true]
inputs:
Risk (0.5);
vars:
TickSize (MinMove / PriceScale),
PS (0),
Units (0),
SL (0),
pip_value (0),
pip_valueJ (0);
//Find manually drawn trendlines
if LastBarOnChart then begin
if TL_GetColor(1)= Black then //Entry TL
Value1 = TL_GetValue(1, Date, Time);
if TL_GetColor(2)= Red then //SL TL
Value2 = TL_GetValue(2, Date, Time);
if TL_GetColor(3)= White then //TP TL
Value3 = TL_GetValue(3, Date, Time);
end;
//Entry order and lot size / contract calculation in relation to risk, SL and account equity
pip_valueJ = convert_currency(datetime,JPY,USD,1000);
if getsymbolname = "EUR/USD" then
pip_value = convert_currency(datetime,USD,GBP,10);
if getsymbolname = "GBP/USD" then
pip_value = convert_currency(datetime,USD,GBP,10);
if getsymbolname = "AUD/USD" then
pip_value = convert_currency(datetime,USD,GBP,10);
if getsymbolname = "EUR/JPY" then
pip_value = convert_currency(datetime,USD,GBP,pip_valueJ);
if getsymbolname = "GBP/JPY" then
pip_value = convert_currency(datetime,USD,GBP,pip_valueJ);
if getsymbolname = "AUD/JPY" then
pip_value = convert_currency(datetime,USD,GBP,pip_valueJ);
if getsymbolname = "USD/JPY" then
pip_value = convert_currency(datetime,USD,GBP,pip_valueJ);
if getsymbolname = "EUR/GBP" then
pip_value = 10;
Print("PJ", pip_valueJ:5:2);
Print("PV", pip_value:5:2);
if OpenEntryContracts = 0 and Close < Value1 and Value2 > Value1 and Value3 < Value1 then begin //ensures TP and SL are the right side of entry TL
SL = Round((Value2 - Value1) / TickSize / 10,2);
PS = Round((Risk/100) * GetRTAccountEquity("xxxxx") / SL / pip_value,2);
Units = Round(PS * 100000,0); //Designed for Oanda account LMAX 100 contracts = 1 lot Oanda 100,000 contracts = 1 lot
Print("SL", SL:5:2);
Print("PS", PS:6:2);
Print("UN", Units:8:0);
Sell Short ("SE") Units contracts next bar At Value1 Limit;
end;
if OpenEntryContracts = 0 and Close > Value1 and Value2 < Value1 and Value3 > Value1 then begin //ensures TP and SL are the right side of entry TL
SL = Round((Value1 - Value2) / TickSize / 10,2);
PS = Round((Risk/100) * GetRTAccountEquity("xxxxx") / SL / pip_value,2);
Units = Round(PS * 100000,0); //Designed for Oanda account LMAX 100 contracts = 1 lot Oanda 100,000 contracts = 1 lot
Print("SL", SL:5:2);
Print("PS", PS:6:2);
Print("UN", Units:8:0);
Buy ("LE") Units contracts next bar At Value1 Limit;
end;
//Trade management
if MarketPosition = -1 then begin
BuyToCover ("Exit TP") next bar At Value3 Limit;
BuyToCover ("Exit SL") next bar At Value2 Stop;
end;
if MarketPosition = 1 then begin
Sell ("Exit TP") next bar at Value3 Limit;
Sell ("Exit SL") next bar at Value2 Stop;
end;