Using TradeVar to store an indicator value for later evaluation

Posted By: boatman

Using TradeVar to store an indicator value for later evaluation - 06/08/16 13:19

What is the correct usage of the TradeVar[] variables for storing an indicator value for later use? I am running into problems using these variables in a portfolio strategy that trades more than one algorithm. In my usage below, I get the expected results if I comment out one of the algo calls in the algo loop. But if I run both algos, the TradeVars for algo1 all return 0, which is not the values assigned to them in the algo1() function.

Wondering if this could be a bug or if I am missing something obvious?

Thanks

Code:
void algo1()
{
...
TradeVar[0] = x
TradeVar[1] = y
...
enterLong();
...
enterShort();
}

void algo2()
{
...
TradeVar[0] = x
TradeVar[1] = y
enterLong();
...
enterShort();

}

function run()
{
...
while(algo(loop("algo1", "algo2")))
	{
		if(Algo == "algo1") algo1();
		if(Algo == "algo2") algo2();

	}	
	
	if(is(EXITRUN))
	{
		for(all_trades)
		{
			printf("\n%s, Value1: %f, Value2: %f", TradeAlgo, TradeVar[0], TradeVar[1]);
		}
	}
}



And an eample of the output - as you can see, the TradeVars for the algo1 trades always return 0:
Code:
algo2, Value1: 37.563797, Value2:: 1.118150
algo2, Value1: 28.984416, Value2:: 1.118747
algo1, Value1: 0.000000, Value2:: 0.000000
algo1, Value1: 0.000000, Value2:: 0.000000
algo2, Value1: 7.145730, Value2:: 1.116183
algo2, Value1: 6.539160, Value2:: 1.116329
algo2, Value1: 6.571875, Value2:: 1.116340
algo1, Value1: 0.000000, Value2:: 0.000000
algo2, Value1: 7.412091, Value2:: 1.116200

Posted By: boatman

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 08:11

Thought I would give this a bump
Posted By: jcl

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 09:52

Maybe you wanted to use AlgoVar, not TradeVar. TradeVar is a variable of a trade, but there is no trade accessed in your code, unless the "..." was a trade enumeration loop.

For passing variables to a specific trade, call enterLong(TMF,Var1,Var2,.. etc.). The TMF is then also called with those parameters, and you can then store them in TradeVars. TradeVar without a trade won't crash, but has no meaning either. It is not passed to the next opened trade.
Posted By: boatman

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 11:34

There is a trade enumeration loop called when EXITRUN is true. This is the last few lines of code in my example. Maybe you missed it because you need to scroll down the code to see it?

It seems that the values assigned to the TradeVars prior to entering a trade with enterLong() or enterShort() are associated with each individual trade, because when I call the trade enumeration loop, I get mostly correct values printed for the TradeVars. However, this is only the case when I comment out one of the algos in the algo loop, which seems like it could be a bug? Either that, or my usage is incorrect.
Posted By: jcl

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 11:57

The usage in your trade enumeration loop is correct, only the rest is wrong.

Setting Tradevar in your code would normally crash, since you're writing into an undefined pointer. For preventing such crashes, Zorro keeps valid pointers for all internal variables. So you can in fact write into trade variables even with no trade, although it makes no sense. In your case you're probably getting the variables of the last valid trade pointer. That's why you see the written values at random in some of your trades.

You can manually set a trade pointer, it's ThisTrade, from an entered trade and then access all variables of this trade. That would work, but it's not the 'official' method.
Posted By: jcl

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 16:33

Yes, but please use a new thread for questions unrelated.

You need to request a V1 API fxtrader account from Oanda. The default account is not supported by the API. Details are in the manual.
Posted By: MatPed

Re: Using TradeVar to store an indicator value for later evaluation - 06/13/16 16:36

Sorry, posted in the wrong session not intentionally. Deleted
Posted By: boatman

Re: Using TradeVar to store an indicator value for later evaluation - 06/14/16 08:20

Thanks for that, jcl. I'll try implementing this using the official approach by using my variables as arguments to the TMF and then storing them in TradeVars. I didn't understand that such parameters had to be passed as arguments to the TMF, but now I can see why that is the case.

Cheers
© 2024 lite-C Forums