Position size  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
meras
Posts: 11
Joined: 22 Feb 2022
Has thanked: 2 times
Been thanked: 1 time

Position size

Postby meras » 30 Jul 2022

Hi,
I'm trying to backtest a strategy where my position size is depending on the ATR of the underlying security. But when I run the test, I recieve the amount of contracts specified in the "Strategy Properties" window. Is there a way to disable the settings from Strategy Properties so my position size is deterined from the values generated in my code?

In case the error lies within my code, you can find it here (I use MACD as a tester):

Code: Select all

////////////////////////////VARIABLES//////////////////////////// vars: entryLong (true), exitLong (true); ////////////////////////////MONEY MANAGEMENT//////////////////////////// Inputs: InitialCapital(10000), rskAmt (0.02), atrLen(14); vars: risk (0), numContracts (0); // Define risk risk = AvgTrueRange(atrLen)*bigpointvalue; numContracts = (InitialCapital * rskAmt) / risk; //Round down to the nearest whole number value1 = Round(numContracts, 0); if(value1 > numContracts) then numContracts = value1 -1 else numContracts = value1; //If numcontracts are less then 1 then this will round it uo to one numcontracts = maxlist(numContracts, 1); ////////////////////////////MACD//////////////////////////// inputs: MACDFastLength( 12 ), MACDSlowLength( 26 ), MACDLength( 9 ) ; variables: var0( 0 ), var1( 0 ), var2( 0 ) ; var0 = MACD( Close, MACDFastLength, MACDSlowLength ) ; var1 = XAverage( var0, MACDLength ) ; var2 = var0 - var1 ; entryLong = var2 crosses over 0; exitLong = var2 crosses under 0; ////////////////////////////LONG SIGNAL//////////////////////////// // Entry Signal Long if marketposition = 0 and entryLong = true then Buy ( "TestEntry" ) numContracts contract next bar at market ; ////////////////////////////EXIT//////////////////////////// if marketposition = 1 and exitLong = true then Sell ( "TestExit" ) numContracts contract next bar at market ;

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: Position size

Postby rrams » 30 Jul 2022

Depending on the historical data, AvgTrueRange can return zero. Which would throw a division by zero error. Other than that it should work.

Try setting the numContracts variable to fixed number and if that does not work then provide a screenshot of your signal property settings and what instrument you are trading.

User avatar
meras
Posts: 11
Joined: 22 Feb 2022
Has thanked: 2 times
Been thanked: 1 time

Re: Position size  [SOLVED]

Postby meras » 31 Jul 2022

Depending on the historical data, AvgTrueRange can return zero. Which would throw a division by zero error. Other than that it should work.

Try setting the numContracts variable to fixed number and if that does not work then provide a screenshot of your signal property settings and what instrument you are trading.
I found the solution. It appears that the "Maximum shares/contracts per position" setting was still applying so I was limited to a maximum of 1 contract.

Thank you for your reply though :)


Return to “MultiCharts”