Created
November 9, 2020 07:54
-
-
Save igotit-anything/2f71bc90591c52ecb1289cfbd1aacb9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| CyManager_Posi.mqh | | |
//| igotit | | |
//| https://www.mql5.com | | |
//+------------------------------------------------------------------+ | |
#property copyright "igotit" | |
#property link "https://www.mql5.com" | |
#property version "1.00" | |
#include <Trade\PositionInfo.mqh> | |
#include <Generic\HashMap.mqh> | |
#include <Arrays\ArrayLong.mqh> | |
class CCyManagerPosi | |
{ | |
private: | |
string m_strSymbol; // 생성자에서 전달방은 심볼 1개의 포지션만 관리함. | |
CPositionInfo m_CPosiInfo; // 포지션 정보 확보 편리. | |
CHashMap<long, int> map_PosiSymbol; //심볼1개의 포지션 티켓을 저장용 맵. key : ticket of position. value : not used. 임의 값 기록해둬도 됨. | |
void Proc_Check_Posi_CreateDelete(string str_symbol); | |
public: | |
CCyManagerPosi(string str_symbol); | |
~CCyManagerPosi(); | |
void OnTrade_CyManagerPosi(); // OnTrade 에서 호출. | |
}; | |
CCyManagerPosi::CCyManagerPosi(string str_symbol) | |
{ | |
m_strSymbol = str_symbol; | |
map_PosiSymbol.Clear(); | |
Proc_Check_Posi_CreateDelete(str_symbol); // 클래스 개체 생성시점 1회만 하면됨. OnInit 에서 호출할 필요없음. | |
} | |
CCyManagerPosi::~CCyManagerPosi() | |
{ | |
} | |
void CCyManagerPosi::OnTrade_CyManagerPosi() | |
{ | |
Proc_Check_Posi_CreateDelete(m_strSymbol); | |
} | |
void CCyManagerPosi::Proc_Check_Posi_CreateDelete(string str_symbol) | |
{ | |
int num_posi_total = PositionsTotal(); // 전종목의 포지션수량. | |
CHashMap<long,int> map_temp_posi_symbol; | |
for(int idx_posi=0; idx_posi<num_posi_total; idx_posi++) | |
{ | |
m_CPosiInfo.SelectByIndex(idx_posi); | |
if(m_CPosiInfo.Symbol() == str_symbol) | |
{ | |
/// check for creation position | |
long ticket = m_CPosiInfo.Ticket(); | |
if(map_PosiSymbol.ContainsKey(ticket)) // map 에 기등록 상태. | |
{ | |
// Print("already exist map"); | |
} | |
else // map 에 없음. 신규 포지션 생성된것 | |
{ | |
// TO DO : Do Something when position created. | |
Print("Position. add to map for Symbol : " + str_symbol);// test | |
map_PosiSymbol.Add(ticket,1);// map 에 추가. value에는 아무거나 기록해도됨 활용하지 않음. | |
} | |
map_temp_posi_symbol.Add(ticket,1); // 현재 심볼의 모든 포지션을 임시 map 에 모두 저장해둠. 아래 포지션 제거된것 확인용. | |
} // if symbol | |
} // for | |
/// check for deleted position | |
int num_posi_map = map_PosiSymbol.Count(); | |
if(num_posi_map > map_temp_posi_symbol.Count()) | |
{ | |
/// search for deleted position ticket. | |
long keys[]; | |
int values[]; | |
CArrayLong arr_keys_deleted; | |
map_PosiSymbol.CopyTo(keys, values); | |
int count_deleted=0; | |
for(int idx_posi=0; idx_posi < num_posi_map; idx_posi++) | |
{ | |
if( map_temp_posi_symbol.ContainsKey(keys[idx_posi]) == false) // 포지션 제거되었음. | |
{ | |
arr_keys_deleted.Add(keys[idx_posi]); | |
}// if | |
}// for | |
// 제거된 포지션들 map 에서도 제거. | |
for(int idx = 0; idx<arr_keys_deleted.Total();idx++) | |
{ | |
// TO DO : Do Something when position deleted. | |
Print("Position Deleted : " + arr_keys_deleted.At(idx)); | |
map_PosiSymbol.Remove(arr_keys_deleted.At(idx)); | |
} | |
} | |
} | |
///////////////// example ///////////////////////////////////////////////// | |
#include "CyManagerPosi.mqh" | |
CCyManagerPosi m_CyManagerPosi(_Symbol); // ea 배치된 챠트의 심볼 1개의 포지션 관리. | |
void OnTrade() | |
{ | |
m_CyManagerPosi.OnTrade_CyManagerPosi(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://igotit.tistory.com/2692