-
-
Save guchimon99/6c32b2fd453cc9abce061fb916e86cd0 to your computer and use it in GitHub Desktop.
IMA を用いた自動取引
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
//+------------------------------------------------------------------+ | |
//| MyFirstAdovisor.mq5 | | |
//| guchimon99 | | |
//| https://twitter.com/guchimon99 | | |
//+------------------------------------------------------------------+ | |
#property copyright "guchimon99" | |
#property link "https://twitter.com/guchimon99" | |
#property version "1.00" | |
#include <Trade\Trade.mqh>; | |
#include <Trade\PositionInfo.mqh> | |
int ima_handle; | |
double ima_buffers[]; | |
double close_buffers[]; | |
double c_volume; | |
string c_symbol; | |
ENUM_TIMEFRAMES c_timeframe; | |
CTrade trade; | |
CPositionInfo position; | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int OnInit() | |
{ | |
c_symbol = _Symbol; | |
c_timeframe = PERIOD_CURRENT; | |
c_volume = 0.1; | |
int period = 40; | |
int shift = 0; | |
ENUM_MA_METHOD mode = MODE_SMA; | |
ENUM_APPLIED_PRICE close = PRICE_CLOSE; | |
ima_handle = iMA(c_symbol, c_timeframe, period, shift, mode, close); | |
if(ima_handle == INVALID_HANDLE) | |
{ | |
Print("Failed to get the indicator handle"); | |
return(INIT_FAILED); | |
} | |
ChartIndicatorAdd(ChartID(), 0, ima_handle); | |
ArraySetAsSeries(ima_buffers, true); | |
ArraySetAsSeries(close_buffers, true); | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
IndicatorRelease(ima_handle); | |
ArrayFree(ima_buffers); | |
ArrayFree(close_buffers); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
int error_ima = CopyBuffer(ima_handle, 0, 1, 2, ima_buffers); | |
int error_close = CopyClose(c_symbol, c_timeframe, 1, 2, close_buffers); | |
if(error_ima < 0 || error_close < 0) | |
{ | |
Print("Failed to copy data from the indicator buffer or price chart buffer"); | |
return; | |
} | |
bool buy_signal = ima_buffers[1] > close_buffers[1] && ima_buffers[0] < close_buffers[0]; | |
bool sell_signal = ima_buffers[1] < close_buffers[1] && ima_buffers[0] > close_buffers[0]; | |
bool has_position = position.Select(c_symbol); | |
ENUM_POSITION_TYPE c_position_type ; | |
if(buy_signal) | |
{ | |
if(has_position) | |
{ | |
c_position_type = position.PositionType(); | |
if(c_position_type == POSITION_TYPE_SELL) | |
trade.PositionClose(c_symbol); | |
if(c_position_type == POSITION_TYPE_BUY) | |
return; | |
} | |
trade.Buy(c_volume, c_symbol); | |
} | |
if(sell_signal) | |
{ | |
if(has_position) | |
{ | |
c_position_type = position.PositionType(); | |
if(c_position_type == POSITION_TYPE_BUY) | |
trade.PositionClose(c_symbol); | |
if(c_position_type == POSITION_TYPE_SELL) | |
return; | |
} | |
trade.Sell(c_volume, c_symbol); | |
} | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment