本帖最後由 綠茶妹 於 09-12-31 07:22 PM 編輯
海龜交易策略的程式碼---->我猜這是tradestation版,
有沒有人要用AB來改寫?
- /****************************************************************************************************
- /海龜交易策略
- /*****************************************************************************************************/
- var nLastRawTime = 0;
- var BLBarOffset = 0;
- var vPrev = 0;
- var vState = 0;
- var vHour;
- var vMin;
- var vSec;
- var vDay;
- var vMonth;
- function preMain() {
- setStudyTitle("MS1");
- setCursorLabelName("Return",0);
- setPriceStudy(true);
- // setStudyMin(-10);
- // setStudyMax(10);
- // addBand(1.00, PS_SOLID, 1, Color.RGB(0,0,0));
- }
- function main(vLength1,vLength2) {
- if (vLength1 == null) {
- vLength1 = 10;
- }
- if (vLength2 == null) {
- vLength2 = 20;
- }
- var vTime = new Date();
- //---------------------------------------------------
- // Get Time Variables 時間變數
- vTime = getValue("Time", 0);
- vHour = vTime.getHours();
- vMin = vTime.getMinutes();
- vSec = vTime.getSeconds();
- vDay = vTime.getDate();
- vMonth = vTime.getMonth() +1;
- setComputeOnClose(true);
- if (getValue("rawtime", 0) != nLastRawTime) {
- nLastRawTime = getValue("rawtime",0);
- BLBarOffset += 1;
- }
- if ((BLBarOffset > vLength1) && (BLBarOffset > vLength2)) {
- var vEL = -1;
- var vCL = -1;
- var vES = -1;
- var vCS = -1;
- if (high() > HLFinder(vLength2, 1)) { vEL = 1; }
- if (low() < HLFinder(vLength1, -1)) { vCL = 1; }
- if (low() < HLFinder(vLength2, -1)) { vES = 1; }
- if (high() > HLFinder(vLength1, 1)) { vCS = 1; }
- // debugPrintln(vHour+":"+vMin+" "+vEL+" : "+vCL+" : "+vES+" : "+vCS);
- // Handle Longs 處理多頭部位
- if ((vEL == 1) && ((!Strategy.isInTrade()) || (Strategy.isShort()))){
- drawShapeRelative(1, low(1) -0.25, Shape.UPARROW, null, Color.green, Shape.ONTOP, getValue("rawtime") + "Buy");
- Strategy.doLong("Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
- }
- if ((vCL == 1) && (Strategy.isLong()) ){
- drawShapeRelative(0, low() -0.25, Shape.DOWNARROW, null, Color.blue, Shape.ONTOP, getValue("rawtime") + "BuyX");
- Strategy.doSell("Long Exit", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
- }
- // Handle Shorts 處理空頭部位
- if ((vES == 1) && ((!Strategy.isInTrade()) || (Strategy.isLong()))){
- drawShapeRelative(1, high(1)+0.25, Shape.DOWNARROW, null, Color.red, Shape.ONTOP, getValue("rawtime") + "Sell");
- Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
- }
- if ((vCS == 1) && (Strategy.isShort()) ){
- drawShapeRelative(0, high()+0.25, Shape.UPARROW, null, Color.magenta, Shape.ONTOP, getValue("rawtime") + "SellX");
- Strategy.doCover("Short Exit", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
- }
- }
- vPrev = vState;
- return ;
- } // end of main.
- function HLFinder(vLength, Type)
- {
- var i;
- HighestHigh = 0;
- LowestLow = 999999;
- for (i = -1; i > (0-(vLength));i--) {
- HighestHigh = Math.max(HighestHigh, high(i));
- LowestLow = Math.min(LowestLow, low(i));
- }
- if (Type == 1) { return (HighestHigh); }
- if (Type == -1) { return (LowestLow); }
- }
複製代碼
來源
http://www.wretch.cc/blog/trader01/9030087 |