新手請教公式設置 - TradeBlazer公式 [開拓者 TB]
- 咨詢內容:
// 自己新學TB,編了個公式卻不能實現,請高手們指導,謝謝。
// 以下為均線交易系統,10日均線上穿20均線為買入,10日均線下穿20均線為賣出
// 均線買入/賣出時機均為平倉反手
// 開倉為二手,達到盈利預期值時平倉一手,等回落后加倉一手
Params
Numeric Length1(10); // 10日均線的參數值
Numeric Length2(20); // 20日均線的參數值
Numeric Lots(2); // 默認的交易數量
Vars
NumericSeries MA1;
NumericSeries MA2;
Numeric MinPoint; // 一個最小變動單位,也就是一跳
Numeric AddSet(50); // 加倉設置
Numeric SubSet(50); // 減倉設置
NumericSeries FirstPrice; // 第一次開倉價格
NumericSeries LastPrice; // 最后一次開倉價格
boolSeries Condition1;
boolSeries Condition2;
Begin
MA1 = XAverage(Close,Length1);
MA2 = XAverage(Close,Length2);
MinPoint = MinMove*PriceScale();
Condition1 = CrossOver(MA1,MA2);
Condition2 = CrossUnder(MA1,MA2);
If(MarketPosition==0) //空倉狀態時
{
if(condition1[1])// 當出現10日均線上穿20均線時,下一個bar開盤價買入
{
FirstPrice = Open;
LastPrice = FirstPrice;
Buy(lots,FirstPrice);
}else if(condition2[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價賣出
{
FirstPrice = Open;
LastPrice = FirstPrice;
Sellshort(Lots,Open);
}
}else if(MarketPosition==1) // 有多頭持倉的情況
{
If(condition2[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價賣出
{
Sellshort(Lots,Open);
}else if(CurrentContracts==2 && High>= LastPrice+SubSet*MinPoint) // 止贏減倉
{
Sell(1,LastPrice+SubSet*MinPoint);
LastPrice =LastPrice + subSet*MinPoint;
}else if(CurrentContracts==1 && Low <=LastEntryPrice - AddSet*MinPoint) // 逢低加倉
{
buy(1,LastPrice- AddSet*MinPoint);
LastPrice = LastPrice - addSet*MinPoint;
}
}else if(MarketPosition==-1);// 有空頭持倉的情況
{
If(condition1[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價平倉并買入
{
Sellshort(Lots,Open);
}Else if(CurrentContracts==2 && Low <= LastEntryPrice - subSet*MinPoint) // 止贏減倉
{
BuyToCover(1,LastPrice);
LastPrice = LastPrice - subSet*MinPoint;
}else if( CurrentContracts==1 && High >=LastPrice + addSet*MinPoint) // 逢高加倉
{
sell(1,LastPrice);
LastPrice = LastPrice + AddSet*MinPoint;
}
}
End - TB技術人員:
好像是這樣吧
// 以下為均線交易系統,10日均線上穿20均線為買入,10日均線下穿20均線為賣出
// 均線買入/賣出時機均為平倉反手
// 開倉為二手,達到盈利預期值時平倉一手,等回落后加倉一手
Params
Numeric Length1(10); // 10日均線的參數值
Numeric Length2(20); // 20日均線的參數值
Numeric Lots(2); // 默認的交易數量
Vars
NumericSeries MA1;
NumericSeries MA2;
Numeric MinPoint; // 一個最小變動單位,也就是一跳
Numeric AddSet(50); // 加倉設置
Numeric SubSet(50); // 減倉設置
NumericSeries FirstPrice; // 第一次開倉價格
NumericSeries LastPrice; // 最后一次開倉價格
boolSeries Condition1;
boolSeries Condition2;
Begin
MA1 = XAverage(Close,Length1);
MA2 = XAverage(Close,Length2);
MinPoint = MinMove*PriceScale();
Condition1 = CrossOver(MA1,MA2);
Condition2 = CrossUnder(MA1,MA2);
If(MarketPosition!=1) //空倉狀態時
{
if(condition1[1])// 當出現10日均線上穿20均線時,下一個bar開盤價買入
{
FirstPrice = Open;
LastPrice = FirstPrice;
Buy(lots,FirstPrice);
}
if(MarketPosition!=-1 and condition2[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價賣出
{
FirstPrice = Open;
LastPrice = FirstPrice;
Sellshort(Lots,Open);
}
}
if(MarketPosition==1) // 有多頭持倉的情況
{
If(condition2[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價賣出
{
Sellshort(Lots,Open);
}else if(CurrentContracts>1 and CurrentContracts<3 && High>= LastPrice+SubSet*MinPoint) // 止贏減倉
{
Sell(1,LastPrice+SubSet*MinPoint);
LastPrice =LastPrice + subSet*MinPoint;
}else if(CurrentContracts<2 && Low <=LastEntryPrice - AddSet*MinPoint) // 逢低加倉
{
buy(1,LastPrice- AddSet*MinPoint);
LastPrice = LastPrice - addSet*MinPoint;
}
}
if(MarketPosition==-1);// 有空頭持倉的情況
{
If(condition1[1]) // 當出現10日均線下穿20均線時,下一個bar開盤價平倉并買入
{
Sellshort(Lots,Open);
}Else if(CurrentContracts>1 and CurrentContracts<3 && Low <= LastEntryPrice - subSet*MinPoint) // 止贏減倉
{
BuyToCover(1,LastPrice);
LastPrice = LastPrice - subSet*MinPoint;
}else if( CurrentContracts<2 && High >=LastPrice + addSet*MinPoint) // 逢高加倉
{
sell(1,LastPrice);
LastPrice = LastPrice + AddSet*MinPoint;
}
}
End - TB客服:
謝謝,不過好像還是沒達到想要的效果
- 網友回復:
回復 3# 青木
您的效果是想要怎樣的?
您可以將各個條件用commentary輸出查看你的條件,一步一步看就知道為什么不是你想要的效果了。
commentary的各種使用方法請看:http://tradeblazer.net/forum/vie ... B%E4%BD%BF%E7%94%A8
或者參考幫助文檔中相關函數說明 - 網友回復:
回復 4# lh948
這個建議非常好,謝謝!
我按照你的方法把公式調整了,效果出來了。不過奇怪的是有多單時符合預期效果,但惟獨有空單時不能止贏和補倉,只能平倉反手。
多單和空單的代碼是同理的,為何效果不同捏?
以下是我修改會的代碼,請幫忙看看:
// 以下為均線交易系統,10日均線上穿20均線為買入,10日均線下穿20均線為買出
Params
Numeric Length1(10); // 10日均線的參數值
Numeric Length2(20); // 20日均線的參數值
Numeric Lots(2); // 默認的交易數量
Vars
NumericSeries MA1;
NumericSeries MA2;
Numeric MinPoint(1); // 一個最小變動單位,也就是一跳
Numeric AddSet(20); // 加倉設置
Numeric SubSet(20); // 減倉設置
NumericSeries myFirstPrice; // 第一次開倉價格
NumericSeries myLastPrice; // 最后一次開倉價格
boolSeries Condition1;
boolSeries Condition2;
Begin
MA1 = XAverage(Close,Length1);
MA2 = XAverage(Close,Length2);
MinPoint = MinMove*PriceScale;
Condition1 = CrossOver(MA1,MA2);
Condition2 = CrossUnder(MA1,MA2);
If(MarketPosition==0 )
{
if(condition1) //空倉狀態時出現10日均線上穿20均線,當前bar收盤價買入
{
myFirstPrice = Close;
myLastPrice = myFirstPrice;
Buy(lots,myFirstPrice);
Commentary("空倉狀態下,上穿20均線時,當前bar收盤價買入");
}Else if(condition2) // 空倉狀態時出現10日均線下穿20均線時,當前bar收盤價賣出
{
myFirstPrice = Close;
myLastPrice = myFirstPrice;
Sellshort(Lots,myFirstPrice);
Commentary("空倉狀態下,下穿20均線時,當前bar收盤價賣出");
}
}Else if(MarketPosition==1 )
{
If(condition2) // 有多單持倉,出現10日均線下穿20均線時,當前bar收盤價賣出
{
myLastPrice = Close;
Sellshort(Lots,myLastPrice);
Commentary("多單平倉并反手");
}Else if(CurrentContracts==2 && High>= myLastPrice+SubSet*MinPoint) // 止贏減倉
{
Sell(1,myLastPrice+SubSet*MinPoint);
Commentary("多單止贏1手");
myLastPrice =myLastPrice + subSet*MinPoint;
}Else if(CurrentContracts==1 && Low <=MA1) // 逢低加倉
{
buy(1,IntPart(MA1));
Commentary("多單補倉1手");
myLastPrice = IntPart(MA1);
}
}Else If(MarketPosition==-1 )
{
if(condition1)// 有空單持倉,出現10日均線上穿20均線時,當前bar收盤價平倉并買入
{
myLastPrice = Close;
Buy(Lots,myLastPrice);
Commentary("空單平倉并反手");
}Else if(CurrentContracts==2 && Low<=myLastPrice-SubSet*MinPoint) // 止贏減倉
{
BuyToCover(1,myLastPrice-SubSet*MinPoint);
Commentary("空單止贏1手");
myLastPrice=myLastPrice-SubSet*MinPoint;
}Else if(CurrentContracts==1 && High>=MA1) // 逢高加倉
{
sell(1,IntPart(MA1));
Commentary("空單補倉1手");
myLastPrice = IntPart(MA1);
}
}
End
相關文章
-
沒有相關內容