Eddiedewaal: Any ideas on how to do this?
|
WHRoeder:
Eddiedewaal: Any ideas on how to do this? |
I dont expect you to code it for me. Ill attach what I have so far. IMy idea was along the same lines aswell. Just cant seem to get it right
//+------------------------------------------------------------------+ //| open trade test 2.mq4 | //| Copyright 2015, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ extern int TakeProfit = 100; extern int StopLoss = 100; extern int direction = 1; //+------------------------------------------------------------------+ //| Open first trade | //+------------------------------------------------------------------+ void OnInit() { //--- double TakeProfitLevelbuy; double StopLossLevelbuy; double TakeProfitLevelsell; double StopLossLevelsell; //---------------------------------------------------- TakeProfitLevelbuy = Bid + TakeProfit*Point; StopLossLevelbuy = Ask - StopLoss*Point; TakeProfitLevelsell = Ask - TakeProfit*Point; StopLossLevelsell = Bid + StopLoss*Point; if (direction == 0) { OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Sell Trade", 11111,Blue); } else if (direction == 1) { OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue); } //-------------------------------------------------------------- } //__________________________________________________________________________________________________________________________________________ //+------------------------------------------------------------------+ //|Open next trade | //+------------------------------------------------------------------+ void OnTick() { double direction1; double TakeProfitLevelbuy; double StopLossLevelbuy; double TakeProfitLevelsell; double StopLossLevelsell; TakeProfitLevelbuy = Bid + TakeProfit*Point; StopLossLevelbuy = Ask - StopLoss*Point; TakeProfitLevelsell = Ask - TakeProfit*Point; StopLossLevelsell = Bid + StopLoss*Point; if (AccountMargin() > 0) { return; } if (OrderType()== 0 && OrderProfit() > 0) { direction1 = 0; } else if(OrderType()==0 && OrderProfit() < 0) { direction1 = 1; } else if (OrderType()== 1 && OrderProfit() > 0) { direction1 = 1; } else if(OrderType()==1 && OrderProfit() < 0) { direction1 = 0; } //-------------------------------------------------------------------- if (direction1 == 0) { { OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue); } } else if(direction1 == 1) { OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue); } if (direction1 == 0) { { Alert("direction1 = 0"); } } else if(direction1 == 1) { Alert("direction1 = 1"); } return; }
as it is now it only opens buy positions, cant figure out why
//+------------------------------------------------------------------+ //| open trade test 2.mq4 | //| Copyright 2015, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ extern int TakeProfit = 100; extern int StopLoss = 100; extern int direction = 1; //+------------------------------------------------------------------+ //| Open first trade | //+------------------------------------------------------------------+ void OnInit() { //--- double TakeProfitLevelbuy; double StopLossLevelbuy; double TakeProfitLevelsell; double StopLossLevelsell; //---------------------------------------------------- TakeProfitLevelbuy = Bid + TakeProfit*Point; StopLossLevelbuy = Ask - StopLoss*Point; TakeProfitLevelsell = Ask - TakeProfit*Point; StopLossLevelsell = Bid + StopLoss*Point; if (direction == 0) { OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Sell Trade", 11111,Blue); } else if (direction == 1) { OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue); } //-------------------------------------------------------------- } //__________________________________________________________________________________________________________________________________________ //+------------------------------------------------------------------+ //|Open next trade | //+------------------------------------------------------------------+ bool OnTick() { double direction1; double TakeProfitLevelbuy; double StopLossLevelbuy; double TakeProfitLevelsell; double StopLossLevelsell; TakeProfitLevelbuy = Bid + TakeProfit*Point; StopLossLevelbuy = Ask - StopLoss*Point; TakeProfitLevelsell = Ask - TakeProfit*Point; StopLossLevelsell = Bid + StopLoss*Point; if (AccountMargin() > 0) { return; } bool getlastorder() { for(int i=OrdersTotal(); i<0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_HISTORY ); if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()<0) { OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue); } else if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()>0) { OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue); } if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()<0) { OrderSend(Symbol(),OP_BUY,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue); } else if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()>0) { OrderSend(OrderType()==OP_SELL,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue); } } }
Eddiedewaal: I dont expect you to code it for me. Ill attach what I have so far. IMy idea was along the same lines aswell. Just cant seem to get it right |
|
for(int i=OrdersTotal(); i<0; --i) { OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()>0) { return(1); } if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()<0) { return(0); } if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()>0) { return(0); } if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()<0) { return(1); }
Your last posed coded returns 0/1 for the highest positioned order in history. Do not assume history is sorted.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi
I am trying to write a expert advisor code that will open a new trade in the same direction as the previous if it was profitable and change direction if it was not.
Any ideas on how to do this?
Thanks