最后由 汉声周经理 于 2021-5-31 11:51 编辑
经典策略编写双均线策略很简单,两根均线,一根长期一根短期,交金叉(短期上穿长期)了我们就平空做多,交死叉(短期下穿长期)了我们就平多做空。这里我们选则25均线和100均线来做,先把经典的策略写出来,代码如下:
- int bar=0;
- void OnTick()
- {
- int i;
- string sym= Symbol();
- double lot=0.1;
- int ticketop;
- int ticketcl;
- int huadian=10;
- int peri= PERIOD_CURRENT;
- int mag=1333;
- if(bar==Bars)
- {
- return;
- }
- bar= Bars;
- if(iMA (sym,peri,25,0, MODE_EMA, PRICE_CLOSE,1)>
- iMA (sym,peri,100,0, MODE_EMA, PRICE_CLOSE,1) &&
- iMA (sym,peri,25,0, MODE_EMA, PRICE_CLOSE,2)<
- iMA (sym,peri,100,0, MODE_EMA, PRICE_CLOSE,2))
- {
- for(i=OrdersTotal()-1;i>=0;i--)
- {
- if(OrderSelect(i,SELECT_BY_POS))
- {
- if(OrderSymbol()==sym && OrderType()==OP_SELL &&
- OrderMagicNumber ()==mag)
- {
- ticketcl= OrderClose (OrderTicket(),
- OrderLots(),MarketInfo(sym,MODE_ASK),huadian);
- }
- }
- }
- ticketop= OrderSend(sym,OP_BUY,lot, MarketInfo(sym,
- MODE_ASK),huadian, 0,0,”Lesson 7”,mag,0,clrRed);
- }
- if(iMA (sym,peri,25,0, MODE_EMA, PRICE_CLOSE,1)<
- iMA (sym,peri,100,0, MODE_EMA, PRICE_CLOSE,1) &&
- iMA (sym,peri,25,0, MODE_EMA, PRICE_CLOSE,2)>
- iMA (sym,peri,100,0, MODE_EMA, PRICE_CLOSE,2))
- {
- for(i=OrdersTotal()-1;i>=0;i--)
- {
- if(OrderSelect(i,SELECT_BY_POS))
- {
- if(OrderSymbol()==sym && OrderType()==OP_BUY &&
- OrderMagicNumber ()==mag)
- {
- ticketcl= OrderClose (OrderTicket(),
- OrderLots(),MarketInfo(sym,MODE_BID),huadian);
- }
- }
- }
- ticketop= OrderSend(sym,OP_SELL,lot, MarketInfo(sym,
- MODE_BID),huadian, 0,0,”Lesson 7”,mag,0,clrRed);
- }
- }
复制代码
|