NexusFi: Find Your Edge


Home Menu

 





Trouble Debugging EL Position Sizing Code


Discussion in EasyLanguage Programming

Updated
    1. trending_up 3,192 views
    2. thumb_up 2 thanks given
    3. group 0 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 gftrader 
Cleveland, Ohio
 
Experience: Advanced
Platform: Tradestation
Trading: Futures
Posts: 14 since Aug 2022
Thanks Given: 6
Thanks Received: 3

Hey everyone,

I have some code I use for position sizing in Easy Language. The idea is the position size of your order increases as your equity increases in your account. It works perfectly in backtesting, but there's a strange issue when I use the code live in my simulated account. The code allows a minimum contract amount of 1 and maximum of 50. We have a "stop loss" set at $10. As you can see in the code below, it will take my risk percentage (0.01) multiplied by starting equity ($2000) divided by my stop loss to get a contract size (vCon) of 2. This works in backtesting, but my live trading consistently tries to fill 50 contracts (the max amount) and gets rejected. It is getting rejected because I made my sim account equity set to $2000. Any ideas why this is failing to work only in live trading?

Inputs:
iRiskPercent( 0.01 ),
iStop$( 10 ),
iPrice( (Close) * 10 ),
istartEquity( 2000 ),
iMaxUnits( 50 ),
iMinUnits( 1 );


var: msg(""),
retVal(false),
debug(true),
vCon(0),
vTotalEquity(0),
vDollarsToRisk(0);
If ( iRiskPercent > .99 OR iRiskPercent < .01 ) Then
Begin
vCon = -1;
Print("Invalid risk percentage input. Please enter a percent value from .01 to .99.");
End;

If ( iStop$ < 0 ) Then
Begin
vCon = -1;
Print("Invalid stop in dollars. Please enter a value greater than zero.");
End;

If ( vCon <> -1 ) Then
Begin

vTotalEquity = istartEquity + Netprofit;
vDollarsToRisk = iRiskPercent * vTotalEquity;
VCon = Intportion( vDollarsToRisk / iStop$ );

If ( VCon * iPrice ) > vTotalEquity then VCon = Intportion( vTotalEquity / iPrice );

If ( VCon > iMaxUnits ) then VCon = iMaxUnits;
If ( VCon < iMinUnits ) then VCon = iMinUnits;

If ( debug ) Then
Begin
msg = "PosSize - Risk: " + Numtostr(iRiskPercent,2) +
" Net Profit: " + Numtostr(NetProfit,2) +
" Total Equity: " + Numtostr(vTotalEquity,0) +
" Dollar Risk: " + Numtostr(vDollarsToRisk,0) +
" Dollar Stop: " + Numtostr(iStop$,2) +
" Units: " + Numtostr(VCon,2);
retVal = _CE_Print_To_Print_Log( msg );
End;
End
Else Print("Pos_Sz_Prnt_Rsk function has invalid input.");


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Iran Airspace Collapses 18 Points to 15.5% While Hormuz …
Prediction Markets & Event Contracts
El Clasico Draws $9.2M in Prediction Market Action -- Bi …
Prediction Markets & Event Contracts
Iran Airspace Contract Surges to 33.5% as Project Freedo …
Prediction Markets & Event Contracts
Iran Lebanon Problem Kills Switzerland Talks, Brent at $ …
Prediction Markets & Event Contracts
Hungary Called for Magyar at 97pct, Ending 16-Year Orban …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Big Mike in Ecuador
197 thanks
Sober Journey With S&P
27 thanks
30 Sessions
20 thanks
Volume Indicators
8 thanks
BERN ALGOS algo trading journal
8 thanks
  #2 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,737 since Jul 2012
Thanks Given: 1,917
Thanks Received: 7,476

That is because the code is only basing its decisions on the Net Profit and Equity of the strategy (not your actual account). So, if you run this strategy for 5 years, and it has a ton of profit built up, the strategy will think it should trade 50 contracts next trade.

What you want, for live trading, is to access your actual account equity, and base decisions off of that.

You should probably create a separate version of the strategy to do this.

Look at TS Help for the "Get" reserved words, like GetRTAccountEquity


Follow me on X Reply With Quote
Thanked by:
  #3 (permalink)
 gftrader 
Cleveland, Ohio
 
Experience: Advanced
Platform: Tradestation
Trading: Futures
Posts: 14 since Aug 2022
Thanks Given: 6
Thanks Received: 3



kevinkdog View Post
That is because the code is only basing its decisions on the Net Profit and Equity of the strategy (not your actual account). So, if you run this strategy for 5 years, and it has a ton of profit built up, the strategy will think it should trade 50 contracts next trade.



What you want, for live trading, is to access your actual account equity, and base decisions off of that.



You should probably create a separate version of the strategy to do this.



Look at TS Help for the "Get" reserved words, like GetRTAccountEquity



I think that makes sense! I’ll give it a shot. Thanks!


Started this thread Reply With Quote
  #4 (permalink)
 gftrader 
Cleveland, Ohio
 
Experience: Advanced
Platform: Tradestation
Trading: Futures
Posts: 14 since Aug 2022
Thanks Given: 6
Thanks Received: 3


kevinkdog View Post
That is because the code is only basing its decisions on the Net Profit and Equity of the strategy (not your actual account). So, if you run this strategy for 5 years, and it has a ton of profit built up, the strategy will think it should trade 50 contracts next trade.

What you want, for live trading, is to access your actual account equity, and base decisions off of that.

You should probably create a separate version of the strategy to do this.

Look at TS Help for the "Get" reserved words, like GetRTAccountEquity

Sorry to bother you. So I updated the code to use GetRTAccountEquity. My live account that I am using for this has $3,000 equity. I took a trade this morning on MCL, which should allow me to purchase 3 contracts with this code. However, it is still only purchasing the minimum (1 contract). Any thoughts? Here is the updated code (appreciate your help!):

Inputs:
iRiskPercent( 0.01 ),
iStop$( 10 ),
iPrice(Close),
iMaxUnits( 50 ),
iMinUnits( 1 );

var:
msg(""),
retVal(false),
debug(true),
vCon(0),
vTotalEquity(0),
vDollarsToRisk(0);
If ( iRiskPercent > .99 OR iRiskPercent < .01 ) Then
Begin
vCon = -1;
Print("Invalid risk percentage input. Please enter a percent value from .01 to .99.");
End;

If ( iStop$ < 0 ) Then
Begin
vCon = -1;
Print("Invalid stop in dollars. Please enter a value greater than zero.");
End;

If ( vCon <> -1 ) Then
Begin

vTotalEquity = Getrtaccountequity(Getaccountid);
vDollarsToRisk = iRiskPercent * vTotalEquity;
VCon = Intportion( vDollarsToRisk / iStop$ );

If ( VCon * iPrice ) > vTotalEquity then VCon = Intportion( vTotalEquity / iPrice );

If ( VCon > iMaxUnits ) then VCon = iMaxUnits;
If ( VCon < iMinUnits ) then VCon = iMinUnits;

If ( debug ) Then
Begin
msg = "PosSize - Risk: " + Numtostr(iRiskPercent,2) +
" Net Profit: " + Numtostr(NetProfit,2) +
" Total Equity: " + Numtostr(vTotalEquity,0) +
" Dollar Risk: " + Numtostr(vDollarsToRisk,0) +
" Dollar Stop: " + Numtostr(iStop$,2) +
" Units: " + Numtostr(VCon,2);
retVal = _CE_Print_To_Print_Log( msg );
End;
End
Else Print("Pos_Sz_Prnt_Rsk function has invalid input.");


Started this thread Reply With Quote
  #5 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,737 since Jul 2012
Thanks Given: 1,917
Thanks Received: 7,476

I would add print statements at various points in your code to see what is going on.

like:

print(date," ", vcon);


and make sure that each variable value is calculating what you think it should.

Are you sure vTotalEquity has the right value?


print(date," ", vTotalEquity);

would be one way to check.


Follow me on X Reply With Quote
Thanked by:
  #6 (permalink)
 gftrader 
Cleveland, Ohio
 
Experience: Advanced
Platform: Tradestation
Trading: Futures
Posts: 14 since Aug 2022
Thanks Given: 6
Thanks Received: 3


kevinkdog View Post
I would add print statements at various points in your code to see what is going on.

like:

print(date," ", vcon);


and make sure that each variable value is calculating what you think it should.

Are you sure vTotalEquity has the right value?


print(date," ", vTotalEquity);

would be one way to check.

Thank you!


Started this thread Reply With Quote




Last Updated on August 17, 2022


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts