positionprofit(1) with buy and sellshort logic problems

Questions about MultiCharts and user contributed studies.
imoneyfish
Posts: 38
Joined: 26 Oct 2011
Has thanked: 6 times
Been thanked: 2 times

positionprofit(1) with buy and sellshort logic problems

Postby imoneyfish » 02 Nov 2011

I have twisted and tested the code below for two days buy no luck. Anyone help me to get my idea works please?

My idea is simple, Once the last trade closed as a winner, this trade will double it's default contracts; once the last trade closed as a lost, this trade will only open half of the defualt contracts.

For example, the first trade the signal gernarate is a buy signal to buy EUR/USD 1 contract(default input value) at 1.3700, then the sellshort signal comes out at 1.3800, then the signal close the long trade as a winner, then should sell 2 contracts at 1.3800 rather than only 1 contracts.

I think the logic problem here is the sellshort is to close the position first, then open a reverse position.... Anyway, I don't have a clue at all, please help. The code is here:

Code: Select all

Inputs:
Length( 20 ),
NumDevsUp( 2 ),
NumDevsDn( -2 ),
stoploss( 300 ),
profittarget( 500 );

variables:
var0( 0 ),
var1( 0 ),
var2( 0 ),
var3( 0 ),
intrabarpersist tradesize( 40000 );

var0 = AverageFC( close, Length ) ;
var1 = StandardDev( close, Length, 1 ) ;
var2 = var0 + NumDevsDn * var1 ;
var3 = var0 + NumDevsUp * var1 ;

condition1 = CurrentBar > 1 and close crosses over var2 ;

condition2 = CurrentBar > 1 and close crosses under var3 ;

if positionprofit(1)>0 then tradesize = tradesize*2;
if positionprofit(1)<0 then tradesize = tradesize/2;

if tradesize > 2141999999 then begin tradesize = 2141999999; end;

if marketposition = 0 then begin

if tradesize > 0 then begin
if condition1 then begin
buy ( "Buyme" ) tradesize contracts next bar at market ;
Print("tradesize= ",tradesize:6:4);
end;
if condition2 then begin
sellshort ("Sellme")tradesize contracts next bar at market ;
Print("tradesize= ",tradesize:6:4);
end;
end;
end else begin
setstoploss(stoploss);
setprofittarget(profittarget);
end;
The output is ridiculous:

tradesize= 40000.0000
tradesize= 156.2500
tradesize= 40000.0000
tradesize= 156.2500
tradesize= 40000.0000
tradesize= 156.2500
tradesize= 40000.0000
tradesize= 2141999999.0000
tradesize= 535499999.7500
tradesize= 1021.3852
tradesize= 63.8366
tradesize= 4183593.7480
tradesize= 4183593.7480
tradesize= 2141999999.0000
tradesize= 2141999999.0000
tradesize= 32684.3262
tradesize= 0.0039
tradesize= 0.0010
tradesize= 0.0000
tradesize= 0.0000
tradesize= 40000.0000
tradesize= 2141999999.0000
tradesize= 535499999.7500
tradesize= 1021.3852
tradesize= 63.8366
tradesize= 4183593.7480
tradesize= 4183593.7480
tradesize= 2141999999.0000
tradesize= 2141999999.0000
tradesize= 32684.3262
tradesize= 0.0039
tradesize= 0.0010
tradesize= 0.0000
tradesize= 0.0000

As I set default tradesize to 40000, the right logic should be 40000, 80000, 160000, 320000 and so on; or 40000,20000,10000,5000 and so on.

however, I think this code is multiple or divided by 2 infinitely until the tradesize because invalid so no only one or two trades will be generated the whole backtesting periods and become useless.

Please help me here becuase I am scratch my brain out.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: positionprofit(1) with buy and sellshort logic problems

Postby furytrader » 03 Nov 2011

I think the problem is that the following bit of code:

Code: Select all

if positionprofit(1)>0 then tradesize = tradesize*2;
if positionprofit(1)<0 then tradesize = tradesize/2;
... is being calculated on EVERY bar, irrespective of whether you just closed out a trade ... so if the last trade was a loser, it keeps reducing the trade size EVERY BAR until a new trade is fired.

The way around is this is make this calculation only ONCE after a trade is closed. The way you could this is as follows:

1) Create a dummy variable (called it vTradeSoFar).

2) Wrap the following code around your original calculation:

Code: Select all

If TotalTrades > vTradesSoFar Then Begin
if positionprofit(1)>0 then tradesize = tradesize*2;
if positionprofit(1)<0 then tradesize = tradesize/2;
vTradesSoFar = TotalTrades;
End;
The way this works is that the program keeps its own running tally of how many trades have been completed using the vTradesSoFar variable. It then compares this tally to MultiChart's own tally of completed trades (which is the accurate one). When MultiCharts shows more completed trades than vTradesSoFar, it means that a new trade has been open AND closed and we can then adjust our trade size based on what happened. Because we then set the value of vTradesSoFar to TotalTrades once we're done with the calculation, we ensure that this code block will only be run once for every trade completed.

The one place where this code can fall down is there are multiple trades entered on the SAME BAR. In that case, you would want to use a for-next loop to cycle through each POSITIONPROFIT value to adjust the trade size. If you need help doing that (it's a little more complicated), just let me know.

imoneyfish
Posts: 38
Joined: 26 Oct 2011
Has thanked: 6 times
Been thanked: 2 times

Re: positionprofit(1) with buy and sellshort logic problems

Postby imoneyfish » 03 Nov 2011

Thanks a lot furytrader. This code finally works.

However, the funny thing here is if the trade size more than the max shares/contracts per postion setting in the signal properties,it will stop calculating; and it looks like positionprofit(1) refer the previous 2 trades ago or something. Let me show you the chart here.
Image
The 3th and 4th trade should be 15000, and that's means the logic still got some problems.


And also, when in optimize, it will get floating-point exception error message.
Thanks anyway. Maybe I will look into more about the for-next loop you mentioned.

Cheers,
Attachments
1.jpg
(158.38 KiB) Downloaded 590 times

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: positionprofit(1) with buy and sellshort logic problems

Postby furytrader » 03 Nov 2011

It may make sense to put the tradesize calculation formula that I mentioned before within the code block that begins "If MarketPosition = 0 ..." so that it makes the calculation right before placing the new orders.


Return to “MultiCharts”