NexusFi: Find Your Edge


Home Menu

 





Trouble Debugging EL Position Sizing Code


Discussion in EasyLanguage Programming

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




 
Search this Thread

Trouble Debugging EL Position Sizing Code

  #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?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
REcommedations for programming help
Sierra Chart
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
How to apply profiles
Traders Hideout
Increase in trading performance by 75%
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
GFIs1 1 DAX trade per day journal
11 thanks
HumbleTraders next chapter
11 thanks
  #2 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351

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 Twitter 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,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351

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 Twitter 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


© 2024 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 - Privacy Policy - Downloads - Top
no new posts