Open positions

This commit is contained in:
Nkondog Anselme
2022-04-01 02:08:33 +01:00
parent 1368e4a83f
commit 816c35632b
8 changed files with 36 additions and 70 deletions
Binary file not shown.
+16 -5
View File
@@ -25,7 +25,7 @@ const int indexColor = 1;
int OnInit()
{
//---
handle = iCustom(gSymbol, PERIOD_CURRENT, "MA-Slope", InpPeriods, InpMethod, InpAppliedPrice);
handle = iCustom(gSymbol, PERIOD_CURRENT, "Nkanven\MA-Slope", InpPeriods, InpMethod, InpAppliedPrice);
if(handle == INVALID_HANDLE)
{
@@ -49,10 +49,18 @@ void OnDeinit(const int reason)
void OnTick()
{
//---
TimeCurrent(dt);
SymbolInfoTick(_Symbol,last_tick);
CheckPreChecks();
if(!gIsPreChecksOk)
return;
ScanPositions();
if(!newBar())
return;
//int maSlope = iCustom(Symbol(), PERIOD_CURRENT, "MA-Slope", 1000, MODE_SMA, PRICE_CLOSE);
int cnt = CopyBuffer(handle, indexMA, 0, 3, bufferMA);
if(cnt<3)
@@ -61,10 +69,13 @@ void OnTick()
currentMA = bufferMA[1];
currentColor = bufferColor[1];
priorMA = bufferMA[0];
priorColor = bufferColor[0];
Print("currentMA ", currentMA, " currentColor ", currentColor, " priorMA ", priorMA, " priorColor");
Print("Signal ", tradeSignal(), " Stop loss ", getAutoStopLoss(tradeSignal()));
Print("currentMA ", currentMA, " currentColor ", currentColor);
CloseTransactions();
ExecuteEntry();
}
//+------------------------------------------------------------------+
@@ -15,8 +15,6 @@ void CloseTransactions()
{
bool result = true;
int cts = PositionsTotal();
ulong theTicket;
double positionProfit = 0.0;
if(cts > 0)
{
@@ -27,10 +25,16 @@ void CloseTransactions()
{
if(PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
{
if(positionProfit > PositionGetDouble(POSITION_PROFIT))
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && currentColor == 1.0)
{
positionProfit = PositionGetDouble(POSITION_PROFIT);
theTicket = ticket;
if(!trade.PositionClose(ticket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && currentColor == 0.0)
{
if(!trade.PositionClose(ticket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
}
}
@@ -39,36 +43,8 @@ void CloseTransactions()
Print("Error (", GetLastError(), ") while selecting position by ticket");
}
}
if(gEmergencyClose)
{
if(!trade.PositionClose(theTicket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void drawdownWatcher()
{
double amountDiff, equity, balance, percentDiff;
balance=AccountInfoDouble(ACCOUNT_BALANCE);
equity=AccountInfoDouble(ACCOUNT_EQUITY);
gEmergencyClose = false;
if(equity < balance)
{
amountDiff = balance - equity;
percentDiff = (amountDiff*100)/balance;
Print("amountDiff ", amountDiff, " percentDiff ", percentDiff, " InpMaxDrawdown ", InpMaxDrawdown);
if(InpMaxDrawdown < percentDiff)
{
gEmergencyClose = true;
}
}
}
//+------------------------------------------------------------------+
Binary file not shown.
+9 -22
View File
@@ -35,21 +35,7 @@ enum ENUM_MODE_TP
TP_AUTO=1, //AUTOMATIC TAKE PROFIT
};
//Enumerative for the stop loss calculation
enum ENUM_MODE_SL_BY
{
SL_BY_POINTS=0, //STOP LOSS PASSED IN POINTS
SL_BY_PRICE=1, //STOP LOSS PASSED BY PRICE
};
//Enumerative for trading time
enum ENUM_MODE_TRADING_TIME
{
DAY_TRADING=0, //Day trade
NIGHT_TRADING=1, //Night trade
DAY_NIGHT_TRADING=2, //Both day & night trade
ALL_DAY_TRADING=3, //Round the clock
};
//Enumerative for trading time
enum ENUM_MODE_TRADE_SIGNAL
@@ -76,22 +62,24 @@ input int InpSlippage=1; //Maximu
input string Comment_01="----------------------"; //Stop loss settings
input ENUM_MODE_SL InpStopLossMode=SL_FIXED; //Stop Loss Mode
input int InpDefaultStopLoss=200; //Default Stop Loss In Points (0=No Stop Loss)
input int InpAutoStopLossCandlesAmount=10; //Auto SL amount of candles
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input double InpMaxDrawdown=2.5; //Max DD level
input bool InpDisableStopLoss=false; //Disable Stop Loss
input string Comment_02="----------------------"; //Take profit settings
input ENUM_MODE_TP InpTakeProfitMode=TP_FIXED; //Take Profit Mode
input int InpDefaultTakeProfit=60; //Default Take Profit In Points (0=No Take Profit)
input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points
input int InpMaxTakeProfit=5000; //Maximum Allowed Take Profit In Points
input double InpTakeProfitPercent=1.0; //Take Profit percent on risk base
input double InpTakeProfitPercent=1.0; //Take Profit Multiplier
input string Comment_1="----------------------"; // Moving average slope settings
input string Comment_1="----------------------"; // Moving average slope settings
input int InpPeriods = 1000; // Periods
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Method
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Price
input string Comment_2="----------------------";
input string Comment_2="----------------------"; // Others
input int InpPriceToMaMarging=100; //Price to MA marging
input string InpComment = __FILE__; //Default trade comment
input int InpMagicNumber = 198901; //Magic Number
@@ -101,14 +89,13 @@ input int InpMagicNumber = 198901; //Magic Number
string gSymbol = Symbol();
double bufferMA[3];
double bufferColor[3];
double currentMA, currentColor, priorMA, priorColor;
double currentMA, currentColor;
int gTotalSellPositions, gTotalBuyPositions, gTotalPositions;
bool gIsOperatingHours=false;
bool gIsPreChecksOk=false; //Indicates if the pre checks are satisfied
bool gIsSpreadOK=false; //Indicates if the spread is low enough to trade
bool IsSpreadOK=false;
bool gEmergencyClose=false; //Urgently close losing trade
bool IsSpreadOK=false; //Urgently close losing trade
double gLotSize=InpDefaultLotSize;
@@ -17,13 +17,6 @@ void CheckPreChecks()
Print("Live Trading is not enabled, please enable it in Metatrader and chart settings");
return;
}
//Trading period verification
if(!gIsOperatingHours)
{
gIsPreChecksOk=false;
Print("Out of trading hours");
return;
}
//Check if the default stop loss you are setting in above the minimum and below the maximum
if(InpDefaultStopLoss<InpMinStopLoss || InpDefaultStopLoss>InpMaxStopLoss)
{
@@ -68,7 +61,6 @@ void CheckPreChecks()
}
//Spread is acceptable
long SpreadCurr=(int)Spread;
Print("Spread ", Spread);
if(SpreadCurr>InpMaxSpread)
{
gIsPreChecksOk=false;
@@ -39,7 +39,7 @@ void ScanPositions()
//If it is a sell order then increment the total count of sell orders
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
gTotalSellPositions++;
Print("POSITION_TYPE_BUY ", POSITION_TYPE_BUY, " POSITION_TYPE_SELL ", POSITION_TYPE_SELL, " PositionGetInteger(POSITION_TYPE) ", PositionGetInteger(POSITION_TYPE));
//Print("POSITION_TYPE_BUY ", POSITION_TYPE_BUY, " POSITION_TYPE_SELL ", POSITION_TYPE_SELL, " PositionGetInteger(POSITION_TYPE) ", PositionGetInteger(POSITION_TYPE));
}
Print("Total positions ", gTotalPositions, " - Total buys ", gTotalBuyPositions, " - Total sells ", gTotalSellPositions);
//Print("Total positions ", gTotalPositions, " - Total buys ", gTotalBuyPositions, " - Total sells ", gTotalSellPositions);
}
Binary file not shown.