COCO研究院

 找回密碼
 註冊
搜索
查看: 2967|回復: 7

[範例程式碼] 一段程式的邏輯錯誤,請指教。

[複製鏈接]
發表於 18-9-28 14:25 | 顯示全部樓層 |閱讀模式
請問各位前輩:

    我寫了一段式,很簡單的,macd 上交叉就買多,下交叉時就買出,然後做空,程式如下:
-----------------------------------------------------



if marketposition = 0  then
begin
if macd1 cross over MacdAVG  then         buy next bar market ;
if macd1 cross below  MacdAVG  then sellshort next bar market ;
end;


if marketposition=1  then
begin
if macd1 cross below  MacdAVG  then
begin
        sell  next bar market;
end;



end;

if marketposition=-1  then
begin
if macd1 cross over  MacdAVG  then
begin
        buytocover  next bar market;
end;
end;
--------------------------------------------------------------

這樣子跑,只會做多,不會做空。但是如果我把底下這行,mark 掉

//if macd1 cross over MacdAVG  then         buy next bar market ;
就會正常的進空單,及回補空單,請問這是那邊的錯誤呢?煩請大家幫忙一下。感謝





發表於 18-9-28 21:58 | 顯示全部樓層
本帖最後由 tony660302 於 18-9-28 23:18 編輯

if marketposition = 0  then
begin
if macd1 cross over MacdAVG  then         buy next bar market ;
if macd1 cross below  MacdAVG  then sellshort next bar market ;
end;


不同的條件式要用 if ... else ... 串起來,不直能簡單粗暴的接並列,這樣電腦看不懂
回復 支持 1 反對 0

使用道具 舉報

發表於 18-10-25 15:10 | 顯示全部樓層
你寫的跟你要的不一樣...

所有的cross over/under,都一直交替出現...
一開始若是cross over
則是 cross over, under, orver, under, ...一直循環下去
1.  cross over +(加上) MP=0  (markepostion)  

  你會買進一口多單(buy),  這時 MP=1
2. 接下來一定是 cross under + MP =1
  結果是平倉(sell), MP變成=0
3. 再來又是 cross over + MP=0
  買進一口多單(buy),  這時 MP=1

4. 接下來一定是 cross under + MP =1
  結果是平倉(sell), MP變成=0  
一直循環下去....(看起來就是只有多單成交)


如果一開始若是cross under
則是 cross under, over, under, orver, under, ...一直循環下去

...
一直循環下去....看起來就是只有空單成交


-----------------------------------------------------
直接寫
cross over 多單買進

cross under 空單買進

不用管平倉(MC下反向單,等同先平倉+再反向下單的效果)





回復 支持 1 反對 0

使用道具 舉報

發表於 18-9-29 16:42 | 顯示全部樓層
MCH有時候偶而出現奇怪的程式信號方式,不是預期的模式
避免這種異常,最佳使用 前後變數 實際比對 交叉,避免使用簡易的 CROSS XXXX 指令
if a[1]>=b[1] and a<b  then    ------------- Cross down
if a[1]<=b[1] and a>b  then    ------------- Cross up

評分

參與人數 1金錢 +5 收起 理由
special + 5 感謝分享

查看全部評分

回復 支持 1 反對 0

使用道具 舉報

發表於 18-9-29 00:25 | 顯示全部樓層
我有一個數千行的策略, 也是用類似樓主的寫法, 但是沒有問題:
var
   mktPos(0);
mktPos = marketposition
if mktPos = 0 then begin...end;
if mktPos = 1 then begin...end;
if mktPos = -1 then begin...end;
 樓主| 發表於 18-9-29 07:12 | 顯示全部樓層
wanwh 發表於 18-9-29 00:25
我有一個數千行的策略, 也是用類似樓主的寫法, 但是沒有問題:
var
   mktPos(0);

數千行.....................
這樣系統不會太慢嗎?呵呵
我記得之前好似也是可以,後來就不行。再來參照大家的建議,來試看看了。
發表於 18-9-30 00:02 | 顯示全部樓層
zhangmall 發表於 18-9-29 07:12
數千行.....................
這樣系統不會太慢嗎?呵呵
我記得之前好似也是可以,後來就不行。再來參照 ...

程式碼符合條件才可執行, 每bar 後只跑一部分程式碼, 而且現在的電腦不慢.

發表於 18-10-1 15:01 | 顯示全部樓層
如果你要macd上交叉就買多, 下交叉時就賣出,
那就是手上一直有單, marketposition 不是 1 就是 -1,
除了第一筆交易之前 marketposition 不會=0,
你原來的寫法是 marketposition =0 時才會進新單,
如果你要當macd上或下交叉時, 同時"回補"而且"進新單",
用以下的寫法MC就會自動幫你回補:

if marketposition <= 0  then
begin
        if macd1 cross over MacdAVG  then buy next bar market ;
end;

if marketposition >= 0  then
begin
        if macd1 cross below MacdAVG  then sellshort next bar market ;
end;


檢查錯誤可用print功能, 然後到output視窗看看問題出在哪裡.
once cleardebug;
print(date,time,marketposition,macd1 cross over MacdAVG,macd1 cross below  MacdAVG);


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

本版積分規則

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

GMT+8, 24-4-29 11:22

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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