Plot questions - multisymbol and trade placement

Posted By: BobbyT

Plot questions - multisymbol and trade placement - 07/16/17 21:04

Hi all,

I have two questions.
1) Is it possible to place a mark on the plot for when a pending trade was placed? This mark would then be joined to when the trade was entered. Therefore 3 marks and 2 lines on the plot per trade. How would one go about this.

2) I am not getting the following block (from the manual) to operate on a multisymbol strategy.
Code:
char name[40]; // string of maximal 39 charactersstrcpy(name,Asset);
strcat(name,":");
strcat(name,Algo);
var equity = EquityShort+EquityLong;
if(equity != 0) plot(name,equity,NEW|AVG,BLUE);


At present, it only plots price, equity graph and equity line for the last symbol in the asset loop.
Could someone please clarify how plot collects data so I can work out where this needs to be. Where should this block be implemented and what is the required program flow to allow said implementation.

At present my code runs like so

Code:
run()
->Asset loop {
->LookForTrades() ->set up price/indicator series, check entry conditions, enter trades
... }
->plot code block as per above
run()
...


Cheers,
BobbyT

Edit: I would like to extend question 2 to multi-timeframe multi-symbol strategies. That is, I would like to plot a chart for each asset-timeframe combination
Posted By: jcl

Re: Plot questions - multisymbol and trade placement - 07/17/17 08:44

1) That's plotGraph().

2) I would remove the if(equity != 0). The order of plots is determined by the order of their first call, so make sure that their first call order is not wildly different dependent on equity.
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/17/17 16:12

Thanks JCL

1) From what I can gather from the manual, TradeBarOpen for pending trades is reset to the bar the trade was oped once it reaches TradeEntryLimit. Does this mean plotGraph() should be called immediately after (and before) TradeEntryLimit is hit. Or is there a better way?

2) I saw reference to the order of plot calls in the manual. But I was unable to make much sense of it. Taking the above question into consideration, would the best way to maintain the plot order call, would this be appropriate program structure.

Code:
run
{
while(asset(loop("AUD/USD","EUR/USD")))
while(algo(loop("M5","M15")))
if(algo == "M5")
{
Indicator/signal operations
...
ThisTrade = enterLong();
Generate plot compatible date with calls to year(),month() etc -> pendingDate
plot(signal, "signal", 0, RED);
plotGraph(pendingPrice, pendingDate, TradeEntryLimit, DOT, GREEN);
}
if(algo == "M15")
{
...
} 
}//end run()



Cheers,
BobbyT
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/18/17 03:19

Update:
I have the following (pseudo) code which is now plotting the pending price of each trade as is it taken:

Code:
Pd = 0.0;
if(entryConditions)
{
Entry = some_price;
ThisTrade = enterLong();
Pd = Entry;
}
plot(signal, "signal", 0, RED);
if(Pd != 0.0) plotGraph("pd price", 0, Pd, CROSS, DARKGREEN);



I originally wanted to use ThisTrade as the control statement however it 'always' evaluates to true after the first trade is placed. If I'm thinking right, this is because ThisTrade stores the last taken trade, regardless of what bar it is on. Is there an 'official' way to control for the plotGraph() call so it only plots a symbol when a trade is placed.

Also, I am no closer to joining the pending price and the opening price of individual trades. It obviously can be done as open/close prices are linked in the simulation charts but how...I have no idea. I don't see anything in the manual about it (other than LINE but I'm failing at implementing that) and I can't find any demo code demonstrating it frown

Cheers,
BobbyT

PS Lets leave the 'plot per asset:algo' issue for another time. I figure it is likely best to get a function set familiarised before getting too funky laugh

EDIT: english
Posted By: jcl

Re: Plot questions - multisymbol and trade placement - 07/18/17 06:45

For plotting only when the trade was opened, store the pending bar in a TradeVar. A line between the pending and fill price can be plotted with plotGraph and LINE.
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/18/17 15:17

Ok so I store the pending in one of my remaining TradeVar slots. Easy enough.

As TradeVar are only available from loop(all_trades/open_trades) and TMF this points to 2 possibilities
1) All plot functions are called within a TMF which just doesn't sound right and you could end up with inconsistent series (out-of-sync plot calls)
2) Loop over open_trades and create a series of the pending bar to be passed back to plotGraph

#2 seems to make more sense for how I understand zorro but seems (computationally) wasteful.

There is no fixed delay between when trades are placed and when they are opened and I think this will cause issues for LINE.
For example, say a pending trade (trade1) is placed @ bar1 but is not filled until bar20. Another trade (trade2) is placed at bar10 and is not filled until bar30. Won't LINE connect the pending bar of trade2 to the open bar of trade1 as it connects the 'last position to the current position'.

I have to admit that besides pointers/memory functions, graphing functions are by far my weakest toolset. I really struggle with them in R. MQL4, not so bad. But I also haven't one anything to interesting on that front smirk

Cheers,
BobbyT
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/18/17 22:53

So I arrived at a solution. It's super ugly (code wise).
I wrote a string of helper functions to get it working. It all of course is dependent on storing the pending bar in TradeVar as you previously indicated. The code is also dependant upon myID, a custom trade identifier as pending trades have no identifier until they are opened.

Here's the helpers and the plot block (which is now called at the end of every bar, directly, in run():
Code:
//check if a trade with status typ 
bool tradesThisBar(string typ, int idx) 
{
	int n = 0;
	
	for(all_trades){
		if(strstr(typ, "Op")) {	//looking at open trades
			if(TradeBarOpen == idx) n++;
		}
		if(strstr(typ, "Cl")) { //looking at closed trades
			if(TradeBarClose == idx) n++;
		}
		if(strstr(typ, "Pd")) { //looking at pendings only
			if(PdBar == idx) n++;
		}
	}
	if(n >= 1) return true;
	else return false;
}

//get the bar index of a trade with status typ and myID id
int getTradeBar(string typ, int id)
{
	int n = -1;
	
	for(all_trades){
		if(strstr(typ, "Op")) {	//looking at open trades
			if(id == myID) n = TradeBarOpen;
		}
		if(strstr(typ, "Cl")) { //looking at closed trades
			if(id == myID) n = TradeBarClose;
		}
		if(strstr(typ, "Pd")) { //looking at pendings only
			if(id == myID) n = PdBar;
		}
	}
	if(n >= 0) return n;
	else return -1;		
}

//get myID for a trade with activity typ at bar idx
var getTradeMyID(string typ, int idx)
{
	int n = -1;
	
	for(all_trades){
		if(strstr(typ, "Op")) {	//looking at open trades
			if(TradeBarOpen == idx) n = myID;
		}
		if(strstr(typ, "Cl")) { //looking at closed trades
			if(TradeBarClose == idx) n = myID;
		}
		if(strstr(typ, "Pd")) { //looking at pendings only
			if(PdBar == idx) n = myID;
		}
	}
	if(n >= 1) return n;
	else return -1;	
}

//get price typ for trade with myID id
var getTradePrice(string typ, int id)  //generalised version of  getPendingPrice
{
	var n = -1.0;
	
	for(all_trades){
		if(strstr(typ, "Op")) {	//looking at open trades
			if(id == myID) n = TradePriceOpen;
		}
		if(strstr(typ, "Cl")) { //looking at closed trades
			if(id == myID) n = TradePriceClose;
		}
		if(strstr(typ, "Pd")) { //looking at pendings only
			if(id == myID) n = TradeEntryLimit;
		}
	}
	if(n >= 0) return n;
	else return -1;	
}

function run()
{
...
//mark where pending trades were placed
	if(tradesThisBar("Pd", Bar)){
		int id = (int)getTradeMyID("Pd", Bar);	
		plotGraph("pdbar", 0, getTradePrice("Pd", id), CROSS, CYAN);
	}
	
	//join the pending price/bar to the open price/bar
	if(tradesThisBar("Op", Bar)){
		int id = (int)getTradeMyID("Op", Bar);
		if(id <= 0) return;					//leave the function if no trade id was found
		
		//sanity check >> are values being retrieved properly
		//printf("nTrade: %i, PendingBarPrice: %i/%.5f, OpenBarPrice: %i/%.5f", id, getTradeBar("Pd", id), getTradePrice("Pd", id), Bar, getTradePrice("Op", id));
		
		plotGraph("pdline", -(getTradeBar("Pd", id)-Bar), getTradePrice("Pd", id), LINE, CYAN); //pending point
		plotGraph("pdline", 0, getTradePrice("Op", id), LINE|END, CYAN); //open point
	}
}



This code feels like it's taking the long way around. Is there a better way to do this such as official lite-c functions or just half-way decent programming?
It does the job but seems very very crude smirk

Cheers,
BobbyT

PS: As this is 'working' now can I re-ask about asset:algo plots? I'd like to generate a separate plot for each asset:algo combination.

I can see how the above functions could be adapted to do such a thing (I think) but from what I gather, Zorro, will only generate 1 plot per simulation. In multiasset strategies, new plots can be attained by selecting the asset in the Asset box and pressing results. I played with this and it works as the manual states.

But this begs the question, how can one create a separate plot for each algo?
Posted By: jcl

Re: Plot questions - multisymbol and trade placement - 07/19/17 07:29

Yes, a shorter way to draw pending lines would be to use a simple TMF that just draws a line when TradeIsEntry becomes true.

You can plot any algo in a separate chart, but there is no scrollbox for select only one specific algo for drawing.
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/19/17 14:05

I thought I had tried putting the plot block in the TMF. It resulted in inconsistent series. But I'm also plotting an indicator on the chart. I'll have another look at it.

So Zorro can produce more than one chart? Can you point me to a reference for plotting individual algo charts?

Cheera,
BobbyT
Posted By: BobbyT

Re: Plot questions - multisymbol and trade placement - 07/20/17 18:22

Seems I bodged something when I played with plotting trades within the TMF the first time around. It works fine now. Go figure...

When I say I would like to plot each algo separately I mean to plot a full chart with price curves, signals and trades all specific to that algo (which in the case here, reflects different timeframes, thus the desire for algo specific plots).

With the exception of manually running each timeframe separately for each asset I cannot see how to do what I'm after.

Cheers,
BobbyT
© 2024 lite-C Forums