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.