Trade Through Lines

Questions about MultiCharts and user contributed studies.
LA901
Posts: 18
Joined: 15 Dec 2014
Has thanked: 7 times
Been thanked: 4 times

Trade Through Lines

Postby LA901 » 19 Dec 2014

I'm new to MC and being an intraday FX trader I mostly have to make quick decisions as to entry and SL for a trade.

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;
But not sure how to nest this together for the 3 x TL's.

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;
Last edited by LA901 on 19 Dec 2014, edited 1 time in total.

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Trade Through Lines

Postby TJ » 19 Dec 2014

First, get rid of those Value1, Value2, etc.,

The "Value1" is used for illustration purpose in the manuals. In real life, please create a meaningful name for each variables. eg. TL1, TL2, or TL.highside, TL.lowside, etc.

Using meaningful variable names will speed up your coding effort, minimize errors, and saves you headaches in debugging.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Trade Through Lines

Postby Henry MultiСharts » 23 Dec 2014

Hello LA901,

Let me answer your questions in order:
  1. GetRTCashBalance - returns current account ballance privided by broker
  2. Probably these should help?
    TL GetFirst - Returns the trendline ID number of the oldest (the first to be drawn on the current chart) trendline of the specified origin; returns a value of -2 if the specified trendline ID number is invalid.
  3. TL GetNext - Returns an ID number of the first existing trendline drawn subsequent to a trendline with the specified ID number, with both trendlines of a specified origin; returns a value of -2 if the specified object ID number is invalid.
  4. TickSize = minmove/pricescale;
  5. There is a prebuilt signal !From Broker To Strategy MP Synchronizer! - Synchronizes the market position inside MultiCharts with the market position at broker by sending a dummy order in charting. You can use it or create your own code based on it. More info here, here and here.


Return to “MultiCharts”