Skip to content

Instantly share code, notes, and snippets.

@guchimon99
Created December 11, 2020 06:02
Show Gist options
  • Save guchimon99/145baf72df64347c8a686882c094a993 to your computer and use it in GitHub Desktop.
Save guchimon99/145baf72df64347c8a686882c094a993 to your computer and use it in GitHub Desktop.
途中段階のナンピン丸 v1
//+------------------------------------------------------------------+
//| 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[]; // close prices
double diff_price; // price difference
double max_price; // max price
double min_price; // min price
ENUM_POSITION_TYPE current_position_type;
double last_order_price;
MqlTick last_tick;
double last_price;
double settle_price;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
c_symbol = _Symbol;
c_period = PERIOD_M15;
current_position_type = POSITION_TYPE_BUY;
refernce_count = 100;
ArraySetAsSeries(close_prices, true);
update_diff_price();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
last_price = get_last_price();
bool shuold_start = get_shuold_start();
bool should_average = get_shuold_average();
bool should_settle = get_shuold_settle();
if(!shuold_start)
{
start();
return;
}
if(should_average)
{
average();
return;
}
if(should_settle)
{
settle();
return;
}
}
//+------------------------------------------------------------------+
//| Update price diff function |
//+------------------------------------------------------------------+
void update_diff_price()
{
if(CopyClose(c_symbol, c_period, 0, 100, close_prices) == -1)
{
Print(GetLastError());
return;
}
max_price = close_prices[ArrayMaximum(close_prices, 0, WHOLE_ARRAY)];
min_price = close_prices[ArrayMinimum(close_prices, 0, WHOLE_ARRAY)];
diff_price = max_price - min_price;
Print("diff price = ", diff_price);
}
//+------------------------------------------------------------------+
//| Get shuold start |
//+------------------------------------------------------------------+
bool get_shuold_start()
{
bool shuold_start = !position.Select(c_symbol);
return shuold_start;
}
//+------------------------------------------------------------------+
//| Get shuold average |
//+------------------------------------------------------------------+
bool get_shuold_average()
{
bool should_average = (
(
current_position_type == POSITION_TYPE_BUY
&& last_price < last_order_price - diff_price * 0.05
) || (
current_position_type == POSITION_TYPE_SELL
&& last_price > last_order_price + diff_price * 0.05
)
);
return should_average;
}
//+------------------------------------------------------------------+
//| Get shuold settle |
//+------------------------------------------------------------------+
bool get_shuold_settle()
{
bool shuold_settle = (
(
current_position_type == POSITION_TYPE_BUY
&& last_price >= settle_price
) || (
current_position_type == POSITION_TYPE_SELL
&& last_price <= settle_price
)
);
return shuold_settle;
}
//+------------------------------------------------------------------+
//| Start |
//+------------------------------------------------------------------+
void start()
{
update_diff_price();
last_order_price = last_price;
switch(current_position_type) {
case POSITION_TYPE_BUY:
trade.Buy(0.001);
settle_price = last_price + diff_price * 0.01;
break;
case POSITION_TYPE_SELL:
trade.Sell(0.001);
settle_price = last_price - diff_price * 0.01;
break;
default:
break;
}
return;
}
//+------------------------------------------------------------------+
//| Average |
//+------------------------------------------------------------------+
void average()
{
}
//+------------------------------------------------------------------+
//| Settle |
//+------------------------------------------------------------------+
void settle()
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double get_last_price()
{
if(!SymbolInfoTick(Symbol(),last_tick))
Print("SymbolInfoTick() failed, error = ",GetLastError());
return last_tick.bid;
}
//+------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment