COCO研究院

 找回密碼
 註冊
搜索
查看: 6374|回復: 32

[教學] 有關futures position size 一問

[複製鏈接]
發表於 14-7-18 23:27 | 顯示全部樓層 |閱讀模式
initial equity=1000000
margin=60000
position size=-100  (100%)

position size set 了-100, 意思應該是每次都把所有total equity (100%) 去buy/short futures contract. 所以第一個transection share(round to integer)=16, (首幾個backtest後 number of shares 都是把total equity/60000)但不知道為什麼往後的number of shares 很奇怪, 不是把total equity/60000. 很random的, 明明total equity 有1300000 但 buy 的number of shares 只有8, 有時只有2

有高手知道為什麼嗎? 應該很直接的, 但就是不知道為什麼, 望高手指點


發表於 14-7-18 23:33 | 顯示全部樓層
Hi,

你是想做到 fixed-dollar amount 的 position sizing 嗎?

ex. 每 20萬做 1口,或 margin 的幾倍做 1口

     當 equity 持續增加到 40萬時,可以做 2口,依此類推

     而 equity 減少時,縮減部位,反之亦然


是想做這樣的回測嗎?
 樓主| 發表於 14-7-19 00:06 | 顯示全部樓層
不, 我是想把全部current total equity(money)用以買/賣 futures contract. 所以我set position size =-100, 但為什麼backtest後不是這樣, number of shares 很奇怪

例如附件, cum profit 有900多萬的, 但為什麼shares 那麼少?
螢幕截圖 2014-07-18 23.51.41.jpg
發表於 14-7-19 00:14 | 顯示全部樓層
本帖最後由 kilroy 於 14-7-19 00:51 編輯
orangelam 發表於 14-7-19 00:06
不, 我是想把全部current total equity(money)用以買/賣 futures contract. 所以我set position size =-100 ...

這邊給個範例參考

SetOption("InitialEquity", 1000000);
SetPositionSize(1, spsShares);

Equity = Ref(Equity(1), -1);
FixDollar= 100000;

...

買賣條件

...

PZ = ValueWhen(Buy OR Short, ceil(Equity/FixDollar));
SetPositionSize( PZ, spsShares);

 樓主| 發表於 14-7-19 01:06 | 顯示全部樓層
SetOption("InitialEquity", 1000000 );
SetPositionSize(100,spsPercentOfEquity);
MarginDeposit=60000;
PointValue=50;

thanks, 但我是這樣的, 但就出現了該圖奇怪的shares, 明顯地不是用100% equity to buy/short futures. cum profit有九百多萬, 但為什麼shares是個位數?
發表於 14-7-19 01:13 | 顯示全部樓層
kilroy 發表於 14-7-19 00:14
這邊給個範例參考

SetOption("InitialEquity", 1000000);

Sorry, but that is absolute nonsense and misuse. Don't use Equity() function in analysis!

發表於 14-7-19 01:18 | 顯示全部樓層
orangelam 發表於 14-7-19 00:06
不, 我是想把全部current total equity(money)用以買/賣 futures contract. 所以我set position size =-100 ...

Short: 32 Contracts * (23483-23544) * 50 = 97600

發表於 14-7-19 01:22 | 顯示全部樓層
本帖最後由 joshsmi 於 14-7-19 01:30 編輯
orangelam 發表於 14-7-19 01:06
SetOption("InitialEquity", 1000000 );
SetPositionSize(100,spsPercentOfEquity);
MarginDeposit=60000;

Upload your apx file (project file) and symbol file located in your database and I will explain why this and that happens and gets calculated.

 樓主| 發表於 14-7-19 01:48 | 顯示全部樓層
joshsmi 發表於 14-7-19 01:18
Short: 32 Contracts * (23483-23544) * 50 = 97600

Short: 32 Contracts * (23483-23544) * 50 = 97600
above is correct but in row 5, initial equity+acc profit=1000000+9085300(in row 4)=10085300
so, number of futures contracts should be 10085300/60000=168, why not 168 contracts? but in row 5, it only 3 contracts... this is weird. any ideas?

my coding setting is very simple as below:
RoundLotSize=1;
TickSize=1;
SetOption("FuturesMode", True );
SetOption("InitialEquity", 1000000 );
SetPositionSize(100,spsPercentOfEquity);
MarginDeposit=60000;
PointValue=50;
.
.
.
buy=
sell=
short=
cover=

that's all... thank you very much!

發表於 14-7-19 01:58 | 顯示全部樓層
Again, upload project file (.apx extension) and symbol file located in database.
發表於 14-7-19 02:53 | 顯示全部樓層
本帖最後由 joshsmi 於 14-7-19 03:14 編輯

There is nothing weird. You just don't understand your own code as it seems to use some additional variable elsewhere.

That's why I said upload project file.

As you can see in the picture below everything in there is in-line with my code.

If you don't understand your own code then don't blame the software for your own limits in understanding but rather keep on learning and understanding what each code line of your code is doing.

BTW, margindeposit is the deposit required for a single contract.
I have used 50,000/contract for the sake of simplicity.



評分

參與人數 1金錢 +5 收起 理由
kilroy + 5 thanks but the pic is too small to see.

查看全部評分

發表於 14-7-19 03:20 | 顯示全部樓層
joshsmi 發表於 14-7-19 01:13
Sorry, but that is absolute nonsense and misuse. Don't use Equity() function in analysis!

Hi,

that is totally okay for discussing "to do better" on AmiBroker, don't be sorry. XD

I don't see why not use "equity()" in analysis or how nonsense it is.

If you please, could be more specific or any suggestions on counting the position size?


(take fixed-dollr amount for example)


It might be good and helpful.








發表於 14-7-19 05:10 | 顯示全部樓層
本帖最後由 joshsmi 於 14-7-19 05:13 編輯

"I don't see why not use "equity()" in analysis or how nonsense it is."

Because Equity() function is:
1. "old" single security backtester and left in for backward compatibility
2. in your example code snippet you called it on top of the code which is wrong anyway
3. for positionsizing there is SetPositionSize() function with multiple options

For example van Tharp fixed fractional position sizing:

Risk = 2; // percentage
StopAmount = 2 * ATR( 20 );
Stop = StopAmount / BuyPrice;
SetPositionSize( Risk / Stop, spsPercentOfEquity );

Buy = <your conditions here>;
Sell = 0; // selling only by stop

ApplyStop( 2, 2, StopAmount, 1 );

4. Equity() is rather used in Chart panes, for example to evaluate Applystops or to remove excessive signals or to plot single security equity etc. Other field of application is if you wanna replicate single security backtest in Exploration mode. etc

Again never ever use Equity() function in (Portfolio)Backtest/Optimize!
Tomasz Janeczko will tell you the same or similar.

For further details read AFL function reference of Equity()

發表於 14-7-19 05:16 | 顯示全部樓層
kilroy wrote: " thanks but the pic is too small to see."

Just click the picture to maximize or download it.
發表於 14-7-19 11:03 | 顯示全部樓層
本帖最後由 jacklcl 於 14-7-19 11:06 編輯
joshsmi 發表於 14-7-19 05:16
kilroy wrote: " thanks but the pic is too small to see."

Just click the picture to maximize or down ...


Hi Joshsmi --

Thanks for your nice sharing.

Actually the picture cannot be maximized and even I downloaded it, it will be unreadable once you zoom in.  Could you please post again the picture such that we can learn more.

Thank you!!
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-5-18 11:55

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook