Increasing Lots Size

Posted By: Qw3rty

Increasing Lots Size - 07/20/20 11:01

Is there a way to increase the Lots size continuously based on a given condition?

if(condition)
Lots = Lots+1; //Lots++
else
Lots = 1;

The above doesn't continuously add to Lots.

I've looked at using TradeLots but no luck. Also using Lots in a vars series didn't work either.

Any ideas?
Posted By: danatrader

Re: Increasing Lots Size - 07/20/20 11:57

Use your Lots in a static var and add +1 per closed trade.
Posted By: Qw3rty

Re: Increasing Lots Size - 07/21/20 11:05

I have tried with a static var and added +1 but the problem is it only adds 1 lot when the condition happens. If the condition happens again it doesn't add another lot so it always sits at a max of 2.

How do I use the close trades without a loop?

I am trying the following:

for(closed_trades) //for(all_trades)
{
if (Equity[0] < max_equity[0])
Count++;
else
Count=1; //doesn't reset the entire counter to 1 and the countr carries on from the last Count++
}

I am using Count as Lots now for enterLong(Count) and enterShort(Count);

Now it increments but I can't get it to reset to 1 and start all over.

See the attached output of trade log lots, Count lots (loop) and Max Lots (Static Var).

Surely there must be an easier way?

Thanks for teh help!

Attached picture Lots.JPG
Attached picture Count.JPG
Attached picture MaxLots.JPG
Posted By: danatrader

Re: Increasing Lots Size - 07/21/20 14:15

I am sure there is, maybe share your complete code, is always easier.
I am not a developer myself, so maybe others can help, but they won't if it is too much difficulty.

Everyone has limited time.
Posted By: Qw3rty

Re: Increasing Lots Size - 07/22/20 09:37

Really appreciate the help on this. Also not a developer so I'm bangiing my head every step of the way and reading the maunal as much as posible.

Sample code. How do I reset the Count in the loop to 1.

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

for(closed_trades)
{
if (crossOver(SMA30,SMA100))
Count++;
else if (crossUnder(SMA30,SMA100))
Count = 1; //Trying to reset counter to 1 for loop but no luck

}

if(crossOver(SMA30,SMA100))
{
enterLong(Count);
}

if(crossUnder(SMA30,SMA100))
{
enterShort(Count);
}

plot("SMA",SMA30,0,RED);
plot("SMA",SMA100,0,BLUE);
plot("Count",Count,NEW,BLUE);
plot("Lots",Lots,NEW,BLUE);

}

I'll take other creative ideas as well.
Posted By: AndrewAMD

Re: Increasing Lots Size - 07/22/20 13:36

Originally Posted by danatrader
I am not a developer myself
Originally Posted by Qw3rty
Also not a developer so I'm bangiing my head every step of the way and reading the maunal as much as posible.
You just might be a developer if you are:
* writing Zorro code and
* banging your head on the keyboard.

Welcome to the club! grin

If you can write a paragraph explaining how and why you want to increase the lot size, I think I can give better advice. Your ultimate goal is not exactly clear.
Posted By: jbhunter

Re: Increasing Lots Size - 07/22/20 22:31

This may be good code to review if you are looking for more samples. It increases the position size as a z score hits predefined levels.

https://robotwealth.com/pairs-trading-zorro/
Posted By: Qw3rty

Re: Increasing Lots Size - 07/24/20 07:33

Originally Posted by jbhunter
This may be good code to review if you are looking for more samples. It increases the position size as a z score hits predefined levels.

https://robotwealth.com/pairs-trading-zorro/


Looks interesting, thank you. I couldn't get it to fully work downloading the assets (that's another story). But this looks more like a grid trading system or trading at specific zones.
Posted By: Qw3rty

Re: Increasing Lots Size - 07/24/20 07:54

Originally Posted by AndrewAMD
Originally Posted by danatrader
I am not a developer myself
Originally Posted by Qw3rty
Also not a developer so I'm bangiing my head every step of the way and reading the maunal as much as posible.
You just might be a developer if you are:
* writing Zorro code and
* banging your head on the keyboard.

Welcome to the club! grin

If you can write a paragraph explaining how and why you want to increase the lot size, I think I can give better advice. Your ultimate goal is not exactly clear.


The life of a developer then lol.

The above is sample code but the outcome is still the same. 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.

When trading using crossovers (as an example) one theory is to double down or use the martingale method on your losses to turn them into a profit. This works in theory but the moving average is exactly that, an average of price which isn’t sufficient and there can be unlimited losses and not an unlimited amount of money. With the moving average crossover there is a high and a low before a new cross happens (peak and trough of the trade). 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. If a new Equity high is reached then the Lot value (count) moves to 1. Otherwise increase the count by X.

If open trade = new equity high then close trade.
If open trade <> new equity high then increase lot (regardless of winning or losing trade).

I really appreciate all the help. My head really hurts trying to get this right.

Let me know if I need to explain further.
Posted By: Morris

Re: Increasing Lots Size - 07/24/20 11:51

Qw3rty,

I must concede I don't fully understand what you are trying to do. But when I look at your for(closed_trades), and you want Count to be reset to 1 and stay there, you might want to add a break statement:

Code
for(closed_trades) {
    if (crossOver(SMA30,SMA100)) {
        Count++;
    } else if (crossUnder(SMA30,SMA100)) {
        Count = 1;
        break_trades;	// to prevent the loop from keeping on counting up
    }
}


But then, wouldn't it be more straightforward to re-shuffle like this, since the loop does not affect the result of the if conditions:

Code
if (crossOver(SMA30,SMA100)) {
    for(closed_trades) {
        Count++;   // Or count this elsewhere to avoid the loop
    }
} else if (crossUnder(SMA30,SMA100)) {
    Count = 1;
}


Again, with the caveat that your intention isn't entirely clear to me...
Posted By: AndrewAMD

Re: Increasing Lots Size - 07/24/20 13:38

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.
Posted By: Qw3rty

Re: Increasing Lots Size - 07/25/20 05:00

Originally Posted by Morris
Qw3rty,

I must concede I don't fully understand what you are trying to do. But when I look at your for(closed_trades), and you want Count to be reset to 1 and stay there, you might want to add a break statement:

Code
for(closed_trades) {
    if (crossOver(SMA30,SMA100)) {
        Count++;
    } else if (crossUnder(SMA30,SMA100)) {
        Count = 1;
        break_trades;	// to prevent the loop from keeping on counting up
    }
}


But then, wouldn't it be more straightforward to re-shuffle like this, since the loop does not affect the result of the if conditions:

Code
if (crossOver(SMA30,SMA100)) {
    for(closed_trades) {
        Count++;   // Or count this elsewhere to avoid the loop
    }
} else if (crossUnder(SMA30,SMA100)) {
    Count = 1;
}


Again, with the caveat that your intention isn't entirely clear to me...



Thanks Morris, this is interesting. I may be able to get rid of the For Loop now but it still doesn't give the result I want i.e. resetting the counter to 1 on trades that result in a higher high of equity. See next post for code update.
Posted By: Qw3rty

Re: Increasing Lots Size - 07/25/20 05:03

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 picture Count.JPG
Posted By: Qw3rty

Re: Increasing Lots Size - 08/01/20 09:26

Why is this code counting every bar in the closed_trades loop, see attached? I just want it to count for each condition and reset the counter to 1 when the condition has been met.

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

}

Attached picture Count.JPG
Posted By: AndrewAMD

Re: Increasing Lots Size - 08/01/20 14:22

I'm not sure. Below are some troubleshooting and debugging tips:
https://manual.zorro-project.com/trouble.htm
https://zorro-project.com/manual/en/watch.htm
https://zorro-project.com/manual/en/chart.htm

Anyways, definitely take a peek at this Zorro manual page regarding Martingale systems. It's no longer published in the Table of Contents, and I really don't know why. It's very good:
https://zorro-project.com/manual/en/tutorial_scam.htm
Posted By: Zheka

Re: Increasing Lots Size - 08/01/20 15:13

Zorro's example on "Equity curve trading" that you are following is wrong: your "total_equity" and "max_equity" are series, i.e. are updated at each BAR (have no connection to each Trade).
(When a system is out of the market - guess what will happen?)

Because of this, in your loop you make comparisons of exactly the same values and increment Count "closed_trades" number of times.

To implement this correctly, you have several options:

- use static series and update it manually at trade exit: ( http://manual.zorro-trader.com/shift). Then you do not need a for(closed_trades) loop.
- at trade exit (in a tmf) save Equity to a TradeVar and separately keep track of the max Equity(no need for a series). Then you access TradeVars and compare these in for(trades) loop and increment Count as needed.
Posted By: Qw3rty

Re: Increasing Lots Size - 08/15/20 06:11

Originally Posted by Zheka
Zorro's example on "Equity curve trading" that you are following is wrong: your "total_equity" and "max_equity" are series, i.e. are updated at each BAR (have no connection to each Trade).
(When a system is out of the market - guess what will happen?)

Because of this, in your loop you make comparisons of exactly the same values and increment Count "closed_trades" number of times.

To implement this correctly, you have several options:

- use static series and update it manually at trade exit: ( http://manual.zorro-trader.com/shift). Then you do not need a for(closed_trades) loop.
- at trade exit (in a tmf) save Equity to a TradeVar and separately keep track of the max Equity(no need for a series). Then you access TradeVars and compare these in for(trades) loop and increment Count as needed.




I haven't had much time lately. I have tried the above but haven't had any success. It could be the way I have coded everything but more investigation is needed. Your first option (static series) looks to be the most straight forward so I'm focusing on that for now.

Thanks for the ideas.
© 2024 lite-C Forums