-
-
Save guchimon99/6dab8b0fac328b5a743ee0caa98bc338 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
//+------------------------------------------------------------------+ | |
//| NanmpinMaru_v1.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> | |
CTrade trade; | |
CPositionInfo position; | |
string c_symbol; // symbol | |
ENUM_TIMEFRAMES c_period; // period | |
int refernce_count; // number to refer for price difference | |
double close_prices[]; | |
double diff_price; // price difference | |
ENUM_POSITION_TYPE c_position_type; | |
double last_order_price; | |
double last_price; | |
double settle_price; | |
double total_order_volume; | |
double first_volume; | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int OnInit() | |
{ | |
c_symbol = _Symbol; | |
c_period = PERIOD_M15; | |
c_position_type = POSITION_TYPE_BUY; | |
first_volume = 1.0; | |
refernce_count = 100; | |
ArraySetAsSeries(close_prices, true); | |
total_order_volume = 0; | |
update_diff_price(); | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
//--- | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
update_last_price(); | |
if(shuold_start()) | |
{ | |
start(); | |
return; | |
} | |
if(should_average()) | |
{ | |
average(); | |
return; | |
} | |
if(should_settle()) | |
{ | |
settle(); | |
return; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| Get shuold start | | |
//+------------------------------------------------------------------+ | |
bool shuold_start() | |
{ | |
return !position.Select(c_symbol); | |
} | |
//+------------------------------------------------------------------+ | |
//| Get shuold average | | |
//+------------------------------------------------------------------+ | |
bool should_average() | |
{ | |
return c_position_type == POSITION_TYPE_BUY ? ( | |
last_price < last_order_price - diff_price * 0.05 | |
) : ( | |
last_price > last_order_price + diff_price * 0.05 | |
); | |
} | |
//+------------------------------------------------------------------+ | |
//| Get shuold settle | | |
//+------------------------------------------------------------------+ | |
bool should_settle() | |
{ | |
return c_position_type == POSITION_TYPE_BUY ? ( | |
last_price >= settle_price | |
) : ( | |
last_price <= settle_price | |
); | |
} | |
//+------------------------------------------------------------------+ | |
//| Start | | |
//+------------------------------------------------------------------+ | |
void start() | |
{ | |
double order_volume = first_volume; | |
if(c_position_type == POSITION_TYPE_BUY) | |
{ | |
trade.Buy(order_volume, c_symbol); | |
} | |
else | |
{ | |
trade.Sell(order_volume, c_symbol); | |
} | |
update_diff_price(); | |
position.Select(c_symbol); | |
last_order_price = position.PriceOpen(); | |
total_order_volume = first_volume; | |
settle_price = last_order_price + (c_position_type == POSITION_TYPE_BUY ? 1 : -1) * diff_price * 0.01; | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| Average | | |
//+------------------------------------------------------------------+ | |
void average() | |
{ | |
double order_volume = total_order_volume / 2; | |
if(c_position_type == POSITION_TYPE_BUY) | |
{ | |
trade.Buy(order_volume, c_symbol); | |
} | |
else | |
{ | |
trade.Sell(order_volume, c_symbol); | |
} | |
position.Select(c_symbol); | |
last_order_price = position.PriceOpen(); | |
total_order_volume += order_volume; | |
} | |
//+------------------------------------------------------------------+ | |
//| Settle | | |
//+------------------------------------------------------------------+ | |
void settle() | |
{ | |
while(position.Select(c_symbol)) | |
{ | |
trade.PositionClose(c_symbol); | |
} | |
c_position_type = c_position_type == POSITION_TYPE_BUY ? POSITION_TYPE_SELL : POSITION_TYPE_BUY; | |
} | |
//+------------------------------------------------------------------+ | |
//| Update last price function | | |
//+------------------------------------------------------------------+ | |
void update_last_price() | |
{ | |
MqlTick last_tick; | |
if(!SymbolInfoTick(Symbol(),last_tick)) | |
Print("SymbolInfoTick() failed, error = ",GetLastError()); | |
last_price =last_tick.bid; | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| Update price diff function | | |
//+------------------------------------------------------------------+ | |
void update_diff_price() | |
{ | |
if(CopyClose(c_symbol, c_period, 0, 100, close_prices) == -1) | |
{ | |
Print(GetLastError()); | |
return; | |
} | |
double max_price = close_prices[ArrayMaximum(close_prices, 0, WHOLE_ARRAY)]; | |
double min_price = close_prices[ArrayMinimum(close_prices, 0, WHOLE_ARRAY)]; | |
diff_price = max_price - min_price; | |
return; | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment