NexusFi: Find Your Edge


Home Menu

 





Martingale code problem


Discussion in MultiCharts

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




 
Search this Thread

Martingale code problem

  #1 (permalink)
 th3web 
Rome Italy
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Clearing with CQG Data Feed
Trading: Futures
Posts: 3 since Apr 2021
Thanks Given: 25
Thanks Received: 1

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 NexusFi mobile app

Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
How to apply profiles
Traders Hideout
Trade idea based off three indicators.
Traders Hideout
Better Renko Gaps
The Elite Circle
 
  #2 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853

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 NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
 th3web 
Rome Italy
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Clearing with CQG Data Feed
Trading: Futures
Posts: 3 since Apr 2021
Thanks Given: 25
Thanks Received: 1


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
  #4 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853


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 NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on January 27, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts