Originally Posted by AndrewAMD
Originally Posted by Qw3rty
I would like to reset the counter or Lot size to 1 for a specific condition when Equity has reached a new high by $X.
Your code is not measuring Equity. Use Zorro's Equity variable to check for new maximums. Ideally, this should be a global variable or static var.

If you declare var Count inside of run, Zorro will forget its value every time another bar arrives. So don't do that. This should also be a global variable or static var.

Originally Posted by Qw3rty
What I am trying to achieve is when a trades loses I increase the lot size BUT if the trade wins I might want to increase the lot size depending on if the Equity has reached a new high by X dollars.
All trades are losing from the moment they are entered because of commission and spread, so you need establish a better definition of "losing trade". You're not monitoring this.

Did you know that that your code is generating a bad Count and simply increasing/decreasing the position by that much? You really have no idea what your position is because your code is clearly not tracking it at all.

Originally Posted by Qw3rty
The above is sample code
It's not clear how much you're leaving out intentionally. But at the end of the day, it looks nothing like what you are describing.


AndrewAMD, you are correct and thank you for pointing this all out. I have declared a global variable of Count (I think I have done it correctly). A losing trade in this instance is one where the moving averages cross but there is no new equity high (keeping it simple without the rollover and spread etc.).

Below is my updated code which I think is very very close except looking at the "testtrades" log file it seems to be increasing the Count for every bar when the trade opens and not with each "losing" trade. Attached is the outcomes I would expect with lots increasing and then resetting on new highs.

How can I fix the Count on "losing" trades instead of every bar?

Thanks for all the help!

/////////////////////////Code/////////////////////

static int Count = 1; //Global Variable needed for incremental count outside of Run function

function run()
{
set(LOGFILE|PLOTNOW|BALANCE|OPENEND);
StartDate = 2016;
EndDate = 2017;
BarPeriod = 5;
LookBack = 5000;
LotAmount = 0.1; //1000 or 0.1;
MaxLong = 1;
MaxShort = 1;
vars Prices = series(priceClose(0));
vars SMA100 = series(SMA(Prices,100));
vars SMA30 = series(SMA(Prices,30));
LotAmount = 0.1;
Lots = 1;
vars total_equity = series(Equity);
vars max_equity = series(MaxVal(total_equity,LookBack));
//static int Count = Lots;

for(closed_trades)
{
if ((total_equity[0]< max_equity[0]))
{
Count++;
}
else
{
Count = 1; //Trying to reset counter to 1 for loop.
break_trades;
}

}

//FOR Loop might not be needed but yields different results
/* if ((total_equity[0]< max_equity[0]))
Count++;
else
Count = 1; //Trying to reset counter to 1 for loop.
*/


//Zorro Forum Code Test
/*if (crossOver(SMA30,SMA100)) {
for(closed_trades) {
Count++; // Or count this elsewhere to avoid the loop
}
} else if (crossUnder(SMA30,SMA100)) {
Count = 1;
}
*/
if(crossOver(SMA30,SMA100))
enterLong(Count);
if (total_equity[0] > max_equity[0])
exitLong();

if(crossUnder(SMA30,SMA100))
enterShort(Count);
if (total_equity[0] >= max_equity[0])
exitShort();


plot("SMA30",SMA30,0,RED);
plot("SMA100",SMA100,0,BLUE);
plot("Count",Count,NEW,BLUE);
plot("Lots",Lots,NEW,BLUE);
plot("Max Equity",max_equity,NEW,BLUE);
plot("Total Equity",total_equity,NEW,BLUE);


}

Attached Files
Count.JPG (29 downloads)