Martingale code problem - futures io
futures io



Martingale code problem


Discussion in MultiCharts

Updated
    1. trending_up 356 views
    2. thumb_up 2 thanks given
    3. group 4 followers
    1. forum 3 posts
    2. attach_file 0 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Martingale code problem

(login for full post details)
  #1 (permalink)
 th3web 
Rome Italy
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Clearing with CQG Data Feed
Trading: Futures
 
Posts: 3 since Apr 2021
Thanks: 20 given, 1 received

I wrote this simple code to have a MACD signal but with a martingale type Money management. It works quite well but I can't fix a problem. When the bot calculates the signal it is as if it divides the long and short traders and creates 2 separate martingales. I would like the martingale to be unique. How can I solve?


Inputs:

FastLength( 12 ), SlowLength( 26 ), MACDLength( 9 ),

double MM_Martingale_Start(1),

double MM_Martin_PF(1),

double MM_Martin_LF(2),

bool MM_Martin_RestartP(true),

bool MM_Martin_RestartL(false);





Vars:

var0( 0 ), var1( 0 ), var2( 0 ),

IntrabarPersist bool Filter(true), //used just before sending order

IntrabarPersist double PriceBuyMarket(0), //order open price

IntrabarPersist double PriceSellMarket(0), //order open price

double ret(0);



var0 = MACD( Close, FastLength, SlowLength ) ;

var1 = XAverage( var0, MACDLength ) ;

var2 = var0 - var1 ;



begin

ret = MM_Martingale_Start;

if PositionProfit(1) > 0 and not MM_Martin_RestartP then

ret = MaxContracts(1) * MM_Martin_PF

else if PositionProfit(1) < 0 and not MM_Martin_RestartL then

ret = MaxContracts(1) * MM_Martin_LF;

end;







//Open Buy Order

condition1 = CurrentBar > 2 and var2 crosses over 0 ;

if condition1

then

begin

Filter = true;

if Filter then

begin

PriceBuyMarket = Close;

Buy ("Long") ret contracts next bar at market;

end;

end;



//Open Sell Order

condition1 = CurrentBar > 2 and var2 crosses under 0 ;

if condition1

then

begin

Filter = true;

if Filter then

begin

PriceSellMarket = Close;

Sell Short ("Short") ret contracts next bar at market;

end;

end;



Sent using the futures.io mobile app

Started this thread Reply With Quote
The following user says Thank You to th3web for this post:

Can you help answer these questions
from other members on futures io?
Tick counter that works with directions?
NinjaTrader
Take Profit/ Tpx during crypto futures trading
Crypto Futures
Emini Trading Levels
Emini and Emicro Index
 
 
(login for full post details)
  #2 (permalink)
 vmodus 
Legendary Systematic Algo Trader
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: IB
Trading: Energies, Grains, Fixed Income
 
vmodus's Avatar
 
Posts: 1,159 since Feb 2017
Thanks: 2,685 given, 2,499 received

It is just a thought, but have you tried splitting this into a long and short signal (strategy)? Just add both signals to your chart and that should do it, I think.

Let me know if you need more details.

~vmodus

Enjoy everything!
Follow me on Twitter Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to vmodus for this post:
 
(login for full post details)
  #3 (permalink)
 th3web 
Rome Italy
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Clearing with CQG Data Feed
Trading: Futures
 
Posts: 3 since Apr 2021
Thanks: 20 given, 1 received


The problem is opposite...Currently with the signals together the martingale behaves as if the long and short signals were separate. I would need the martingale to work for both signals at the same time. If I have a losing long signal and the next signal is not a long but a short I still need to apply the martingale factor. Now if I have a losing long signal the martingale factor is applied to the next long signal and not to the next one (both long and short).

Started this thread Reply With Quote
 
(login for full post details)
  #4 (permalink)
 vmodus 
Legendary Systematic Algo Trader
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: IB
Trading: Energies, Grains, Fixed Income
 
vmodus's Avatar
 
Posts: 1,159 since Feb 2017
Thanks: 2,685 given, 2,499 received


th3web View Post
The problem is opposite...Currently with the signals together the martingale behaves as if the long and short signals were separate. I would need the martingale to work for both signals at the same time. If I have a losing long signal and the next signal is not a long but a short I still need to apply the martingale factor. Now if I have a losing long signal the martingale factor is applied to the next long signal and not to the next one (both long and short).

Okay, I understand better. I am having difficulty visualizing this, so maybe you can diagram this in a process/program flowchart, which may help your thinking. Working through the code, you might need to add logic for Martingale short and long, or just keeping track of which direction you are going.

You code makes me think you are pyramiding, not applying Martingale. It looks like you are adding to a losing position (adding an additional contract/share). Adding a position in the other direction will close the current position. Martingale, as I understand it, applies to closed trades. So you need to track the last trade, both direction and whether it was a profit or loss.

Again, if I were you, I would flowchart this on paper or use a tool like draw.io / diagrams.net (https://app.diagrams.net/. This is what I use, and it usually helps me sort out flawed logic.

Here is a link to one of my systems; refer to section 6: Build, where you can view a flowchart of that system:
https://systematicalgotrader.com/2022/01/15/trading-idea-026-rsi-hann-1/

~vmodus

Enjoy everything!
Follow me on Twitter Visit my futures io Trade Journal Reply With Quote


futures io Trading Community Platforms and Indicators MultiCharts > Martingale code problem


Last Updated on January 27, 2022


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
     



Copyright © 2022 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts