ナンピンでトレードするEA
ナンピンでトレードするEAを作成していきたいと思います。
- RSIの逆張りトレード
- チャートが逆行した場合にナンピンする
メタエディタ(MetaEditor)を立ち上げる
メタエディタ(MetaEditor)を立ち上げましょう。今回は名前を「Nampin-EA」で作成します。
パラメータを記述する
パラメータを記述しましょう。
input int magic = 5;//マジックナンバー input double lots = 0.1;//ロット input double slippage = 3;//スリッページ input int RSI_period = 14;//RSIの期間 input double nampin = 1000;//ナンピン input int maxnampin = 5;//最大ナンピン数
Copy
グローバル変数を記述する
グローバル変数を記述しましょう。
Static int Ticket_Number;//チケットナンバー
Copy
OnTick()関数を記述
OnTick()関数を記述しましょう。
RSI_Entry()関数を記述する
allClose()関数を記述する
ソースコード全体
//+------------------------------------------------------------------+ //| Nampin-EA.mq4 | //| Copyright 2020, FX-EA System Project Creator | //| https://creator.fx-ea-system-project.com/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, FX-EA System Project Creator" #property link "https://creator.fx-ea-system-project.com/" #property version "1.00" #property strict /*パラメーター*/ input int magic = 5;//マジックナンバー input double lots = 0.1;//ロット input double slippage = 3;//スリッページ input int RSI_period = 14;//RSIの期間 input double nampin = 1000;//ナンピン input int maxnampin = 5;//最大ナンピン数 Static int Ticket_Number;//チケットナンバー //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int cnt; int CurrentPosition;//ポジション double profit;//決済 CurrentPosition=-1; for(cnt=0;cnt<OrdersTotal();cnt++) { if(OrderSelect(cnt,SELECT_BY_POS)==false)continue; if(OrderMagicNumber()!=magic)continue; profit=profit+OrderProfit(); if(OrderMagicNumber()==magic)CurrentPosition=cnt; } if(CurrentPosition==-1)//CurrentPositionが-1と等しい場合 { if(RSI_Entry(RSI_period)==1)//RSI_Entryの条件が1と等しい { Ticket_Number = OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,0,0,"buy",magic,0,Blue);//ロングエントリー } if(RSI_Entry(RSI_period)==2)//RSI_Entryの条件が2と等しい { Ticket_Number = OrderSend(Symbol(),OP_SELL,lots,Bid,slippage,0,0,"sell",magic,0,Red);//ショートエントリー } } else//CurrentPositionが-1と等しくなければ { OrderSelect(CurrentPosition,SELECT_BY_POS); if(OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { if(OrdersTotal()<(maxnampin+1)&&Close[0]<(OrderOpenPrice()-nampin*Point)) { Ticket_Number = OrderSend(Symbol(),OP_BUY,OrderLots(),Ask,slippage,0,0,"buy2",magic,0,Blue); } if(profit>10) { allClose(Green); } } if(OrderType()==OP_SELL) { if(OrdersTotal()<(maxnampin+1)&&Close[0]>(OrderOpenPrice()+nampin*Point)) { Ticket_Number = OrderSend(Symbol(),OP_SELL,OrderLots(),Bid,slippage,0,0,"sell2",magic,0,Red); } if(profit>10) { allClose(Yellow); } } } } } //+------------------------------------------------------------------+ /*RSIロジック*/ int RSI_Entry(int period) { double RSI; RSI=iRSI(Symbol(),0,period,PRICE_CLOSE,0);//RSI if(RSI < 30)//RSIが30より小さい場合 { return(1);//1を返す } if(RSI > 70)//RSIが70より大きい場合 { return(2);//2を返す } return(0);//0を返す } /*ポジション決済*/ void allClose(color clr) { int i; for(i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)//ポジションを持っていなければ continue;//コンテニューする if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)//注文の通貨ペアと現在の通貨ペアが違うかマジックナンバーが該当しなければ continue;//コンテニューする if(OrderType()==OP_BUY)//ロングポジションがあれば OrderClose(OrderTicket(),OrderLots(),Bid,NormalizeDouble(slippage,0),clr);//ポジション決済 if(OrderType()==OP_SELL)//ショートポジションがあれば OrderClose(OrderTicket(),OrderLots(),Ask,NormalizeDouble(slippage,0),clr);//ポジション決済 } }
Copy
コメント