Hello everyone. I have a concept I would like to automate and have made an attempt at doing so. However, I need the smarter programmers here to point me in the right direction so that I can make this work as intended.
Here is the logic at a high level.
The EA does nothing while there are no trades on the chart. You (the trader) will manually open a trade.
The EA would come alive once it sees that a new trade has been opened on the chart.
It will figure out if it was a buy or a sell. (it does the same thing but opposite for buy vs sell)
Say it is a BUY for this example. Then the EA will open one pending buy X# pips above the current open trade for Y# of lots. From here one of two things can happen:
Option One: The price moves against the open trade. The open trade gets stopped out. => In that case, close the pending order and go back to sleep.
Option Two: The price moves in favor and the pending order is triggered. => In that case: Open a new pending order with the same parameters as before (the EA will open a pending buy X# pips above the current open trade for Y# of lots.)
This continues endlessly. (I have separate code to close trades)
Here is my code: Please let me know what you would change or do differently. I am not a programmer and very open to suggestions.
--- THANK YOU!!! ---
Here is the logic at a high level.
The EA does nothing while there are no trades on the chart. You (the trader) will manually open a trade.
The EA would come alive once it sees that a new trade has been opened on the chart.
It will figure out if it was a buy or a sell. (it does the same thing but opposite for buy vs sell)
Say it is a BUY for this example. Then the EA will open one pending buy X# pips above the current open trade for Y# of lots. From here one of two things can happen:
Option One: The price moves against the open trade. The open trade gets stopped out. => In that case, close the pending order and go back to sleep.
Option Two: The price moves in favor and the pending order is triggered. => In that case: Open a new pending order with the same parameters as before (the EA will open a pending buy X# pips above the current open trade for Y# of lots.)
This continues endlessly. (I have separate code to close trades)
Here is my code: Please let me know what you would change or do differently. I am not a programmer and very open to suggestions.
--- THANK YOU!!! ---
Inserted Code
//+------------------------------------------------------------------+
//| LeverageIn001.mq4 |
//| Copyright 2018, Platanero |
//| http://www.forexfactory.com/platanero |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Platanero"
#property link "http://www.forexfactory.com/platanero"
#property version "1.00"
#property strict
//--- input parameters
input double Lots=0.01;
input int StopLoss=50;
input int Distance=30;
input int Magic=39573;
double PipPoint;
bool paramsSet;
bool sellonly;
bool buyonly;
bool pendingOpened;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if (Digits < 4) PipPoint = 0.01;
else PipPoint = 0.0001;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(paramsSet==true && OrdersTotal()==0){ // if this condition is met, it because there used to be an open order but was stopped out. Cancel pending.
for (int a=0;a<OrdersTotal();a++)
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
{
OrderDelete(OrderTicket());
}
}
if(OrdersTotal()==0){ //// If this condition is met, it is because there are no open orders at this time. All params are set to false.
paramsSet=false;
buyonly=false;
sellonly=false;
}
if (OrdersTotal() > 0 && paramsSet!=true){ ///if this condition is met, there is an open order.
for (int a=0;a<OrdersTotal();a++)
{
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
//// if you find a buy order was opened, set params as such:
if (OrderType()==OP_BUY && OrderCloseTime()==0)
{
buyonly=true;
sellonly=false;
paramsSet=true;
}
//// if you find a sell order was opened, set params as such:
if(OrderType()==OP_SELL && OrderCloseTime()==0)
{
buyonly=false;
sellonly=true;
paramsSet=true;
}
}
}
if (buyonly=true && pendingOpened!=true){
for (int a=0;a<OrdersTotal();a++)
{
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
//// if you find a PENDING order was placed, exit. Otherwise, open one.
if (OrderType()==OP_BUYSTOP && OrderCloseTime()==0) return;
else
{
int ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,(GetAsk()+(Distance*PipPoint)),3,StopLoss,0,"My order",Magic,0,clrGreen);
pendingOpened=true;
}
}
}
if (sellonly=true && pendingOpened!=true){
for (int a=0;a<OrdersTotal();a++)
{
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
//// if you find a PENDING order was placed, exit. Otherwise, open one.
if (OrderType()==OP_SELLSTOP && OrderCloseTime()==0) return;
else
{
int ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,(GetBid()-(Distance*PipPoint)),3,StopLoss,0,"My order",Magic,0,clrGreen);
pendingOpened=true;
}
}
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetBid()
{
return(NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetAsk()
{
return(NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits));
} Don't look down when learning to fly.