 |
max687
初級會員
註冊日期: Sep 2015
來 自: 台北市
文章數量: 11 |
請教公式
版主您好,
我想寫一個公式,一直無法成功,煩請幫我看看問題是出在哪,謝謝!
目標:
買訊:
收盤價上穿5日均線 AND 5日均線>=10日均線.
賣訊:
收盤價跌破10日均線.
規則:
1.只計算第一個買,賣訊(只有:買進---賣出---買進---).
2.賣訊出現後,10天(限制日期)內的買訊不算.
我嘗試寫的公式如下:
買訊:=cross(c,ma(c,5)) and ma(c,5)>=ma(c,10);
賣訊:=cross(ma(c,10),c);
限制日期:10;
倉位:=0;
CC:=C;
for i = 1 to datacount do begin
if 倉位=0 then begin
if 買訊[i] then 買進價:=CC[i];盈虧[i]:=(CC[i]-買進價)*100/買進價;倉位=1;
end else if 倉位=1 then begin
if 賣訊[i] then 賣出價:=CC[i];平倉盈虧[i]:=(賣出價-買進價)*100/買進價;倉位=0;買進價:=0; i = i + 限制日期;
end ;
end;
-----------------------------------------------
這指標顯示:"數組下標越界或超出有效數據區間;"
我剛開始學習寫程式,很沒概念,麻煩您幫我看看要怎麼改,謝謝!
|
|
向版主報告此篇 |  |
|
2016-03-15 17:45 |
|
|
|  |
 |
cgjj
總版主

註冊日期: Oct 2003
來 自:
文章數量: 18191 |
回覆: 請教公式
引用: 最初由 max687 發表
版主您好,
我想寫一個公式,一直無法成功,煩請幫我看看問題是出在哪,謝謝!
目標:
買訊:
收盤價上穿5日均線 AND 5日均線>=10日均線.
賣訊:
收盤價跌破10日均線.
規則:
1.只計算第一個買,賣訊(只有:買進---賣出---買進---).
2.賣訊出現後,10天(限制日期)內的買訊不算.
我嘗試寫的公式如下:
買訊:=cross(c,ma(c,5)) and ma(c,5)>=ma(c,10);
賣訊:=cross(ma(c,10),c);
限制日期:10;
倉位:=0;
CC:=C;
for i = 1 to datacount do begin
if 倉位=0 then begin
if 買訊[i] then 買進價:=CC[i];盈虧[i]:=(CC[i]-買進價)*100/買進價;倉位=1;
end else if 倉位=1 then begin
if 賣訊[i] then 賣出價:=CC[i];平倉盈虧[i]:=(賣出價-買進價)*100/買進價;倉位=0;買進價:=0; i = i + 限制日期;
end ;
end;
-----------------------------------------------
這指標顯示:"數組下標越界或超出有效數據區間;"
我剛開始學習寫程式,很沒概念,麻煩您幫我看看要怎麼改,謝謝!
原碼: MA5:=ma(c,5); MA10:=ma(c,10);
買訊:cross(c,MA5) and MA5>=MA10 or barpos=0 colorred;
賣訊:cross(MA10,c) or barpos=0 colorgreen;
訊號:=買訊-賣訊; NowBS:=0; Last賣:=0;
BS:=訊號*0; 限制日期:=10;
for i=1 to datacount do begin
if NowBS=1 and 訊號[i]=-1 then begin
NowBS:=-1; Last賣:=i;
end else if NowBS=-1 and 訊號[i]=1 then begin
if i-Last賣>限制日期 then NowBS:=1;
end else if NowBS=0 then begin
NowBS:=訊號[i]; if NowBS=-1 then Last賣:=i;
end;
BS[i]:=NowBS;
end;
BS:=if(BS<>ref(BS,1) or barpos=lbound(BS),BS,0);
買訊:=BS=1;
賣訊:=BS=-1;
|
|
向版主報告此篇 |  |
|
2016-03-16 10:09 |
|
|
|  |
 |
max687
初級會員
註冊日期: Sep 2015
來 自: 台北市
文章數量: 11 |
謝謝版主,我還有2個問題要麻煩您.
1.如果第1個出現的是賣訊,那就不取,這條件只要做多.
2我想顯示從買進到賣出,每天以收盤計算的浮動損益,我自己嘗試的程式如下,但仍完全無法顯示.
原碼如下:
MA5:=ma(c,5); MA10:=ma(c,10);
買訊:cross(c,MA5) and MA5>=MA10 or barpos=0 colorred;
賣訊:cross(MA10,c) or barpos=0 colorgreen;
訊號:=買訊-賣訊; NowBS:=0; Last賣:=0;
CC:=C;
進價:=C*0 NOAXIS;
出價:=C*0 NOAXIS;
BS:=訊號*0; 限制日期:=10;
for i=1 to datacount do begin
if NowBS=1 and 訊號[i]=-1 then begin
出價:=CC[i];平倉盈虧:=(出價-進價)*100/進價;
NowBS:=-1; Last賣:=i;
end else if NowBS=-1 and 訊號[i]=1 then begin
if i-Last賣>限制日期 then
進價:=CC[i];盈虧:=(CC[i]-進價)*訊號*100/進價;NowBS:=1;
end else if NowBS=0 then begin
NowBS:=訊號[i]; if NowBS=-1 then Last賣:=i;
end;
BS[i]:=NowBS;
end;
BS:=if(BS<>ref(BS,1) or barpos=lbound(BS),BS,0);
買訊:=BS=1;
賣訊:=BS=-1;
非常感謝!
|
|
向版主報告此篇 |  |
|
2016-03-16 13:11 |
|
|
|  |
 |
max687
初級會員
註冊日期: Sep 2015
來 自: 台北市
文章數量: 11 |
版主太厲害了!
已經可以用了,把LAST賣設定為-99,我想破頭也想不到.這程式真不是一般人能寫的呀.
非常感謝!
|
|
向版主報告此篇 |  |
|
2016-03-16 19:14 |
|
|
|  |
 |
max687
初級會員
註冊日期: Sep 2015
來 自: 台北市
文章數量: 11 |
請問如果我要加上:"開始計算的日期"這個限制條件,該怎麼做?
我的寫法如下:
MA5:=ma(c,5); MA10:=ma(c,10);
買訊:cross(c,MA5) and MA5>=MA10 or barpos=0 colorred;
賣訊:cross(MA10,c) or barpos=0 colorgreen;
訊號:=買訊-賣訊; NowBS:=0; Last賣:=-99; CC:=C;
BS:=訊號*0; 限制日期:=10; 盈虧:訊號*0 linethick;
-------------------------------------------------
開始日期:1000;
KK:=max(0,barssince(c)-開始日期);
--------------------------------------------------
我想從1000天前,或是上市日(小於1000天)開始計算,所以加了這2行,希望當底下的i大於KK這個數,再開始計算,但不知要怎麼加進去.
另外,如果要加一個"截止日期"(提前結束,例如:只顯示1000天到500天這段時間的訊號),又該如何?
感謝!
for i=1 to datacount do begin
if NowBS=1 and 訊號[i]=-1 then begin
NowBS:=-1; Last賣:=i;
end else if NowBS=-1 and 訊號[i]=1 then begin
if i-Last賣>限制日期 then begin
NowBS:=1; 進價:=CC[i];
end;
end else if NowBS=0 and 訊號[i]=1 then begin
NowBS:=訊號[i]; 進價:=CC[i];
end;
BS[i]:=NowBS;
if 進價>0 then 盈虧[i]:=CC[i]-進價;
if NowBS=-1 then 進價:=0;
end;
BS:=if(BS<>ref(BS,1) or barpos=lbound(BS),BS,0);
買訊:=BS=1;
賣訊:=BS=-1;
|
|
向版主報告此篇 |  |
|
2016-03-19 14:11 |
|
|
|  |
 |
cgjj
總版主

註冊日期: Oct 2003
來 自:
文章數量: 18191 |
引用: 最初由 max687 發表
請問如果我要加上:"開始計算的日期"這個限制條件,該怎麼做?
我的寫法如下:
MA5:=ma(c,5); MA10:=ma(c,10);
買訊:cross(c,MA5) and MA5>=MA10 or barpos=0 colorred;
賣訊:cross(MA10,c) or barpos=0 colorgreen;
訊號:=買訊-賣訊; NowBS:=0; Last賣:=-99; CC:=C;
BS:=訊號*0; 限制日期:=10; 盈虧:訊號*0 linethick;
-------------------------------------------------
開始日期:1000;
KK:=max(0,barssince(c)-開始日期);
--------------------------------------------------
我想從1000天前,或是上市日(小於1000天)開始計算,所以加了這2行,希望當底下的i大於KK這個數,再開始計算,但不知要怎麼加進去.
另外,如果要加一個"截止日期"(提前結束,例如:只顯示1000天到500天這段時間的訊號),又該如何?
感謝!
...............
將此段
開始日期:1000;
KK:=max(0,barssince(c)-開始日期);
for i=1 to datacount do begin
改為
StKNo:=datacount-1000;
if StKNo<1 then StKNo:=1;
for i=StKNo to datacount do begin
|
|
向版主報告此篇 |  |
|
2016-03-21 09:58 |
|
|
|  |
本站所有內容未經作者授權禁止轉貼節錄, 發表言論僅供參考勿作為投資決策依據。瀏覽本站請使用 IE 5.5 以上版本, 最佳瀏覽解析度 1024 x 768 全彩。
|
Powered by: vBulletin Version 2.3.0 - Copyright©2000-, Jelsoft Enterprises Limited.
簡愛洋行 製作 Copyright 2003-. All Rights Reserved. 聯絡我們
|