Spready

User avatar
SteveHopwood
Owner
Posts: 9771
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Spready

Post by SteveHopwood »

SERIOUS WARNING
  • Most Forex traders lose all their money.
  • Using the robot posted here in trading Forex does not guarantee success.
  • Trading this robot could lead to serious financial loss.
  • Trading this robot without understanding its underlying trading strategies guarantees traders will lose their money.
  • This is not a set-and-forget ea; there is no such thing and anyone who tries to claim there is, is either stupid or lying. This ea requires frequent manual intervention.
  • At best, a trading robot is only 90% as good as the manual strategy it trades. At best. At worst, it can be much less effective. If the strategy is rubbish, so is the robot.
  • To trade this robot, you have to understand:
    • How to use EA's.
    • Hedging
    • Basket trading
MAKE SURE YOU READ THIS POST http://www.stevehopwoodforex.com/phpBB3 ... 41#p111241 There was a Forex bloodbath on Jan 15th 2015 that you need to know about.

I noticed this on an Afterprime demo:
Zero spread.PNG
Lots of zero spreads mean that simultaneous buy and sell orders can be placed at the same price. There were never more than 2 points in between the orders even when the timing did not quite work out.

Open a hedged pair of buy and sell orders. Place a grid of 10 buy and sell stops then wait for the market to do its thing. Treat each pair as an individual basket and close the orders at a set profit target. One side will eventually overwhelm the other even when a ranging market fills a number of stop orders on both sides.

I dropped the initial market orders after a while so here is what Spready does:
  • Wait for a zero spread event then:
    • Send a grid of x (default 10) stop orders x pips from the market(default 20).
    • Monitor the position awaiting the opportunity to close at the basket cash TP target. Set this target to lot size multiplied by 10, so the default lot size of 0.1 gives a $100 profit target.
There are no individual trade TP/SL although the inputs are there for anyone wanting to experiment with them.

This is an experiment and I cannot believe it has not been tried before; we shall have to try it and see. The demo is up 35% since starting and so looks promising.

I have included a couple of scripts to aid manual closing. The Nuclear Option script sets a flag during its operation, that Spready spots and waits for the flag to disappear before carrying out further operations.

Try Spready out and see if she works for you.




:xm: :rocket:

Matters of general interest
Go here to download and run the script that will fill your platform's missing chart history: http://www.stevehopwoodforex.com/phpBB3 ... f=15&t=254
Read this post:
http://www.stevehopwoodforex.com/phpBB3 ... p?f=6&t=78 Newbie traders, read this for inspiration and warning. Those of you who have suffered losses, read it for inspiration.

Masterly summary of hedging: http://www.forexfactory.com/showthread. ... ost4977179

EA coding
I receive may requests to code EA's. Traders, if I code your EA for free, then I will share it here. If you want me to code your EA without sharing, then my fee is $100 payable into my paypal account. I explain why at http://www.stevehopwoodforex.com/phpBB3 ... ?f=15&t=79, at the bottom of the post.

Useful utilities/EA's/scripts -

-
You do not have the required permissions to view the files attached to this post.
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
biobier
Trader
Posts: 72
Joined: Mon Mar 18, 2019 7:24 am

Re: Spready

Post by biobier »

Nice toy Steve.
I assume in a live account the slippage will make this not profitable but we shall see.
I suggest to use a OrderCloseby function to offset the initial Buy and Sell before the CloseAllTrades. This will save comission once.

Code: Select all

//CHeck if broker allows CloseBy
if((int)MarketInfo(Symbol(),MODE_CLOSEBY_ALLOWED)==1)
	multiple_closeby(symbol==AllSymbols?NULL:Symbol(),MagicNumber,true);
CloseAllTrades(AllTrades);

bool multiple_closeby(const string symbol=NULL, const int magic=NULL,bool sameLotOnly=false)
{
   for(int i=OrdersTotal()-1; i>0; --i){
      //first loop by all orders
      if(OrderSelect(i, SELECT_BY_POS)
         && OrderType() < 2
         && (symbol == NULL || OrderSymbol() == symbol)
         && (magic == NULL  || OrderMagicNumber() == magic)
      ){
         int    first_ticket = OrderTicket();
         string first_symbol = OrderSymbol();
         int    first_type   = OrderType();
         double first_lot    = OrderLots();
         double first_profit = OrderProfit();
         bool lookForFullBalance=true;
         for(int j=OrdersTotal()-1; j>=0; --j){
            //second loop to find suitable trade that has enough profit to blance this one out at least by 100%
            if(OrderSelect(j, SELECT_BY_POS)
               && OrderType() < 2
               && (magic == NULL || OrderMagicNumber() == magic)
               && OrderSymbol() == first_symbol
               && OrderType()   != first_type
               && OrderTicket() != first_ticket
               && (!sameLotOnly || first_lot==OrderLots())
            ){
               while(IsTradeContextBusy()) Sleep(10);
               if(!lookForFullBalance || (lookForFullBalance && first_profit*-1 <= OrderProfit())){
                  if(OrderCloseBy(first_ticket, OrderTicket()))
                     return multiple_closeby(symbol, magic,sameLotOnly);//new call as the orders situation changed
                  else
                     return false;
               }else if(lookForFullBalance && j<0){ //now look again for no full balance
                 lookForFullBalance=false;
                 j=OrdersTotal()-1;
              }
            }
         }//for
      }//if
   }//for
   return true;

Must-reads for FOREX NOOBS as me:
Help for Newbies.
Information For Beginners
User avatar
SteveHopwood
Owner
Posts: 9771
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Spready

Post by SteveHopwood »

biobier wrote: Fri Sep 22, 2023 10:25 am Nice toy Steve.
I assume in a live account the slippage will make this not profitable but we shall see.
I suggest to use a OrderCloseby function to offset the initial Buy and Sell before the CloseAllTrades. This will save comission once.
Thanks biobier. I tried adding the code but the compilation fails with errors at the two highlighted bits:
multiple_closeby(symbol==AllSymbols?NULL:Symbol(),MagicNumber,true);

Am I missing a couple of declarations?

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
biobier
Trader
Posts: 72
Joined: Mon Mar 18, 2019 7:24 am

Re: Spready

Post by biobier »

SteveHopwood wrote: Sat Sep 23, 2023 2:30 pm
Thanks biobier. I tried adding the code but the compilation fails with errors at the two highlighted bits:
multiple_closeby(symbol==AllSymbols?NULL:Symbol(),MagicNumber,true);

Am I missing a couple of declarations?

:xm: :rocket:
Sorry this was copied from a multipair EA, for spready the call can be simply

Code: Select all

multiple_closeby(Symbol(),MagicNumber,true);
Must-reads for FOREX NOOBS as me:
Help for Newbies.
Information For Beginners
thomasmore
Trader
Posts: 269
Joined: Tue Nov 15, 2011 10:35 pm
Location: Knokke, Belgium

Re: Spready

Post by thomasmore »

Hi Steve,
The EA made its first profits !
However, if I change the DistanceBetweenTradePips from the default 20 to something else, that change is ignored.
Best regards,
Ludo
Spready.png
You do not have the required permissions to view the files attached to this post.
User avatar
SteveHopwood
Owner
Posts: 9771
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Spready

Post by SteveHopwood »

thomasmore wrote: Tue Sep 26, 2023 7:58 am Hi Steve,
The EA made its first profits !
However, if I change the DistanceBetweenTradePips from the default 20 to something else, that change is ignored.
Best regards,
Ludo
Spready.png
Thanks Ludo. I had not noticed this. I had inadvertently left two lines of price incrementing in the code so that the price for the next stop order was calculated twice. The default of 20 means that the stop orders are being placed 40 pips apart. Yours are being placed 10 pips apart. Fixed in post 1.

The DIY is easy.

Go to void sendOrders() and replace the bprice and sprice declarations with:
double bprice = NormalizeDouble(Ask + (DistanceBetweenTrades / factor), Digits);
double sprice = NormalizeDouble(Bid - (DistanceBetweenTrades / factor), Digits);

Go to void SendBuyGrid(string symbol, int type, double price, double lot) and comment out/delete this line of code:
price = NormalizeDouble(price + (DistanceBetweenTrades / factor), Digits);

Go to void SendSellGrid(string symbol, int type, double price, double lot) and comment out/delete this line of code:
price = NormalizeDouble(price - (DistanceBetweenTrades / factor), Digits);

I will add biobier's contribution later this week.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9771
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Spready

Post by SteveHopwood »

V 1a is in post 1, with biobier's OrderCloseBy code included. Thanks again for a wonderful contribution biobier.

DIY involves some copy/pasting.

Navigate to void lookForBasketClosure(). Replace the function with this:

Code: Select all

void lookForBasketClosure()
{

   bool closeNeeded =false;

   if (CashUpl >= CashProfitTarget)
   {
      closeNeeded = true;
      //CloseAllTrades(AllTrades);
      //return;
   
   }//if (CashUpl >= CashProfitTarget)
   
   //Exit the function if no closure required.
   if (!closeNeeded)
      return;
      
   //biobier supplied this code and the multiple_closeby() function. Many thanks, biobier
   //CHeck if broker allows CloseBy
   if((int)MarketInfo(Symbol(),MODE_CLOSEBY_ALLOWED)==1)
   	multiple_closeby(Symbol(),MagicNumber,true);
   CloseAllTrades(AllTrades);

      

}//End void lookForBasketClosure()
Underneath the function, paste this function:

Code: Select all

bool multiple_closeby(const string symbol=NULL, const int magic=NULL,bool sameLotOnly=false)
{
   for(int i=OrdersTotal()-1; i>0; --i){
      //first loop by all orders
      if(BetterOrderSelect(i, SELECT_BY_POS)
         && OrderType() < 2
         && (symbol == NULL || OrderSymbol() == symbol)
         && (magic == NULL  || OrderMagicNumber() == magic)
      ){
         int    first_ticket = OrderTicket();
         string first_symbol = OrderSymbol();
         int    first_type   = OrderType();
         double first_lot    = OrderLots();
         double first_profit = OrderProfit();
         bool lookForFullBalance=true;
         for(int j=OrdersTotal()-1; j>=0; --j){
            //second loop to find suitable trade that has enough profit to blance this one out at least by 100%
            if(BetterOrderSelect(j, SELECT_BY_POS)
               && OrderType() < 2
               && (magic == NULL || OrderMagicNumber() == magic)
               && OrderSymbol() == first_symbol
               && OrderType()   != first_type
               && OrderTicket() != first_ticket
               && (!sameLotOnly || first_lot==OrderLots())
            ){
               while(IsTradeContextBusy()) Sleep(10);
               if(!lookForFullBalance || (lookForFullBalance && first_profit*-1 <= OrderProfit())){
                  if(OrderCloseBy(first_ticket, OrderTicket()))
                     return multiple_closeby(symbol, magic,sameLotOnly);//new call as the orders situation changed
                  else
                     return false;
               }else if(lookForFullBalance && j<0){ //now look again for no full balance
                 lookForFullBalance=false;
                 j=OrdersTotal()-1;
              }
            }
         }//for
      }//if
   }//for
   return true;

}//End bool multiple_closeby
Make sure that you copy the code from this post. biobier's copying missed the final brace.

Fixing the bloop that Ludo spotted has made a difference so far - 1 basket closed so far and the entire position mostly in draw up rather than our usual DD. :lol:

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
fiox
Trader
Posts: 147
Joined: Wed Feb 18, 2015 6:16 pm
Location: Italy

Re: Spready

Post by fiox »

Thanks Steve, giving it a chance to see how it works. At the moment the only limit I see is when both long/short sides manage to trigger all the orders, returning to a situation of disadvantageous equilibrium. Perhaps the situation is "easily resolved" by increasing the number of orders, if it comes to that case.
User avatar
SteveHopwood
Owner
Posts: 9771
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Spready

Post by SteveHopwood »

fiox wrote: Fri Sep 29, 2023 2:22 pm Thanks Steve, giving it a chance to see how it works. At the moment the only limit I see is when both long/short sides manage to trigger all the orders, returning to a situation of disadvantageous equilibrium. Perhaps the situation is "easily resolved" by increasing the number of orders, if it comes to that case.
I suggest that we worry about what to do if the situation arises. There would need to be a monumentally ranging market using my defaults for this to happen.

It could be different when users try lower distances between the orders as Ludo is - 10 in his case. It would not take long to add code that detects when all of the stop orders have filled and then adds a further grid. We shall see.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
fiox
Trader
Posts: 147
Joined: Wed Feb 18, 2015 6:16 pm
Location: Italy

Re: Spready

Post by fiox »

As promised I started some demos 1 month ago, with different settings and cross. Below are the results to date, no manual intervention. Summary: closed trades. Exposure: open trades

The results are interesting. As I expected, the crosses with low ADR fail to perform decently, in fact practically all of them with the exception of GBP/JPY and EUR/AUD. XAU/USD is often in profit, even if some movements occurred during news and therefore on a real account it would be very different, deserves attention, I will try to analyze the operations in detail

From next week I will vary the crosses, leaving only those with large ADR, and probably some exotics.
Let's see how it goes :)

Attaching the results for possible comparison, open to ideas
You do not have the required permissions to view the files attached to this post.
Post Reply

Return to “Automated trading systems”