I have an EA that uses a strategy that involves at one point two trades going off at the same time.
Is it hedging?
I want to be able to set two different values for each trade using the same function.
What type of values?
I am using a simple assumed-loss martingale for testing purposes, and both trades stay the same value
Same as above .. what value?
I can show code if needed
Yeah, I'll recommend the code and some more clarification of what you are tying to accomplish.
//+------------------------------------------------------------------+ //| SSJ$FXv1.0.0.0 | //| Cash Turner | //| javascript:prompt() | //+------------------------------------------------------------------+ #define MAGICMA 6334613436373636 extern bool BAMA = true; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- if(OrdersTotal()==2)return; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { int orders=HistoryTotal(); // history orders total double lot =0.00; int risk = 0; double debt = 0; //---- calcuulate number of losses orders without a break if(BAMA==true) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- risk++; if(OrderProfit()<0)debt=debt+OrderProfit(); // Alert(debt); Alert(risk); //--- if(OrderProfit() > 0)break; if(risk==1) lot=0.01; if(risk==2) lot=MathAbs(debt*0.1); if(risk==3) lot=MathAbs(debt*0.1); if(risk==4) lot=MathAbs(debt*0.1); if(risk==5) lot=MathAbs(debt*0.1); if(risk==6) lot=MathAbs(debt*0.1); if(risk==7) lot=MathAbs(debt*0.1); if(risk==8) lot=MathAbs(debt*0.1); if(risk==9) lot=MathAbs(debt*0.1); if(risk==10)lot=MathAbs(debt*0.1); if(risk==11)lot=MathAbs(debt*0.1); if(risk==12)lot=MathAbs(debt*0.1); if(risk==13)lot=MathAbs(debt*0.1); if(risk==14)lot=MathAbs(debt*0.1); if(risk==15)lot=MathAbs(debt*0.1); if(risk==16)lot=MathAbs(debt*0.1); if(risk==17)lot=MathAbs(debt*0.1); if(risk==18)lot=MathAbs(debt*0.1); if(risk==19)lot=MathAbs(debt*0.1); } /*possibly another for loop??*/ } //---- return lot size if(lot<=0.01) lot=0.01; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ int CheckForOpen() { double ma; double ma2; int res; //---- get Moving Average ma=iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0); ma2=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0); //---- Sell Conditions. if(ma < ma2) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+0.0010,Bid-0.0010,"",MAGICMA,0,Red); return; } //---- Buy conditions if(ma > ma2) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-0.0010,Ask+0.0010,"",MAGICMA,0,Blue); return; } } //------------ int CheckForBalance() { int res; double ma; double ma2; double lots; //---MA's ma=iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0); ma2=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0); //---Enter conditions for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; //---- Turns out adding a buy is a horrible idea, I would have seriously never guessed this outcome! if(OrderType()==OP_BUY && ma > ma2) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+0.0010,Bid-0.0010,"",MAGICMA,0,Yellow); } } } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<10 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==1) CheckForBalance(); if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); //---- } //+------------------------------------------------------------------+The only code that seems to be capable of changing without crushing the results is the LotsOptimized() function. When it makes a buy and sell call at the same time, the risk loop does not increment so it ends up smashing my martingale. Is there a way to fix this while preserving most of the code?
you still havent explained exactly what it is your trying to change, you said you want two different values for each of the trades, are you saying you want two different lot sizes ?
you still havent explained exactly what it is your trying to change, you said you want two different values for each of the trades, are you saying you want two different lot sizes ?
I am trying to change the lot sizes while preserving the loops incrementation.
The hedge trade and the buy trade go off at the same time, and I think that's the reason why the loop doesn't double, but I could be wrong.
Does that make sense? I just don't want to make something too long that noone reads it =/
A) By using different Magic Numbers for the two trade types, would that help your looping?
B) I think that MAGICMA is WAY too big. I thought it was limited to between –2,147,483,648 to 2,147,483,647
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi!
I have an EA that uses a strategy that involves at one point two trades going off at the same time. At first it was just a place holder, then it turned out to be rather effective! I want to be able to set two different values for each trade using the same function.
I am using a simple assumed-loss martingale for testing purposes, and both trades stay the same value, which messes up the strat entirely.
Is there a way around this?
I can show code if needed. Or, can someone point me to articles that show this? The search function didn't seem to work at all when I tried it today. No jokes.
Thanks!!