Using the open_trades enumeration loop

Posted By: Pork

Using the open_trades enumeration loop - 10/02/13 15:15

Hi there,
I would like to examine the open trades' open price and set a variable. Whenever I add this code the SCRIPT CRAASHES
The code works until a trade is actually opened. And it doesn't work in a called function.
Where am I going wrong?

if(TradeIsOpen) {
for(open_trades){
if(TradePriceOpen>CheckPivotH) {
CheckPivotH=TradePriceOpen;
}
if(TradePriceOpen<CheckPivotL) {
CheckPivotL=TradePriceOpen;
}
}
}
Thanks
P
Posted By: DdlV

Re: Using the open_trades enumeration loop - 10/02/13 17:06

Hi Pork,

TradeIsOpen is a trade variable, so it's designed to be accessed within the open_trades loop (or a TMF) for each trade, not outside the loop. You may be confusing it with NumOpenTotal, which is a global variable...

HTH.
Posted By: Pork

Re: Using the open_trades enumeration loop - 10/02/13 18:00

Thanks Ddlv, but NumOpenTotal also crashes. BTW v1.16
I also thought maybe look back might be causing it.
But removing that also didn't help.
It will work until the trade is opened, or until the comparison is true.
I thought maybe it was the TradeOpenPrice inside the loop, but the manual says that should work.
The loop is inside the run() function, but I tried it in it's own function and that didn't work either.
How should one retrieve the open price for each trade if multiple trades are open?
I can probably create an array and move the price when a trade is opened.
I'm stuck again.
Thanks
P
Posted By: Pork

Re: Using the open_trades enumeration loop - 10/02/13 18:16

I read about the trade* pointer, will have to try that in a loop.
P
Posted By: jcl

Re: Using the open_trades enumeration loop - 10/02/13 18:24

It's difficult to give advices without knowing your script. DdlV told you why your code above won't work, but we can't know what other crashes are in the rest of your script.

There are many, many ways to write code that crashes. A crash is often obvious, but if you can't immediately see it in the script, comment out parts until you found the line that crashed. Then examine the line and its context - the most frequent crash reason is an empty pointer.
Posted By: DdlV

Re: Using the open_trades enumeration loop - 10/02/13 20:20

Hi Pork,

To answer your specific question, for(open_trades) loops through all Pending and Open trades. The Pending ones won't have a TradePriceOpen yet (use TradeIsOpen to bypass them), but the Open ones will.

Re. the crash, as jcl says it's pretty hard to debug a script without seeing all of it. If you don't want to post it follow the debug advice and narrow it down to what's causing the crash.

HTH.
Posted By: Pork

Re: Using the open_trades enumeration loop - 10/03/13 12:17

Thanks Guys, you did it again. Don't know what I would do without you.
I've attached a test file for you with appropriate commenting, sorry I didn't do it before.
But it comes down to these lines:

if(NumOpenTotal>0){
for(open_trades) {
...
...
}}

if(NumOpenTotal>0) {
//the following is the offending line that crashes the script. however it doesn't crash if open_trades is commented out. It also works the other way. If this line is commented out the open_trades line will not crash the script, but they don't appear to work together. shocked

if(TradeIsShort) {
...
...
}}

Thanks Again
P

Attached File
atest2.zip  (35 downloads)
Posted By: jcl

Re: Using the open_trades enumeration loop - 10/03/13 13:11

This is still the same problem - you're still using trade variables like "TradeIsShort" or "TradeIsLong" outside the context of a trade.

Trade variables belong to a trade, they make no sense without. So you can not use them outside a TMF or an open_trades loop. This even crashes your script because you're reading from a nonexistent pointer.

Maybe you were confusing the "TradeIsShort" or "TradeIsLong" variables with something else? What did you want to do with them?
Posted By: Pork

Re: Using the open_trades enumeration loop - 10/03/13 15:56

OK. Great. Thank You.
I think I have it.
1. I can't have an open_trades loop without checking to be sure there is an open trade.
2. I have to use a global variable NumOpenTotal to check for open trades not a "trade variable" (TradeisOpen).
3. Pending trades don't have a TradePriceOpen, so I can only check that after checking the "TradeisOpen" trade variable.
4. I can't check any trade variables without doing so in either an open_trades loop or a separate "trade management function" (TMF).
so the correct sequence is:

if(NumOpenTotal>0)
for(open_trades)
if(TradeIsOpen)
perform some task
if TradeisLong
perform task #2
if TradeIsShort
performtask #3
endfor
endif

or maybe
if(NumOpenTotal>0)
performtmf();
then place the code including the open_trades loop in the performtmf function

Thanks again
P
Posted By: jcl

Re: Using the open_trades enumeration loop - 10/04/13 10:13

Almost correct, only the if(NumOpenTotal) is unnecessary, as this is handled by the loop anyway. You can find several examples of open_trades loops in the manual.

Whether to handle trades in loop or in a TMF depends on if you need to run the task once per bar or at every price tick.
© 2024 lite-C Forums