last commit

This commit is contained in:
Nkondog Anselme
2021-12-30 22:12:56 +01:00
parent 8afa56f9cb
commit cd9327b295
7 changed files with 78 additions and 68 deletions
Binary file not shown.
+71 -22
View File
@@ -9,7 +9,7 @@
// Moving Average grid strategy // Moving Average grid strategy
/* /*
Set pending orders x poinst above and below price. Set pending orders x point above and below price.
If price above SMA, buy and set buy orders x time the ATR above and below price. If price above SMA, buy and set buy orders x time the ATR above and below price.
If price below SMA, sell and set sell orders x time the ATR above and below price. If price below SMA, sell and set sell orders x time the ATR above and below price.
Close all position at the close of the first candle crossing the moving average. Close all position at the close of the first candle crossing the moving average.
@@ -25,13 +25,13 @@ CiATR* atr;
//#include <DL_ErrorHandling.mqh> // Error library //#include <DL_ErrorHandling.mqh> // Error library
//#include <Nkanven\MAGrid\PreChecks.mqh> // Prechecks //#include <Nkanven\MAGrid\PreChecks.mqh> // Prechecks
//#include <Nkanven\MAGrid\TradingHour.mqh> // //#include <Nkanven\MAGrid\TradingHour.mqh> //
//#include <Trade\Trade.mqh> #include <Trade\Trade.mqh>
#include <Nkanven\MAGrid\ScanPositions.mqh> // Scan for opened positions #include <Nkanven\MAGrid\ScanPositions.mqh> // Scan for opened positions
//#include <Nkanven\MAGrid\CheckHistory.mqh> //Check transaction history //#include <Nkanven\MAGrid\CheckHistory.mqh> //Check transaction history
//#include <Nkanven\MAGrid\TradeManager.mqh> //Manage trade dynamic open and close conditions //#include <Nkanven\MAGrid\TradeManager.mqh> //Manage trade dynamic open and close conditions
#include <Nkanven\MAGrid\EntriesManager.mqh> // Check buy and sell entries signals and execute them #include <Nkanven\MAGrid\EntriesManager.mqh> // Check buy and sell entries signals and execute them
#include <Nkanven\MAGrid\LotSizeCal.mqh> // Lot size calculate #include <Nkanven\MAGrid\LotSizeCal.mqh> // Lot size calculate
//#include <Nkanven\MAGrid\ClosePositions.mqh> // Close opened positions #include <Nkanven\MAGrid\CloseTransactions.mqh> // Close opened positions
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -63,37 +63,86 @@ void OnTick()
{ {
//--- //---
SymbolInfoTick(_Symbol,last_tick); SymbolInfoTick(_Symbol,last_tick);
//Get technical indicators values
ma.Refresh(-1); ma.Refresh(-1);
gMa = ma.Main(1); gMa = ma.Main(1);
atr.Refresh(-1); atr.Refresh(-1);
gAtr = atr.Main(1); gAtr = atr.Main(1);
//Initial position scanning
ScanPositions(); ScanPositions();
Print("Total transaction ", gTotalTransactions);
if(gTotalTransactions>0)
return;
CheckSpread(); Print("Price is below SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa, " Total buy ", gTotalBuyPositions);
EvaluateEntry();
ExecuteEntry();
}
//+------------------------------------------------------------------+
//Check and return if the spread is not too high //Check closing signal
void CheckSpread()
{ //Close all buy position and orders if price is below MA
//Get the current spread in points, the (int) transforms the double coming from MarketInfo into an integer to avoid a warning when compiling if(iClose(gSymbol, PERIOD_CURRENT, 1) < gMa && gTotalTransactions > 0)
long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD);
Print("Spread ", SpreadCurr);
if(SpreadCurr<=InpMaxSpread)
{ {
gIsSpreadOK=true; Print("Price is below SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa);
CloseTransactions(SIGNAL_EXIT_BUY);
} }
else else
{ {
gIsSpreadOK=false; //Close all sell positions and orders if price is above MA
if(iClose(gSymbol, PERIOD_CURRENT, 1) > gMa && gTotalTransactions > 0)
{
Print("Price is above SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa);
CloseTransactions(SIGNAL_EXIT_SELL);
}
}
//Rescan positions
ScanPositions();
Print("Total transaction ", gTotalTransactions, " gTotalBuyPositions ", gTotalBuyPositions);
//Do not open positions if there are positions or orders pending
if(gTotalTransactions>0)
{
//If there's no position, close all pending orders
if(gTotalBuyPositions == 0 && gTotalTransactions > 0)
{
Print("Delete all");
CloseTransactions(SIGNAL_EXIT_ALL);
}
else
{
if(gTotalSellPositions==0 && gTotalTransactions >0)
{
CloseTransactions(SIGNAL_EXIT_ALL);
}
else
{
return;
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CheckSpread();
EvaluateEntry();
ExecuteEntry();
}
//+------------------------------------------------------------------+
//Check and return if the spread is not too high
void CheckSpread()
{
//Get the current spread in points, the (int) transforms the double coming from MarketInfo into an integer to avoid a warning when compiling
long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD);
Print("Spread ", SpreadCurr);
if(SpreadCurr<=InpMaxSpread)
{
gIsSpreadOK=true;
}
else
{
gIsSpreadOK=false;
}
} }
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
-38
View File
@@ -1,38 +0,0 @@
//+------------------------------------------------------------------+
//| ClosePositions.mqh |
//| Copyright 2021, Nkondog Anselme Venceslas |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
CTrade trade;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool OrderClose()
{
bool result = true;
int cnt = OrdersTotal();
if(cnt == 1)
{
for(int i = cnt-1; i>=0; i--)
{
ulong ticket = OrderGetTicket(i);
if(OrderSelect(ticket))
{
result &= trade.OrderDelete(ticket);
}
else
{
result = false;
}
}
}
return(result);
}
//+------------------------------------------------------------------+
Binary file not shown.
+3 -4
View File
@@ -17,16 +17,15 @@ void LotSizeCalculate(double SL=0)
if(SL!=0) if(SL!=0)
{ {
double RiskBaseAmount=0; double RiskBaseAmount=0;
double RiskBase=0;
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty //TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
double TickValue=SymbolInfoDouble(gSymbol,SYMBOL_TRADE_TICK_VALUE); double TickValue=SymbolInfoDouble(gSymbol,SYMBOL_TRADE_TICK_VALUE);
//Define the base for the risk calculation depending on the parameter chosen //Define the base for the risk calculation depending on the parameter chosen
if(RiskBase==RISK_BASE_BALANCE) if(InpRiskBase==RISK_BASE_BALANCE)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE); RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
if(RiskBase==RISK_BASE_EQUITY) if(InpRiskBase==RISK_BASE_EQUITY)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY); RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
if(RiskBase==RISK_BASE_FREEMARGIN) if(InpRiskBase==RISK_BASE_FREEMARGIN)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN); RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
//Calculate the Position Size //Calculate the Position Size
+3 -3
View File
@@ -93,15 +93,15 @@ struct LastTransaction
// Input Section // Input Section
// //
// Fast moving average // Fast moving average
input int InpFastPeriods = 10; // Fast periods input int InpFastPeriods = 200; // Fast periods
input ENUM_MA_METHOD InpFastMethod = MODE_SMA; // Fast method input ENUM_MA_METHOD InpFastMethod = MODE_SMA; // Fast method
input ENUM_APPLIED_PRICE InpFastAppliedPrice = PRICE_CLOSE; // Fast price input ENUM_APPLIED_PRICE InpFastAppliedPrice = PRICE_CLOSE; // Fast price
// Slow moving average /* Slow moving average
input int InpSlowPeriods = 20; // Slow periods input int InpSlowPeriods = 20; // Slow periods
input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method
input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price
*/
input int InpAtrPeriod = 14; // ATR period input int InpAtrPeriod = 14; // ATR period
// Bar numbers for comparison // Bar numbers for comparison
//input int InpBar2 = 2; // Base bar number //input int InpBar2 = 2; // Base bar number
+1 -1
View File
@@ -41,7 +41,7 @@ bool ScanPositions()
//If it is a sell order then increment the total count of sell orders //If it is a sell order then increment the total count of sell orders
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
gTotalSellPositions++; gTotalSellPositions++;
Print("POSITION_TYPE_BUY ", POSITION_TYPE_BUY, " POSITION_TYPE_SELL ", POSITION_TYPE_SELL, " PositionGetInteger(POSITION_TYPE) ", PositionGetInteger(POSITION_TYPE));
//Find what is the open time of the most recent trade and assign it to LastBarTraded //Find what is the open time of the most recent trade and assign it to LastBarTraded
//this is necessary to check if we already traded in the current candle //this is necessary to check if we already traded in the current candle
if((datetime)PositionGetInteger(POSITION_TIME)>gLastBarTraded || gLastBarTraded==NULL) if((datetime)PositionGetInteger(POSITION_TIME)>gLastBarTraded || gLastBarTraded==NULL)