ThisTrade crashes test

Posted By: tomna1993

ThisTrade crashes test - 05/19/21 14:36

Hi,

I would like to assign trade pointer to ThisTrade when I enter a trade in tick function, but it crashes the test. I tried it two ways:
Code
function tick()
{
	if (LongSignal && (priceClose(0) >= LongSignalEntry) && !TradeIsOpen)
	{	
		Lots = ceil(RISK / ((LongSignalEntry - GridTakeProfit[0]) * POINT_CURRENCY_VALUE));
		TRADE* RunningLongTrade = enterLong(LongTradeManagement);
	}	


and
Code
function tick()
{
	if (LongSignal && (priceClose(0) >= LongSignalEntry) && !TradeIsOpen)
	{	
		Lots = ceil(RISK / ((LongSignalEntry - GridTakeProfit[0]) * POINT_CURRENCY_VALUE));
		ThisTrade = enterLong(LongTradeManagement);
	}	


I think now that I can't assign to ThisTrade the trade pointer in tick function, I did not try it in run function, but anyway it's no way I open trade in run (because of low timeframes I trade).
I use trade variables in run and in TMF functions and they work correctly but I read in manual I should set ThisTrade to be sure that trade variables have correct values. There is !TradeIsOpen which is checked before trade is open. What can I do in that situations? Create a global bool variable and use it till I open trade?
Posted By: AndrewAMD

Re: ThisTrade crashes test - 05/19/21 14:46

TradeIsOpen is invalid until you specify a trade.

Perhaps you should be checking one of the trade statistics instead.
https://zorro-project.com/manual/en/winloss.htm
Posted By: tomna1993

Re: ThisTrade crashes test - 05/20/21 13:08

Thanks for the idea Andrew, I'll try it!
After I opened a position can I use trade variables without assigning the enterLong function to ThisTrade?
Posted By: AndrewAMD

Re: ThisTrade crashes test - 05/20/21 13:20

No. ThisTrade must be assigned in order for trade variables to work.

Also, if a trade fails, enterLong can return 0 (a null pointer). So if ThisTrade is assigned a null pointer, accessing trade variables can crash your script. Always check that enterLong returns a valid pointer before using the pointer.
Posted By: tomna1993

Re: ThisTrade crashes test - 05/20/21 17:11

Ohh, that was the problem! Thank you for your help! Have a nice day!
© 2024 lite-C Forums