Adding a trailing Stop loss to an expert advisor using MQL5 - algorithmic-trading

Hi I'm new to MQL5 and I wanted to add a trailing stop loss to my expert advisor but some reason it does not add. Here is the code:
if(PositionSelect(_Symbol) && UseTrailingStop == true)
{
double TrailingStop = (atr[1] * 3) + close[1];
Trail.TrailingStop(_Symbol,TrailingStop,0,0);
}
Please note that close[1] is for the close price of the previous bar and atr[1] is for the value of the average true range. What am i doing wrong?????

There you go: hope this is helpful.
//--- trailing position
for(i=0;i<PositionsTotal();i++)
{
if(Symbol()==PositionGetSymbol(i))
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
sl=MathMax(PositionGetDouble(POSITION_PRICE_OPEN)+Spread*_Point,Bid-SL*_Point);
if(sl>PositionGetDouble(POSITION_SL) && (Bid-StopLevel*_Point-Spread*_Point)>PositionGetDouble(POSITION_PRICE_OPEN))
{
request.action = TRADE_ACTION_SLTP;
request.symbol = _Symbol;
request.sl = NormalizeDouble(sl,_Digits);
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request,result);
if(result.retcode==10009 || result.retcode==10008) // request executed
Print("Moving Stop Loss of Buy position #",request.order);
else
{
Print(ResultRetcodeDescription(result.retcode));
return;
}
return;
}
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
sl=MathMin(PositionGetDouble(POSITION_PRICE_OPEN)-Spread*_Point,Ask+SL*_Point);
if(sl<PositionGetDouble(POSITION_SL) && (PositionGetDouble(POSITION_PRICE_OPEN)-StopLevel*_Point-Spread*_Point)>Ask)
{
request.action = TRADE_ACTION_SLTP;
request.symbol = _Symbol;
request.sl = NormalizeDouble(sl,_Digits);
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request,result);
if(result.retcode==10009 || result.retcode==10008) // request executed
Print("Moving Stop Loss of Sell position #",request.order);
else
{
Print(ResultRetcodeDescription(result.retcode));
return;
}
return;
}
}
}
}

Related

CPU getting choked when below code is executed

public bool CheckIfValidSignedPdfExists(string fileServerPath, int memberagreementId)
{
var filePaths = Directory.GetFiles(fileServerPath);
if (filePaths.Length < 2)
{
return false;
}
var fileInfo = new List<FileInfo>();
foreach (var path in filePaths)
{
if (File.Exists(path))
fileInfo.Add(new FileInfo(path));
}
if (fileInfo.Count < 2)
return false;
else
{
fileInfo = fileInfo.OrderByDescending(x => x.LastWriteTimeUtc).Take(2).ToList();
//Rules: 1. Name 2.size 3. Last write time 4. Extension
var firstPdf = fileInfo.FirstOrDefault();
var lastPdf = fileInfo.LastOrDefault();
var firstFileNameCheck = string.Concat(Convert.ToString(memberagreementId), ".pdf");
var lastFileNameCheck = lastPdf.Name.StartsWith(Convert.ToString(memberagreementId) + "-")
&& lastPdf.Name.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase);
if ((firstPdf.Name.Equals(firstFileNameCheck, StringComparison.OrdinalIgnoreCase)) &&
(lastFileNameCheck) && (firstPdf.Length > lastPdf.Length) && (firstPdf.LastWriteTimeUtc >= lastPdf.LastWriteTimeUtc))
return true;
else
return false;
}
}
When this piece of code is running, we can see the cpu spike and the thread always relate to the above code.
HIGH CPU Thread Code
CPU Spike
We are not sure what wrong we are doing in this code which is causing this issue.

Troubles with for loop and Range

var colRange = getRange("Sheet1!G2:G200")
logger.log(colRange)
logger.log(colRange[0])
for(var i = 0; i < colRange.length; i++) {
if(activeCell.getColumn() == 7 && activeCell.getRow() == colRange[i] && ss.getActiveSheet().getName()=="Sheet1") {
newValue=e.value;
oldValue=e.oldValue;
if(!e.value) {
activeCell.setValue("");
}
else {
if (!e.oldValue) {
activeCell.setValue(newValue);
}
else {
activeCell.setValue(oldValue+', '+newValue);
}
}
}
}
Could anybody help with the for loop. Need it to check every row of column G to allow multiple drop down selections. If I replace colRange[i] with the specific row it does work. I assume I need to loop through each range G2, G3, G4, etc
Please explain what you are trying to
It's hard to figure out what you are trying to accomplish with you code but this is my best guess
function onEdit(e) {
const sh = e.range.getSheet();
if (e.range.columnStart == 7 && e.range.rowStart > 1 && sh.getName() == "Sheet1") {
if (!e.value) {
e.range.setValue("");//doesn't make sense it's already null
} else if (!e.oldValue) {
e.range.setValue(e.value);
} else {
e.range.setValue(oldValue + ', ' + newValue);
}
}
}

Order closing and opening very quickly unintendedly in MQL4 when using multiple criteria with 'OR' operator

I am trying to close my trades based on 2 separate types of criteria,
Regardless of what the profit is, close the trade after 1020 seconds.
If my import value changes from -1 to 1 or vice versa, close it regardless of any other variables.
So far, my code works fine with 1 criteria, however when I add 2 if statements, MT4 does this thing where it opens and closes the trades very quickly, within every half a second, it doesn't look like its working properly.
If somebody could let me know what's making it do it, I would be grateful.
My code is below,
//Global Variables
//int Value = ReadCSVFile and get cell value(Either -1 or 1)
int Ticket;
int TicketBuy;
datetime SwitchBuy
datetime SwitchSell
void OnTick()
{
//Open Trades based on Value from CSV and if No other buy orders open
if(Value == 1){
if(getBuyOrderCount(Symbol(),magic) < 1){
Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue);
SwitchBuy = TimeCurrent();
}}
if(Value == (-1)){
if(SellOrderCount(Symbol(),magic) < 1){
TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue);
SwitchSell = TimeCurrent();
}}
//Close Trades based on value in CSV file changing
if(Ticket > 0){
if(Value == (-1)||(TimeCurrent()>(SwitchBuy+60))){
CloseAllTradesBuy();
Ticket = 0;
Alert("Buy Trade Closed Because Probabilities Changed to -1!");
}
}
if(TicketSell > 0){
if(Value == 1||(TimeCurrent()>(SwitchBuy+60))){
CloseAllTradesSell();
TicketSell = 0;
Alert("Sell Trade Closed Because Probabilities Changed to 1!");
}
}
//Second Independent Criteria
if((SwitchBuy+1020) < TimeCurrent()){
if(OrderType() == OP_BUY){
CloseAllTradesBuy();
}
}
if((SwitchSell+1020) < TimeCurrent()){
if(OrderType() == OP_SELL){
CloseAllTradesSell();
}
}
}
//Functions to Close Trades
double CloseAllTradesBuy(){
for (int i =OrdersTotal(); i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
if(OrderType()==OP_BUY)
if (OrderSymbol()==Symbol())
{
OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
}
}
}
double CloseAllTradesSell(){
for (int i =OrdersTotal(); i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
if(OrderType()==OP_SELL)
if (OrderSymbol()==Symbol())
{
OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
}
}
}
change this code of close orders :
//Functions to Close Trades
double CloseAllTradesBuy(){
for (int i =OrdersTotal()-1; i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
}
}
}
double CloseAllTradesSell(){
for (int i =OrdersTotal()-1; i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
}
}
}

Ace Editor: Lock or Readonly Code Segment

Using the Ace Code Editor can I lock or make readonly a segment of code but still allow other lines of code to be written or edited during a session?
Here is the start of a solution:
$(function() {
var editor = ace.edit("editor1")
, session = editor.getSession()
, Range = require("ace/range").Range
, range = new Range(1, 4, 1, 10)
, markerId = session.addMarker(range, "readonly-highlight");
session.setMode("ace/mode/javascript");
editor.keyBinding.addKeyboardHandler({
handleKeyboard : function(data, hash, keyString, keyCode, event) {
if (hash === -1 || (keyCode <= 40 && keyCode >= 37)) return false;
if (intersects(range)) {
return {command:"null", passEvent:false};
}
}
});
before(editor, 'onPaste', preventReadonly);
before(editor, 'onCut', preventReadonly);
range.start = session.doc.createAnchor(range.start);
range.end = session.doc.createAnchor(range.end);
range.end.$insertRight = true;
function before(obj, method, wrapper) {
var orig = obj[method];
obj[method] = function() {
var args = Array.prototype.slice.call(arguments);
return wrapper.call(this, function(){
return orig.apply(obj, args);
}, args);
}
return obj[method];
}
function intersects(range) {
return editor.getSelectionRange().intersects(range);
}
function preventReadonly(next, args) {
if (intersects(range)) return;
next();
}
});
see it working in this fiddle: http://jsfiddle.net/bzwheeler/btsxgena/
The major working pieces are:
create start and end ace anchors which track the location of a 'readonly' portion as the document around it changes.
create a range to encapsulate the anchors
add a custom keyhandler to check if the current impending keypress will affect the readonly range and cancel it if so.
add custom paste/cut handlers to protect against right-click menu and browser menu cut/paste actions
You can do it by listening to the exec events:
// Prevent editing first and last line of editor
editor.commands.on("exec", function(e) {
var rowCol = editor.selection.getCursor();
if ((rowCol.row === 0) || ((rowCol.row + 1) === editor.session.getLength())) {
e.preventDefault();
e.stopPropagation();
}
});
Source: https://jsfiddle.net/tripflex/y0huvc1b/
I suggest something else easier and more reliable to prevent range to be modified (check it!)
var old$tryReplace = editor.$tryReplace;
editor.$tryReplace = function(range, replacement) {
return intersects(range)?null:old$tryReplace.apply(this, arguments);
}
var session = editor.getSession();
var oldInsert = session.insert;
session.insert = function(position, text) {
return oldInsert.apply(this, [position, outsideRange(position)?text:""]);
}
var oldRemove = session.remove;
session.remove = function(range) {
return intersects(range)?false:oldRemove.apply(this, arguments);
}
var oldMoveText = session.moveText;
session.moveText = function(fromRange, toPosition, copy) {
if (intersects(fromRange) || !outsideRange(toPosition)) return fromRange;
return oldMoveText.apply(this, arguments)
}
outsideRange = function (position) {
var s0 = range.start;
if (position.row < s0.row || (position.row == s0.row && position.column <= s0.column)) return true; // position must be before range.start
var e0 = range.end;
if (position.row > e0.row || (position.row == e0.row && position.column >= e0.column)) return true; // or after range.end
return false;
}
intersects = function(withRange) {
var e = withRange.end, s0 = range.start, s = withRange.start, e0 = range.end;
if (e.row < s0.row || (e.row == s0.row && e.column <= s0.column)) return false; // withRange.end must be before range.start
if (s.row > e0.row || (s.row == e0.row && s.column >= e0.column)) return false; // or withRange.start must be after range.end
return true;
}

Pass fees from one shipping method to another

I have a tricky shipping issue that I'm trying to work out. I have a custom extension that calculates the table rates for all of the domestic shipping. But for international, one type of product(category A) is a flat $35/product shipping fee and the rest of the products (categories B and C) are calculated by UPS and USPS. The only way I've been able to figure out how to properly calculate shipping if a customer purchases both types of products is to create a table rate for Category A, then pass it along to UPS/USPS as a handling fee. Is there a variable/method I can use for this process? I haven't yet found one.
As requested, here's my function:
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
// Cut out code where it adds up the categories and the number of items in each category
$rates = $this->getRates($request, $_categories);
if (!empty($rates))
{
$rateTypes = array();
foreach($rates as $rate)
{
$rateTypes[] = $rate['method_name'];
}
$rateTypes = array_unique($rateTypes);
$i=0;
// Create array to pass along to UPS/USPS, if needed
$tempCategories = $_categories;
foreach($rateTypes as $rateType)
{
$groupPrice = 0;
foreach($_categories as $category=>$catQty)
{
$rateExists = false;
$standardRate = 0;
foreach($rates as $rate)
{
$rateCats = explode(',',$rate['category_list']);
if(in_array($category,$rateCats) && $rate['method_name'] == $rateType )
{
$rateExists = true;
if($rate['condition_type'] == 'I'){
$ratePrice = $rate['price'] * $catQty;
}
else if ($rate['condition_type'] == 'O') {
$ratePrice = $rate['price'];
}
unset($tempCategories[$category]);
}
else if(in_array($category,$rateCats) && $rate['method_name'] == "Standard" && $rateType != "Standard")
{
if($rate['condition_type'] == 'I'){
$standardRate += $rate['price'] * $catQty;
}
else if ($rate['condition_type'] == 'O') {
$standardRate += $rate['price'];
}
unset($tempCategories[$category]);
}
}
if($rateExists == false)
{
$groupPrice += $standardRate;
}
else
$groupPrice += $ratePrice;
}
if(count($tempCategories) > 0)
{
// Figure out how to pass the $groupPrice to other shipping methods here
}
else {
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier('shippingcodes');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('shippingcodes_'.$rateType);
$method->setMethodTitle($rateType);
$method->setPrice($groupPrice);
$result->append($method);
}
}
}
else
return false;
return $result;
}

Resources