NexusFi: Find Your Edge


Home Menu

 





My MM / PositionSize Code


Discussion in EasyLanguage Programming

Updated
    1. trending_up 4,194 views
    2. thumb_up 7 thanks given
    3. group 2 followers
    1. forum 10 posts
    2. attach_file 0 attachments




 
Search this Thread

My MM / PositionSize Code

  #1 (permalink)
sagetrade
Frankfurt / Germany
 
Posts: 47 since Jul 2013
Thanks Given: 28
Thanks Received: 11

Hey Guys, Im having a common Trendfollower in place including MA Crossovers, that I want to write a simple MM code for. Here is my idea:

Instead of having...

Quoting 
Buy ("Enter Long") 3 Contracts Next Bar At Market;

I'd like to write a function with name "TradeSize", so that i can write...

Quoting 
Buy ("Enter Long") TradeSize Contracts Next Bar At Market;

This function is supposed to include a variable "RiskSize (1000)"

The tradesize function should then take the current share price into account, when all conditions are met and calculate the following...

Quoting 
mathround(TradeSize) = RiskSize / (SharePrice - IndicatorValue(Supertrend))

...where IndicatorValue(Supertrend) is the current value of the Supertrend indicator!

Would be great, if one of you guys could give me some input in how a function like this can work out for me!


The Supertrend code is from BigMike himself and the code as well as pla-files can be found in this thread:

Quoting 

Does this logic sound reasonable to you?
Does it have any flaws?
Can you help me with which commands do I need for "mathround", "Currentshareprice" and the value of "Supertrend"?
What inputs and vars do I need to achieve this?

Thanks so much!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
ZombieSqueeze
Platforms and Indicators
Trade idea based off three indicators.
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
23 thanks
Tao te Trade: way of the WLD
20 thanks
My NQ Trading Journal
19 thanks
HumbleTraders next chapter
9 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627


Hi sagetrade,

you seem well on track already with the written logic.

If you want Tradesize to be a function, I would suggest making RiskSize an input for it. I would also suggest instead of simply rounding the outcome, to make sure you only round down to the next full value. This will make sure that you always stay within your risk limits.
Big Mike already provided the function for the Supertrend, so you simply need to call it within your Tradesize function.
It can look something like this:

 
Code
Inputs: 
	RiskSize			(NumericSimple),
	ATRLength			(NumericSimple), 
	ATRMult			(NumericSimple), 
	Strength			(NumericSimple);
	
Variables:
	vSTrend			(0),
	vTradeSize			(0),
	vSuperTrend			(0);

//call the supertrend function
vSuperTrend = SuperTrend(ATRLength, ATRMult, Strength, vSTrend);

//the Close - vSuperTrend <> 0 check avoids division by zero errors	
if Close - vSuperTrend <> 0 then	
	vTradeSize = RiskSize / AbsValue(Close - vSuperTrend)
else
	vTradeSize = 0; {you should change this value to whatever you want to use for the size in cases the last price is equal to the supertrend}
	
//round to the next lowest value
vTradeSize = Floor(vTradeSize);

//TradeSize is the name of the function, if you use a different name, you need to change TradeSize to this name
TradeSize = vTradeSize;
The code logic as it is has one serious problem that you probably want to avoid. When the difference between the last price and the Supertrend is very small, the Tradesize will become huge. This should be avoided with setting a maximum for the size. Another thing I noticed is that the Tradesize in general is quite large at least in my testing here on the @ES it seems so. Take a look at the outcome on a chart using the function within the Custom 1 Line indicator or place it into an indicator.

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
sagetrade
Frankfurt / Germany
 
Posts: 47 since Jul 2013
Thanks Given: 28
Thanks Received: 11

Thank you so much, ABCTG!

A huge TradeSize should not appear if you choose your RiskSize variable carefully, furthermore it depends on which parameters you choose for the Supertrend function and which timeframe you are running it.

If RiskSize is small, your timeframe is of larger scale and if your parameters for the Supertrend are higher than standard, your tradesize should be fine. My intention was to get a volatility adjusted TradeSize. Naturally, in some settings TradeSize will get kind of large and in others it will be smaller.

I tried to include that function in my Sample Strategy, would be great if you could take a look!


Quoting 

// Sage Test Strategy 15.07.2013

Inputs:

smalength ( 200 ),
emalength ( 50 ),
ATRLength ( 10 ),
ATRMult ( 3 ),
Strength ( 10 ),
RiskSize ( 100 );

Variables:

smav ( 0 ),
emav ( 0 ),
vSTrend ( 0 ),
vSuperTrend ( 0 ),
vTradeSize ( 0 ),
st1 ( 0 );

// Call MAs
smav = Average(Close, smalength);
emav = XAverage(Close, emalength);

// Call Supertrend
vSuperTrend = Supertrend(ATRLength, ATRMult, Strength, vSTrend);

// Call Tradesize
vTradeSize = TradeSize(RiskSize, ATRLength, ATRMult, Strength, vStrend);

// Open new positions (Long Only)

if marketposition = 0 then begin // Only open new positions if flat

if close > vSuperTrend then begin // Checks if Supertrend is below price action

end;

// EMA crosses Above SMA & Both Rising
if emav crosses above smav and smav > smav[1] and emav > emav[1] and emav > smav then begin // If SMAV and EMAV are rising and cross
Buy ("Enter Long") vTradeSize Contracts Next Bar At Market;

end;

end;

// Send Initial SL Order

if marketposition = 1 then begin // Checks if positions are opened (Long)
st1 = vSuperTrend;
Sell ("Exit Stop") all Contracts Next Bar At st1 Stop; //Close open positions

end;

// Close existing positions

if marketposition = 1 then begin //Checks if positions are opened

if close < vSuperTrend then begin //If supertrend switches from green to red
Sell ("Exit Regular") all Contracts next Bar At Close; //Closes open positions

end;
end;


Reply With Quote
Thanked by:
  #5 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

You are welcome. The line
 
Code
Buy ("Enter Long") TradeSize Contracts Next Bar At Market;
should include the variable and not the function name. You need to change TradeSize to vTradeSize. You also might want to use the number of contracts you entered with to be closed with your Exit Stop (or use "all contracts" there, too).
Other than that I didn't see anything at first glance. Although you could move the calculations from vTradeSize into the main part of the strategy. You are calling the Supertrend there already and with the function you'd call it twice now.

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #6 (permalink)
sagetrade
Frankfurt / Germany
 
Posts: 47 since Jul 2013
Thanks Given: 28
Thanks Received: 11

Thanx ABCTG,

Can you explain what you mean by "Although you could move the calculations from vTradeSize into the main part of the strategy. You are calling the Supertrend there already and with the function you'd call it twice now."

I get an Error for calling the TradeSize function.

Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

What is the error message you are getting?

I meant you could change the part where you call the TradeSize function with this code from the function. You don't have to do this, I just saw that you are using the SuperTrend in the signal, too. So you could save one call by moving the size calculation to the signal directly and simply use the SuperTrend variable there.


 
Code
//the Close - vSuperTrend <> 0 check avoids division by zero errors	
if Close - vSuperTrend <> 0 then	
	vTradeSize = RiskSize / AbsValue(Close - vSuperTrend)
else
	vTradeSize = 0; {you should change this value to whatever you want to use for the size in cases the last price is equal to the supertrend}
	
//round to the next lowest value
vTradeSize = Floor(vTradeSize);
Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #8 (permalink)
sagetrade
Frankfurt / Germany
 
Posts: 47 since Jul 2013
Thanks Given: 28
Thanks Received: 11

Hi ABC, needed some time to resolve this. I managed to fix the Error problem.

Yes, the Supertrend is supposed to be in the signal too, don't know the exact role of it yet, its probably not used to be the sole criterion for entry, but switch maybe used as an exit or something. Im just tryin to rack up my EL-skills a little here.

thx for all ur support up to now, will definitly recommed you if I get a chance to!



I see, instead of using TradeSize as a function you mean I could put it into the signal directly.
Did I understand you correctly here?

What I'm wondering is, I will need to change input risksize on a regular basis manually, in order to reflect my money management preferences. Also Tradesize might also be used by different signals.

Another issue here is that if this signal is active on a regular basis, im wondering if I can get into any trouble with the strategy. What is going to happen if the signal has open positions and im going to double risksize? Am I right if I say that there is nothing happening to the current positions, but future positions will be larger? Is this any different if I change this input in TradeSize function or in the signal directly? Like if I change inputs in the signal-script, signal will go offline or something?

So what are the Advantages/Disadvantages of putting the Code into the signal, or keep it as a seperate function?

thx Sage

Reply With Quote
  #9 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,433 since Apr 2013
Thanks Given: 481
Thanks Received: 1,627

You are welcome.

Yes, you understood it correctly the code could go directly into the signal, too.

It's no problem to change the trade size manually or to have the same signal on different charts with different settings.
However one thing you should note, in case you change things in the code or the inputs a running system will be switched off.

Regards,
ABCTG


sagetrade View Post
Hi ABC, needed some time to resolve this. I managed to fix the Error problem.

Yes, the Supertrend is supposed to be in the signal too, don't know the exact role of it yet, its probably not used to be the sole criterion for entry, but switch maybe used as an exit or something. Im just tryin to rack up my EL-skills a little here.

thx for all ur support up to now, will definitly recommed you if I get a chance to!



I see, instead of using TradeSize as a function you mean I could put it into the signal directly.
Did I understand you correctly here?

What I'm wondering is, I will need to change input risksize on a regular basis manually, in order to reflect my money management preferences. Also Tradesize might also be used by different signals.

Another issue here is that if this signal is active on a regular basis, im wondering if I can get into any trouble with the strategy. What is going to happen if the signal has open positions and im going to double risksize? Am I right if I say that there is nothing happening to the current positions, but future positions will be larger? Is this any different if I change this input in TradeSize function or in the signal directly? Like if I change inputs in the signal-script, signal will go offline or something?

So what are the Advantages/Disadvantages of putting the Code into the signal, or keep it as a seperate function?

thx Sage


Follow me on Twitter Reply With Quote
Thanked by:
  #10 (permalink)
sagetrade
Frankfurt / Germany
 
Posts: 47 since Jul 2013
Thanks Given: 28
Thanks Received: 11



ABCTG View Post
You are welcome.

Yes, you understood it correctly the code could go directly into the signal, too.

It's no problem to change the trade size manually or to have the same signal on different charts with different settings.
However one thing you should note, in case you change things in the code or the inputs a running system will be switched off.

Regards,
ABCTG

Can I avoid the system being switched off, if I keep TradeSize as a seperate function and just change the input in there?

Can MC sync open positions if I change RiskSize, system is switched off and I switch it on again?

Reply With Quote




Last Updated on August 11, 2013


© 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