trade information on the chart  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

trade information on the chart

Postby arnie » 19 Oct 2016

I'm debating here with how to retrieve some information from the time I get in a trade
Here's what I have

Image

The first 4 text lines are OK.
The problem resides with the time in trade, high since entry, low since entry and entry price.
Time in trade acts weird because all seems OK but sometimes I can see we're in a trade for the past 2 minutes and all of a sudden it goes back to 1 minute in the trade, which I don't understand why this happens.

I'm using 1 tick chart because if I use a minute chart, the high and low will always be the bar's high and low and not really the highest high and lowest low prices tested after my entry.
Also in here I'm having difficulties in retrieving these prices. Although it seems I'm always able to get the high, the low and entry are always zero.

I also tried to use the keywords to retrieve from the broker the entry time and entry price but I just can't get any information when using those keywords.

Thoughts would be much appreciated.

Here's the code

Code: Select all

[IntrabarOrderGeneration = true];

//Strategy, SA on

inputs:
ShiftBox (0),
TimeOffSet (5),
LabelSize (10),
LabelColor (lightgray),
LongColor (green),
ShortColor (red),
LabelFont ("Arial Bold");

vars:
intrabarpersist MPatB (0),
intrabarpersist prevMPatB (0),
intrabarpersist myEntrytimes (0),
intrabarpersist timeDiff (0),
intrabarpersist lastprice (close),
sessHigh (0),
sessHi (0),
sessLow (0),
sessLo (0),
priceEntry (0),
entryPrc (0),
txtStr ("Turn SA On"),
txtTime (-1),
txtContracts (-1),
txtPosition (-1),
myTime (0),
myTime2 (0),
myMinutes (0),
offSetTime (ELTimeToDateTime(TimeOffSet)),
dTick (MinMove/PriceScale),
boxShift (0),
newposition (false);


boxShift = ShiftBox * dTick;

if getappinfo(airealtimecalc) = 1 and getappinfo(aistrategyauto) = 1 then begin

MPatB = MarketPosition_at_Broker;

if MPatB <> prevMPatB and MPatB <> 0 then begin
myEntrytimes = currenttime_s;
SessHi = close;
SessLo = close;
priceEntry = close;
end;

if MPatB <> 0 then begin
if sessHi < High then sessHi = high;
if sessLo > Low then sessLo = low;
sessHigh = sessHi;
sessLow = sessLo;
priceentry = priceEntry;


//timeDiff = TimeToSeconds(Time_s) - myEntrytimeS ;
timeDiff = currentTime_s - myEntrytimeS ;

txtStr = ("account - " + text(GetAccount(1)) + newline +
newline + ifftext (MPatB < 0, " Short position", " Long position") + newline + text(MPatB) + " contract (s)" + newline +
newline + FormatTime("HH:mm:ss", el_timetodatetime_s(myEntrytimes)) + " - entry time" + NewLine +
newline + FormatTime("HH:mm:ss", el_timetodatetime_s(timediff)) + " - time in trade" + NewLine +
NumToStr(sessHigh, 2) + " - high since entry" + newline +
NumToStr(sessLow, 2) + " - low since entry" + newline +
NumToStr(priceentry , 2) + " - entry price");

end; //if LastBarOnChart_s then Print all text variables;

if MPatB = 0 then begin
txtStr = " NO POSITION ";
end;

prevMPatB = MPatB;
end;

RecalcLastBarAfter (1);

if Barnumber = 1 then begin
txtTime = text_new_s(JulianToDate(aiRightDispDateTime), GetAppInfo(aiRightDispDateTime), GetAppInfo(aiHighestDispValue) - BoxShift,"");
text_setfontname(txtTime, LabelFont);
text_setcolor(txtTime, LabelColor);
text_setsize(txtTime, LabelSize);
Text_SetStyle(txtTime, 1, 0);
//text_setborder(txtTime , true);
//text_setbgcolor(txtTime , black);
text_lock(txtTime, true);
end
else begin
text_SetString(txtTime, txtStr);
text_SetColor (txtTime, iff(MPatB = 0, LabelColor, iff(MPatB < 0,ShortColor ,LongColor)));

if BarStatus(1) = 2 then begin
Text_SetLocation_s(txtTime, JulianToDate(GetAppInfo(aiRightDispDateTime)),DateTime2ELTime_s(GetAppInfo(aiRightDispDateTime) -
offSetTime), GetAppInfo(aiHighestDispValue) - boxShift);
end;
end;

Attachments
broker_stats_simple.pla
(11.19 KiB) Downloaded 283 times
account.png
account.png (41.16 KiB) Viewed 852 times

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: trade information on the chart

Postby ABC » 19 Oct 2016

arnie,

you are using tracking variables that are not intrabar persist, but reset them intrabar i.e. as soon as there is a market position change. This will likely result in the variables not saving the new values, but instead keeping 0 (which doesn't matter for SessHi, but for the other two it will).

Regards,

ABC

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: trade information on the chart  [SOLVED]

Postby bensat » 19 Oct 2016

I thought testing this on DEMO-accounts is pretty useless while the retrieving any 'fake'-data from the 'broker' is not possible.

Kind Regards.

Ben

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: trade information on the chart

Postby arnie » 19 Oct 2016

I thought testing this on DEMO-accounts is pretty useless while the retrieving any 'fake'-data from the 'broker' is not possible.

Kind Regards.

Ben
Yeah, my prior attempt was not successful but the problem was with the broker selection. I had the wrong one on the strategy properties.

At least with CQG's demo accounts it's working fine.

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: trade information on the chart

Postby arnie » 24 Oct 2016

Here is the final result for my manual order tracker.
This will not give any historical data of your orders, only a real time look at your last order.
This will not be able to track multiple orders (ie. add to your position). Every time you add to your position, the study reset its values.
This study is intended for who trades all in/all out.

There're 4 different views.

The first indicates the signal is switched OFF so one needs click the SA button. Remember that if you have several account you need to go to the strategy properties an select the correct broker plugin at the bottom of the window

Image

The second indicates that there's no opened position (if there's no position there's no need to have the n/a indication)

Image

When a long position is opened, you'll have green text

Image

When a short position is opened, you'll have red text

Image

For this to work properly one need to plot it on a tick chart.
If plotted on a minute chart the MAE and MFE will retrieve wrong values since the high and low reading will be from the last bar of the chart.
Also, from time to time the entry price might be off by 1 tick which will put the P&L, MAE and MFE also off by 1 tick.
Since we can't retrieve from the broker the entry price in a way that we can use in this study, we retrieve the last price at the time of entry.

If you can improve its calculations please feel free to change it and of course post it here so we all can take advantage of it.

Code: Select all

// 24/10/2016
// http://www.multicharts.com/discussion/viewtopic.php?f=1&t=50194
// Plot on a chart our manual trades stats, namely the Time in Trade, MAE and MFE

[IntrabarOrderGeneration = true];

//Strategy, SA on

inputs:
ShiftBox (0),
TimeOffSet (5),
LabelSize (10),
LabelColor (lightgray),
LongColor (green),
ShortColor (red),
LabelFont ("Arial Bold");

vars:
intrabarpersist MPatB (0),
intrabarpersist prevMPatB (0),
intrabarpersist myEntrytimes (0),
intrabarpersist timeDiff (0),
intrabarpersist lastprice (close),
intrabarpersist sessHi (0),
intrabarpersist sessLo (0),
intrabarpersist priceEntry (0),
sessHigh (0),
sessLow (0),
txtStr ("Turn SA On "),
txtTime (-1),
txtContracts (-1),
txtPosition (-1),
myTime (0),
offSetTime (ELTimeToDateTime(TimeOffSet)),
dTick (MinMove/PriceScale),
boxShift (0),
PnL (0),
MAE (0),
MFE (0);

if getappinfo(airealtimecalc) = 1 and getappinfo(aistrategyauto) = 1 then begin

MPatB = MarketPosition_at_Broker;
lastprice = close;
boxShift = ShiftBox * dTick;

if MPatB <> prevMPatB and MPatB <> 0 then begin
//myEntrytimes = currenttime_s;
myEntrytimes = TimeToSeconds(Time_s);
SessHi = close;
SessLo = close;
priceEntry = close;
end;

if MPatB <> 0 then begin
if sessHi < high then
sessHi = high;
if sessLo > low then
sessLo = low;
sessHigh = sessHi;
sessLow = sessLo;
priceEntry = priceEntry;
//timeDiff = currentTime_s - myEntrytimeS ;
timeDiff = TimeToSeconds(Time_s) - myEntrytimeS ;
myTime = SecondsToTime_s(timeDiff);

if MPatB > 0 then begin
PnL = (lastprice - priceEntry) / dtick;
MAE = (priceEntry - sessLow) / dtick;
MFE = (sessHigh - priceEntry) / dtick;
end
else begin
PnL = (priceEntry - lastprice) / dtick;
MAE = (sessHigh - priceEntry) / dtick;
MFE = (priceEntry - sessLow) / dtick;
end;

txtStr = (" Account " + newline +
text(" " + GetAccount(1)) + " " + newline +

newline + ifftext (MPatB < 0, " Short Position ", " Long Position ") + newline +
text(MPatB) + " ctr (s) " + newline +

newline + " Time in Trade " + newline +
FormatTime(" HH:mm:ss ", el_timetodatetime_s(mytime)) + newline +

newline + " P&L " + newline +
" " + NumToStr(pnl, 0) + " tick (s) " + newline +

newline + " MAE " + newline +
" " + NumToStr(MAE, 0) + " tick (s) " + newline +

newline + " MFE " + newline +
" " + NumToStr(MFE, 0) + " tick (s) ");
end;

if MPatB = 0 then begin
//txtStr = " NO POSITION ";

txtStr = (" Account " + newline +
text(" " + GetAccount(1)) + newline +

newline + " NO POSITION " + newline +
text(" n/a") + newline +

newline + " Time in Trade " + newline +
text(" n/a") + newline +

newline + " P&L " + newline +
text(" n/a") + newline +

newline + " MAE " + newline +
text(" n/a") + newline +

newline + " MFE " + newline +
text(" n/a"));
end;

prevMPatB = MPatB;
end;

if Barnumber = 1 then begin
txtTime = text_new_s(JulianToDate(aiRightDispDateTime), GetAppInfo(aiRightDispDateTime), GetAppInfo(aiHighestDispValue) - BoxShift,"");
text_setfontname(txtTime, LabelFont);
text_setcolor(txtTime, LabelColor);
text_setsize(txtTime, LabelSize);
Text_SetStyle(txtTime, 1, 0);
text_lock(txtTime, true);
end
else begin
text_SetString(txtTime, txtStr);
text_SetColor (txtTime, iff(MPatB = 0, LabelColor, iff(MPatB < 0,ShortColor ,LongColor)));

if BarStatus(1) = 2 then begin
Text_SetLocation_s(txtTime, JulianToDate(GetAppInfo(aiRightDispDateTime)),DateTime2ELTime_s(GetAppInfo(aiRightDispDateTime) -
offSetTime), GetAppInfo(aiHighestDispValue) - boxShift);
end;
end;

RecalcLastBarAfter (1);

Attachments
trade_win04.png
trade_win04.png (12.2 KiB) Viewed 810 times
trade_win03.png
trade_win03.png (12.35 KiB) Viewed 809 times
trade_win02.png
trade_win02.png (12.66 KiB) Viewed 809 times
trade_win01.png
trade_win01.png (8.74 KiB) Viewed 809 times


Return to “MultiCharts”