謝謝舞太極大的告知,
找到問題出在哪了,不過再來還要找解決的方法,
因為在一開始設定變數值有指定初使值0,
而在程式全部執行完畢後又從頭開始執行,
這時候滿足買進條件時值被設為1的bought的值又被設為0了,
所以導致原本取得的停損值又被重新設定了一次,
知道問題出在那裏,再來就是要想辦法讓變數的值的初始化只執行一次,
明天想想看要怎麼寫,如果有大大知道怎麼寫的話也請跟小弟說一聲,謝謝。
- //初始化下單大師
- OMComAPI = CreateStaticObject("OMSignAPI.OMCOMAPI");
- OMComAPI.IniDllAndPosition("test", 0);
- //設定停損的變數
- Buystop=0;
- Sellstop=0;
- //設定手上是否有單的變數
- bought =0;
- sold=0;
- //加入這一行可以看出值又變為零了
- Plot(bought,"\nbought",colorGreen,styleownscale|stylenolabel);
- //因為要馬上就可以進場,所以用收盤價大於8000為進場滿足條件
- if (LastValue(C>8000) AND bought == 0 )
- {
- NowDateTime=Now(0);
- NowPosition = 1;
- NowClose=LastValue(Close);
- OMComAPI.GoOrder("test","","NowDateTime",Nowposition,Nowclose);
- Buystop=Nowclose-10;
- Sellstop=0;
- bought=1;
- sold=0;
- }
- //收盤前平倉,或是滿足停損價則平倉
- if (LastValue(TimeNum())==134000 OR LastValue(Close)==Buystop OR LastValue(Close)==Sellstop)
- {
- NowDateTime=Now(0);
- NowPosition = 0;
- NowClose=LastValue(Close);
- OMComAPI.GoOrder("test","","NowDateTime",NowPosition,NowClose);
- Buystop=0;
- Sellstop=0;
- bought=0;
- sold=0;
- }
- //把停損價和是否有倉位顯示出來,以便觀察
- Plot(Sellstop,"\nsellstop",colorGreen,styleownscale|stylenolabel);
- Plot(Buystop,"\nbuystop",colorYellow,styleownscale|stylenolabel);
- Plot(bought,"\nbought",colorGreen,styleownscale|stylenolabel);
- Plot(sold,"\nsold",colorGreen,styleownscale|stylenolabel);
複製代碼 |