Jump to content
Medved Trader Forums
Crypto

Linking Paintbars on Intraday Chart

Recommended Posts

Hi forum

I'm new to Medved Trader (not new to trading).  I'm experimenting with the Intraday charts and have created a number of paintbars which are appearing on the chart.

I've looked at the YouTube tutorials and documentation and can't seem to find what I'd like to do.

Is it possible to link the paintbars so they only fire/appear on the intraday chart if the other paintbar conditions equate to true. For example I have a buy paintbar called b_p and a sell paintbar called s_p.  Can I set up the paintbars so 's_p' only appears after 'b_bp' appears and then once 's_p' appears 'b_bp' can trigger again ? Not sure if that's clear, currently on my intraday chart I have many consecutive 'buy' signal and 'sell' signals and I only want to see alternating signals buy sell buy sell buy sell etc

Cheers

Share this post


Link to post
Share on other sites

you cannot link one paintbar to another. However, you can basically implement the functionality of all the paintbars in question in one, using the Advanced mode. Then you can do conditional triggering or triggering differently for different events. Can also track the result of the paintbar from one bar to another.

Share this post


Link to post
Share on other sites

Hi Jerry

Do you mean something like TriggerAlert(String AlertName, String Message) ?

So if I had a Buy indicator and my conditions evaluated to true I would code something like this:

TriggerAlert('TradeAlert', 'BUY')

and then in my SELL indicator - how would I reference that alert - if TradeAlert = 'BUY' { //set another alert} ??

Alternatively can i use in the Simple paintbar editor set the Alert, Name it and create a message like below ? And how can I reference that alert in another paintbar ?

image.png.697adb5725eb5ef4614028b11562e9bb.png

Sorry for all the Q's, I'm struggling with this at the moment.

Crypto

Share this post


Link to post
Share on other sites

no, thats not it. You can combine Multiple Paintbars within one the way you indicated only for the most simplistic ones, where each original paintbar consists of only one rule. For anything more complex, you would want to switch to Advanced Mode, were you can write expressions in simplified C# syntax.

So using pseudo code, you could do something like:

Boolean condition1Triggered = false;
Boolean condition2Triggered = false;
if (some condition 1)
{
         ...TriggerAlert or set scan etc....
         condition1Triggered = true;
}
if (some condition 2 and condition1Triggered == true)
{
         ...TriggerAlert or set scan etc....
         condition2Triggered = true;
}
if (some condition 3 and (condition1Triggered == true or condition2Triggered == true))
{
         ...TriggerAlert or set scan etc....
}

In advanced mode, you can have variables, functions, complex conditions, etc.  Also, if you set the color in each of the different situations to a different value, you could reference the current color or the painbar, or previous values.

NOTE: the easiest thing to do is first define as much as you can in Simple mode, define each condition as a separate rule, even if they don't tie together.  Specify all items to be set on true condition - Color, Shape, Alert, Scan result.... then when everything is defined, switch to Advanced mode. That way MT will generate all the code for you so you don't have to start from scratch. You can then just copy/paste to put the pieces, conditions, etc, together.

Share this post


Link to post
Share on other sites

Thanks Jerry,

I made the mistake of thinking the simple paintbar conditions all made up a logical AND and realised they don't :) Ok thanks for the tip re adding the variables in simple editor first and then converting to advanced as it certainly makes things easier whilst I'm new to this app. I've created some paintbars using the advanced editor and they're behaving as expected.  I'll try the triggers now - I assume the triggers are visible only within the scope of the current paintbar and not in another paintbar I create ? That would be ideal for me so I can code a buy paintbar which sets a trigger and check if it's true in my sell paintbar.  I'll experiment now.

Cheers

Share this post


Link to post
Share on other sites

actually, you can do logical AND between two rules in simple mode. The rules are evaluated top down. if you do
RULE 1: if A < B then.... (do nothing)  ELSE and stop
RULE 2: if C < D then (put in stuff to trigger your scan or paintbar here)

That will be the same as doing the AND compare between rules 1 and 2, since for the trigger actions to occur, both A<B and C<D would have to be true. if rule 1 is false, then the "and stop" part would get executed, and rule 2 would never be checked.
 

Share this post


Link to post
Share on other sites

I'm trying Jerry ... so here's a simple example below.

gBuy1 is my boolean variable to indicate a buy

My first IF is my Buy rule - it works and appears on the chart

My second IF is my Sell rule - I check if gBuy1 is true and if my sell condition is true, set color/shape and clear my gBuy1 signal (set to false) so my first IF can be executed.

 My second IF never executes.  If I remove the boolean gBuy1 it executes and shows on my intraday however this is not what I want. Is the boolean defined (gBuy1) in this paintbar persistent every time period or am I doing something else wrong ? I'm close, I feel it :)

public void MainCalculation()
{
    Boolean gBuy1 = false;
    
    if ((!gBuy1) && (Close > Low[1]) )
    {
        SetColor(SysColor.Positive);
        SetShape(PBShape.ArrowRight);
        TriggerAlert("Buy01");
        gBuy1 = true;
        return;
    }
    
    if (gBuy1 && (Close < Close[1]) )
    {
        SetColor(SysColor.Bid);
        SetShape(PBShape.Star);
        gBuy1 = false;
        return;
    }
    
}

 

Edited by Crypto

Share this post


Link to post
Share on other sites

you have a "return" inside the first IF. so if the first condition is true, it never gets to the 2nd. remove the return statements.

Share this post


Link to post
Share on other sites

Thanks Jerry,

However that doesn't work for what I want to do.  I haven't been able to communicate what I'm trying to achieve.  If I remove the returns I get a combination of buy buy buy sell sell sell or buy sell. etc.

If t represents time I want to do this:

Buy t1 t2 t3 t4 Sell t4 t5 t6 t7 t8 Buy etc

In other words a Sell can't appear unless a Buy has occurred in the past and a Buy can't appear unless a Sell has occurred in the past.

Let's leave it at that for this thread, I appreciate your replies.

Cheers

Share this post


Link to post
Share on other sites

ok, you want to reference the values of the paintbar but in past bars. When you say "Sell can't appear unless a Buy has occurred", how far back do you check? or as far back as it goes? and I assume you mean the BUY has to be the last value for the Paintbar?

so if you have BUY.... nothing... SELL... nothing.... and now you check both a sell and a buy occurred before but the SELL was the last one... so if I am understanding you correctly, only the BUY condition can be triggered. Is that right?

Share this post


Link to post
Share on other sites

Maybe an example will explain better.

I have a rule (my Buy signal), my conditional logic, that I think will be an entry point for a long trade and I also have another rule that I want to use as my exit, ie when I want to sell.  My sell logic will hopefully tell me to either exit for a good profit or get out of a bad trade, my stop loss rule.

So let's say I enter a BTCUSD trade:

5/Feb I buy BTC @ 6912 USD

6th to 16th Feb my BUY rule may trigger/equate to TRUE again and again - but I don't care because I'm already in the trade.  The only rule/code I'm interested in now is my SELL rule.  My sell rule will either stop me out or tell me to exit on the 17th of Feb for a profit.  Now that I'm out of that trade, I now want to display the next BUY rule hit because I want to get into another trade so I don't care about my SELL rule right now because I'm not in a trade.

That's why I thought I needed 2 paintbars however I couldn't work out how I could determine in my SELL paintbar if the BUY had been triggered. Once a Buy is triggered, I don't want to see any more BUYs on my chart.

This is a daily example, however I want to do this on the Intraday chart. I've drawn Green for buy and Red for SELL.

image.png.58ccb27097e7bd1847065cb2355cb239.png

 

 

Share this post


Link to post
Share on other sites

so what you basically need to know is if you are in a trade or not in a trade.

The Advanced Mode can handle that but would need to add one more "advanced" step.  on the Paintbar window toolbar, click on the STATE tab and click on "Add State Keeping Code". that just adds a few functions to your paintbar. Initially all of them are just stubs - don't do anything.  Find this one:

private struct PaintbarState
{

}

and just add the declaration for variable InTrade, so it looks like this:

private struct PaintbarState
{
    public Boolean InTrade = false;
}

In the Paintbar Initialize function, add  InTrade = false; so it looks like this:

private void PaintbarInitialize()
{
    InTrade = false;
}

Now just change your MainCalculation Function to be :
 

public void MainCalculation()
{
    if (!CurrentState.InTrade && (Close > Low[1]) )
    {
        SetColor(SysColor.Positive);
        SetShape(PBShape.ArrowRight);
        TriggerAlert("Buy01");
        CurrentState.InTrade = true;
        return;
    }
    
    if (CurrentState.InTrade && (Close < Close[1]) )
    {
        SetColor(SysColor.Bid);
        SetShape(PBShape.Star);
        CurrentState.InTrade = false;
        return;
    }
}

Share this post


Link to post
Share on other sites

Yes, thanks that's exactly what I want to do and I understand the logic, here's the code as it stands:

I'm getting a compile error for this statement in the struct: public Boolean InTrade = false;

Error says: CS0573    'MT.Charting.RUNTIME.Paintbar_C_06.PaintbarState.InTrade': cannot have instance field initializers in structs

Googling this I'm reading you can't initialize in Structs maybe ?

If I try to remove the '=false' in the private struct PaintbarState I get the error CS0103 The name 'InTrade' does not exist in the current context

public void MainCalculation()
{
    if (!CurrentState.InTrade && (Close > Low[1]) )
    {
        SetColor(SysColor.Positive);
        SetShape(PBShape.ArrowRight);
        TriggerAlert("Buy01");
        CurrentState.InTrade = true;
        return;
    }
    
    if (CurrentState.InTrade && (Close < Close[1]) )
    {
        SetColor(SysColor.Bid);
        SetShape(PBShape.Star);
        CurrentState.InTrade = false;
        return;
    }
}


/// <summary>
/// Is called at start of paintbar calculation, should be used to initialize anything needed for the paintbar
/// </summary>
private void PaintbarInitialize()
{
    InTrade = false;
}

/// <summary>
/// Holds paintbar state - fill with variables needed to be preserved for next paintbar calc
/// </summary>
private struct PaintbarState
{
    public Boolean InTrade = false;
}

/// <summary>
/// Holds current PB state - use to calc PB, changes to it carry over to next PB calc
/// </summary>
private PaintbarState CurrentState;

/// <summary>
/// Holds saved PB state - internal
/// </summary>
private PaintbarState SavedState;

/// <summary>
/// Is called at start of paintbar calculation, should be used to clear the paintbar state
/// </summary>
private void PaintbarClearState()
{
    CurrentState = new PaintbarState();
}

/// <summary>
/// Saves paintbar state (called internally).
/// </summary>
private void PaintbarSaveState()
{
    SavedState = CurrentState;
}

/// <summary>
/// Restores paintbar state (called internally).
/// </summary>
private void PaintbarRestoreState()
{
    CurrentState = SavedState;
}

 

Share this post


Link to post
Share on other sites

I tried that initially however I was still getting an error in the Initialize section with

27, 5    CS0103    The name 'InTrade' does not exist in the current context    

So I deleted the InTrade = false; in PaintbarInitialize (assuming there is some sort of internal initialization anyway) and now I think it works :) will check thoroughly now.

So now I'm getting the desired behavior in terms of buy sell buy sell etc. Just need to work on my rules now :) : Thanks Jerry !!

image.png.cad87bf5a363cabb12448d44410a0981.png

Share this post


Link to post
Share on other sites

Yes that works - ty.  So here is the complete example working code if anyone is trying to do the same thing:

public void MainCalculation()
{
    if (!CurrentState.InTrade && (Close > Low[1]) )
    {
        SetColor(SysColor.Positive);
        SetShape(PBShape.ArrowRight);
        CurrentState.InTrade = true;
        return;
    }
    
    if (CurrentState.InTrade && (Close < Close[1]) )
    {
        SetColor(SysColor.Bid);
        SetShape(PBShape.Star);
        CurrentState.InTrade = false;
        return;
    }
}


/// <summary>
/// Is called at start of paintbar calculation, should be used to initialize anything needed for the paintbar
/// </summary>
private void PaintbarInitialize()
{
    CurrentState.InTrade = false;
}

/// <summary>
/// Holds paintbar state - fill with variables needed to be preserved for next paintbar calc
/// </summary>
private struct PaintbarState
{
    public Boolean InTrade;
    
}

/// <summary>
/// Holds current PB state - use to calc PB, changes to it carry over to next PB calc
/// </summary>
private PaintbarState CurrentState;

/// <summary>
/// Holds saved PB state - internal
/// </summary>
private PaintbarState SavedState;

/// <summary>
/// Is called at start of paintbar calculation, should be used to clear the paintbar state
/// </summary>
private void PaintbarClearState()
{
    CurrentState = new PaintbarState();
}

/// <summary>
/// Saves paintbar state (called internally).
/// </summary>
private void PaintbarSaveState()
{
    SavedState = CurrentState;
}

/// <summary>
/// Restores paintbar state (called internally).
/// </summary>
private void PaintbarRestoreState()
{
    CurrentState = SavedState;
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×