molanis.com/forum - Tools for MetaTrader : The place to ask about the best expert advisor builder, expert advisor downloads, and expert advisor programming

AFTER YOU REGISTER SEND US AN EMAIL TO ACTIVATE YOUR ACCOUNT - Before posting:-1- Please read the user guide -2- Try the examples -3- Search in the forum
It is currently Sat May 11, 2024 12:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Here's the code to call your EA's last trade price:
PostPosted: Sun Oct 21, 2012 9:32 pm 
Offline

Joined: Sun Jun 19, 2011 9:38 pm
Posts: 270
This can be useful for limiting your Martingale orders to lastOrderPrice +/- something... like 50 pips for example, in your trading conditions. Obviously, this is for SB Pro users.

First, declare 3 new variables in your global variables like so:
Code:
double  lastOrderPrice;
double  mPipBuffer;
extern int MartingalePips=50;

Note that extern creates a new adjustable EA input!

Then, put the following code under:
int start()
Code:
for (int i=OrdersTotal()-1; i>=0; i--)
  {
    if ( OrderSelect(i,SELECT_BY_POS, MODE_TRADES) )
      {
        if (   OrderType() == ( OP_SELL || OP_BUY ) 
           &&  OrderSymbol() == Symbol() 
           &&  OrderMagicNumber() == ( MagicNumberShort || MagicNumberLong ) )

               lastOrderPrice = OrderOpenPrice();
      }
  }

mPipBuffer = MartingalePips * PipMultiplier * Point;


Of course, the last step is adding && iClose(Symbol(),0,0)>=lastOrderPrice+mPipBuffer to your BUY condition for example. I trust the admins will tell me if I have it wrong. Hopefully this will inspire others to post their hardcode mod's as well.

_________________
I'm not a programmer, but I play one on TV.


Top
 Profile  
 
 Post subject: Re: Here's the code to call your EA's last trade price:
PostPosted: Mon Oct 22, 2012 9:45 am 
Offline
Site Admin

Joined: Fri Oct 16, 2009 3:40 pm
Posts: 451
Thanks. Great code.
We are working on a function that does the same but uses the pair as an input so you can get the last traded price per pair.

For code like this:
for (int i=OrdersTotal()-1; i>=0; i--)

I like to write it like this:
int variablename=OrdersTotal();
for (int i=variablename-1; i>=0; i--)


Top
 Profile  
 
 Post subject: Re: Here's the code to call your EA's last trade price:
PostPosted: Mon Oct 22, 2012 10:50 am 
Offline

Joined: Sun Jun 19, 2011 9:38 pm
Posts: 270
Very nice. I'll be doing backflips when the new version comes out. :D

Let's keep this train rolling. For any risk junkies out there, put this in your globals:
Code:
extern bool    whileOpen = true;    // use martingale while positions are open--can stack floating P/L--Created by User


And this in with the afterLoss and afterWin conditions:
Code:
      if (whileOpen) {
         if ((CalculateOpenPositions(Symbol(),OP_SELL||OP_BUY,MagicNumberShort||MagicNumberLong))<MaxNumberofPositions) {
            lot=NormalizeDouble(OrderLots()*MartingaleMultiplier,AccDigits);
            }
         }


It's designed to stack open trades--longs and shorts. Note that the open trades can be losing or winning (P/L is floating), so build your // trading conditions using +/-MartingalePips as appropriate. To separate long stacking from short stacking, create 2 new variables and break apart the CalculateOpenPositions string to build 2 new conditions.

_________________
I'm not a programmer, but I play one on TV.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by Molanis © 2009