Hi
Can anybody please help me!
I want to write code to multiply the Lots every time A trade has stopped at stop loss and to reset the Lots if a trade has stopped at take profit.
I am new to the programming language and are struggling
Please help
Hi
Can anybody please help me!
I want to write code to multiply the Lots every time A trade has stopped at stop loss and to reset the Lots if a trade has stopped at take profit.
I am new to the programming language and are struggling
Please help
if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)){
if(OrderProfit() < 0)My_Lots = OrderLots * My_Lot_Increment;
else My_Lots = Original_Lots;
}
thanks I will give it a try!!!!
Joe
Hi Kennyhubbard
It dose not work if I test the it it only increment it ones and then it stays there and even with a profit it stays on the incremented value and dose not reset.
thanks for the help
Hi joe,
There are a million places where this could go wrong. The code I posted was very simple and broad based. I am assuming that you are keeping it really simple, so you don't plan to trade this with other EA's, etc.....
There are one or two problems in the above code. The OrderLots should read OrderLots()
Anyway, if you post the code, I may be able to help you further.
Here is the code!! If I have lost 1 it must multiply Lots with 2. if I lost 2 it must multiply Lots with 4. If I lost 3 it must multiply Lots with 8......and so on. If I had a win at any stage between it must reset the Lots to 0.1 untill a lost and remultiply it again.
thanks for the help. I think if it works it is going to be a profitable EA. The gamblers use this system to win on roulette to win on Black or Red. I think if I can combine it with forex it will be profitable. It makes
thanks
Joe
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 0.1;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 10;
extern bool UseTakeProfit = True;
extern int TakeProfit = 10;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;
int BarCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
if (EachTickMode) Current = 0; else Current = 1;
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
double Buy1_1 = iADX(NULL, 0, 6, PRICE_CLOSE, MODE_MAIN, Current + 0);
double Buy1_2 = iADX(NULL, 0, 5, PRICE_CLOSE, MODE_MAIN, Current + 0);
double Sell1_1 = iADX(NULL, 0, 6, PRICE_CLOSE, MODE_MAIN, Current + 0);
double Sell1_2 = iADX(NULL, 0, 5, PRICE_CLOSE, MODE_MAIN, Current + 0);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
//Check position
bool IsTrade = False;
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (Buy1_1 > Buy1_2) Order = SIGNAL_BUY;
if (Sell1_1 < Sell1_2) Order = SIGNAL_SELL;
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
if (!EachTickMode) BarCount = Bars;
return(0);
}
Last edited by joepayne; 05-27-2010 at 09:19.
The code I showed you earlier has a bug............here is the correct code.........
The system that you are referring to is a martingale system and there is a lot of debate surrounding the use of these systems. I know which side of the fence I sit on this one, but its your money......personally I think you have a better chance at the roulette.PHP Code:double Get_Lots()
{
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)){
if(OrderProfit() < 0)double lLots = OrderLots() * Lot_Increment;
else lLots = Lots;
}
else lLots = Lots;
return(NormalizeDouble(lLots, LotDigits));
}
Last edited by kennyhubbard; 05-27-2010 at 12:28.
Thanks for the help. I just need to fine tune it. then I hope it works.
Thanks again
Joe
Thanks kennyhubbard
it works great. now all I need is if it has lost 3 or 4 times it must also reset the Lots to 0.1
Joe
Just add the extra line to limit the max lots that you desire. You need to define Max_Lots at the beginning of the program, probably an extern variable is best.PHP Code:double Get_Lots()
{
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)){
if(OrderProfit() < 0)double lLots = OrderLots() * Lot_Increment;
else lLots = Lots;
}
else lLots = Lots;
if(lLots > Max_Lots)lLots = Lots;//<<<<<<<<<<<<<<<<<<<<<<<<<<<
return(NormalizeDouble(lLots, LotDigits));